cpu.c 51.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
/*
 * Copyright (C) 1996-1998 by the Board of Trustees
 *    of Leland Stanford Junior University.
 * 
 * This file is part of the SimOS distribution. 
 * See LICENSE file for terms of the license. 
 *
 */

/****************************************************************
 * cpu.c
 * 
 * Main fetch/decode/execute loop and supporting routines. 
 * 
 * $Author: apatti $
 * $Date: 2003/06/24 17:44:47 $
 *****************************************************************/

#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <setjmp.h>
#include "mipsy_simos.h"
#include "addr_layout.h"
#include "simtypes.h"
#include "cpu.h"
#include "cpu_state.h"
#include "sim_error.h"
#include "cp0.h"
#include "fpu.h"
#include "eventcallback.h"
#include "cpu_stats.h"
#include "cpu_interface.h"
#include "hw_events.h"
#include "tcl_init.h"
#include "params.h"
#include "simutil.h"
#include "registry.h"
#include "print_insts.h"
#include "trace.h"
#include "memref.h"
#include "machine_params.h"
#include "opcodes.h"

#ifdef USE_FLASHLITE
#include "flash_interface.h"
#endif

#ifdef MIPSY_MXS
#  include <errno.h>
#  include <stdlib.h>
#  include "ms.h"
#  define IN_MXS(_P) ((_P)->inMXS)
#else 
#  define IN_MXS(_P) (0)
#endif


#ifdef SOLO
#  include "solo.h"
extern int sys_place_range_StallCPUS;
static int isSolo = 1;
#  ifdef FLASHPOINT
#     include "solo_page.h"
#     define BIG_ENDIAN 1
#     include "machine.h"
#     include "memspy_simulator.h"
#     include "mipsy_interface.h"
#  endif
#else
void SoloTclInit(Tcl_Interp *interp) {}
static int isSolo = 0;
#endif

#ifdef SOLO
#define HOST_DATA_ADDR(vAddr, paddr, cpu) (char *)(vAddr)
#else
#define HOST_DATA_ADDR(vAddr, paddr, cpu) \
  (PHYS_TO_MEMADDR(M_FROM_CPU(cpu),paddr)+(IS_REMAPPED_PADDR(paddr,cpu) ? remapVec->NodeAddr[cpu] : 0))
#endif

/*****************************************************************
 * Global CPU Variables 
 *****************************************************************/
CPUState   *PE;                        /* The big daddy of them all */
CPUState   *pePtr[MIPSY_MAX_CPUS];
jmp_buf     jmpEnv;                    /* Used to exit the main loop */
PA          LLAddrs[MIPSY_MAX_CPUS];   /* LL/SC sans caches */
int         numLLactive = 0;           /* LL/SC sans caches */
bool        mipsySyncWhenDone = FALSE;
bool        mipsySkipCaches   = FALSE;
bool        exitMipsy = FALSE;         /* Set this flag in the debugger to 
                                          leave mipsy */
static CPUState *P;                    /* P is always set to current cpu */

SimTime mipsyCurrentTime = 0;

extern int InterpretVerboseDebug(ClientData clientData, Tcl_Interp *interp,
                                 int argc, char *argv[]);
extern void MipsyInitStats(void);

#ifdef DEBUG_DATA_VERBOSE
unsigned long verboseDebugEnabled = 0;
#endif

/* Local CPU Functions */
static Result ReadInstruction(VA, Inst *);
static Result UncachedWrite(VA, PA, void *, RefSize, bool accelerated);
static Result UncachedRead(VA, PA, void *, RefSize);
static void   CPURun(void *);
static void   DoneRunning(void);

#ifdef USE_FLASHLITE
# ifndef FLASHPOINT
typedef uint64 LL;
# endif
extern LL FlashAdvanceTime(LL newInstTime);
/* Advance flashlite notion of time. */
#endif

#ifdef T5_MODEL
int enableVerilogNode;/* This may allow me to enable the verilog
                         node after mipsy initializes itself */
int T5NodeNum;/* Which node is the T5 acting like? */
#endif

#ifdef MIPSY_MXS
static void SwitchSimulators(int cpuNum,EventCallbackHdr *hdr, void *arg);
static EventCallbackHdr switchSimHdr;
int mxs_annotation = 0;
#endif
static uint mxsRunInterval, mipsyRunInterval;


/*****************************************************************
 * EVENT Support
 *****************************************************************/
#define INSTRUCTION_DONE_EVENT() \
 TraceInstruction(P, instr); \
 PRINT_INSTRUCTION(instr); \
 STATS_INC(cpuNum, numInstructions, 1); \
 if (STATS_VALUE(cpuNum, numInstructions) > STATS_VALUE(cpuNum, nextInstrSample)) { \
    INST_SAMPLE_EVENT(MipsyReadTime(cpuNum), cpuNum, P->PC); \
    STATS_INC(cpuNum, nextInstrSample, MS_SAMPLE_INSTR_INTERVAL); \
 }

/*****************************************************************
 * MEMORY ANNOTATION SUPPORT!
 *****************************************************************/
#define CHECK_LD_ANN(_vAddr, _pAddr) \
{  if (annLoads) {        \
      AnnPtr ptr = AnnFMLookup(_vAddr, ANNFM_LD_TYPE);\
      if (ptr)         \
         AnnExec(ptr); \
      ptr = AnnFMLookup(_pAddr, ANNFM_LD_TYPE);\
      if (ptr)         \
         AnnExec(ptr); \
   }                         \
}

#define CHECK_ST_ANN(_vAddr,_pAddr) \
{ if (annStores) {         \
      AnnPtr ptr = AnnFMLookup(_vAddr, ANNFM_ST_TYPE);\
      if (ptr)         \
         AnnExec(ptr); \
      ptr = AnnFMLookup(_pAddr, ANNFM_ST_TYPE);\
      if (ptr)         \
         AnnExec(ptr); \
 }                         \
}

/*****************************************************************
 * MipsyInit
 *
 *****************************************************************/
void
MipsyInit(void) 
{
   int i;

   if (!strcmp(CACHE_MODEL, "None")) {
      MipsyTurnOffCaches();
   }

   MipsyInitStats();
   MipsyInitFPU();
   PrintInstsInit();

   for (i = 0; i < TOTAL_CPUS; i++) {
#ifndef SOLO
      EnableInterruptCheck(&PE[i], CHECK_FOR_INTERRUPTS_INTERVAL);
#endif
      LLAddrs[i] = (PA) -1;
      PE[i].timerCycleCount = 0;
      PE[i].inMXS = 0;  
   }

#ifdef MIPSY_MXS
   {
      int i;
      for (i=0; i < TOTAL_CPUS; i++) {
         PE[i].st = (struct s_cpu_state *)
            ZMALLOC(sizeof (struct s_cpu_state),"s_cpu_state");
         if (PE[i].st == NULL) {
            perror ("Mipsy - malloc failed");
            ASSERT (0);
         }
         ms_st_init (PE[i].st);
         PE[i].st->mipsyPtr = (void *) (PE + i);
         PE[i].switchToMXS = PE[i].switchToMIPSY = 0;
      }
   }
#endif
}


/*****************************************************************
 * MipsyCycleCount
 * This is entered into the backdoor vector and is used by memstat 
 * to get the time at each entry into the log.
 *****************************************************************/
SimTime
MipsyCycleCount(int cpuNum) 
{
   return MipsyReadTime(cpuNum);
}

/*****************************************************************
 * MipsyCurrentCpuCycleCount
 *
 * This is entered into the detail vector and is used by the OS
 * as the on-chip cycle counter of the T5 COP0.
 * 
 *****************************************************************/
SimTime
MipsyCurrentCpuCycleCount(void)
{
   return MipsyReadTime(P->myNum);
}

/*****************************************************************
 * MipsyInstructionCount
 * 
 *****************************************************************/
SimTime
MipsyInstructionCount(int cpuNum)
{
   return STATS_VALUE(cpuNum, numInstructions);
}

/*****************************************************************
 * MipsyCurrentCpuNum
 * 
 * Anyone can call this from the cpu vector to determine the current
 * cpu.
 *****************************************************************/
int
MipsyCurrentCpuNum(void)
{
   return P->myNum;
}

/*****************************************************************
 * 
 *****************************************************************/
void
MipsyStall(int cpuNum)
{
  PE[cpuNum].cpuStatus = cpu_stalled;
  STATS_SET(cpuNum, stallStart, MipsyReadTime(cpuNum));
}

/*****************************************************************
 * MipsyUnstall
 *
 * Call this from any memory simulators when a processor is no
 * longer stalled. 
 * The initial if test ensures that the CPU can transition to the halted
 * state at any time without having to ensure that no memory
 * request is outstanding.
 *****************************************************************/
void
MipsyUnstall(int cpuNum)
{
   if (PE[cpuNum].cpuStatus == cpu_halted) {
      return;
   }

   if (PE[cpuNum].cpuStatus == cpu_stalled) {
      PE[cpuNum].cpuStatus = cpu_running;
      STATS_ADD_INTERVAL(cpuNum, stallTime, stallStart);
   } else if (PE[cpuNum].cpuStatus == cpu_libc_blocked) {
      return;
   } else if (PE[cpuNum].cpuStatus == cpu_bdoor_stalled) {
      return;
   } else {
      /* Was probably stalled on an uncached_op */
      PE[cpuNum].cpuStatus = cpu_running;
   }
}

/* *****************************************************************
 * Mipsy LL/SC support routines
 * *****************************************************************/

void MipsyClearLockFlag(int cpuNum)
{
   PE[cpuNum].LLbit  = 0;
}

bool MipsyGetLockFlag(int cpuNum)
{
   return PE[cpuNum].LLbit;
}

PA MipsyGetLockAddr(int cpuNum)
{
   return PE[cpuNum].LLAddr;
}

CPUStatus MipsyCPUStatus(int cpuNum)
{
   return PE[cpuNum].cpuStatus;   
}

/*****************************************************************
 * MipsyFinishMemRequests
 *
 * Spin advancing time until all cpus are unstalled.
 *****************************************************************/
void 
MipsyFinishMemRequests(void) 
{
   CPUState *lastP; 
   CPUState *firstP;
   bool somebodyStalled = FALSE;
   int i;

   for (i = 0; i < TOTAL_CPUS; i++) {
      if (PE[i].cpuStatus != cpu_running) {
         somebodyStalled = TRUE;
      }
   }

   firstP = &PE[0];
   lastP  = &PE[TOTAL_CPUS-1];
   P      = lastP;

   while (somebodyStalled) {
      P++;
      if (P > lastP) {
         P = firstP;
         mipsyCurrentTime++;
         EventPollSingleQueue(MipsyReadTime(0));
      } /* Things to do each wrap-around */
      somebodyStalled = FALSE;
      for (i = 0; i < TOTAL_CPUS; i++) {
         if (PE[i].cpuStatus != cpu_running &&
             PE[i].cpuStatus != cpu_idle    && /* idle if it was taken out */
             PE[i].cpuStatus != cpu_halted) {  /* was halted after fault injection */
            somebodyStalled = TRUE;
         }
      }
   }
}

/*****************************************************************
 * PrepareToSwitch
 *
 * This just checks if the current PC value has an annotation. 
 * This is needed in the cpu switching code so that we get a chance 
 * to advance the PC before moving to the next simulator.
 *****************************************************************/
static void
PrepareToSwitch(void)
{
   int cpu;
   
   for (cpu=0; cpu < TOTAL_CPUS; cpu++) {
      CPUState *P = &PE[cpu];
      if (AnnFMLookup(P->PC, ANNFM_PC_TYPE)) {
         CPUWarning("MIPSY: Switching on PC annotation -> advancing PC\n");
         /* advance PC only if current instruction is not a branch thats
            going to be taken */
         if (P->branchStatus == BranchStatus_taken) {
            CPUWarning("CPU %d on branch 0x%x (postPC), keeping it there\n",
                       cpu, P->PC);
         } else {
            P->PC = P->nPC;
            P->nPC += INST_SIZE;
         }
      } else if (AnnFMLookup(P->PC, ANNFM_PRE_PC_TYPE)) {
         if (P->nPC != (PE->PC + INST_SIZE)) {
            CPUWarning("CPU %d on delay slot 0x%x (prePC), backing up to 0x%x\n",
                       cpu, P->PC, P->PC - INST_SIZE);
            P->PC -= INST_SIZE;
         }
      }
   }
}

/*****************************************************************
 * MipsyExit
 * 
 *****************************************************************/
void 
MipsyExit(CPUType exitTo)
{
   PrepareToSwitch();
#ifndef SOLO
   /* Before we exit mipsy, we need to make sure that all outstanding
      memory references have completed. This just advances time until
      all outstanding memory references are complete. This is
      determined to be done when all processors have moved 
    */ 
   MipsySetExitSimulator(exitTo);
   MipsyFinishMemRequests();
#endif
   PrintRealTime("Simulation End");
   /* This kicks us out of the main loop */
   longjmp(jmpEnv, 1);
}

/*****************************************************************
 * ReadInstruction()
 * 
 * Called from the main execution loop each time an instruction 
 * is needed. The first argument is the PC value to read from. 
 * "backdoor" is a way of avoiding going through the cache
 * and memory system to directly access something in our real-life
 * address space (mipsy or simos). This procedure better be as fast as
 * possible!!! The Result returned is just a way of telling the
 * processor whether it has to stall or not (i.e. if there was an icache
 * miss. 
 *****************************************************************/
static Result
ReadInstruction(VA vAddr, Inst *inst)        
{
   Result ret;
   PA pAddr;
   int64 bdoorRetval = 0; 
   uint tlbFlavor = TLB_IFETCH|TLB_READING;
   void *bdoorAddr = 0;

   if (TranslateVirtual(P, vAddr, &pAddr, &tlbFlavor, &bdoorAddr) != SUCCESS)
      return FAILURE;

   if (!tlbFlavor) {
      P->pcVPNcache = PAGE_NUMBER(vAddr);
      P->pcPaddrcache = FORM_ADDR(PAGE_NUMBER(pAddr), 0);

      if (SKIP_CACHES(P)) {
         *inst =*(Inst *)HOST_DATA_ADDR(vAddr, pAddr, P->myNum);
         return SUCCESS;
      }

      ret =  MemRefReadInst(P->myNum, vAddr, pAddr, inst);
if (getenv("SIMOS_TRACEPC")) fprintf(stderr, "fetch 0x%08x   0x%08x\r\n", (int)vAddr,BE2HO_4(*inst));

      if (ret != SUCCESS) {
         if ((ret == STALL) || (ret == FAILURE)) {
            P->stalledInst = 0;
            P->cpuStatus = cpu_stalled;
         } else if (ret == BUSERROR) {
            RECORD_EXCEPTION(P, EXC_IBE, E_VEC, vAddr, 
                             P->CP0[C0_TLBHI], P->CP0[C0_CTXT], P->CP0[C0_XCTXT]);
         } else {
            CPUWarning("Unknown return value (%d) from ReadICache\n", ret);
         }
      }

      /*
       * The processor can have been idled as a consequence of an annotations
       * For now, we can implement this by checking for the cpuStatus
       */
      if (P->cpuStatus == cpu_idle) {
         P->stalledInst = 0;
         ret = FAILURE;
      } 
      return ret;
   } else {
#ifndef SOLO
      /* this might be one of three things:
       *    the boot prom (provided by flashlite)
       *    the prom slave loop (SimOS magic)
       *    a backdoor call
       */
      if (tlbFlavor & TLB_UNCACHED) {
         /* executing instructions from the prom provided by flashlite
          * or simmagic
          */
         ret =  UncachedRead(vAddr, pAddr, inst, WORD_SZ);
if (getenv("SIMOS_TRACEPC")) fprintf(stderr, "ufetch 0x%08x   0x%08x\r\n", (int)vAddr,BE2HO_4(*inst));
	return ret;
      } 

      /* A backdoor  call. Execute it on the real CPU. This */
      /* assumes that all backdoor calls take less than 4 */
      /* arguments. */

      ret = MemRefSync(P->myNum);
      if (ret != SUCCESS) {
         /* Make sure all cached and uncached writes are flushed
          * out of buffers before doing a backdoor call. This 
          * is needed for calls that start I/O.
          */
         return FAILURE;
      }

      SIM_DEBUG(('b', "MIPSY:CPU %d: Backdoor call at PC %#x PA %#x RA %#x\n", 
                 P->myNum, P->PC, pAddr, P->R[31]));

      if (tlbFlavor == TLB_BDOOR_DATA) {
         *inst = *(Inst *)bdoorAddr;
      } else if (tlbFlavor == TLB_BDOOR_FUNC) {
         Result returnFlag;
         
         returnFlag = ((Result (*)(int, uint, uint, unsigned char *))bdoorAddr)
            (P->myNum, vAddr, BDOOR_LOAD_WORD, (unsigned char *)inst);
if (getenv("SIMOS_TRACEPC")) fprintf(stderr, "bfetch 0x%08x   0x%08x\r\n", (int)vAddr,BE2HO_4(*inst));

         if (returnFlag == SUCCESS) {
            return SUCCESS;
         } else if (returnFlag == BUSERROR) {
            RECORD_EXCEPTION(P, EXC_IBE, E_VEC, vAddr, 
                             P->CP0[C0_TLBHI], P->CP0[C0_CTXT], P->CP0[C0_XCTXT]);
            return BUSERROR;
         } else {
            CPUError("Unknown result type: 0x%x\n", returnFlag);
         }
      } else {
         ASSERT(tlbFlavor == TLB_BDOOR_FUNC_COMPAT);
         bdoorRetval = ((int64 (*)(int,int,int,int))
                        (bdoorAddr))(P->R[4],P->R[5],P->R[6],P->R[7]);
         P->R[2] = bdoorRetval >> 32;
         P->R[3] = bdoorRetval & 0xffffffff;
         *inst = 0; /* NOP */
         P->nPC = P->R[31];
      }
      return SUCCESS;
#endif
#ifdef SOLO
      /* Solo Mipsy has back door calls that need to be handled */
      /* We may need to know our cpu number at this time */
      soloCPUNum = P->myNum;
      bdoorRetval = ((int64 (*)(int,int,int,int))P->PC)
         (P->R[4],P->R[5],P->R[6],P->R[7]);
      P->R[2] = bdoorRetval >> 32;
      P->R[3] = bdoorRetval & 0xffffffff;

      P->nPC = P->R[31];
      *inst = 0; /* NOP */
      STATS_INC(P->myNum, numBdoorInsts, 1);
      return SUCCESS;
#endif
   }
}

/****************************************************************
 * MipsyReadMem
 * 
 * Common routine used by the CPU to read memory.
 *****************************************************************/
Result
MipsyReadMem(VA vAddr, void *data, RefSize size, RefFlavor flavor)
{
   Result ret;
   PA pAddr;
   uint tlbFlavor = TLB_READING;
   void *bdoorAddr = 0;
   
   ret = TranslateVirtual(P, vAddr, &pAddr, &tlbFlavor, &bdoorAddr);
   
   if (ret == SUCCESS) { 
      if (!tlbFlavor) {

         if (flavor & LL_FLAVOR) { 
            P->LLAddr =  pAddr & ~(SCACHE_LINE_SIZE-1);
            if (UNCACHED_LL_SC) {
               LLAddrs[P->myNum] = pAddr & ~(SCACHE_LINE_SIZE-1);
            }
         }
 
         if (SKIP_CACHES(P)) {
            switch (size) {
            case BYTE_SZ:
               *(byte *)data = *(byte *)HOST_DATA_ADDR(vAddr, pAddr, P->myNum);
               break;
            case HALF_SZ:
               *(short *)data = BE2HO_2(*(short *)HOST_DATA_ADDR(vAddr, pAddr, P->myNum));
               break;
            case WORD_SZ:
               *(uint *)data = BE2HO_4(*(uint *)HOST_DATA_ADDR(vAddr, pAddr, P->myNum));
               break;
            case DOUBLE_SZ:
               *(int64 *)data =BE2HO_8(*(int64 *)HOST_DATA_ADDR(vAddr, pAddr, P->myNum));
               break;
            default:
               CPUError("Bad size in MipsyReadMem\n");
               ASSERT(0);
            }
            return SUCCESS;
         }
         
         /* Go through the cache interface */
         switch (size) {
         case BYTE_SZ:
            ret = MemRefReadData(P->myNum, vAddr, pAddr, data, BYTE_SZ, NO_FLAVOR);
            break;
         case HALF_SZ:
            ret = MemRefReadData(P->myNum, vAddr, pAddr, data, HALF_SZ, NO_FLAVOR);
            *(short *)data = BE2HO_2(*(short *)data);
            break;
         case WORD_SZ:
            ret = MemRefReadData(P->myNum, vAddr, pAddr, data, WORD_SZ, flavor);
            *(uint *)data = BE2HO_4(*(uint *)data);
            break;
         case DOUBLE_SZ:
            ret = MemRefReadData(P->myNum, vAddr, pAddr, data, DOUBLE_SZ, flavor);
            *(int64 *)data = BE2HO_8(*(int64 *)data);
            break;
         default:
            CPUError("Bad size in MipsyReadMem\n");
            ASSERT(0);
         }

         if (ret != SUCCESS) { 
oops:
            if ((ret == STALL) || (ret == FAILURE)) {
               P->cpuStatus = cpu_stalled;
            } else if (ret == BUSERROR) {
               RECORD_EXCEPTION(P,EXC_DBE, E_VEC, vAddr, 
                                P->CP0[C0_TLBHI], P->CP0[C0_CTXT], P->CP0[C0_XCTXT]);
               return ret;
            } else {
               CPUWarning("Bad return value (%d) from MemRefReadData\n", ret);
            }
         } else {
            /* We hit in the DCACHE */
            CHECK_LD_ANN(vAddr, pAddr);
            STATS_INC(P->myNum, dReads, 1);
         }
      } else {
         if (tlbFlavor & TLB_UNCACHED) {
            ret = UncachedRead(vAddr, pAddr, data, size);
	    if (ret != SUCCESS) {
		P->cpuStatus = cpu_running;
		goto oops;
	    }
            switch (size) {
            case BYTE_SZ:
               *(byte *)data = *(byte *)data;
               break;
            case HALF_SZ:
               *(short *)data = BE2HO_2(*(short *)data);
               break;

            case WORD_SZ:
               *(uint *)data = BE2HO_4(*(uint *)data);
               break;
            case DOUBLE_SZ:
               *(uint64 *)data = BE2HO_8(*(uint64 *)data);
               break;
            default:
               CPUError("Bad size in MipsyReadMem\n");
               ASSERT(0);
            }
#ifdef HWBCOPY
            if ((size == DOUBLE_SZ) && (tlbFlavor == TLB_MSG_SPACE)) ret = SUCCESS;
#endif
         } else if (tlbFlavor == TLB_BDOOR_DATA) {

            STATS_INC(P->myNum, numBdoorInsts, 1);

            switch (size) {
            case BYTE_SZ:
               *(byte *)data = *(byte *)bdoorAddr;
               break;
            case HALF_SZ:
               *(short *)data = *(short *)bdoorAddr;
               break;

            case WORD_SZ:
               *(uint *)data = *(uint *)pAddr;
               break;
            case DOUBLE_SZ:
               *(uint64 *)data = *(uint64 *)bdoorAddr;
               break;
            default:
               CPUError("Bad size in MipsyReadMem\n");
               ASSERT(0);
            }
            return SUCCESS;

         } else {
            Result returnFlag;
            
            ASSERT(tlbFlavor == TLB_BDOOR_FUNC);

            switch (size) {
            case BYTE_SZ:
               returnFlag = ((Result (*)(int, uint, uint, unsigned char *))bdoorAddr)
                  (P->myNum, vAddr, BDOOR_LOAD_BYTE, data);
               break;
            case HALF_SZ:
               returnFlag = ((Result (*)(int, uint, uint, short *))bdoorAddr)
                  (P->myNum, vAddr, BDOOR_LOAD_HALF, data);
               break;
            case WORD_SZ:
               returnFlag = ((Result (*)(int, uint, uint, int*))bdoorAddr)
                  (P->myNum, vAddr, BDOOR_LOAD_WORD, data);
               break;
            case DOUBLE_SZ:
               returnFlag = ((Result (*)(int, uint, uint, uint64 *))bdoorAddr)
                  (P->myNum, vAddr, BDOOR_LOAD_DOUBLE, data);
               break;
            default:
               CPUError("Bad size in MipsyReadMem\n");
               ASSERT(0);
            }
            
            if (returnFlag == SUCCESS) {
               return SUCCESS;
            } else if (returnFlag == BUSERROR) {
               RECORD_EXCEPTION(P, EXC_DBE, E_VEC, vAddr, 
                                P->CP0[C0_TLBHI], P->CP0[C0_CTXT], P->CP0[C0_XCTXT]);
               return BUSERROR;
            } else {
               CPUError("Unknown result type: 0x%x\n", returnFlag);
            }
         }
      }
   }
   return ret;
}

/* Used by solo */
Result
SoloReadByte(int cpuNum, VA vAddr, char *data)
{
   return MipsyReadMem(vAddr, (void *)data, BYTE_SZ, NO_FLAVOR);
}


/****************************************************************
 * MipsyWriteMem
 * 
 * Common routine used by the CPU to write memory.
 *****************************************************************/
Result
MipsyWriteMem(VA vAddr, void *data, RefSize size, RefFlavor flavor)
{
   Result ret;
   PA pAddr;
   uint tlbFlavor = TLB_WRITING;
   void *bdoorAddr = 0;

   if (TranslateVirtual(P, vAddr, &pAddr, &tlbFlavor, &bdoorAddr) != SUCCESS) {
      return FAILURE;
   }

   if (flavor == SC_FLAVOR) {
      if (!P->LLbit)
         return SCFAILURE;
   }

   if (!tlbFlavor) {
      /* Special code to handle LL/SC when caches are inactive */
      if (UNCACHED_LL_SC) {
         if (numLLactive) {
            int cpu;
            PA matchAddr = pAddr & ~(SCACHE_LINE_SIZE-1);
            if (P->LLbit) {
               P->LLbit = 0;
               LLAddrs[P->myNum] = (PA) -1;
               numLLactive--;
            }
            for (cpu = 0; cpu < TOTAL_CPUS; cpu++) {
               if (LLAddrs[cpu] == matchAddr) {
                  PE[cpu].LLbit = 0;
                  LLAddrs[cpu] = (PA) -1;
                  numLLactive--;
               }
            }
         }
      }

      if (SKIP_CACHES(P)) {
         switch (size) {
         case BYTE_SZ:
            *(byte *)HOST_DATA_ADDR(vAddr, pAddr, P->myNum)   = *(byte *)data;
            break;
         case HALF_SZ:
            *(short *)HOST_DATA_ADDR(vAddr, pAddr, P->myNum)  = HO2BE_2(*(short *)data);
            break;
         case WORD_SZ:
            *(uint *)HOST_DATA_ADDR(vAddr, pAddr, P->myNum)   = HO2BE_4(*(uint *)data);
            break;
         case DOUBLE_SZ:
            *(uint64 *)HOST_DATA_ADDR(vAddr, pAddr, P->myNum) = HO2BE_8(*(uint64 *)data);
            break;
         default:
            ASSERT(0);
         }
         return SUCCESS;
      }
      
      /* Normal path through the cache interface. */
      switch (size) {
      case BYTE_SZ:
         ret = MemRefWriteData(P->myNum, vAddr, pAddr, 
                               *(byte *)data, BYTE_SZ, NO_FLAVOR);
         break;
      case HALF_SZ:
         ret =  MemRefWriteData(P->myNum, vAddr, pAddr,
                                HO2BE_2(*(short *)data), HALF_SZ, NO_FLAVOR);
         break;
      case WORD_SZ:
         ret = MemRefWriteData(P->myNum, vAddr, pAddr, 
                               HO2BE_4(*(uint *)data), WORD_SZ, flavor); 
         break;
      case DOUBLE_SZ:
         ret =  MemRefWriteData(P->myNum, vAddr, pAddr, 
                                HO2BE_8(*(uint64 *)data), DOUBLE_SZ, flavor);
         break;
      default:
         ASSERT(0);
      }

      if (ret != SUCCESS) { 
         if ((ret == STALL) || (ret == FAILURE)) {
            P->cpuStatus = cpu_stalled;
         } else if (ret == BUSERROR) {
            RECORD_EXCEPTION(P, EXC_DBE, E_VEC, vAddr, 
                             P->CP0[C0_TLBHI], P->CP0[C0_CTXT], P->CP0[C0_XCTXT]) ;
            return ret;
         } else if (ret == SCFAILURE) {
            /* This is OK, isn't it? */
         } else { 
            CPUWarning("Bad return value (%d) from MemRefWriteData\n", ret);
         }
      } else {
         CHECK_ST_ANN(vAddr, pAddr);
         STATS_INC(P->myNum, dWrites, 1);
      }
      return ret;
   }

   if (tlbFlavor & TLB_UNCACHED) {

      switch (size) {
      case BYTE_SZ:
	 return UncachedWrite(vAddr, pAddr, data, size, (bool)tlbFlavor & TLB_ACCELERATED);
      case HALF_SZ: {
	 short tmp = HO2BE_2(*(short*)data);
	 return UncachedWrite(vAddr, pAddr, &tmp, size, (bool)tlbFlavor & TLB_ACCELERATED);
	 }
      case WORD_SZ: {
	 uint tmp = HO2BE_4(*(uint*)data);
	 return UncachedWrite(vAddr, pAddr, &tmp, size, (bool)tlbFlavor & TLB_ACCELERATED);
	 }
      case DOUBLE_SZ: {
	 uint64 tmp = HO2BE_8(*(uint64*)data);
	 return UncachedWrite(vAddr, pAddr, &tmp, size, (bool)tlbFlavor & TLB_ACCELERATED);
	 }
      default:
         ASSERT(0);
      }
#ifdef HWBCOPY
   } else if (tlbFlavor & TLB_MSG_SPACE) {
      return SUCCESS;
#endif
   } else if (tlbFlavor == TLB_BDOOR_DATA) { 
#ifndef SOLO
      ASSERT(0);
#endif
         switch (size) {
         case BYTE_SZ:
            *(byte *)bdoorAddr = *(byte *)data;
            break;
         case HALF_SZ:
            *(short *)bdoorAddr = *(short *)data;
            break;
         case WORD_SZ:
            *(uint *)pAddr = *(uint *)data;
            break;
         case DOUBLE_SZ:
#ifdef SOLO
            *(uint64 *)pAddr = *(uint64 *)data;
#else
            *(uint64 *)bdoorAddr = *(uint64 *)data;
#endif
            break;
         default:
            ASSERT(0);
         }
      return SUCCESS;
   } else {
      Result returnFlag;
      
      ASSERT(tlbFlavor == TLB_BDOOR_FUNC);

      switch (size) {
      case BYTE_SZ:
         returnFlag = ((Result (*)(int, uint, uint, unsigned char*))bdoorAddr)
            (P->myNum, vAddr, BDOOR_STORE_BYTE, (byte *)data);
         break;
      case HALF_SZ:
         returnFlag = ((Result (*)(int, uint, uint, short*))bdoorAddr)
            (P->myNum, vAddr, BDOOR_STORE_HALF, (short *)data);
         break;
      case WORD_SZ:
         returnFlag = ((Result (*)(int, uint, uint, uint*))bdoorAddr)
            (P->myNum, vAddr, BDOOR_STORE_WORD, (uint *)data);
         break;
      case DOUBLE_SZ:
         returnFlag = ((Result (*)(int, uint, uint, uint64*))bdoorAddr)
            (P->myNum, vAddr, BDOOR_STORE_DOUBLE, (uint64 *)data);
         break;
      default:
         ASSERT(0);
      }

      if (returnFlag == SUCCESS) {
         return SUCCESS;
      } else if (returnFlag == BUSERROR) {
         ASSERT(0);
         RECORD_EXCEPTION(P, EXC_DBE, E_VEC, vAddr, 
                          P->CP0[C0_TLBHI], P->CP0[C0_CTXT], P->CP0[C0_XCTXT]);
         return BUSERROR;
      } else {
         CPUError("Unknown result type: 0x%x\n", returnFlag);
      }
   }
   return SUCCESS;
}

Result
SoloWriteByte(int cpuNum, VA vAddr, char data)
{
   return MipsyWriteMem(vAddr, &data, BYTE_SZ, NO_FLAVOR);
}

/*****************************************************************
 * UncachedWrite. 
 *****************************************************************/
static Result
UncachedWrite(VA vAddr, PA pAddr, void *data, RefSize size, bool accelerated)
{
   Result ret;
#ifdef FLASHPOINT
   uint flavor;
   FLASHAddress addr;
   LL storeData;

#endif

   ASSERT ((size == BYTE_SZ) || (size == HALF_SZ) 
           || (size == WORD_SZ) || (size == DOUBLE_SZ));

   /* need to temporarily set cpu_stalled_uncached before MemRefWrite
      because it might block */
   P->cpuStatus = cpu_stalled_uncached;
   ret = MemRefWriteUncached(P->myNum, vAddr, pAddr, data, size, accelerated);

   if (ret != SUCCESS) {
      P->cpuStatus = cpu_stalled_uncached;
   } else {
      P->cpuStatus = cpu_running;

#ifdef FLASHPOINT
      flavor = TLB_UNCACHED;
      addr.ll = SoloV_to_P(P->myNum, vAddr, P->myNum, 0, &flavor); 
      storeData = *((LL *) data);
      
      CPUPrint("Mipsy: cpu %d, Uncached store to 0x%llx, data is %lld\n", 
               P->myNum, addr.ll, storeData);
      
      if(addr.fa.space == PERF) {
         CPUPrint("Mipsy: Perf store!\n");
         if((addr.fa.offset & PERF_FP_MASK) == PERF_FP_ENTER_PROC) {
            Memspy_ProcedureNum newProcedure =
               Memspy_Sim_GetProcedureNum(GetSimulatorMemspyGlobals(),
                                          P->myNum);
            newProcedure += storeData;
            CPUPrint("Mispy: Enter proc, delta = %lld\n", storeData);
            Memspy_Sim_SetProcedureNum(GetSimulatorMemspyGlobals(),
                                       P->myNum, 
                                       newProcedure);

            FP_BinStatsProcedureDelta(P->myNum, storeData);
         }
         if((addr.fa.offset & PERF_FP_MASK) == PERF_FP_NEW_BIN) {
            Memspy_VA app_addr = (Memspy_VA) 
               SoloGetMemoryAddr(storeData & 0xffffffffffLL);
            unsigned length = (unsigned) (storeData >> 52);

            Memspy_Sim_MapPage(GetSimulatorMemspyGlobals(),
                               app_addr, length);
            CPUPrint("Mipsy: Perf Mapping page containing vaddr %lx, paddr %llx to bin %lld, length %u\n", app_addr,
                     storeData & 0xffffffffffLL,
                     (storeData>>40)&0xfff, length);

         }
      }
      
#endif
   }

   CHECK_ST_ANN(vAddr,pAddr);
   STATS_INC(P->myNum, dWrites, 1);

   TraceUncachedDataRef(P, vAddr, pAddr);

   return ret;
}

/****************************************************************
 * UncachedRead
 *****************************************************************/
static Result
UncachedRead(VA vAddr, PA pAddr, void *data, RefSize size)
{
   Result ret;

   ASSERT ((size == BYTE_SZ) || (size == HALF_SZ) 
           || (size == WORD_SZ) || (size == DOUBLE_SZ));

   /* need to temporarily set cpu_stalled_uncached before MemRefRead
      because it might block */
   P->cpuStatus = cpu_stalled_uncached;
   ret = MemRefReadUncached(P->myNum, vAddr, pAddr, data, size);

   if (ret != SUCCESS) {
      P->cpuStatus = cpu_stalled_uncached;
   } else {
      P->cpuStatus = cpu_running;
   }
   CHECK_LD_ANN(vAddr,pAddr);
   STATS_INC(P->myNum, dReads, 1);

   TraceUncachedDataRef(P, vAddr, pAddr);

   return ret;
}

/*****************************************************************
 *
 ****************************************************************/
void
MipsyNakUncachedOp(int cpuNum)
{
   CPUState *P = &PE[cpuNum];
   StatusReg statusReg;

   MipsyUnstall(cpuNum);
   statusReg.ts_data = P->CP0[C0_SR];
   if (statusReg.s32.ts_erl) {
      if (P->nPC == CACHE_ERR_VEC_BEV0) {
      /* HACK
         assume CPU was waiting for this uncached op to finish before
         starting the cache error handler, so make it take the error now.
         Undo effects of previous call to MipsyCacheError and call it again
         (this time with cpu_running) */
         CPUWarning("Uncached op nak'd on cpu %d while waiting for cache error\n",
                    cpuNum);
         P->nPC = P->CP0[C0_ERROR_EPC];
         MipsyCacheError(cpuNum, 1);
      }
   }
}

/*****************************************************************
 *
 *****************************************************************/
void
MipsyErrUncachedOp(int cpuNum, uint addr)
{
   CPUState *P = &PE[cpuNum];
   StatusReg statusReg;

   MipsyTakeBusError(cpuNum, FALSE, addr);

   /* dont need MipsyUnstall(cpuNum) because TakeBusError already does that */
   statusReg.ts_data = P->CP0[C0_SR];
   if (statusReg.s32.ts_erl) {
      if (P->nPC == CACHE_ERR_VEC_BEV0) {
      /* HACK
         assume CPU was waiting for this uncached op to finish before
         starting the cache error handler, so make it take the error now.
         Undo effects of previous call to MipsyCacheError and call it again
         (this time with cpu_running) */
         CPUWarning("Uncached op err'd on cpu %d while waiting for cache error\n",
                    cpuNum);
         P->nPC = P->CP0[C0_ERROR_EPC];
         MipsyCacheError(cpuNum, 1);
      }
   }
}

void
MipsyReissueUncachedOp(int cpuNum)
{
   MipsyUnstall(cpuNum);
}

void 
MipsyTurnOnCaches(void)
{
   mipsySkipCaches = FALSE;
}

void
MipsyTurnOffCaches(void)
{
   CPUWarning("MIPSY: Turning off caches without flushing.\n");
   mipsySkipCaches = TRUE;
}

bool
MipsyCachesAreOn(void)
{
   return !mipsySkipCaches;
}

/*****************************************************************
 * Bus errors that aren't determined until you are in the memory 
 * system will come back through this path
 *****************************************************************/
void
MipsyTakeBusError(int cpuNum, bool isIRef, VA vAddr)
{
   CPUState *P = &PE[cpuNum];
   PA pAddr;

   ASSERT((P->cpuStatus == cpu_stalled) || (P->cpuStatus == cpu_stalled_uncached));
   P->cpuStatus = cpu_running;

   if (TranslateVirtualNoSideeffect(P, vAddr, &pAddr) != SUCCESS) {
      CPUWarning("Couldn't translate virtual in MipsyTakeBusError\n");
      ASSERT(0);
   }

   MemRefRemoveReq(cpuNum, pAddr, SCACHE_LINE_SIZE);

   if (isIRef) {
      RECORD_EXCEPTION(P, EXC_IBE, E_VEC, vAddr, 
                       P->CP0[C0_TLBHI], P->CP0[C0_CTXT], P->CP0[C0_XCTXT]);
   } else {
      RECORD_EXCEPTION(P, EXC_DBE, E_VEC, vAddr, 
                       P->CP0[C0_TLBHI], P->CP0[C0_CTXT], P->CP0[C0_XCTXT]);
   }
}

/*****************************************************************
 * MipsyRun
 * 
 * This is the interface into starting the fetch-decode-execute
 * loop. 
 *****************************************************************/
void 
MipsyRun(int swtch)
{
   int i;
   PrintRealTime("Simulation Start");

   MipsyInitFPU();
#ifdef MIPSY_MXS
   if ((mxsRunInterval > 0) && (mipsyRunInterval > 0)) {
       /* Run first in mipsy and then switch */
      EventDoCallback(0,SwitchSimulators, &switchSimHdr, 0, mipsyRunInterval);
   } else {
      int cpu;

       /* If we aren't going to switch back and forth, start in MXS */
      for (cpu=0; cpu < TOTAL_CPUS; cpu++)
         PE[cpu].inMXS = 1;
   }
#endif 

#ifdef T5_MODEL
   {
      int usingT5 = 0;
      ParamLookup(&usingT5, "MEMSYS.FLASH.UseNewtT5", PARAM_INT);
      if (usingT5) {
        ParamLookup(&T5NodeNum, "MEMSYS.FLASH.VerilogNodeNum", PARAM_INT);
      } else {
         T5NodeNum = -1;         /* Won't match any node numbers */
      }
   }
#endif      

   for (i=0; i < TOTAL_CPUS; i++) {
      /* Optimization to avoid directly indexing the PE array */
      pePtr[i] = &PE[i];
   }

   AnnFMInit(128);
   if (!swtch) {
      /* this is not a switch from another simulator */
      AnnCommonSetup();
      for (i=0; i < TOTAL_CPUS; i++) {
         P = &PE[i];
         AnnExec(AnnFind("simos", "enter"));
      }
   }

   /* Now jump into the main loop.When it is time for mipsy to quit */
   /* simulating, a longjmp will switch control here and then to */
   /* DoneRunning(). */ 

   if (setjmp(jmpEnv) != 0) {
      DoneRunning();
   } else {
      TraceInit();
      CPURun(NULL);
   }

#ifdef MIPSY_MXS
   if ((mxsRunInterval > 0) && (mipsyRunInterval > 0)) {
       EventCallbackRemove(&switchSimHdr);  
   }
#endif /* MIPSY_MXS */

   MemRefExit();

#ifdef DATA_HANDLING
#ifndef SOLO
   /* flush cache. This is inappropriate for SOLO, not to mention it crashes*/
   {
      int cpu;
      for (cpu = 0; cpu < TOTAL_CPUS; cpu++) {
         FlushEntireCache(cpu, 0);
      }
   }
#endif
#endif
}

/****************************************************************
 * CPURun
 *
 * This is the fetch-decode-execute loop. Loop exit points include
 * exceptions and calls to the memory system to fetch instructions and
 * data. The premise behind exceptions is that the PC will not be 
 * incremented so that the exception handler can set the correct PC.
 * It takes the void * parameter to match the function type that
 * sproc() expects.
 ****************************************************************/
static void 
CPURun(void *Nada)
{
   register  CPUState *lastP; 
   register  CPUState *firstP;
   int i;

   firstP = &PE[0];
   lastP  = &PE[TOTAL_CPUS-1];
   P      = lastP;

   for (i=0; i < TOTAL_CPUS; i++) {
      P = &PE[i];
      pePtr[i] = &PE[i];
   }

   while (1) {
      Inst instr;
      register uint cpuNum;
      register uint op; 

      P++;

      if (P > lastP) {
         P = firstP;
         mipsyCurrentTime++; 
         EventPollSingleQueue(MipsyReadTime(0));
      } 
      
      if (P->cpuStatus != cpu_running) {
         /* 
          * A cpu can be halted or stalled. In solo mipsy, all but the 
          * first cpu is halted until the CREATE macro is encountered. 
          */ 
         continue;
      }

#ifdef USE_FLASHLITE
#ifdef SOLO
      if (sys_place_range_StallCPUS) {
         continue;
      }
#endif

#ifdef T5_MODEL
      /* 
       * If we're using the T5 model on this node to model the processor,
       * we'll need to make sure this node just idles 
       */
      if (T5NodeNum == P->myNum) {
         if (T5NodeNum == 0) {
            /* 
             * Special case for verilog on node 0: need to make sure 
             * Flashlite's threads can run as needed 
             */
            (void)  FlashAdvanceTime(MipsyReadTime(cpuNum));  
         }
         continue;
      }
#endif
#endif

      cpuNum = P->myNum;

      if (P->takeInterrupt) {
         if (P->stalledInst) {
         /* 
          * If we've reached this point, it means that we have just
          * unstalled the processor and are immediately supposed to
          * take an interrupt. In the past, this would smash the
          * current instruction and have it reissued when the
          * interrupt handling finished. Now we will first let the
          * unstall complete. This is especially useful in the
          * uncached cases where we don't want to reissue the ops.
          */
         } else {
            P->takeInterrupt = FALSE;
            if (!IN_MXS(P)) {
               EXCEPTION(P, EXC_INT);
            }
         }
      }

#ifdef MIPSY_MXS
      if (P->inMXS) {
        mxs:
         ms_cycle_once (P->st);
         continue;
      } else {
         if (P->switchToMXS && (P->PC == P->nPC-4) && (P->stalledInst == 0)) {
            P->inMXS = 1;
            P->switchToMXS = 0;
            CopyToMXS(P);
            CPUWarning("MXS: Switching to MXS on CPU%d at 0x%x time %lld\n", 
                       cpuNum, P->PC, (uint64)MipsyReadTime(cpuNum));
            goto mxs;
         }
      }
#endif

      /* 
       * Shortcut to keep from going through ReadInstruction again
       * after an instruction caused a dcache miss and stalled. When
       * the processor's data cache miss completes, we'll still have 
       * the instruction ready and waiting. 
       */

      if (P->stalledInst) {
         instr = P->stalledInst;
         P->stalledInst = FALSE;
      }  else { 
         Result ret;
         VA vAddr = P->PC;

         /* 
          * Anything we can do to avoid translating the virtual
          * address is a good thing. This is a quick check to see if
          * the current instruction is on the same page as the last
          * one. If so, we don't have to translate it again. This 
          * cache entry is of course cleared whenever there is a
          * chance that this translation is no longer correct. 
          */

         if (PAGE_NUMBER(vAddr) == P->pcVPNcache) {
            PA p = P->pcPaddrcache + PAGE_OFFSET(vAddr);

            if (SKIP_CACHES(P)) {
               instr = *(Inst *)HOST_DATA_ADDR(vAddr, p, cpuNum);
if (getenv("SIMOS_TRACEPC")) fprintf(stderr, "sfetch 0x%08x   0x%08x\r\n", (int)vAddr,BE2HO_4(instr));
            } else {
               /* 
                * Do a quick check if this was in the last line 
                * checked. This cached address is set on a icache hit and 
                * is invalidated whenever a line is flushed from the 
                * icache. This could be changed to selectively delete the 
                * line, but these are infrequent actions. 
                */
               
               if (QUICK_ICACHE_CHECK(p, P)) {
                  int lineIndex;
                  /* 
                   * Quick cache hit! Be careful here - it won't work 
                   * with bypassing l1 caches right now. Don't need to 
                   * touch the lru bit since it will be set to this line. 
                   */
                  MS_INSTRUCTION_SET(P->myNum, P->cachedSetIndex);
                  MS_INSTRUCTION(cpuNum, p);
                  lineIndex = p & (ICACHE_LINE_SIZE-1);
                  instr = *(Inst *) (P->cachedILineData + lineIndex);
if (getenv("SIMOS_TRACEPC")) fprintf(stderr, "qfetch 0x%08x   0x%08x\r\n", (int)vAddr,BE2HO_4(instr));
               } else {
                  /* 
                   * 2nd quickest route to the icache, no translation 
                   * needed, but we must check the cache. 
                   */
                  ret = MemRefReadInst(cpuNum, vAddr, p, &instr);
if (getenv("SIMOS_TRACEPC")) fprintf(stderr, "xfetch 0x%08x   0x%08x\r\n", (int)vAddr,BE2HO_4(instr));
                  if (ret != SUCCESS) {
                     if ((ret == STALL) || (ret == FAILURE)) {
                        P->stalledInst = 0;
                        STATS_SET(cpuNum, stallStart, MipsyReadTime(cpuNum));
                        P->cpuStatus = cpu_stalled;
                        continue;
                     } else if (ret == BUSERROR) {
                        RECORD_EXCEPTION(P, EXC_IBE, E_VEC, vAddr, P->CP0[C0_TLBHI], 
                                         P->CP0[C0_CTXT], P->CP0[C0_XCTXT]);
                     } else {
                        CPUError("Bad return(%d) from ReadICache\n", ret);
                     }
                  }
               }
            }
         } else { 
            /* 
             * This is the slow path to get an instruction. Full address 
             * translation and checking of the Icache is required. 
             */ 
            ret = ReadInstruction(vAddr, &instr);
            if (ret != SUCCESS) {
               if (P->cpuStatus != cpu_running) {
                  STATS_SET(cpuNum, stallStart, MipsyReadTime(cpuNum));
               }
               continue;
            } else {
               STATS_ADD_INTERVAL(cpuNum, stallTime, stallStart);
            }
         }

         /* 
          * The stalledInstr is stored in HO. In all other cases, we
          * instruction is fetched in BE and must be converted to HO.
          */
         instr = BE2HO_4(instr);

         STATS_INC(cpuNum, iReads, 1);
      };

      if (mipsy_debug_mode) {
	 if (mipsy_debug_mode > 1) {
	     mipsy_debug_mode--;
         } else if ((cpuNum == mipsy_break_nexti) 
             || (mipsy_break_nexti == MIPSY_BREAKANYCPU)) {
            /* 
             * MipsyDebug returns nonzero if the PC has changed 
             * or the contents of it have been overwritten, so it 
             * needs to be reloaded. 
             */
            if (MipsyDebug(cpuNum, 1)) {
               continue;
            }
         } else if (mipsy_sigusr) {
            mipsy_debug_mode = 0;
            mipsy_sigusr = 0;
            AnnExec(AnnFind("simos", "sigusr"));
         }
      }

      op = MAJOR_OP(instr);

      if (mipsOpcodes[op].func(instr, P) == SUCCESS) {
         /* CPUPrint("PC %llx - %s\n", P->PC, mipsOpcodes[op].opname);
          */
         /* Things to do after every successful instruction */
         FIX_REG_ZERO;
         INSTRUCTION_DONE_EVENT();

         if (RunPCAnnotations(P->PC, P->nPC)) 
            continue;
         P->PC = P->nPC;

         if (P->branchStatus == BranchStatus_taken) {
            P->nPC = P->branchTarget;
         } else {
            P->nPC += INST_SIZE;
         }

         if (P->branchStatus == BranchStatus_taken 
             || P->branchStatus == BranchStatus_nottaken ) { 
            P->branchStatus = BranchStatus_bd;
         } else { 
            P->branchStatus = BranchStatus_none;
         }
      }
   }
}

/*****************************************************************
 * DoneRunning
 *****************************************************************/
static void
DoneRunning(void) 
{
   PrintRealTime("Simulation End");
   MipsyExitFPU();

   /*
    * Mark as much true sharing as possible
    */
   FalseSharingCleanup();
   
   if (SKIP_CACHES(P)) {
      CPUWarning("DoneRunning and no caches being used\n");
   } else {
      if (mipsySyncWhenDone) {
         Result allret, ret;
         int timeout, i;
         CPUPrint("Syncing all processors at time %lld\n", (uint64) MipsyReadTime(0));
         for (timeout = 0; timeout < 1000*1000; timeout++) { 
            allret = SUCCESS;
            for (i = 0; i < TOTAL_CPUS; i++) {
               if (PE[i].cpuStatus == cpu_running) {
                  ret = MemRefSync(i);
                  if (ret == STALL) {
                     allret = ret;
                  }
               } else {
                  if (PE[i].cpuStatus != cpu_halted &&
                      PE[i].cpuStatus != cpu_idle) {
                     allret = STALL;
                  }
               }
            }
            if (allret == SUCCESS) 
               break;
            MIPSY_ADD_TIME(0, 1);
            EventPollSingleQueue(MipsyReadTime(0));
         } 
         if (allret != SUCCESS) 
            CPUWarning("Sync failed!\n"); 
      }
   }
}


/*****************************************************************
 * Mipsy-specific annotation code
 *****************************************************************/
void
MipsyInstallMemAnnotation(VA vAddr)
{
   /* Think of a clever way to optimize this! */
}

/*****************************************************************
 * MipsyTclInit - mipsy-specific tcl/memstat initialization
*****************************************************************/
extern int memstatOption;
#define MemStatNone 0
#define MemStatGlobal 1

void MipsyTclInit(Tcl_Interp *interp)
{
   Tcl_CreateCommand(interp, "instDump", PrintInstTclCmd,
                     (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);
   Tcl_CreateCommand(interp, "printInsts", PrintInstTclCmd,
                     (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);

#ifdef DASH_PREFETCH
   Tcl_CreateCommand(interp, "dashPrefetch", DashTclCmd,
                     (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);
#endif
   Tcl_CreateCommand(interp, "verboseDebug", InterpretVerboseDebug,
                     (char *)NULL, (Tcl_CmdDeleteProc*)NULL);
#ifdef TRACING
   Tcl_CreateCommand(interp, "traceDump", TraceDumpTclCmd,
                     (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);
#endif

   Tcl_LinkVar(interp, SaveString("SIMOS(SOLO)"), (char*)&isSolo,
               TCL_LINK_READ_ONLY | TCL_LINK_BOOLEAN);

   ParamRegister("PARAM(CPU.Mipsy.MipsySampleCycles)", (char *)&mipsyRunInterval, 
                 PARAM_INT);
   ParamRegister("PARAM(CPU.Mipsy.MXSSampleCycles)", (char *)&mxsRunInterval, 
                 PARAM_INT);
#ifdef SOLO
   memstatOption = MemStatNone;
#else
   memstatOption = MemStatGlobal;
#endif

#ifdef MIPSY_MXS
   ms_events_init();
#endif
}

/*****************************************************************
 * Tcl interface for controlling mipsy's vebose debugging
 *****************************************************************/
int 
InterpretVerboseDebug(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
{

#ifndef DEBUG_DATA_VERBOSE
   Tcl_AppendResult(interp, 
                    "verboseDebug not supported unless mipsy compiled -DDEBUG_DATA_VERBOSE",
                    NULL);
   return TCL_ERROR;

#else 

#ifndef SOLO
   if (simosCPUType != MIPSY) {
      Tcl_AppendResult(interp, "verboseDebug not supported unless running in mipsy",
                       NULL);
      return TCL_ERROR;
   }
#endif
   if (argc < 2) {
      Tcl_AppendResult(interp, 
                       "Bad: should be: verboseDebug <on|off>", 
                       NULL);
      return TCL_ERROR;
   }

   if (!strcmp(argv[1], "off")) {
      CPUWarning("verboseDebug: Turning verbose cache debugging OFF\n");
      verboseDebugEnabled = 0;
   } else if (!strcmp(argv[1], "on")) {
      CPUWarning("verboseDebug: Turning verbose cache debugging ON\n");
      verboseDebugEnabled = 1;
   } else {
      Tcl_AppendResult(interp, 
                    "bad: should be: verboseDebug <on|off>", NULL);
      return TCL_ERROR;
   }

   return TCL_OK;
#endif
}


void 
MXSSwitch(int cpu, int enterMXS)
{
#ifdef MIPSY_MXS
   if ((PE[cpu].inMXS && enterMXS) || (!PE[cpu].inMXS && !enterMXS))
      return;
   if (enterMXS)
      PE[cpu].switchToMXS = 1;
   else
      PE[cpu].switchToMIPSY = 1;
   return;
#else
   CPUWarning("Trying to switch between mipsy and MXS, "
              "but this simos was built without MXS\n");
#endif
}

#ifdef MIPSY_MXS
/*
 * DoUncachedRead - perform an uncached/bdoor read for MXS.
 */
int 
DoUncachedRead(struct s_cpu_state *st, uint vAddr, uint pAddr, 
               int size, void *data)
{
   RefSize refsize;

   switch (size) {
   case 1: 
      refsize = BYTE_SZ; 
      break;
   case 2: 
      refsize = HALF_SZ; 
      break;
   case 4: 
      refsize = WORD_SZ; 
      break;
   case 8: 
      refsize = DOUBLE_SZ; 
      break;
   default: 
      ASSERT(0);
   }
   return MipsyReadMem(vAddr, data, refsize, NO_FLAVOR);
}

/*
 * DoUncachedWrite - perform an uncached/bdoor write for MXS
 */
int 
DoUncachedWrite(struct s_cpu_state *st,  uint vAddr, uint pAddr,  
                int size, int accel, void *data)
{
   RefSize refsize;

   switch (size) {
   case 1: 
      refsize = BYTE_SZ; 
      break;
   case 2: 
      refsize = HALF_SZ; 
      break;
   case 4: 
      refsize = WORD_SZ; 
      break;
   case 8: 
      refsize = DOUBLE_SZ; 
      break;
   default: 
      ASSERT(0);
   }
   return MipsyWriteMem(vAddr, data, refsize, NO_FLAVOR);
}


/*
 * GetLastException - Return the exception number of the last 
 *                    execption RECORDed by the RECORD_EXCEPTION macro.
 */

int
GetLastException(struct s_cpu_state *st)
{
   CPUState *P = (CPUState *) (st->mipsyPtr);
   return P->lastException;
}

void
RecordMxsMemsysStall(struct s_cpu_state *st, SimTime stallTime)
{
  CPUState *P = (CPUState *) (st->mipsyPtr);
  int cpuNum = P->myNum;

  STATS_INC(cpuNum, stallTime, stallTime);
}

static void 
SwitchSimulators(int cpuNum,EventCallbackHdr *hdr, void *arg)
{
   int i;
   SimTime interval;
   ASSERT( cpuNum ==  0 );
   if (PE[0].inMXS) {
      for (i = 0; i < TOTAL_CPUS; i++) {
         MXSSwitch(i,FALSE);
         /* WARNING - need switch simulators annotation */
      }
      interval = mipsyRunInterval;
   } else {
      for (i = 0; i < TOTAL_CPUS; i++) {
         MXSSwitch(i,TRUE);
         /* WARNING - need switch simulators annotation */       
      }
      interval = mxsRunInterval;
   }
   EventDoCallback(cpuNum, SwitchSimulators, &switchSimHdr, 0, interval);
}
#endif  /* MIPSY_MXS */