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

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

12 13 14 15 16 17 18 19 20 21 22 23 24 25
#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

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

B
bellard 已提交
33 34 35 36
void raise_exception(int tt)
{
    env->exception_index = tt;
    cpu_loop_exit();
37
}
B
bellard 已提交
38

B
blueswir1 已提交
39
void helper_trap(target_ulong nb_trap)
40
{
B
blueswir1 已提交
41 42 43 44 45 46 47 48 49 50 51 52
    env->exception_index = TT_TRAP + (nb_trap & 0x7f);
    cpu_loop_exit();
}

void helper_trapcc(target_ulong nb_trap, target_ulong do_trap)
{
    if (do_trap) {
        env->exception_index = TT_TRAP + (nb_trap & 0x7f);
        cpu_loop_exit();
    }
}

53
void helper_check_ieee_exceptions(void)
B
blueswir1 已提交
54 55 56 57 58
{
    target_ulong status;

    status = get_float_exception_flags(&env->fp_status);
    if (status) {
B
blueswir1 已提交
59
        /* Copy IEEE 754 flags into FSR */
B
blueswir1 已提交
60
        if (status & float_flag_invalid)
B
blueswir1 已提交
61
            env->fsr |= FSR_NVC;
B
blueswir1 已提交
62
        if (status & float_flag_overflow)
B
blueswir1 已提交
63
            env->fsr |= FSR_OFC;
B
blueswir1 已提交
64
        if (status & float_flag_underflow)
B
blueswir1 已提交
65
            env->fsr |= FSR_UFC;
B
blueswir1 已提交
66
        if (status & float_flag_divbyzero)
B
blueswir1 已提交
67
            env->fsr |= FSR_DZC;
B
blueswir1 已提交
68
        if (status & float_flag_inexact)
B
blueswir1 已提交
69 70
            env->fsr |= FSR_NXC;

B
blueswir1 已提交
71
        if ((env->fsr & FSR_CEXC_MASK) & ((env->fsr & FSR_TEM_MASK) >> 23)) {
B
blueswir1 已提交
72 73 74
            /* Unmasked exception, generate a trap */
            env->fsr |= FSR_FTT_IEEE_EXCP;
            raise_exception(TT_FP_EXCP);
B
blueswir1 已提交
75
        } else {
B
blueswir1 已提交
76 77 78
            /* Accumulate exceptions */
            env->fsr |= (env->fsr & FSR_CEXC_MASK) << 5;
        }
B
blueswir1 已提交
79
    }
80 81
}

82 83 84 85 86
void helper_clear_float_exceptions(void)
{
    set_float_exception_flags(0, &env->fp_status);
}

B
bellard 已提交
87 88 89
#ifdef USE_INT_TO_FLOAT_HELPERS
void do_fitos(void)
{
T
ths 已提交
90
    FT0 = int32_to_float32(*((int32_t *)&FT1), &env->fp_status);
B
bellard 已提交
91 92 93 94
}

void do_fitod(void)
{
T
ths 已提交
95
    DT0 = int32_to_float64(*((int32_t *)&FT1), &env->fp_status);
B
bellard 已提交
96
}
97 98 99 100 101 102 103 104

#if defined(CONFIG_USER_ONLY)
void do_fitoq(void)
{
    QT0 = int32_to_float128(*((int32_t *)&FT1), &env->fp_status);
}
#endif

B
blueswir1 已提交
105 106 107 108 109 110 111 112 113 114
#ifdef TARGET_SPARC64
void do_fxtos(void)
{
    FT0 = int64_to_float32(*((int64_t *)&DT1), &env->fp_status);
}

void do_fxtod(void)
{
    DT0 = int64_to_float64(*((int64_t *)&DT1), &env->fp_status);
}
115 116 117 118 119 120 121

#if defined(CONFIG_USER_ONLY)
void do_fxtoq(void)
{
    QT0 = int64_to_float128(*((int32_t *)&DT1), &env->fp_status);
}
#endif
B
blueswir1 已提交
122
#endif
B
bellard 已提交
123 124
#endif

125
void helper_fabss(void)
126
{
B
bellard 已提交
127
    FT0 = float32_abs(FT1);
128 129
}

B
bellard 已提交
130
#ifdef TARGET_SPARC64
131
void helper_fabsd(void)
B
bellard 已提交
132 133 134
{
    DT0 = float64_abs(DT1);
}
B
blueswir1 已提交
135 136

#if defined(CONFIG_USER_ONLY)
137
void helper_fabsq(void)
B
blueswir1 已提交
138 139 140 141
{
    QT0 = float128_abs(QT1);
}
#endif
B
bellard 已提交
142 143
#endif

144
void helper_fsqrts(void)
145
{
B
bellard 已提交
146
    FT0 = float32_sqrt(FT1, &env->fp_status);
147 148
}

149
void helper_fsqrtd(void)
150
{
B
bellard 已提交
151
    DT0 = float64_sqrt(DT1, &env->fp_status);
152 153
}

B
blueswir1 已提交
154
#if defined(CONFIG_USER_ONLY)
155
void helper_fsqrtq(void)
B
blueswir1 已提交
156 157 158 159 160
{
    QT0 = float128_sqrt(QT1, &env->fp_status);
}
#endif

161
#define GEN_FCMP(name, size, reg1, reg2, FS, TRAP)                      \
162
    void glue(helper_, name) (void)                                     \
B
bellard 已提交
163
    {                                                                   \
B
blueswir1 已提交
164 165
        target_ulong new_fsr;                                           \
                                                                        \
B
bellard 已提交
166 167 168
        env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS);                     \
        switch (glue(size, _compare) (reg1, reg2, &env->fp_status)) {   \
        case float_relation_unordered:                                  \
B
blueswir1 已提交
169
            new_fsr = (FSR_FCC1 | FSR_FCC0) << FS;                      \
170
            if ((env->fsr & FSR_NVM) || TRAP) {                         \
B
blueswir1 已提交
171
                env->fsr |= new_fsr;                                    \
172 173
                env->fsr |= FSR_NVC;                                    \
                env->fsr |= FSR_FTT_IEEE_EXCP;                          \
B
bellard 已提交
174 175 176 177 178 179
                raise_exception(TT_FP_EXCP);                            \
            } else {                                                    \
                env->fsr |= FSR_NVA;                                    \
            }                                                           \
            break;                                                      \
        case float_relation_less:                                       \
B
blueswir1 已提交
180
            new_fsr = FSR_FCC0 << FS;                                   \
B
bellard 已提交
181 182
            break;                                                      \
        case float_relation_greater:                                    \
B
blueswir1 已提交
183
            new_fsr = FSR_FCC1 << FS;                                   \
B
bellard 已提交
184 185
            break;                                                      \
        default:                                                        \
B
blueswir1 已提交
186
            new_fsr = 0;                                                \
B
bellard 已提交
187 188
            break;                                                      \
        }                                                               \
B
blueswir1 已提交
189
        env->fsr |= new_fsr;                                            \
190 191
    }

192 193 194 195 196
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 已提交
197

B
blueswir1 已提交
198 199 200 201 202
#ifdef CONFIG_USER_ONLY
GEN_FCMP(fcmpq, float128, QT0, QT1, 0, 0);
GEN_FCMP(fcmpeq, float128, QT0, QT1, 0, 1);
#endif

B
bellard 已提交
203
#ifdef TARGET_SPARC64
204 205 206 207 208 209 210 211 212 213 214
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 已提交
215

216 217
GEN_FCMP(fcmpes_fcc2, float32, FT0, FT1, 24, 1);
GEN_FCMP(fcmped_fcc2, float64, DT0, DT1, 24, 1);
B
bellard 已提交
218

219 220
GEN_FCMP(fcmpes_fcc3, float32, FT0, FT1, 26, 1);
GEN_FCMP(fcmped_fcc3, float64, DT0, DT1, 26, 1);
B
blueswir1 已提交
221 222 223 224 225 226 227 228
#ifdef CONFIG_USER_ONLY
GEN_FCMP(fcmpq_fcc1, float128, QT0, QT1, 22, 0);
GEN_FCMP(fcmpq_fcc2, float128, QT0, QT1, 24, 0);
GEN_FCMP(fcmpq_fcc3, float128, QT0, QT1, 26, 0);
GEN_FCMP(fcmpeq_fcc1, float128, QT0, QT1, 22, 1);
GEN_FCMP(fcmpeq_fcc2, float128, QT0, QT1, 24, 1);
GEN_FCMP(fcmpeq_fcc3, float128, QT0, QT1, 26, 1);
#endif
B
bellard 已提交
229 230
#endif

B
blueswir1 已提交
231
#if !defined(TARGET_SPARC64) && !defined(CONFIG_USER_ONLY) && defined(DEBUG_MXCC)
232 233 234 235 236 237 238 239 240 241 242
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
blueswir1 已提交
243 244 245 246
#if (defined(TARGET_SPARC64) || !defined(CONFIG_USER_ONLY)) \
    && defined(DEBUG_ASI)
static void dump_asi(const char *txt, target_ulong addr, int asi, int size,
                     uint64_t r1)
247 248 249 250
{
    switch (size)
    {
    case 1:
B
blueswir1 已提交
251 252
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %02" PRIx64 "\n", txt,
                    addr, asi, r1 & 0xff);
253 254
        break;
    case 2:
B
blueswir1 已提交
255 256
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %04" PRIx64 "\n", txt,
                    addr, asi, r1 & 0xffff);
257 258
        break;
    case 4:
B
blueswir1 已提交
259 260
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %08" PRIx64 "\n", txt,
                    addr, asi, r1 & 0xffffffff);
261 262
        break;
    case 8:
B
blueswir1 已提交
263 264
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %016" PRIx64 "\n", txt,
                    addr, asi, r1);
265 266 267 268 269
        break;
    }
}
#endif

B
blueswir1 已提交
270 271 272
#ifndef TARGET_SPARC64
#ifndef CONFIG_USER_ONLY
uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
273
{
B
blueswir1 已提交
274
    uint64_t ret = 0;
275
#if defined(DEBUG_MXCC) || defined(DEBUG_ASI)
B
blueswir1 已提交
276
    uint32_t last_addr = addr;
277
#endif
B
bellard 已提交
278 279

    switch (asi) {
280
    case 2: /* SuperSparc MXCC registers */
B
blueswir1 已提交
281
        switch (addr) {
282
        case 0x01c00a00: /* MXCC control register */
B
blueswir1 已提交
283 284 285 286
            if (size == 8)
                ret = env->mxccregs[3];
            else
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr, size);
287 288 289 290 291
            break;
        case 0x01c00a04: /* MXCC control register */
            if (size == 4)
                ret = env->mxccregs[3];
            else
B
blueswir1 已提交
292
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr, size);
293
            break;
294 295
        case 0x01c00c00: /* Module reset register */
            if (size == 8) {
B
blueswir1 已提交
296
                ret = env->mxccregs[5];
297 298
                // should we do something here?
            } else
B
blueswir1 已提交
299
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr, size);
300
            break;
301
        case 0x01c00f00: /* MBus port address register */
B
blueswir1 已提交
302 303 304 305
            if (size == 8)
                ret = env->mxccregs[7];
            else
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr, size);
306 307
            break;
        default:
B
blueswir1 已提交
308
            DPRINTF_MXCC("%08x: unimplemented address, size: %d\n", addr, size);
309 310
            break;
        }
B
blueswir1 已提交
311 312
        DPRINTF_MXCC("asi = %d, size = %d, sign = %d, addr = %08x -> ret = %08x,"
                     "addr = %08x\n", asi, size, sign, last_addr, ret, addr);
313 314 315
#ifdef DEBUG_MXCC
        dump_mxcc(env);
#endif
316
        break;
317
    case 3: /* MMU probe */
B
blueswir1 已提交
318 319 320
        {
            int mmulev;

B
blueswir1 已提交
321
            mmulev = (addr >> 8) & 15;
B
blueswir1 已提交
322 323
            if (mmulev > 4)
                ret = 0;
B
blueswir1 已提交
324 325 326 327
            else
                ret = mmu_probe(env, addr, mmulev);
            DPRINTF_MMU("mmu_probe: 0x%08x (lev %d) -> 0x%08" PRIx64 "\n",
                        addr, mmulev, ret);
B
blueswir1 已提交
328 329
        }
        break;
330
    case 4: /* read MMU regs */
B
blueswir1 已提交
331
        {
B
blueswir1 已提交
332
            int reg = (addr >> 8) & 0x1f;
333

B
blueswir1 已提交
334 335
            ret = env->mmuregs[reg];
            if (reg == 3) /* Fault status cleared on read */
B
blueswir1 已提交
336 337 338 339 340
                env->mmuregs[3] = 0;
            else if (reg == 0x13) /* Fault status read */
                ret = env->mmuregs[3];
            else if (reg == 0x14) /* Fault address read */
                ret = env->mmuregs[4];
B
blueswir1 已提交
341
            DPRINTF_MMU("mmu_read: reg[%d] = 0x%08" PRIx64 "\n", reg, ret);
B
blueswir1 已提交
342 343
        }
        break;
B
blueswir1 已提交
344 345 346 347
    case 5: // Turbosparc ITLB Diagnostic
    case 6: // Turbosparc DTLB Diagnostic
    case 7: // Turbosparc IOTLB Diagnostic
        break;
348 349 350
    case 9: /* Supervisor code access */
        switch(size) {
        case 1:
B
blueswir1 已提交
351
            ret = ldub_code(addr);
352 353
            break;
        case 2:
B
blueswir1 已提交
354
            ret = lduw_code(addr & ~1);
355 356 357
            break;
        default:
        case 4:
B
blueswir1 已提交
358
            ret = ldl_code(addr & ~3);
359 360
            break;
        case 8:
B
blueswir1 已提交
361
            ret = ldq_code(addr & ~7);
362 363 364
            break;
        }
        break;
365 366 367
    case 0xa: /* User data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
368
            ret = ldub_user(addr);
369 370
            break;
        case 2:
B
blueswir1 已提交
371
            ret = lduw_user(addr & ~1);
372 373 374
            break;
        default:
        case 4:
B
blueswir1 已提交
375
            ret = ldl_user(addr & ~3);
376 377
            break;
        case 8:
B
blueswir1 已提交
378
            ret = ldq_user(addr & ~7);
379 380 381 382 383 384
            break;
        }
        break;
    case 0xb: /* Supervisor data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
385
            ret = ldub_kernel(addr);
386 387
            break;
        case 2:
B
blueswir1 已提交
388
            ret = lduw_kernel(addr & ~1);
389 390 391
            break;
        default:
        case 4:
B
blueswir1 已提交
392
            ret = ldl_kernel(addr & ~3);
393 394
            break;
        case 8:
B
blueswir1 已提交
395
            ret = ldq_kernel(addr & ~7);
396 397 398
            break;
        }
        break;
399 400 401 402 403 404
    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 已提交
405 406
        switch(size) {
        case 1:
B
blueswir1 已提交
407
            ret = ldub_phys(addr);
B
bellard 已提交
408 409
            break;
        case 2:
B
blueswir1 已提交
410
            ret = lduw_phys(addr & ~1);
B
bellard 已提交
411 412 413
            break;
        default:
        case 4:
B
blueswir1 已提交
414
            ret = ldl_phys(addr & ~3);
B
bellard 已提交
415
            break;
B
bellard 已提交
416
        case 8:
B
blueswir1 已提交
417
            ret = ldq_phys(addr & ~7);
B
blueswir1 已提交
418
            break;
B
bellard 已提交
419
        }
B
blueswir1 已提交
420
        break;
421
    case 0x21 ... 0x2f: /* MMU passthrough, 0x100000000 to 0xfffffffff */
422 423
        switch(size) {
        case 1:
B
blueswir1 已提交
424
            ret = ldub_phys((target_phys_addr_t)addr
425 426 427
                            | ((target_phys_addr_t)(asi & 0xf) << 32));
            break;
        case 2:
B
blueswir1 已提交
428
            ret = lduw_phys((target_phys_addr_t)(addr & ~1)
429 430 431 432
                            | ((target_phys_addr_t)(asi & 0xf) << 32));
            break;
        default:
        case 4:
B
blueswir1 已提交
433
            ret = ldl_phys((target_phys_addr_t)(addr & ~3)
434 435 436
                           | ((target_phys_addr_t)(asi & 0xf) << 32));
            break;
        case 8:
B
blueswir1 已提交
437
            ret = ldq_phys((target_phys_addr_t)(addr & ~7)
438
                           | ((target_phys_addr_t)(asi & 0xf) << 32));
B
blueswir1 已提交
439
            break;
440
        }
B
blueswir1 已提交
441
        break;
B
blueswir1 已提交
442 443 444
    case 0x30: // Turbosparc secondary cache diagnostic
    case 0x31: // Turbosparc RAM snoop
    case 0x32: // Turbosparc page table descriptor diagnostic
B
blueswir1 已提交
445 446 447
    case 0x39: /* data cache diagnostic register */
        ret = 0;
        break;
B
blueswir1 已提交
448
    case 8: /* User code access, XXX */
449
    default:
B
blueswir1 已提交
450
        do_unassigned_access(addr, 0, 0, asi);
B
blueswir1 已提交
451 452
        ret = 0;
        break;
453
    }
454 455 456
    if (sign) {
        switch(size) {
        case 1:
B
blueswir1 已提交
457
            ret = (int8_t) ret;
B
blueswir1 已提交
458
            break;
459
        case 2:
B
blueswir1 已提交
460 461 462 463
            ret = (int16_t) ret;
            break;
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
464
            break;
465 466 467 468
        default:
            break;
        }
    }
469
#ifdef DEBUG_ASI
B
blueswir1 已提交
470
    dump_asi("read ", last_addr, asi, size, ret);
471
#endif
B
blueswir1 已提交
472
    return ret;
473 474
}

B
blueswir1 已提交
475
void helper_st_asi(target_ulong addr, uint64_t val, int asi, int size)
476 477
{
    switch(asi) {
478
    case 2: /* SuperSparc MXCC registers */
B
blueswir1 已提交
479
        switch (addr) {
480 481
        case 0x01c00000: /* MXCC stream data register 0 */
            if (size == 8)
B
blueswir1 已提交
482
                env->mxccdata[0] = val;
483
            else
B
blueswir1 已提交
484
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr, size);
485 486 487
            break;
        case 0x01c00008: /* MXCC stream data register 1 */
            if (size == 8)
B
blueswir1 已提交
488
                env->mxccdata[1] = val;
489
            else
B
blueswir1 已提交
490
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr, size);
491 492 493
            break;
        case 0x01c00010: /* MXCC stream data register 2 */
            if (size == 8)
B
blueswir1 已提交
494
                env->mxccdata[2] = val;
495
            else
B
blueswir1 已提交
496
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr, size);
497 498 499
            break;
        case 0x01c00018: /* MXCC stream data register 3 */
            if (size == 8)
B
blueswir1 已提交
500
                env->mxccdata[3] = val;
501
            else
B
blueswir1 已提交
502
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr, size);
503 504 505
            break;
        case 0x01c00100: /* MXCC stream source */
            if (size == 8)
B
blueswir1 已提交
506
                env->mxccregs[0] = val;
507
            else
B
blueswir1 已提交
508
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr, size);
509 510 511 512 513 514 515
            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)
B
blueswir1 已提交
516
                env->mxccregs[1] = val;
517
            else
B
blueswir1 已提交
518
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr, size);
519 520 521 522 523 524 525
            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)
B
blueswir1 已提交
526
                env->mxccregs[3] = val;
527
            else
B
blueswir1 已提交
528
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr, size);
529 530 531
            break;
        case 0x01c00a04: /* MXCC control register */
            if (size == 4)
B
blueswir1 已提交
532
                env->mxccregs[3] = (env->mxccregs[0xa] & 0xffffffff00000000ULL) | val;
533
            else
B
blueswir1 已提交
534
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr, size);
535 536
            break;
        case 0x01c00e00: /* MXCC error register  */
537
            // writing a 1 bit clears the error
538
            if (size == 8)
B
blueswir1 已提交
539
                env->mxccregs[6] &= ~val;
540
            else
B
blueswir1 已提交
541
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr, size);
542 543 544
            break;
        case 0x01c00f00: /* MBus port address register */
            if (size == 8)
B
blueswir1 已提交
545
                env->mxccregs[7] = val;
546
            else
B
blueswir1 已提交
547
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr, size);
548 549
            break;
        default:
B
blueswir1 已提交
550
            DPRINTF_MXCC("%08x: unimplemented address, size: %d\n", addr, size);
551 552
            break;
        }
B
blueswir1 已提交
553
        DPRINTF_MXCC("asi = %d, size = %d, addr = %08x, val = %08x\n", asi, size, addr, val);
554 555 556
#ifdef DEBUG_MXCC
        dump_mxcc(env);
#endif
557
        break;
558
    case 3: /* MMU flush */
B
blueswir1 已提交
559 560
        {
            int mmulev;
B
bellard 已提交
561

B
blueswir1 已提交
562
            mmulev = (addr >> 8) & 15;
563
            DPRINTF_MMU("mmu flush level %d\n", mmulev);
B
blueswir1 已提交
564 565
            switch (mmulev) {
            case 0: // flush page
B
blueswir1 已提交
566
                tlb_flush_page(env, addr & 0xfffff000);
B
blueswir1 已提交
567 568 569 570 571 572 573 574 575 576
                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 已提交
577
#ifdef DEBUG_MMU
B
blueswir1 已提交
578
            dump_mmu(env);
B
bellard 已提交
579
#endif
B
blueswir1 已提交
580
        }
581
        break;
582
    case 4: /* write MMU regs */
B
blueswir1 已提交
583
        {
B
blueswir1 已提交
584
            int reg = (addr >> 8) & 0x1f;
B
blueswir1 已提交
585
            uint32_t oldreg;
586

B
blueswir1 已提交
587
            oldreg = env->mmuregs[reg];
B
bellard 已提交
588
            switch(reg) {
589
            case 0: // Control Register
B
blueswir1 已提交
590
                env->mmuregs[reg] = (env->mmuregs[reg] & 0xff000000) |
B
blueswir1 已提交
591
                                    (val & 0x00ffffff);
B
blueswir1 已提交
592 593
                // Mappings generated during no-fault mode or MMU
                // disabled mode are invalid in normal mode
B
blueswir1 已提交
594 595
                if ((oldreg & (MMU_E | MMU_NF | env->mmu_bm)) !=
                    (env->mmuregs[reg] & (MMU_E | MMU_NF | env->mmu_bm)))
B
bellard 已提交
596 597
                    tlb_flush(env, 1);
                break;
598
            case 1: // Context Table Pointer Register
B
blueswir1 已提交
599
                env->mmuregs[reg] = val & env->mmu_ctpr_mask;
600 601
                break;
            case 2: // Context Register
B
blueswir1 已提交
602
                env->mmuregs[reg] = val & env->mmu_cxr_mask;
B
bellard 已提交
603 604 605 606 607 608
                if (oldreg != env->mmuregs[reg]) {
                    /* we flush when the MMU context changes because
                       QEMU has no MMU context support */
                    tlb_flush(env, 1);
                }
                break;
609 610 611 612
            case 3: // Synchronous Fault Status Register with Clear
            case 4: // Synchronous Fault Address Register
                break;
            case 0x10: // TLB Replacement Control Register
B
blueswir1 已提交
613
                env->mmuregs[reg] = val & env->mmu_trcr_mask;
B
bellard 已提交
614
                break;
615
            case 0x13: // Synchronous Fault Status Register with Read and Clear
B
blueswir1 已提交
616
                env->mmuregs[3] = val & env->mmu_sfsr_mask;
B
blueswir1 已提交
617
                break;
618
            case 0x14: // Synchronous Fault Address Register
B
blueswir1 已提交
619
                env->mmuregs[4] = val;
B
blueswir1 已提交
620
                break;
B
bellard 已提交
621
            default:
B
blueswir1 已提交
622
                env->mmuregs[reg] = val;
B
bellard 已提交
623 624 625
                break;
            }
            if (oldreg != env->mmuregs[reg]) {
626
                DPRINTF_MMU("mmu change reg[%d]: 0x%08x -> 0x%08x\n", reg, oldreg, env->mmuregs[reg]);
B
bellard 已提交
627
            }
628
#ifdef DEBUG_MMU
B
blueswir1 已提交
629
            dump_mmu(env);
B
bellard 已提交
630
#endif
B
blueswir1 已提交
631
        }
632
        break;
B
blueswir1 已提交
633 634 635 636
    case 5: // Turbosparc ITLB Diagnostic
    case 6: // Turbosparc DTLB Diagnostic
    case 7: // Turbosparc IOTLB Diagnostic
        break;
637 638 639
    case 0xa: /* User data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
640
            stb_user(addr, val);
641 642
            break;
        case 2:
B
blueswir1 已提交
643
            stw_user(addr & ~1, val);
644 645 646
            break;
        default:
        case 4:
B
blueswir1 已提交
647
            stl_user(addr & ~3, val);
648 649
            break;
        case 8:
B
blueswir1 已提交
650
            stq_user(addr & ~7, val);
651 652 653 654 655 656
            break;
        }
        break;
    case 0xb: /* Supervisor data access */
        switch(size) {
        case 1:
B
blueswir1 已提交
657
            stb_kernel(addr, val);
658 659
            break;
        case 2:
B
blueswir1 已提交
660
            stw_kernel(addr & ~1, val);
661 662 663
            break;
        default:
        case 4:
B
blueswir1 已提交
664
            stl_kernel(addr & ~3, val);
665 666
            break;
        case 8:
B
blueswir1 已提交
667
            stq_kernel(addr & ~7, val);
668 669 670
            break;
        }
        break;
671 672 673 674 675 676 677 678 679 680
    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 已提交
681
    case 0x17: /* Block copy, sta access */
B
blueswir1 已提交
682
        {
B
blueswir1 已提交
683 684
            // val = src
            // addr = dst
B
blueswir1 已提交
685
            // copy 32 bytes
686
            unsigned int i;
B
blueswir1 已提交
687
            uint32_t src = val & ~3, dst = addr & ~3, temp;
688

689 690 691 692
            for (i = 0; i < 32; i += 4, src += 4, dst += 4) {
                temp = ldl_kernel(src);
                stl_kernel(dst, temp);
            }
B
blueswir1 已提交
693
        }
694
        break;
B
bellard 已提交
695
    case 0x1f: /* Block fill, stda access */
B
blueswir1 已提交
696
        {
B
blueswir1 已提交
697 698
            // addr = dst
            // fill 32 bytes with val
699
            unsigned int i;
B
blueswir1 已提交
700
            uint32_t dst = addr & 7;
701 702 703

            for (i = 0; i < 32; i += 8, dst += 8)
                stq_kernel(dst, val);
B
blueswir1 已提交
704
        }
705
        break;
706
    case 0x20: /* MMU passthrough */
B
blueswir1 已提交
707
        {
B
bellard 已提交
708 709
            switch(size) {
            case 1:
B
blueswir1 已提交
710
                stb_phys(addr, val);
B
bellard 已提交
711 712
                break;
            case 2:
B
blueswir1 已提交
713
                stw_phys(addr & ~1, val);
B
bellard 已提交
714 715 716
                break;
            case 4:
            default:
B
blueswir1 已提交
717
                stl_phys(addr & ~3, val);
B
bellard 已提交
718
                break;
B
bellard 已提交
719
            case 8:
B
blueswir1 已提交
720
                stq_phys(addr & ~7, val);
B
bellard 已提交
721
                break;
B
bellard 已提交
722
            }
B
blueswir1 已提交
723
        }
724
        break;
B
blueswir1 已提交
725
    case 0x21 ... 0x2f: /* MMU passthrough, 0x100000000 to 0xfffffffff */
B
blueswir1 已提交
726
        {
727 728
            switch(size) {
            case 1:
B
blueswir1 已提交
729 730
                stb_phys((target_phys_addr_t)addr
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
731 732
                break;
            case 2:
B
blueswir1 已提交
733 734
                stw_phys((target_phys_addr_t)(addr & ~1)
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
735 736 737
                break;
            case 4:
            default:
B
blueswir1 已提交
738 739
                stl_phys((target_phys_addr_t)(addr & ~3)
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
740 741
                break;
            case 8:
B
blueswir1 已提交
742 743
                stq_phys((target_phys_addr_t)(addr & ~7)
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
744 745
                break;
            }
B
blueswir1 已提交
746
        }
747
        break;
B
blueswir1 已提交
748 749 750 751
    case 0x30: // store buffer tags or Turbosparc secondary cache diagnostic
    case 0x31: // store buffer data, Ross RT620 I-cache flush or
               // Turbosparc snoop RAM
    case 0x32: // store buffer control or Turbosparc page table descriptor diagnostic
752 753
    case 0x36: /* I-cache flash clear */
    case 0x37: /* D-cache flash clear */
B
blueswir1 已提交
754 755
    case 0x38: /* breakpoint diagnostics */
    case 0x4c: /* breakpoint action */
756
        break;
B
blueswir1 已提交
757
    case 8: /* User code access, XXX */
758
    case 9: /* Supervisor code access, XXX */
759
    default:
B
blueswir1 已提交
760
        do_unassigned_access(addr, 1, 0, asi);
761
        break;
762
    }
763
#ifdef DEBUG_ASI
B
blueswir1 已提交
764
    dump_asi("write", addr, asi, size, val);
765
#endif
766 767
}

768 769 770 771
#endif /* CONFIG_USER_ONLY */
#else /* TARGET_SPARC64 */

#ifdef CONFIG_USER_ONLY
B
blueswir1 已提交
772
uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
773 774
{
    uint64_t ret = 0;
B
blueswir1 已提交
775 776 777
#if defined(DEBUG_ASI)
    target_ulong last_addr = addr;
#endif
778 779 780 781 782 783 784 785 786 787 788 789

    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:
B
blueswir1 已提交
790
                ret = ldub_raw(addr);
791 792
                break;
            case 2:
B
blueswir1 已提交
793
                ret = lduw_raw(addr & ~1);
794 795
                break;
            case 4:
B
blueswir1 已提交
796
                ret = ldl_raw(addr & ~3);
797 798 799
                break;
            default:
            case 8:
B
blueswir1 已提交
800
                ret = ldq_raw(addr & ~7);
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
                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 已提交
824
            break;
825 826
        case 4:
            ret = bswap32(ret);
B
blueswir1 已提交
827
            break;
828 829
        case 8:
            ret = bswap64(ret);
B
blueswir1 已提交
830
            break;
831 832 833 834 835 836 837 838 839 840 841 842
        default:
            break;
        }
    default:
        break;
    }

    /* Convert to signed number */
    if (sign) {
        switch(size) {
        case 1:
            ret = (int8_t) ret;
B
blueswir1 已提交
843
            break;
844 845
        case 2:
            ret = (int16_t) ret;
B
blueswir1 已提交
846
            break;
847 848
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
849
            break;
850 851 852 853
        default:
            break;
        }
    }
B
blueswir1 已提交
854 855 856 857
#ifdef DEBUG_ASI
    dump_asi("read ", last_addr, asi, size, ret);
#endif
    return ret;
858 859
}

B
blueswir1 已提交
860
void helper_st_asi(target_ulong addr, target_ulong val, int asi, int size)
861
{
B
blueswir1 已提交
862 863 864
#ifdef DEBUG_ASI
    dump_asi("write", addr, asi, size, val);
#endif
865 866 867 868 869 870 871 872 873
    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:
B
blueswir1 已提交
874
            addr = bswap16(addr);
B
blueswir1 已提交
875
            break;
876
        case 4:
B
blueswir1 已提交
877
            addr = bswap32(addr);
B
blueswir1 已提交
878
            break;
879
        case 8:
B
blueswir1 已提交
880
            addr = bswap64(addr);
B
blueswir1 已提交
881
            break;
882 883 884 885 886 887 888 889 890 891 892 893 894
        default:
            break;
        }
    default:
        break;
    }

    switch(asi) {
    case 0x80: // Primary
    case 0x88: // Primary LE
        {
            switch(size) {
            case 1:
B
blueswir1 已提交
895
                stb_raw(addr, val);
896 897
                break;
            case 2:
B
blueswir1 已提交
898
                stw_raw(addr & ~1, val);
899 900
                break;
            case 4:
B
blueswir1 已提交
901
                stl_raw(addr & ~3, val);
902 903 904
                break;
            case 8:
            default:
B
blueswir1 已提交
905
                stq_raw(addr & ~7, val);
906 907 908 909 910 911 912 913 914 915 916 917 918 919
                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:
B
blueswir1 已提交
920
        do_unassigned_access(addr, 1, 0, 1);
921 922 923 924 925
        return;
    }
}

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

B
blueswir1 已提交
927
uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
B
bellard 已提交
928
{
B
bellard 已提交
929
    uint64_t ret = 0;
B
blueswir1 已提交
930 931 932
#if defined(DEBUG_ASI)
    target_ulong last_addr = addr;
#endif
B
bellard 已提交
933

B
blueswir1 已提交
934
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
B
blueswir1 已提交
935
        || (asi >= 0x30 && asi < 0x80 && !(env->hpstate & HS_PRIV)))
B
blueswir1 已提交
936
        raise_exception(TT_PRIV_ACT);
B
bellard 已提交
937 938

    switch (asi) {
939 940 941 942 943 944 945
    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 已提交
946 947 948
            if (env->hpstate & HS_PRIV) {
                switch(size) {
                case 1:
B
blueswir1 已提交
949
                    ret = ldub_hypv(addr);
B
blueswir1 已提交
950 951
                    break;
                case 2:
B
blueswir1 已提交
952
                    ret = lduw_hypv(addr & ~1);
B
blueswir1 已提交
953 954
                    break;
                case 4:
B
blueswir1 已提交
955
                    ret = ldl_hypv(addr & ~3);
B
blueswir1 已提交
956 957 958
                    break;
                default:
                case 8:
B
blueswir1 已提交
959
                    ret = ldq_hypv(addr & ~7);
B
blueswir1 已提交
960 961 962 963 964
                    break;
                }
            } else {
                switch(size) {
                case 1:
B
blueswir1 已提交
965
                    ret = ldub_kernel(addr);
B
blueswir1 已提交
966 967
                    break;
                case 2:
B
blueswir1 已提交
968
                    ret = lduw_kernel(addr & ~1);
B
blueswir1 已提交
969 970
                    break;
                case 4:
B
blueswir1 已提交
971
                    ret = ldl_kernel(addr & ~3);
B
blueswir1 已提交
972 973 974
                    break;
                default:
                case 8:
B
blueswir1 已提交
975
                    ret = ldq_kernel(addr & ~7);
B
blueswir1 已提交
976 977
                    break;
                }
978 979 980 981
            }
        } else {
            switch(size) {
            case 1:
B
blueswir1 已提交
982
                ret = ldub_user(addr);
983 984
                break;
            case 2:
B
blueswir1 已提交
985
                ret = lduw_user(addr & ~1);
986 987
                break;
            case 4:
B
blueswir1 已提交
988
                ret = ldl_user(addr & ~3);
989 990 991
                break;
            default:
            case 8:
B
blueswir1 已提交
992
                ret = ldq_user(addr & ~7);
993 994 995 996
                break;
            }
        }
        break;
B
bellard 已提交
997 998
    case 0x14: // Bypass
    case 0x15: // Bypass, non-cacheable
999 1000
    case 0x1c: // Bypass LE
    case 0x1d: // Bypass, non-cacheable LE
B
blueswir1 已提交
1001
        {
B
bellard 已提交
1002 1003
            switch(size) {
            case 1:
B
blueswir1 已提交
1004
                ret = ldub_phys(addr);
B
bellard 已提交
1005 1006
                break;
            case 2:
B
blueswir1 已提交
1007
                ret = lduw_phys(addr & ~1);
B
bellard 已提交
1008 1009
                break;
            case 4:
B
blueswir1 已提交
1010
                ret = ldl_phys(addr & ~3);
B
bellard 已提交
1011 1012 1013
                break;
            default:
            case 8:
B
blueswir1 已提交
1014
                ret = ldq_phys(addr & ~7);
B
bellard 已提交
1015 1016
                break;
            }
B
blueswir1 已提交
1017 1018
            break;
        }
B
bellard 已提交
1019 1020 1021 1022 1023 1024 1025
    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
1026
    case 0x81: // Secondary
B
bellard 已提交
1027 1028 1029
    case 0x83: // Secondary no-fault
    case 0x89: // Secondary LE
    case 0x8b: // Secondary no-fault LE
B
blueswir1 已提交
1030 1031
        // XXX
        break;
B
bellard 已提交
1032
    case 0x45: // LSU
B
blueswir1 已提交
1033 1034
        ret = env->lsu;
        break;
B
bellard 已提交
1035
    case 0x50: // I-MMU regs
B
blueswir1 已提交
1036
        {
B
blueswir1 已提交
1037
            int reg = (addr >> 3) & 0xf;
B
bellard 已提交
1038

B
blueswir1 已提交
1039 1040 1041
            ret = env->immuregs[reg];
            break;
        }
B
bellard 已提交
1042 1043 1044
    case 0x51: // I-MMU 8k TSB pointer
    case 0x52: // I-MMU 64k TSB pointer
    case 0x55: // I-MMU data access
B
blueswir1 已提交
1045 1046
        // XXX
        break;
B
bellard 已提交
1047
    case 0x56: // I-MMU tag read
B
blueswir1 已提交
1048 1049 1050 1051 1052 1053
        {
            unsigned int i;

            for (i = 0; i < 64; i++) {
                // Valid, ctx match, vaddr match
                if ((env->itlb_tte[i] & 0x8000000000000000ULL) != 0 &&
B
blueswir1 已提交
1054
                    env->itlb_tag[i] == addr) {
B
blueswir1 已提交
1055 1056 1057 1058 1059 1060
                    ret = env->itlb_tag[i];
                    break;
                }
            }
            break;
        }
B
bellard 已提交
1061
    case 0x58: // D-MMU regs
B
blueswir1 已提交
1062
        {
B
blueswir1 已提交
1063
            int reg = (addr >> 3) & 0xf;
B
bellard 已提交
1064

B
blueswir1 已提交
1065 1066 1067
            ret = env->dmmuregs[reg];
            break;
        }
B
bellard 已提交
1068
    case 0x5e: // D-MMU tag read
B
blueswir1 已提交
1069 1070 1071 1072 1073 1074
        {
            unsigned int i;

            for (i = 0; i < 64; i++) {
                // Valid, ctx match, vaddr match
                if ((env->dtlb_tte[i] & 0x8000000000000000ULL) != 0 &&
B
blueswir1 已提交
1075
                    env->dtlb_tag[i] == addr) {
B
blueswir1 已提交
1076 1077 1078 1079 1080 1081
                    ret = env->dtlb_tag[i];
                    break;
                }
            }
            break;
        }
B
bellard 已提交
1082 1083 1084 1085
    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 已提交
1086 1087 1088
    case 0x48: // Interrupt dispatch, RO
    case 0x49: // Interrupt data receive
    case 0x7f: // Incoming interrupt vector, RO
B
blueswir1 已提交
1089 1090
        // XXX
        break;
B
bellard 已提交
1091 1092 1093 1094
    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 已提交
1095
    case 0x77: // Interrupt vector, WO
B
bellard 已提交
1096
    default:
B
blueswir1 已提交
1097
        do_unassigned_access(addr, 0, 0, 1);
B
blueswir1 已提交
1098 1099
        ret = 0;
        break;
B
bellard 已提交
1100
    }
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115

    /* 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 已提交
1116
            break;
1117 1118
        case 4:
            ret = bswap32(ret);
B
blueswir1 已提交
1119
            break;
1120 1121
        case 8:
            ret = bswap64(ret);
B
blueswir1 已提交
1122
            break;
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
        default:
            break;
        }
    default:
        break;
    }

    /* Convert to signed number */
    if (sign) {
        switch(size) {
        case 1:
            ret = (int8_t) ret;
B
blueswir1 已提交
1135
            break;
1136 1137
        case 2:
            ret = (int16_t) ret;
B
blueswir1 已提交
1138
            break;
1139 1140
        case 4:
            ret = (int32_t) ret;
B
blueswir1 已提交
1141
            break;
1142 1143 1144 1145
        default:
            break;
        }
    }
B
blueswir1 已提交
1146 1147 1148 1149
#ifdef DEBUG_ASI
    dump_asi("read ", last_addr, asi, size, ret);
#endif
    return ret;
B
bellard 已提交
1150 1151
}

B
blueswir1 已提交
1152
void helper_st_asi(target_ulong addr, target_ulong val, int asi, int size)
B
bellard 已提交
1153
{
B
blueswir1 已提交
1154 1155 1156
#ifdef DEBUG_ASI
    dump_asi("write", addr, asi, size, val);
#endif
B
blueswir1 已提交
1157
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
B
blueswir1 已提交
1158
        || (asi >= 0x30 && asi < 0x80 && !(env->hpstate & HS_PRIV)))
B
blueswir1 已提交
1159
        raise_exception(TT_PRIV_ACT);
B
bellard 已提交
1160

1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
    /* Convert to little endian */
    switch (asi) {
    case 0x0c: // Nucleus Little Endian (LE)
    case 0x18: // As if user primary LE
    case 0x19: // As if user secondary LE
    case 0x1c: // Bypass LE
    case 0x1d: // Bypass, non-cacheable LE
    case 0x88: // Primary LE
    case 0x89: // Secondary LE
        switch(size) {
        case 2:
B
blueswir1 已提交
1172
            addr = bswap16(addr);
B
blueswir1 已提交
1173
            break;
1174
        case 4:
B
blueswir1 已提交
1175
            addr = bswap32(addr);
B
blueswir1 已提交
1176
            break;
1177
        case 8:
B
blueswir1 已提交
1178
            addr = bswap64(addr);
B
blueswir1 已提交
1179
            break;
1180 1181 1182 1183 1184 1185 1186
        default:
            break;
        }
    default:
        break;
    }

B
bellard 已提交
1187
    switch(asi) {
1188 1189 1190 1191 1192
    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 已提交
1193 1194 1195
            if (env->hpstate & HS_PRIV) {
                switch(size) {
                case 1:
B
blueswir1 已提交
1196
                    stb_hypv(addr, val);
B
blueswir1 已提交
1197 1198
                    break;
                case 2:
B
blueswir1 已提交
1199
                    stw_hypv(addr & ~1, val);
B
blueswir1 已提交
1200 1201
                    break;
                case 4:
B
blueswir1 已提交
1202
                    stl_hypv(addr & ~3, val);
B
blueswir1 已提交
1203 1204 1205
                    break;
                case 8:
                default:
B
blueswir1 已提交
1206
                    stq_hypv(addr & ~7, val);
B
blueswir1 已提交
1207 1208 1209 1210 1211
                    break;
                }
            } else {
                switch(size) {
                case 1:
B
blueswir1 已提交
1212
                    stb_kernel(addr, val);
B
blueswir1 已提交
1213 1214
                    break;
                case 2:
B
blueswir1 已提交
1215
                    stw_kernel(addr & ~1, val);
B
blueswir1 已提交
1216 1217
                    break;
                case 4:
B
blueswir1 已提交
1218
                    stl_kernel(addr & ~3, val);
B
blueswir1 已提交
1219 1220 1221
                    break;
                case 8:
                default:
B
blueswir1 已提交
1222
                    stq_kernel(addr & ~7, val);
B
blueswir1 已提交
1223 1224
                    break;
                }
1225 1226 1227 1228
            }
        } else {
            switch(size) {
            case 1:
B
blueswir1 已提交
1229
                stb_user(addr, val);
1230 1231
                break;
            case 2:
B
blueswir1 已提交
1232
                stw_user(addr & ~1, val);
1233 1234
                break;
            case 4:
B
blueswir1 已提交
1235
                stl_user(addr & ~3, val);
1236 1237 1238
                break;
            case 8:
            default:
B
blueswir1 已提交
1239
                stq_user(addr & ~7, val);
1240 1241 1242 1243
                break;
            }
        }
        break;
B
bellard 已提交
1244 1245
    case 0x14: // Bypass
    case 0x15: // Bypass, non-cacheable
1246 1247
    case 0x1c: // Bypass LE
    case 0x1d: // Bypass, non-cacheable LE
B
blueswir1 已提交
1248
        {
B
bellard 已提交
1249 1250
            switch(size) {
            case 1:
B
blueswir1 已提交
1251
                stb_phys(addr, val);
B
bellard 已提交
1252 1253
                break;
            case 2:
B
blueswir1 已提交
1254
                stw_phys(addr & ~1, val);
B
bellard 已提交
1255 1256
                break;
            case 4:
B
blueswir1 已提交
1257
                stl_phys(addr & ~3, val);
B
bellard 已提交
1258 1259 1260
                break;
            case 8:
            default:
B
blueswir1 已提交
1261
                stq_phys(addr & ~7, val);
B
bellard 已提交
1262 1263
                break;
            }
B
blueswir1 已提交
1264 1265
        }
        return;
B
bellard 已提交
1266 1267 1268 1269 1270 1271 1272
    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 已提交
1273
    case 0x81: // Secondary
B
bellard 已提交
1274
    case 0x89: // Secondary LE
B
blueswir1 已提交
1275 1276
        // XXX
        return;
B
bellard 已提交
1277
    case 0x45: // LSU
B
blueswir1 已提交
1278 1279 1280 1281
        {
            uint64_t oldreg;

            oldreg = env->lsu;
B
blueswir1 已提交
1282
            env->lsu = val & (DMMU_E | IMMU_E);
B
blueswir1 已提交
1283 1284 1285
            // Mappings generated during D/I MMU disabled mode are
            // invalid in normal mode
            if (oldreg != env->lsu) {
1286
                DPRINTF_MMU("LSU change: 0x%" PRIx64 " -> 0x%" PRIx64 "\n", oldreg, env->lsu);
B
bellard 已提交
1287
#ifdef DEBUG_MMU
B
blueswir1 已提交
1288
                dump_mmu(env);
B
bellard 已提交
1289
#endif
B
blueswir1 已提交
1290 1291 1292 1293
                tlb_flush(env, 1);
            }
            return;
        }
B
bellard 已提交
1294
    case 0x50: // I-MMU regs
B
blueswir1 已提交
1295
        {
B
blueswir1 已提交
1296
            int reg = (addr >> 3) & 0xf;
B
blueswir1 已提交
1297
            uint64_t oldreg;
1298

B
blueswir1 已提交
1299
            oldreg = env->immuregs[reg];
B
bellard 已提交
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
            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 已提交
1310 1311
                if ((val & 1) == 0)
                    val = 0; // Clear SFSR
B
bellard 已提交
1312 1313 1314 1315 1316 1317
                break;
            case 5: // TSB access
            case 6: // Tag access
            default:
                break;
            }
B
blueswir1 已提交
1318
            env->immuregs[reg] = val;
B
bellard 已提交
1319
            if (oldreg != env->immuregs[reg]) {
1320
                DPRINTF_MMU("mmu change reg[%d]: 0x%08" PRIx64 " -> 0x%08" PRIx64 "\n", reg, oldreg, env->immuregs[reg]);
B
bellard 已提交
1321
            }
1322
#ifdef DEBUG_MMU
B
blueswir1 已提交
1323
            dump_mmu(env);
B
bellard 已提交
1324
#endif
B
blueswir1 已提交
1325 1326
            return;
        }
B
bellard 已提交
1327
    case 0x54: // I-MMU data in
B
blueswir1 已提交
1328 1329 1330 1331 1332 1333 1334
        {
            unsigned int i;

            // Try finding an invalid entry
            for (i = 0; i < 64; i++) {
                if ((env->itlb_tte[i] & 0x8000000000000000ULL) == 0) {
                    env->itlb_tag[i] = env->immuregs[6];
B
blueswir1 已提交
1335
                    env->itlb_tte[i] = val;
B
blueswir1 已提交
1336 1337 1338 1339 1340 1341 1342
                    return;
                }
            }
            // Try finding an unlocked entry
            for (i = 0; i < 64; i++) {
                if ((env->itlb_tte[i] & 0x40) == 0) {
                    env->itlb_tag[i] = env->immuregs[6];
B
blueswir1 已提交
1343
                    env->itlb_tte[i] = val;
B
blueswir1 已提交
1344 1345 1346 1347 1348 1349
                    return;
                }
            }
            // error state?
            return;
        }
B
bellard 已提交
1350
    case 0x55: // I-MMU data access
B
blueswir1 已提交
1351
        {
B
blueswir1 已提交
1352
            unsigned int i = (addr >> 3) & 0x3f;
B
bellard 已提交
1353

B
blueswir1 已提交
1354
            env->itlb_tag[i] = env->immuregs[6];
B
blueswir1 已提交
1355
            env->itlb_tte[i] = val;
B
blueswir1 已提交
1356 1357
            return;
        }
B
bellard 已提交
1358
    case 0x57: // I-MMU demap
B
blueswir1 已提交
1359 1360
        // XXX
        return;
B
bellard 已提交
1361
    case 0x58: // D-MMU regs
B
blueswir1 已提交
1362
        {
B
blueswir1 已提交
1363
            int reg = (addr >> 3) & 0xf;
B
blueswir1 已提交
1364
            uint64_t oldreg;
1365

B
blueswir1 已提交
1366
            oldreg = env->dmmuregs[reg];
B
bellard 已提交
1367 1368 1369 1370 1371
            switch(reg) {
            case 0: // RO
            case 4:
                return;
            case 3: // SFSR
B
blueswir1 已提交
1372 1373
                if ((val & 1) == 0) {
                    val = 0; // Clear SFSR, Fault address
B
blueswir1 已提交
1374 1375
                    env->dmmuregs[4] = 0;
                }
B
blueswir1 已提交
1376
                env->dmmuregs[reg] = val;
B
bellard 已提交
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
                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 已提交
1387
            env->dmmuregs[reg] = val;
B
bellard 已提交
1388
            if (oldreg != env->dmmuregs[reg]) {
1389
                DPRINTF_MMU("mmu change reg[%d]: 0x%08" PRIx64 " -> 0x%08" PRIx64 "\n", reg, oldreg, env->dmmuregs[reg]);
B
bellard 已提交
1390
            }
1391
#ifdef DEBUG_MMU
B
blueswir1 已提交
1392
            dump_mmu(env);
B
bellard 已提交
1393
#endif
B
blueswir1 已提交
1394 1395
            return;
        }
B
bellard 已提交
1396
    case 0x5c: // D-MMU data in
B
blueswir1 已提交
1397 1398 1399 1400 1401 1402 1403
        {
            unsigned int i;

            // Try finding an invalid entry
            for (i = 0; i < 64; i++) {
                if ((env->dtlb_tte[i] & 0x8000000000000000ULL) == 0) {
                    env->dtlb_tag[i] = env->dmmuregs[6];
B
blueswir1 已提交
1404
                    env->dtlb_tte[i] = val;
B
blueswir1 已提交
1405 1406 1407 1408 1409 1410 1411
                    return;
                }
            }
            // Try finding an unlocked entry
            for (i = 0; i < 64; i++) {
                if ((env->dtlb_tte[i] & 0x40) == 0) {
                    env->dtlb_tag[i] = env->dmmuregs[6];
B
blueswir1 已提交
1412
                    env->dtlb_tte[i] = val;
B
blueswir1 已提交
1413 1414 1415 1416 1417 1418
                    return;
                }
            }
            // error state?
            return;
        }
B
bellard 已提交
1419
    case 0x5d: // D-MMU data access
B
blueswir1 已提交
1420
        {
B
blueswir1 已提交
1421
            unsigned int i = (addr >> 3) & 0x3f;
B
bellard 已提交
1422

B
blueswir1 已提交
1423
            env->dtlb_tag[i] = env->dmmuregs[6];
B
blueswir1 已提交
1424
            env->dtlb_tte[i] = val;
B
blueswir1 已提交
1425 1426
            return;
        }
B
bellard 已提交
1427
    case 0x5f: // D-MMU demap
B
bellard 已提交
1428
    case 0x49: // Interrupt data receive
B
blueswir1 已提交
1429 1430
        // XXX
        return;
B
bellard 已提交
1431 1432 1433 1434 1435 1436 1437
    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 已提交
1438 1439 1440 1441 1442 1443
    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 已提交
1444
    default:
B
blueswir1 已提交
1445
        do_unassigned_access(addr, 1, 0, 1);
B
blueswir1 已提交
1446
        return;
B
bellard 已提交
1447 1448
    }
}
1449
#endif /* CONFIG_USER_ONLY */
1450

B
blueswir1 已提交
1451
void helper_ldf_asi(target_ulong addr, int asi, int size, int rd)
1452 1453
{
    unsigned int i;
B
blueswir1 已提交
1454
    target_ulong val;
1455 1456 1457 1458 1459 1460

    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 已提交
1461 1462 1463 1464
        if (rd & 7) {
            raise_exception(TT_ILL_INSN);
            return;
        }
B
blueswir1 已提交
1465
        if (addr & 0x3f) {
B
blueswir1 已提交
1466 1467 1468 1469
            raise_exception(TT_UNALIGNED);
            return;
        }
        for (i = 0; i < 16; i++) {
B
blueswir1 已提交
1470 1471
            *(uint32_t *)&env->fpr[rd++] = helper_ld_asi(addr, asi & 0x8f, 4, 0);
            addr += 4;
1472 1473 1474 1475 1476 1477 1478
        }

        return;
    default:
        break;
    }

B
blueswir1 已提交
1479
    val = helper_ld_asi(addr, asi, size, 0);
1480 1481 1482
    switch(size) {
    default:
    case 4:
B
blueswir1 已提交
1483
        *((uint32_t *)&FT0) = val;
1484 1485
        break;
    case 8:
B
blueswir1 已提交
1486
        *((int64_t *)&DT0) = val;
1487
        break;
B
blueswir1 已提交
1488 1489 1490 1491 1492
#if defined(CONFIG_USER_ONLY)
    case 16:
        // XXX
        break;
#endif
1493 1494 1495
    }
}

B
blueswir1 已提交
1496
void helper_stf_asi(target_ulong addr, int asi, int size, int rd)
1497 1498
{
    unsigned int i;
B
blueswir1 已提交
1499
    target_ulong val = 0;
1500 1501 1502 1503 1504 1505

    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 已提交
1506 1507 1508 1509
        if (rd & 7) {
            raise_exception(TT_ILL_INSN);
            return;
        }
B
blueswir1 已提交
1510
        if (addr & 0x3f) {
B
blueswir1 已提交
1511 1512 1513 1514
            raise_exception(TT_UNALIGNED);
            return;
        }
        for (i = 0; i < 16; i++) {
B
blueswir1 已提交
1515 1516 1517
            val = *(uint32_t *)&env->fpr[rd++];
            helper_st_asi(addr, val, asi & 0x8f, 4);
            addr += 4;
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
        }

        return;
    default:
        break;
    }

    switch(size) {
    default:
    case 4:
B
blueswir1 已提交
1528
        val = *((uint32_t *)&FT0);
1529 1530
        break;
    case 8:
B
blueswir1 已提交
1531
        val = *((int64_t *)&DT0);
1532
        break;
B
blueswir1 已提交
1533 1534 1535 1536 1537
#if defined(CONFIG_USER_ONLY)
    case 16:
        // XXX
        break;
#endif
1538
    }
B
blueswir1 已提交
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
    helper_st_asi(addr, val, asi, size);
}

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

    val1 &= 0xffffffffUL;
    ret = helper_ld_asi(addr, asi, 4, 0);
    ret &= 0xffffffffUL;
    if (val1 == ret)
        helper_st_asi(addr, val2 & 0xffffffffUL, asi, 4);
    return ret;
1553 1554
}

B
blueswir1 已提交
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
target_ulong helper_casx_asi(target_ulong addr, target_ulong val1,
                             target_ulong val2, uint32_t asi)
{
    target_ulong ret;

    ret = helper_ld_asi(addr, asi, 8, 0);
    if (val1 == ret)
        helper_st_asi(addr, val2, asi, 8);
    return ret;
}
1565
#endif /* TARGET_SPARC64 */
B
bellard 已提交
1566 1567

#ifndef TARGET_SPARC64
B
blueswir1 已提交
1568
void helper_rett(void)
1569
{
1570 1571
    unsigned int cwp;

1572 1573 1574
    if (env->psret == 1)
        raise_exception(TT_ILL_INSN);

1575
    env->psret = 1;
1576
    cwp = (env->cwp + 1) & (NWINDOWS - 1);
1577 1578 1579 1580 1581 1582
    if (env->wim & (1 << cwp)) {
        raise_exception(TT_WIN_UNF);
    }
    set_cwp(cwp);
    env->psrs = env->psrps;
}
B
bellard 已提交
1583
#endif
1584

B
blueswir1 已提交
1585 1586 1587 1588 1589
uint64_t helper_pack64(target_ulong high, target_ulong low)
{
    return ((uint64_t)high << 32) | (uint64_t)(low & 0xffffffff);
}

B
bellard 已提交
1590
void helper_ldfsr(void)
1591
{
B
bellard 已提交
1592
    int rnd_mode;
1593 1594
    switch (env->fsr & FSR_RD_MASK) {
    case FSR_RD_NEAREST:
B
bellard 已提交
1595
        rnd_mode = float_round_nearest_even;
B
blueswir1 已提交
1596
        break;
B
bellard 已提交
1597
    default:
1598
    case FSR_RD_ZERO:
B
bellard 已提交
1599
        rnd_mode = float_round_to_zero;
B
blueswir1 已提交
1600
        break;
1601
    case FSR_RD_POS:
B
bellard 已提交
1602
        rnd_mode = float_round_up;
B
blueswir1 已提交
1603
        break;
1604
    case FSR_RD_NEG:
B
bellard 已提交
1605
        rnd_mode = float_round_down;
B
blueswir1 已提交
1606
        break;
1607
    }
B
bellard 已提交
1608
    set_float_rounding_mode(rnd_mode, &env->fp_status);
1609
}
B
bellard 已提交
1610 1611 1612 1613 1614 1615

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

B
bellard 已提交
1617
#ifndef TARGET_SPARC64
B
blueswir1 已提交
1618
void helper_wrpsr(target_ulong new_psr)
1619
{
B
blueswir1 已提交
1620
    if ((new_psr & PSR_CWP) >= NWINDOWS)
1621 1622
        raise_exception(TT_ILL_INSN);
    else
B
blueswir1 已提交
1623
        PUT_PSR(env, new_psr);
1624 1625
}

B
blueswir1 已提交
1626
target_ulong helper_rdpsr(void)
1627
{
B
blueswir1 已提交
1628
    return GET_PSR(env);
1629
}
B
bellard 已提交
1630 1631 1632

#else

B
blueswir1 已提交
1633
target_ulong helper_popc(target_ulong val)
B
bellard 已提交
1634
{
B
blueswir1 已提交
1635
    return ctpop64(val);
B
bellard 已提交
1636
}
B
bellard 已提交
1637 1638 1639 1640 1641 1642

static inline uint64_t *get_gregset(uint64_t pstate)
{
    switch (pstate) {
    default:
    case 0:
B
blueswir1 已提交
1643
        return env->bgregs;
B
bellard 已提交
1644
    case PS_AG:
B
blueswir1 已提交
1645
        return env->agregs;
B
bellard 已提交
1646
    case PS_MG:
B
blueswir1 已提交
1647
        return env->mgregs;
B
bellard 已提交
1648
    case PS_IG:
B
blueswir1 已提交
1649
        return env->igregs;
B
bellard 已提交
1650 1651 1652
    }
}

1653
static inline void change_pstate(uint64_t new_pstate)
B
bellard 已提交
1654
{
1655
    uint64_t pstate_regs, new_pstate_regs;
B
bellard 已提交
1656 1657 1658 1659 1660
    uint64_t *src, *dst;

    pstate_regs = env->pstate & 0xc01;
    new_pstate_regs = new_pstate & 0xc01;
    if (new_pstate_regs != pstate_regs) {
B
blueswir1 已提交
1661 1662 1663 1664 1665
        // 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 已提交
1666 1667 1668 1669
    }
    env->pstate = new_pstate;
}

B
blueswir1 已提交
1670
void helper_wrpstate(target_ulong new_state)
1671
{
B
blueswir1 已提交
1672
    change_pstate(new_state & 0xf3f);
1673 1674
}

B
blueswir1 已提交
1675
void helper_done(void)
B
bellard 已提交
1676 1677
{
    env->tl--;
1678 1679 1680 1681 1682 1683 1684
    env->tsptr = &env->ts[env->tl];
    env->pc = env->tsptr->tpc;
    env->npc = env->tsptr->tnpc + 4;
    PUT_CCR(env, env->tsptr->tstate >> 32);
    env->asi = (env->tsptr->tstate >> 24) & 0xff;
    change_pstate((env->tsptr->tstate >> 8) & 0xf3f);
    PUT_CWP64(env, env->tsptr->tstate & 0xff);
B
bellard 已提交
1685 1686
}

B
blueswir1 已提交
1687
void helper_retry(void)
B
bellard 已提交
1688 1689
{
    env->tl--;
1690 1691 1692 1693 1694 1695 1696
    env->tsptr = &env->ts[env->tl];
    env->pc = env->tsptr->tpc;
    env->npc = env->tsptr->tnpc;
    PUT_CCR(env, env->tsptr->tstate >> 32);
    env->asi = (env->tsptr->tstate >> 24) & 0xff;
    change_pstate((env->tsptr->tstate >> 8) & 0xf3f);
    PUT_CWP64(env, env->tsptr->tstate & 0xff);
B
bellard 已提交
1697
}
B
bellard 已提交
1698
#endif
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732

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
B
blueswir1 已提交
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768
#ifdef DEBUG_PCALL
static const char * const excp_names[0x50] = {
    [TT_TFAULT] = "Instruction Access Fault",
    [TT_TMISS] = "Instruction Access MMU Miss",
    [TT_CODE_ACCESS] = "Instruction Access Error",
    [TT_ILL_INSN] = "Illegal Instruction",
    [TT_PRIV_INSN] = "Privileged Instruction",
    [TT_NFPU_INSN] = "FPU Disabled",
    [TT_FP_EXCP] = "FPU Exception",
    [TT_TOVF] = "Tag Overflow",
    [TT_CLRWIN] = "Clean Windows",
    [TT_DIV_ZERO] = "Division By Zero",
    [TT_DFAULT] = "Data Access Fault",
    [TT_DMISS] = "Data Access MMU Miss",
    [TT_DATA_ACCESS] = "Data Access Error",
    [TT_DPROT] = "Data Protection Error",
    [TT_UNALIGNED] = "Unaligned Memory Access",
    [TT_PRIV_ACT] = "Privileged Action",
    [TT_EXTINT | 0x1] = "External Interrupt 1",
    [TT_EXTINT | 0x2] = "External Interrupt 2",
    [TT_EXTINT | 0x3] = "External Interrupt 3",
    [TT_EXTINT | 0x4] = "External Interrupt 4",
    [TT_EXTINT | 0x5] = "External Interrupt 5",
    [TT_EXTINT | 0x6] = "External Interrupt 6",
    [TT_EXTINT | 0x7] = "External Interrupt 7",
    [TT_EXTINT | 0x8] = "External Interrupt 8",
    [TT_EXTINT | 0x9] = "External Interrupt 9",
    [TT_EXTINT | 0xa] = "External Interrupt 10",
    [TT_EXTINT | 0xb] = "External Interrupt 11",
    [TT_EXTINT | 0xc] = "External Interrupt 12",
    [TT_EXTINT | 0xd] = "External Interrupt 13",
    [TT_EXTINT | 0xe] = "External Interrupt 14",
    [TT_EXTINT | 0xf] = "External Interrupt 15",
};
#endif

1769 1770 1771 1772
void do_interrupt(int intno)
{
#ifdef DEBUG_PCALL
    if (loglevel & CPU_LOG_INT) {
B
blueswir1 已提交
1773
        static int count;
B
blueswir1 已提交
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
        const char *name;

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

        fprintf(logfile, "%6d: %s (v=%04x) pc=%016" PRIx64 " npc=%016" PRIx64
                " SP=%016" PRIx64 "\n",
                count, name, intno,
1793 1794
                env->pc,
                env->npc, env->regwptr[6]);
B
blueswir1 已提交
1795
        cpu_dump_state(env, logfile, fprintf, 0);
1796
#if 0
B
blueswir1 已提交
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
        {
            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");
        }
1808
#endif
B
blueswir1 已提交
1809
        count++;
1810 1811
    }
#endif
1812
#if !defined(CONFIG_USER_ONLY)
B
bellard 已提交
1813
    if (env->tl == MAXTL) {
B
bellard 已提交
1814
        cpu_abort(env, "Trap 0x%04x while trap level is MAXTL, Error state", env->exception_index);
B
blueswir1 已提交
1815
        return;
1816 1817
    }
#endif
1818 1819 1820 1821 1822 1823
    env->tsptr->tstate = ((uint64_t)GET_CCR(env) << 32) |
        ((env->asi & 0xff) << 24) | ((env->pstate & 0xf3f) << 8) |
        GET_CWP64(env);
    env->tsptr->tpc = env->pc;
    env->tsptr->tnpc = env->npc;
    env->tsptr->tt = intno;
1824 1825 1826 1827 1828 1829 1830 1831
    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 已提交
1832 1833 1834
    env->tbr &= ~0x7fffULL;
    env->tbr |= ((env->tl > 1) ? 1 << 14 : 0) | (intno << 5);
    if (env->tl < MAXTL - 1) {
B
blueswir1 已提交
1835
        env->tl++;
B
bellard 已提交
1836
    } else {
B
blueswir1 已提交
1837 1838 1839
        env->pstate |= PS_RED;
        if (env->tl != MAXTL)
            env->tl++;
B
bellard 已提交
1840
    }
1841
    env->tsptr = &env->ts[env->tl];
1842 1843 1844 1845 1846
    env->pc = env->tbr;
    env->npc = env->pc + 4;
    env->exception_index = 0;
}
#else
B
blueswir1 已提交
1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
#ifdef DEBUG_PCALL
static const char * const excp_names[0x80] = {
    [TT_TFAULT] = "Instruction Access Fault",
    [TT_ILL_INSN] = "Illegal Instruction",
    [TT_PRIV_INSN] = "Privileged Instruction",
    [TT_NFPU_INSN] = "FPU Disabled",
    [TT_WIN_OVF] = "Window Overflow",
    [TT_WIN_UNF] = "Window Underflow",
    [TT_UNALIGNED] = "Unaligned Memory Access",
    [TT_FP_EXCP] = "FPU Exception",
    [TT_DFAULT] = "Data Access Fault",
    [TT_TOVF] = "Tag Overflow",
    [TT_EXTINT | 0x1] = "External Interrupt 1",
    [TT_EXTINT | 0x2] = "External Interrupt 2",
    [TT_EXTINT | 0x3] = "External Interrupt 3",
    [TT_EXTINT | 0x4] = "External Interrupt 4",
    [TT_EXTINT | 0x5] = "External Interrupt 5",
    [TT_EXTINT | 0x6] = "External Interrupt 6",
    [TT_EXTINT | 0x7] = "External Interrupt 7",
    [TT_EXTINT | 0x8] = "External Interrupt 8",
    [TT_EXTINT | 0x9] = "External Interrupt 9",
    [TT_EXTINT | 0xa] = "External Interrupt 10",
    [TT_EXTINT | 0xb] = "External Interrupt 11",
    [TT_EXTINT | 0xc] = "External Interrupt 12",
    [TT_EXTINT | 0xd] = "External Interrupt 13",
    [TT_EXTINT | 0xe] = "External Interrupt 14",
    [TT_EXTINT | 0xf] = "External Interrupt 15",
    [TT_TOVF] = "Tag Overflow",
    [TT_CODE_ACCESS] = "Instruction Access Error",
    [TT_DATA_ACCESS] = "Data Access Error",
    [TT_DIV_ZERO] = "Division By Zero",
    [TT_NCP_INSN] = "Coprocessor Disabled",
};
#endif

1882 1883 1884 1885 1886 1887
void do_interrupt(int intno)
{
    int cwp;

#ifdef DEBUG_PCALL
    if (loglevel & CPU_LOG_INT) {
B
blueswir1 已提交
1888
        static int count;
B
blueswir1 已提交
1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
        const char *name;

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

        fprintf(logfile, "%6d: %s (v=%02x) pc=%08x npc=%08x SP=%08x\n",
                count, name, intno,
1903 1904
                env->pc,
                env->npc, env->regwptr[6]);
B
blueswir1 已提交
1905
        cpu_dump_state(env, logfile, fprintf, 0);
1906
#if 0
B
blueswir1 已提交
1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
        {
            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");
        }
1918
#endif
B
blueswir1 已提交
1919
        count++;
1920 1921
    }
#endif
1922
#if !defined(CONFIG_USER_ONLY)
1923
    if (env->psret == 0) {
B
bellard 已提交
1924
        cpu_abort(env, "Trap 0x%02x while interrupts disabled, Error state", env->exception_index);
B
blueswir1 已提交
1925
        return;
1926 1927 1928
    }
#endif
    env->psret = 0;
1929
    cwp = (env->cwp - 1) & (NWINDOWS - 1);
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
    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

1942
#if !defined(CONFIG_USER_ONLY)
1943

1944 1945 1946
static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
                                void *retaddr);

1947
#define MMUSUFFIX _mmu
1948
#define ALIGNED_ONLY
1949 1950 1951 1952 1953
#ifdef __s390__
# define GETPC() ((void*)((unsigned long)__builtin_return_address(0) & 0x7fffffffUL))
#else
# define GETPC() (__builtin_return_address(0))
#endif
1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966

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

1967 1968 1969
static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
                                void *retaddr)
{
B
blueswir1 已提交
1970 1971 1972 1973
#ifdef DEBUG_UNALIGNED
    printf("Unaligned access to 0x%x from 0x%x\n", addr, env->pc);
#endif
    raise_exception(TT_UNALIGNED);
1974
}
1975 1976 1977 1978 1979

/* 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 */
1980
void tlb_fill(target_ulong addr, int is_write, int mmu_idx, void *retaddr)
1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
{
    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;

1992
    ret = cpu_sparc_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
    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
2010 2011

#ifndef TARGET_SPARC64
2012
void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
2013 2014 2015 2016 2017 2018 2019 2020
                          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;
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031
#ifdef DEBUG_UNASSIGNED
    if (is_asi)
        printf("Unassigned mem %s access to " TARGET_FMT_plx " asi 0x%02x from "
               TARGET_FMT_lx "\n",
               is_exec ? "exec" : is_write ? "write" : "read", addr, is_asi,
               env->pc);
    else
        printf("Unassigned mem %s access to " TARGET_FMT_plx " from "
               TARGET_FMT_lx "\n",
               is_exec ? "exec" : is_write ? "write" : "read", addr, env->pc);
#endif
2032
    if (env->mmuregs[3]) /* Fault status register */
B
blueswir1 已提交
2033
        env->mmuregs[3] = 1; /* overflow (not read before another fault) */
2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
    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)) {
2045 2046 2047 2048
        if (is_exec)
            raise_exception(TT_CODE_ACCESS);
        else
            raise_exception(TT_DATA_ACCESS);
2049 2050 2051 2052
    }
    env = saved_env;
}
#else
2053
void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
2054 2055 2056 2057 2058 2059 2060 2061 2062
                          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;
2063
    printf("Unassigned mem access to " TARGET_FMT_plx " from " TARGET_FMT_lx "\n",
2064 2065 2066
           addr, env->pc);
    env = saved_env;
#endif
2067 2068 2069 2070
    if (is_exec)
        raise_exception(TT_CODE_ACCESS);
    else
        raise_exception(TT_DATA_ACCESS);
2071 2072
}
#endif
2073