page.c 26 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
/* ***************************************************************************/
/* *                                                                         */
/* * page.c - does some memory allocation, plus paging for "virtual memory"  */
/* *                                                                         */
/* ***************************************************************************/

/* This code was liberated from flashlite, hence the copyright:              */
/* ************************************************************************* */
/* *                                                                       * */
/* *               Copyright (C) 1993-1998 Stanford University             * */
/* *                                                                       * */
/* *  These coded instructions, statements, and computer programs contain  * */
/* *  unpublished proprietary information of Stanford University, and      * */
/* *  are protected by Federal copyright law.  They may not be disclosed   * */
/* *  to third parties or copied or duplicated in any form, in whole or    * */
/* *  in part, without the prior written consent of Stanford University.   * */
/* *                                                                       * */
/* ************************************************************************* */

/*#define COMA*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "solo.h"
#include "simtypes.h"
#include "sim_error.h"
#include "solo_page.h"
#include "cp0.h"
#include "tcl_init.h"
#include "params.h"

#ifdef USE_FLASHLITE
# include "machine.h"
# ifdef FLASHPOINT
#  include "mipsy_interface.h"
#  include "flash_interface.h"
# endif
#endif

#include "pcache.h"		/* So we know the MHT size */
#include "scache.h"		/* So we know the MHT size */
#include "bstring.h"

#define MINIMUM_VA_VALUE	100
#define ONE_MEGABYTE	0x100000

/* padding so that comaHACKADDR 
 * is on a FLASH cacheline of its
 * own.  We need to avoid a situation
 * where it is on the same cache line
 * as soloActiveProcs...bad things
 * happen if it is!!!
 */
static struct garbage {
  int PAD[128];
  int comaHACKADDR;
  int PAD1[128];
} junk;

#ifdef COMA
void SysPlaceRangeCOMAHack(unsigned procNum, FLASHAddress addrToDelete);
#else
void SysPlaceRangeCOMAHack(unsigned procNum, FLASHAddress addrToDelete) {}
#endif

int soloPACompressNodeShift;
uint soloPACompressOffsetMask;
uint soloLockBase;
uint soloBarrierBase;
uint soloTotalMemory;

static int nextclus = 0;       /* next processor to assign pages to */
static unsigned *cachepage;	/* array of next cacheable space page numbers */

static int totalProcs;   /* Number of processors on system */
static int totalMemPerProc; /* Memory per node */
static int applicationMemoryStart;  /* ??? */
static enum { ROUND_ROBIN, FIRST_TOUCHED} allocationPolicy;


/* Global FlashLite declarations */

/* local function prototypes */

static void InitPageTrans(void);
static void InitMemAllocation(void);
static int NumBits(unsigned number);
static bool RemoveMapping(VA v_addr);
#ifdef FLASHPOINT
// I need to call this from solo_anl.c when using flashpoint
bool GetPhysicalAddr(VA v_addr, SoloPA *p_addr, uint *flavor);
SoloPA AllocatePage(int module, VA va);
void NewMapping(VA v_addr, SoloPA p_addr,  uint flavor);
#else
static bool GetPhysicalAddr(VA v_addr, SoloPA *p_addr, uint *flavor);
static SoloPA AllocatePage(int module, VA va);
static void NewMapping(VA v_addr, SoloPA p_addr,  uint flavor);
#endif

static void TrySysPlaceRange(int cpuNum, EventCallbackHdr *hdr, void *v);

static EventCallbackHdr sys_place_range_callback;
static int sys_place_range_tries;
static VA sys_place_range_start;
static VA sys_place_range_stop;
static int sys_place_range_node;
int sys_place_range_StallCPUS = 0;
static unsigned *nextPageFrame;

static char *protocol;

#define SYS_PLACE_RANGE_QUIESCENT_TIME	100000
#define SYS_PLACE_RANGE_MAX_TRIES	15

void 
SoloInitPageTranslation(void)
{
   InitMemAllocation();
   InitPageTrans();

   ParamLookup(&protocol, "MEMSYS.FLASH.Protocol", PARAM_STRING);
}


void
Solosys_place_range(VA start, VA stop, int node)
{
   SoloPA pa;
   VA 	va;
   int oldNode;
   uint flavor;
   int i;

#ifdef DEBUG_PAGE_VERBOSE
   CPUPrint( "sys_place_range %d: Allocating range %x to %x\n",
      node, start, stop);
#endif
   pa = 0LL;
   va = start & SOLO_PAGE_NUMBER_MASK;
   do {
      if (!GetPhysicalAddr(va, &pa, &flavor))   {
	 ASSERT(node < totalProcs);
         pa = AllocatePage(node, va);
	 NewMapping( va, pa, TLB_CACHED);
      } else {
         oldNode = SOLO_PA_NODE(pa);
	 if (oldNode != node) {

#ifdef DEBUG_PAGE_VERBOSE
	    CPUWarning("sys_place_range %u: Moving page %x (%llx) from %d to %d\n", 
                       node, va, pa, oldNode, node);
#endif

	    sys_place_range_StallCPUS = 1;
				/* Tell all the CPU's not to do anything */
	    sys_place_range_tries = 0;
	    sys_place_range_start = va;
	    sys_place_range_stop = stop;
	    sys_place_range_node = node;

	    sys_place_range_callback.active = 0;
	    EventDoCallback(soloCPUNum, TrySysPlaceRange,
			    &sys_place_range_callback, 0,
			    SYS_PLACE_RANGE_QUIESCENT_TIME);
	    break;		/* TrySysPlaceRange will loop for me */
	 }
      }
      va += SOLO_PAGE_SIZE;
   } while (va < stop);
}
 
static void
TrySysPlaceRange(int cpuNum, EventCallbackHdr *hdr, void *v)
{
   SoloPA pa, currentPA;
   PA realPA;
   FLASHAddress transit;
   VA 	va, currentVA;
   int cpu, i, j, result, wasDirty;
   int oldNode;
   uint flavor;
   int node = sys_place_range_node;

#define LINE_BUFFER_SIZE 1024
   char data[LINE_BUFFER_SIZE];

#ifdef DEBUG_PAGE_VERBOSE
   CPUPrint( "TrySysPlaceRange %d: Moving VA range %8.8x to %8.8x\n",
	     node, sys_place_range_start, sys_place_range_stop);
#endif

   ASSERT(LINE_BUFFER_SIZE >= SCACHE_LINE_SIZE);

   /* Check MHT's to see if anything is outstanding */
   for (i=0; i<soloActiveProcs; i++) {
      for (j = 0; j < MHT_SIZE; j++) {
         if (CACHE[i].MHT[j].inuse) {
	    CPUPrint("sys_place_range %u: wakeup %d found MHT %d, entry %d in use\n",
		     cpuNum, sys_place_range_tries, i, j);
	       sys_place_range_tries++;

	       if (sys_place_range_tries >= SYS_PLACE_RANGE_MAX_TRIES) {
		  CPUError("sys_place_range %u: too many tries with MHT full\n",cpuNum);
	       }
	       EventDoCallback(cpuNum, TrySysPlaceRange,
			       &sys_place_range_callback, 0,
			       SYS_PLACE_RANGE_QUIESCENT_TIME);
	       return;
	 }
      }
   }

   /* Ok, MHT's seem clean, so let's clean up the caches. Now when I find mappings
      already established, I go ahead and move them.  Grab on to your socks.*/
   
   ASSERT(node < totalProcs);
   va = sys_place_range_start;
   do {
      if (!GetPhysicalAddr(va, &pa, &flavor))   {
         pa = AllocatePage(node, va);
	 NewMapping( va, pa, TLB_CACHED);
      } else {
         oldNode = SOLO_PA_NODE(pa);
	 if (oldNode != node) {
	    for (cpu=0; cpu<soloActiveProcs; cpu++) {
	       for (currentPA = pa, currentVA = va;
		    currentPA < pa + SOLO_PAGE_SIZE;
		    currentPA += SCACHE_LINE_SIZE,
		       currentVA += SCACHE_LINE_SIZE) {
		  realPA = SoloCompressAddr(currentPA);
		  if (CacheExtract(cpu, realPA, SCACHE_LINE_SIZE,
				   &wasDirty, (char *)data)) {
		     if (wasDirty) {
			bcopy(data, (char *)currentVA, SCACHE_LINE_SIZE); 
                        if (!strcmp(protocol,"COMA")) {
                           transit.ll = currentPA;
                           SysPlaceRangeCOMAHack(cpu, transit);
                        }
		     }
		  }
	       }
	    }

	    /* Do the actual remapping operation */
	    CPUPrint("sys_place_range %u: Moving page %x (%llx) to %d\n",
		     cpuNum, va, pa, node);
            if (!RemoveMapping(va)) {
               CPUError("RemoveMapping failed though mapping exists!\n");
            }

	    pa = AllocatePage(node, va);
	    NewMapping(va, pa, TLB_CACHED);
	    CPUPrint("sys_place_range %u: VA %x now at PA %llx\n",
		     cpuNum, va, pa);
	 }
      }
      va += SOLO_PAGE_SIZE;
   } while (va < sys_place_range_stop);

   sys_place_range_StallCPUS = 0;
}
   

/* ****************************************************************************/
/* *   V_to_P  maps virtual to physical addresses                            **/
/* *            - global cacheble space assigned round-robin                 **/
/* *            - private cacheable space assigned to home cluster           **/
/* ****************************************************************************/
SoloPA
SoloV_to_P(int proc, VA va, int clus, bool isFrame, uint *tlbFlavor)
{
   SoloPA pa;
   bool	retVal;
   uint flavor;


#ifdef DEBUG_PAGE_VERBOSE
   CPUPrint( "V_to_P %u: Translating %x", proc, va);
#endif

#ifdef T5_MODEL
   pa = (LL) va;
#ifdef DEBUG_PAGE_VERBOSE
   CPUPrint( "\nV_to_P %u: Returning PA==VA\n", proc);
#endif
   return pa;
#else   
   pa = 0LL;

   if (va < MINIMUM_VA_VALUE) {
      CPUError("\nV_to_P %d: Got virtual address 0x%x\n", proc,va);
   }
      /* Check for references to other spaces */
   if (va & VIRTUAL_MSG_MASK) {
      /* This is MSG space */
#ifdef USE_FLASHLITE
	 retVal = GetPhysicalAddr((va & 0x7fffffff), &pa, &flavor);
	 ASSERT(retVal); ASSERT(flavor == TLB_CACHED);
         pa |= (MSG << SOLO_PA_SPACE_SHIFT);
	 *tlbFlavor = TLB_UNCACHED;
	 flavor = TLB_UNCACHED; /* this is so the (*tlbFlavor) below */
                                /* doesn't squash the uncached setting. */
#ifdef DEBUG_PAGE_VERBOSE
      CPUPrint("\nMSG_SPACE Returning translation %16.16llx\n",
	       pa);
#endif
#else
      CPUError("\nBad access to VA 0x%x\n",va);
#endif
   } else if (!GetPhysicalAddr(va, &pa, &flavor)) {
      int node;
      if (isFrame == FALSE)  {  /* global */
	 if (allocationPolicy == ROUND_ROBIN) {
	    nextclus = ((va & SOLO_PAGE_NUMBER_MASK) >> SOLO_PAGE_SHIFT) &
	       (totalProcs - 1);
	    node = nextclus;
	 } else {  /* first touched */
	    node = clus;
	 }
      } else {					/* private */
	 node = proc;
      }
      ASSERT(node < totalProcs);
      pa = AllocatePage(node, va);
      NewMapping(va, pa, TLB_CACHED);
      flavor = TLB_CACHED;
   }
   (*tlbFlavor) = flavor;

#ifdef DEBUG_PAGE_VERBOSE
   CPUPrint( "Resulting translation --> %16.16llx\n", pa);
#endif

   return pa;
#endif /* T5_MODEL */

}

bool
SoloPageHasBeenMapped(VA vaddr)
{
   SoloPA paddr;
   uint flavor;

   return (GetPhysicalAddr(vaddr, &paddr, &flavor));
}

void
SoloEstablishMapping(SoloPA pa, VA va, int space, uint tlbFlavor)
{
   SoloPA addr;
   uint flavor;
   /* Make sure we don't have a normal mapping of this virtual address */
   /* to some other physical address */
   if (GetPhysicalAddr(va, &addr, &flavor)) {
#ifdef DEBUG_PAGE_VERBOSE
      CPUPrint( "EstablishMapping: flushing old mapping!\n");
#endif
      if (!RemoveMapping(va)) {
	 CPUError("RemoveMapping failed though old mapping exists!\n");
      }
   }

   addr = SOLO_FORM_PA(SOLO_PA_NODE(pa),space,SOLO_PA_OFFSET(pa));
                  /* Be sure space is set in mapping */
   NewMapping(va, addr, tlbFlavor);
}

int
SoloGetHome(unsigned vaddr)
{
   SoloPA paddr;
   uint flavor;

   if (GetPhysicalAddr(vaddr, &paddr, &flavor) == FALSE) {
      return -1;
   }
   else {
      return SOLO_PA_NODE(paddr);
   }
}

/* *************************************************************************** */
/* *  AllocatePage allocates the next page of cacheable space           * */
/* *************************************************************************** */

SoloPA
AllocatePage(int module, VA va)
{
   VA pageFrame;
   unsigned numFrames;
   PA offset;

   /* Used for COMA only */
   LL current, upperBound, lowerBound;
   unsigned reservedMemoryPerNode = 0;

   if (!strcmp(protocol,"COMA")) {
      ParamLookup(&reservedMemoryPerNode, "MEMSYS.FLASH.ReservedMemoryPerNode",
                  PARAM_INT);
   }

   ASSERT(module < totalProcs);

   numFrames = ONE_MEGABYTE >> SOLO_PAGE_SHIFT;

   /*   pageFrame = (va & (ONE_MEGABYTE - 1)) >> SOLO_PAGE_SHIFT;*/

   if (nextPageFrame[module] >= soloLockBase) {
      nextPageFrame[module] = 0;
   }

   pageFrame = nextPageFrame[module];
   nextPageFrame[module] = (nextPageFrame[module] + 1) % 
     (ONE_MEGABYTE >> SOLO_PAGE_SHIFT);

   ASSERT(cachepage[(module * numFrames) + pageFrame] !=
      (totalMemPerProc/ONE_MEGABYTE));

   cachepage[(module * numFrames) + pageFrame]++;

#define LOG_ONE_MB 20
   if (!strcmp(protocol,"COMA")) {
      if (reservedMemoryPerNode > 0) {
         lowerBound = (LL)((module*reservedMemoryPerNode)%totalMemPerProc);
         upperBound = (LL)(((module*reservedMemoryPerNode)%totalMemPerProc) + 
                           reservedMemoryPerNode);
         
#ifdef DEBUG_PAGE_VERBOSE
         CPUPrint("LOOK:  lower reserved bound is %llx, "
                  "curr = %llx, upper bound is %llx\n", lowerBound,
                  ((LL)(((cachepage[(module * numFrames) + pageFrame] - 1) << 
                         LOG_ONE_MB) + (pageFrame << SOLO_PAGE_SHIFT))),
                  upperBound);
#endif
         while ((lowerBound <=
                 (((cachepage[(module * numFrames) + pageFrame] - 1) << LOG_ONE_MB) + 
                  (pageFrame << SOLO_PAGE_SHIFT) + applicationMemoryStart)) && 
                ((((cachepage[(module * numFrames) + pageFrame] - 1) << LOG_ONE_MB) + 
                  (pageFrame << SOLO_PAGE_SHIFT) + applicationMemoryStart) <
                 upperBound)) {
            /* complicated, but the gist of it is that you fell into 
               the reserved memory portion of memory.  
               increment the stripe so that this is no longer true */
#ifdef DEBUG_PAGE_VERBOSE
            CPUPrint("LOOK:  lower reserved bound is %llx, "
                     "curr = %llx, upper bound is %llx\n",lowerBound,
                     ((LL)(((cachepage[(module * numFrames) + pageFrame] - 1) << 
                            LOG_ONE_MB) + (pageFrame << SOLO_PAGE_SHIFT) +
                           applicationMemoryStart)),
                     upperBound);
            CPUPrint("NextCacheablePage reserved memory conflict: "
                     "module = %d, pageFrame = %X, count = %x, MemPerProc = %X\n", 
                     module, pageFrame, cachepage[(module * numFrames)+pageFrame], 
                     totalMemPerProc); 
#endif
            ASSERT(cachepage[(module * numFrames) + pageFrame] !=
                   (totalMemPerProc/ONE_MEGABYTE));
            cachepage[(module * numFrames) + pageFrame]++;
         }
      }
   }
#ifdef DEBUG_PAGE_VERBOSE
   CPUPrint( "AllocatePage: module = %d, pageFrame = %X, count = %x, MemPerProc = %X\n", module, pageFrame, cachepage[(module * numFrames)+pageFrame],
      totalMemPerProc);
#endif

   offset = (((PA)(((cachepage[(module * numFrames) + pageFrame] - 1) << 20)
      + (pageFrame << SOLO_PAGE_SHIFT))) + applicationMemoryStart) + 
         (va & SOLO_PAGE_OFFSET_MASK);

   if ((PA) offset > (PA)soloLockBase) {
      CPUError("Mipsy: application brk() would have encroached on lock space:\nCurrent heap top: %8.8x  soloLockBase: %8.8x\n",
	       offset, soloLockBase);
   }
   
   return SOLO_FORM_PA(module,0,offset);

}


/* *************************************************************************** */
/* *  InitMemAllocation initializes the per-module page counters             * */
/* *************************************************************************** */

static void
InitMemAllocation(void)
{
   unsigned i;
   unsigned j;
   unsigned numFrames;
   int membits;
   char *buf;

   applicationMemoryStart = 0;
   totalProcs = TOTAL_CPUS;

   ParamLookup(&totalMemPerProc, "MEMSYS.FLASH.TotalMemPerProc", PARAM_INT);
                               
              
    /*
    * Compute values needed for compress/decompress of Solo 64 bit addresses
    * into 32bit PAs. We do this by dividing the 32 bits into a node and
    * memory offset field.  To preserve the cache coloring we do we require
    * that the memory offset value at least enough bits for the cache color.
    */
   membits = NumBits(totalMemPerProc+applicationMemoryStart);
   if (membits < NumBits(ONE_MEGABYTE-1)) {
      membits = NumBits(ONE_MEGABYTE-1);
   }
   if ((NumBits(totalProcs)+membits) > sizeof(PA)*8) {
      CPUError("%d processors with %d bytes of memory is too much\n",
               totalProcs, totalMemPerProc);
   }
   soloPACompressNodeShift = membits;
   soloPACompressOffsetMask = (1 << soloPACompressNodeShift)-1;
   soloTotalMemory = totalMemPerProc;
   soloLockBase = totalMemPerProc - 64*1024;
                                /* Reserve 32K for space 1 */
   soloBarrierBase = totalMemPerProc - 32*1024;
                                /* Reserve 32K for space 2 */

   ParamLookup(&buf, "MEMSYS.FLASH.PageAllocationPolicy", PARAM_STRING);
   if (strcasecmp(buf, "ROUND_ROBIN") == 0) { 
      allocationPolicy = ROUND_ROBIN;
   } else if (strcasecmp(buf, "FIRST_TOUCHED") == 0) {
      allocationPolicy = FIRST_TOUCHED;
   } else {
      CPUError("Unknown page allocation policy %s\n", buf);
   }

   numFrames = ONE_MEGABYTE >> SOLO_PAGE_SHIFT;
   cachepage = (unsigned *) malloc (numFrames*sizeof(int)*totalProcs);
   if (cachepage == (unsigned *) NULL) {
      CPUError("Out of memory in SoloInitMemAllocation");
   }
   nextPageFrame = (unsigned *) malloc(sizeof(unsigned)*totalProcs);
   if (nextPageFrame == (unsigned *) NULL) {
      CPUError("Out of memory in SoloInitMemAllocation");
   }

   /* starting page for each cluster's cacheable space */
   for (i = 0; i < totalProcs; i++)   {
      /* NOTE: This isn't so good for P > 256 processors since numFrames
       * is 256 */
      nextPageFrame[i] = ((numFrames/totalProcs) % numFrames) * i;
      for (j=0; j < numFrames; j++) {
	 cachepage[(i * numFrames) + j] = 0;
      }
   }
}


typedef struct mapping_struct mapping_pair;

struct mapping_struct
{ 
   VA     va;           /* virtual address */
   SoloPA pa;		/* physical address */
   int flavor;		/* flavor of mapping */
   mapping_pair *v_next;	/* ptr to next virtual mapping_pair; 
				   nil if last mapping_pair */
   mapping_pair *p_next;	/* ptr to next physical mapping_pair; 
				nil if last mapping_pair */
};



static mapping_pair *v_map[MAPPING_HASH_SIZE];	/* array of lists that make up tlb */
static mapping_pair *p_map[MAPPING_HASH_SIZE];	/* array of lists for reverse map */

/* WARNING -> this routine is terribly slow due to the compiler not
   handling long long worth squat. I've hard coded the routine in
   assembly */
/*
#define mapping_hash(_a) (((_a) >> SOLO_PAGE_SHIFT) % MAPPING_HASH_SIZE)
*/
extern unsigned mapping_hash(unsigned long long addr);


/* InitPageTrans initializes address translation.  This is not necessary if   */
/* a map is about to be read in, as ReadMap also performs the necessary   */
/* initialization.                                                        */

static void
InitPageTrans(void)
{
   int i;
   
   for (i = 0; i < MAPPING_HASH_SIZE; i++) {
      v_map[i] = 0;
      p_map[i] = 0;
   }
}

/* NewMapping takes care of the map hash table for a new virtual-to-     */
/* physical mapping.  It inserts the new mapping at the head of the      */
/* appropriate virtual hash bin.  The user is responsible for ensuring   */
/* that the mapping does not already exist before calling this routine.  */

void
NewMapping(VA v_addr, SoloPA p_addr, uint flavor)
/* cpu #(private); SHAREDBLK(shared) */
/* virtual address */
/* physical block number */
/* address space type */
{    
   VA	my_virtual;
   SoloPA	physical;
   mapping_pair	*current;
   unsigned	v_hash_index, p_hash_index;
   VA		alignedVA;
   FLASHAddress	alignedPA;

#ifdef DEBUG_PAGE_VERBOSE
   CPUPrint("New Mapping: va %#x pa %#16.16llx\n",
            v_addr, p_addr);
#endif
   my_virtual = v_addr & SOLO_PAGE_NUMBER_MASK;
   physical = p_addr & (SoloPA)SOLO_PAGE_NUMBER_MASK_LONG;
   
   /* create new mapping mapping_pair */
   
   if ((current = (mapping_pair *) malloc(sizeof(mapping_pair))) == 0) {
      CPUError("malloc of new mapping mapping_pair failed\n");
   }
   current->va = my_virtual;
   current->pa = physical;
   current->flavor = flavor;
   
   /* insert new mapping into hash table */
   
   v_hash_index = mapping_hash(my_virtual);
   current->v_next = v_map[v_hash_index];
   v_map[v_hash_index] = current;
   
   p_hash_index = mapping_hash(physical);

   current->p_next = p_map[p_hash_index];
   p_map[p_hash_index] = current;

   
#ifdef DEBUG_PAGE_VERBOSE
   CPUPrint("New Mapping: VA: %8.8x  PA: %16.16llx\n",
	     v_addr, p_addr);
#endif
 
}

/* GetPhysicalAddr performs a virtual-to-physical address translation  */
/* using the map hash table.  TRUE is returned if the translation      */
/* was successful.                                                     */

bool
GetPhysicalAddr(VA v_addr, SoloPA *p_addr, uint *flavor)
{
   VA	my_virtual;
   mapping_pair	*current;
   unsigned  hash_index;
   
   my_virtual = v_addr & SOLO_PAGE_NUMBER_MASK;
   hash_index = mapping_hash(my_virtual);
   
   /* search for this virtual page's mapping in hash table */
   
   current = v_map[hash_index];
   while (current != 0)   {
      if (current->va == my_virtual) {
	 *p_addr = current->pa + (v_addr & SOLO_PAGE_OFFSET_MASK);
         *flavor = current->flavor;
         return TRUE;
      }
      current = current->v_next;
   }
   return FALSE;
}



/* GetMemoryAddr performs a physical-to-virtual address translation  */
/* using the map hash table.  TRUE is returned if the translation      */
/* was successful.                                                     */

char *
SoloGetMemoryAddr(SoloPA p_addr)
{
   SoloPA	physical;
   mapping_pair	*current;
   unsigned	hash_index;
   unsigned	offset;
   VA           v_addr;

   /* ADDED PURELY BECAUSE OF THE COMA PROBLEM
    * I DESCRIBE BELOW
    */
   unsigned     fake_coma_i = 0;
   unsigned     fake_coma_offset;
   unsigned     fake_coma_space = 0;
   SoloPA       new_node_id;
/*
   // because we initialize all directory entries to be EXCL at
   // (fake) boot time, when you access data, it may return and force
   // displacement of something that has not even been accessed
   // yet (this displaced data would have its state at EXCL due to
   // the initialization, so it would be thought that replacement is
   // necessary).  In this case, it will not have a valid VA->PA translation
   // so the above test will fail and the simulation will die
   // unnecessarily.  The hack I am using to avoid this is to pick
   // a different PA to access if the current one has no translation.
   // We print a warning to make sure that if it is a real bug we
   // catch it.
*/

   physical = p_addr & (SoloPA)SOLO_PAGE_NUMBER_MASK_LONG;
   hash_index = mapping_hash(physical);

#ifdef DEBUG_PAGE_VERBOSE
   CPUPrint("GetVirtualAddr: physical: %16.16llx\n",
	    p_addr);
#endif

#ifdef T5_MODEL
   v_addr = (VA) (p_addr & 0xffffffffLL);
   /* Extract low bits... */
   CPUPrint("GetVirtualAddr: Returning VA==PA: %8.8x\n",
	    v_addr);
   return (char*) v_addr;
#else

   /* search for this virtual page's mapping in hash table */
   
   current = p_map[hash_index];
   while (current != 0)   {
#ifdef DEBUG_PAGE_VERBOSE
      CPUPrint("GetVirtualAddr: VA: %8.8x  PA: %16.16llx args: physical %llx\n",
               current->va, current->pa, physical);
#endif
      if (current->pa == physical) {
	 v_addr = current->va;
	 offset = (unsigned) (p_addr & (SoloPA)SOLO_PAGE_OFFSET_MASK_LONG);
	 v_addr |= offset;
         return (char *) v_addr;
      }
      else current = current->p_next;
   }

   if (!strcmp(protocol,"COMA")) {

      /*   CPUWarning("Bad argument passed to GetMemoryAddress\n");*/
      /*
        // CPUWarning("Bad argument passed to GetMemoryAddress, addr = %16.16llx\n",
        // physical);
        // COMA HACK...SEE COMMENT AT TOP OF FUNCTION
        // we actually choose a known valid VA so we do
        // not accidentally overwrite a valid PA.  We
        // should do extra checks to make sure we are
        // doing this because of our replacement problem
        // rather than because there is an actual bug
        */
      while (fake_coma_i < totalProcs){
         fake_coma_space = SOLO_PA_SPACE(p_addr);
         fake_coma_offset = SOLO_PA_OFFSET(p_addr);
         /* we choose a different physical address:  same node number, */
         /* CPUPrint("Checking for COMA rplc case, node %u, curr_addr = %16.16llx\n",
            fake_coma_i, physical); */
         p_addr = SOLO_FORM_PA(fake_coma_i, fake_coma_space, fake_coma_offset);
         physical = p_addr & (SoloPA)SOLO_PAGE_NUMBER_MASK;
         hash_index = mapping_hash(physical);
         
         /* search for this virtual page's mapping in hash table */
         
         current = p_map[hash_index];
         while (current != 0)   {
            if (current->pa == physical) {
               v_addr = current->va;
               offset = (unsigned) (p_addr & (SoloPA)SOLO_PAGE_OFFSET_MASK);
               v_addr |= offset;
               /* return (char *) v_addr; */
               /* here, we realize this is probably due to the
                * COMA replacement problem, so we return the
                * fake VA that we set up above
                */
               return (char *)&(junk.comaHACKADDR);
               
            }
            else current = current->p_next;
         }
         fake_coma_i++;
      }
   }
   return NULL;
#endif /* T5_MODEL */
   
}

static int
NumBits(unsigned int number)
{
   int i;
   for (i = 0; i < 32; i++) {
      if ((1 << i) > number) 
         return i;
   }
   return 32;
}

/* RemoveMapping removes an existing mapping for a VA. */

static bool
RemoveMapping(VA v_addr)
{
   VA	my_virtual;
   mapping_pair	*current, *prev;
   int	hash_index;
   bool		found;
   
   my_virtual = v_addr & SOLO_PAGE_NUMBER_MASK;
   hash_index = mapping_hash(my_virtual);
   
   /* search for this virtual page's mapping in hash table */
   
   current = v_map[hash_index];
   prev = 0;			/* Indicate first element is head */
   found = FALSE;
   while ((!found) && (current != 0))   {
      if (current->va == my_virtual) {
	 found = TRUE;
	 break;
      } else {
	 prev = current;
	 current = current->v_next;
      }
   }
   if (found) {
#ifdef DEBUG_PAGE_VERBOSE
      CPUPrint("RemoveMapping: VA: %8.8x  PA: %16.16llx\n",
	       current->va, current->pa);
#endif
      if (prev == 0) {
	 v_map[hash_index] = current->v_next;
         /*  Remove current from hash bucket head */
      } else {
	 prev->v_next = current->v_next;
         /* Remove current from prev's chain */
      }
   }
   return found;
}