Statistics
| Branch: | Revision:

root / target-sparc / op_helper.c @ 06057e6f

History | View | Annotate | Download (76.8 kB)

1
#include "exec.h"
2
#include "host-utils.h"
3
#include "helper.h"
4
#if !defined(CONFIG_USER_ONLY)
5
#include "softmmu_exec.h"
6
#endif /* !defined(CONFIG_USER_ONLY) */
7

    
8
//#define DEBUG_MMU
9
//#define DEBUG_MXCC
10
//#define DEBUG_UNALIGNED
11
//#define DEBUG_UNASSIGNED
12
//#define DEBUG_ASI
13

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

    
21
#ifdef DEBUG_MXCC
22
#define DPRINTF_MXCC(fmt, args...) \
23
do { printf("MXCC: " fmt , ##args); } while (0)
24
#else
25
#define DPRINTF_MXCC(fmt, args...) do {} while (0)
26
#endif
27

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

    
35
#ifdef TARGET_SPARC64
36
#ifndef TARGET_ABI32
37
#define AM_CHECK(env1) ((env1)->pstate & PS_AM)
38
#else
39
#define AM_CHECK(env1) (1)
40
#endif
41
#endif
42

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

    
51
void raise_exception(int tt)
52
{
53
    env->exception_index = tt;
54
    cpu_loop_exit();
55
}
56

    
57
void helper_trap(target_ulong nb_trap)
58
{
59
    env->exception_index = TT_TRAP + (nb_trap & 0x7f);
60
    cpu_loop_exit();
61
}
62

    
63
void helper_trapcc(target_ulong nb_trap, target_ulong do_trap)
64
{
65
    if (do_trap) {
66
        env->exception_index = TT_TRAP + (nb_trap & 0x7f);
67
        cpu_loop_exit();
68
    }
69
}
70

    
71
void helper_check_align(target_ulong addr, uint32_t align)
72
{
73
    if (addr & align) {
74
#ifdef DEBUG_UNALIGNED
75
    printf("Unaligned access to 0x" TARGET_FMT_lx " from 0x" TARGET_FMT_lx
76
           "\n", addr, env->pc);
77
#endif
78
        raise_exception(TT_UNALIGNED);
79
    }
80
}
81

    
82
#define F_HELPER(name, p) void helper_f##name##p(void)
83

    
84
#define F_BINOP(name)                                           \
85
    F_HELPER(name, s)                                           \
86
    {                                                           \
87
        FT0 = float32_ ## name (FT0, FT1, &env->fp_status);     \
88
    }                                                           \
89
    F_HELPER(name, d)                                           \
90
    {                                                           \
91
        DT0 = float64_ ## name (DT0, DT1, &env->fp_status);     \
92
    }                                                           \
93
    F_HELPER(name, q)                                           \
94
    {                                                           \
95
        QT0 = float128_ ## name (QT0, QT1, &env->fp_status);    \
96
    }
97

    
98
F_BINOP(add);
99
F_BINOP(sub);
100
F_BINOP(mul);
101
F_BINOP(div);
102
#undef F_BINOP
103

    
104
void helper_fsmuld(void)
105
{
106
    DT0 = float64_mul(float32_to_float64(FT0, &env->fp_status),
107
                      float32_to_float64(FT1, &env->fp_status),
108
                      &env->fp_status);
109
}
110

    
111
void helper_fdmulq(void)
112
{
113
    QT0 = float128_mul(float64_to_float128(DT0, &env->fp_status),
114
                       float64_to_float128(DT1, &env->fp_status),
115
                       &env->fp_status);
116
}
117

    
118
F_HELPER(neg, s)
119
{
120
    FT0 = float32_chs(FT1);
121
}
122

    
123
#ifdef TARGET_SPARC64
124
F_HELPER(neg, d)
125
{
126
    DT0 = float64_chs(DT1);
127
}
128

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

    
135
/* Integer to float conversion.  */
136
F_HELPER(ito, s)
137
{
138
    FT0 = int32_to_float32(*((int32_t *)&FT1), &env->fp_status);
139
}
140

    
141
F_HELPER(ito, d)
142
{
143
    DT0 = int32_to_float64(*((int32_t *)&FT1), &env->fp_status);
144
}
145

    
146
F_HELPER(ito, q)
147
{
148
    QT0 = int32_to_float128(*((int32_t *)&FT1), &env->fp_status);
149
}
150

    
151
#ifdef TARGET_SPARC64
152
F_HELPER(xto, s)
153
{
154
    FT0 = int64_to_float32(*((int64_t *)&DT1), &env->fp_status);
155
}
156

    
157
F_HELPER(xto, d)
158
{
159
    DT0 = int64_to_float64(*((int64_t *)&DT1), &env->fp_status);
160
}
161

    
162
F_HELPER(xto, q)
163
{
164
    QT0 = int64_to_float128(*((int64_t *)&DT1), &env->fp_status);
165
}
166
#endif
167
#undef F_HELPER
168

    
169
/* floating point conversion */
170
void helper_fdtos(void)
171
{
172
    FT0 = float64_to_float32(DT1, &env->fp_status);
173
}
174

    
175
void helper_fstod(void)
176
{
177
    DT0 = float32_to_float64(FT1, &env->fp_status);
178
}
179

    
180
void helper_fqtos(void)
181
{
182
    FT0 = float128_to_float32(QT1, &env->fp_status);
183
}
184

    
185
void helper_fstoq(void)
186
{
187
    QT0 = float32_to_float128(FT1, &env->fp_status);
188
}
189

    
190
void helper_fqtod(void)
191
{
192
    DT0 = float128_to_float64(QT1, &env->fp_status);
193
}
194

    
195
void helper_fdtoq(void)
196
{
197
    QT0 = float64_to_float128(DT1, &env->fp_status);
198
}
199

    
200
/* Float to integer conversion.  */
201
void helper_fstoi(void)
202
{
203
    *((int32_t *)&FT0) = float32_to_int32_round_to_zero(FT1, &env->fp_status);
204
}
205

    
206
void helper_fdtoi(void)
207
{
208
    *((int32_t *)&FT0) = float64_to_int32_round_to_zero(DT1, &env->fp_status);
209
}
210

    
211
void helper_fqtoi(void)
212
{
213
    *((int32_t *)&FT0) = float128_to_int32_round_to_zero(QT1, &env->fp_status);
214
}
215

    
216
#ifdef TARGET_SPARC64
217
void helper_fstox(void)
218
{
219
    *((int64_t *)&DT0) = float32_to_int64_round_to_zero(FT1, &env->fp_status);
220
}
221

    
222
void helper_fdtox(void)
223
{
224
    *((int64_t *)&DT0) = float64_to_int64_round_to_zero(DT1, &env->fp_status);
225
}
226

    
227
void helper_fqtox(void)
228
{
229
    *((int64_t *)&DT0) = float128_to_int64_round_to_zero(QT1, &env->fp_status);
230
}
231

    
232
void helper_faligndata(void)
233
{
234
    uint64_t tmp;
235

    
236
    tmp = (*((uint64_t *)&DT0)) << ((env->gsr & 7) * 8);
237
    /* on many architectures a shift of 64 does nothing */
238
    if ((env->gsr & 7) != 0) {
239
        tmp |= (*((uint64_t *)&DT1)) >> (64 - (env->gsr & 7) * 8);
240
    }
241
    *((uint64_t *)&DT0) = tmp;
242
}
243

    
244
void helper_movl_FT0_0(void)
245
{
246
    *((uint32_t *)&FT0) = 0;
247
}
248

    
249
void helper_movl_DT0_0(void)
250
{
251
    *((uint64_t *)&DT0) = 0;
252
}
253

    
254
void helper_movl_FT0_1(void)
255
{
256
    *((uint32_t *)&FT0) = 0xffffffff;
257
}
258

    
259
void helper_movl_DT0_1(void)
260
{
261
    *((uint64_t *)&DT0) = 0xffffffffffffffffULL;
262
}
263

    
264
void helper_fnot(void)
265
{
266
    *(uint64_t *)&DT0 = ~*(uint64_t *)&DT1;
267
}
268

    
269
void helper_fnots(void)
270
{
271
    *(uint32_t *)&FT0 = ~*(uint32_t *)&FT1;
272
}
273

    
274
void helper_fnor(void)
275
{
276
    *(uint64_t *)&DT0 = ~(*(uint64_t *)&DT0 | *(uint64_t *)&DT1);
277
}
278

    
279
void helper_fnors(void)
280
{
281
    *(uint32_t *)&FT0 = ~(*(uint32_t *)&FT0 | *(uint32_t *)&FT1);
282
}
283

    
284
void helper_for(void)
285
{
286
    *(uint64_t *)&DT0 |= *(uint64_t *)&DT1;
287
}
288

    
289
void helper_fors(void)
290
{
291
    *(uint32_t *)&FT0 |= *(uint32_t *)&FT1;
292
}
293

    
294
void helper_fxor(void)
295
{
296
    *(uint64_t *)&DT0 ^= *(uint64_t *)&DT1;
297
}
298

    
299
void helper_fxors(void)
300
{
301
    *(uint32_t *)&FT0 ^= *(uint32_t *)&FT1;
302
}
303

    
304
void helper_fand(void)
305
{
306
    *(uint64_t *)&DT0 &= *(uint64_t *)&DT1;
307
}
308

    
309
void helper_fands(void)
310
{
311
    *(uint32_t *)&FT0 &= *(uint32_t *)&FT1;
312
}
313

    
314
void helper_fornot(void)
315
{
316
    *(uint64_t *)&DT0 = *(uint64_t *)&DT0 | ~*(uint64_t *)&DT1;
317
}
318

    
319
void helper_fornots(void)
320
{
321
    *(uint32_t *)&FT0 = *(uint32_t *)&FT0 | ~*(uint32_t *)&FT1;
322
}
323

    
324
void helper_fandnot(void)
325
{
326
    *(uint64_t *)&DT0 = *(uint64_t *)&DT0 & ~*(uint64_t *)&DT1;
327
}
328

    
329
void helper_fandnots(void)
330
{
331
    *(uint32_t *)&FT0 = *(uint32_t *)&FT0 & ~*(uint32_t *)&FT1;
332
}
333

    
334
void helper_fnand(void)
335
{
336
    *(uint64_t *)&DT0 = ~(*(uint64_t *)&DT0 & *(uint64_t *)&DT1);
337
}
338

    
339
void helper_fnands(void)
340
{
341
    *(uint32_t *)&FT0 = ~(*(uint32_t *)&FT0 & *(uint32_t *)&FT1);
342
}
343

    
344
void helper_fxnor(void)
345
{
346
    *(uint64_t *)&DT0 ^= ~*(uint64_t *)&DT1;
347
}
348

    
349
void helper_fxnors(void)
350
{
351
    *(uint32_t *)&FT0 ^= ~*(uint32_t *)&FT1;
352
}
353

    
354
#ifdef WORDS_BIGENDIAN
355
#define VIS_B64(n) b[7 - (n)]
356
#define VIS_W64(n) w[3 - (n)]
357
#define VIS_SW64(n) sw[3 - (n)]
358
#define VIS_L64(n) l[1 - (n)]
359
#define VIS_B32(n) b[3 - (n)]
360
#define VIS_W32(n) w[1 - (n)]
361
#else
362
#define VIS_B64(n) b[n]
363
#define VIS_W64(n) w[n]
364
#define VIS_SW64(n) sw[n]
365
#define VIS_L64(n) l[n]
366
#define VIS_B32(n) b[n]
367
#define VIS_W32(n) w[n]
368
#endif
369

    
370
typedef union {
371
    uint8_t b[8];
372
    uint16_t w[4];
373
    int16_t sw[4];
374
    uint32_t l[2];
375
    float64 d;
376
} vis64;
377

    
378
typedef union {
379
    uint8_t b[4];
380
    uint16_t w[2];
381
    uint32_t l;
382
    float32 f;
383
} vis32;
384

    
385
void helper_fpmerge(void)
386
{
387
    vis64 s, d;
388

    
389
    s.d = DT0;
390
    d.d = DT1;
391

    
392
    // Reverse calculation order to handle overlap
393
    d.VIS_B64(7) = s.VIS_B64(3);
394
    d.VIS_B64(6) = d.VIS_B64(3);
395
    d.VIS_B64(5) = s.VIS_B64(2);
396
    d.VIS_B64(4) = d.VIS_B64(2);
397
    d.VIS_B64(3) = s.VIS_B64(1);
398
    d.VIS_B64(2) = d.VIS_B64(1);
399
    d.VIS_B64(1) = s.VIS_B64(0);
400
    //d.VIS_B64(0) = d.VIS_B64(0);
401

    
402
    DT0 = d.d;
403
}
404

    
405
void helper_fmul8x16(void)
406
{
407
    vis64 s, d;
408
    uint32_t tmp;
409

    
410
    s.d = DT0;
411
    d.d = DT1;
412

    
413
#define PMUL(r)                                                 \
414
    tmp = (int32_t)d.VIS_SW64(r) * (int32_t)s.VIS_B64(r);       \
415
    if ((tmp & 0xff) > 0x7f)                                    \
416
        tmp += 0x100;                                           \
417
    d.VIS_W64(r) = tmp >> 8;
418

    
419
    PMUL(0);
420
    PMUL(1);
421
    PMUL(2);
422
    PMUL(3);
423
#undef PMUL
424

    
425
    DT0 = d.d;
426
}
427

    
428
void helper_fmul8x16al(void)
429
{
430
    vis64 s, d;
431
    uint32_t tmp;
432

    
433
    s.d = DT0;
434
    d.d = DT1;
435

    
436
#define PMUL(r)                                                 \
437
    tmp = (int32_t)d.VIS_SW64(1) * (int32_t)s.VIS_B64(r);       \
438
    if ((tmp & 0xff) > 0x7f)                                    \
439
        tmp += 0x100;                                           \
440
    d.VIS_W64(r) = tmp >> 8;
441

    
442
    PMUL(0);
443
    PMUL(1);
444
    PMUL(2);
445
    PMUL(3);
446
#undef PMUL
447

    
448
    DT0 = d.d;
449
}
450

    
451
void helper_fmul8x16au(void)
452
{
453
    vis64 s, d;
454
    uint32_t tmp;
455

    
456
    s.d = DT0;
457
    d.d = DT1;
458

    
459
#define PMUL(r)                                                 \
460
    tmp = (int32_t)d.VIS_SW64(0) * (int32_t)s.VIS_B64(r);       \
461
    if ((tmp & 0xff) > 0x7f)                                    \
462
        tmp += 0x100;                                           \
463
    d.VIS_W64(r) = tmp >> 8;
464

    
465
    PMUL(0);
466
    PMUL(1);
467
    PMUL(2);
468
    PMUL(3);
469
#undef PMUL
470

    
471
    DT0 = d.d;
472
}
473

    
474
void helper_fmul8sux16(void)
475
{
476
    vis64 s, d;
477
    uint32_t tmp;
478

    
479
    s.d = DT0;
480
    d.d = DT1;
481

    
482
#define PMUL(r)                                                         \
483
    tmp = (int32_t)d.VIS_SW64(r) * ((int32_t)s.VIS_SW64(r) >> 8);       \
484
    if ((tmp & 0xff) > 0x7f)                                            \
485
        tmp += 0x100;                                                   \
486
    d.VIS_W64(r) = tmp >> 8;
487

    
488
    PMUL(0);
489
    PMUL(1);
490
    PMUL(2);
491
    PMUL(3);
492
#undef PMUL
493

    
494
    DT0 = d.d;
495
}
496

    
497
void helper_fmul8ulx16(void)
498
{
499
    vis64 s, d;
500
    uint32_t tmp;
501

    
502
    s.d = DT0;
503
    d.d = DT1;
504

    
505
#define PMUL(r)                                                         \
506
    tmp = (int32_t)d.VIS_SW64(r) * ((uint32_t)s.VIS_B64(r * 2));        \
507
    if ((tmp & 0xff) > 0x7f)                                            \
508
        tmp += 0x100;                                                   \
509
    d.VIS_W64(r) = tmp >> 8;
510

    
511
    PMUL(0);
512
    PMUL(1);
513
    PMUL(2);
514
    PMUL(3);
515
#undef PMUL
516

    
517
    DT0 = d.d;
518
}
519

    
520
void helper_fmuld8sux16(void)
521
{
522
    vis64 s, d;
523
    uint32_t tmp;
524

    
525
    s.d = DT0;
526
    d.d = DT1;
527

    
528
#define PMUL(r)                                                         \
529
    tmp = (int32_t)d.VIS_SW64(r) * ((int32_t)s.VIS_SW64(r) >> 8);       \
530
    if ((tmp & 0xff) > 0x7f)                                            \
531
        tmp += 0x100;                                                   \
532
    d.VIS_L64(r) = tmp;
533

    
534
    // Reverse calculation order to handle overlap
535
    PMUL(1);
536
    PMUL(0);
537
#undef PMUL
538

    
539
    DT0 = d.d;
540
}
541

    
542
void helper_fmuld8ulx16(void)
543
{
544
    vis64 s, d;
545
    uint32_t tmp;
546

    
547
    s.d = DT0;
548
    d.d = DT1;
549

    
550
#define PMUL(r)                                                         \
551
    tmp = (int32_t)d.VIS_SW64(r) * ((uint32_t)s.VIS_B64(r * 2));        \
552
    if ((tmp & 0xff) > 0x7f)                                            \
553
        tmp += 0x100;                                                   \
554
    d.VIS_L64(r) = tmp;
555

    
556
    // Reverse calculation order to handle overlap
557
    PMUL(1);
558
    PMUL(0);
559
#undef PMUL
560

    
561
    DT0 = d.d;
562
}
563

    
564
void helper_fexpand(void)
565
{
566
    vis32 s;
567
    vis64 d;
568

    
569
    s.l = (uint32_t)(*(uint64_t *)&DT0 & 0xffffffff);
570
    d.d = DT1;
571
    d.VIS_L64(0) = s.VIS_W32(0) << 4;
572
    d.VIS_L64(1) = s.VIS_W32(1) << 4;
573
    d.VIS_L64(2) = s.VIS_W32(2) << 4;
574
    d.VIS_L64(3) = s.VIS_W32(3) << 4;
575

    
576
    DT0 = d.d;
577
}
578

    
579
#define VIS_HELPER(name, F)                             \
580
    void name##16(void)                                 \
581
    {                                                   \
582
        vis64 s, d;                                     \
583
                                                        \
584
        s.d = DT0;                                      \
585
        d.d = DT1;                                      \
586
                                                        \
587
        d.VIS_W64(0) = F(d.VIS_W64(0), s.VIS_W64(0));   \
588
        d.VIS_W64(1) = F(d.VIS_W64(1), s.VIS_W64(1));   \
589
        d.VIS_W64(2) = F(d.VIS_W64(2), s.VIS_W64(2));   \
590
        d.VIS_W64(3) = F(d.VIS_W64(3), s.VIS_W64(3));   \
591
                                                        \
592
        DT0 = d.d;                                      \
593
    }                                                   \
594
                                                        \
595
    void name##16s(void)                                \
596
    {                                                   \
597
        vis32 s, d;                                     \
598
                                                        \
599
        s.f = FT0;                                      \
600
        d.f = FT1;                                      \
601
                                                        \
602
        d.VIS_W32(0) = F(d.VIS_W32(0), s.VIS_W32(0));   \
603
        d.VIS_W32(1) = F(d.VIS_W32(1), s.VIS_W32(1));   \
604
                                                        \
605
        FT0 = d.f;                                      \
606
    }                                                   \
607
                                                        \
608
    void name##32(void)                                 \
609
    {                                                   \
610
        vis64 s, d;                                     \
611
                                                        \
612
        s.d = DT0;                                      \
613
        d.d = DT1;                                      \
614
                                                        \
615
        d.VIS_L64(0) = F(d.VIS_L64(0), s.VIS_L64(0));   \
616
        d.VIS_L64(1) = F(d.VIS_L64(1), s.VIS_L64(1));   \
617
                                                        \
618
        DT0 = d.d;                                      \
619
    }                                                   \
620
                                                        \
621
    void name##32s(void)                                \
622
    {                                                   \
623
        vis32 s, d;                                     \
624
                                                        \
625
        s.f = FT0;                                      \
626
        d.f = FT1;                                      \
627
                                                        \
628
        d.l = F(d.l, s.l);                              \
629
                                                        \
630
        FT0 = d.f;                                      \
631
    }
632

    
633
#define FADD(a, b) ((a) + (b))
634
#define FSUB(a, b) ((a) - (b))
635
VIS_HELPER(helper_fpadd, FADD)
636
VIS_HELPER(helper_fpsub, FSUB)
637

    
638
#define VIS_CMPHELPER(name, F)                                        \
639
    void name##16(void)                                           \
640
    {                                                             \
641
        vis64 s, d;                                               \
642
                                                                  \
643
        s.d = DT0;                                                \
644
        d.d = DT1;                                                \
645
                                                                  \
646
        d.VIS_W64(0) = F(d.VIS_W64(0), s.VIS_W64(0))? 1: 0;       \
647
        d.VIS_W64(0) |= F(d.VIS_W64(1), s.VIS_W64(1))? 2: 0;      \
648
        d.VIS_W64(0) |= F(d.VIS_W64(2), s.VIS_W64(2))? 4: 0;      \
649
        d.VIS_W64(0) |= F(d.VIS_W64(3), s.VIS_W64(3))? 8: 0;      \
650
                                                                  \
651
        DT0 = d.d;                                                \
652
    }                                                             \
653
                                                                  \
654
    void name##32(void)                                           \
655
    {                                                             \
656
        vis64 s, d;                                               \
657
                                                                  \
658
        s.d = DT0;                                                \
659
        d.d = DT1;                                                \
660
                                                                  \
661
        d.VIS_L64(0) = F(d.VIS_L64(0), s.VIS_L64(0))? 1: 0;       \
662
        d.VIS_L64(0) |= F(d.VIS_L64(1), s.VIS_L64(1))? 2: 0;      \
663
                                                                  \
664
        DT0 = d.d;                                                \
665
    }
666

    
667
#define FCMPGT(a, b) ((a) > (b))
668
#define FCMPEQ(a, b) ((a) == (b))
669
#define FCMPLE(a, b) ((a) <= (b))
670
#define FCMPNE(a, b) ((a) != (b))
671

    
672
VIS_CMPHELPER(helper_fcmpgt, FCMPGT)
673
VIS_CMPHELPER(helper_fcmpeq, FCMPEQ)
674
VIS_CMPHELPER(helper_fcmple, FCMPLE)
675
VIS_CMPHELPER(helper_fcmpne, FCMPNE)
676
#endif
677

    
678
void helper_check_ieee_exceptions(void)
679
{
680
    target_ulong status;
681

    
682
    status = get_float_exception_flags(&env->fp_status);
683
    if (status) {
684
        /* Copy IEEE 754 flags into FSR */
685
        if (status & float_flag_invalid)
686
            env->fsr |= FSR_NVC;
687
        if (status & float_flag_overflow)
688
            env->fsr |= FSR_OFC;
689
        if (status & float_flag_underflow)
690
            env->fsr |= FSR_UFC;
691
        if (status & float_flag_divbyzero)
692
            env->fsr |= FSR_DZC;
693
        if (status & float_flag_inexact)
694
            env->fsr |= FSR_NXC;
695

    
696
        if ((env->fsr & FSR_CEXC_MASK) & ((env->fsr & FSR_TEM_MASK) >> 23)) {
697
            /* Unmasked exception, generate a trap */
698
            env->fsr |= FSR_FTT_IEEE_EXCP;
699
            raise_exception(TT_FP_EXCP);
700
        } else {
701
            /* Accumulate exceptions */
702
            env->fsr |= (env->fsr & FSR_CEXC_MASK) << 5;
703
        }
704
    }
705
}
706

    
707
void helper_clear_float_exceptions(void)
708
{
709
    set_float_exception_flags(0, &env->fp_status);
710
}
711

    
712
void helper_fabss(void)
713
{
714
    FT0 = float32_abs(FT1);
715
}
716

    
717
#ifdef TARGET_SPARC64
718
void helper_fabsd(void)
719
{
720
    DT0 = float64_abs(DT1);
721
}
722

    
723
void helper_fabsq(void)
724
{
725
    QT0 = float128_abs(QT1);
726
}
727
#endif
728

    
729
void helper_fsqrts(void)
730
{
731
    FT0 = float32_sqrt(FT1, &env->fp_status);
732
}
733

    
734
void helper_fsqrtd(void)
735
{
736
    DT0 = float64_sqrt(DT1, &env->fp_status);
737
}
738

    
739
void helper_fsqrtq(void)
740
{
741
    QT0 = float128_sqrt(QT1, &env->fp_status);
742
}
743

    
744
#define GEN_FCMP(name, size, reg1, reg2, FS, TRAP)                      \
745
    void glue(helper_, name) (void)                                     \
746
    {                                                                   \
747
        target_ulong new_fsr;                                           \
748
                                                                        \
749
        env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS);                     \
750
        switch (glue(size, _compare) (reg1, reg2, &env->fp_status)) {   \
751
        case float_relation_unordered:                                  \
752
            new_fsr = (FSR_FCC1 | FSR_FCC0) << FS;                      \
753
            if ((env->fsr & FSR_NVM) || TRAP) {                         \
754
                env->fsr |= new_fsr;                                    \
755
                env->fsr |= FSR_NVC;                                    \
756
                env->fsr |= FSR_FTT_IEEE_EXCP;                          \
757
                raise_exception(TT_FP_EXCP);                            \
758
            } else {                                                    \
759
                env->fsr |= FSR_NVA;                                    \
760
            }                                                           \
761
            break;                                                      \
762
        case float_relation_less:                                       \
763
            new_fsr = FSR_FCC0 << FS;                                   \
764
            break;                                                      \
765
        case float_relation_greater:                                    \
766
            new_fsr = FSR_FCC1 << FS;                                   \
767
            break;                                                      \
768
        default:                                                        \
769
            new_fsr = 0;                                                \
770
            break;                                                      \
771
        }                                                               \
772
        env->fsr |= new_fsr;                                            \
773
    }
774

    
775
GEN_FCMP(fcmps, float32, FT0, FT1, 0, 0);
776
GEN_FCMP(fcmpd, float64, DT0, DT1, 0, 0);
777

    
778
GEN_FCMP(fcmpes, float32, FT0, FT1, 0, 1);
779
GEN_FCMP(fcmped, float64, DT0, DT1, 0, 1);
780

    
781
GEN_FCMP(fcmpq, float128, QT0, QT1, 0, 0);
782
GEN_FCMP(fcmpeq, float128, QT0, QT1, 0, 1);
783

    
784
#ifdef TARGET_SPARC64
785
GEN_FCMP(fcmps_fcc1, float32, FT0, FT1, 22, 0);
786
GEN_FCMP(fcmpd_fcc1, float64, DT0, DT1, 22, 0);
787
GEN_FCMP(fcmpq_fcc1, float128, QT0, QT1, 22, 0);
788

    
789
GEN_FCMP(fcmps_fcc2, float32, FT0, FT1, 24, 0);
790
GEN_FCMP(fcmpd_fcc2, float64, DT0, DT1, 24, 0);
791
GEN_FCMP(fcmpq_fcc2, float128, QT0, QT1, 24, 0);
792

    
793
GEN_FCMP(fcmps_fcc3, float32, FT0, FT1, 26, 0);
794
GEN_FCMP(fcmpd_fcc3, float64, DT0, DT1, 26, 0);
795
GEN_FCMP(fcmpq_fcc3, float128, QT0, QT1, 26, 0);
796

    
797
GEN_FCMP(fcmpes_fcc1, float32, FT0, FT1, 22, 1);
798
GEN_FCMP(fcmped_fcc1, float64, DT0, DT1, 22, 1);
799
GEN_FCMP(fcmpeq_fcc1, float128, QT0, QT1, 22, 1);
800

    
801
GEN_FCMP(fcmpes_fcc2, float32, FT0, FT1, 24, 1);
802
GEN_FCMP(fcmped_fcc2, float64, DT0, DT1, 24, 1);
803
GEN_FCMP(fcmpeq_fcc2, float128, QT0, QT1, 24, 1);
804

    
805
GEN_FCMP(fcmpes_fcc3, float32, FT0, FT1, 26, 1);
806
GEN_FCMP(fcmped_fcc3, float64, DT0, DT1, 26, 1);
807
GEN_FCMP(fcmpeq_fcc3, float128, QT0, QT1, 26, 1);
808
#endif
809

    
810
#if !defined(TARGET_SPARC64) && !defined(CONFIG_USER_ONLY) && \
811
    defined(DEBUG_MXCC)
812
static void dump_mxcc(CPUState *env)
813
{
814
    printf("mxccdata: %016llx %016llx %016llx %016llx\n",
815
           env->mxccdata[0], env->mxccdata[1],
816
           env->mxccdata[2], env->mxccdata[3]);
817
    printf("mxccregs: %016llx %016llx %016llx %016llx\n"
818
           "          %016llx %016llx %016llx %016llx\n",
819
           env->mxccregs[0], env->mxccregs[1],
820
           env->mxccregs[2], env->mxccregs[3],
821
           env->mxccregs[4], env->mxccregs[5],
822
           env->mxccregs[6], env->mxccregs[7]);
823
}
824
#endif
825

    
826
#if (defined(TARGET_SPARC64) || !defined(CONFIG_USER_ONLY)) \
827
    && defined(DEBUG_ASI)
828
static void dump_asi(const char *txt, target_ulong addr, int asi, int size,
829
                     uint64_t r1)
830
{
831
    switch (size)
832
    {
833
    case 1:
834
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %02" PRIx64 "\n", txt,
835
                    addr, asi, r1 & 0xff);
836
        break;
837
    case 2:
838
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %04" PRIx64 "\n", txt,
839
                    addr, asi, r1 & 0xffff);
840
        break;
841
    case 4:
842
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %08" PRIx64 "\n", txt,
843
                    addr, asi, r1 & 0xffffffff);
844
        break;
845
    case 8:
846
        DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %016" PRIx64 "\n", txt,
847
                    addr, asi, r1);
848
        break;
849
    }
850
}
851
#endif
852

    
853
#ifndef TARGET_SPARC64
854
#ifndef CONFIG_USER_ONLY
855
uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
856
{
857
    uint64_t ret = 0;
858
#if defined(DEBUG_MXCC) || defined(DEBUG_ASI)
859
    uint32_t last_addr = addr;
860
#endif
861

    
862
    helper_check_align(addr, size - 1);
863
    switch (asi) {
864
    case 2: /* SuperSparc MXCC registers */
865
        switch (addr) {
866
        case 0x01c00a00: /* MXCC control register */
867
            if (size == 8)
868
                ret = env->mxccregs[3];
869
            else
870
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
871
                             size);
872
            break;
873
        case 0x01c00a04: /* MXCC control register */
874
            if (size == 4)
875
                ret = env->mxccregs[3];
876
            else
877
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
878
                             size);
879
            break;
880
        case 0x01c00c00: /* Module reset register */
881
            if (size == 8) {
882
                ret = env->mxccregs[5];
883
                // should we do something here?
884
            } else
885
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
886
                             size);
887
            break;
888
        case 0x01c00f00: /* MBus port address register */
889
            if (size == 8)
890
                ret = env->mxccregs[7];
891
            else
892
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
893
                             size);
894
            break;
895
        default:
896
            DPRINTF_MXCC("%08x: unimplemented address, size: %d\n", addr,
897
                         size);
898
            break;
899
        }
900
        DPRINTF_MXCC("asi = %d, size = %d, sign = %d, "
901
                     "addr = %08x -> ret = %08x,"
902
                     "addr = %08x\n", asi, size, sign, last_addr, ret, addr);
903
#ifdef DEBUG_MXCC
904
        dump_mxcc(env);
905
#endif
906
        break;
907
    case 3: /* MMU probe */
908
        {
909
            int mmulev;
910

    
911
            mmulev = (addr >> 8) & 15;
912
            if (mmulev > 4)
913
                ret = 0;
914
            else
915
                ret = mmu_probe(env, addr, mmulev);
916
            DPRINTF_MMU("mmu_probe: 0x%08x (lev %d) -> 0x%08" PRIx64 "\n",
917
                        addr, mmulev, ret);
918
        }
919
        break;
920
    case 4: /* read MMU regs */
921
        {
922
            int reg = (addr >> 8) & 0x1f;
923

    
924
            ret = env->mmuregs[reg];
925
            if (reg == 3) /* Fault status cleared on read */
926
                env->mmuregs[3] = 0;
927
            else if (reg == 0x13) /* Fault status read */
928
                ret = env->mmuregs[3];
929
            else if (reg == 0x14) /* Fault address read */
930
                ret = env->mmuregs[4];
931
            DPRINTF_MMU("mmu_read: reg[%d] = 0x%08" PRIx64 "\n", reg, ret);
932
        }
933
        break;
934
    case 5: // Turbosparc ITLB Diagnostic
935
    case 6: // Turbosparc DTLB Diagnostic
936
    case 7: // Turbosparc IOTLB Diagnostic
937
        break;
938
    case 9: /* Supervisor code access */
939
        switch(size) {
940
        case 1:
941
            ret = ldub_code(addr);
942
            break;
943
        case 2:
944
            ret = lduw_code(addr);
945
            break;
946
        default:
947
        case 4:
948
            ret = ldl_code(addr);
949
            break;
950
        case 8:
951
            ret = ldq_code(addr);
952
            break;
953
        }
954
        break;
955
    case 0xa: /* User data access */
956
        switch(size) {
957
        case 1:
958
            ret = ldub_user(addr);
959
            break;
960
        case 2:
961
            ret = lduw_user(addr);
962
            break;
963
        default:
964
        case 4:
965
            ret = ldl_user(addr);
966
            break;
967
        case 8:
968
            ret = ldq_user(addr);
969
            break;
970
        }
971
        break;
972
    case 0xb: /* Supervisor data access */
973
        switch(size) {
974
        case 1:
975
            ret = ldub_kernel(addr);
976
            break;
977
        case 2:
978
            ret = lduw_kernel(addr);
979
            break;
980
        default:
981
        case 4:
982
            ret = ldl_kernel(addr);
983
            break;
984
        case 8:
985
            ret = ldq_kernel(addr);
986
            break;
987
        }
988
        break;
989
    case 0xc: /* I-cache tag */
990
    case 0xd: /* I-cache data */
991
    case 0xe: /* D-cache tag */
992
    case 0xf: /* D-cache data */
993
        break;
994
    case 0x20: /* MMU passthrough */
995
        switch(size) {
996
        case 1:
997
            ret = ldub_phys(addr);
998
            break;
999
        case 2:
1000
            ret = lduw_phys(addr);
1001
            break;
1002
        default:
1003
        case 4:
1004
            ret = ldl_phys(addr);
1005
            break;
1006
        case 8:
1007
            ret = ldq_phys(addr);
1008
            break;
1009
        }
1010
        break;
1011
    case 0x21 ... 0x2f: /* MMU passthrough, 0x100000000 to 0xfffffffff */
1012
        switch(size) {
1013
        case 1:
1014
            ret = ldub_phys((target_phys_addr_t)addr
1015
                            | ((target_phys_addr_t)(asi & 0xf) << 32));
1016
            break;
1017
        case 2:
1018
            ret = lduw_phys((target_phys_addr_t)addr
1019
                            | ((target_phys_addr_t)(asi & 0xf) << 32));
1020
            break;
1021
        default:
1022
        case 4:
1023
            ret = ldl_phys((target_phys_addr_t)addr
1024
                           | ((target_phys_addr_t)(asi & 0xf) << 32));
1025
            break;
1026
        case 8:
1027
            ret = ldq_phys((target_phys_addr_t)addr
1028
                           | ((target_phys_addr_t)(asi & 0xf) << 32));
1029
            break;
1030
        }
1031
        break;
1032
    case 0x30: // Turbosparc secondary cache diagnostic
1033
    case 0x31: // Turbosparc RAM snoop
1034
    case 0x32: // Turbosparc page table descriptor diagnostic
1035
    case 0x39: /* data cache diagnostic register */
1036
        ret = 0;
1037
        break;
1038
    case 8: /* User code access, XXX */
1039
    default:
1040
        do_unassigned_access(addr, 0, 0, asi);
1041
        ret = 0;
1042
        break;
1043
    }
1044
    if (sign) {
1045
        switch(size) {
1046
        case 1:
1047
            ret = (int8_t) ret;
1048
            break;
1049
        case 2:
1050
            ret = (int16_t) ret;
1051
            break;
1052
        case 4:
1053
            ret = (int32_t) ret;
1054
            break;
1055
        default:
1056
            break;
1057
        }
1058
    }
1059
#ifdef DEBUG_ASI
1060
    dump_asi("read ", last_addr, asi, size, ret);
1061
#endif
1062
    return ret;
1063
}
1064

    
1065
void helper_st_asi(target_ulong addr, uint64_t val, int asi, int size)
1066
{
1067
    helper_check_align(addr, size - 1);
1068
    switch(asi) {
1069
    case 2: /* SuperSparc MXCC registers */
1070
        switch (addr) {
1071
        case 0x01c00000: /* MXCC stream data register 0 */
1072
            if (size == 8)
1073
                env->mxccdata[0] = val;
1074
            else
1075
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
1076
                             size);
1077
            break;
1078
        case 0x01c00008: /* MXCC stream data register 1 */
1079
            if (size == 8)
1080
                env->mxccdata[1] = val;
1081
            else
1082
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
1083
                             size);
1084
            break;
1085
        case 0x01c00010: /* MXCC stream data register 2 */
1086
            if (size == 8)
1087
                env->mxccdata[2] = val;
1088
            else
1089
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
1090
                             size);
1091
            break;
1092
        case 0x01c00018: /* MXCC stream data register 3 */
1093
            if (size == 8)
1094
                env->mxccdata[3] = val;
1095
            else
1096
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
1097
                             size);
1098
            break;
1099
        case 0x01c00100: /* MXCC stream source */
1100
            if (size == 8)
1101
                env->mxccregs[0] = val;
1102
            else
1103
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
1104
                             size);
1105
            env->mxccdata[0] = ldq_phys((env->mxccregs[0] & 0xffffffffULL) +
1106
                                        0);
1107
            env->mxccdata[1] = ldq_phys((env->mxccregs[0] & 0xffffffffULL) +
1108
                                        8);
1109
            env->mxccdata[2] = ldq_phys((env->mxccregs[0] & 0xffffffffULL) +
1110
                                        16);
1111
            env->mxccdata[3] = ldq_phys((env->mxccregs[0] & 0xffffffffULL) +
1112
                                        24);
1113
            break;
1114
        case 0x01c00200: /* MXCC stream destination */
1115
            if (size == 8)
1116
                env->mxccregs[1] = val;
1117
            else
1118
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
1119
                             size);
1120
            stq_phys((env->mxccregs[1] & 0xffffffffULL) +  0,
1121
                     env->mxccdata[0]);
1122
            stq_phys((env->mxccregs[1] & 0xffffffffULL) +  8,
1123
                     env->mxccdata[1]);
1124
            stq_phys((env->mxccregs[1] & 0xffffffffULL) + 16,
1125
                     env->mxccdata[2]);
1126
            stq_phys((env->mxccregs[1] & 0xffffffffULL) + 24,
1127
                     env->mxccdata[3]);
1128
            break;
1129
        case 0x01c00a00: /* MXCC control register */
1130
            if (size == 8)
1131
                env->mxccregs[3] = val;
1132
            else
1133
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
1134
                             size);
1135
            break;
1136
        case 0x01c00a04: /* MXCC control register */
1137
            if (size == 4)
1138
                env->mxccregs[3] = (env->mxccregs[0xa] & 0xffffffff00000000ULL)
1139
                    | val;
1140
            else
1141
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
1142
                             size);
1143
            break;
1144
        case 0x01c00e00: /* MXCC error register  */
1145
            // writing a 1 bit clears the error
1146
            if (size == 8)
1147
                env->mxccregs[6] &= ~val;
1148
            else
1149
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
1150
                             size);
1151
            break;
1152
        case 0x01c00f00: /* MBus port address register */
1153
            if (size == 8)
1154
                env->mxccregs[7] = val;
1155
            else
1156
                DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
1157
                             size);
1158
            break;
1159
        default:
1160
            DPRINTF_MXCC("%08x: unimplemented address, size: %d\n", addr,
1161
                         size);
1162
            break;
1163
        }
1164
        DPRINTF_MXCC("asi = %d, size = %d, addr = %08x, val = %08x\n", asi,
1165
                     size, addr, val);
1166
#ifdef DEBUG_MXCC
1167
        dump_mxcc(env);
1168
#endif
1169
        break;
1170
    case 3: /* MMU flush */
1171
        {
1172
            int mmulev;
1173

    
1174
            mmulev = (addr >> 8) & 15;
1175
            DPRINTF_MMU("mmu flush level %d\n", mmulev);
1176
            switch (mmulev) {
1177
            case 0: // flush page
1178
                tlb_flush_page(env, addr & 0xfffff000);
1179
                break;
1180
            case 1: // flush segment (256k)
1181
            case 2: // flush region (16M)
1182
            case 3: // flush context (4G)
1183
            case 4: // flush entire
1184
                tlb_flush(env, 1);
1185
                break;
1186
            default:
1187
                break;
1188
            }
1189
#ifdef DEBUG_MMU
1190
            dump_mmu(env);
1191
#endif
1192
        }
1193
        break;
1194
    case 4: /* write MMU regs */
1195
        {
1196
            int reg = (addr >> 8) & 0x1f;
1197
            uint32_t oldreg;
1198

    
1199
            oldreg = env->mmuregs[reg];
1200
            switch(reg) {
1201
            case 0: // Control Register
1202
                env->mmuregs[reg] = (env->mmuregs[reg] & 0xff000000) |
1203
                                    (val & 0x00ffffff);
1204
                // Mappings generated during no-fault mode or MMU
1205
                // disabled mode are invalid in normal mode
1206
                if ((oldreg & (MMU_E | MMU_NF | env->mmu_bm)) !=
1207
                    (env->mmuregs[reg] & (MMU_E | MMU_NF | env->mmu_bm)))
1208
                    tlb_flush(env, 1);
1209
                break;
1210
            case 1: // Context Table Pointer Register
1211
                env->mmuregs[reg] = val & env->mmu_ctpr_mask;
1212
                break;
1213
            case 2: // Context Register
1214
                env->mmuregs[reg] = val & env->mmu_cxr_mask;
1215
                if (oldreg != env->mmuregs[reg]) {
1216
                    /* we flush when the MMU context changes because
1217
                       QEMU has no MMU context support */
1218
                    tlb_flush(env, 1);
1219
                }
1220
                break;
1221
            case 3: // Synchronous Fault Status Register with Clear
1222
            case 4: // Synchronous Fault Address Register
1223
                break;
1224
            case 0x10: // TLB Replacement Control Register
1225
                env->mmuregs[reg] = val & env->mmu_trcr_mask;
1226
                break;
1227
            case 0x13: // Synchronous Fault Status Register with Read and Clear
1228
                env->mmuregs[3] = val & env->mmu_sfsr_mask;
1229
                break;
1230
            case 0x14: // Synchronous Fault Address Register
1231
                env->mmuregs[4] = val;
1232
                break;
1233
            default:
1234
                env->mmuregs[reg] = val;
1235
                break;
1236
            }
1237
            if (oldreg != env->mmuregs[reg]) {
1238
                DPRINTF_MMU("mmu change reg[%d]: 0x%08x -> 0x%08x\n",
1239
                            reg, oldreg, env->mmuregs[reg]);
1240
            }
1241
#ifdef DEBUG_MMU
1242
            dump_mmu(env);
1243
#endif
1244
        }
1245
        break;
1246
    case 5: // Turbosparc ITLB Diagnostic
1247
    case 6: // Turbosparc DTLB Diagnostic
1248
    case 7: // Turbosparc IOTLB Diagnostic
1249
        break;
1250
    case 0xa: /* User data access */
1251
        switch(size) {
1252
        case 1:
1253
            stb_user(addr, val);
1254
            break;
1255
        case 2:
1256
            stw_user(addr, val);
1257
            break;
1258
        default:
1259
        case 4:
1260
            stl_user(addr, val);
1261
            break;
1262
        case 8:
1263
            stq_user(addr, val);
1264
            break;
1265
        }
1266
        break;
1267
    case 0xb: /* Supervisor data access */
1268
        switch(size) {
1269
        case 1:
1270
            stb_kernel(addr, val);
1271
            break;
1272
        case 2:
1273
            stw_kernel(addr, val);
1274
            break;
1275
        default:
1276
        case 4:
1277
            stl_kernel(addr, val);
1278
            break;
1279
        case 8:
1280
            stq_kernel(addr, val);
1281
            break;
1282
        }
1283
        break;
1284
    case 0xc: /* I-cache tag */
1285
    case 0xd: /* I-cache data */
1286
    case 0xe: /* D-cache tag */
1287
    case 0xf: /* D-cache data */
1288
    case 0x10: /* I/D-cache flush page */
1289
    case 0x11: /* I/D-cache flush segment */
1290
    case 0x12: /* I/D-cache flush region */
1291
    case 0x13: /* I/D-cache flush context */
1292
    case 0x14: /* I/D-cache flush user */
1293
        break;
1294
    case 0x17: /* Block copy, sta access */
1295
        {
1296
            // val = src
1297
            // addr = dst
1298
            // copy 32 bytes
1299
            unsigned int i;
1300
            uint32_t src = val & ~3, dst = addr & ~3, temp;
1301

    
1302
            for (i = 0; i < 32; i += 4, src += 4, dst += 4) {
1303
                temp = ldl_kernel(src);
1304
                stl_kernel(dst, temp);
1305
            }
1306
        }
1307
        break;
1308
    case 0x1f: /* Block fill, stda access */
1309
        {
1310
            // addr = dst
1311
            // fill 32 bytes with val
1312
            unsigned int i;
1313
            uint32_t dst = addr & 7;
1314

    
1315
            for (i = 0; i < 32; i += 8, dst += 8)
1316
                stq_kernel(dst, val);
1317
        }
1318
        break;
1319
    case 0x20: /* MMU passthrough */
1320
        {
1321
            switch(size) {
1322
            case 1:
1323
                stb_phys(addr, val);
1324
                break;
1325
            case 2:
1326
                stw_phys(addr, val);
1327
                break;
1328
            case 4:
1329
            default:
1330
                stl_phys(addr, val);
1331
                break;
1332
            case 8:
1333
                stq_phys(addr, val);
1334
                break;
1335
            }
1336
        }
1337
        break;
1338
    case 0x21 ... 0x2f: /* MMU passthrough, 0x100000000 to 0xfffffffff */
1339
        {
1340
            switch(size) {
1341
            case 1:
1342
                stb_phys((target_phys_addr_t)addr
1343
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1344
                break;
1345
            case 2:
1346
                stw_phys((target_phys_addr_t)addr
1347
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1348
                break;
1349
            case 4:
1350
            default:
1351
                stl_phys((target_phys_addr_t)addr
1352
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1353
                break;
1354
            case 8:
1355
                stq_phys((target_phys_addr_t)addr
1356
                         | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1357
                break;
1358
            }
1359
        }
1360
        break;
1361
    case 0x30: // store buffer tags or Turbosparc secondary cache diagnostic
1362
    case 0x31: // store buffer data, Ross RT620 I-cache flush or
1363
               // Turbosparc snoop RAM
1364
    case 0x32: // store buffer control or Turbosparc page table
1365
               // descriptor diagnostic
1366
    case 0x36: /* I-cache flash clear */
1367
    case 0x37: /* D-cache flash clear */
1368
    case 0x38: /* breakpoint diagnostics */
1369
    case 0x4c: /* breakpoint action */
1370
        break;
1371
    case 8: /* User code access, XXX */
1372
    case 9: /* Supervisor code access, XXX */
1373
    default:
1374
        do_unassigned_access(addr, 1, 0, asi);
1375
        break;
1376
    }
1377
#ifdef DEBUG_ASI
1378
    dump_asi("write", addr, asi, size, val);
1379
#endif
1380
}
1381

    
1382
#endif /* CONFIG_USER_ONLY */
1383
#else /* TARGET_SPARC64 */
1384

    
1385
#ifdef CONFIG_USER_ONLY
1386
uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
1387
{
1388
    uint64_t ret = 0;
1389
#if defined(DEBUG_ASI)
1390
    target_ulong last_addr = addr;
1391
#endif
1392

    
1393
    if (asi < 0x80)
1394
        raise_exception(TT_PRIV_ACT);
1395

    
1396
    helper_check_align(addr, size - 1);
1397
    address_mask(env, &addr);
1398

    
1399
    switch (asi) {
1400
    case 0x80: // Primary
1401
    case 0x82: // Primary no-fault
1402
    case 0x88: // Primary LE
1403
    case 0x8a: // Primary no-fault LE
1404
        {
1405
            switch(size) {
1406
            case 1:
1407
                ret = ldub_raw(addr);
1408
                break;
1409
            case 2:
1410
                ret = lduw_raw(addr);
1411
                break;
1412
            case 4:
1413
                ret = ldl_raw(addr);
1414
                break;
1415
            default:
1416
            case 8:
1417
                ret = ldq_raw(addr);
1418
                break;
1419
            }
1420
        }
1421
        break;
1422
    case 0x81: // Secondary
1423
    case 0x83: // Secondary no-fault
1424
    case 0x89: // Secondary LE
1425
    case 0x8b: // Secondary no-fault LE
1426
        // XXX
1427
        break;
1428
    default:
1429
        break;
1430
    }
1431

    
1432
    /* Convert from little endian */
1433
    switch (asi) {
1434
    case 0x88: // Primary LE
1435
    case 0x89: // Secondary LE
1436
    case 0x8a: // Primary no-fault LE
1437
    case 0x8b: // Secondary no-fault LE
1438
        switch(size) {
1439
        case 2:
1440
            ret = bswap16(ret);
1441
            break;
1442
        case 4:
1443
            ret = bswap32(ret);
1444
            break;
1445
        case 8:
1446
            ret = bswap64(ret);
1447
            break;
1448
        default:
1449
            break;
1450
        }
1451
    default:
1452
        break;
1453
    }
1454

    
1455
    /* Convert to signed number */
1456
    if (sign) {
1457
        switch(size) {
1458
        case 1:
1459
            ret = (int8_t) ret;
1460
            break;
1461
        case 2:
1462
            ret = (int16_t) ret;
1463
            break;
1464
        case 4:
1465
            ret = (int32_t) ret;
1466
            break;
1467
        default:
1468
            break;
1469
        }
1470
    }
1471
#ifdef DEBUG_ASI
1472
    dump_asi("read ", last_addr, asi, size, ret);
1473
#endif
1474
    return ret;
1475
}
1476

    
1477
void helper_st_asi(target_ulong addr, target_ulong val, int asi, int size)
1478
{
1479
#ifdef DEBUG_ASI
1480
    dump_asi("write", addr, asi, size, val);
1481
#endif
1482
    if (asi < 0x80)
1483
        raise_exception(TT_PRIV_ACT);
1484

    
1485
    helper_check_align(addr, size - 1);
1486
    address_mask(env, &addr);
1487

    
1488
    /* Convert to little endian */
1489
    switch (asi) {
1490
    case 0x88: // Primary LE
1491
    case 0x89: // Secondary LE
1492
        switch(size) {
1493
        case 2:
1494
            addr = bswap16(addr);
1495
            break;
1496
        case 4:
1497
            addr = bswap32(addr);
1498
            break;
1499
        case 8:
1500
            addr = bswap64(addr);
1501
            break;
1502
        default:
1503
            break;
1504
        }
1505
    default:
1506
        break;
1507
    }
1508

    
1509
    switch(asi) {
1510
    case 0x80: // Primary
1511
    case 0x88: // Primary LE
1512
        {
1513
            switch(size) {
1514
            case 1:
1515
                stb_raw(addr, val);
1516
                break;
1517
            case 2:
1518
                stw_raw(addr, val);
1519
                break;
1520
            case 4:
1521
                stl_raw(addr, val);
1522
                break;
1523
            case 8:
1524
            default:
1525
                stq_raw(addr, val);
1526
                break;
1527
            }
1528
        }
1529
        break;
1530
    case 0x81: // Secondary
1531
    case 0x89: // Secondary LE
1532
        // XXX
1533
        return;
1534

    
1535
    case 0x82: // Primary no-fault, RO
1536
    case 0x83: // Secondary no-fault, RO
1537
    case 0x8a: // Primary no-fault LE, RO
1538
    case 0x8b: // Secondary no-fault LE, RO
1539
    default:
1540
        do_unassigned_access(addr, 1, 0, 1);
1541
        return;
1542
    }
1543
}
1544

    
1545
#else /* CONFIG_USER_ONLY */
1546

    
1547
uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
1548
{
1549
    uint64_t ret = 0;
1550
#if defined(DEBUG_ASI)
1551
    target_ulong last_addr = addr;
1552
#endif
1553

    
1554
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
1555
        || ((env->features & CPU_FEATURE_HYPV) && asi >= 0x30 && asi < 0x80
1556
            && !(env->hpstate & HS_PRIV)))
1557
        raise_exception(TT_PRIV_ACT);
1558

    
1559
    helper_check_align(addr, size - 1);
1560
    switch (asi) {
1561
    case 0x10: // As if user primary
1562
    case 0x18: // As if user primary LE
1563
    case 0x80: // Primary
1564
    case 0x82: // Primary no-fault
1565
    case 0x88: // Primary LE
1566
    case 0x8a: // Primary no-fault LE
1567
        if ((asi & 0x80) && (env->pstate & PS_PRIV)) {
1568
            if ((env->features & CPU_FEATURE_HYPV) && env->hpstate & HS_PRIV) {
1569
                switch(size) {
1570
                case 1:
1571
                    ret = ldub_hypv(addr);
1572
                    break;
1573
                case 2:
1574
                    ret = lduw_hypv(addr);
1575
                    break;
1576
                case 4:
1577
                    ret = ldl_hypv(addr);
1578
                    break;
1579
                default:
1580
                case 8:
1581
                    ret = ldq_hypv(addr);
1582
                    break;
1583
                }
1584
            } else {
1585
                switch(size) {
1586
                case 1:
1587
                    ret = ldub_kernel(addr);
1588
                    break;
1589
                case 2:
1590
                    ret = lduw_kernel(addr);
1591
                    break;
1592
                case 4:
1593
                    ret = ldl_kernel(addr);
1594
                    break;
1595
                default:
1596
                case 8:
1597
                    ret = ldq_kernel(addr);
1598
                    break;
1599
                }
1600
            }
1601
        } else {
1602
            switch(size) {
1603
            case 1:
1604
                ret = ldub_user(addr);
1605
                break;
1606
            case 2:
1607
                ret = lduw_user(addr);
1608
                break;
1609
            case 4:
1610
                ret = ldl_user(addr);
1611
                break;
1612
            default:
1613
            case 8:
1614
                ret = ldq_user(addr);
1615
                break;
1616
            }
1617
        }
1618
        break;
1619
    case 0x14: // Bypass
1620
    case 0x15: // Bypass, non-cacheable
1621
    case 0x1c: // Bypass LE
1622
    case 0x1d: // Bypass, non-cacheable LE
1623
        {
1624
            switch(size) {
1625
            case 1:
1626
                ret = ldub_phys(addr);
1627
                break;
1628
            case 2:
1629
                ret = lduw_phys(addr);
1630
                break;
1631
            case 4:
1632
                ret = ldl_phys(addr);
1633
                break;
1634
            default:
1635
            case 8:
1636
                ret = ldq_phys(addr);
1637
                break;
1638
            }
1639
            break;
1640
        }
1641
    case 0x24: // Nucleus quad LDD 128 bit atomic
1642
    case 0x2c: // Nucleus quad LDD 128 bit atomic LE
1643
        //  Only ldda allowed
1644
        raise_exception(TT_ILL_INSN);
1645
        return 0;
1646
    case 0x04: // Nucleus
1647
    case 0x0c: // Nucleus Little Endian (LE)
1648
    case 0x11: // As if user secondary
1649
    case 0x19: // As if user secondary LE
1650
    case 0x4a: // UPA config
1651
    case 0x81: // Secondary
1652
    case 0x83: // Secondary no-fault
1653
    case 0x89: // Secondary LE
1654
    case 0x8b: // Secondary no-fault LE
1655
        // XXX
1656
        break;
1657
    case 0x45: // LSU
1658
        ret = env->lsu;
1659
        break;
1660
    case 0x50: // I-MMU regs
1661
        {
1662
            int reg = (addr >> 3) & 0xf;
1663

    
1664
            ret = env->immuregs[reg];
1665
            break;
1666
        }
1667
    case 0x51: // I-MMU 8k TSB pointer
1668
    case 0x52: // I-MMU 64k TSB pointer
1669
        // XXX
1670
        break;
1671
    case 0x55: // I-MMU data access
1672
        {
1673
            int reg = (addr >> 3) & 0x3f;
1674

    
1675
            ret = env->itlb_tte[reg];
1676
            break;
1677
        }
1678
    case 0x56: // I-MMU tag read
1679
        {
1680
            int reg = (addr >> 3) & 0x3f;
1681

    
1682
            ret = env->itlb_tag[reg];
1683
            break;
1684
        }
1685
    case 0x58: // D-MMU regs
1686
        {
1687
            int reg = (addr >> 3) & 0xf;
1688

    
1689
            ret = env->dmmuregs[reg];
1690
            break;
1691
        }
1692
    case 0x5d: // D-MMU data access
1693
        {
1694
            int reg = (addr >> 3) & 0x3f;
1695

    
1696
            ret = env->dtlb_tte[reg];
1697
            break;
1698
        }
1699
    case 0x5e: // D-MMU tag read
1700
        {
1701
            int reg = (addr >> 3) & 0x3f;
1702

    
1703
            ret = env->dtlb_tag[reg];
1704
            break;
1705
        }
1706
    case 0x46: // D-cache data
1707
    case 0x47: // D-cache tag access
1708
    case 0x4b: // E-cache error enable
1709
    case 0x4c: // E-cache asynchronous fault status
1710
    case 0x4d: // E-cache asynchronous fault address
1711
    case 0x4e: // E-cache tag data
1712
    case 0x66: // I-cache instruction access
1713
    case 0x67: // I-cache tag access
1714
    case 0x6e: // I-cache predecode
1715
    case 0x6f: // I-cache LRU etc.
1716
    case 0x76: // E-cache tag
1717
    case 0x7e: // E-cache tag
1718
        break;
1719
    case 0x59: // D-MMU 8k TSB pointer
1720
    case 0x5a: // D-MMU 64k TSB pointer
1721
    case 0x5b: // D-MMU data pointer
1722
    case 0x48: // Interrupt dispatch, RO
1723
    case 0x49: // Interrupt data receive
1724
    case 0x7f: // Incoming interrupt vector, RO
1725
        // XXX
1726
        break;
1727
    case 0x54: // I-MMU data in, WO
1728
    case 0x57: // I-MMU demap, WO
1729
    case 0x5c: // D-MMU data in, WO
1730
    case 0x5f: // D-MMU demap, WO
1731
    case 0x77: // Interrupt vector, WO
1732
    default:
1733
        do_unassigned_access(addr, 0, 0, 1);
1734
        ret = 0;
1735
        break;
1736
    }
1737

    
1738
    /* Convert from little endian */
1739
    switch (asi) {
1740
    case 0x0c: // Nucleus Little Endian (LE)
1741
    case 0x18: // As if user primary LE
1742
    case 0x19: // As if user secondary LE
1743
    case 0x1c: // Bypass LE
1744
    case 0x1d: // Bypass, non-cacheable LE
1745
    case 0x88: // Primary LE
1746
    case 0x89: // Secondary LE
1747
    case 0x8a: // Primary no-fault LE
1748
    case 0x8b: // Secondary no-fault LE
1749
        switch(size) {
1750
        case 2:
1751
            ret = bswap16(ret);
1752
            break;
1753
        case 4:
1754
            ret = bswap32(ret);
1755
            break;
1756
        case 8:
1757
            ret = bswap64(ret);
1758
            break;
1759
        default:
1760
            break;
1761
        }
1762
    default:
1763
        break;
1764
    }
1765

    
1766
    /* Convert to signed number */
1767
    if (sign) {
1768
        switch(size) {
1769
        case 1:
1770
            ret = (int8_t) ret;
1771
            break;
1772
        case 2:
1773
            ret = (int16_t) ret;
1774
            break;
1775
        case 4:
1776
            ret = (int32_t) ret;
1777
            break;
1778
        default:
1779
            break;
1780
        }
1781
    }
1782
#ifdef DEBUG_ASI
1783
    dump_asi("read ", last_addr, asi, size, ret);
1784
#endif
1785
    return ret;
1786
}
1787

    
1788
void helper_st_asi(target_ulong addr, target_ulong val, int asi, int size)
1789
{
1790
#ifdef DEBUG_ASI
1791
    dump_asi("write", addr, asi, size, val);
1792
#endif
1793
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
1794
        || ((env->features & CPU_FEATURE_HYPV) && asi >= 0x30 && asi < 0x80
1795
            && !(env->hpstate & HS_PRIV)))
1796
        raise_exception(TT_PRIV_ACT);
1797

    
1798
    helper_check_align(addr, size - 1);
1799
    /* Convert to little endian */
1800
    switch (asi) {
1801
    case 0x0c: // Nucleus Little Endian (LE)
1802
    case 0x18: // As if user primary LE
1803
    case 0x19: // As if user secondary LE
1804
    case 0x1c: // Bypass LE
1805
    case 0x1d: // Bypass, non-cacheable LE
1806
    case 0x88: // Primary LE
1807
    case 0x89: // Secondary LE
1808
        switch(size) {
1809
        case 2:
1810
            addr = bswap16(addr);
1811
            break;
1812
        case 4:
1813
            addr = bswap32(addr);
1814
            break;
1815
        case 8:
1816
            addr = bswap64(addr);
1817
            break;
1818
        default:
1819
            break;
1820
        }
1821
    default:
1822
        break;
1823
    }
1824

    
1825
    switch(asi) {
1826
    case 0x10: // As if user primary
1827
    case 0x18: // As if user primary LE
1828
    case 0x80: // Primary
1829
    case 0x88: // Primary LE
1830
        if ((asi & 0x80) && (env->pstate & PS_PRIV)) {
1831
            if ((env->features & CPU_FEATURE_HYPV) && env->hpstate & HS_PRIV) {
1832
                switch(size) {
1833
                case 1:
1834
                    stb_hypv(addr, val);
1835
                    break;
1836
                case 2:
1837
                    stw_hypv(addr, val);
1838
                    break;
1839
                case 4:
1840
                    stl_hypv(addr, val);
1841
                    break;
1842
                case 8:
1843
                default:
1844
                    stq_hypv(addr, val);
1845
                    break;
1846
                }
1847
            } else {
1848
                switch(size) {
1849
                case 1:
1850
                    stb_kernel(addr, val);
1851
                    break;
1852
                case 2:
1853
                    stw_kernel(addr, val);
1854
                    break;
1855
                case 4:
1856
                    stl_kernel(addr, val);
1857
                    break;
1858
                case 8:
1859
                default:
1860
                    stq_kernel(addr, val);
1861
                    break;
1862
                }
1863
            }
1864
        } else {
1865
            switch(size) {
1866
            case 1:
1867
                stb_user(addr, val);
1868
                break;
1869
            case 2:
1870
                stw_user(addr, val);
1871
                break;
1872
            case 4:
1873
                stl_user(addr, val);
1874
                break;
1875
            case 8:
1876
            default:
1877
                stq_user(addr, val);
1878
                break;
1879
            }
1880
        }
1881
        break;
1882
    case 0x14: // Bypass
1883
    case 0x15: // Bypass, non-cacheable
1884
    case 0x1c: // Bypass LE
1885
    case 0x1d: // Bypass, non-cacheable LE
1886
        {
1887
            switch(size) {
1888
            case 1:
1889
                stb_phys(addr, val);
1890
                break;
1891
            case 2:
1892
                stw_phys(addr, val);
1893
                break;
1894
            case 4:
1895
                stl_phys(addr, val);
1896
                break;
1897
            case 8:
1898
            default:
1899
                stq_phys(addr, val);
1900
                break;
1901
            }
1902
        }
1903
        return;
1904
    case 0x24: // Nucleus quad LDD 128 bit atomic
1905
    case 0x2c: // Nucleus quad LDD 128 bit atomic LE
1906
        //  Only ldda allowed
1907
        raise_exception(TT_ILL_INSN);
1908
        return;
1909
    case 0x04: // Nucleus
1910
    case 0x0c: // Nucleus Little Endian (LE)
1911
    case 0x11: // As if user secondary
1912
    case 0x19: // As if user secondary LE
1913
    case 0x4a: // UPA config
1914
    case 0x81: // Secondary
1915
    case 0x89: // Secondary LE
1916
        // XXX
1917
        return;
1918
    case 0x45: // LSU
1919
        {
1920
            uint64_t oldreg;
1921

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

    
1941
            oldreg = env->immuregs[reg];
1942
            switch(reg) {
1943
            case 0: // RO
1944
            case 4:
1945
                return;
1946
            case 1: // Not in I-MMU
1947
            case 2:
1948
            case 7:
1949
            case 8:
1950
                return;
1951
            case 3: // SFSR
1952
                if ((val & 1) == 0)
1953
                    val = 0; // Clear SFSR
1954
                break;
1955
            case 5: // TSB access
1956
            case 6: // Tag access
1957
            default:
1958
                break;
1959
            }
1960
            env->immuregs[reg] = val;
1961
            if (oldreg != env->immuregs[reg]) {
1962
                DPRINTF_MMU("mmu change reg[%d]: 0x%08" PRIx64 " -> 0x%08"
1963
                            PRIx64 "\n", reg, oldreg, env->immuregs[reg]);
1964
            }
1965
#ifdef DEBUG_MMU
1966
            dump_mmu(env);
1967
#endif
1968
            return;
1969
        }
1970
    case 0x54: // I-MMU data in
1971
        {
1972
            unsigned int i;
1973

    
1974
            // Try finding an invalid entry
1975
            for (i = 0; i < 64; i++) {
1976
                if ((env->itlb_tte[i] & 0x8000000000000000ULL) == 0) {
1977
                    env->itlb_tag[i] = env->immuregs[6];
1978
                    env->itlb_tte[i] = val;
1979
                    return;
1980
                }
1981
            }
1982
            // Try finding an unlocked entry
1983
            for (i = 0; i < 64; i++) {
1984
                if ((env->itlb_tte[i] & 0x40) == 0) {
1985
                    env->itlb_tag[i] = env->immuregs[6];
1986
                    env->itlb_tte[i] = val;
1987
                    return;
1988
                }
1989
            }
1990
            // error state?
1991
            return;
1992
        }
1993
    case 0x55: // I-MMU data access
1994
        {
1995
            unsigned int i = (addr >> 3) & 0x3f;
1996

    
1997
            env->itlb_tag[i] = env->immuregs[6];
1998
            env->itlb_tte[i] = val;
1999
            return;
2000
        }
2001
    case 0x57: // I-MMU demap
2002
        // XXX
2003
        return;
2004
    case 0x58: // D-MMU regs
2005
        {
2006
            int reg = (addr >> 3) & 0xf;
2007
            uint64_t oldreg;
2008

    
2009
            oldreg = env->dmmuregs[reg];
2010
            switch(reg) {
2011
            case 0: // RO
2012
            case 4:
2013
                return;
2014
            case 3: // SFSR
2015
                if ((val & 1) == 0) {
2016
                    val = 0; // Clear SFSR, Fault address
2017
                    env->dmmuregs[4] = 0;
2018
                }
2019
                env->dmmuregs[reg] = val;
2020
                break;
2021
            case 1: // Primary context
2022
            case 2: // Secondary context
2023
            case 5: // TSB access
2024
            case 6: // Tag access
2025
            case 7: // Virtual Watchpoint
2026
            case 8: // Physical Watchpoint
2027
            default:
2028
                break;
2029
            }
2030
            env->dmmuregs[reg] = val;
2031
            if (oldreg != env->dmmuregs[reg]) {
2032
                DPRINTF_MMU("mmu change reg[%d]: 0x%08" PRIx64 " -> 0x%08"
2033
                            PRIx64 "\n", reg, oldreg, env->dmmuregs[reg]);
2034
            }
2035
#ifdef DEBUG_MMU
2036
            dump_mmu(env);
2037
#endif
2038
            return;
2039
        }
2040
    case 0x5c: // D-MMU data in
2041
        {
2042
            unsigned int i;
2043

    
2044
            // Try finding an invalid entry
2045
            for (i = 0; i < 64; i++) {
2046
                if ((env->dtlb_tte[i] & 0x8000000000000000ULL) == 0) {
2047
                    env->dtlb_tag[i] = env->dmmuregs[6];
2048
                    env->dtlb_tte[i] = val;
2049
                    return;
2050
                }
2051
            }
2052
            // Try finding an unlocked entry
2053
            for (i = 0; i < 64; i++) {
2054
                if ((env->dtlb_tte[i] & 0x40) == 0) {
2055
                    env->dtlb_tag[i] = env->dmmuregs[6];
2056
                    env->dtlb_tte[i] = val;
2057
                    return;
2058
                }
2059
            }
2060
            // error state?
2061
            return;
2062
        }
2063
    case 0x5d: // D-MMU data access
2064
        {
2065
            unsigned int i = (addr >> 3) & 0x3f;
2066

    
2067
            env->dtlb_tag[i] = env->dmmuregs[6];
2068
            env->dtlb_tte[i] = val;
2069
            return;
2070
        }
2071
    case 0x5f: // D-MMU demap
2072
    case 0x49: // Interrupt data receive
2073
        // XXX
2074
        return;
2075
    case 0x46: // D-cache data
2076
    case 0x47: // D-cache tag access
2077
    case 0x4b: // E-cache error enable
2078
    case 0x4c: // E-cache asynchronous fault status
2079
    case 0x4d: // E-cache asynchronous fault address
2080
    case 0x4e: // E-cache tag data
2081
    case 0x66: // I-cache instruction access
2082
    case 0x67: // I-cache tag access
2083
    case 0x6e: // I-cache predecode
2084
    case 0x6f: // I-cache LRU etc.
2085
    case 0x76: // E-cache tag
2086
    case 0x7e: // E-cache tag
2087
        return;
2088
    case 0x51: // I-MMU 8k TSB pointer, RO
2089
    case 0x52: // I-MMU 64k TSB pointer, RO
2090
    case 0x56: // I-MMU tag read, RO
2091
    case 0x59: // D-MMU 8k TSB pointer, RO
2092
    case 0x5a: // D-MMU 64k TSB pointer, RO
2093
    case 0x5b: // D-MMU data pointer, RO
2094
    case 0x5e: // D-MMU tag read, RO
2095
    case 0x48: // Interrupt dispatch, RO
2096
    case 0x7f: // Incoming interrupt vector, RO
2097
    case 0x82: // Primary no-fault, RO
2098
    case 0x83: // Secondary no-fault, RO
2099
    case 0x8a: // Primary no-fault LE, RO
2100
    case 0x8b: // Secondary no-fault LE, RO
2101
    default:
2102
        do_unassigned_access(addr, 1, 0, 1);
2103
        return;
2104
    }
2105
}
2106
#endif /* CONFIG_USER_ONLY */
2107

    
2108
void helper_ldda_asi(target_ulong addr, int asi, int rd)
2109
{
2110
    if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
2111
        || ((env->features & CPU_FEATURE_HYPV) && asi >= 0x30 && asi < 0x80
2112
            && !(env->hpstate & HS_PRIV)))
2113
        raise_exception(TT_PRIV_ACT);
2114

    
2115
    switch (asi) {
2116
    case 0x24: // Nucleus quad LDD 128 bit atomic
2117
    case 0x2c: // Nucleus quad LDD 128 bit atomic LE
2118
        helper_check_align(addr, 0xf);
2119
        if (rd == 0) {
2120
            env->gregs[1] = ldq_kernel(addr + 8);
2121
            if (asi == 0x2c)
2122
                bswap64s(&env->gregs[1]);
2123
        } else if (rd < 8) {
2124
            env->gregs[rd] = ldq_kernel(addr);
2125
            env->gregs[rd + 1] = ldq_kernel(addr + 8);
2126
            if (asi == 0x2c) {
2127
                bswap64s(&env->gregs[rd]);
2128
                bswap64s(&env->gregs[rd + 1]);
2129
            }
2130
        } else {
2131
            env->regwptr[rd] = ldq_kernel(addr);
2132
            env->regwptr[rd + 1] = ldq_kernel(addr + 8);
2133
            if (asi == 0x2c) {
2134
                bswap64s(&env->regwptr[rd]);
2135
                bswap64s(&env->regwptr[rd + 1]);
2136
            }
2137
        }
2138
        break;
2139
    default:
2140
        helper_check_align(addr, 0x3);
2141
        if (rd == 0)
2142
            env->gregs[1] = helper_ld_asi(addr + 4, asi, 4, 0);
2143
        else if (rd < 8) {
2144
            env->gregs[rd] = helper_ld_asi(addr, asi, 4, 0);
2145
            env->gregs[rd + 1] = helper_ld_asi(addr + 4, asi, 4, 0);
2146
        } else {
2147
            env->regwptr[rd] = helper_ld_asi(addr, asi, 4, 0);
2148
            env->regwptr[rd + 1] = helper_ld_asi(addr + 4, asi, 4, 0);
2149
        }
2150
        break;
2151
    }
2152
}
2153

    
2154
void helper_ldf_asi(target_ulong addr, int asi, int size, int rd)
2155
{
2156
    unsigned int i;
2157
    target_ulong val;
2158

    
2159
    helper_check_align(addr, 3);
2160
    switch (asi) {
2161
    case 0xf0: // Block load primary
2162
    case 0xf1: // Block load secondary
2163
    case 0xf8: // Block load primary LE
2164
    case 0xf9: // Block load secondary LE
2165
        if (rd & 7) {
2166
            raise_exception(TT_ILL_INSN);
2167
            return;
2168
        }
2169
        helper_check_align(addr, 0x3f);
2170
        for (i = 0; i < 16; i++) {
2171
            *(uint32_t *)&env->fpr[rd++] = helper_ld_asi(addr, asi & 0x8f, 4,
2172
                                                         0);
2173
            addr += 4;
2174
        }
2175

    
2176
        return;
2177
    default:
2178
        break;
2179
    }
2180

    
2181
    val = helper_ld_asi(addr, asi, size, 0);
2182
    switch(size) {
2183
    default:
2184
    case 4:
2185
        *((uint32_t *)&FT0) = val;
2186
        break;
2187
    case 8:
2188
        *((int64_t *)&DT0) = val;
2189
        break;
2190
    case 16:
2191
        // XXX
2192
        break;
2193
    }
2194
}
2195

    
2196
void helper_stf_asi(target_ulong addr, int asi, int size, int rd)
2197
{
2198
    unsigned int i;
2199
    target_ulong val = 0;
2200

    
2201
    helper_check_align(addr, 3);
2202
    switch (asi) {
2203
    case 0xf0: // Block store primary
2204
    case 0xf1: // Block store secondary
2205
    case 0xf8: // Block store primary LE
2206
    case 0xf9: // Block store secondary LE
2207
        if (rd & 7) {
2208
            raise_exception(TT_ILL_INSN);
2209
            return;
2210
        }
2211
        helper_check_align(addr, 0x3f);
2212
        for (i = 0; i < 16; i++) {
2213
            val = *(uint32_t *)&env->fpr[rd++];
2214
            helper_st_asi(addr, val, asi & 0x8f, 4);
2215
            addr += 4;
2216
        }
2217

    
2218
        return;
2219
    default:
2220
        break;
2221
    }
2222

    
2223
    switch(size) {
2224
    default:
2225
    case 4:
2226
        val = *((uint32_t *)&FT0);
2227
        break;
2228
    case 8:
2229
        val = *((int64_t *)&DT0);
2230
        break;
2231
    case 16:
2232
        // XXX
2233
        break;
2234
    }
2235
    helper_st_asi(addr, val, asi, size);
2236
}
2237

    
2238
target_ulong helper_cas_asi(target_ulong addr, target_ulong val1,
2239
                            target_ulong val2, uint32_t asi)
2240
{
2241
    target_ulong ret;
2242

    
2243
    val1 &= 0xffffffffUL;
2244
    ret = helper_ld_asi(addr, asi, 4, 0);
2245
    ret &= 0xffffffffUL;
2246
    if (val1 == ret)
2247
        helper_st_asi(addr, val2 & 0xffffffffUL, asi, 4);
2248
    return ret;
2249
}
2250

    
2251
target_ulong helper_casx_asi(target_ulong addr, target_ulong val1,
2252
                             target_ulong val2, uint32_t asi)
2253
{
2254
    target_ulong ret;
2255

    
2256
    ret = helper_ld_asi(addr, asi, 8, 0);
2257
    if (val1 == ret)
2258
        helper_st_asi(addr, val2, asi, 8);
2259
    return ret;
2260
}
2261
#endif /* TARGET_SPARC64 */
2262

    
2263
#ifndef TARGET_SPARC64
2264
void helper_rett(void)
2265
{
2266
    unsigned int cwp;
2267

    
2268
    if (env->psret == 1)
2269
        raise_exception(TT_ILL_INSN);
2270

    
2271
    env->psret = 1;
2272
    cwp = cpu_cwp_inc(env, env->cwp + 1) ;
2273
    if (env->wim & (1 << cwp)) {
2274
        raise_exception(TT_WIN_UNF);
2275
    }
2276
    set_cwp(cwp);
2277
    env->psrs = env->psrps;
2278
}
2279
#endif
2280

    
2281
target_ulong helper_udiv(target_ulong a, target_ulong b)
2282
{
2283
    uint64_t x0;
2284
    uint32_t x1;
2285

    
2286
    x0 = a | ((uint64_t) (env->y) << 32);
2287
    x1 = b;
2288

    
2289
    if (x1 == 0) {
2290
        raise_exception(TT_DIV_ZERO);
2291
    }
2292

    
2293
    x0 = x0 / x1;
2294
    if (x0 > 0xffffffff) {
2295
        env->cc_src2 = 1;
2296
        return 0xffffffff;
2297
    } else {
2298
        env->cc_src2 = 0;
2299
        return x0;
2300
    }
2301
}
2302

    
2303
target_ulong helper_sdiv(target_ulong a, target_ulong b)
2304
{
2305
    int64_t x0;
2306
    int32_t x1;
2307

    
2308
    x0 = a | ((int64_t) (env->y) << 32);
2309
    x1 = b;
2310

    
2311
    if (x1 == 0) {
2312
        raise_exception(TT_DIV_ZERO);
2313
    }
2314

    
2315
    x0 = x0 / x1;
2316
    if ((int32_t) x0 != x0) {
2317
        env->cc_src2 = 1;
2318
        return x0 < 0? 0x80000000: 0x7fffffff;
2319
    } else {
2320
        env->cc_src2 = 0;
2321
        return x0;
2322
    }
2323
}
2324

    
2325
uint64_t helper_pack64(target_ulong high, target_ulong low)
2326
{
2327
    return ((uint64_t)high << 32) | (uint64_t)(low & 0xffffffff);
2328
}
2329

    
2330
void helper_stdf(target_ulong addr, int mem_idx)
2331
{
2332
    helper_check_align(addr, 7);
2333
#if !defined(CONFIG_USER_ONLY)
2334
    switch (mem_idx) {
2335
    case 0:
2336
        stfq_user(addr, DT0);
2337
        break;
2338
    case 1:
2339
        stfq_kernel(addr, DT0);
2340
        break;
2341
#ifdef TARGET_SPARC64
2342
    case 2:
2343
        stfq_hypv(addr, DT0);
2344
        break;
2345
#endif
2346
    default:
2347
        break;
2348
    }
2349
#else
2350
    address_mask(env, &addr);
2351
    stfq_raw(addr, DT0);
2352
#endif
2353
}
2354

    
2355
void helper_lddf(target_ulong addr, int mem_idx)
2356
{
2357
    helper_check_align(addr, 7);
2358
#if !defined(CONFIG_USER_ONLY)
2359
    switch (mem_idx) {
2360
    case 0:
2361
        DT0 = ldfq_user(addr);
2362
        break;
2363
    case 1:
2364
        DT0 = ldfq_kernel(addr);
2365
        break;
2366
#ifdef TARGET_SPARC64
2367
    case 2:
2368
        DT0 = ldfq_hypv(addr);
2369
        break;
2370
#endif
2371
    default:
2372
        break;
2373
    }
2374
#else
2375
    address_mask(env, &addr);
2376
    DT0 = ldfq_raw(addr);
2377
#endif
2378
}
2379

    
2380
void helper_ldqf(target_ulong addr, int mem_idx)
2381
{
2382
    // XXX add 128 bit load
2383
    CPU_QuadU u;
2384

    
2385
    helper_check_align(addr, 7);
2386
#if !defined(CONFIG_USER_ONLY)
2387
    switch (mem_idx) {
2388
    case 0:
2389
        u.ll.upper = ldq_user(addr);
2390
        u.ll.lower = ldq_user(addr + 8);
2391
        QT0 = u.q;
2392
        break;
2393
    case 1:
2394
        u.ll.upper = ldq_kernel(addr);
2395
        u.ll.lower = ldq_kernel(addr + 8);
2396
        QT0 = u.q;
2397
        break;
2398
#ifdef TARGET_SPARC64
2399
    case 2:
2400
        u.ll.upper = ldq_hypv(addr);
2401
        u.ll.lower = ldq_hypv(addr + 8);
2402
        QT0 = u.q;
2403
        break;
2404
#endif
2405
    default:
2406
        break;
2407
    }
2408
#else
2409
    address_mask(env, &addr);
2410
    u.ll.upper = ldq_raw(addr);
2411
    u.ll.lower = ldq_raw((addr + 8) & 0xffffffffULL);
2412
    QT0 = u.q;
2413
#endif
2414
}
2415

    
2416
void helper_stqf(target_ulong addr, int mem_idx)
2417
{
2418
    // XXX add 128 bit store
2419
    CPU_QuadU u;
2420

    
2421
    helper_check_align(addr, 7);
2422
#if !defined(CONFIG_USER_ONLY)
2423
    switch (mem_idx) {
2424
    case 0:
2425
        u.q = QT0;
2426
        stq_user(addr, u.ll.upper);
2427
        stq_user(addr + 8, u.ll.lower);
2428
        break;
2429
    case 1:
2430
        u.q = QT0;
2431
        stq_kernel(addr, u.ll.upper);
2432
        stq_kernel(addr + 8, u.ll.lower);
2433
        break;
2434
#ifdef TARGET_SPARC64
2435
    case 2:
2436
        u.q = QT0;
2437
        stq_hypv(addr, u.ll.upper);
2438
        stq_hypv(addr + 8, u.ll.lower);
2439
        break;
2440
#endif
2441
    default:
2442
        break;
2443
    }
2444
#else
2445
    u.q = QT0;
2446
    address_mask(env, &addr);
2447
    stq_raw(addr, u.ll.upper);
2448
    stq_raw((addr + 8) & 0xffffffffULL, u.ll.lower);
2449
#endif
2450
}
2451

    
2452
void helper_ldfsr(void)
2453
{
2454
    int rnd_mode;
2455

    
2456
    PUT_FSR32(env, *((uint32_t *) &FT0));
2457
    switch (env->fsr & FSR_RD_MASK) {
2458
    case FSR_RD_NEAREST:
2459
        rnd_mode = float_round_nearest_even;
2460
        break;
2461
    default:
2462
    case FSR_RD_ZERO:
2463
        rnd_mode = float_round_to_zero;
2464
        break;
2465
    case FSR_RD_POS:
2466
        rnd_mode = float_round_up;
2467
        break;
2468
    case FSR_RD_NEG:
2469
        rnd_mode = float_round_down;
2470
        break;
2471
    }
2472
    set_float_rounding_mode(rnd_mode, &env->fp_status);
2473
}
2474

    
2475
void helper_stfsr(void)
2476
{
2477
    *((uint32_t *) &FT0) = GET_FSR32(env);
2478
}
2479

    
2480
void helper_debug(void)
2481
{
2482
    env->exception_index = EXCP_DEBUG;
2483
    cpu_loop_exit();
2484
}
2485

    
2486
#ifndef TARGET_SPARC64
2487
/* XXX: use another pointer for %iN registers to avoid slow wrapping
2488
   handling ? */
2489
void helper_save(void)
2490
{
2491
    uint32_t cwp;
2492

    
2493
    cwp = cpu_cwp_dec(env, env->cwp - 1);
2494
    if (env->wim & (1 << cwp)) {
2495
        raise_exception(TT_WIN_OVF);
2496
    }
2497
    set_cwp(cwp);
2498
}
2499

    
2500
void helper_restore(void)
2501
{
2502
    uint32_t cwp;
2503

    
2504
    cwp = cpu_cwp_inc(env, env->cwp + 1);
2505
    if (env->wim & (1 << cwp)) {
2506
        raise_exception(TT_WIN_UNF);
2507
    }
2508
    set_cwp(cwp);
2509
}
2510

    
2511
void helper_wrpsr(target_ulong new_psr)
2512
{
2513
    if ((new_psr & PSR_CWP) >= env->nwindows)
2514
        raise_exception(TT_ILL_INSN);
2515
    else
2516
        PUT_PSR(env, new_psr);
2517
}
2518

    
2519
target_ulong helper_rdpsr(void)
2520
{
2521
    return GET_PSR(env);
2522
}
2523

    
2524
#else
2525
/* XXX: use another pointer for %iN registers to avoid slow wrapping
2526
   handling ? */
2527
void helper_save(void)
2528
{
2529
    uint32_t cwp;
2530

    
2531
    cwp = cpu_cwp_dec(env, env->cwp - 1);
2532
    if (env->cansave == 0) {
2533
        raise_exception(TT_SPILL | (env->otherwin != 0 ?
2534
                                    (TT_WOTHER | ((env->wstate & 0x38) >> 1)):
2535
                                    ((env->wstate & 0x7) << 2)));
2536
    } else {
2537
        if (env->cleanwin - env->canrestore == 0) {
2538
            // XXX Clean windows without trap
2539
            raise_exception(TT_CLRWIN);
2540
        } else {
2541
            env->cansave--;
2542
            env->canrestore++;
2543
            set_cwp(cwp);
2544
        }
2545
    }
2546
}
2547

    
2548
void helper_restore(void)
2549
{
2550
    uint32_t cwp;
2551

    
2552
    cwp = cpu_cwp_inc(env, env->cwp + 1);
2553
    if (env->canrestore == 0) {
2554
        raise_exception(TT_FILL | (env->otherwin != 0 ?
2555
                                   (TT_WOTHER | ((env->wstate & 0x38) >> 1)):
2556
                                   ((env->wstate & 0x7) << 2)));
2557
    } else {
2558
        env->cansave++;
2559
        env->canrestore--;
2560
        set_cwp(cwp);
2561
    }
2562
}
2563

    
2564
void helper_flushw(void)
2565
{
2566
    if (env->cansave != env->nwindows - 2) {
2567
        raise_exception(TT_SPILL | (env->otherwin != 0 ?
2568
                                    (TT_WOTHER | ((env->wstate & 0x38) >> 1)):
2569
                                    ((env->wstate & 0x7) << 2)));
2570
    }
2571
}
2572

    
2573
void helper_saved(void)
2574
{
2575
    env->cansave++;
2576
    if (env->otherwin == 0)
2577
        env->canrestore--;
2578
    else
2579
        env->otherwin--;
2580
}
2581

    
2582
void helper_restored(void)
2583
{
2584
    env->canrestore++;
2585
    if (env->cleanwin < env->nwindows - 1)
2586
        env->cleanwin++;
2587
    if (env->otherwin == 0)
2588
        env->cansave--;
2589
    else
2590
        env->otherwin--;
2591
}
2592

    
2593
target_ulong helper_rdccr(void)
2594
{
2595
    return GET_CCR(env);
2596
}
2597

    
2598
void helper_wrccr(target_ulong new_ccr)
2599
{
2600
    PUT_CCR(env, new_ccr);
2601
}
2602

    
2603
// CWP handling is reversed in V9, but we still use the V8 register
2604
// order.
2605
target_ulong helper_rdcwp(void)
2606
{
2607
    return GET_CWP64(env);
2608
}
2609

    
2610
void helper_wrcwp(target_ulong new_cwp)
2611
{
2612
    PUT_CWP64(env, new_cwp);
2613
}
2614

    
2615
// This function uses non-native bit order
2616
#define GET_FIELD(X, FROM, TO)                                  \
2617
    ((X) >> (63 - (TO)) & ((1ULL << ((TO) - (FROM) + 1)) - 1))
2618

    
2619
// This function uses the order in the manuals, i.e. bit 0 is 2^0
2620
#define GET_FIELD_SP(X, FROM, TO)               \
2621
    GET_FIELD(X, 63 - (TO), 63 - (FROM))
2622

    
2623
target_ulong helper_array8(target_ulong pixel_addr, target_ulong cubesize)
2624
{
2625
    return (GET_FIELD_SP(pixel_addr, 60, 63) << (17 + 2 * cubesize)) |
2626
        (GET_FIELD_SP(pixel_addr, 39, 39 + cubesize - 1) << (17 + cubesize)) |
2627
        (GET_FIELD_SP(pixel_addr, 17 + cubesize - 1, 17) << 17) |
2628
        (GET_FIELD_SP(pixel_addr, 56, 59) << 13) |
2629
        (GET_FIELD_SP(pixel_addr, 35, 38) << 9) |
2630
        (GET_FIELD_SP(pixel_addr, 13, 16) << 5) |
2631
        (((pixel_addr >> 55) & 1) << 4) |
2632
        (GET_FIELD_SP(pixel_addr, 33, 34) << 2) |
2633
        GET_FIELD_SP(pixel_addr, 11, 12);
2634
}
2635

    
2636
target_ulong helper_alignaddr(target_ulong addr, target_ulong offset)
2637
{
2638
    uint64_t tmp;
2639

    
2640
    tmp = addr + offset;
2641
    env->gsr &= ~7ULL;
2642
    env->gsr |= tmp & 7ULL;
2643
    return tmp & ~7ULL;
2644
}
2645

    
2646
target_ulong helper_popc(target_ulong val)
2647
{
2648
    return ctpop64(val);
2649
}
2650

    
2651
static inline uint64_t *get_gregset(uint64_t pstate)
2652
{
2653
    switch (pstate) {
2654
    default:
2655
    case 0:
2656
        return env->bgregs;
2657
    case PS_AG:
2658
        return env->agregs;
2659
    case PS_MG:
2660
        return env->mgregs;
2661
    case PS_IG:
2662
        return env->igregs;
2663
    }
2664
}
2665

    
2666
void change_pstate(uint64_t new_pstate)
2667
{
2668
    uint64_t pstate_regs, new_pstate_regs;
2669
    uint64_t *src, *dst;
2670

    
2671
    pstate_regs = env->pstate & 0xc01;
2672
    new_pstate_regs = new_pstate & 0xc01;
2673
    if (new_pstate_regs != pstate_regs) {
2674
        // Switch global register bank
2675
        src = get_gregset(new_pstate_regs);
2676
        dst = get_gregset(pstate_regs);
2677
        memcpy32(dst, env->gregs);
2678
        memcpy32(env->gregs, src);
2679
    }
2680
    env->pstate = new_pstate;
2681
}
2682

    
2683
void helper_wrpstate(target_ulong new_state)
2684
{
2685
    if (!(env->features & CPU_FEATURE_GL))
2686
        change_pstate(new_state & 0xf3f);
2687
}
2688

    
2689
void helper_done(void)
2690
{
2691
    env->pc = env->tsptr->tpc;
2692
    env->npc = env->tsptr->tnpc + 4;
2693
    PUT_CCR(env, env->tsptr->tstate >> 32);
2694
    env->asi = (env->tsptr->tstate >> 24) & 0xff;
2695
    change_pstate((env->tsptr->tstate >> 8) & 0xf3f);
2696
    PUT_CWP64(env, env->tsptr->tstate & 0xff);
2697
    env->tl--;
2698
    env->tsptr = &env->ts[env->tl & MAXTL_MASK];
2699
}
2700

    
2701
void helper_retry(void)
2702
{
2703
    env->pc = env->tsptr->tpc;
2704
    env->npc = env->tsptr->tnpc;
2705
    PUT_CCR(env, env->tsptr->tstate >> 32);
2706
    env->asi = (env->tsptr->tstate >> 24) & 0xff;
2707
    change_pstate((env->tsptr->tstate >> 8) & 0xf3f);
2708
    PUT_CWP64(env, env->tsptr->tstate & 0xff);
2709
    env->tl--;
2710
    env->tsptr = &env->ts[env->tl & MAXTL_MASK];
2711
}
2712
#endif
2713

    
2714
void cpu_set_cwp(CPUState *env1, int new_cwp)
2715
{
2716
    /* put the modified wrap registers at their proper location */
2717
    if (env1->cwp == env1->nwindows - 1)
2718
        memcpy32(env1->regbase, env1->regbase + env1->nwindows * 16);
2719
    env1->cwp = new_cwp;
2720
    /* put the wrap registers at their temporary location */
2721
    if (new_cwp == env1->nwindows - 1)
2722
        memcpy32(env1->regbase + env1->nwindows * 16, env1->regbase);
2723
    env1->regwptr = env1->regbase + (new_cwp * 16);
2724
}
2725

    
2726
void set_cwp(int new_cwp)
2727
{
2728
    cpu_set_cwp(env, new_cwp);
2729
}
2730

    
2731
void helper_flush(target_ulong addr)
2732
{
2733
    addr &= ~7;
2734
    tb_invalidate_page_range(addr, addr + 8);
2735
}
2736

    
2737
#if !defined(CONFIG_USER_ONLY)
2738

    
2739
static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
2740
                                void *retaddr);
2741

    
2742
#define MMUSUFFIX _mmu
2743
#define ALIGNED_ONLY
2744

    
2745
#define SHIFT 0
2746
#include "softmmu_template.h"
2747

    
2748
#define SHIFT 1
2749
#include "softmmu_template.h"
2750

    
2751
#define SHIFT 2
2752
#include "softmmu_template.h"
2753

    
2754
#define SHIFT 3
2755
#include "softmmu_template.h"
2756

    
2757
/* XXX: make it generic ? */
2758
static void cpu_restore_state2(void *retaddr)
2759
{
2760
    TranslationBlock *tb;
2761
    unsigned long pc;
2762

    
2763
    if (retaddr) {
2764
        /* now we have a real cpu fault */
2765
        pc = (unsigned long)retaddr;
2766
        tb = tb_find_pc(pc);
2767
        if (tb) {
2768
            /* the PC is inside the translated code. It means that we have
2769
               a virtual CPU fault */
2770
            cpu_restore_state(tb, env, pc, (void *)(long)env->cond);
2771
        }
2772
    }
2773
}
2774

    
2775
static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
2776
                                void *retaddr)
2777
{
2778
#ifdef DEBUG_UNALIGNED
2779
    printf("Unaligned access to 0x" TARGET_FMT_lx " from 0x" TARGET_FMT_lx
2780
           "\n", addr, env->pc);
2781
#endif
2782
    cpu_restore_state2(retaddr);
2783
    raise_exception(TT_UNALIGNED);
2784
}
2785

    
2786
/* try to fill the TLB and return an exception if error. If retaddr is
2787
   NULL, it means that the function was called in C code (i.e. not
2788
   from generated code or from helper.c) */
2789
/* XXX: fix it to restore all registers */
2790
void tlb_fill(target_ulong addr, int is_write, int mmu_idx, void *retaddr)
2791
{
2792
    int ret;
2793
    CPUState *saved_env;
2794

    
2795
    /* XXX: hack to restore env in all cases, even if not called from
2796
       generated code */
2797
    saved_env = env;
2798
    env = cpu_single_env;
2799

    
2800
    ret = cpu_sparc_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
2801
    if (ret) {
2802
        cpu_restore_state2(retaddr);
2803
        cpu_loop_exit();
2804
    }
2805
    env = saved_env;
2806
}
2807

    
2808
#endif
2809

    
2810
#ifndef TARGET_SPARC64
2811
void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
2812
                          int is_asi)
2813
{
2814
    CPUState *saved_env;
2815

    
2816
    /* XXX: hack to restore env in all cases, even if not called from
2817
       generated code */
2818
    saved_env = env;
2819
    env = cpu_single_env;
2820
#ifdef DEBUG_UNASSIGNED
2821
    if (is_asi)
2822
        printf("Unassigned mem %s access to " TARGET_FMT_plx
2823
               " asi 0x%02x from " TARGET_FMT_lx "\n",
2824
               is_exec ? "exec" : is_write ? "write" : "read", addr, is_asi,
2825
               env->pc);
2826
    else
2827
        printf("Unassigned mem %s access to " TARGET_FMT_plx " from "
2828
               TARGET_FMT_lx "\n",
2829
               is_exec ? "exec" : is_write ? "write" : "read", addr, env->pc);
2830
#endif
2831
    if (env->mmuregs[3]) /* Fault status register */
2832
        env->mmuregs[3] = 1; /* overflow (not read before another fault) */
2833
    if (is_asi)
2834
        env->mmuregs[3] |= 1 << 16;
2835
    if (env->psrs)
2836
        env->mmuregs[3] |= 1 << 5;
2837
    if (is_exec)
2838
        env->mmuregs[3] |= 1 << 6;
2839
    if (is_write)
2840
        env->mmuregs[3] |= 1 << 7;
2841
    env->mmuregs[3] |= (5 << 2) | 2;
2842
    env->mmuregs[4] = addr; /* Fault address register */
2843
    if ((env->mmuregs[0] & MMU_E) && !(env->mmuregs[0] & MMU_NF)) {
2844
        if (is_exec)
2845
            raise_exception(TT_CODE_ACCESS);
2846
        else
2847
            raise_exception(TT_DATA_ACCESS);
2848
    }
2849
    env = saved_env;
2850
}
2851
#else
2852
void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
2853
                          int is_asi)
2854
{
2855
#ifdef DEBUG_UNASSIGNED
2856
    CPUState *saved_env;
2857

    
2858
    /* XXX: hack to restore env in all cases, even if not called from
2859
       generated code */
2860
    saved_env = env;
2861
    env = cpu_single_env;
2862
    printf("Unassigned mem access to " TARGET_FMT_plx " from " TARGET_FMT_lx
2863
           "\n", addr, env->pc);
2864
    env = saved_env;
2865
#endif
2866
    if (is_exec)
2867
        raise_exception(TT_CODE_ACCESS);
2868
    else
2869
        raise_exception(TT_DATA_ACCESS);
2870
}
2871
#endif
2872