Fix I/D MMU tag reads
[qemu] / target-sparc / op_helper.c
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     tmp |= (*((uint64_t *)&DT1)) >> (64 - (env->gsr & 7) * 8);
238     *((uint64_t *)&DT0) = tmp;
239 }
240
241 void helper_movl_FT0_0(void)
242 {
243     *((uint32_t *)&FT0) = 0;
244 }
245
246 void helper_movl_DT0_0(void)
247 {
248     *((uint64_t *)&DT0) = 0;
249 }
250
251 void helper_movl_FT0_1(void)
252 {
253     *((uint32_t *)&FT0) = 0xffffffff;
254 }
255
256 void helper_movl_DT0_1(void)
257 {
258     *((uint64_t *)&DT0) = 0xffffffffffffffffULL;
259 }
260
261 void helper_fnot(void)
262 {
263     *(uint64_t *)&DT0 = ~*(uint64_t *)&DT1;
264 }
265
266 void helper_fnots(void)
267 {
268     *(uint32_t *)&FT0 = ~*(uint32_t *)&FT1;
269 }
270
271 void helper_fnor(void)
272 {
273     *(uint64_t *)&DT0 = ~(*(uint64_t *)&DT0 | *(uint64_t *)&DT1);
274 }
275
276 void helper_fnors(void)
277 {
278     *(uint32_t *)&FT0 = ~(*(uint32_t *)&FT0 | *(uint32_t *)&FT1);
279 }
280
281 void helper_for(void)
282 {
283     *(uint64_t *)&DT0 |= *(uint64_t *)&DT1;
284 }
285
286 void helper_fors(void)
287 {
288     *(uint32_t *)&FT0 |= *(uint32_t *)&FT1;
289 }
290
291 void helper_fxor(void)
292 {
293     *(uint64_t *)&DT0 ^= *(uint64_t *)&DT1;
294 }
295
296 void helper_fxors(void)
297 {
298     *(uint32_t *)&FT0 ^= *(uint32_t *)&FT1;
299 }
300
301 void helper_fand(void)
302 {
303     *(uint64_t *)&DT0 &= *(uint64_t *)&DT1;
304 }
305
306 void helper_fands(void)
307 {
308     *(uint32_t *)&FT0 &= *(uint32_t *)&FT1;
309 }
310
311 void helper_fornot(void)
312 {
313     *(uint64_t *)&DT0 = *(uint64_t *)&DT0 | ~*(uint64_t *)&DT1;
314 }
315
316 void helper_fornots(void)
317 {
318     *(uint32_t *)&FT0 = *(uint32_t *)&FT0 | ~*(uint32_t *)&FT1;
319 }
320
321 void helper_fandnot(void)
322 {
323     *(uint64_t *)&DT0 = *(uint64_t *)&DT0 & ~*(uint64_t *)&DT1;
324 }
325
326 void helper_fandnots(void)
327 {
328     *(uint32_t *)&FT0 = *(uint32_t *)&FT0 & ~*(uint32_t *)&FT1;
329 }
330
331 void helper_fnand(void)
332 {
333     *(uint64_t *)&DT0 = ~(*(uint64_t *)&DT0 & *(uint64_t *)&DT1);
334 }
335
336 void helper_fnands(void)
337 {
338     *(uint32_t *)&FT0 = ~(*(uint32_t *)&FT0 & *(uint32_t *)&FT1);
339 }
340
341 void helper_fxnor(void)
342 {
343     *(uint64_t *)&DT0 ^= ~*(uint64_t *)&DT1;
344 }
345
346 void helper_fxnors(void)
347 {
348     *(uint32_t *)&FT0 ^= ~*(uint32_t *)&FT1;
349 }
350
351 #ifdef WORDS_BIGENDIAN
352 #define VIS_B64(n) b[7 - (n)]
353 #define VIS_W64(n) w[3 - (n)]
354 #define VIS_SW64(n) sw[3 - (n)]
355 #define VIS_L64(n) l[1 - (n)]
356 #define VIS_B32(n) b[3 - (n)]
357 #define VIS_W32(n) w[1 - (n)]
358 #else
359 #define VIS_B64(n) b[n]
360 #define VIS_W64(n) w[n]
361 #define VIS_SW64(n) sw[n]
362 #define VIS_L64(n) l[n]
363 #define VIS_B32(n) b[n]
364 #define VIS_W32(n) w[n]
365 #endif
366
367 typedef union {
368     uint8_t b[8];
369     uint16_t w[4];
370     int16_t sw[4];
371     uint32_t l[2];
372     float64 d;
373 } vis64;
374
375 typedef union {
376     uint8_t b[4];
377     uint16_t w[2];
378     uint32_t l;
379     float32 f;
380 } vis32;
381
382 void helper_fpmerge(void)
383 {
384     vis64 s, d;
385
386     s.d = DT0;
387     d.d = DT1;
388
389     // Reverse calculation order to handle overlap
390     d.VIS_B64(7) = s.VIS_B64(3);
391     d.VIS_B64(6) = d.VIS_B64(3);
392     d.VIS_B64(5) = s.VIS_B64(2);
393     d.VIS_B64(4) = d.VIS_B64(2);
394     d.VIS_B64(3) = s.VIS_B64(1);
395     d.VIS_B64(2) = d.VIS_B64(1);
396     d.VIS_B64(1) = s.VIS_B64(0);
397     //d.VIS_B64(0) = d.VIS_B64(0);
398
399     DT0 = d.d;
400 }
401
402 void helper_fmul8x16(void)
403 {
404     vis64 s, d;
405     uint32_t tmp;
406
407     s.d = DT0;
408     d.d = DT1;
409
410 #define PMUL(r)                                                 \
411     tmp = (int32_t)d.VIS_SW64(r) * (int32_t)s.VIS_B64(r);       \
412     if ((tmp & 0xff) > 0x7f)                                    \
413         tmp += 0x100;                                           \
414     d.VIS_W64(r) = tmp >> 8;
415
416     PMUL(0);
417     PMUL(1);
418     PMUL(2);
419     PMUL(3);
420 #undef PMUL
421
422     DT0 = d.d;
423 }
424
425 void helper_fmul8x16al(void)
426 {
427     vis64 s, d;
428     uint32_t tmp;
429
430     s.d = DT0;
431     d.d = DT1;
432
433 #define PMUL(r)                                                 \
434     tmp = (int32_t)d.VIS_SW64(1) * (int32_t)s.VIS_B64(r);       \
435     if ((tmp & 0xff) > 0x7f)                                    \
436         tmp += 0x100;                                           \
437     d.VIS_W64(r) = tmp >> 8;
438
439     PMUL(0);
440     PMUL(1);
441     PMUL(2);
442     PMUL(3);
443 #undef PMUL
444
445     DT0 = d.d;
446 }
447
448 void helper_fmul8x16au(void)
449 {
450     vis64 s, d;
451     uint32_t tmp;
452
453     s.d = DT0;
454     d.d = DT1;
455
456 #define PMUL(r)                                                 \
457     tmp = (int32_t)d.VIS_SW64(0) * (int32_t)s.VIS_B64(r);       \
458     if ((tmp & 0xff) > 0x7f)                                    \
459         tmp += 0x100;                                           \
460     d.VIS_W64(r) = tmp >> 8;
461
462     PMUL(0);
463     PMUL(1);
464     PMUL(2);
465     PMUL(3);
466 #undef PMUL
467
468     DT0 = d.d;
469 }
470
471 void helper_fmul8sux16(void)
472 {
473     vis64 s, d;
474     uint32_t tmp;
475
476     s.d = DT0;
477     d.d = DT1;
478
479 #define PMUL(r)                                                         \
480     tmp = (int32_t)d.VIS_SW64(r) * ((int32_t)s.VIS_SW64(r) >> 8);       \
481     if ((tmp & 0xff) > 0x7f)                                            \
482         tmp += 0x100;                                                   \
483     d.VIS_W64(r) = tmp >> 8;
484
485     PMUL(0);
486     PMUL(1);
487     PMUL(2);
488     PMUL(3);
489 #undef PMUL
490
491     DT0 = d.d;
492 }
493
494 void helper_fmul8ulx16(void)
495 {
496     vis64 s, d;
497     uint32_t tmp;
498
499     s.d = DT0;
500     d.d = DT1;
501
502 #define PMUL(r)                                                         \
503     tmp = (int32_t)d.VIS_SW64(r) * ((uint32_t)s.VIS_B64(r * 2));        \
504     if ((tmp & 0xff) > 0x7f)                                            \
505         tmp += 0x100;                                                   \
506     d.VIS_W64(r) = tmp >> 8;
507
508     PMUL(0);
509     PMUL(1);
510     PMUL(2);
511     PMUL(3);
512 #undef PMUL
513
514     DT0 = d.d;
515 }
516
517 void helper_fmuld8sux16(void)
518 {
519     vis64 s, d;
520     uint32_t tmp;
521
522     s.d = DT0;
523     d.d = DT1;
524
525 #define PMUL(r)                                                         \
526     tmp = (int32_t)d.VIS_SW64(r) * ((int32_t)s.VIS_SW64(r) >> 8);       \
527     if ((tmp & 0xff) > 0x7f)                                            \
528         tmp += 0x100;                                                   \
529     d.VIS_L64(r) = tmp;
530
531     // Reverse calculation order to handle overlap
532     PMUL(1);
533     PMUL(0);
534 #undef PMUL
535
536     DT0 = d.d;
537 }
538
539 void helper_fmuld8ulx16(void)
540 {
541     vis64 s, d;
542     uint32_t tmp;
543
544     s.d = DT0;
545     d.d = DT1;
546
547 #define PMUL(r)                                                         \
548     tmp = (int32_t)d.VIS_SW64(r) * ((uint32_t)s.VIS_B64(r * 2));        \
549     if ((tmp & 0xff) > 0x7f)                                            \
550         tmp += 0x100;                                                   \
551     d.VIS_L64(r) = tmp;
552
553     // Reverse calculation order to handle overlap
554     PMUL(1);
555     PMUL(0);
556 #undef PMUL
557
558     DT0 = d.d;
559 }
560
561 void helper_fexpand(void)
562 {
563     vis32 s;
564     vis64 d;
565
566     s.l = (uint32_t)(*(uint64_t *)&DT0 & 0xffffffff);
567     d.d = DT1;
568     d.VIS_L64(0) = s.VIS_W32(0) << 4;
569     d.VIS_L64(1) = s.VIS_W32(1) << 4;
570     d.VIS_L64(2) = s.VIS_W32(2) << 4;
571     d.VIS_L64(3) = s.VIS_W32(3) << 4;
572
573     DT0 = d.d;
574 }
575
576 #define VIS_HELPER(name, F)                             \
577     void name##16(void)                                 \
578     {                                                   \
579         vis64 s, d;                                     \
580                                                         \
581         s.d = DT0;                                      \
582         d.d = DT1;                                      \
583                                                         \
584         d.VIS_W64(0) = F(d.VIS_W64(0), s.VIS_W64(0));   \
585         d.VIS_W64(1) = F(d.VIS_W64(1), s.VIS_W64(1));   \
586         d.VIS_W64(2) = F(d.VIS_W64(2), s.VIS_W64(2));   \
587         d.VIS_W64(3) = F(d.VIS_W64(3), s.VIS_W64(3));   \
588                                                         \
589         DT0 = d.d;                                      \
590     }                                                   \
591                                                         \
592     void name##16s(void)                                \
593     {                                                   \
594         vis32 s, d;                                     \
595                                                         \
596         s.f = FT0;                                      \
597         d.f = FT1;                                      \
598                                                         \
599         d.VIS_W32(0) = F(d.VIS_W32(0), s.VIS_W32(0));   \
600         d.VIS_W32(1) = F(d.VIS_W32(1), s.VIS_W32(1));   \
601                                                         \
602         FT0 = d.f;                                      \
603     }                                                   \
604                                                         \
605     void name##32(void)                                 \
606     {                                                   \
607         vis64 s, d;                                     \
608                                                         \
609         s.d = DT0;                                      \
610         d.d = DT1;                                      \
611                                                         \
612         d.VIS_L64(0) = F(d.VIS_L64(0), s.VIS_L64(0));   \
613         d.VIS_L64(1) = F(d.VIS_L64(1), s.VIS_L64(1));   \
614                                                         \
615         DT0 = d.d;                                      \
616     }                                                   \
617                                                         \
618     void name##32s(void)                                \
619     {                                                   \
620         vis32 s, d;                                     \
621                                                         \
622         s.f = FT0;                                      \
623         d.f = FT1;                                      \
624                                                         \
625         d.l = F(d.l, s.l);                              \
626                                                         \
627         FT0 = d.f;                                      \
628     }
629
630 #define FADD(a, b) ((a) + (b))
631 #define FSUB(a, b) ((a) - (b))
632 VIS_HELPER(helper_fpadd, FADD)
633 VIS_HELPER(helper_fpsub, FSUB)
634
635 #define VIS_CMPHELPER(name, F)                                        \
636     void name##16(void)                                           \
637     {                                                             \
638         vis64 s, d;                                               \
639                                                                   \
640         s.d = DT0;                                                \
641         d.d = DT1;                                                \
642                                                                   \
643         d.VIS_W64(0) = F(d.VIS_W64(0), s.VIS_W64(0))? 1: 0;       \
644         d.VIS_W64(0) |= F(d.VIS_W64(1), s.VIS_W64(1))? 2: 0;      \
645         d.VIS_W64(0) |= F(d.VIS_W64(2), s.VIS_W64(2))? 4: 0;      \
646         d.VIS_W64(0) |= F(d.VIS_W64(3), s.VIS_W64(3))? 8: 0;      \
647                                                                   \
648         DT0 = d.d;                                                \
649     }                                                             \
650                                                                   \
651     void name##32(void)                                           \
652     {                                                             \
653         vis64 s, d;                                               \
654                                                                   \
655         s.d = DT0;                                                \
656         d.d = DT1;                                                \
657                                                                   \
658         d.VIS_L64(0) = F(d.VIS_L64(0), s.VIS_L64(0))? 1: 0;       \
659         d.VIS_L64(0) |= F(d.VIS_L64(1), s.VIS_L64(1))? 2: 0;      \
660                                                                   \
661         DT0 = d.d;                                                \
662     }
663
664 #define FCMPGT(a, b) ((a) > (b))
665 #define FCMPEQ(a, b) ((a) == (b))
666 #define FCMPLE(a, b) ((a) <= (b))
667 #define FCMPNE(a, b) ((a) != (b))
668
669 VIS_CMPHELPER(helper_fcmpgt, FCMPGT)
670 VIS_CMPHELPER(helper_fcmpeq, FCMPEQ)
671 VIS_CMPHELPER(helper_fcmple, FCMPLE)
672 VIS_CMPHELPER(helper_fcmpne, FCMPNE)
673 #endif
674
675 void helper_check_ieee_exceptions(void)
676 {
677     target_ulong status;
678
679     status = get_float_exception_flags(&env->fp_status);
680     if (status) {
681         /* Copy IEEE 754 flags into FSR */
682         if (status & float_flag_invalid)
683             env->fsr |= FSR_NVC;
684         if (status & float_flag_overflow)
685             env->fsr |= FSR_OFC;
686         if (status & float_flag_underflow)
687             env->fsr |= FSR_UFC;
688         if (status & float_flag_divbyzero)
689             env->fsr |= FSR_DZC;
690         if (status & float_flag_inexact)
691             env->fsr |= FSR_NXC;
692
693         if ((env->fsr & FSR_CEXC_MASK) & ((env->fsr & FSR_TEM_MASK) >> 23)) {
694             /* Unmasked exception, generate a trap */
695             env->fsr |= FSR_FTT_IEEE_EXCP;
696             raise_exception(TT_FP_EXCP);
697         } else {
698             /* Accumulate exceptions */
699             env->fsr |= (env->fsr & FSR_CEXC_MASK) << 5;
700         }
701     }
702 }
703
704 void helper_clear_float_exceptions(void)
705 {
706     set_float_exception_flags(0, &env->fp_status);
707 }
708
709 void helper_fabss(void)
710 {
711     FT0 = float32_abs(FT1);
712 }
713
714 #ifdef TARGET_SPARC64
715 void helper_fabsd(void)
716 {
717     DT0 = float64_abs(DT1);
718 }
719
720 void helper_fabsq(void)
721 {
722     QT0 = float128_abs(QT1);
723 }
724 #endif
725
726 void helper_fsqrts(void)
727 {
728     FT0 = float32_sqrt(FT1, &env->fp_status);
729 }
730
731 void helper_fsqrtd(void)
732 {
733     DT0 = float64_sqrt(DT1, &env->fp_status);
734 }
735
736 void helper_fsqrtq(void)
737 {
738     QT0 = float128_sqrt(QT1, &env->fp_status);
739 }
740
741 #define GEN_FCMP(name, size, reg1, reg2, FS, TRAP)                      \
742     void glue(helper_, name) (void)                                     \
743     {                                                                   \
744         target_ulong new_fsr;                                           \
745                                                                         \
746         env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS);                     \
747         switch (glue(size, _compare) (reg1, reg2, &env->fp_status)) {   \
748         case float_relation_unordered:                                  \
749             new_fsr = (FSR_FCC1 | FSR_FCC0) << FS;                      \
750             if ((env->fsr & FSR_NVM) || TRAP) {                         \
751                 env->fsr |= new_fsr;                                    \
752                 env->fsr |= FSR_NVC;                                    \
753                 env->fsr |= FSR_FTT_IEEE_EXCP;                          \
754                 raise_exception(TT_FP_EXCP);                            \
755             } else {                                                    \
756                 env->fsr |= FSR_NVA;                                    \
757             }                                                           \
758             break;                                                      \
759         case float_relation_less:                                       \
760             new_fsr = FSR_FCC0 << FS;                                   \
761             break;                                                      \
762         case float_relation_greater:                                    \
763             new_fsr = FSR_FCC1 << FS;                                   \
764             break;                                                      \
765         default:                                                        \
766             new_fsr = 0;                                                \
767             break;                                                      \
768         }                                                               \
769         env->fsr |= new_fsr;                                            \
770     }
771
772 GEN_FCMP(fcmps, float32, FT0, FT1, 0, 0);
773 GEN_FCMP(fcmpd, float64, DT0, DT1, 0, 0);
774
775 GEN_FCMP(fcmpes, float32, FT0, FT1, 0, 1);
776 GEN_FCMP(fcmped, float64, DT0, DT1, 0, 1);
777
778 GEN_FCMP(fcmpq, float128, QT0, QT1, 0, 0);
779 GEN_FCMP(fcmpeq, float128, QT0, QT1, 0, 1);
780
781 #ifdef TARGET_SPARC64
782 GEN_FCMP(fcmps_fcc1, float32, FT0, FT1, 22, 0);
783 GEN_FCMP(fcmpd_fcc1, float64, DT0, DT1, 22, 0);
784 GEN_FCMP(fcmpq_fcc1, float128, QT0, QT1, 22, 0);
785
786 GEN_FCMP(fcmps_fcc2, float32, FT0, FT1, 24, 0);
787 GEN_FCMP(fcmpd_fcc2, float64, DT0, DT1, 24, 0);
788 GEN_FCMP(fcmpq_fcc2, float128, QT0, QT1, 24, 0);
789
790 GEN_FCMP(fcmps_fcc3, float32, FT0, FT1, 26, 0);
791 GEN_FCMP(fcmpd_fcc3, float64, DT0, DT1, 26, 0);
792 GEN_FCMP(fcmpq_fcc3, float128, QT0, QT1, 26, 0);
793
794 GEN_FCMP(fcmpes_fcc1, float32, FT0, FT1, 22, 1);
795 GEN_FCMP(fcmped_fcc1, float64, DT0, DT1, 22, 1);
796 GEN_FCMP(fcmpeq_fcc1, float128, QT0, QT1, 22, 1);
797
798 GEN_FCMP(fcmpes_fcc2, float32, FT0, FT1, 24, 1);
799 GEN_FCMP(fcmped_fcc2, float64, DT0, DT1, 24, 1);
800 GEN_FCMP(fcmpeq_fcc2, float128, QT0, QT1, 24, 1);
801
802 GEN_FCMP(fcmpes_fcc3, float32, FT0, FT1, 26, 1);
803 GEN_FCMP(fcmped_fcc3, float64, DT0, DT1, 26, 1);
804 GEN_FCMP(fcmpeq_fcc3, float128, QT0, QT1, 26, 1);
805 #endif
806
807 #if !defined(TARGET_SPARC64) && !defined(CONFIG_USER_ONLY) && \
808     defined(DEBUG_MXCC)
809 static void dump_mxcc(CPUState *env)
810 {
811     printf("mxccdata: %016llx %016llx %016llx %016llx\n",
812            env->mxccdata[0], env->mxccdata[1],
813            env->mxccdata[2], env->mxccdata[3]);
814     printf("mxccregs: %016llx %016llx %016llx %016llx\n"
815            "          %016llx %016llx %016llx %016llx\n",
816            env->mxccregs[0], env->mxccregs[1],
817            env->mxccregs[2], env->mxccregs[3],
818            env->mxccregs[4], env->mxccregs[5],
819            env->mxccregs[6], env->mxccregs[7]);
820 }
821 #endif
822
823 #if (defined(TARGET_SPARC64) || !defined(CONFIG_USER_ONLY)) \
824     && defined(DEBUG_ASI)
825 static void dump_asi(const char *txt, target_ulong addr, int asi, int size,
826                      uint64_t r1)
827 {
828     switch (size)
829     {
830     case 1:
831         DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %02" PRIx64 "\n", txt,
832                     addr, asi, r1 & 0xff);
833         break;
834     case 2:
835         DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %04" PRIx64 "\n", txt,
836                     addr, asi, r1 & 0xffff);
837         break;
838     case 4:
839         DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %08" PRIx64 "\n", txt,
840                     addr, asi, r1 & 0xffffffff);
841         break;
842     case 8:
843         DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %016" PRIx64 "\n", txt,
844                     addr, asi, r1);
845         break;
846     }
847 }
848 #endif
849
850 #ifndef TARGET_SPARC64
851 #ifndef CONFIG_USER_ONLY
852 uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
853 {
854     uint64_t ret = 0;
855 #if defined(DEBUG_MXCC) || defined(DEBUG_ASI)
856     uint32_t last_addr = addr;
857 #endif
858
859     helper_check_align(addr, size - 1);
860     switch (asi) {
861     case 2: /* SuperSparc MXCC registers */
862         switch (addr) {
863         case 0x01c00a00: /* MXCC control register */
864             if (size == 8)
865                 ret = env->mxccregs[3];
866             else
867                 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
868                              size);
869             break;
870         case 0x01c00a04: /* MXCC control register */
871             if (size == 4)
872                 ret = env->mxccregs[3];
873             else
874                 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
875                              size);
876             break;
877         case 0x01c00c00: /* Module reset register */
878             if (size == 8) {
879                 ret = env->mxccregs[5];
880                 // should we do something here?
881             } else
882                 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
883                              size);
884             break;
885         case 0x01c00f00: /* MBus port address register */
886             if (size == 8)
887                 ret = env->mxccregs[7];
888             else
889                 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
890                              size);
891             break;
892         default:
893             DPRINTF_MXCC("%08x: unimplemented address, size: %d\n", addr,
894                          size);
895             break;
896         }
897         DPRINTF_MXCC("asi = %d, size = %d, sign = %d, "
898                      "addr = %08x -> ret = %08x,"
899                      "addr = %08x\n", asi, size, sign, last_addr, ret, addr);
900 #ifdef DEBUG_MXCC
901         dump_mxcc(env);
902 #endif
903         break;
904     case 3: /* MMU probe */
905         {
906             int mmulev;
907
908             mmulev = (addr >> 8) & 15;
909             if (mmulev > 4)
910                 ret = 0;
911             else
912                 ret = mmu_probe(env, addr, mmulev);
913             DPRINTF_MMU("mmu_probe: 0x%08x (lev %d) -> 0x%08" PRIx64 "\n",
914                         addr, mmulev, ret);
915         }
916         break;
917     case 4: /* read MMU regs */
918         {
919             int reg = (addr >> 8) & 0x1f;
920
921             ret = env->mmuregs[reg];
922             if (reg == 3) /* Fault status cleared on read */
923                 env->mmuregs[3] = 0;
924             else if (reg == 0x13) /* Fault status read */
925                 ret = env->mmuregs[3];
926             else if (reg == 0x14) /* Fault address read */
927                 ret = env->mmuregs[4];
928             DPRINTF_MMU("mmu_read: reg[%d] = 0x%08" PRIx64 "\n", reg, ret);
929         }
930         break;
931     case 5: // Turbosparc ITLB Diagnostic
932     case 6: // Turbosparc DTLB Diagnostic
933     case 7: // Turbosparc IOTLB Diagnostic
934         break;
935     case 9: /* Supervisor code access */
936         switch(size) {
937         case 1:
938             ret = ldub_code(addr);
939             break;
940         case 2:
941             ret = lduw_code(addr);
942             break;
943         default:
944         case 4:
945             ret = ldl_code(addr);
946             break;
947         case 8:
948             ret = ldq_code(addr);
949             break;
950         }
951         break;
952     case 0xa: /* User data access */
953         switch(size) {
954         case 1:
955             ret = ldub_user(addr);
956             break;
957         case 2:
958             ret = lduw_user(addr);
959             break;
960         default:
961         case 4:
962             ret = ldl_user(addr);
963             break;
964         case 8:
965             ret = ldq_user(addr);
966             break;
967         }
968         break;
969     case 0xb: /* Supervisor data access */
970         switch(size) {
971         case 1:
972             ret = ldub_kernel(addr);
973             break;
974         case 2:
975             ret = lduw_kernel(addr);
976             break;
977         default:
978         case 4:
979             ret = ldl_kernel(addr);
980             break;
981         case 8:
982             ret = ldq_kernel(addr);
983             break;
984         }
985         break;
986     case 0xc: /* I-cache tag */
987     case 0xd: /* I-cache data */
988     case 0xe: /* D-cache tag */
989     case 0xf: /* D-cache data */
990         break;
991     case 0x20: /* MMU passthrough */
992         switch(size) {
993         case 1:
994             ret = ldub_phys(addr);
995             break;
996         case 2:
997             ret = lduw_phys(addr);
998             break;
999         default:
1000         case 4:
1001             ret = ldl_phys(addr);
1002             break;
1003         case 8:
1004             ret = ldq_phys(addr);
1005             break;
1006         }
1007         break;
1008     case 0x21 ... 0x2f: /* MMU passthrough, 0x100000000 to 0xfffffffff */
1009         switch(size) {
1010         case 1:
1011             ret = ldub_phys((target_phys_addr_t)addr
1012                             | ((target_phys_addr_t)(asi & 0xf) << 32));
1013             break;
1014         case 2:
1015             ret = lduw_phys((target_phys_addr_t)addr
1016                             | ((target_phys_addr_t)(asi & 0xf) << 32));
1017             break;
1018         default:
1019         case 4:
1020             ret = ldl_phys((target_phys_addr_t)addr
1021                            | ((target_phys_addr_t)(asi & 0xf) << 32));
1022             break;
1023         case 8:
1024             ret = ldq_phys((target_phys_addr_t)addr
1025                            | ((target_phys_addr_t)(asi & 0xf) << 32));
1026             break;
1027         }
1028         break;
1029     case 0x30: // Turbosparc secondary cache diagnostic
1030     case 0x31: // Turbosparc RAM snoop
1031     case 0x32: // Turbosparc page table descriptor diagnostic
1032     case 0x39: /* data cache diagnostic register */
1033         ret = 0;
1034         break;
1035     case 8: /* User code access, XXX */
1036     default:
1037         do_unassigned_access(addr, 0, 0, asi);
1038         ret = 0;
1039         break;
1040     }
1041     if (sign) {
1042         switch(size) {
1043         case 1:
1044             ret = (int8_t) ret;
1045             break;
1046         case 2:
1047             ret = (int16_t) ret;
1048             break;
1049         case 4:
1050             ret = (int32_t) ret;
1051             break;
1052         default:
1053             break;
1054         }
1055     }
1056 #ifdef DEBUG_ASI
1057     dump_asi("read ", last_addr, asi, size, ret);
1058 #endif
1059     return ret;
1060 }
1061
1062 void helper_st_asi(target_ulong addr, uint64_t val, int asi, int size)
1063 {
1064     helper_check_align(addr, size - 1);
1065     switch(asi) {
1066     case 2: /* SuperSparc MXCC registers */
1067         switch (addr) {
1068         case 0x01c00000: /* MXCC stream data register 0 */
1069             if (size == 8)
1070                 env->mxccdata[0] = val;
1071             else
1072                 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
1073                              size);
1074             break;
1075         case 0x01c00008: /* MXCC stream data register 1 */
1076             if (size == 8)
1077                 env->mxccdata[1] = val;
1078             else
1079                 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
1080                              size);
1081             break;
1082         case 0x01c00010: /* MXCC stream data register 2 */
1083             if (size == 8)
1084                 env->mxccdata[2] = val;
1085             else
1086                 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
1087                              size);
1088             break;
1089         case 0x01c00018: /* MXCC stream data register 3 */
1090             if (size == 8)
1091                 env->mxccdata[3] = val;
1092             else
1093                 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
1094                              size);
1095             break;
1096         case 0x01c00100: /* MXCC stream source */
1097             if (size == 8)
1098                 env->mxccregs[0] = val;
1099             else
1100                 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
1101                              size);
1102             env->mxccdata[0] = ldq_phys((env->mxccregs[0] & 0xffffffffULL) +
1103                                         0);
1104             env->mxccdata[1] = ldq_phys((env->mxccregs[0] & 0xffffffffULL) +
1105                                         8);
1106             env->mxccdata[2] = ldq_phys((env->mxccregs[0] & 0xffffffffULL) +
1107                                         16);
1108             env->mxccdata[3] = ldq_phys((env->mxccregs[0] & 0xffffffffULL) +
1109                                         24);
1110             break;
1111         case 0x01c00200: /* MXCC stream destination */
1112             if (size == 8)
1113                 env->mxccregs[1] = val;
1114             else
1115                 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
1116                              size);
1117             stq_phys((env->mxccregs[1] & 0xffffffffULL) +  0,
1118                      env->mxccdata[0]);
1119             stq_phys((env->mxccregs[1] & 0xffffffffULL) +  8,
1120                      env->mxccdata[1]);
1121             stq_phys((env->mxccregs[1] & 0xffffffffULL) + 16,
1122                      env->mxccdata[2]);
1123             stq_phys((env->mxccregs[1] & 0xffffffffULL) + 24,
1124                      env->mxccdata[3]);
1125             break;
1126         case 0x01c00a00: /* MXCC control register */
1127             if (size == 8)
1128                 env->mxccregs[3] = val;
1129             else
1130                 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
1131                              size);
1132             break;
1133         case 0x01c00a04: /* MXCC control register */
1134             if (size == 4)
1135                 env->mxccregs[3] = (env->mxccregs[0xa] & 0xffffffff00000000ULL)
1136                     | val;
1137             else
1138                 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
1139                              size);
1140             break;
1141         case 0x01c00e00: /* MXCC error register  */
1142             // writing a 1 bit clears the error
1143             if (size == 8)
1144                 env->mxccregs[6] &= ~val;
1145             else
1146                 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
1147                              size);
1148             break;
1149         case 0x01c00f00: /* MBus port address register */
1150             if (size == 8)
1151                 env->mxccregs[7] = val;
1152             else
1153                 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", addr,
1154                              size);
1155             break;
1156         default:
1157             DPRINTF_MXCC("%08x: unimplemented address, size: %d\n", addr,
1158                          size);
1159             break;
1160         }
1161         DPRINTF_MXCC("asi = %d, size = %d, addr = %08x, val = %08x\n", asi,
1162                      size, addr, val);
1163 #ifdef DEBUG_MXCC
1164         dump_mxcc(env);
1165 #endif
1166         break;
1167     case 3: /* MMU flush */
1168         {
1169             int mmulev;
1170
1171             mmulev = (addr >> 8) & 15;
1172             DPRINTF_MMU("mmu flush level %d\n", mmulev);
1173             switch (mmulev) {
1174             case 0: // flush page
1175                 tlb_flush_page(env, addr & 0xfffff000);
1176                 break;
1177             case 1: // flush segment (256k)
1178             case 2: // flush region (16M)
1179             case 3: // flush context (4G)
1180             case 4: // flush entire
1181                 tlb_flush(env, 1);
1182                 break;
1183             default:
1184                 break;
1185             }
1186 #ifdef DEBUG_MMU
1187             dump_mmu(env);
1188 #endif
1189         }
1190         break;
1191     case 4: /* write MMU regs */
1192         {
1193             int reg = (addr >> 8) & 0x1f;
1194             uint32_t oldreg;
1195
1196             oldreg = env->mmuregs[reg];
1197             switch(reg) {
1198             case 0: // Control Register
1199                 env->mmuregs[reg] = (env->mmuregs[reg] & 0xff000000) |
1200                                     (val & 0x00ffffff);
1201                 // Mappings generated during no-fault mode or MMU
1202                 // disabled mode are invalid in normal mode
1203                 if ((oldreg & (MMU_E | MMU_NF | env->mmu_bm)) !=
1204                     (env->mmuregs[reg] & (MMU_E | MMU_NF | env->mmu_bm)))
1205                     tlb_flush(env, 1);
1206                 break;
1207             case 1: // Context Table Pointer Register
1208                 env->mmuregs[reg] = val & env->mmu_ctpr_mask;
1209                 break;
1210             case 2: // Context Register
1211                 env->mmuregs[reg] = val & env->mmu_cxr_mask;
1212                 if (oldreg != env->mmuregs[reg]) {
1213                     /* we flush when the MMU context changes because
1214                        QEMU has no MMU context support */
1215                     tlb_flush(env, 1);
1216                 }
1217                 break;
1218             case 3: // Synchronous Fault Status Register with Clear
1219             case 4: // Synchronous Fault Address Register
1220                 break;
1221             case 0x10: // TLB Replacement Control Register
1222                 env->mmuregs[reg] = val & env->mmu_trcr_mask;
1223                 break;
1224             case 0x13: // Synchronous Fault Status Register with Read and Clear
1225                 env->mmuregs[3] = val & env->mmu_sfsr_mask;
1226                 break;
1227             case 0x14: // Synchronous Fault Address Register
1228                 env->mmuregs[4] = val;
1229                 break;
1230             default:
1231                 env->mmuregs[reg] = val;
1232                 break;
1233             }
1234             if (oldreg != env->mmuregs[reg]) {
1235                 DPRINTF_MMU("mmu change reg[%d]: 0x%08x -> 0x%08x\n",
1236                             reg, oldreg, env->mmuregs[reg]);
1237             }
1238 #ifdef DEBUG_MMU
1239             dump_mmu(env);
1240 #endif
1241         }
1242         break;
1243     case 5: // Turbosparc ITLB Diagnostic
1244     case 6: // Turbosparc DTLB Diagnostic
1245     case 7: // Turbosparc IOTLB Diagnostic
1246         break;
1247     case 0xa: /* User data access */
1248         switch(size) {
1249         case 1:
1250             stb_user(addr, val);
1251             break;
1252         case 2:
1253             stw_user(addr, val);
1254             break;
1255         default:
1256         case 4:
1257             stl_user(addr, val);
1258             break;
1259         case 8:
1260             stq_user(addr, val);
1261             break;
1262         }
1263         break;
1264     case 0xb: /* Supervisor data access */
1265         switch(size) {
1266         case 1:
1267             stb_kernel(addr, val);
1268             break;
1269         case 2:
1270             stw_kernel(addr, val);
1271             break;
1272         default:
1273         case 4:
1274             stl_kernel(addr, val);
1275             break;
1276         case 8:
1277             stq_kernel(addr, val);
1278             break;
1279         }
1280         break;
1281     case 0xc: /* I-cache tag */
1282     case 0xd: /* I-cache data */
1283     case 0xe: /* D-cache tag */
1284     case 0xf: /* D-cache data */
1285     case 0x10: /* I/D-cache flush page */
1286     case 0x11: /* I/D-cache flush segment */
1287     case 0x12: /* I/D-cache flush region */
1288     case 0x13: /* I/D-cache flush context */
1289     case 0x14: /* I/D-cache flush user */
1290         break;
1291     case 0x17: /* Block copy, sta access */
1292         {
1293             // val = src
1294             // addr = dst
1295             // copy 32 bytes
1296             unsigned int i;
1297             uint32_t src = val & ~3, dst = addr & ~3, temp;
1298
1299             for (i = 0; i < 32; i += 4, src += 4, dst += 4) {
1300                 temp = ldl_kernel(src);
1301                 stl_kernel(dst, temp);
1302             }
1303         }
1304         break;
1305     case 0x1f: /* Block fill, stda access */
1306         {
1307             // addr = dst
1308             // fill 32 bytes with val
1309             unsigned int i;
1310             uint32_t dst = addr & 7;
1311
1312             for (i = 0; i < 32; i += 8, dst += 8)
1313                 stq_kernel(dst, val);
1314         }
1315         break;
1316     case 0x20: /* MMU passthrough */
1317         {
1318             switch(size) {
1319             case 1:
1320                 stb_phys(addr, val);
1321                 break;
1322             case 2:
1323                 stw_phys(addr, val);
1324                 break;
1325             case 4:
1326             default:
1327                 stl_phys(addr, val);
1328                 break;
1329             case 8:
1330                 stq_phys(addr, val);
1331                 break;
1332             }
1333         }
1334         break;
1335     case 0x21 ... 0x2f: /* MMU passthrough, 0x100000000 to 0xfffffffff */
1336         {
1337             switch(size) {
1338             case 1:
1339                 stb_phys((target_phys_addr_t)addr
1340                          | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1341                 break;
1342             case 2:
1343                 stw_phys((target_phys_addr_t)addr
1344                          | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1345                 break;
1346             case 4:
1347             default:
1348                 stl_phys((target_phys_addr_t)addr
1349                          | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1350                 break;
1351             case 8:
1352                 stq_phys((target_phys_addr_t)addr
1353                          | ((target_phys_addr_t)(asi & 0xf) << 32), val);
1354                 break;
1355             }
1356         }
1357         break;
1358     case 0x30: // store buffer tags or Turbosparc secondary cache diagnostic
1359     case 0x31: // store buffer data, Ross RT620 I-cache flush or
1360                // Turbosparc snoop RAM
1361     case 0x32: // store buffer control or Turbosparc page table
1362                // descriptor diagnostic
1363     case 0x36: /* I-cache flash clear */
1364     case 0x37: /* D-cache flash clear */
1365     case 0x38: /* breakpoint diagnostics */
1366     case 0x4c: /* breakpoint action */
1367         break;
1368     case 8: /* User code access, XXX */
1369     case 9: /* Supervisor code access, XXX */
1370     default:
1371         do_unassigned_access(addr, 1, 0, asi);
1372         break;
1373     }
1374 #ifdef DEBUG_ASI
1375     dump_asi("write", addr, asi, size, val);
1376 #endif
1377 }
1378
1379 #endif /* CONFIG_USER_ONLY */
1380 #else /* TARGET_SPARC64 */
1381
1382 #ifdef CONFIG_USER_ONLY
1383 uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
1384 {
1385     uint64_t ret = 0;
1386 #if defined(DEBUG_ASI)
1387     target_ulong last_addr = addr;
1388 #endif
1389
1390     if (asi < 0x80)
1391         raise_exception(TT_PRIV_ACT);
1392
1393     helper_check_align(addr, size - 1);
1394     address_mask(env, &addr);
1395
1396     switch (asi) {
1397     case 0x80: // Primary
1398     case 0x82: // Primary no-fault
1399     case 0x88: // Primary LE
1400     case 0x8a: // Primary no-fault LE
1401         {
1402             switch(size) {
1403             case 1:
1404                 ret = ldub_raw(addr);
1405                 break;
1406             case 2:
1407                 ret = lduw_raw(addr);
1408                 break;
1409             case 4:
1410                 ret = ldl_raw(addr);
1411                 break;
1412             default:
1413             case 8:
1414                 ret = ldq_raw(addr);
1415                 break;
1416             }
1417         }
1418         break;
1419     case 0x81: // Secondary
1420     case 0x83: // Secondary no-fault
1421     case 0x89: // Secondary LE
1422     case 0x8b: // Secondary no-fault LE
1423         // XXX
1424         break;
1425     default:
1426         break;
1427     }
1428
1429     /* Convert from little endian */
1430     switch (asi) {
1431     case 0x88: // Primary LE
1432     case 0x89: // Secondary LE
1433     case 0x8a: // Primary no-fault LE
1434     case 0x8b: // Secondary no-fault LE
1435         switch(size) {
1436         case 2:
1437             ret = bswap16(ret);
1438             break;
1439         case 4:
1440             ret = bswap32(ret);
1441             break;
1442         case 8:
1443             ret = bswap64(ret);
1444             break;
1445         default:
1446             break;
1447         }
1448     default:
1449         break;
1450     }
1451
1452     /* Convert to signed number */
1453     if (sign) {
1454         switch(size) {
1455         case 1:
1456             ret = (int8_t) ret;
1457             break;
1458         case 2:
1459             ret = (int16_t) ret;
1460             break;
1461         case 4:
1462             ret = (int32_t) ret;
1463             break;
1464         default:
1465             break;
1466         }
1467     }
1468 #ifdef DEBUG_ASI
1469     dump_asi("read ", last_addr, asi, size, ret);
1470 #endif
1471     return ret;
1472 }
1473
1474 void helper_st_asi(target_ulong addr, target_ulong val, int asi, int size)
1475 {
1476 #ifdef DEBUG_ASI
1477     dump_asi("write", addr, asi, size, val);
1478 #endif
1479     if (asi < 0x80)
1480         raise_exception(TT_PRIV_ACT);
1481
1482     helper_check_align(addr, size - 1);
1483     address_mask(env, &addr);
1484
1485     /* Convert to little endian */
1486     switch (asi) {
1487     case 0x88: // Primary LE
1488     case 0x89: // Secondary LE
1489         switch(size) {
1490         case 2:
1491             addr = bswap16(addr);
1492             break;
1493         case 4:
1494             addr = bswap32(addr);
1495             break;
1496         case 8:
1497             addr = bswap64(addr);
1498             break;
1499         default:
1500             break;
1501         }
1502     default:
1503         break;
1504     }
1505
1506     switch(asi) {
1507     case 0x80: // Primary
1508     case 0x88: // Primary LE
1509         {
1510             switch(size) {
1511             case 1:
1512                 stb_raw(addr, val);
1513                 break;
1514             case 2:
1515                 stw_raw(addr, val);
1516                 break;
1517             case 4:
1518                 stl_raw(addr, val);
1519                 break;
1520             case 8:
1521             default:
1522                 stq_raw(addr, val);
1523                 break;
1524             }
1525         }
1526         break;
1527     case 0x81: // Secondary
1528     case 0x89: // Secondary LE
1529         // XXX
1530         return;
1531
1532     case 0x82: // Primary no-fault, RO
1533     case 0x83: // Secondary no-fault, RO
1534     case 0x8a: // Primary no-fault LE, RO
1535     case 0x8b: // Secondary no-fault LE, RO
1536     default:
1537         do_unassigned_access(addr, 1, 0, 1);
1538         return;
1539     }
1540 }
1541
1542 #else /* CONFIG_USER_ONLY */
1543
1544 uint64_t helper_ld_asi(target_ulong addr, int asi, int size, int sign)
1545 {
1546     uint64_t ret = 0;
1547 #if defined(DEBUG_ASI)
1548     target_ulong last_addr = addr;
1549 #endif
1550
1551     if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
1552         || ((env->features & CPU_FEATURE_HYPV) && asi >= 0x30 && asi < 0x80
1553             && !(env->hpstate & HS_PRIV)))
1554         raise_exception(TT_PRIV_ACT);
1555
1556     helper_check_align(addr, size - 1);
1557     switch (asi) {
1558     case 0x10: // As if user primary
1559     case 0x18: // As if user primary LE
1560     case 0x80: // Primary
1561     case 0x82: // Primary no-fault
1562     case 0x88: // Primary LE
1563     case 0x8a: // Primary no-fault LE
1564         if ((asi & 0x80) && (env->pstate & PS_PRIV)) {
1565             if ((env->features & CPU_FEATURE_HYPV) && env->hpstate & HS_PRIV) {
1566                 switch(size) {
1567                 case 1:
1568                     ret = ldub_hypv(addr);
1569                     break;
1570                 case 2:
1571                     ret = lduw_hypv(addr);
1572                     break;
1573                 case 4:
1574                     ret = ldl_hypv(addr);
1575                     break;
1576                 default:
1577                 case 8:
1578                     ret = ldq_hypv(addr);
1579                     break;
1580                 }
1581             } else {
1582                 switch(size) {
1583                 case 1:
1584                     ret = ldub_kernel(addr);
1585                     break;
1586                 case 2:
1587                     ret = lduw_kernel(addr);
1588                     break;
1589                 case 4:
1590                     ret = ldl_kernel(addr);
1591                     break;
1592                 default:
1593                 case 8:
1594                     ret = ldq_kernel(addr);
1595                     break;
1596                 }
1597             }
1598         } else {
1599             switch(size) {
1600             case 1:
1601                 ret = ldub_user(addr);
1602                 break;
1603             case 2:
1604                 ret = lduw_user(addr);
1605                 break;
1606             case 4:
1607                 ret = ldl_user(addr);
1608                 break;
1609             default:
1610             case 8:
1611                 ret = ldq_user(addr);
1612                 break;
1613             }
1614         }
1615         break;
1616     case 0x14: // Bypass
1617     case 0x15: // Bypass, non-cacheable
1618     case 0x1c: // Bypass LE
1619     case 0x1d: // Bypass, non-cacheable LE
1620         {
1621             switch(size) {
1622             case 1:
1623                 ret = ldub_phys(addr);
1624                 break;
1625             case 2:
1626                 ret = lduw_phys(addr);
1627                 break;
1628             case 4:
1629                 ret = ldl_phys(addr);
1630                 break;
1631             default:
1632             case 8:
1633                 ret = ldq_phys(addr);
1634                 break;
1635             }
1636             break;
1637         }
1638     case 0x24: // Nucleus quad LDD 128 bit atomic
1639     case 0x2c: // Nucleus quad LDD 128 bit atomic LE
1640         //  Only ldda allowed
1641         raise_exception(TT_ILL_INSN);
1642         return 0;
1643     case 0x04: // Nucleus
1644     case 0x0c: // Nucleus Little Endian (LE)
1645     case 0x11: // As if user secondary
1646     case 0x19: // As if user secondary LE
1647     case 0x4a: // UPA config
1648     case 0x81: // Secondary
1649     case 0x83: // Secondary no-fault
1650     case 0x89: // Secondary LE
1651     case 0x8b: // Secondary no-fault LE
1652         // XXX
1653         break;
1654     case 0x45: // LSU
1655         ret = env->lsu;
1656         break;
1657     case 0x50: // I-MMU regs
1658         {
1659             int reg = (addr >> 3) & 0xf;
1660
1661             ret = env->immuregs[reg];
1662             break;
1663         }
1664     case 0x51: // I-MMU 8k TSB pointer
1665     case 0x52: // I-MMU 64k TSB pointer
1666         // XXX
1667         break;
1668     case 0x55: // I-MMU data access
1669         {
1670             int reg = (addr >> 3) & 0x3f;
1671
1672             ret = env->itlb_tte[reg];
1673             break;
1674         }
1675     case 0x56: // I-MMU tag read
1676         {
1677             int reg = (addr >> 3) & 0x3f;
1678
1679             ret = env->itlb_tag[reg];
1680             break;
1681         }
1682     case 0x58: // D-MMU regs
1683         {
1684             int reg = (addr >> 3) & 0xf;
1685
1686             ret = env->dmmuregs[reg];
1687             break;
1688         }
1689     case 0x5d: // D-MMU data access
1690         {
1691             int reg = (addr >> 3) & 0x3f;
1692
1693             ret = env->dtlb_tte[reg];
1694             break;
1695         }
1696     case 0x5e: // D-MMU tag read
1697         {
1698             int reg = (addr >> 3) & 0x3f;
1699
1700             ret = env->dtlb_tag[reg];
1701             break;
1702         }
1703     case 0x46: // D-cache data
1704     case 0x47: // D-cache tag access
1705     case 0x4b: // E-cache error enable
1706     case 0x4c: // E-cache asynchronous fault status
1707     case 0x4d: // E-cache asynchronous fault address
1708     case 0x4e: // E-cache tag data
1709     case 0x66: // I-cache instruction access
1710     case 0x67: // I-cache tag access
1711     case 0x6e: // I-cache predecode
1712     case 0x6f: // I-cache LRU etc.
1713     case 0x76: // E-cache tag
1714     case 0x7e: // E-cache tag
1715         break;
1716     case 0x59: // D-MMU 8k TSB pointer
1717     case 0x5a: // D-MMU 64k TSB pointer
1718     case 0x5b: // D-MMU data pointer
1719     case 0x48: // Interrupt dispatch, RO
1720     case 0x49: // Interrupt data receive
1721     case 0x7f: // Incoming interrupt vector, RO
1722         // XXX
1723         break;
1724     case 0x54: // I-MMU data in, WO
1725     case 0x57: // I-MMU demap, WO
1726     case 0x5c: // D-MMU data in, WO
1727     case 0x5f: // D-MMU demap, WO
1728     case 0x77: // Interrupt vector, WO
1729     default:
1730         do_unassigned_access(addr, 0, 0, 1);
1731         ret = 0;
1732         break;
1733     }
1734
1735     /* Convert from little endian */
1736     switch (asi) {
1737     case 0x0c: // Nucleus Little Endian (LE)
1738     case 0x18: // As if user primary LE
1739     case 0x19: // As if user secondary LE
1740     case 0x1c: // Bypass LE
1741     case 0x1d: // Bypass, non-cacheable LE
1742     case 0x88: // Primary LE
1743     case 0x89: // Secondary LE
1744     case 0x8a: // Primary no-fault LE
1745     case 0x8b: // Secondary no-fault LE
1746         switch(size) {
1747         case 2:
1748             ret = bswap16(ret);
1749             break;
1750         case 4:
1751             ret = bswap32(ret);
1752             break;
1753         case 8:
1754             ret = bswap64(ret);
1755             break;
1756         default:
1757             break;
1758         }
1759     default:
1760         break;
1761     }
1762
1763     /* Convert to signed number */
1764     if (sign) {
1765         switch(size) {
1766         case 1:
1767             ret = (int8_t) ret;
1768             break;
1769         case 2:
1770             ret = (int16_t) ret;
1771             break;
1772         case 4:
1773             ret = (int32_t) ret;
1774             break;
1775         default:
1776             break;
1777         }
1778     }
1779 #ifdef DEBUG_ASI
1780     dump_asi("read ", last_addr, asi, size, ret);
1781 #endif
1782     return ret;
1783 }
1784
1785 void helper_st_asi(target_ulong addr, target_ulong val, int asi, int size)
1786 {
1787 #ifdef DEBUG_ASI
1788     dump_asi("write", addr, asi, size, val);
1789 #endif
1790     if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
1791         || ((env->features & CPU_FEATURE_HYPV) && asi >= 0x30 && asi < 0x80
1792             && !(env->hpstate & HS_PRIV)))
1793         raise_exception(TT_PRIV_ACT);
1794
1795     helper_check_align(addr, size - 1);
1796     /* Convert to little endian */
1797     switch (asi) {
1798     case 0x0c: // Nucleus Little Endian (LE)
1799     case 0x18: // As if user primary LE
1800     case 0x19: // As if user secondary LE
1801     case 0x1c: // Bypass LE
1802     case 0x1d: // Bypass, non-cacheable LE
1803     case 0x88: // Primary LE
1804     case 0x89: // Secondary LE
1805         switch(size) {
1806         case 2:
1807             addr = bswap16(addr);
1808             break;
1809         case 4:
1810             addr = bswap32(addr);
1811             break;
1812         case 8:
1813             addr = bswap64(addr);
1814             break;
1815         default:
1816             break;
1817         }
1818     default:
1819         break;
1820     }
1821
1822     switch(asi) {
1823     case 0x10: // As if user primary
1824     case 0x18: // As if user primary LE
1825     case 0x80: // Primary
1826     case 0x88: // Primary LE
1827         if ((asi & 0x80) && (env->pstate & PS_PRIV)) {
1828             if ((env->features & CPU_FEATURE_HYPV) && env->hpstate & HS_PRIV) {
1829                 switch(size) {
1830                 case 1:
1831                     stb_hypv(addr, val);
1832                     break;
1833                 case 2:
1834                     stw_hypv(addr, val);
1835                     break;
1836                 case 4:
1837                     stl_hypv(addr, val);
1838                     break;
1839                 case 8:
1840                 default:
1841                     stq_hypv(addr, val);
1842                     break;
1843                 }
1844             } else {
1845                 switch(size) {
1846                 case 1:
1847                     stb_kernel(addr, val);
1848                     break;
1849                 case 2:
1850                     stw_kernel(addr, val);
1851                     break;
1852                 case 4:
1853                     stl_kernel(addr, val);
1854                     break;
1855                 case 8:
1856                 default:
1857                     stq_kernel(addr, val);
1858                     break;
1859                 }
1860             }
1861         } else {
1862             switch(size) {
1863             case 1:
1864                 stb_user(addr, val);
1865                 break;
1866             case 2:
1867                 stw_user(addr, val);
1868                 break;
1869             case 4:
1870                 stl_user(addr, val);
1871                 break;
1872             case 8:
1873             default:
1874                 stq_user(addr, val);
1875                 break;
1876             }
1877         }
1878         break;
1879     case 0x14: // Bypass
1880     case 0x15: // Bypass, non-cacheable
1881     case 0x1c: // Bypass LE
1882     case 0x1d: // Bypass, non-cacheable LE
1883         {
1884             switch(size) {
1885             case 1:
1886                 stb_phys(addr, val);
1887                 break;
1888             case 2:
1889                 stw_phys(addr, val);
1890                 break;
1891             case 4:
1892                 stl_phys(addr, val);
1893                 break;
1894             case 8:
1895             default:
1896                 stq_phys(addr, val);
1897                 break;
1898             }
1899         }
1900         return;
1901     case 0x24: // Nucleus quad LDD 128 bit atomic
1902     case 0x2c: // Nucleus quad LDD 128 bit atomic LE
1903         //  Only ldda allowed
1904         raise_exception(TT_ILL_INSN);
1905         return;
1906     case 0x04: // Nucleus
1907     case 0x0c: // Nucleus Little Endian (LE)
1908     case 0x11: // As if user secondary
1909     case 0x19: // As if user secondary LE
1910     case 0x4a: // UPA config
1911     case 0x81: // Secondary
1912     case 0x89: // Secondary LE
1913         // XXX
1914         return;
1915     case 0x45: // LSU
1916         {
1917             uint64_t oldreg;
1918
1919             oldreg = env->lsu;
1920             env->lsu = val & (DMMU_E | IMMU_E);
1921             // Mappings generated during D/I MMU disabled mode are
1922             // invalid in normal mode
1923             if (oldreg != env->lsu) {
1924                 DPRINTF_MMU("LSU change: 0x%" PRIx64 " -> 0x%" PRIx64 "\n",
1925                             oldreg, env->lsu);
1926 #ifdef DEBUG_MMU
1927                 dump_mmu(env);
1928 #endif
1929                 tlb_flush(env, 1);
1930             }
1931             return;
1932         }
1933     case 0x50: // I-MMU regs
1934         {
1935             int reg = (addr >> 3) & 0xf;
1936             uint64_t oldreg;
1937
1938             oldreg = env->immuregs[reg];
1939             switch(reg) {
1940             case 0: // RO
1941             case 4:
1942                 return;
1943             case 1: // Not in I-MMU
1944             case 2:
1945             case 7:
1946             case 8:
1947                 return;
1948             case 3: // SFSR
1949                 if ((val & 1) == 0)
1950                     val = 0; // Clear SFSR
1951                 break;
1952             case 5: // TSB access
1953             case 6: // Tag access
1954             default:
1955                 break;
1956             }
1957             env->immuregs[reg] = val;
1958             if (oldreg != env->immuregs[reg]) {
1959                 DPRINTF_MMU("mmu change reg[%d]: 0x%08" PRIx64 " -> 0x%08"
1960                             PRIx64 "\n", reg, oldreg, env->immuregs[reg]);
1961             }
1962 #ifdef DEBUG_MMU
1963             dump_mmu(env);
1964 #endif
1965             return;
1966         }
1967     case 0x54: // I-MMU data in
1968         {
1969             unsigned int i;
1970
1971             // Try finding an invalid entry
1972             for (i = 0; i < 64; i++) {
1973                 if ((env->itlb_tte[i] & 0x8000000000000000ULL) == 0) {
1974                     env->itlb_tag[i] = env->immuregs[6];
1975                     env->itlb_tte[i] = val;
1976                     return;
1977                 }
1978             }
1979             // Try finding an unlocked entry
1980             for (i = 0; i < 64; i++) {
1981                 if ((env->itlb_tte[i] & 0x40) == 0) {
1982                     env->itlb_tag[i] = env->immuregs[6];
1983                     env->itlb_tte[i] = val;
1984                     return;
1985                 }
1986             }
1987             // error state?
1988             return;
1989         }
1990     case 0x55: // I-MMU data access
1991         {
1992             unsigned int i = (addr >> 3) & 0x3f;
1993
1994             env->itlb_tag[i] = env->immuregs[6];
1995             env->itlb_tte[i] = val;
1996             return;
1997         }
1998     case 0x57: // I-MMU demap
1999         // XXX
2000         return;
2001     case 0x58: // D-MMU regs
2002         {
2003             int reg = (addr >> 3) & 0xf;
2004             uint64_t oldreg;
2005
2006             oldreg = env->dmmuregs[reg];
2007             switch(reg) {
2008             case 0: // RO
2009             case 4:
2010                 return;
2011             case 3: // SFSR
2012                 if ((val & 1) == 0) {
2013                     val = 0; // Clear SFSR, Fault address
2014                     env->dmmuregs[4] = 0;
2015                 }
2016                 env->dmmuregs[reg] = val;
2017                 break;
2018             case 1: // Primary context
2019             case 2: // Secondary context
2020             case 5: // TSB access
2021             case 6: // Tag access
2022             case 7: // Virtual Watchpoint
2023             case 8: // Physical Watchpoint
2024             default:
2025                 break;
2026             }
2027             env->dmmuregs[reg] = val;
2028             if (oldreg != env->dmmuregs[reg]) {
2029                 DPRINTF_MMU("mmu change reg[%d]: 0x%08" PRIx64 " -> 0x%08"
2030                             PRIx64 "\n", reg, oldreg, env->dmmuregs[reg]);
2031             }
2032 #ifdef DEBUG_MMU
2033             dump_mmu(env);
2034 #endif
2035             return;
2036         }
2037     case 0x5c: // D-MMU data in
2038         {
2039             unsigned int i;
2040
2041             // Try finding an invalid entry
2042             for (i = 0; i < 64; i++) {
2043                 if ((env->dtlb_tte[i] & 0x8000000000000000ULL) == 0) {
2044                     env->dtlb_tag[i] = env->dmmuregs[6];
2045                     env->dtlb_tte[i] = val;
2046                     return;
2047                 }
2048             }
2049             // Try finding an unlocked entry
2050             for (i = 0; i < 64; i++) {
2051                 if ((env->dtlb_tte[i] & 0x40) == 0) {
2052                     env->dtlb_tag[i] = env->dmmuregs[6];
2053                     env->dtlb_tte[i] = val;
2054                     return;
2055                 }
2056             }
2057             // error state?
2058             return;
2059         }
2060     case 0x5d: // D-MMU data access
2061         {
2062             unsigned int i = (addr >> 3) & 0x3f;
2063
2064             env->dtlb_tag[i] = env->dmmuregs[6];
2065             env->dtlb_tte[i] = val;
2066             return;
2067         }
2068     case 0x5f: // D-MMU demap
2069     case 0x49: // Interrupt data receive
2070         // XXX
2071         return;
2072     case 0x46: // D-cache data
2073     case 0x47: // D-cache tag access
2074     case 0x4b: // E-cache error enable
2075     case 0x4c: // E-cache asynchronous fault status
2076     case 0x4d: // E-cache asynchronous fault address
2077     case 0x4e: // E-cache tag data
2078     case 0x66: // I-cache instruction access
2079     case 0x67: // I-cache tag access
2080     case 0x6e: // I-cache predecode
2081     case 0x6f: // I-cache LRU etc.
2082     case 0x76: // E-cache tag
2083     case 0x7e: // E-cache tag
2084         return;
2085     case 0x51: // I-MMU 8k TSB pointer, RO
2086     case 0x52: // I-MMU 64k TSB pointer, RO
2087     case 0x56: // I-MMU tag read, RO
2088     case 0x59: // D-MMU 8k TSB pointer, RO
2089     case 0x5a: // D-MMU 64k TSB pointer, RO
2090     case 0x5b: // D-MMU data pointer, RO
2091     case 0x5e: // D-MMU tag read, RO
2092     case 0x48: // Interrupt dispatch, RO
2093     case 0x7f: // Incoming interrupt vector, RO
2094     case 0x82: // Primary no-fault, RO
2095     case 0x83: // Secondary no-fault, RO
2096     case 0x8a: // Primary no-fault LE, RO
2097     case 0x8b: // Secondary no-fault LE, RO
2098     default:
2099         do_unassigned_access(addr, 1, 0, 1);
2100         return;
2101     }
2102 }
2103 #endif /* CONFIG_USER_ONLY */
2104
2105 void helper_ldda_asi(target_ulong addr, int asi, int rd)
2106 {
2107     if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
2108         || ((env->features & CPU_FEATURE_HYPV) && asi >= 0x30 && asi < 0x80
2109             && !(env->hpstate & HS_PRIV)))
2110         raise_exception(TT_PRIV_ACT);
2111
2112     switch (asi) {
2113     case 0x24: // Nucleus quad LDD 128 bit atomic
2114     case 0x2c: // Nucleus quad LDD 128 bit atomic LE
2115         helper_check_align(addr, 0xf);
2116         if (rd == 0) {
2117             env->gregs[1] = ldq_kernel(addr + 8);
2118             if (asi == 0x2c)
2119                 bswap64s(&env->gregs[1]);
2120         } else if (rd < 8) {
2121             env->gregs[rd] = ldq_kernel(addr);
2122             env->gregs[rd + 1] = ldq_kernel(addr + 8);
2123             if (asi == 0x2c) {
2124                 bswap64s(&env->gregs[rd]);
2125                 bswap64s(&env->gregs[rd + 1]);
2126             }
2127         } else {
2128             env->regwptr[rd] = ldq_kernel(addr);
2129             env->regwptr[rd + 1] = ldq_kernel(addr + 8);
2130             if (asi == 0x2c) {
2131                 bswap64s(&env->regwptr[rd]);
2132                 bswap64s(&env->regwptr[rd + 1]);
2133             }
2134         }
2135         break;
2136     default:
2137         helper_check_align(addr, 0x3);
2138         if (rd == 0)
2139             env->gregs[1] = helper_ld_asi(addr + 4, asi, 4, 0);
2140         else if (rd < 8) {
2141             env->gregs[rd] = helper_ld_asi(addr, asi, 4, 0);
2142             env->gregs[rd + 1] = helper_ld_asi(addr + 4, asi, 4, 0);
2143         } else {
2144             env->regwptr[rd] = helper_ld_asi(addr, asi, 4, 0);
2145             env->regwptr[rd + 1] = helper_ld_asi(addr + 4, asi, 4, 0);
2146         }
2147         break;
2148     }
2149 }
2150
2151 void helper_ldf_asi(target_ulong addr, int asi, int size, int rd)
2152 {
2153     unsigned int i;
2154     target_ulong val;
2155
2156     helper_check_align(addr, 3);
2157     switch (asi) {
2158     case 0xf0: // Block load primary
2159     case 0xf1: // Block load secondary
2160     case 0xf8: // Block load primary LE
2161     case 0xf9: // Block load secondary LE
2162         if (rd & 7) {
2163             raise_exception(TT_ILL_INSN);
2164             return;
2165         }
2166         helper_check_align(addr, 0x3f);
2167         for (i = 0; i < 16; i++) {
2168             *(uint32_t *)&env->fpr[rd++] = helper_ld_asi(addr, asi & 0x8f, 4,
2169                                                          0);
2170             addr += 4;
2171         }
2172
2173         return;
2174     default:
2175         break;
2176     }
2177
2178     val = helper_ld_asi(addr, asi, size, 0);
2179     switch(size) {
2180     default:
2181     case 4:
2182         *((uint32_t *)&FT0) = val;
2183         break;
2184     case 8:
2185         *((int64_t *)&DT0) = val;
2186         break;
2187     case 16:
2188         // XXX
2189         break;
2190     }
2191 }
2192
2193 void helper_stf_asi(target_ulong addr, int asi, int size, int rd)
2194 {
2195     unsigned int i;
2196     target_ulong val = 0;
2197
2198     helper_check_align(addr, 3);
2199     switch (asi) {
2200     case 0xf0: // Block store primary
2201     case 0xf1: // Block store secondary
2202     case 0xf8: // Block store primary LE
2203     case 0xf9: // Block store secondary LE
2204         if (rd & 7) {
2205             raise_exception(TT_ILL_INSN);
2206             return;
2207         }
2208         helper_check_align(addr, 0x3f);
2209         for (i = 0; i < 16; i++) {
2210             val = *(uint32_t *)&env->fpr[rd++];
2211             helper_st_asi(addr, val, asi & 0x8f, 4);
2212             addr += 4;
2213         }
2214
2215         return;
2216     default:
2217         break;
2218     }
2219
2220     switch(size) {
2221     default:
2222     case 4:
2223         val = *((uint32_t *)&FT0);
2224         break;
2225     case 8:
2226         val = *((int64_t *)&DT0);
2227         break;
2228     case 16:
2229         // XXX
2230         break;
2231     }
2232     helper_st_asi(addr, val, asi, size);
2233 }
2234
2235 target_ulong helper_cas_asi(target_ulong addr, target_ulong val1,
2236                             target_ulong val2, uint32_t asi)
2237 {
2238     target_ulong ret;
2239
2240     val1 &= 0xffffffffUL;
2241     ret = helper_ld_asi(addr, asi, 4, 0);
2242     ret &= 0xffffffffUL;
2243     if (val1 == ret)
2244         helper_st_asi(addr, val2 & 0xffffffffUL, asi, 4);
2245     return ret;
2246 }
2247
2248 target_ulong helper_casx_asi(target_ulong addr, target_ulong val1,
2249                              target_ulong val2, uint32_t asi)
2250 {
2251     target_ulong ret;
2252
2253     ret = helper_ld_asi(addr, asi, 8, 0);
2254     if (val1 == ret)
2255         helper_st_asi(addr, val2, asi, 8);
2256     return ret;
2257 }
2258 #endif /* TARGET_SPARC64 */
2259
2260 #ifndef TARGET_SPARC64
2261 void helper_rett(void)
2262 {
2263     unsigned int cwp;
2264
2265     if (env->psret == 1)
2266         raise_exception(TT_ILL_INSN);
2267
2268     env->psret = 1;
2269     cwp = cpu_cwp_inc(env, env->cwp + 1) ;
2270     if (env->wim & (1 << cwp)) {
2271         raise_exception(TT_WIN_UNF);
2272     }
2273     set_cwp(cwp);
2274     env->psrs = env->psrps;
2275 }
2276 #endif
2277
2278 target_ulong helper_udiv(target_ulong a, target_ulong b)
2279 {
2280     uint64_t x0;
2281     uint32_t x1;
2282
2283     x0 = a | ((uint64_t) (env->y) << 32);
2284     x1 = b;
2285
2286     if (x1 == 0) {
2287         raise_exception(TT_DIV_ZERO);
2288     }
2289
2290     x0 = x0 / x1;
2291     if (x0 > 0xffffffff) {
2292         env->cc_src2 = 1;
2293         return 0xffffffff;
2294     } else {
2295         env->cc_src2 = 0;
2296         return x0;
2297     }
2298 }
2299
2300 target_ulong helper_sdiv(target_ulong a, target_ulong b)
2301 {
2302     int64_t x0;
2303     int32_t x1;
2304
2305     x0 = a | ((int64_t) (env->y) << 32);
2306     x1 = b;
2307
2308     if (x1 == 0) {
2309         raise_exception(TT_DIV_ZERO);
2310     }
2311
2312     x0 = x0 / x1;
2313     if ((int32_t) x0 != x0) {
2314         env->cc_src2 = 1;
2315         return x0 < 0? 0x80000000: 0x7fffffff;
2316     } else {
2317         env->cc_src2 = 0;
2318         return x0;
2319     }
2320 }
2321
2322 uint64_t helper_pack64(target_ulong high, target_ulong low)
2323 {
2324     return ((uint64_t)high << 32) | (uint64_t)(low & 0xffffffff);
2325 }
2326
2327 void helper_stdf(target_ulong addr, int mem_idx)
2328 {
2329     helper_check_align(addr, 7);
2330 #if !defined(CONFIG_USER_ONLY)
2331     switch (mem_idx) {
2332     case 0:
2333         stfq_user(addr, DT0);
2334         break;
2335     case 1:
2336         stfq_kernel(addr, DT0);
2337         break;
2338 #ifdef TARGET_SPARC64
2339     case 2:
2340         stfq_hypv(addr, DT0);
2341         break;
2342 #endif
2343     default:
2344         break;
2345     }
2346 #else
2347     address_mask(env, &addr);
2348     stfq_raw(addr, DT0);
2349 #endif
2350 }
2351
2352 void helper_lddf(target_ulong addr, int mem_idx)
2353 {
2354     helper_check_align(addr, 7);
2355 #if !defined(CONFIG_USER_ONLY)
2356     switch (mem_idx) {
2357     case 0:
2358         DT0 = ldfq_user(addr);
2359         break;
2360     case 1:
2361         DT0 = ldfq_kernel(addr);
2362         break;
2363 #ifdef TARGET_SPARC64
2364     case 2:
2365         DT0 = ldfq_hypv(addr);
2366         break;
2367 #endif
2368     default:
2369         break;
2370     }
2371 #else
2372     address_mask(env, &addr);
2373     DT0 = ldfq_raw(addr);
2374 #endif
2375 }
2376
2377 void helper_ldqf(target_ulong addr, int mem_idx)
2378 {
2379     // XXX add 128 bit load
2380     CPU_QuadU u;
2381
2382     helper_check_align(addr, 7);
2383 #if !defined(CONFIG_USER_ONLY)
2384     switch (mem_idx) {
2385     case 0:
2386         u.ll.upper = ldq_user(addr);
2387         u.ll.lower = ldq_user(addr + 8);
2388         QT0 = u.q;
2389         break;
2390     case 1:
2391         u.ll.upper = ldq_kernel(addr);
2392         u.ll.lower = ldq_kernel(addr + 8);
2393         QT0 = u.q;
2394         break;
2395 #ifdef TARGET_SPARC64
2396     case 2:
2397         u.ll.upper = ldq_hypv(addr);
2398         u.ll.lower = ldq_hypv(addr + 8);
2399         QT0 = u.q;
2400         break;
2401 #endif
2402     default:
2403         break;
2404     }
2405 #else
2406     address_mask(env, &addr);
2407     u.ll.upper = ldq_raw(addr);
2408     u.ll.lower = ldq_raw((addr + 8) & 0xffffffffULL);
2409     QT0 = u.q;
2410 #endif
2411 }
2412
2413 void helper_stqf(target_ulong addr, int mem_idx)
2414 {
2415     // XXX add 128 bit store
2416     CPU_QuadU u;
2417
2418     helper_check_align(addr, 7);
2419 #if !defined(CONFIG_USER_ONLY)
2420     switch (mem_idx) {
2421     case 0:
2422         u.q = QT0;
2423         stq_user(addr, u.ll.upper);
2424         stq_user(addr + 8, u.ll.lower);
2425         break;
2426     case 1:
2427         u.q = QT0;
2428         stq_kernel(addr, u.ll.upper);
2429         stq_kernel(addr + 8, u.ll.lower);
2430         break;
2431 #ifdef TARGET_SPARC64
2432     case 2:
2433         u.q = QT0;
2434         stq_hypv(addr, u.ll.upper);
2435         stq_hypv(addr + 8, u.ll.lower);
2436         break;
2437 #endif
2438     default:
2439         break;
2440     }
2441 #else
2442     u.q = QT0;
2443     address_mask(env, &addr);
2444     stq_raw(addr, u.ll.upper);
2445     stq_raw((addr + 8) & 0xffffffffULL, u.ll.lower);
2446 #endif
2447 }
2448
2449 void helper_ldfsr(void)
2450 {
2451     int rnd_mode;
2452
2453     PUT_FSR32(env, *((uint32_t *) &FT0));
2454     switch (env->fsr & FSR_RD_MASK) {
2455     case FSR_RD_NEAREST:
2456         rnd_mode = float_round_nearest_even;
2457         break;
2458     default:
2459     case FSR_RD_ZERO:
2460         rnd_mode = float_round_to_zero;
2461         break;
2462     case FSR_RD_POS:
2463         rnd_mode = float_round_up;
2464         break;
2465     case FSR_RD_NEG:
2466         rnd_mode = float_round_down;
2467         break;
2468     }
2469     set_float_rounding_mode(rnd_mode, &env->fp_status);
2470 }
2471
2472 void helper_stfsr(void)
2473 {
2474     *((uint32_t *) &FT0) = GET_FSR32(env);
2475 }
2476
2477 void helper_debug(void)
2478 {
2479     env->exception_index = EXCP_DEBUG;
2480     cpu_loop_exit();
2481 }
2482
2483 #ifndef TARGET_SPARC64
2484 /* XXX: use another pointer for %iN registers to avoid slow wrapping
2485    handling ? */
2486 void helper_save(void)
2487 {
2488     uint32_t cwp;
2489
2490     cwp = cpu_cwp_dec(env, env->cwp - 1);
2491     if (env->wim & (1 << cwp)) {
2492         raise_exception(TT_WIN_OVF);
2493     }
2494     set_cwp(cwp);
2495 }
2496
2497 void helper_restore(void)
2498 {
2499     uint32_t cwp;
2500
2501     cwp = cpu_cwp_inc(env, env->cwp + 1);
2502     if (env->wim & (1 << cwp)) {
2503         raise_exception(TT_WIN_UNF);
2504     }
2505     set_cwp(cwp);
2506 }
2507
2508 void helper_wrpsr(target_ulong new_psr)
2509 {
2510     if ((new_psr & PSR_CWP) >= env->nwindows)
2511         raise_exception(TT_ILL_INSN);
2512     else
2513         PUT_PSR(env, new_psr);
2514 }
2515
2516 target_ulong helper_rdpsr(void)
2517 {
2518     return GET_PSR(env);
2519 }
2520
2521 #else
2522 /* XXX: use another pointer for %iN registers to avoid slow wrapping
2523    handling ? */
2524 void helper_save(void)
2525 {
2526     uint32_t cwp;
2527
2528     cwp = cpu_cwp_dec(env, env->cwp - 1);
2529     if (env->cansave == 0) {
2530         raise_exception(TT_SPILL | (env->otherwin != 0 ?
2531                                     (TT_WOTHER | ((env->wstate & 0x38) >> 1)):
2532                                     ((env->wstate & 0x7) << 2)));
2533     } else {
2534         if (env->cleanwin - env->canrestore == 0) {
2535             // XXX Clean windows without trap
2536             raise_exception(TT_CLRWIN);
2537         } else {
2538             env->cansave--;
2539             env->canrestore++;
2540             set_cwp(cwp);
2541         }
2542     }
2543 }
2544
2545 void helper_restore(void)
2546 {
2547     uint32_t cwp;
2548
2549     cwp = cpu_cwp_inc(env, env->cwp + 1);
2550     if (env->canrestore == 0) {
2551         raise_exception(TT_FILL | (env->otherwin != 0 ?
2552                                    (TT_WOTHER | ((env->wstate & 0x38) >> 1)):
2553                                    ((env->wstate & 0x7) << 2)));
2554     } else {
2555         env->cansave++;
2556         env->canrestore--;
2557         set_cwp(cwp);
2558     }
2559 }
2560
2561 void helper_flushw(void)
2562 {
2563     if (env->cansave != env->nwindows - 2) {
2564         raise_exception(TT_SPILL | (env->otherwin != 0 ?
2565                                     (TT_WOTHER | ((env->wstate & 0x38) >> 1)):
2566                                     ((env->wstate & 0x7) << 2)));
2567     }
2568 }
2569
2570 void helper_saved(void)
2571 {
2572     env->cansave++;
2573     if (env->otherwin == 0)
2574         env->canrestore--;
2575     else
2576         env->otherwin--;
2577 }
2578
2579 void helper_restored(void)
2580 {
2581     env->canrestore++;
2582     if (env->cleanwin < env->nwindows - 1)
2583         env->cleanwin++;
2584     if (env->otherwin == 0)
2585         env->cansave--;
2586     else
2587         env->otherwin--;
2588 }
2589
2590 target_ulong helper_rdccr(void)
2591 {
2592     return GET_CCR(env);
2593 }
2594
2595 void helper_wrccr(target_ulong new_ccr)
2596 {
2597     PUT_CCR(env, new_ccr);
2598 }
2599
2600 // CWP handling is reversed in V9, but we still use the V8 register
2601 // order.
2602 target_ulong helper_rdcwp(void)
2603 {
2604     return GET_CWP64(env);
2605 }
2606
2607 void helper_wrcwp(target_ulong new_cwp)
2608 {
2609     PUT_CWP64(env, new_cwp);
2610 }
2611
2612 // This function uses non-native bit order
2613 #define GET_FIELD(X, FROM, TO)                                  \
2614     ((X) >> (63 - (TO)) & ((1ULL << ((TO) - (FROM) + 1)) - 1))
2615
2616 // This function uses the order in the manuals, i.e. bit 0 is 2^0
2617 #define GET_FIELD_SP(X, FROM, TO)               \
2618     GET_FIELD(X, 63 - (TO), 63 - (FROM))
2619
2620 target_ulong helper_array8(target_ulong pixel_addr, target_ulong cubesize)
2621 {
2622     return (GET_FIELD_SP(pixel_addr, 60, 63) << (17 + 2 * cubesize)) |
2623         (GET_FIELD_SP(pixel_addr, 39, 39 + cubesize - 1) << (17 + cubesize)) |
2624         (GET_FIELD_SP(pixel_addr, 17 + cubesize - 1, 17) << 17) |
2625         (GET_FIELD_SP(pixel_addr, 56, 59) << 13) |
2626         (GET_FIELD_SP(pixel_addr, 35, 38) << 9) |
2627         (GET_FIELD_SP(pixel_addr, 13, 16) << 5) |
2628         (((pixel_addr >> 55) & 1) << 4) |
2629         (GET_FIELD_SP(pixel_addr, 33, 34) << 2) |
2630         GET_FIELD_SP(pixel_addr, 11, 12);
2631 }
2632
2633 target_ulong helper_alignaddr(target_ulong addr, target_ulong offset)
2634 {
2635     uint64_t tmp;
2636
2637     tmp = addr + offset;
2638     env->gsr &= ~7ULL;
2639     env->gsr |= tmp & 7ULL;
2640     return tmp & ~7ULL;
2641 }
2642
2643 target_ulong helper_popc(target_ulong val)
2644 {
2645     return ctpop64(val);
2646 }
2647
2648 static inline uint64_t *get_gregset(uint64_t pstate)
2649 {
2650     switch (pstate) {
2651     default:
2652     case 0:
2653         return env->bgregs;
2654     case PS_AG:
2655         return env->agregs;
2656     case PS_MG:
2657         return env->mgregs;
2658     case PS_IG:
2659         return env->igregs;
2660     }
2661 }
2662
2663 void change_pstate(uint64_t new_pstate)
2664 {
2665     uint64_t pstate_regs, new_pstate_regs;
2666     uint64_t *src, *dst;
2667
2668     pstate_regs = env->pstate & 0xc01;
2669     new_pstate_regs = new_pstate & 0xc01;
2670     if (new_pstate_regs != pstate_regs) {
2671         // Switch global register bank
2672         src = get_gregset(new_pstate_regs);
2673         dst = get_gregset(pstate_regs);
2674         memcpy32(dst, env->gregs);
2675         memcpy32(env->gregs, src);
2676     }
2677     env->pstate = new_pstate;
2678 }
2679
2680 void helper_wrpstate(target_ulong new_state)
2681 {
2682     if (!(env->features & CPU_FEATURE_GL))
2683         change_pstate(new_state & 0xf3f);
2684 }
2685
2686 void helper_done(void)
2687 {
2688     env->pc = env->tsptr->tpc;
2689     env->npc = env->tsptr->tnpc + 4;
2690     PUT_CCR(env, env->tsptr->tstate >> 32);
2691     env->asi = (env->tsptr->tstate >> 24) & 0xff;
2692     change_pstate((env->tsptr->tstate >> 8) & 0xf3f);
2693     PUT_CWP64(env, env->tsptr->tstate & 0xff);
2694     env->tl--;
2695     env->tsptr = &env->ts[env->tl & MAXTL_MASK];
2696 }
2697
2698 void helper_retry(void)
2699 {
2700     env->pc = env->tsptr->tpc;
2701     env->npc = env->tsptr->tnpc;
2702     PUT_CCR(env, env->tsptr->tstate >> 32);
2703     env->asi = (env->tsptr->tstate >> 24) & 0xff;
2704     change_pstate((env->tsptr->tstate >> 8) & 0xf3f);
2705     PUT_CWP64(env, env->tsptr->tstate & 0xff);
2706     env->tl--;
2707     env->tsptr = &env->ts[env->tl & MAXTL_MASK];
2708 }
2709 #endif
2710
2711 void cpu_set_cwp(CPUState *env1, int new_cwp)
2712 {
2713     /* put the modified wrap registers at their proper location */
2714     if (env1->cwp == env1->nwindows - 1)
2715         memcpy32(env1->regbase, env1->regbase + env1->nwindows * 16);
2716     env1->cwp = new_cwp;
2717     /* put the wrap registers at their temporary location */
2718     if (new_cwp == env1->nwindows - 1)
2719         memcpy32(env1->regbase + env1->nwindows * 16, env1->regbase);
2720     env1->regwptr = env1->regbase + (new_cwp * 16);
2721 }
2722
2723 void set_cwp(int new_cwp)
2724 {
2725     cpu_set_cwp(env, new_cwp);
2726 }
2727
2728 void helper_flush(target_ulong addr)
2729 {
2730     addr &= ~7;
2731     tb_invalidate_page_range(addr, addr + 8);
2732 }
2733
2734 #if !defined(CONFIG_USER_ONLY)
2735
2736 static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
2737                                 void *retaddr);
2738
2739 #define MMUSUFFIX _mmu
2740 #define ALIGNED_ONLY
2741
2742 #define SHIFT 0
2743 #include "softmmu_template.h"
2744
2745 #define SHIFT 1
2746 #include "softmmu_template.h"
2747
2748 #define SHIFT 2
2749 #include "softmmu_template.h"
2750
2751 #define SHIFT 3
2752 #include "softmmu_template.h"
2753
2754 /* XXX: make it generic ? */
2755 static void cpu_restore_state2(void *retaddr)
2756 {
2757     TranslationBlock *tb;
2758     unsigned long pc;
2759
2760     if (retaddr) {
2761         /* now we have a real cpu fault */
2762         pc = (unsigned long)retaddr;
2763         tb = tb_find_pc(pc);
2764         if (tb) {
2765             /* the PC is inside the translated code. It means that we have
2766                a virtual CPU fault */
2767             cpu_restore_state(tb, env, pc, (void *)(long)env->cond);
2768         }
2769     }
2770 }
2771
2772 static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
2773                                 void *retaddr)
2774 {
2775 #ifdef DEBUG_UNALIGNED
2776     printf("Unaligned access to 0x" TARGET_FMT_lx " from 0x" TARGET_FMT_lx
2777            "\n", addr, env->pc);
2778 #endif
2779     cpu_restore_state2(retaddr);
2780     raise_exception(TT_UNALIGNED);
2781 }
2782
2783 /* try to fill the TLB and return an exception if error. If retaddr is
2784    NULL, it means that the function was called in C code (i.e. not
2785    from generated code or from helper.c) */
2786 /* XXX: fix it to restore all registers */
2787 void tlb_fill(target_ulong addr, int is_write, int mmu_idx, void *retaddr)
2788 {
2789     int ret;
2790     CPUState *saved_env;
2791
2792     /* XXX: hack to restore env in all cases, even if not called from
2793        generated code */
2794     saved_env = env;
2795     env = cpu_single_env;
2796
2797     ret = cpu_sparc_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
2798     if (ret) {
2799         cpu_restore_state2(retaddr);
2800         cpu_loop_exit();
2801     }
2802     env = saved_env;
2803 }
2804
2805 #endif
2806
2807 #ifndef TARGET_SPARC64
2808 void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
2809                           int is_asi)
2810 {
2811     CPUState *saved_env;
2812
2813     /* XXX: hack to restore env in all cases, even if not called from
2814        generated code */
2815     saved_env = env;
2816     env = cpu_single_env;
2817 #ifdef DEBUG_UNASSIGNED
2818     if (is_asi)
2819         printf("Unassigned mem %s access to " TARGET_FMT_plx
2820                " asi 0x%02x from " TARGET_FMT_lx "\n",
2821                is_exec ? "exec" : is_write ? "write" : "read", addr, is_asi,
2822                env->pc);
2823     else
2824         printf("Unassigned mem %s access to " TARGET_FMT_plx " from "
2825                TARGET_FMT_lx "\n",
2826                is_exec ? "exec" : is_write ? "write" : "read", addr, env->pc);
2827 #endif
2828     if (env->mmuregs[3]) /* Fault status register */
2829         env->mmuregs[3] = 1; /* overflow (not read before another fault) */
2830     if (is_asi)
2831         env->mmuregs[3] |= 1 << 16;
2832     if (env->psrs)
2833         env->mmuregs[3] |= 1 << 5;
2834     if (is_exec)
2835         env->mmuregs[3] |= 1 << 6;
2836     if (is_write)
2837         env->mmuregs[3] |= 1 << 7;
2838     env->mmuregs[3] |= (5 << 2) | 2;
2839     env->mmuregs[4] = addr; /* Fault address register */
2840     if ((env->mmuregs[0] & MMU_E) && !(env->mmuregs[0] & MMU_NF)) {
2841         if (is_exec)
2842             raise_exception(TT_CODE_ACCESS);
2843         else
2844             raise_exception(TT_DATA_ACCESS);
2845     }
2846     env = saved_env;
2847 }
2848 #else
2849 void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
2850                           int is_asi)
2851 {
2852 #ifdef DEBUG_UNASSIGNED
2853     CPUState *saved_env;
2854
2855     /* XXX: hack to restore env in all cases, even if not called from
2856        generated code */
2857     saved_env = env;
2858     env = cpu_single_env;
2859     printf("Unassigned mem access to " TARGET_FMT_plx " from " TARGET_FMT_lx
2860            "\n", addr, env->pc);
2861     env = saved_env;
2862 #endif
2863     if (is_exec)
2864         raise_exception(TT_CODE_ACCESS);
2865     else
2866         raise_exception(TT_DATA_ACCESS);
2867 }
2868 #endif
2869