Example #1
0
/** \brief Acquire the mutex
 *
 */
os_result os_mutexLock_s (os_mutex *mutex)
{
   Error err;
   Semaphore semaphore;
   SignedValue svalue;

   semaphore = os_os_mutexGetSem(mutex);
   err = WaitForSemaphore(semaphore);

   assert (err != ActivityIsAlreadyInUse);

   if ( err == Success )
   {
      err = GetSemaphoreValue(semaphore, &svalue);
      assert( err == Success );
      if ( svalue > 1 )
      {
         /* This is not a recursive mutex */
         err = ReleaseSemaphore(semaphore);
         assert( err == Success );
         err = Failure;
      }
   }

   return (err == Success ? os_resultSuccess : os_resultFail);
}
Example #2
0
static Error rsGetSemaphore( Semaphore *sem, int index )
{
   Error err;
   Error err2;
   static const enum Command cmd = RSGetSemaphore;

   err = WaitForSemaphore( os_connLock );
   assert ( err == Success );
   if ( err == Success )
   {
      err = Send( os_conn, (Address)&cmd, sizeof(cmd) );
      assert ( err == Success );
      if ( err == Success )
      {
         err = Send( os_conn, (Address)&index, sizeof(int) );
         assert ( err == Success );
         if ( err == Success )
         {
            err == rsReceiveReturn();
            if ( err == Success )
            {
               err = ReceiveObject( os_conn, (Object *)sem );
               assert( err == Success );
            }
         }
      }

      err2 = ReleaseSemaphore(os_connLock);
      assert ( err2 == Success );
      (void)err2;
   }
   return (err);
}
Example #3
0
//blocked if no available alive hosts
CHostObject* CHostSet::RetrieveTocken()
{
	WaitForSemaphore( m_smAlives );

	Lock();
	CHostObject* pHostObj = m_trAliveHosts.RemoveHead();	//always return the fastest computer
//	m_trRunningHosts.Insert( pHostObj );					//buffer it into the running set.
	m_lsRunningHosts.push_back( pHostObj );					//buffer it into the running list
	Unlock();

	return pHostObj;
}
Example #4
0
/**
  Wait all APs to performs an atomic compare exchange operation to release semaphore.

  @param   NumberOfAPs      AP number

**/
VOID
WaitForAllAPs (
  IN      UINTN                     NumberOfAPs
  )
{
  UINTN                             BspIndex;

  BspIndex = mSmmMpSyncData->BspIndex;
  while (NumberOfAPs-- > 0) {
    WaitForSemaphore (&mSmmMpSyncData->CpuData[BspIndex].Run);
  }
}
Example #5
0
CClientSession* CClientManager::RetrieveSession( int nAlgorithm )
{
	CServerToken* pToken = NULL;
	while( true ){
		WaitForSemaphore( m_hSema );
		{
			CObjLocker<CClientManager> locker(this);
			multiset<CServerToken*, TokenCpuCmp>::iterator pos;
			switch( nAlgorithm ){
			case CMRS_FASTEST:
				pos = m_ServTokens.end();
				advance( pos, -1 );
				break;
			case CMRS_SLOWEST:
				pos = m_ServTokens.begin();
				break;
			}
			ASSERT( pos!=m_ServTokens.end() );
			pToken = *pos;
			m_ServTokens.erase( pos );
			if( pToken->IsDead() ){			//remove the dead token
				m_PeerTokens.erase( pToken->GetServAddr() );
				pToken = NULL;
			}
		}
		if( pToken!=NULL )break;
	}

	pToken->SetState( CTS_RUNNING );
	CClientSession* pSession = new CClientSession( CSessionAddr(GetLocalInAddr(), GetNextSessionId()), pToken );

	//get the proxy pointer, create the proxy if necessary
	IN_ADDR addrServ = pToken->GetServAddr().m_addrHost;
	CClientProxy* pClntProxy = NULL;
	{
		CObjLocker<CClientManager> locker(this);
		map<IN_ADDR, CClientProxy*>::iterator pos = m_ClntProxies.find( addrServ );
		if( pos!=m_ClntProxies.end() )pClntProxy = pos->second;
		else{
			pClntProxy = new CClientProxy( CSessionAddr( GetLocalInAddr(), 0 ), addrServ );
			m_ClntProxies[addrServ] = pClntProxy;
		}
	}
	//establish the relationship of proxy and session
	pClntProxy->InsertSession( pSession );
	pSession->SetProxy( pClntProxy );

	return pSession;
}
Example #6
0
static Error rsNewSemaphore( Semaphore *sem, int *index, uint64_t *objectID )
{
   Error err;
   Error err2;
   static const enum Command cmd = RSCreateSemaphore;

   Address lr;

   err = WaitForSemaphore( os_connLock );
   assert ( err == Success );
   if ( err == Success )
   {
      err = Send( os_conn, (Address)&cmd, sizeof(cmd) );
      assert( err == Success );
      if ( err == Success )
      {
         err  = rsReceiveReturn();
         if ( err == Success )
         {
            err = ReceiveObject( os_conn, (Object *)sem );
            assert( err == Success );
            if ( err == Success )
            {
               err = Receive(os_conn, (Address)objectID, sizeof(uint64_t), &lr);
               assert( lr == sizeof(uint64_t) && err == Success );
               if ( err == Success )
               {
                  err = Receive( os_conn, (Address)index, sizeof(int), &lr );
                  assert( lr == sizeof(int) && err == Success );
               }
            }
         }
      }

      err2 = ReleaseSemaphore(os_connLock);
      assert ( err2 == Success );
      (void)err2;
   }
   return (err);
}
Example #7
0
int
ospl_main(
    int argc,
    char *argv[])
{
    u_result retVal;
    char *name;
    char *config;
#ifdef INTEGRITY
    Error err;
    Semaphore networkSvcStartSem = SemaphoreObjectNumber(13);
    name = "networking";
    config = "file:///ospl.xml";

    err = WaitForSemaphore(networkSvcStartSem);
    assert ( err == Success );

    argc = 3;
#else

#ifdef NW_DEBUGGING
    /* Stop and wait for debugger */
    if (argc > 3) {
        if (strcmp(argv[3], "wait") == 0) {
            int p = 0;
            while (p==0) {
                os_time delay = {1, 0};
                printf("Networking sleeping, waiting for debugger\n");
                os_nanoSleep(delay);
            }
        }
        argc--;
    }

    if (argc > 3) {
        int i;
        printf("Ignoring superfluous parameters: ");
        for (i=3; i<argc; i++) {
            printf(" %s", argv[i]);
        }
        printf("\n");
    }
#endif

#endif
    /* First check command line arguments */
    if (argc == 3)
    {
#ifndef INTEGRITY
        name = argv[1];
        config = argv[2];
#endif
        /* Initialize user API */
        retVal = u_userInitialise();
        if (retVal == U_RESULT_OK) {
                /* The actual service actions */
            nw_serviceMain(name, config);

            u_userDetach();
        } else {
           NW_REPORT_ERROR("networking main loop",
                           "Error attaching to kernel, bailing out");
        }
    } else {
       NW_REPORT_ERROR("networking main loop",
                       "Usage: networking name <configuration-URI>");
    }

    return 0;

}
Example #8
0
/**
  SMI handler for AP.

  @param     CpuIndex         AP processor Index.
  @param     ValidSmi         Indicates that current SMI is a valid SMI or not.
  @param     SyncMode         SMM MP sync mode.

**/
VOID
APHandler (
  IN      UINTN                     CpuIndex,
  IN      BOOLEAN                   ValidSmi,
  IN      SMM_CPU_SYNC_MODE         SyncMode
  )
{
  UINT64                            Timer;
  UINTN                             BspIndex;
  MTRR_SETTINGS                     Mtrrs;

  //
  // Timeout BSP
  //
  for (Timer = StartSyncTimer ();
       !IsSyncTimerTimeout (Timer) &&
       !mSmmMpSyncData->InsideSmm;
       ) {
    CpuPause ();
  }

  if (!mSmmMpSyncData->InsideSmm) {
    //
    // BSP timeout in the first round
    //
    if (mSmmMpSyncData->BspIndex != -1) {
      //
      // BSP Index is known
      //
      BspIndex = mSmmMpSyncData->BspIndex;
      ASSERT (CpuIndex != BspIndex);

      //
      // Send SMI IPI to bring BSP in
      //
      SendSmiIpi ((UINT32)gSmmCpuPrivate->ProcessorInfo[BspIndex].ProcessorId);

      //
      // Now clock BSP for the 2nd time
      //
      for (Timer = StartSyncTimer ();
           !IsSyncTimerTimeout (Timer) &&
           !mSmmMpSyncData->InsideSmm;
           ) {
        CpuPause ();
      }

      if (!mSmmMpSyncData->InsideSmm) {
        //
        // Give up since BSP is unable to enter SMM
        // and signal the completion of this AP
        WaitForSemaphore (&mSmmMpSyncData->Counter);
        return;
      }
    } else {
      //
      // Don't know BSP index. Give up without sending IPI to BSP.
      //
      WaitForSemaphore (&mSmmMpSyncData->Counter);
      return;
    }
  }

  //
  // BSP is available
  //
  BspIndex = mSmmMpSyncData->BspIndex;
  ASSERT (CpuIndex != BspIndex);

  //
  // Mark this processor's presence
  //
  mSmmMpSyncData->CpuData[CpuIndex].Present = TRUE;

  if (SyncMode == SmmCpuSyncModeTradition || SmmCpuFeaturesNeedConfigureMtrrs()) {
    //
    // Notify BSP of arrival at this point
    //
    ReleaseSemaphore (&mSmmMpSyncData->CpuData[BspIndex].Run);
  }

  if (SmmCpuFeaturesNeedConfigureMtrrs()) {
    //
    // Wait for the signal from BSP to backup MTRRs
    //
    WaitForSemaphore (&mSmmMpSyncData->CpuData[CpuIndex].Run);

    //
    // Backup OS MTRRs
    //
    MtrrGetAllMtrrs(&Mtrrs);

    //
    // Signal BSP the completion of this AP
    //
    ReleaseSemaphore (&mSmmMpSyncData->CpuData[BspIndex].Run);

    //
    // Wait for BSP's signal to program MTRRs
    //
    WaitForSemaphore (&mSmmMpSyncData->CpuData[CpuIndex].Run);

    //
    // Replace OS MTRRs with SMI MTRRs
    //
    ReplaceOSMtrrs (CpuIndex);

    //
    // Signal BSP the completion of this AP
    //
    ReleaseSemaphore (&mSmmMpSyncData->CpuData[BspIndex].Run);
  }

  while (TRUE) {
    //
    // Wait for something to happen
    //
    WaitForSemaphore (&mSmmMpSyncData->CpuData[CpuIndex].Run);

    //
    // Check if BSP wants to exit SMM
    //
    if (!mSmmMpSyncData->InsideSmm) {
      break;
    }

    //
    // BUSY should be acquired by SmmStartupThisAp()
    //
    ASSERT (
      !AcquireSpinLockOrFail (&mSmmMpSyncData->CpuData[CpuIndex].Busy)
      );

    //
    // Invoke the scheduled procedure
    //
    (*mSmmMpSyncData->CpuData[CpuIndex].Procedure) (
      (VOID*)mSmmMpSyncData->CpuData[CpuIndex].Parameter
      );

    //
    // Release BUSY
    //
    ReleaseSpinLock (&mSmmMpSyncData->CpuData[CpuIndex].Busy);
  }

  if (SmmCpuFeaturesNeedConfigureMtrrs()) {
    //
    // Notify BSP the readiness of this AP to program MTRRs
    //
    ReleaseSemaphore (&mSmmMpSyncData->CpuData[BspIndex].Run);

    //
    // Wait for the signal from BSP to program MTRRs
    //
    WaitForSemaphore (&mSmmMpSyncData->CpuData[CpuIndex].Run);

    //
    // Restore OS MTRRs
    //
    SmmCpuFeaturesReenableSmrr ();
    MtrrSetAllMtrrs(&Mtrrs);
  }

  //
  // Notify BSP the readiness of this AP to Reset states/semaphore for this processor
  //
  ReleaseSemaphore (&mSmmMpSyncData->CpuData[BspIndex].Run);

  //
  // Wait for the signal from BSP to Reset states/semaphore for this processor
  //
  WaitForSemaphore (&mSmmMpSyncData->CpuData[CpuIndex].Run);

  //
  // Reset states/semaphore for this processor
  //
  mSmmMpSyncData->CpuData[CpuIndex].Present = FALSE;

  //
  // Notify BSP the readiness of this AP to exit SMM
  //
  ReleaseSemaphore (&mSmmMpSyncData->CpuData[BspIndex].Run);

}