clock.c 15.4 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
/*
 * 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. 
 *
 */

 /****************************************************************
 * clock.c
 * 
 * $Author: blythe $
 * $Date: 2002/05/29 01:09:10 $
 *****************************************************************/
#include <stdio.h>
#include <string.h>
#include "simmisc.h"
#include "checkpoint.h"
#include "simmagic.h"
#include "sips.h"
#include "eventcallback.h"
#include "simutil.h"
#include "embra.h"
#include "cp0.h"
#include "clock.h"
#include "main_run.h"
#include "mem_control.h"
#include "stats.h"
#include "callout.h"
#include "translator.h"
#include "debug.h"
#include "driver.h"
#include "hw_events.h"
#include "embra_interface.h"

/* **********************************************************************
 * All of the clock shared state is in shared memory. All of the callbacks
 * must be in shared memory, event non IPI as the callbacks themselves are
 * queued.
 * **********************************************************************/

#define MAX_INTERRUPT_CALLBACKS (16*SIM_MAXCPUS)


typedef struct ClockSharedState {
    EventCallbackHdr interrupts[MAX_INTERRUPT_CALLBACKS];
    int rotor;
    int lock;
    EventCallbackHdr pcSampling[SIM_MAXCPUS];
    EventCallbackHdr periodicCallback[SIM_MAXCPUS];
    EventCallbackHdr periodicAnnotationCallback;
    EventCallbackHdr statCallback[SIM_MAXCPUS];
    EventCallbackHdr bootCallback;
    EventCallbackHdr exitCallback;
} ClockSharedState;

extern K0A non_excepting_tv( int cpuNum, VA va );

static ClockSharedState *clockState;
/* Local Data */
static int pc_sample_interval = 256;

int callout_interval = 0;
/* Local Functions */
static void InitPeriodicCallbacks(int cpu);
static void StatCallback(int cpuNum, EventCallbackHdr *hdr,void *arg);
static void PeriodicCallback(int cpuNum, EventCallbackHdr *hdr,void *arg);
static void PeriodicAnnotationCallback(int cpuNum, EventCallbackHdr *hdr,void *arg);
static void BootCallback(int cpuNum, EventCallbackHdr *hdr,void *arg);
static SimTime next_PCSample;

static void InitPeriodicCallbacks(int cpu)
{
   ASSERT (cpu==0);
  if (embra.stats) {
     EventDoCallback(cpu,StatCallback,
                     &clockState->statCallback[cpu],0,
                     embra.statInterval);
  }
  EventDoCallback(cpu,PeriodicCallback,
                  &clockState->periodicCallback[cpu],0,
                  embra.miscCheckInterval);

  if (cpu == 0) {
     EventDoCallback(0,PeriodicAnnotationCallback,
                     &clockState->periodicAnnotationCallback,0,
                     embra.periodicAnnInterval);
  }
}


void EmbraClosePeriodicCallbacks(void)
{
   int cpu = 0;
   if (EventCallbackActive(&clockState->statCallback[cpu])) {
      EventCallbackRemove(&clockState->statCallback[cpu]);
   }
   if (EventCallbackActive(&clockState->periodicCallback[cpu])) {
      EventCallbackRemove(&clockState->periodicCallback[cpu]);
   }
   if (EventCallbackActive(&clockState->periodicAnnotationCallback)) { 
      EventCallbackRemove(&clockState->periodicAnnotationCallback);
   }
}



void
Embra_Clock_Init(int cpuNum)
{

   /*
    * allow fast check from assembly 
    */

   callout_interval = embra.timeQuantum;

   if( embra.MPinUP ) {
      int i;
      EMP[TOTAL_CPUS].eventQueueTimePtr = SingleEventQueueCalltimeAddr;
      for( i = 0; i <= TOTAL_CPUS; i++) {
         EMP[i].timeQuantum = embra.timeQuantum;
         EMP[i].cycleCountdown = EMP[i].timeQuantum;
      }
      InitPeriodicCallbacks(0);
   } else {
      ASSERT (!cpuNum);
      ASSERT( TOTAL_CPUS ==1);
      EMP[0].eventQueueTimePtr = SingleEventQueueCalltimeAddr;
      EMP[0].timeQuantum       = embra.timeQuantum;
      EMP[cpuNum].cycleCountdown = embra.timeQuantum;
      InitPeriodicCallbacks(cpuNum);
   }
}

int
Update_And_Check_Interrupts( int cpuNum , VA targetPC)
{
   /*CPUWarning("CPU %d Update and Check HW 0x%x\n ", 
            cpuNum,
            EMP[cpuNum].intrBitsPtr[0] ); */
   /* Update and check for interrupts */

   EMP[cpuNum].CP0[C0_CAUSE] = 
      (EMP[cpuNum].CP0[C0_CAUSE] & ~CAUSE_EXTINTBITS) | 
      ((EMP[cpuNum].intrBitsPtr[0] << CAUSE_IPSHIFT) & CAUSE_EXTINTBITS);

   if (((EMP[cpuNum].CP0[C0_CAUSE] & EMP[cpuNum].CP0[C0_SR]) & SR_IMASK) &&
        (EMP[cpuNum].CP0[C0_SR] & SR_IEC) &&
       !((EMP[cpuNum].CP0[C0_SR] & (SR_EXL|SR_ERL)))) {

      /* Processors generally take excpetions in between instructions */
      /* so when  we are stalled for a cache miss (or whatever), delay */
      /* excpetion (interrupt) detection */
         if( !EMP[cpuNum].stalled ) {
            if (targetPC) { 
               ASSERT (!IN_BD(targetPC));
               ASSERT (IS_KSEG0(targetPC) || IS_KUSEG(targetPC));

               /*
                * now is the time to field the post-pc annotation,
                * since we are skipping the instruction by smashing
                * the PC.
                */
               ASSERT (cpuNum==curEmp->myNum);
               AnnExec(AnnFMLookup(CLEAR_BD(EMP[cpuNum].PC),ANNFM_PC_TYPE));
               EMP[cpuNum].PC = targetPC;
            }
            Em_EXCEPTION(cpuNum,EXC_INT,0);
            EmbraSideEffect();
            return 1;
         }
   }
   return 0;
}

void EmIntrBitsChanged(int cpuNum)
{
   Update_And_Check_Interrupts(cpuNum,0);
}


/* Do Interrupt Now (only takes exception if interrupt is enabled) */

static void EmbraInterruptCallback(int cpuNum, EventCallbackHdr *hdr, void *arg)
{
   IEC intrtoset = (IEC) (int) arg;   /* keep compiler quiet by casting thru int */

   SIM_DEBUG(('i',"DEV %d %lld Send 0x%x ", 
              cpuNum, EmbraCpuCycleCount(cpuNum), intrtoset));
   RaiseIBit(cpuNum, intrtoset);
   SIM_DEBUG(('i', "Ibits 0x%x\n", EMP[cpuNum].intrBitsPtr[0]));

   Update_And_Check_Interrupts( cpuNum,0 );
}

/* Do deliver SIPS Now */
static void EmbraDeliverSIPSCallback(int cpuNum, EventCallbackHdr *hdr,void *arg)
{
   sim_sips_deliver(cpuNum, (int)arg /* chan */);
   Update_And_Check_Interrupts( cpuNum ,0);
}

/* Do Interrupt Now (only takes exception if interrupt is enabled) */
static void BootCallback(int cpuNum, EventCallbackHdr *hdr,void *arg)
{
   int cpu;
   for( cpu = 1; cpu < TOTAL_CPUS; cpu++ ) {
      uint launchAddr = (uint)sim_misc.launchAddr[cpu];
      if( launchAddr ) {
        /* XXX - non-backdoor address is launch, backdoor address is call */
         if( IS_BACKDOOR(launchAddr) ) {
            int64 result;
            /* Do call */
            result = ((int64 (*)(int,int,int,int))launchAddr)
               ( sim_misc.launchArg[cpu][0],
                 sim_misc.launchArg[cpu][1],
                 sim_misc.launchArg[cpu][2],
                 sim_misc.launchArg[cpu][3] );

            if( result == SLAVELOOP_CONTINUE ) {
               /* signal init function done */
               sim_misc.launchAddr[cpu] = 0;
               /* clear so will be 0 if not set up on next call */
               sim_misc.launchArg[cpu][0] = 0;
               sim_misc.launchArg[cpu][1] = 0;
               sim_misc.launchArg[cpu][2] = 0;
               sim_misc.launchArg[cpu][3] = 0;
               CPUPut("Slave %d returning to launch wait\n",cpu );
            }
         } else {
            /* Add everybody in & let mem_control handle launch */
            int i;
            for( i = 0; i <= TOTAL_CPUS; i++ ) {
               EMP[i].next = &EMP[(i+1)%(TOTAL_CPUS+1)];
            } /* for */
            /* Don't reinstall callback */
            return;
         }
      }
   }
   EventDoCallback(cpuNum,BootCallback,hdr,0,embra.timeQuantum);
}

void Embra_Send_Interrupt( int cpuNum, IEC intrtoset, SimTime delay )
{    
    int count = 0;
    EventCallbackHdr *event;

    if( delay == 0 ) {
       /* SIMLOCK();*/ 
       RaiseIBit(cpuNum, intrtoset);
       /* SIMUNLOCK();*/
       Update_And_Check_Interrupts( cpuNum,0 );
    } else {
       clockState->rotor = (clockState->rotor+1)%MAX_INTERRUPT_CALLBACKS;
       while( EventCallbackActive(&clockState->interrupts[clockState->rotor]) ){
          count++;
          ASSERT( count < MAX_INTERRUPT_CALLBACKS  );
          clockState->rotor = (clockState->rotor+1)% MAX_INTERRUPT_CALLBACKS;
       }
       event = &clockState->interrupts[ clockState->rotor];
       EventDoCallback(cpuNum,EmbraInterruptCallback,event,(void *)(int)intrtoset,delay);
    }
}

void Embra_Deliver_SIPS( int cpuNum, int chan, SimTime delay )
{    
    int count = 0;
    EventCallbackHdr *event;

    clockState->rotor = (clockState->rotor+1)%MAX_INTERRUPT_CALLBACKS;
    while( EventCallbackActive(&clockState->interrupts[clockState->rotor]) ){
        count++;
        ASSERT( count < MAX_INTERRUPT_CALLBACKS  );
        clockState->rotor = (clockState->rotor+1)% MAX_INTERRUPT_CALLBACKS;
    }
    event = &clockState->interrupts[ clockState->rotor];
    EventDoCallback(cpuNum,EmbraDeliverSIPSCallback,event,(void *)chan,delay);
}

static void StatCallback(int cpuNum, EventCallbackHdr *hdr,void *arg)
{
   Print_Recent_Stats(cpuNum);
   EventDoCallback(cpuNum,StatCallback,hdr,0,embra.statInterval);
}


static void PeriodicCallback(int cpuNum, EventCallbackHdr *hdr,void *arg)
{
#if 0
    CPUPrint("callback %i %lld\n",cpuNum,CPUVec.CpuCycleCount(cpuNum));
#endif
    EventDoCallback(cpuNum,PeriodicCallback,hdr,0,embra.miscCheckInterval);

    EmbraPollSigUsr(cpuNum);


    if( sim_misc.myCPUType != sim_misc.enterThisCPU) {
       EmbraExit(sim_misc.enterThisCPU);
    }

}

static void PeriodicAnnotationCallback(int cpuNum, EventCallbackHdr *hdr,void *arg)
{
   ASSERT(cpuNum == 0);
   EventDoCallback(0,PeriodicAnnotationCallback,hdr,0,embra.periodicAnnInterval);
   PERIODIC_EVENT;
}


/* ****************************************************************
 * Periodic_Callout is called very frequently. Keep it as short as 
 * possible. 
 * ****************************************************************/


void IncCC(int cpuNum, int inc)
{
   EMP[cpuNum].cycleCount += EMP[cpuNum].timeQuantum;
}

/*#define DEBUG_EVENTPOLL*/
#ifdef DEBUG_EVENTPOLL
static struct {
   uint cycleCountdown;
   uint PC;
   uint64 cycleCount;
   uint stalled;
} debug_evpoll[SIM_MAXCPUS];
static SimTime last_call;
#endif

void GeneralEmEventPoll(int cpuNum)
{
   /*CPUPrint("%d GeneralEmEventPoll\n", cpuNum);*/
   ASSERT(cpuNum < TOTAL_CPUS);
#ifdef notdef
#ifdef STALL_BY_REMOVING_FROM_LOOP
   /* This needs justification */
   /*EMP[TOTAL_CPUS].cycleCount += EMP[0].timeQuantum;*/
   /*EventPollSingleQueue(EMP[TOTAL_CPUS].cycleCount);*/
#else
   ASSERT(cpuNum == 0 );
#endif
#endif /* notdef */
   /* Value passed to EventPoll is a hint telling it how far down the
      callback queue to look.  We know all cycle counts are within 1
      quantum, so this should suffice */
#ifdef DEBUG_EVENTPOLL
   ASSERT( EmbraCpuCycleCount(0) + 4*EMP[0].timeQuantum > last_call);
#endif
   EventPollSingleQueue(EmbraCpuCycleCount(0) + 4*EMP[0].timeQuantum);
#ifdef DEBUG_EVENTPOLL
   debug_evpoll[cpuNum].cycleCountdown = EMP[cpuNum].cycleCountdown;
   debug_evpoll[cpuNum].PC = EMP[cpuNum].PC;
   debug_evpoll[cpuNum].cycleCount = EMP[cpuNum].cycleCount;
   debug_evpoll[cpuNum].stalled = EMP[cpuNum].stalled;
   last_call = EmbraCpuCycleCount(0) + 4*EMP[0].timeQuantum;
#endif
   /* some event hung off the cycle count may have
    * changed memory (eg. the debugger).  This checks that.
    */
   FlushTCIfNecessary(cpuNum);
   ReenterTC(EMP[TOTAL_CPUS].next);
   /* NOT REACHED */
}

/* This is called when a single event queue is used TOTAL_CPUS == 1 || MPinUP */
EmbraState* CEmEventPoll(void)
{

   /* Value passed to EventPoll is a hint telling it how far down the
      callback queue to look.  We know all cycle counts are within 1
      quantum, so this should suffice */
   SimTime now_ish = EmbraCpuCycleCount(0);
   if (eventQueues->calltime + 100000 < now_ish) {
      CPUWarning("CEmEventPoll missing callouts calltime=%lld, nowish=%lld \n",
                 eventQueues->calltime, now_ish);
   }
   
         
#ifdef DEBUG_EVENTPOLL
   ASSERT( now_ish >= last_call );
#endif

   if( EventPendingSingleQueue(now_ish) ) {
      EventProcessSingleQueue( now_ish );
      /* Code is written to assume that this will happen because
         exceptions can be raised in a callback */
      ReenterTC( &EMP[0] );
      /* NOT REACHED */
   }

#ifdef DEBUG_EVENTPOLL
   debug_evpoll[cpuNum].cycleCountdown = EMP[cpuNum].cycleCountdown;
   debug_evpoll[cpuNum].PC = EMP[cpuNum].PC;
   debug_evpoll[cpuNum].cycleCount = EMP[cpuNum].cycleCount;
   debug_evpoll[cpuNum].stalled = EMP[cpuNum].stalled;
   last_call = EmbraCpuCycleCount(0) + 4*EMP[0].timeQuantum;
#endif
   /* ReenterTC(EMP[TOTAL_CPUS].next);*/
   /* NOT REACHED */
   return &EMP[0];
}

void NonReturningProcReturned(void)
{
   VASSERT(0,("Non-returning procedure returned.  This is bad.\n"));
}

SimTime EmbraReadTime( void )
{
   if( embra.MPinUP ) {
      return EmbraCpuCycleCount(0);
   } else {
      return EmbraCpuCycleCount(CURR_CPU);
   }
}

/*#define DEBUG_CLOCK*/
#ifdef DEBUG_CLOCK
static struct {
   uint cycleCountdown;
   uint PC;
   uint64 cycleCount;
   uint clock_val;
} debug_clock[SIM_MAXCPUS];
#endif
SimTime EmbraCpuCycleCount(int cpuNum)
{
   static SimTime last_returned_value;
   SimTime new_value;

   new_value = EMP[cpuNum].cycleCount + 
      ( (int64)(EMP[cpuNum].timeQuantum - 
                    EMP[cpuNum].cycleCountdown ) );
   
#ifdef DEBUG_CLOCK
   if (new_value < debug_clock[cpuNum].clock_val) {
      CPUWarning("cpu %d retrograde clock %lld->%lld, staying at max\n",
                 cpuNum, debug_clock[cpuNum].clock_val, new_value);
      new_value = debug_clock[cpuNum].clock_val;
   }
   
   debug_clock[cpuNum].cycleCountdown = EMP[cpuNum].cycleCountdown;
   debug_clock[cpuNum].cycleCount = EMP[cpuNum].cycleCount;
   debug_clock[cpuNum].PC = EMP[cpuNum].PC;
   debug_clock[cpuNum].clock_val = new_value;
#endif
   return new_value;
}

/* Make the .cycleCount field the actual current time */

void EmbraFixCycleCounts(void)
{
   int cpuNum;
   SimTime maxTime = 0;
   for (cpuNum=0;cpuNum<TOTAL_CPUS;cpuNum++) { 
      EMP[cpuNum].cycleCount += EMP[cpuNum].timeQuantum - 
         EMP[cpuNum].cycleCountdown;
      EMP[cpuNum].cycleCountdown = EMP[cpuNum].timeQuantum;
      if ( EMP[cpuNum].cycleCount > maxTime) { 
         maxTime = EMP[cpuNum].cycleCount;
      }
   }
   for (cpuNum=0;cpuNum<TOTAL_CPUS;cpuNum++) { 
      EMP[cpuNum].cycleCount = maxTime;
   }
}


void EmbraClockInit(void)
{
    if( !clockState ) {
        clockState = MallocShared(sizeof(ClockSharedState),"EmClock");
    }
    ASSERT( clockState );
}

/******************************************************************
 * Write a call to the system call exit on the user's stack.  Then 
 * jump to it 
*****************************************************************/
void 
EmbraMakeProcExit(int cpuNum)
{
   EmbraState *P = &EMP[cpuNum];
   uint *pPtr;
   VA vSP;

   /* Insure that we are not writting over a page boundary by using */
   /* the beginning of the page*/  
   vSP = FORM_ADDR( PAGE_NUMBER(P->R[REG_SP]), 0 );
   pPtr = (uint*) non_excepting_tv( cpuNum, vSP );
   if( !pPtr ) {
      CPUWarning("Failure to translate stack addr in MakeProcExit\n");
      return;
   }

   CPUWarning("PROCexit: CPU %d smashing address %#x\n", P->myNum, vSP);

   *pPtr++ = CIi( addiu_op, A0, G0, 0 ); /* li a0, 0 */
   *pPtr++ = CIi( addiu_op, V0, G0, 1001 ); /* li v0, 1001 */

   /* This trick won't work in base mode both because of this opcode */
   /* because we are not flushing the data cache*/
   *pPtr++ = CIs( syscall_op, 0, 0, 0); /* syscall */
   P->PC = vSP;
   return;
}