2 #include "host-utils.h"
7 //#define DEBUG_UNALIGNED
8 //#define DEBUG_UNASSIGNED
12 #define DPRINTF_MMU(fmt, args...) \
13 do { printf("MMU: " fmt , ##args); } while (0)
15 #define DPRINTF_MMU(fmt, args...)
19 #define DPRINTF_MXCC(fmt, args...) \
20 do { printf("MXCC: " fmt , ##args); } while (0)
22 #define DPRINTF_MXCC(fmt, args...)
26 #define DPRINTF_ASI(fmt, args...) \
27 do { printf("ASI: " fmt , ##args); } while (0)
29 #define DPRINTF_ASI(fmt, args...)
32 void raise_exception(int tt)
34 env->exception_index = tt;
38 void check_ieee_exceptions()
40 T0 = get_float_exception_flags(&env->fp_status);
43 /* Copy IEEE 754 flags into FSR */
44 if (T0 & float_flag_invalid)
46 if (T0 & float_flag_overflow)
48 if (T0 & float_flag_underflow)
50 if (T0 & float_flag_divbyzero)
52 if (T0 & float_flag_inexact)
55 if ((env->fsr & FSR_CEXC_MASK) & ((env->fsr & FSR_TEM_MASK) >> 23))
57 /* Unmasked exception, generate a trap */
58 env->fsr |= FSR_FTT_IEEE_EXCP;
59 raise_exception(TT_FP_EXCP);
63 /* Accumulate exceptions */
64 env->fsr |= (env->fsr & FSR_CEXC_MASK) << 5;
69 #ifdef USE_INT_TO_FLOAT_HELPERS
72 set_float_exception_flags(0, &env->fp_status);
73 FT0 = int32_to_float32(*((int32_t *)&FT1), &env->fp_status);
74 check_ieee_exceptions();
79 DT0 = int32_to_float64(*((int32_t *)&FT1), &env->fp_status);
82 #if defined(CONFIG_USER_ONLY)
85 QT0 = int32_to_float128(*((int32_t *)&FT1), &env->fp_status);
92 set_float_exception_flags(0, &env->fp_status);
93 FT0 = int64_to_float32(*((int64_t *)&DT1), &env->fp_status);
94 check_ieee_exceptions();
99 set_float_exception_flags(0, &env->fp_status);
100 DT0 = int64_to_float64(*((int64_t *)&DT1), &env->fp_status);
101 check_ieee_exceptions();
104 #if defined(CONFIG_USER_ONLY)
107 set_float_exception_flags(0, &env->fp_status);
108 QT0 = int64_to_float128(*((int32_t *)&DT1), &env->fp_status);
109 check_ieee_exceptions();
117 FT0 = float32_abs(FT1);
120 #ifdef TARGET_SPARC64
123 DT0 = float64_abs(DT1);
126 #if defined(CONFIG_USER_ONLY)
129 QT0 = float128_abs(QT1);
136 set_float_exception_flags(0, &env->fp_status);
137 FT0 = float32_sqrt(FT1, &env->fp_status);
138 check_ieee_exceptions();
143 set_float_exception_flags(0, &env->fp_status);
144 DT0 = float64_sqrt(DT1, &env->fp_status);
145 check_ieee_exceptions();
148 #if defined(CONFIG_USER_ONLY)
151 set_float_exception_flags(0, &env->fp_status);
152 QT0 = float128_sqrt(QT1, &env->fp_status);
153 check_ieee_exceptions();
157 #define GEN_FCMP(name, size, reg1, reg2, FS, TRAP) \
158 void glue(do_, name) (void) \
160 env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS); \
161 switch (glue(size, _compare) (reg1, reg2, &env->fp_status)) { \
162 case float_relation_unordered: \
163 T0 = (FSR_FCC1 | FSR_FCC0) << FS; \
164 if ((env->fsr & FSR_NVM) || TRAP) { \
166 env->fsr |= FSR_NVC; \
167 env->fsr |= FSR_FTT_IEEE_EXCP; \
168 raise_exception(TT_FP_EXCP); \
170 env->fsr |= FSR_NVA; \
173 case float_relation_less: \
174 T0 = FSR_FCC0 << FS; \
176 case float_relation_greater: \
177 T0 = FSR_FCC1 << FS; \
186 GEN_FCMP(fcmps, float32, FT0, FT1, 0, 0);
187 GEN_FCMP(fcmpd, float64, DT0, DT1, 0, 0);
189 GEN_FCMP(fcmpes, float32, FT0, FT1, 0, 1);
190 GEN_FCMP(fcmped, float64, DT0, DT1, 0, 1);
192 #ifdef CONFIG_USER_ONLY
193 GEN_FCMP(fcmpq, float128, QT0, QT1, 0, 0);
194 GEN_FCMP(fcmpeq, float128, QT0, QT1, 0, 1);
197 #ifdef TARGET_SPARC64
198 GEN_FCMP(fcmps_fcc1, float32, FT0, FT1, 22, 0);
199 GEN_FCMP(fcmpd_fcc1, float64, DT0, DT1, 22, 0);
201 GEN_FCMP(fcmps_fcc2, float32, FT0, FT1, 24, 0);
202 GEN_FCMP(fcmpd_fcc2, float64, DT0, DT1, 24, 0);
204 GEN_FCMP(fcmps_fcc3, float32, FT0, FT1, 26, 0);
205 GEN_FCMP(fcmpd_fcc3, float64, DT0, DT1, 26, 0);
207 GEN_FCMP(fcmpes_fcc1, float32, FT0, FT1, 22, 1);
208 GEN_FCMP(fcmped_fcc1, float64, DT0, DT1, 22, 1);
210 GEN_FCMP(fcmpes_fcc2, float32, FT0, FT1, 24, 1);
211 GEN_FCMP(fcmped_fcc2, float64, DT0, DT1, 24, 1);
213 GEN_FCMP(fcmpes_fcc3, float32, FT0, FT1, 26, 1);
214 GEN_FCMP(fcmped_fcc3, float64, DT0, DT1, 26, 1);
215 #ifdef CONFIG_USER_ONLY
216 GEN_FCMP(fcmpq_fcc1, float128, QT0, QT1, 22, 0);
217 GEN_FCMP(fcmpq_fcc2, float128, QT0, QT1, 24, 0);
218 GEN_FCMP(fcmpq_fcc3, float128, QT0, QT1, 26, 0);
219 GEN_FCMP(fcmpeq_fcc1, float128, QT0, QT1, 22, 1);
220 GEN_FCMP(fcmpeq_fcc2, float128, QT0, QT1, 24, 1);
221 GEN_FCMP(fcmpeq_fcc3, float128, QT0, QT1, 26, 1);
225 #ifndef TARGET_SPARC64
226 #ifndef CONFIG_USER_ONLY
229 static void dump_mxcc(CPUState *env)
231 printf("mxccdata: %016llx %016llx %016llx %016llx\n",
232 env->mxccdata[0], env->mxccdata[1], env->mxccdata[2], env->mxccdata[3]);
233 printf("mxccregs: %016llx %016llx %016llx %016llx\n"
234 " %016llx %016llx %016llx %016llx\n",
235 env->mxccregs[0], env->mxccregs[1], env->mxccregs[2], env->mxccregs[3],
236 env->mxccregs[4], env->mxccregs[5], env->mxccregs[6], env->mxccregs[7]);
241 static void dump_asi(const char * txt, uint32_t addr, int asi, int size,
242 uint32_t r1, uint32_t r2)
247 DPRINTF_ASI("%s %08x asi 0x%02x = %02x\n", txt, addr, asi, r1 & 0xff);
250 DPRINTF_ASI("%s %08x asi 0x%02x = %04x\n", txt, addr, asi, r1 & 0xffff);
253 DPRINTF_ASI("%s %08x asi 0x%02x = %08x\n", txt, addr, asi, r1);
256 DPRINTF_ASI("%s %08x asi 0x%02x = %016llx\n", txt, addr, asi,
257 r2 | ((uint64_t)r1 << 32));
263 void helper_ld_asi(int asi, int size, int sign)
267 #if defined(DEBUG_MXCC) || defined(DEBUG_ASI)
268 uint32_t last_T0 = T0;
272 case 2: /* SuperSparc MXCC registers */
274 case 0x01c00a00: /* MXCC control register */
276 ret = env->mxccregs[3] >> 32;
277 T0 = env->mxccregs[3];
279 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
281 case 0x01c00a04: /* MXCC control register */
283 ret = env->mxccregs[3];
285 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
287 case 0x01c00c00: /* Module reset register */
289 ret = env->mxccregs[5] >> 32;
290 T0 = env->mxccregs[5];
291 // should we do something here?
293 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
295 case 0x01c00f00: /* MBus port address register */
297 ret = env->mxccregs[7] >> 32;
298 T0 = env->mxccregs[7];
300 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
303 DPRINTF_MXCC("%08x: unimplemented address, size: %d\n", T0, size);
306 DPRINTF_MXCC("asi = %d, size = %d, sign = %d, T0 = %08x -> ret = %08x,"
307 "T0 = %08x\n", asi, size, sign, last_T0, ret, T0);
312 case 3: /* MMU probe */
316 mmulev = (T0 >> 8) & 15;
320 ret = mmu_probe(env, T0, mmulev);
323 DPRINTF_MMU("mmu_probe: 0x%08x (lev %d) -> 0x%08x\n", T0, mmulev, ret);
326 case 4: /* read MMU regs */
328 int reg = (T0 >> 8) & 0x1f;
330 ret = env->mmuregs[reg];
331 if (reg == 3) /* Fault status cleared on read */
333 else if (reg == 0x13) /* Fault status read */
334 ret = env->mmuregs[3];
335 else if (reg == 0x14) /* Fault address read */
336 ret = env->mmuregs[4];
337 DPRINTF_MMU("mmu_read: reg[%d] = 0x%08x\n", reg, ret);
340 case 9: /* Supervisor code access */
346 ret = lduw_code(T0 & ~1);
350 ret = ldl_code(T0 & ~3);
353 tmp = ldq_code(T0 & ~7);
359 case 0xa: /* User data access */
365 ret = lduw_user(T0 & ~1);
369 ret = ldl_user(T0 & ~3);
372 tmp = ldq_user(T0 & ~7);
378 case 0xb: /* Supervisor data access */
381 ret = ldub_kernel(T0);
384 ret = lduw_kernel(T0 & ~1);
388 ret = ldl_kernel(T0 & ~3);
391 tmp = ldq_kernel(T0 & ~7);
397 case 0xc: /* I-cache tag */
398 case 0xd: /* I-cache data */
399 case 0xe: /* D-cache tag */
400 case 0xf: /* D-cache data */
402 case 0x20: /* MMU passthrough */
408 ret = lduw_phys(T0 & ~1);
412 ret = ldl_phys(T0 & ~3);
415 tmp = ldq_phys(T0 & ~7);
421 case 0x2e: /* MMU passthrough, 0xexxxxxxxx */
422 case 0x2f: /* MMU passthrough, 0xfxxxxxxxx */
425 ret = ldub_phys((target_phys_addr_t)T0
426 | ((target_phys_addr_t)(asi & 0xf) << 32));
429 ret = lduw_phys((target_phys_addr_t)(T0 & ~1)
430 | ((target_phys_addr_t)(asi & 0xf) << 32));
434 ret = ldl_phys((target_phys_addr_t)(T0 & ~3)
435 | ((target_phys_addr_t)(asi & 0xf) << 32));
438 tmp = ldq_phys((target_phys_addr_t)(T0 & ~7)
439 | ((target_phys_addr_t)(asi & 0xf) << 32));
445 case 0x39: /* data cache diagnostic register */
448 case 0x21 ... 0x2d: /* MMU passthrough, unassigned */
450 do_unassigned_access(T0, 0, 0, asi);
470 dump_asi("read ", last_T0, asi, size, T1, T0);
474 void helper_st_asi(int asi, int size)
477 case 2: /* SuperSparc MXCC registers */
479 case 0x01c00000: /* MXCC stream data register 0 */
481 env->mxccdata[0] = ((uint64_t)T1 << 32) | T2;
483 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
485 case 0x01c00008: /* MXCC stream data register 1 */
487 env->mxccdata[1] = ((uint64_t)T1 << 32) | T2;
489 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
491 case 0x01c00010: /* MXCC stream data register 2 */
493 env->mxccdata[2] = ((uint64_t)T1 << 32) | T2;
495 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
497 case 0x01c00018: /* MXCC stream data register 3 */
499 env->mxccdata[3] = ((uint64_t)T1 << 32) | T2;
501 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
503 case 0x01c00100: /* MXCC stream source */
505 env->mxccregs[0] = ((uint64_t)T1 << 32) | T2;
507 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
508 env->mxccdata[0] = ldq_phys((env->mxccregs[0] & 0xffffffffULL) + 0);
509 env->mxccdata[1] = ldq_phys((env->mxccregs[0] & 0xffffffffULL) + 8);
510 env->mxccdata[2] = ldq_phys((env->mxccregs[0] & 0xffffffffULL) + 16);
511 env->mxccdata[3] = ldq_phys((env->mxccregs[0] & 0xffffffffULL) + 24);
513 case 0x01c00200: /* MXCC stream destination */
515 env->mxccregs[1] = ((uint64_t)T1 << 32) | T2;
517 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
518 stq_phys((env->mxccregs[1] & 0xffffffffULL) + 0, env->mxccdata[0]);
519 stq_phys((env->mxccregs[1] & 0xffffffffULL) + 8, env->mxccdata[1]);
520 stq_phys((env->mxccregs[1] & 0xffffffffULL) + 16, env->mxccdata[2]);
521 stq_phys((env->mxccregs[1] & 0xffffffffULL) + 24, env->mxccdata[3]);
523 case 0x01c00a00: /* MXCC control register */
525 env->mxccregs[3] = ((uint64_t)T1 << 32) | T2;
527 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
529 case 0x01c00a04: /* MXCC control register */
531 env->mxccregs[3] = (env->mxccregs[0xa] & 0xffffffff00000000ULL) | T1;
533 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
535 case 0x01c00e00: /* MXCC error register */
536 // writing a 1 bit clears the error
538 env->mxccregs[6] &= ~(((uint64_t)T1 << 32) | T2);
540 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
542 case 0x01c00f00: /* MBus port address register */
544 env->mxccregs[7] = ((uint64_t)T1 << 32) | T2;
546 DPRINTF_MXCC("%08x: unimplemented access size: %d\n", T0, size);
549 DPRINTF_MXCC("%08x: unimplemented address, size: %d\n", T0, size);
552 DPRINTF_MXCC("asi = %d, size = %d, T0 = %08x, T1 = %08x\n", asi, size, T0, T1);
557 case 3: /* MMU flush */
561 mmulev = (T0 >> 8) & 15;
562 DPRINTF_MMU("mmu flush level %d\n", mmulev);
564 case 0: // flush page
565 tlb_flush_page(env, T0 & 0xfffff000);
567 case 1: // flush segment (256k)
568 case 2: // flush region (16M)
569 case 3: // flush context (4G)
570 case 4: // flush entire
581 case 4: /* write MMU regs */
583 int reg = (T0 >> 8) & 0x1f;
586 oldreg = env->mmuregs[reg];
589 env->mmuregs[reg] = (env->mmuregs[reg] & 0xff000000) |
591 // Mappings generated during no-fault mode or MMU
592 // disabled mode are invalid in normal mode
593 if ((oldreg & (MMU_E | MMU_NF | env->mmu_bm)) !=
594 (env->mmuregs[reg] & (MMU_E | MMU_NF | env->mmu_bm)))
598 env->mmuregs[reg] = T1;
599 if (oldreg != env->mmuregs[reg]) {
600 /* we flush when the MMU context changes because
601 QEMU has no MMU context support */
609 env->mmuregs[3] = T1;
612 env->mmuregs[4] = T1;
615 env->mmuregs[reg] = T1;
618 if (oldreg != env->mmuregs[reg]) {
619 DPRINTF_MMU("mmu change reg[%d]: 0x%08x -> 0x%08x\n", reg, oldreg, env->mmuregs[reg]);
626 case 0xa: /* User data access */
632 stw_user(T0 & ~1, T1);
636 stl_user(T0 & ~3, T1);
639 stq_user(T0 & ~7, ((uint64_t)T1 << 32) | T2);
643 case 0xb: /* Supervisor data access */
649 stw_kernel(T0 & ~1, T1);
653 stl_kernel(T0 & ~3, T1);
656 stq_kernel(T0 & ~7, ((uint64_t)T1 << 32) | T2);
660 case 0xc: /* I-cache tag */
661 case 0xd: /* I-cache data */
662 case 0xe: /* D-cache tag */
663 case 0xf: /* D-cache data */
664 case 0x10: /* I/D-cache flush page */
665 case 0x11: /* I/D-cache flush segment */
666 case 0x12: /* I/D-cache flush region */
667 case 0x13: /* I/D-cache flush context */
668 case 0x14: /* I/D-cache flush user */
670 case 0x17: /* Block copy, sta access */
673 // address (T0) = dst
676 uint32_t src = T1 & ~3, dst = T0 & ~3, temp;
678 for (i = 0; i < 32; i += 4, src += 4, dst += 4) {
679 temp = ldl_kernel(src);
680 stl_kernel(dst, temp);
684 case 0x1f: /* Block fill, stda access */
687 // address (T0) = dst
690 uint32_t dst = T0 & 7;
693 val = (((uint64_t)T1) << 32) | T2;
695 for (i = 0; i < 32; i += 8, dst += 8)
696 stq_kernel(dst, val);
699 case 0x20: /* MMU passthrough */
706 stw_phys(T0 & ~1, T1);
710 stl_phys(T0 & ~3, T1);
713 stq_phys(T0 & ~7, ((uint64_t)T1 << 32) | T2);
718 case 0x2e: /* MMU passthrough, 0xexxxxxxxx */
719 case 0x2f: /* MMU passthrough, 0xfxxxxxxxx */
723 stb_phys((target_phys_addr_t)T0
724 | ((target_phys_addr_t)(asi & 0xf) << 32), T1);
727 stw_phys((target_phys_addr_t)(T0 & ~1)
728 | ((target_phys_addr_t)(asi & 0xf) << 32), T1);
732 stl_phys((target_phys_addr_t)(T0 & ~3)
733 | ((target_phys_addr_t)(asi & 0xf) << 32), T1);
736 stq_phys((target_phys_addr_t)(T0 & ~7)
737 | ((target_phys_addr_t)(asi & 0xf) << 32),
738 ((uint64_t)T1 << 32) | T2);
743 case 0x30: /* store buffer tags */
744 case 0x31: /* store buffer data or Ross RT620 I-cache flush */
745 case 0x32: /* store buffer control */
746 case 0x36: /* I-cache flash clear */
747 case 0x37: /* D-cache flash clear */
748 case 0x38: /* breakpoint diagnostics */
749 case 0x4c: /* breakpoint action */
751 case 9: /* Supervisor code access, XXX */
752 case 0x21 ... 0x2d: /* MMU passthrough, unassigned */
754 do_unassigned_access(T0, 1, 0, asi);
758 dump_asi("write", T0, asi, size, T1, T2);
762 #endif /* CONFIG_USER_ONLY */
763 #else /* TARGET_SPARC64 */
765 #ifdef CONFIG_USER_ONLY
766 void helper_ld_asi(int asi, int size, int sign)
771 raise_exception(TT_PRIV_ACT);
774 case 0x80: // Primary
775 case 0x82: // Primary no-fault
776 case 0x88: // Primary LE
777 case 0x8a: // Primary no-fault LE
784 ret = lduw_raw(T0 & ~1);
787 ret = ldl_raw(T0 & ~3);
791 ret = ldq_raw(T0 & ~7);
796 case 0x81: // Secondary
797 case 0x83: // Secondary no-fault
798 case 0x89: // Secondary LE
799 case 0x8b: // Secondary no-fault LE
806 /* Convert from little endian */
808 case 0x88: // Primary LE
809 case 0x89: // Secondary LE
810 case 0x8a: // Primary no-fault LE
811 case 0x8b: // Secondary no-fault LE
829 /* Convert to signed number */
848 void helper_st_asi(int asi, int size)
851 raise_exception(TT_PRIV_ACT);
853 /* Convert to little endian */
855 case 0x88: // Primary LE
856 case 0x89: // Secondary LE
875 case 0x80: // Primary
876 case 0x88: // Primary LE
883 stw_raw(T0 & ~1, T1);
886 stl_raw(T0 & ~3, T1);
890 stq_raw(T0 & ~7, T1);
895 case 0x81: // Secondary
896 case 0x89: // Secondary LE
900 case 0x82: // Primary no-fault, RO
901 case 0x83: // Secondary no-fault, RO
902 case 0x8a: // Primary no-fault LE, RO
903 case 0x8b: // Secondary no-fault LE, RO
905 do_unassigned_access(T0, 1, 0, 1);
910 #else /* CONFIG_USER_ONLY */
912 void helper_ld_asi(int asi, int size, int sign)
916 if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
917 || (asi >= 0x30 && asi < 0x80 && !(env->hpstate & HS_PRIV)))
918 raise_exception(TT_PRIV_ACT);
921 case 0x10: // As if user primary
922 case 0x18: // As if user primary LE
923 case 0x80: // Primary
924 case 0x82: // Primary no-fault
925 case 0x88: // Primary LE
926 case 0x8a: // Primary no-fault LE
927 if ((asi & 0x80) && (env->pstate & PS_PRIV)) {
928 if (env->hpstate & HS_PRIV) {
934 ret = lduw_hypv(T0 & ~1);
937 ret = ldl_hypv(T0 & ~3);
941 ret = ldq_hypv(T0 & ~7);
947 ret = ldub_kernel(T0);
950 ret = lduw_kernel(T0 & ~1);
953 ret = ldl_kernel(T0 & ~3);
957 ret = ldq_kernel(T0 & ~7);
967 ret = lduw_user(T0 & ~1);
970 ret = ldl_user(T0 & ~3);
974 ret = ldq_user(T0 & ~7);
980 case 0x15: // Bypass, non-cacheable
981 case 0x1c: // Bypass LE
982 case 0x1d: // Bypass, non-cacheable LE
989 ret = lduw_phys(T0 & ~1);
992 ret = ldl_phys(T0 & ~3);
996 ret = ldq_phys(T0 & ~7);
1001 case 0x04: // Nucleus
1002 case 0x0c: // Nucleus Little Endian (LE)
1003 case 0x11: // As if user secondary
1004 case 0x19: // As if user secondary LE
1005 case 0x24: // Nucleus quad LDD 128 bit atomic
1006 case 0x2c: // Nucleus quad LDD 128 bit atomic
1007 case 0x4a: // UPA config
1008 case 0x81: // Secondary
1009 case 0x83: // Secondary no-fault
1010 case 0x89: // Secondary LE
1011 case 0x8b: // Secondary no-fault LE
1017 case 0x50: // I-MMU regs
1019 int reg = (T0 >> 3) & 0xf;
1021 ret = env->immuregs[reg];
1024 case 0x51: // I-MMU 8k TSB pointer
1025 case 0x52: // I-MMU 64k TSB pointer
1026 case 0x55: // I-MMU data access
1029 case 0x56: // I-MMU tag read
1033 for (i = 0; i < 64; i++) {
1034 // Valid, ctx match, vaddr match
1035 if ((env->itlb_tte[i] & 0x8000000000000000ULL) != 0 &&
1036 env->itlb_tag[i] == T0) {
1037 ret = env->itlb_tag[i];
1043 case 0x58: // D-MMU regs
1045 int reg = (T0 >> 3) & 0xf;
1047 ret = env->dmmuregs[reg];
1050 case 0x5e: // D-MMU tag read
1054 for (i = 0; i < 64; i++) {
1055 // Valid, ctx match, vaddr match
1056 if ((env->dtlb_tte[i] & 0x8000000000000000ULL) != 0 &&
1057 env->dtlb_tag[i] == T0) {
1058 ret = env->dtlb_tag[i];
1064 case 0x59: // D-MMU 8k TSB pointer
1065 case 0x5a: // D-MMU 64k TSB pointer
1066 case 0x5b: // D-MMU data pointer
1067 case 0x5d: // D-MMU data access
1068 case 0x48: // Interrupt dispatch, RO
1069 case 0x49: // Interrupt data receive
1070 case 0x7f: // Incoming interrupt vector, RO
1073 case 0x54: // I-MMU data in, WO
1074 case 0x57: // I-MMU demap, WO
1075 case 0x5c: // D-MMU data in, WO
1076 case 0x5f: // D-MMU demap, WO
1077 case 0x77: // Interrupt vector, WO
1079 do_unassigned_access(T0, 0, 0, 1);
1084 /* Convert from little endian */
1086 case 0x0c: // Nucleus Little Endian (LE)
1087 case 0x18: // As if user primary LE
1088 case 0x19: // As if user secondary LE
1089 case 0x1c: // Bypass LE
1090 case 0x1d: // Bypass, non-cacheable LE
1091 case 0x88: // Primary LE
1092 case 0x89: // Secondary LE
1093 case 0x8a: // Primary no-fault LE
1094 case 0x8b: // Secondary no-fault LE
1112 /* Convert to signed number */
1119 ret = (int16_t) ret;
1122 ret = (int32_t) ret;
1131 void helper_st_asi(int asi, int size)
1133 if ((asi < 0x80 && (env->pstate & PS_PRIV) == 0)
1134 || (asi >= 0x30 && asi < 0x80 && !(env->hpstate & HS_PRIV)))
1135 raise_exception(TT_PRIV_ACT);
1137 /* Convert to little endian */
1139 case 0x0c: // Nucleus Little Endian (LE)
1140 case 0x18: // As if user primary LE
1141 case 0x19: // As if user secondary LE
1142 case 0x1c: // Bypass LE
1143 case 0x1d: // Bypass, non-cacheable LE
1144 case 0x88: // Primary LE
1145 case 0x89: // Secondary LE
1164 case 0x10: // As if user primary
1165 case 0x18: // As if user primary LE
1166 case 0x80: // Primary
1167 case 0x88: // Primary LE
1168 if ((asi & 0x80) && (env->pstate & PS_PRIV)) {
1169 if (env->hpstate & HS_PRIV) {
1175 stw_hypv(T0 & ~1, T1);
1178 stl_hypv(T0 & ~3, T1);
1182 stq_hypv(T0 & ~7, T1);
1191 stw_kernel(T0 & ~1, T1);
1194 stl_kernel(T0 & ~3, T1);
1198 stq_kernel(T0 & ~7, T1);
1208 stw_user(T0 & ~1, T1);
1211 stl_user(T0 & ~3, T1);
1215 stq_user(T0 & ~7, T1);
1220 case 0x14: // Bypass
1221 case 0x15: // Bypass, non-cacheable
1222 case 0x1c: // Bypass LE
1223 case 0x1d: // Bypass, non-cacheable LE
1230 stw_phys(T0 & ~1, T1);
1233 stl_phys(T0 & ~3, T1);
1237 stq_phys(T0 & ~7, T1);
1242 case 0x04: // Nucleus
1243 case 0x0c: // Nucleus Little Endian (LE)
1244 case 0x11: // As if user secondary
1245 case 0x19: // As if user secondary LE
1246 case 0x24: // Nucleus quad LDD 128 bit atomic
1247 case 0x2c: // Nucleus quad LDD 128 bit atomic
1248 case 0x4a: // UPA config
1249 case 0x81: // Secondary
1250 case 0x89: // Secondary LE
1258 env->lsu = T1 & (DMMU_E | IMMU_E);
1259 // Mappings generated during D/I MMU disabled mode are
1260 // invalid in normal mode
1261 if (oldreg != env->lsu) {
1262 DPRINTF_MMU("LSU change: 0x%" PRIx64 " -> 0x%" PRIx64 "\n", oldreg, env->lsu);
1270 case 0x50: // I-MMU regs
1272 int reg = (T0 >> 3) & 0xf;
1275 oldreg = env->immuregs[reg];
1280 case 1: // Not in I-MMU
1287 T1 = 0; // Clear SFSR
1289 case 5: // TSB access
1290 case 6: // Tag access
1294 env->immuregs[reg] = T1;
1295 if (oldreg != env->immuregs[reg]) {
1296 DPRINTF_MMU("mmu change reg[%d]: 0x%08" PRIx64 " -> 0x%08" PRIx64 "\n", reg, oldreg, env->immuregs[reg]);
1303 case 0x54: // I-MMU data in
1307 // Try finding an invalid entry
1308 for (i = 0; i < 64; i++) {
1309 if ((env->itlb_tte[i] & 0x8000000000000000ULL) == 0) {
1310 env->itlb_tag[i] = env->immuregs[6];
1311 env->itlb_tte[i] = T1;
1315 // Try finding an unlocked entry
1316 for (i = 0; i < 64; i++) {
1317 if ((env->itlb_tte[i] & 0x40) == 0) {
1318 env->itlb_tag[i] = env->immuregs[6];
1319 env->itlb_tte[i] = T1;
1326 case 0x55: // I-MMU data access
1328 unsigned int i = (T0 >> 3) & 0x3f;
1330 env->itlb_tag[i] = env->immuregs[6];
1331 env->itlb_tte[i] = T1;
1334 case 0x57: // I-MMU demap
1337 case 0x58: // D-MMU regs
1339 int reg = (T0 >> 3) & 0xf;
1342 oldreg = env->dmmuregs[reg];
1348 if ((T1 & 1) == 0) {
1349 T1 = 0; // Clear SFSR, Fault address
1350 env->dmmuregs[4] = 0;
1352 env->dmmuregs[reg] = T1;
1354 case 1: // Primary context
1355 case 2: // Secondary context
1356 case 5: // TSB access
1357 case 6: // Tag access
1358 case 7: // Virtual Watchpoint
1359 case 8: // Physical Watchpoint
1363 env->dmmuregs[reg] = T1;
1364 if (oldreg != env->dmmuregs[reg]) {
1365 DPRINTF_MMU("mmu change reg[%d]: 0x%08" PRIx64 " -> 0x%08" PRIx64 "\n", reg, oldreg, env->dmmuregs[reg]);
1372 case 0x5c: // D-MMU data in
1376 // Try finding an invalid entry
1377 for (i = 0; i < 64; i++) {
1378 if ((env->dtlb_tte[i] & 0x8000000000000000ULL) == 0) {
1379 env->dtlb_tag[i] = env->dmmuregs[6];
1380 env->dtlb_tte[i] = T1;
1384 // Try finding an unlocked entry
1385 for (i = 0; i < 64; i++) {
1386 if ((env->dtlb_tte[i] & 0x40) == 0) {
1387 env->dtlb_tag[i] = env->dmmuregs[6];
1388 env->dtlb_tte[i] = T1;
1395 case 0x5d: // D-MMU data access
1397 unsigned int i = (T0 >> 3) & 0x3f;
1399 env->dtlb_tag[i] = env->dmmuregs[6];
1400 env->dtlb_tte[i] = T1;
1403 case 0x5f: // D-MMU demap
1404 case 0x49: // Interrupt data receive
1407 case 0x51: // I-MMU 8k TSB pointer, RO
1408 case 0x52: // I-MMU 64k TSB pointer, RO
1409 case 0x56: // I-MMU tag read, RO
1410 case 0x59: // D-MMU 8k TSB pointer, RO
1411 case 0x5a: // D-MMU 64k TSB pointer, RO
1412 case 0x5b: // D-MMU data pointer, RO
1413 case 0x5e: // D-MMU tag read, RO
1414 case 0x48: // Interrupt dispatch, RO
1415 case 0x7f: // Incoming interrupt vector, RO
1416 case 0x82: // Primary no-fault, RO
1417 case 0x83: // Secondary no-fault, RO
1418 case 0x8a: // Primary no-fault LE, RO
1419 case 0x8b: // Secondary no-fault LE, RO
1421 do_unassigned_access(T0, 1, 0, 1);
1425 #endif /* CONFIG_USER_ONLY */
1427 void helper_ldf_asi(int asi, int size, int rd)
1429 target_ulong tmp_T0 = T0, tmp_T1 = T1;
1433 case 0xf0: // Block load primary
1434 case 0xf1: // Block load secondary
1435 case 0xf8: // Block load primary LE
1436 case 0xf9: // Block load secondary LE
1438 raise_exception(TT_ILL_INSN);
1442 raise_exception(TT_UNALIGNED);
1445 for (i = 0; i < 16; i++) {
1446 helper_ld_asi(asi & 0x8f, 4, 0);
1447 *(uint32_t *)&env->fpr[rd++] = T1;
1458 helper_ld_asi(asi, size, 0);
1462 *((uint32_t *)&FT0) = T1;
1465 *((int64_t *)&DT0) = T1;
1467 #if defined(CONFIG_USER_ONLY)
1476 void helper_stf_asi(int asi, int size, int rd)
1478 target_ulong tmp_T0 = T0, tmp_T1 = T1;
1482 case 0xf0: // Block store primary
1483 case 0xf1: // Block store secondary
1484 case 0xf8: // Block store primary LE
1485 case 0xf9: // Block store secondary LE
1487 raise_exception(TT_ILL_INSN);
1491 raise_exception(TT_UNALIGNED);
1494 for (i = 0; i < 16; i++) {
1495 T1 = *(uint32_t *)&env->fpr[rd++];
1496 helper_st_asi(asi & 0x8f, 4);
1510 T1 = *((uint32_t *)&FT0);
1513 T1 = *((int64_t *)&DT0);
1515 #if defined(CONFIG_USER_ONLY)
1521 helper_st_asi(asi, size);
1525 #endif /* TARGET_SPARC64 */
1527 #ifndef TARGET_SPARC64
1532 if (env->psret == 1)
1533 raise_exception(TT_ILL_INSN);
1536 cwp = (env->cwp + 1) & (NWINDOWS - 1);
1537 if (env->wim & (1 << cwp)) {
1538 raise_exception(TT_WIN_UNF);
1541 env->psrs = env->psrps;
1545 void helper_ldfsr(void)
1548 switch (env->fsr & FSR_RD_MASK) {
1549 case FSR_RD_NEAREST:
1550 rnd_mode = float_round_nearest_even;
1554 rnd_mode = float_round_to_zero;
1557 rnd_mode = float_round_up;
1560 rnd_mode = float_round_down;
1563 set_float_rounding_mode(rnd_mode, &env->fp_status);
1568 env->exception_index = EXCP_DEBUG;
1572 #ifndef TARGET_SPARC64
1575 if ((T0 & PSR_CWP) >= NWINDOWS)
1576 raise_exception(TT_ILL_INSN);
1593 static inline uint64_t *get_gregset(uint64_t pstate)
1608 static inline void change_pstate(uint64_t new_pstate)
1610 uint64_t pstate_regs, new_pstate_regs;
1611 uint64_t *src, *dst;
1613 pstate_regs = env->pstate & 0xc01;
1614 new_pstate_regs = new_pstate & 0xc01;
1615 if (new_pstate_regs != pstate_regs) {
1616 // Switch global register bank
1617 src = get_gregset(new_pstate_regs);
1618 dst = get_gregset(pstate_regs);
1619 memcpy32(dst, env->gregs);
1620 memcpy32(env->gregs, src);
1622 env->pstate = new_pstate;
1625 void do_wrpstate(void)
1627 change_pstate(T0 & 0xf3f);
1633 env->pc = env->tnpc[env->tl];
1634 env->npc = env->tnpc[env->tl] + 4;
1635 PUT_CCR(env, env->tstate[env->tl] >> 32);
1636 env->asi = (env->tstate[env->tl] >> 24) & 0xff;
1637 change_pstate((env->tstate[env->tl] >> 8) & 0xf3f);
1638 PUT_CWP64(env, env->tstate[env->tl] & 0xff);
1644 env->pc = env->tpc[env->tl];
1645 env->npc = env->tnpc[env->tl];
1646 PUT_CCR(env, env->tstate[env->tl] >> 32);
1647 env->asi = (env->tstate[env->tl] >> 24) & 0xff;
1648 change_pstate((env->tstate[env->tl] >> 8) & 0xf3f);
1649 PUT_CWP64(env, env->tstate[env->tl] & 0xff);
1653 void set_cwp(int new_cwp)
1655 /* put the modified wrap registers at their proper location */
1656 if (env->cwp == (NWINDOWS - 1))
1657 memcpy32(env->regbase, env->regbase + NWINDOWS * 16);
1659 /* put the wrap registers at their temporary location */
1660 if (new_cwp == (NWINDOWS - 1))
1661 memcpy32(env->regbase + NWINDOWS * 16, env->regbase);
1662 env->regwptr = env->regbase + (new_cwp * 16);
1663 REGWPTR = env->regwptr;
1666 void cpu_set_cwp(CPUState *env1, int new_cwp)
1668 CPUState *saved_env;
1670 target_ulong *saved_regwptr;
1675 saved_regwptr = REGWPTR;
1681 REGWPTR = saved_regwptr;
1685 #ifdef TARGET_SPARC64
1686 void do_interrupt(int intno)
1689 if (loglevel & CPU_LOG_INT) {
1691 fprintf(logfile, "%6d: v=%04x pc=%016" PRIx64 " npc=%016" PRIx64 " SP=%016" PRIx64 "\n",
1694 env->npc, env->regwptr[6]);
1695 cpu_dump_state(env, logfile, fprintf, 0);
1701 fprintf(logfile, " code=");
1702 ptr = (uint8_t *)env->pc;
1703 for(i = 0; i < 16; i++) {
1704 fprintf(logfile, " %02x", ldub(ptr + i));
1706 fprintf(logfile, "\n");
1712 #if !defined(CONFIG_USER_ONLY)
1713 if (env->tl == MAXTL) {
1714 cpu_abort(env, "Trap 0x%04x while trap level is MAXTL, Error state", env->exception_index);
1718 env->tstate[env->tl] = ((uint64_t)GET_CCR(env) << 32) | ((env->asi & 0xff) << 24) |
1719 ((env->pstate & 0xf3f) << 8) | GET_CWP64(env);
1720 env->tpc[env->tl] = env->pc;
1721 env->tnpc[env->tl] = env->npc;
1722 env->tt[env->tl] = intno;
1723 change_pstate(PS_PEF | PS_PRIV | PS_AG);
1725 if (intno == TT_CLRWIN)
1726 set_cwp((env->cwp - 1) & (NWINDOWS - 1));
1727 else if ((intno & 0x1c0) == TT_SPILL)
1728 set_cwp((env->cwp - env->cansave - 2) & (NWINDOWS - 1));
1729 else if ((intno & 0x1c0) == TT_FILL)
1730 set_cwp((env->cwp + 1) & (NWINDOWS - 1));
1731 env->tbr &= ~0x7fffULL;
1732 env->tbr |= ((env->tl > 1) ? 1 << 14 : 0) | (intno << 5);
1733 if (env->tl < MAXTL - 1) {
1736 env->pstate |= PS_RED;
1737 if (env->tl != MAXTL)
1741 env->npc = env->pc + 4;
1742 env->exception_index = 0;
1745 void do_interrupt(int intno)
1750 if (loglevel & CPU_LOG_INT) {
1752 fprintf(logfile, "%6d: v=%02x pc=%08x npc=%08x SP=%08x\n",
1755 env->npc, env->regwptr[6]);
1756 cpu_dump_state(env, logfile, fprintf, 0);
1762 fprintf(logfile, " code=");
1763 ptr = (uint8_t *)env->pc;
1764 for(i = 0; i < 16; i++) {
1765 fprintf(logfile, " %02x", ldub(ptr + i));
1767 fprintf(logfile, "\n");
1773 #if !defined(CONFIG_USER_ONLY)
1774 if (env->psret == 0) {
1775 cpu_abort(env, "Trap 0x%02x while interrupts disabled, Error state", env->exception_index);
1780 cwp = (env->cwp - 1) & (NWINDOWS - 1);
1782 env->regwptr[9] = env->pc;
1783 env->regwptr[10] = env->npc;
1784 env->psrps = env->psrs;
1786 env->tbr = (env->tbr & TBR_BASE_MASK) | (intno << 4);
1788 env->npc = env->pc + 4;
1789 env->exception_index = 0;
1793 #if !defined(CONFIG_USER_ONLY)
1795 static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
1798 #define MMUSUFFIX _mmu
1799 #define ALIGNED_ONLY
1801 # define GETPC() ((void*)((unsigned long)__builtin_return_address(0) & 0x7fffffffUL))
1803 # define GETPC() (__builtin_return_address(0))
1807 #include "softmmu_template.h"
1810 #include "softmmu_template.h"
1813 #include "softmmu_template.h"
1816 #include "softmmu_template.h"
1818 static void do_unaligned_access(target_ulong addr, int is_write, int is_user,
1821 #ifdef DEBUG_UNALIGNED
1822 printf("Unaligned access to 0x%x from 0x%x\n", addr, env->pc);
1824 raise_exception(TT_UNALIGNED);
1827 /* try to fill the TLB and return an exception if error. If retaddr is
1828 NULL, it means that the function was called in C code (i.e. not
1829 from generated code or from helper.c) */
1830 /* XXX: fix it to restore all registers */
1831 void tlb_fill(target_ulong addr, int is_write, int mmu_idx, void *retaddr)
1833 TranslationBlock *tb;
1836 CPUState *saved_env;
1838 /* XXX: hack to restore env in all cases, even if not called from
1841 env = cpu_single_env;
1843 ret = cpu_sparc_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
1846 /* now we have a real cpu fault */
1847 pc = (unsigned long)retaddr;
1848 tb = tb_find_pc(pc);
1850 /* the PC is inside the translated code. It means that we have
1851 a virtual CPU fault */
1852 cpu_restore_state(tb, env, pc, (void *)T2);
1862 #ifndef TARGET_SPARC64
1863 void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
1866 CPUState *saved_env;
1868 /* XXX: hack to restore env in all cases, even if not called from
1871 env = cpu_single_env;
1872 #ifdef DEBUG_UNASSIGNED
1874 printf("Unassigned mem %s access to " TARGET_FMT_plx " asi 0x%02x from "
1876 is_exec ? "exec" : is_write ? "write" : "read", addr, is_asi,
1879 printf("Unassigned mem %s access to " TARGET_FMT_plx " from "
1881 is_exec ? "exec" : is_write ? "write" : "read", addr, env->pc);
1883 if (env->mmuregs[3]) /* Fault status register */
1884 env->mmuregs[3] = 1; /* overflow (not read before another fault) */
1886 env->mmuregs[3] |= 1 << 16;
1888 env->mmuregs[3] |= 1 << 5;
1890 env->mmuregs[3] |= 1 << 6;
1892 env->mmuregs[3] |= 1 << 7;
1893 env->mmuregs[3] |= (5 << 2) | 2;
1894 env->mmuregs[4] = addr; /* Fault address register */
1895 if ((env->mmuregs[0] & MMU_E) && !(env->mmuregs[0] & MMU_NF)) {
1897 raise_exception(TT_CODE_ACCESS);
1899 raise_exception(TT_DATA_ACCESS);
1904 void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
1907 #ifdef DEBUG_UNASSIGNED
1908 CPUState *saved_env;
1910 /* XXX: hack to restore env in all cases, even if not called from
1913 env = cpu_single_env;
1914 printf("Unassigned mem access to " TARGET_FMT_plx " from " TARGET_FMT_lx "\n",
1919 raise_exception(TT_CODE_ACCESS);
1921 raise_exception(TT_DATA_ACCESS);