op_helper.c 50.9 KB
Newer Older
1
#include "exec.h"
blueswir1's avatar
blueswir1 committed
2
#include "host-utils.h"
3

bellard's avatar
bellard committed
4
//#define DEBUG_PCALL
bellard's avatar
bellard committed
5
//#define DEBUG_MMU
6
//#define DEBUG_MXCC
blueswir1's avatar
blueswir1 committed
7
//#define DEBUG_UNALIGNED
8
//#define DEBUG_UNASSIGNED
bellard's avatar
bellard committed
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

bellard's avatar
bellard committed
24
25
26
27
void raise_exception(int tt)
{
    env->exception_index = tt;
    cpu_loop_exit();
28
}
bellard's avatar
bellard committed
29

30
31
32
33
34
void check_ieee_exceptions()
{
     T0 = get_float_exception_flags(&env->fp_status);
     if (T0)
     {
blueswir1's avatar
blueswir1 committed
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
     }
}

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

void do_fitod(void)
{
ths's avatar
ths committed
71
    DT0 = int32_to_float64(*((int32_t *)&FT1), &env->fp_status);
bellard's avatar
bellard committed
72
}
73
74
75
76
77
78
79
80

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

81
82
83
84
85
86
87
88
89
90
91
92
93
94
#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();
}
95
96
97
98
99
100
101
102
103

#if defined(CONFIG_USER_ONLY)
void do_fxtoq(void)
{
    set_float_exception_flags(0, &env->fp_status);
    QT0 = int64_to_float128(*((int32_t *)&DT1), &env->fp_status);
    check_ieee_exceptions();
}
#endif
104
#endif
bellard's avatar
bellard committed
105
106
107
#endif

void do_fabss(void)
108
{
bellard's avatar
bellard committed
109
    FT0 = float32_abs(FT1);
110
111
}

bellard's avatar
bellard committed
112
113
114
115
116
#ifdef TARGET_SPARC64
void do_fabsd(void)
{
    DT0 = float64_abs(DT1);
}
117
118
119
120
121
122
123

#if defined(CONFIG_USER_ONLY)
void do_fabsq(void)
{
    QT0 = float128_abs(QT1);
}
#endif
bellard's avatar
bellard committed
124
125
#endif

bellard's avatar
bellard committed
126
void do_fsqrts(void)
127
{
128
    set_float_exception_flags(0, &env->fp_status);
bellard's avatar
bellard committed
129
    FT0 = float32_sqrt(FT1, &env->fp_status);
130
    check_ieee_exceptions();
131
132
}

bellard's avatar
bellard committed
133
void do_fsqrtd(void)
134
{
135
    set_float_exception_flags(0, &env->fp_status);
bellard's avatar
bellard committed
136
    DT0 = float64_sqrt(DT1, &env->fp_status);
137
    check_ieee_exceptions();
138
139
}

140
141
142
143
144
145
146
147
148
#if defined(CONFIG_USER_ONLY)
void do_fsqrtq(void)
{
    set_float_exception_flags(0, &env->fp_status);
    QT0 = float128_sqrt(QT1, &env->fp_status);
    check_ieee_exceptions();
}
#endif

149
#define GEN_FCMP(name, size, reg1, reg2, FS, TRAP)                      \
bellard's avatar
bellard committed
150
151
152
153
154
155
    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;                           \
156
            if ((env->fsr & FSR_NVM) || TRAP) {                         \
bellard's avatar
bellard committed
157
                env->fsr |= T0;                                         \
158
159
                env->fsr |= FSR_NVC;                                    \
                env->fsr |= FSR_FTT_IEEE_EXCP;                          \
bellard's avatar
bellard committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
                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;                                                 \
176
177
    }

178
179
180
181
182
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);
bellard's avatar
bellard committed
183

184
185
186
187
188
#ifdef CONFIG_USER_ONLY
GEN_FCMP(fcmpq, float128, QT0, QT1, 0, 0);
GEN_FCMP(fcmpeq, float128, QT0, QT1, 0, 1);
#endif

bellard's avatar
bellard committed
189
#ifdef TARGET_SPARC64
190
191
192
193
194
195
196
197
198
199
200
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);
bellard's avatar
bellard committed
201

202
203
GEN_FCMP(fcmpes_fcc2, float32, FT0, FT1, 24, 1);
GEN_FCMP(fcmped_fcc2, float64, DT0, DT1, 24, 1);
bellard's avatar
bellard committed
204

205
206
GEN_FCMP(fcmpes_fcc3, float32, FT0, FT1, 26, 1);
GEN_FCMP(fcmped_fcc3, float64, DT0, DT1, 26, 1);
207
208
209
210
211
212
213
214
#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
bellard's avatar
bellard committed
215
216
217
#endif

#ifndef TARGET_SPARC64
218
#ifndef CONFIG_USER_ONLY
219
220
221
222
223
224
225
226
227
228
229
230
231

#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

bellard's avatar
bellard committed
232
void helper_ld_asi(int asi, int size, int sign)
233
{
bellard's avatar
bellard committed
234
    uint32_t ret = 0;
235
    uint64_t tmp;
236
237
238
#ifdef DEBUG_MXCC
    uint32_t last_T0 = T0;
#endif
bellard's avatar
bellard committed
239
240

    switch (asi) {
241
    case 2: /* SuperSparc MXCC registers */
242
243
244
        switch (T0) {
        case 0x01c00a00: /* MXCC control register */
            if (size == 8) {
245
246
                ret = env->mxccregs[3] >> 32;
                T0 = env->mxccregs[3];
247
248
249
250
251
252
253
254
255
            } 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;
256
257
258
259
260
261
262
263
        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;
264
265
        case 0x01c00f00: /* MBus port address register */
            if (size == 8) {
266
267
                ret = env->mxccregs[7] >> 32;
                T0 = env->mxccregs[7];
268
269
270
271
272
273
274
275
276
277
278
279
            } 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
280
        break;
281
    case 3: /* MMU probe */
blueswir1's avatar
blueswir1 committed
282
283
284
285
286
287
288
289
290
291
        {
            int mmulev;

            mmulev = (T0 >> 8) & 15;
            if (mmulev > 4)
                ret = 0;
            else {
                ret = mmu_probe(env, T0, mmulev);
                //bswap32s(&ret);
            }
292
            DPRINTF_MMU("mmu_probe: 0x%08x (lev %d) -> 0x%08x\n", T0, mmulev, ret);
blueswir1's avatar
blueswir1 committed
293
294
        }
        break;
295
    case 4: /* read MMU regs */
blueswir1's avatar
blueswir1 committed
296
        {
297
            int reg = (T0 >> 8) & 0x1f;
298

blueswir1's avatar
blueswir1 committed
299
300
            ret = env->mmuregs[reg];
            if (reg == 3) /* Fault status cleared on read */
301
302
303
304
305
                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];
306
            DPRINTF_MMU("mmu_read: reg[%d] = 0x%08x\n", reg, ret);
blueswir1's avatar
blueswir1 committed
307
308
        }
        break;
309
310
311
312
313
314
315
316
317
318
319
320
321
    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:
322
323
            tmp = ldq_code(T0 & ~7);
            ret = tmp >> 32;
324
            T0 = tmp;
325
326
327
            break;
        }
        break;
328
329
330
331
332
333
334
335
336
337
338
339
340
    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:
341
342
            tmp = ldq_user(T0 & ~7);
            ret = tmp >> 32;
343
            T0 = tmp;
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
            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:
360
361
            tmp = ldq_kernel(T0 & ~7);
            ret = tmp >> 32;
362
            T0 = tmp;
363
364
365
            break;
        }
        break;
366
367
368
369
370
371
    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 */
bellard's avatar
bellard committed
372
373
374
375
376
377
378
379
380
381
382
        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;
bellard's avatar
bellard committed
383
        case 8:
384
385
            tmp = ldq_phys(T0 & ~7);
            ret = tmp >> 32;
386
            T0 = tmp;
blueswir1's avatar
blueswir1 committed
387
            break;
bellard's avatar
bellard committed
388
        }
blueswir1's avatar
blueswir1 committed
389
        break;
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
    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:
407
            tmp = ldq_phys((target_phys_addr_t)(T0 & ~7)
408
                           | ((target_phys_addr_t)(asi & 0xf) << 32));
409
            ret = tmp >> 32;
410
            T0 = tmp;
blueswir1's avatar
blueswir1 committed
411
            break;
412
        }
blueswir1's avatar
blueswir1 committed
413
        break;
414
    case 0x21 ... 0x2d: /* MMU passthrough, unassigned */
415
    default:
416
        do_unassigned_access(T0, 0, 0, 1);
blueswir1's avatar
blueswir1 committed
417
418
        ret = 0;
        break;
419
    }
420
421
422
423
    if (sign) {
        switch(size) {
        case 1:
            T1 = (int8_t) ret;
blueswir1's avatar
blueswir1 committed
424
            break;
425
426
        case 2:
            T1 = (int16_t) ret;
blueswir1's avatar
blueswir1 committed
427
            break;
428
429
430
431
432
433
434
        default:
            T1 = ret;
            break;
        }
    }
    else
        T1 = ret;
435
436
}

437
void helper_st_asi(int asi, int size)
438
439
{
    switch(asi) {
440
    case 2: /* SuperSparc MXCC registers */
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
        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)
bellard's avatar
bellard committed
494
                env->mxccregs[3] = (env->mxccregs[0xa] & 0xffffffff00000000ULL) | T1;
495
496
497
498
            else
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
            break;
        case 0x01c00e00: /* MXCC error register  */
499
            // writing a 1 bit clears the error
500
            if (size == 8)
501
                env->mxccregs[6] &= ~(((uint64_t)T1 << 32) | T2);
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
            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
519
        break;
520
    case 3: /* MMU flush */
blueswir1's avatar
blueswir1 committed
521
522
        {
            int mmulev;
bellard's avatar
bellard committed
523

blueswir1's avatar
blueswir1 committed
524
            mmulev = (T0 >> 8) & 15;
525
            DPRINTF_MMU("mmu flush level %d\n", mmulev);
blueswir1's avatar
blueswir1 committed
526
527
528
529
530
531
532
533
534
535
536
537
538
            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;
            }
bellard's avatar
bellard committed
539
#ifdef DEBUG_MMU
blueswir1's avatar
blueswir1 committed
540
            dump_mmu(env);
bellard's avatar
bellard committed
541
#endif
blueswir1's avatar
blueswir1 committed
542
543
            return;
        }
544
    case 4: /* write MMU regs */
blueswir1's avatar
blueswir1 committed
545
        {
546
            int reg = (T0 >> 8) & 0x1f;
blueswir1's avatar
blueswir1 committed
547
            uint32_t oldreg;
548

blueswir1's avatar
blueswir1 committed
549
            oldreg = env->mmuregs[reg];
bellard's avatar
bellard committed
550
551
            switch(reg) {
            case 0:
552
553
                env->mmuregs[reg] = (env->mmuregs[reg] & 0xff000000) |
                                    (T1 & 0x00ffffff);
blueswir1's avatar
blueswir1 committed
554
555
                // Mappings generated during no-fault mode or MMU
                // disabled mode are invalid in normal mode
556
557
                if ((oldreg & (MMU_E | MMU_NF | env->mmu_bm)) !=
                    (env->mmuregs[reg] & (MMU_E | MMU_NF | env->mmu_bm)))
bellard's avatar
bellard committed
558
559
560
                    tlb_flush(env, 1);
                break;
            case 2:
blueswir1's avatar
blueswir1 committed
561
                env->mmuregs[reg] = T1;
bellard's avatar
bellard committed
562
563
564
565
566
567
568
569
570
                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;
571
572
573
574
575
576
            case 0x13:
                env->mmuregs[3] = T1;
                break;
            case 0x14:
                env->mmuregs[4] = T1;
                break;
bellard's avatar
bellard committed
577
            default:
blueswir1's avatar
blueswir1 committed
578
                env->mmuregs[reg] = T1;
bellard's avatar
bellard committed
579
580
581
                break;
            }
            if (oldreg != env->mmuregs[reg]) {
582
                DPRINTF_MMU("mmu change reg[%d]: 0x%08x -> 0x%08x\n", reg, oldreg, env->mmuregs[reg]);
bellard's avatar
bellard committed
583
            }
584
#ifdef DEBUG_MMU
blueswir1's avatar
blueswir1 committed
585
            dump_mmu(env);
bellard's avatar
bellard committed
586
#endif
blueswir1's avatar
blueswir1 committed
587
588
            return;
        }
589
590
591
592
593
594
595
596
597
598
599
600
601
    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:
602
            stq_user(T0 & ~7, ((uint64_t)T1 << 32) | T2);
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
            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:
619
            stq_kernel(T0 & ~7, ((uint64_t)T1 << 32) | T2);
620
621
622
            break;
        }
        break;
623
624
625
626
627
628
629
630
631
632
    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;
bellard's avatar
bellard committed
633
    case 0x17: /* Block copy, sta access */
blueswir1's avatar
blueswir1 committed
634
635
636
637
        {
            // value (T1) = src
            // address (T0) = dst
            // copy 32 bytes
638
639
            unsigned int i;
            uint32_t src = T1 & ~3, dst = T0 & ~3, temp;
640

641
642
643
644
            for (i = 0; i < 32; i += 4, src += 4, dst += 4) {
                temp = ldl_kernel(src);
                stl_kernel(dst, temp);
            }
blueswir1's avatar
blueswir1 committed
645
646
        }
        return;
bellard's avatar
bellard committed
647
    case 0x1f: /* Block fill, stda access */
blueswir1's avatar
blueswir1 committed
648
649
650
651
        {
            // value (T1, T2)
            // address (T0) = dst
            // fill 32 bytes
652
653
654
            unsigned int i;
            uint32_t dst = T0 & 7;
            uint64_t val;
bellard's avatar
bellard committed
655

656
657
658
659
            val = (((uint64_t)T1) << 32) | T2;

            for (i = 0; i < 32; i += 8, dst += 8)
                stq_kernel(dst, val);
blueswir1's avatar
blueswir1 committed
660
661
        }
        return;
662
    case 0x20: /* MMU passthrough */
blueswir1's avatar
blueswir1 committed
663
        {
bellard's avatar
bellard committed
664
665
666
667
668
669
670
671
672
673
674
            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;
bellard's avatar
bellard committed
675
            case 8:
676
                stq_phys(T0 & ~7, ((uint64_t)T1 << 32) | T2);
bellard's avatar
bellard committed
677
                break;
bellard's avatar
bellard committed
678
            }
blueswir1's avatar
blueswir1 committed
679
680
        }
        return;
681
682
    case 0x2e: /* MMU passthrough, 0xexxxxxxxx */
    case 0x2f: /* MMU passthrough, 0xfxxxxxxxx */
blueswir1's avatar
blueswir1 committed
683
        {
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
            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:
699
700
701
                stq_phys((target_phys_addr_t)(T0 & ~7)
                           | ((target_phys_addr_t)(asi & 0xf) << 32),
                         ((uint64_t)T1 << 32) | T2);
702
703
                break;
            }
blueswir1's avatar
blueswir1 committed
704
705
        }
        return;
706
707
708
709
710
    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 */
711
    case 0x21 ... 0x2d: /* MMU passthrough, unassigned */
712
    default:
713
        do_unassigned_access(T0, 1, 0, 1);
blueswir1's avatar
blueswir1 committed
714
        return;
715
716
717
    }
}

718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
#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);
blueswir1's avatar
blueswir1 committed
771
            break;
772
773
        case 4:
            ret = bswap32(ret);
blueswir1's avatar
blueswir1 committed
774
            break;
775
776
        case 8:
            ret = bswap64(ret);
blueswir1's avatar
blueswir1 committed
777
            break;
778
779
780
781
782
783
784
785
786
787
788
789
        default:
            break;
        }
    default:
        break;
    }

    /* Convert to signed number */
    if (sign) {
        switch(size) {
        case 1:
            ret = (int8_t) ret;
blueswir1's avatar
blueswir1 committed
790
            break;
791
792
        case 2:
            ret = (int16_t) ret;
blueswir1's avatar
blueswir1 committed
793
            break;
794
795
        case 4:
            ret = (int32_t) ret;
blueswir1's avatar
blueswir1 committed
796
            break;
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
        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);
blueswir1's avatar
blueswir1 committed
816
            break;
817
818
        case 4:
            T0 = bswap32(T0);
blueswir1's avatar
blueswir1 committed
819
            break;
820
821
        case 8:
            T0 = bswap64(T0);
blueswir1's avatar
blueswir1 committed
822
            break;
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
        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 */
bellard's avatar
bellard committed
867
868
869

void helper_ld_asi(int asi, int size, int sign)
{
bellard's avatar
bellard committed
870
    uint64_t ret = 0;
bellard's avatar
bellard committed
871

blueswir1's avatar
blueswir1 committed
872
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
blueswir1's avatar
blueswir1 committed
873
        || (asi >= 0x30 && asi < 0x80 && !(env->hpstate & HS_PRIV)))
blueswir1's avatar
blueswir1 committed
874
        raise_exception(TT_PRIV_ACT);
bellard's avatar
bellard committed
875
876

    switch (asi) {
877
878
879
880
881
882
883
    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)) {
blueswir1's avatar
blueswir1 committed
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
            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;
                }
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
            }
        } 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;
bellard's avatar
bellard committed
935
936
    case 0x14: // Bypass
    case 0x15: // Bypass, non-cacheable
937
938
    case 0x1c: // Bypass LE
    case 0x1d: // Bypass, non-cacheable LE
blueswir1's avatar
blueswir1 committed
939
        {
bellard's avatar
bellard committed
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
            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;
            }
blueswir1's avatar
blueswir1 committed
955
956
            break;
        }
bellard's avatar
bellard committed
957
958
959
960
961
962
963
    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
964
    case 0x81: // Secondary
bellard's avatar
bellard committed
965
966
967
    case 0x83: // Secondary no-fault
    case 0x89: // Secondary LE
    case 0x8b: // Secondary no-fault LE
blueswir1's avatar
blueswir1 committed
968
969
        // XXX
        break;
bellard's avatar
bellard committed
970
    case 0x45: // LSU
blueswir1's avatar
blueswir1 committed
971
972
        ret = env->lsu;
        break;
bellard's avatar
bellard committed
973
    case 0x50: // I-MMU regs
blueswir1's avatar
blueswir1 committed
974
975
        {
            int reg = (T0 >> 3) & 0xf;
bellard's avatar
bellard committed
976

blueswir1's avatar
blueswir1 committed
977
978
979
            ret = env->immuregs[reg];
            break;
        }
bellard's avatar
bellard committed
980
981
982
    case 0x51: // I-MMU 8k TSB pointer
    case 0x52: // I-MMU 64k TSB pointer
    case 0x55: // I-MMU data access
blueswir1's avatar
blueswir1 committed
983
984
        // XXX
        break;
bellard's avatar
bellard committed
985
    case 0x56: // I-MMU tag read
blueswir1's avatar
blueswir1 committed
986
987
988
989
990
991
992
993
994
995
996
997
998
        {
            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;
        }
bellard's avatar
bellard committed
999
    case 0x58: // D-MMU regs
blueswir1's avatar
blueswir1 committed
1000
1001
        {
            int reg = (T0 >> 3) & 0xf;
bellard's avatar
bellard committed
1002

blueswir1's avatar
blueswir1 committed
1003
1004
1005
            ret = env->dmmuregs[reg];
            break;
        }
bellard's avatar
bellard committed
1006
    case 0x5e: // D-MMU tag read
blueswir1's avatar
blueswir1 committed
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
        {
            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;
        }
bellard's avatar
bellard committed
1020
1021
1022
1023
    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
bellard's avatar
bellard committed
1024
1025
1026
    case 0x48: // Interrupt dispatch, RO
    case 0x49: // Interrupt data receive
    case 0x7f: // Incoming interrupt vector, RO
blueswir1's avatar
blueswir1 committed
1027
1028
        // XXX
        break;
bellard's avatar
bellard committed
1029
1030
1031
1032
    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
bellard's avatar
bellard committed
1033
    case 0x77: // Interrupt vector, WO
bellard's avatar
bellard committed
1034
    default:
1035
        do_unassigned_access(T0, 0, 0, 1);
blueswir1's avatar
blueswir1 committed
1036
1037
        ret = 0;
        break;
bellard's avatar
bellard committed
1038
    }
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053

    /* 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);
blueswir1's avatar
blueswir1 committed
1054
            break;
1055
1056
        case 4:
            ret = bswap32(ret);
blueswir1's avatar
blueswir1 committed
1057
            break;
1058
1059
        case 8:
            ret = bswap64(ret);
blueswir1's avatar
blueswir1 committed
1060
            break;
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
        default:
            break;
        }
    default:
        break;
    }

    /* Convert to signed number */
    if (sign) {
        switch(size) {
        case 1:
            ret = (int8_t) ret;
blueswir1's avatar
blueswir1 committed
1073
            break;
1074
1075
        case 2:
            ret = (int16_t) ret;
blueswir1's avatar
blueswir1 committed
1076
            break;
1077
1078
        case 4:
            ret = (int32_t) ret;
blueswir1's avatar
blueswir1 committed
1079
            break;
1080
1081
1082
1083
        default:
            break;
        }
    }
bellard's avatar
bellard committed
1084
1085
1086
    T1 = ret;
}

1087
void helper_st_asi(int asi, int size)
bellard's avatar
bellard committed
1088
{
blueswir1's avatar
blueswir1 committed
1089
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
blueswir1's avatar
blueswir1 committed
1090
        || (asi >= 0x30 && asi < 0x80 && !(env->hpstate & HS_PRIV)))
blueswir1's avatar
blueswir1 committed
1091
        raise_exception(TT_PRIV_ACT);
bellard's avatar
bellard committed
1092

1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
    /* 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);
blueswir1's avatar
blueswir1 committed
1105
            break;
1106
1107
        case 4:
            T0 = bswap32(T0);
blueswir1's avatar
blueswir1 committed
1108
            break;
1109
1110
        case 8:
            T0 = bswap64(T0);
blueswir1's avatar
blueswir1 committed
1111
            break;
1112
1113
1114
1115
1116
1117
1118
        default:
            break;
        }
    default:
        break;
    }

bellard's avatar
bellard committed
1119
    switch(asi) {
1120
1121
1122
1123
1124
    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)) {
blueswir1's avatar
blueswir1 committed
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
            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;
                }
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
            }
        } 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;
bellard's avatar
bellard committed
1176
1177
    case 0x14: // Bypass
    case 0x15: // Bypass, non-cacheable
1178
1179
    case 0x1c: // Bypass LE
    case 0x1d: // Bypass, non-cacheable LE
blueswir1's avatar
blueswir1 committed
1180
        {
bellard's avatar
bellard committed
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
            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;
            }
blueswir1's avatar
blueswir1 committed
1196
1197
        }
        return;
bellard's avatar
bellard committed
1198
1199
1200
1201
1202
1203
1204
    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
blueswir1's avatar
blueswir1 committed
1205
    case 0x81: // Secondary
bellard's avatar
bellard committed
1206
    case 0x89: // Secondary LE
blueswir1's avatar
blueswir1 committed
1207
1208
        // XXX
        return;
bellard's avatar
bellard committed
1209
    case 0x45: // LSU
blueswir1's avatar
blueswir1 committed
1210
1211
1212
1213
1214
1215
1216
1217
        {
            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) {
1218
                DPRINTF_MMU("LSU change: 0x%" PRIx64 " -> 0x%" PRIx64 "\n", oldreg, env->lsu);
bellard's avatar
bellard committed
1219
#ifdef DEBUG_MMU
blueswir1's avatar
blueswir1 committed
1220
                dump_mmu(env);
bellard's avatar
bellard committed
1221
#endif
blueswir1's avatar
blueswir1 committed
1222
1223
1224
1225
                tlb_flush(env, 1);
            }
            return;
        }
bellard's avatar
bellard committed
1226
    case 0x50: // I-MMU regs
blueswir1's avatar
blueswir1 committed
1227
1228
1229
        {
            int reg = (T0 >> 3) & 0xf;
            uint64_t oldreg;
1230

blueswir1's avatar
blueswir1 committed
1231
            oldreg = env->immuregs[reg];
bellard's avatar
bellard committed
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
            switch(reg) {
            case 0: // RO
            case 4:
                return;
            case 1: // Not in I-MMU
            case 2:
            case 7:
            case 8:
                return;
            case 3: // SFSR
blueswir1's avatar
blueswir1 committed
1242
1243
                if ((T1 & 1) == 0)
                    T1 = 0; // Clear SFSR
bellard's avatar
bellard committed
1244
1245
1246
1247
1248
1249
                break;
            case 5: // TSB access
            case 6: // Tag access
            default:
                break;
            }
blueswir1's avatar
blueswir1 committed
1250
            env->immuregs[reg] = T1;
bellard's avatar
bellard committed
1251
            if (oldreg != env->immuregs[reg]) {
1252
                DPRINTF_MMU("mmu change reg[%d]: 0x%08" PRIx64 " -> 0x%08" PRIx64 "\n", reg, oldreg, env->immuregs[reg]);
bellard's avatar
bellard committed
1253
            }
1254
#ifdef DEBUG_MMU
blueswir1's avatar
blueswir1 committed
1255
            dump_mmu(env);
bellard's avatar
bellard committed
1256
#endif
blueswir1's avatar
blueswir1 committed
1257
1258
            return;
        }
bellard's avatar
bellard committed
1259
    case 0x54: // I-MMU data in
blueswir1's avatar
blueswir1 committed
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
        {
            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;
        }
bellard's avatar
bellard committed
1282
    case 0x55: // I-MMU data access
blueswir1's avatar
blueswir1 committed
1283
1284
        {
            unsigned int i = (T0 >> 3) & 0x3f;
bellard's avatar
bellard committed
1285

blueswir1's avatar
blueswir1 committed
1286
1287
1288
1289
            env->itlb_tag[i] = env->immuregs[6];
            env->itlb_tte[i] = T1;
            return;
        }
bellard's avatar
bellard committed
1290
    case 0x57: // I-MMU demap
blueswir1's avatar
blueswir1 committed
1291
1292
        // XXX
        return;
bellard's avatar
bellard committed
1293
    case 0x58: // D-MMU regs
blueswir1's avatar
blueswir1 committed
1294
1295
1296
        {
            int reg = (T0 >> 3) & 0xf;
            uint64_t oldreg;
1297

blueswir1's avatar
blueswir1 committed
1298
            oldreg = env->dmmuregs[reg];
bellard's avatar
bellard committed
1299
1300
1301
1302
1303
            switch(reg) {
            case 0: // RO
            case 4:
                return;
            case 3: // SFSR
blueswir1's avatar
blueswir1 committed
1304
1305
1306
1307
1308
                if ((T1 & 1) == 0) {
                    T1 = 0; // Clear SFSR, Fault address
                    env->dmmuregs[4] = 0;
                }
                env->dmmuregs[reg] = T1;
bellard's avatar
bellard committed
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
                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;
            }
blueswir1's avatar
blueswir1 committed
1319
            env->dmmuregs[reg] = T1;
bellard's avatar
bellard committed
1320
            if (oldreg != env->dmmuregs[reg]) {
1321
                DPRINTF_MMU("mmu change reg[%d]: 0x%08" PRIx64 " -> 0x%08" PRIx64 "\n", reg, oldreg, env->dmmuregs[reg]);
bellard's avatar
bellard committed
1322
            }
1323
#ifdef DEBUG_MMU
blueswir1's avatar
blueswir1 committed
1324
            dump_mmu(env);
bellard's avatar
bellard committed
1325
#endif
blueswir1's avatar
blueswir1 committed
1326
1327
            return;
        }
bellard's avatar
bellard committed
1328
    case 0x5c: // D-MMU data in
blueswir1's avatar
blueswir1 committed
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
        {
            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;
        }
bellard's avatar
bellard committed
1351
    case 0x5d: // D-MMU data access
blueswir1's avatar
blueswir1 committed
1352
1353
        {
            unsigned int i = (T0 >> 3) & 0x3f;
bellard's avatar
bellard committed
1354

blueswir1's avatar
blueswir1 committed
1355