delta.c 22.2 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
/*
 * Copyright (C) 1998 by the Board of Trustees
 *    of Leland Stanford Junior University.
 * Copyright (C) 1998 Digital Equipment Corporation
 *
 * This file is part of the SimOS distribution.
 * See LICENSE file for terms of the license.
 *
 */

/* ******************************************************
 * delta.c
 * ******************************************************/

#include <stdio.h>
#include <malloc.h>
#include <setjmp.h>
#include <alpha/inst.h>
#include <sys/signal.h>

#include "simtypes.h"
#include "sim_error.h"
#include "tcl_init.h"
#include "machine_params.h"
#include "arch_specifics.h"
#include "params.h"
#include "annotations.h"
#include "gdb_interface.h"

#include "tc.h"
#include "tc_coherence.h"
#include "userflush.h"

#include "alpha.h"
#include "ev5.h"
#include "ev5_ipr.h"
#include "alpha_gdb.h"
#include "gamma.h"
#include "stats.h"
#include "qc.h"
#include "tb.h"

#include "delta.h"
#include "translator.h"
#include "emit.h"

void **constantsPtr;
void **currentConstant;

union alpha_instruction illegalInstr;

struct DeltaState delta;

#define DELTA_NUM_CACHES 1

static int deltaSigfpeUseLongjmp = 0;

static void DeltaEvents(void);

static void DeltaCPUVectorInit(void);
static char *DeltaStats(void);
     
extern jmp_buf jmpEnv;
static void DeltaDoneRunning(void)
{
  CPUWarning("Exiting the Delta simulator \n");
  AnnExec(AnnFind("simos", "exit"));
}

/* ******************************************************
 * main entry point
 * ******************************************************/

void ExecuteDelta(void)
{

   AlphaState *dummy = (AlphaState *) malloc(sizeof(AlphaState));
   bzero((char*)dummy,sizeof(AlphaState));

   /*
   * ssetup original arguments
   */
  GammaInitTables();

  GammaCPUVectorInit(); /* default functions */
  DeltaCPUVectorInit(); /* override some of them */

  if (!alphaLateInitDone) {
     /*
      * Stuff that needs late initalization.
      * XXX should use a register mechanism
      */

     AnnCommonSetup();
     AnnFMInit(128);
     InstallPoller();  /* devices */
     alphaLateInitDone = 1;
  }
  

  /*
   * simos enter annotation
   */
  {
     int cpu;
     for (cpu=0;cpu<TOTAL_CPUS;cpu++) {
	curPE = PE[cpu];
	AnnExec(AnnFind("simos","enter"));
     }
     curPE = PE[0];
  }


  /*
   * Configure DELTA
   */
  
  delta.useDQC = 1;
  delta.useIQC = 1;
  delta.chainKnownBranches = 1;
  delta.chainBranches = 1;
  delta.chainSpec = 1;
  delta.inlineIQC = 1;
  delta.inlineDQC = 1;
  delta.registerAllocate = 1;
  delta.mergeBranches = 1;
  delta.saveRegOpt = 1;
  delta.bypassICheck = 1;

 
  /*
   * Initalize the translation cache
   */
  {

     int size[1]   = {4*1024*1024}; /* THIS IS THE MAX 'cause branches */
     int pcSize[1] = {1024 * 128};
     int annSize[1] = {16*1024};

     illegalInstr.word = 0;
     illegalInstr.common.opcode = 0; /* callpal */;
     illegalInstr.word |= 0x3d; /* retsys, privileged opcode */
      
       
     usercacheinit();
     TC_init(DELTA_NUM_CACHES,size,pcSize,annSize,illegalInstr.word);
     TCcoherence_init(alphaMemoryPtr);
 }
  /*
   * link PE 0 and the dummy. Other PE are still in the PROM
   * loop.
   */


  PE[0]->cpuState = cpu_running;
  PE[0]->timeQuantum = delta.timeQuantum;
  PE[0]->cycleCountDown = delta.timeQuantum;
  PE[0]->cycleCount = 0;
  PE[0]->nextCPU = dummy;
  dummy->nextCPU = PE[0];
  dummy->myNum = -1;



  ASSERT(delta.timeQuantum > 0);

  CPUWarning("DELTA: P->PC entry = 0x%lx \n",PE[0]->PC); 
  DeltaClearTranslationCache(TCFLUSH_ALL,DELTAFLUSH_INIT);
  {
     void * (*f)() = GetEmitFunction(0,CALLOUT_INITSP);
     delta.stackPtr = f();
  }
  curPE = dummy;
  InitSimulatorStats(32*1024*1024, DeltaStats);

  DeltaFPInit(); /* installs SIGFPE handler */

  delta.debugCycle = 28491190;
  delta.debugCycle = 0;
  {
     int toCPUType = NO_CPU;
     if ((toCPUType = setjmp(jmpEnv)) != 0) {
        DeltaDoneRunning();
        simosCPUType = toCPUType;
     } else {
        if (getenv("DELTA_SIGFPE_USE_LONGJMP") != NULL)
           deltaSigfpeUseLongjmp = atoi(getenv("DELTA_SIGFPE_USE_LONGJMP"));
        DeltaRun();
     }
  } 
}
    


#if 1
#define DELTA_CHECK(_x) 
#else
#define DELTA_CHECK(_x) DeltaCheck(_x) 
#endif

TCA checkTCA;
void DeltaCheck(char *string)
{
   SimTime time = CPUVec.CycleCount(0);
   if (time >= 19327695) { 
      CPUPrint("XXX DELTA_CHECK at %lld PC=0x%lx  tca=%lx %s \n",
	       (uint64)time,curPE->PC,checkTCA,string);
   }
}

/* **********************************************************
 * NextTCA: find the next TCA at all cost, 
 *          possibly raising an ITLB exception, calling
 *          the translator or even flusing the TC
 * **********************************************************/

static inline TCA NextTCA(void)
{    
   AlphaState *P = curPE;
   PA pAddr=0;
   MA mAddr, checkMA;
   MMUStatus status=MMU_SUCCESS;
   ASSERT(P->myNum>=0);

   checkMA = QCLookup(P->curIQC,P->PC,0);
   if ( delta.useIQC) {
      mAddr = checkMA;
      pAddr = MEMADDR_TO_PHYS(M_FROM_CPU(P->myNum),mAddr);
   } else {
      mAddr = 0;
   }

   if (!mAddr) { 
      status =  EV5_ITranslateVirtual(P,&pAddr, 1);
      mAddr = PHYS_TO_MEMADDR(M_FROM_CPU(P->myNum),pAddr);
      ASSERT( !checkMA || checkMA==mAddr);
   }
   if (status==MMU_SUCCESS) {
      int tcCache = 0;
      TCA tca = TC_PCLookup(tcCache,P->PC,mAddr);
      if (!tca) {
         int flushCount = tcGenNumber;
         /* QCAnalCheck(P,P->curDQC,TB_DATA); */
         tca = Translate(P->myNum,P->PC,pAddr,mAddr,P->curIQC);
         if (flushCount !=tcGenNumber) return 0;
      }
      return tca;
   } else {
      ASSERT(!checkMA);
      ASSERT(status==MMU_EXCEPTION);
      return 0;
   }
}



/* ******************************************************** 
 * DeltaRun: main loop
 * ********************************************************/

  
static void DeltaEnterTC(TCA tca)
{
   AlphaState *P = curPE;
   checkTCA = tca;
   DELTA_CHECK("EnterTC");
   ASSERT(!IS_PAL(P));
   if (delta.debugCycle &&
       delta.debugCycle < CPUVec.CycleCount(0)) {
      CPUPrint("%ld Entering TC for PC=0x%lx tca=0x%lx\n",
	       CPUVec.CycleCount(curPE->myNum),curPE->PC,tca);
   }
   delta.EnterTC(P,tca,delta.stackPtr,constantsPtr);
   NOTREACHED();
}

static jmp_buf deltaJmpEnv;

void DeltaRun()
{
   if (deltaSigfpeUseLongjmp)
      setjmp(deltaJmpEnv);
   while(1) {
      
      if (curPE->myNum<0) {
         DeltaEvents();
         ASSERT( curPE->myNum>=0);
      } else if (IS_PAL(curPE)){ 
	 if (delta.debugCycle && delta.debugCycle < CPUVec.CycleCount(0)) {
	    CPUPrint("%ld Entering DeltaGammaPalCode for PC=0x%lx\n",
		     CPUVec.CycleCount(curPE->myNum),
		     curPE->PC);
	 }
         DeltaGammaRun(curPE);
	 if (curPE->cycleCountDown < 0) { 
	    curPE = curPE->nextCPU;
	 }
      } else {
         TCA tca = NextTCA();
         if (tca) { 
	    DeltaEnterTC(tca);
            /* notreached */
            NOTREACHED();
         } else {
            /*
             * either we got an exception, of
             * the TC got flushed. In any case, we
             * continue with the same processor
             */          
         }
      }
   }
}


static void DeltaEvents(void)
{
   AlphaState *t=curPE->nextCPU;
   SimTime max = 0;
   ASSERT(curPE->myNum==-1);
   while (t !=curPE) { 
      SimTime time = t->cycleCount +t->timeQuantum - t->cycleCountDown;
      while( t->cycleCountDown < 0) { 
	 t->cycleCountDown += t->timeQuantum;
         t->cycleCount     += t->timeQuantum;
      }
      if (time > max) max = time;
      t = t->nextCPU;
   }
   curPE = curPE->nextCPU;
   ASSERT(curPE->myNum == 0);
   EventProcess(-1,max);
}


void CalloutEvents(void)
{
   ASSERT(curPE->myNum==-1);
   DeltaEvents();
   DeltaRun();
   NOTREACHED();
}

/*
 * Chaining for branches
 */


TCA CalloutChain(TCA chainAddr, VA prevPC)
{
   TCA tca; 

   ASSERT (curPE->myNum>=0);
   ASSERT(!IS_PAL(curPE));

   tca= NextTCA();
   if (delta.debugCycle && 
       CPUVec.CycleCount(0) > delta.debugCycle ) {
      CPUPrint("%ld CalloutChain pc=0x%lx ra=0x%lx tca=0x%lx \n",
	       CPUVec.CycleCount(curPE->myNum),curPE->PC,curPE->reg[REG_RA],tca);
   }
   if (tca) {
      return PatchBranch(prevPC,curPE->PC,chainAddr,tca); 
   } else {
      DeltaRun();
      /* notreached */
      NOTREACHED();
      return 0;
   }
}

/*
 * Chaining for register indirect calls
 */

TCA CalloutChainSpeculative(TCA chainAddr)
{
   TCA tca; 
   ASSERT(curPE->myNum >=0);
   ASSERT(!IS_PAL(curPE));
   tca= NextTCA();
   if (delta.debugCycle && 
       CPUVec.CycleCount(0) > delta.debugCycle ) {
      CPUPrint("%ld CalloutChain pc=0x%lx ra=0x%lx tca=0x%lx \n",
	       CPUVec.CycleCount(curPE->myNum),curPE->PC,curPE->reg[REG_RA],tca);
   }
   if (tca) {
      if (chainAddr) { 
         return PatchSpeculative(chainAddr,tca);
      } else { 
         return tca;
      }
   } else {
      DeltaRun();
      /* notreached */
      NOTREACHED();
      return 0;
   }
}


TCA CalloutChainCtxt(void)
{
   TCA tca; 
   if (curPE->myNum < 0) { 
      CalloutEvents();
      NOTREACHED();
   }
   if (IS_PAL(curPE)) { 
      DeltaRun();
      NOTREACHED();
   }

   tca= NextTCA();
   
   if (delta.debugCycle && 
       CPUVec.CycleCount(0) > delta.debugCycle ) {
      CPUPrint("%ld CalloutChain pc=0x%lx ra=0x%lx tca=0x%lx \n",
	       CPUVec.CycleCount(curPE->myNum),curPE->PC,curPE->reg[REG_RA],tca);
   }
   if (tca) {
      return tca;
   } else {
      DeltaRun();
      /* notreached */
      NOTREACHED();
      return 0;
   }
}

/*
 * Be very careful of side-effects on prePC and postPC 
 * annotations. e.g. don't perform register preloading on
 * those basic blocks.
 * Also should make sure that the annotation only
 * fires once if we reenter the TC
 */

void CalloutPostPCAnn(void)
{
   VA pc = curPE->PC;
   int x = delta.sideEffectPC;
   int y = delta.sideEffectMem;
   AnnPtr aptr = AnnFMLookup(curPE->PC,ANNFM_PC_TYPE);
   ASSERT(aptr);
   DELTA_CHECK("PostPCAnn");
   ASSERT(CPUVec.CycleCount(0)!= 28198929);
   AnnExec(aptr);
   ASSERT( x==delta.sideEffectPC);
   if (y!=delta.sideEffectMem) {
      /*
       * if this is a problem, we should go into
       * some kind of emulation mode to figure out the
       * next PC.
       
       * Could also have this done by passing the next PC
       * as an argument, computed in the TC
       */
      CPUWarning("TC flushed on Post-PC annotation. BAD \n");
   }
   ASSERT( y==delta.sideEffectMem);
   ASSERT( pc==curPE->PC) ;
   return;
}

void CalloutPrePCAnn(void)
{ 
   int x = delta.sideEffectPC;
   int y = delta.sideEffectMem;
   AnnPtr aptr = AnnFMLookup(curPE->PC,ANNFM_PRE_PC_TYPE);
   ASSERT(aptr);
   DELTA_CHECK("PrePCAnn");
   AnnExec(aptr);
   if (x!= delta.sideEffectPC || 
       y!= delta.sideEffectMem) {
      DeltaRun();
      NOTREACHED();
   }
   return;
}

void CalloutNever(TCA tca)
{
   CPUError("Reached CalloutNever at tca=0x%lx PC=0x%lx \n",
	    tca,curPE->PC);
}


void CalloutCallPal(union alpha_instruction instr)
{
   DeltaGammaRun(curPE);
   DeltaRun();
   NOTREACHED();
}


void CalloutMisc(void)
{
   DeltaGammaRun(curPE);
   DeltaRun();
   NOTREACHED();
}


void CalloutFEN(void)
{
     EV5_Trap(curPE,TRAP_FEN);
   DeltaGammaRun(curPE);
   DeltaRun();
   NOTREACHED();
}


void CalloutITB(void)
{
   MA mAddr=0; 
   PA pAddr=0;
   MMUStatus status;
   AlphaState *P = curPE;

   DELTA_CHECK("ITB");
   /*
    * should be moved to assembly
    */
   if (!delta.inlineIQC) { 
      mAddr = QCLookup(P->curIQC,P->PC,0);
      ASSERT( mAddr == delta.QCLookupRead(P->curIQC,P->PC));
      if (mAddr && delta.useIQC) return;
   } else {
#if 0
      mAddr =  QCLookup(P->curIQC,P->PC,0);
      ASSERT(!mAddr);
#endif
   }
   status = EV5_ITranslateVirtual(P,&pAddr, 1); 
   if (status==MMU_SUCCESS) {
      ASSERT(!mAddr || (mAddr=PHYS_TO_MEMADDR(0,pAddr)));
      return;
   } else {
      ASSERT(!mAddr);
      ASSERT( status==MMU_EXCEPTION);
      DeltaRun();
   }
   NOTREACHED();
}


/*
 * XXX this is real slow, but the
 * XXX P->instr is needed in case of a 
 * XXX trap
 */




MA CalloutDTBLoad(VA vAddr)
{
   AlphaState *P = curPE;
   PA pAddr=0;
   MA mAddr;
   MA qcMA=0;
   MMUStatus status;
  
   DELTA_CHECK("DTBLoad");
   /* ASSERT( CPUVec.CycleCount(0)!=19450462); */

#ifdef DEBUG_DELTA_MMSTAT
   status = EV5_ITranslateVirtual(P,&pAddr,1);
   ASSERT( status==MMU_SUCCESS);
   mAddr = PHYS_TO_MEMADDR(M_FROM_CPU(P->myNum),pAddr);
   P->instr = *(TCA) mAddr;
#endif   

   if (!delta.inlineDQC) { 
      qcMA = QCLookup(P->curDQC,vAddr,0);
      ASSERT( qcMA == delta.QCLookupRead(P->curDQC,vAddr));
      if (qcMA && delta.useDQC) {
         return qcMA;
      }
   }  else {
#if 0
      qcMA =QCLookup(P->curDQC,vAddr,0);
      ASSERT(!qcMA);
#endif
   }
   status = EV5_DTranslateVirtual(P,vAddr,0,1,&pAddr); 
   if (status==MMU_SUCCESS) {
      mAddr = PHYS_TO_MEMADDR(M_FROM_CPU(P->myNum),pAddr);
      ASSERT(!qcMA || (qcMA==mAddr));
      return mAddr;
   } else if (status==MMU_EXCEPTION) {
      ASSERT(!qcMA);
      DeltaRun();
      NOTREACHED();
      return 0;
   } else {
      ASSERT(!qcMA);
      ASSERT( status==MMU_UNCACHED);
      DeltaGammaRun(P);
      DeltaRun();
      NOTREACHED();
      return 0; 
   }
}

MA CalloutDTBStore(VA vAddr)
{
   AlphaState *P = curPE;
   PA pAddr=0;
   MMUStatus status;
   MA mAddr;
   MA qcMA=0;
   DELTA_CHECK("DTBStore");

#ifdef DEBUG_DELTA_MMSTAT
   status = EV5_ITranslateVirtual(P,&pAddr,1);
   ASSERT( status==MMU_SUCCESS);
   mAddr = PHYS_TO_MEMADDR(M_FROM_CPU(P->myNum),pAddr);
   P->instr = *(TCA) mAddr;
#endif

   if (!delta.inlineDQC) {
      qcMA = QCLookup(P->curDQC,vAddr,1);
      
      ASSERT( qcMA == delta.QCLookupWrite(P->curDQC,vAddr));
      
      if (qcMA && delta.useDQC) { 
         return qcMA;
      }
   } else {
#if 0
      qcMA = QCLookup(P->curDQC,vAddr,1);
      ASSERT(!qcMA);
#endif
   }

   status = EV5_DTranslateVirtual(P,vAddr,1,1,&pAddr); 
   if (status==MMU_SUCCESS) {
      mAddr = PHYS_TO_MEMADDR(M_FROM_CPU(P->myNum),pAddr);
      ASSERT( !qcMA || (qcMA==mAddr));
      if (TCcoherence_is_code(mAddr)) {
         DeltaGammaRun(P); /* required for forward progress */
         DeltaClearTranslationCache(TCFLUSH_ALL,DELTAFLUSH_COHERENCE);
         DeltaRun();
         NOTREACHED();
         return 0;
      } else {
         return mAddr;
      }
   } else if (status==MMU_EXCEPTION) {
      ASSERT( !qcMA);
      DeltaRun();
      NOTREACHED();
      return 0;
   } else {
      ASSERT(!qcMA);
      ASSERT( status==MMU_UNCACHED);
      DeltaGammaRun(P);
      DeltaRun();
      NOTREACHED();
      return 0; 
   }
}


/* ********************************************************
 * CPUVector functions. The gamma functions are installed
 * by default, but some of them need to be overridden
 * ********************************************************/


SimTime
DeltaCycleCount(int cpuNum) 
{
   AlphaState *P = PE[cpuNum];
   if (!P) {
      return 0;
   } else { 
      return P->cycleCount + P->timeQuantum - P->cycleCountDown;
   }
}


/*
 * This algorithm incidentally also works for 
 * GAMMA, in case we eliminate P->instr
 */
Inst DeltaCurrentInstruction(int cpuNum)
{
   Inst inst;
   AlphaState *P = PE[cpuNum];
   VA oldPC = P->PC;
   MA mAddr = QCLookup(P->curIQC,P->PC,0);
   ASSERT(P->myNum==cpuNum && cpuNum>=0);

   /*  QCConsistencyCheck(P,P->curIQC,TB_INSTR); */

   if (!mAddr) {
      PA pAddr =0;
      MMUStatus status;
      if (!IS_PAL(P)) {
	 CPUWarning("DeltaCurrentInstruction oldPC=%lx \n",oldPC);
	 ASSERT(0);
      }
      status = EV5_ITranslateVirtual(P,&pAddr,0);

      ASSERT( status==MMU_SUCCESS);
      mAddr = PHYS_TO_MEMADDR(M_FROM_CPU(P->myNum),pAddr);
   }
   inst = *(Inst*) mAddr;
#ifdef DEBUG_DELTA_MMSTAT
   ASSERT( inst == P->instr.word); /* debug */
   CPUWarning("DEBUG_DELTA_MMSTAT clear \n");
#endif
   return inst;
}
 
Result DeltaPutReg(int cpu, int regnum, Reg val)
{
   Result res = GammaPutReg(cpu,regnum,val);
   if (regnum == PC_REGNUM) { 
      delta.sideEffectPC++;
   }
   return res;
}


Result DeltaPutMem(int cpuNum, VA vAddr, uint nbytes, char *buf)
{
   int i;
   PA pA=0;
   if (0)
   CPUWarning("Put mem size %d addr 0x%lx buf 0x%x \n",nbytes,vAddr,*(uint32*)buf);
   for (i=0;i<nbytes;i++) {
      if (EV5_DTranslateVirtual(PE[cpuNum],vAddr+i,0,0,&pA)==MMU_SUCCESS) {
         MA mAddr = PHYS_TO_MEMADDR(M_FROM_CPU(cpuNum),pA);
         *(char*)mAddr =  buf[i];
	 if (TCcoherence_is_code(mAddr)) {
	    DeltaClearTranslationCache(TCFLUSH_ALL,DELTAFLUSH_PUTMEM);
	    delta.sideEffectMem++;
	 }
      } else {
	 return FAILURE;
      }
   }
   return SUCCESS;

}



void DeltaDebug( int cpuNum , int sigUsr)
{

   if (cpuNum < 0 ) {
      ASSERT( curPE->myNum >=0);
      Simdebug_run(SIGUSR2, curPE->myNum);
   } else {
      Simdebug_run(SIGUSR2, cpuNum);
   }
   return;       
}


void DeltaDMAInval(int machine, PA *list)
{
   int cpuNum;
   PA *paPtr;
   
   for (paPtr = list; *paPtr; paPtr++) { 
      ASSERT (*paPtr < MEM_SIZE(0));
      if (TCcoherence_check(PHYS_TO_MEMADDR(machine,*paPtr),
                            PHYS_TO_MEMADDR(machine,*paPtr+PAGE_SIZE))) {
         CPUWarning("DeltaDMAInval: conflict for pAddr=0x%lx \n",
                    *paPtr);
         DeltaClearTranslationCache(TCFLUSH_ALL,DELTAFLUSH_DMAINVAL);
      }        
   }
}

/* **************************************************
 * DeltaLaunchSlaveCPU
 * NEVE RETURNS
 */

void DeltaLaunchSlaveCPU(int cpu)
{
   int i;
   CPUWarning("DELTA launch slave cpu %d \n",cpu);
   PE[cpu]->cpuState = cpu_running;
   PE[cpu]->timeQuantum = PE[0]->timeQuantum;
   PE[cpu]->cycleCountDown = PE[0]->cycleCountDown;
   PE[cpu]->cycleCount = PE[0]->cycleCount;

   for(i=cpu-1;i>=0;i--) {
      if (PE[i]->cpuState == cpu_running) {
	 PE[cpu]->nextCPU = PE[i]->nextCPU;
	 PE[i]->nextCPU = PE[cpu];
	 return;
      }
   }
   ASSERT(0);
}

   

static void DeltaCPUVectorInit(void)
{
   CPUVec.CycleCount           = DeltaCycleCount;
   CPUVec.InstrCount           = DeltaCycleCount;
   CPUVec.CurrentInstruction   = DeltaCurrentInstruction;
   CPUVec.PutRegister          = DeltaPutReg;
   CPUVec.PutMemory            = DeltaPutMem;
   CPUVec.Handle_Debug_Signal  = DeltaDebug;
   CPUVec.DMAInval             = DeltaDMAInval;
   CPUVec.LaunchSlaveCPU       = DeltaLaunchSlaveCPU;
}



/* ******************************************************* 
 * DeltaParameters
 * *******************************************************/

void DeltaTclInit(Tcl_Interp *interp)
{
   ParamRegister("PARAM(CPU.Delta.TimeQuantum)", (char *)&delta.timeQuantum, PARAM_INT);
}



void DeltaClearTranslationCache(int code, DeltaFlushCause cause) 
{
   char *reason;
   switch (cause) { 
   case DELTAFLUSH_INIT: 
      reason = " Init";
      break;
   case   DELTAFLUSH_COHERENCE:
      reason = "TCcoherence violation";
      break;
   case DELTAFLUSH_TC:
      reason = "TC overflow";
      break;
   case DELTAFLUSH_CONSTANTS:
      reason = "Constants overflow\n";
      break;
   case DELTAFLUSH_DMAINVAL:
      reason = "DMA conflict";
      break;
   case DELTAFLUSH_PUTMEM:
      reason = "PutMem conflict";
      break;
   default: NOTREACHED(); reason = "none";
   }


   if (cause!=DELTAFLUSH_COHERENCE) { 
      CPUWarning("DeltaClearTranslationCache of cache=%d  for %s \n",
		 code,reason);
   } else {
      delta.stats.coherenceFlushes++;
   }
   
   if (code==TCFLUSH_ALL || DELTA_NUM_CACHES==1) { 
      TCcoherence_flush();
      DeltaInitConstants();
   }
   TC_flush(code);
   if (code==TCFLUSH_ALL) { 
      int i;
      for (i=0;i<DELTA_NUM_CACHES;i++) {
	 EmitFunctions(i);
      }
   } else {
      EmitFunctions(code);
   }
   delta.EnterTC = ( void (*)()) GetEmitFunction(0,CALLOUT_ENTERTC);
   delta.QCLookupRead  = ( MA (*)()) GetEmitFunction(0,CALLOUT_QCLOOKUP_READ);
   delta.QCLookupWrite = ( MA (*)()) GetEmitFunction(0,CALLOUT_QCLOOKUP_WRITE);


}


/* ***************************************************************
 * Constants management 
 * ***************************************************************/


void DeltaInitConstants(void)
{
   if (!constantsPtr) { 
      constantsPtr = malloc(MAX_CONSTANTS*sizeof(void *));
   }
   bzero((char*)constantsPtr,MAX_CONSTANTS*sizeof(void *));

   constantsPtr[CALLOUT_CALLPAL]   = CalloutCallPal;
   constantsPtr[CALLOUT_ITB]       = CalloutITB;
   constantsPtr[CALLOUT_DTB_LOAD]  = CalloutDTBLoad;
   constantsPtr[CALLOUT_DTB_STORE] = CalloutDTBStore;
   constantsPtr[CALLOUT_MISC]      = CalloutMisc;
   constantsPtr[CALLOUT_FEN]       = CalloutFEN;
   constantsPtr[CALLOUT_CHAIN]     = CalloutChain;
   constantsPtr[CALLOUT_CHAIN_SPEC]= CalloutChainSpeculative;
   constantsPtr[CALLOUT_CHAIN_CTXT]= CalloutChainCtxt;
   constantsPtr[CALLOUT_POSTPC_ANN]= CalloutPostPCAnn;
   constantsPtr[CALLOUT_PREPC_ANN] = CalloutPrePCAnn;
   constantsPtr[CALLOUT_NEVER]     = CalloutNever;

   /* .... other constants follow */
   currentConstant = constantsPtr+MAX_CALLOUTS;
}
   

int DeltaAllocateConstant(void *value) 

{
   int64 offset;
   *currentConstant = value;
   offset = (char*)currentConstant  - (char*)constantsPtr;
   currentConstant++;
   ASSERT (offset < MAX_CONSTANTS*sizeof(void *));
   return (int)offset;
}

int DeltaRemainingConstants()
{
   return MAX_CONSTANTS - (currentConstant - constantsPtr);
}



char *DeltaStats()
{
   static char buf[256];
   sprintf(buf,"TCcoh %5d ",delta.stats.coherenceFlushes);
   delta.stats.coherenceFlushes = 0;
   return buf;
}

/* 
 * signal
 */

void DeltaFPInit()
{
  struct sigaction action;
  struct sigaction oaction;
  static void DeltaSIGFPEHandler(int signal, siginfo_t *psiginfo, void *pcontext);

  action.sa_handler = (void(*)(int))DeltaSIGFPEHandler;
  action.sa_mask = 0;
  action.sa_flags = SA_SIGINFO;
  sigaction(SIGFPE, &action, &oaction);
}


void DeltaSIGFPEHandler(int signal, siginfo_t *psiginfo, struct sigcontext *ctxt)
{
  long code = psiginfo->si_code;
  Reg fpcr = gammaReadFPCR();
  Reg ofpcr = 0;
  Reg pc = (curPE != NULL ? curPE->PC : -1L);
  int instr = (curPE != NULL ? DeltaCurrentInstruction(curPE->myNum) : 0);
  if (curPE != NULL) {
    ofpcr = curPE->fpcr;
  }
  CPUWarning("@@@@ DeltaSIGFPEHandler: pc=%lx instr=%x ofpcr=%lx actual fpcr=%lx code=%lx\n", 
	  pc, instr, ofpcr, fpcr, code);

  /*
   * XXX should check that this is indeed a AXPTYPE_CANTRAP instruction
   * XXX with its trap barrier
   */

  curPE->PC = ctxt->sc_regs[DELTAREG_CALLOUT_PC];

  /* set FPCR and raise the EV5_Trap */
  EV5_Trap(curPE, TRAP_ARITH);
  if (deltaSigfpeUseLongjmp) {
     longjmp(deltaJmpEnv, 0);
  } else {
     /* reinstall the handler */
     DeltaFPInit();
     /* call DeltaRun -- @@@@ grows the stack? */
     DeltaRun();
  }
  NOTREACHED();

}