os/kernelhwsrv/kernel/eka/nkern/nkerns.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32\nkern\nkerns.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
// NThreadBase member data
sl@0
    19
#define __INCLUDE_NTHREADBASE_DEFINES__
sl@0
    20
sl@0
    21
#include <e32cmn.h>
sl@0
    22
#include <e32cmn_private.h>
sl@0
    23
#include "nk_priv.h"
sl@0
    24
sl@0
    25
extern "C" void ExcFault(TAny*);
sl@0
    26
sl@0
    27
/******************************************************************************
sl@0
    28
 * Thread
sl@0
    29
 ******************************************************************************/
sl@0
    30
sl@0
    31
void InvalidExec()
sl@0
    32
	{
sl@0
    33
	FAULT();
sl@0
    34
	}
sl@0
    35
sl@0
    36
static const SFastExecTable DefaultFastExecTable={0,{0}};
sl@0
    37
static const SSlowExecTable DefaultSlowExecTable={0,(TLinAddr)InvalidExec,0,{{0,0}}};
sl@0
    38
sl@0
    39
const SNThreadHandlers NThread_Default_Handlers =
sl@0
    40
	{
sl@0
    41
	NTHREAD_DEFAULT_EXIT_HANDLER,
sl@0
    42
	NTHREAD_DEFAULT_STATE_HANDLER,
sl@0
    43
	NTHREAD_DEFAULT_EXCEPTION_HANDLER,
sl@0
    44
	NTHREAD_DEFAULT_TIMEOUT_HANDLER
sl@0
    45
	};
sl@0
    46
sl@0
    47
/** Create a fast mutex
sl@0
    48
sl@0
    49
	@publishedPartner
sl@0
    50
	@released
sl@0
    51
*/
sl@0
    52
EXPORT_C NFastMutex::NFastMutex()
sl@0
    53
	: iHoldingThread(0), iWaiting(0)
sl@0
    54
	{
sl@0
    55
	}
sl@0
    56
sl@0
    57
/** Create a spin lock
sl@0
    58
sl@0
    59
	@internalComponent
sl@0
    60
*/
sl@0
    61
EXPORT_C TSpinLock::TSpinLock(TUint)
sl@0
    62
	: iLock(0)
sl@0
    63
	{
sl@0
    64
	}
sl@0
    65
sl@0
    66
/** Create a R/W spin lock
sl@0
    67
sl@0
    68
	@internalComponent
sl@0
    69
*/
sl@0
    70
EXPORT_C TRWSpinLock::TRWSpinLock(TUint)
sl@0
    71
	: iLock(0)
sl@0
    72
	{
sl@0
    73
	}
sl@0
    74
sl@0
    75
NThreadBase::NThreadBase()
sl@0
    76
	{
sl@0
    77
	// from TPriListLink
sl@0
    78
	iPriority = 0;
sl@0
    79
	iSpare1 = 0;
sl@0
    80
	iSpare2 = 0;
sl@0
    81
	iSpare3 = 0;
sl@0
    82
sl@0
    83
	iRequestSemaphore.iOwningThread=(NThreadBase*)this;
sl@0
    84
	new (&iTimer) NTimer(TimerExpired,this);
sl@0
    85
	iRequestSemaphore.iOwningThread = this;
sl@0
    86
sl@0
    87
	iHeldFastMutex = 0;
sl@0
    88
	iWaitFastMutex = 0;
sl@0
    89
	iAddressSpace = 0;
sl@0
    90
	iTime = 0;
sl@0
    91
	iTimeslice = 0;
sl@0
    92
	iWaitObj = 0;
sl@0
    93
	iSuspendCount = 0;
sl@0
    94
	iCsCount = 0;
sl@0
    95
	iCsFunction = 0;
sl@0
    96
	iReturnValue = 0;	
sl@0
    97
	iStackBase = 0;
sl@0
    98
	iStackSize = 0;
sl@0
    99
	iHandlers = 0;
sl@0
   100
	iFastExecTable = 0;
sl@0
   101
	iSlowExecTable = 0;
sl@0
   102
	iSavedSP = 0;
sl@0
   103
	iExtraContext = 0;
sl@0
   104
	iExtraContextSize = 0;
sl@0
   105
	iLastStartTime = 0;
sl@0
   106
	iTotalCpuTime = 0;
sl@0
   107
	iTag = 0;
sl@0
   108
	iVemsData = 0;
sl@0
   109
	iUserModeCallbacks = 0;
sl@0
   110
	iSpare7 = 0;
sl@0
   111
	iSpare8 = 0;
sl@0
   112
	}
sl@0
   113
sl@0
   114
TInt NThreadBase::Create(SNThreadCreateInfo& aInfo, TBool aInitial)
sl@0
   115
	{
sl@0
   116
	if (aInfo.iPriority<0 || aInfo.iPriority>63)
sl@0
   117
		return KErrArgument;
sl@0
   118
	if (aInfo.iPriority==0 && !aInitial)
sl@0
   119
		return KErrArgument;
sl@0
   120
	new (this) NThreadBase;
sl@0
   121
	iStackBase=(TLinAddr)aInfo.iStackBase;
sl@0
   122
	iStackSize=aInfo.iStackSize;
sl@0
   123
	iTimeslice=(aInfo.iTimeslice>0)?aInfo.iTimeslice:-1;
sl@0
   124
	iTime=iTimeslice;
sl@0
   125
#ifdef _DEBUG
sl@0
   126
	// When the crazy scheduler is active, refuse to set any priority higher than 1
sl@0
   127
	if (KCrazySchedulerEnabled())
sl@0
   128
		iPriority=TUint8(Min(1,aInfo.iPriority));
sl@0
   129
	else
sl@0
   130
#endif
sl@0
   131
		{
sl@0
   132
		iPriority=TUint8(aInfo.iPriority);
sl@0
   133
		}
sl@0
   134
	iHandlers = aInfo.iHandlers ? aInfo.iHandlers : &NThread_Default_Handlers;
sl@0
   135
	iFastExecTable=aInfo.iFastExecTable?aInfo.iFastExecTable:&DefaultFastExecTable;
sl@0
   136
	iSlowExecTable=(aInfo.iSlowExecTable?aInfo.iSlowExecTable:&DefaultSlowExecTable)->iEntries;
sl@0
   137
	iSpare2=(TUint8)aInfo.iAttributes;		// iSpare2 is NThread attributes
sl@0
   138
	if (aInitial)
sl@0
   139
		{
sl@0
   140
		iNState=EReady;
sl@0
   141
		iSuspendCount=0;
sl@0
   142
		TheScheduler.Add(this);
sl@0
   143
		TheScheduler.iCurrentThread=this;
sl@0
   144
		TheScheduler.iKernCSLocked=0;		// now that current thread is defined
sl@0
   145
		}
sl@0
   146
	else
sl@0
   147
		{
sl@0
   148
		iNState=ESuspended;
sl@0
   149
		iSuspendCount=-1;
sl@0
   150
		}
sl@0
   151
	return KErrNone;
sl@0
   152
	}
sl@0
   153
sl@0
   154
void NThread_Default_State_Handler(NThread* __DEBUG_ONLY(aThread), TInt __DEBUG_ONLY(aOperation), TInt __DEBUG_ONLY(aParameter))
sl@0
   155
	{
sl@0
   156
	__KTRACE_OPT(KPANIC,DEBUGPRINT("Unknown NState %d: thread %T op %08x par %08x",aThread,aThread->iNState,aOperation,aParameter));
sl@0
   157
	FAULT();
sl@0
   158
	}
sl@0
   159
sl@0
   160
void NThread_Default_Exception_Handler(TAny* aContext, NThread*)
sl@0
   161
	{
sl@0
   162
	ExcFault(aContext);
sl@0
   163
	}
sl@0
   164
sl@0
   165
sl@0
   166
/** Create a nanothread.
sl@0
   167
sl@0
   168
	This function is intended to be used by the EPOC kernel and by personality
sl@0
   169
	layers. A nanothread may not use most of the functions available to normal
sl@0
   170
	Symbian OS threads. Use Kern::ThreadCreate() to create a Symbian OS thread.
sl@0
   171
sl@0
   172
	@param aThread Pointer to control block for thread to create.
sl@0
   173
	@param aInfo Information needed for creating the thread.
sl@0
   174
sl@0
   175
	@see SNThreadCreateInfo
sl@0
   176
	@see Kern::ThreadCreate
sl@0
   177
sl@0
   178
	@pre	Call in a thread context.
sl@0
   179
	@pre	Interrupts must be enabled.
sl@0
   180
	@pre	Kernel must be unlocked.
sl@0
   181
 */
sl@0
   182
EXPORT_C TInt NKern::ThreadCreate(NThread* aThread, SNThreadCreateInfo& aInfo)
sl@0
   183
	{
sl@0
   184
	CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC,"NKern::ThreadCreate");
sl@0
   185
	return aThread->Create(aInfo,FALSE);
sl@0
   186
	}
sl@0
   187
sl@0
   188
// User-mode callbacks
sl@0
   189
sl@0
   190
TUserModeCallback::TUserModeCallback(TUserModeCallbackFunc aFunc) :
sl@0
   191
	iNext(KUserModeCallbackUnqueued),
sl@0
   192
	iFunc(aFunc)
sl@0
   193
	{
sl@0
   194
	}
sl@0
   195
sl@0
   196
TUserModeCallback::~TUserModeCallback()
sl@0
   197
	{
sl@0
   198
	__NK_ASSERT_DEBUG(iNext == KUserModeCallbackUnqueued);
sl@0
   199
	}
sl@0
   200
sl@0
   201
TInt NKern::QueueUserModeCallback(NThreadBase* aThread, TUserModeCallback* aCallback)
sl@0
   202
	{
sl@0
   203
	if (aCallback->iNext != KUserModeCallbackUnqueued)
sl@0
   204
		return KErrInUse;
sl@0
   205
	TInt r = KErrDied;
sl@0
   206
	NKern::Lock();
sl@0
   207
	TUserModeCallback* listHead = aThread->iUserModeCallbacks;
sl@0
   208
	if (((TLinAddr)listHead & 3) == 0)
sl@0
   209
		{
sl@0
   210
		aCallback->iNext = listHead;
sl@0
   211
		aThread->iUserModeCallbacks = aCallback;
sl@0
   212
		r = KErrNone;
sl@0
   213
		}
sl@0
   214
	NKern::Unlock();
sl@0
   215
	return r;
sl@0
   216
	}
sl@0
   217
sl@0
   218
// Called with interrupts disabled
sl@0
   219
// The vast majority of times this is called with zero or one callback pending
sl@0
   220
void NThreadBase::CallUserModeCallbacks()
sl@0
   221
	{
sl@0
   222
	while (iUserModeCallbacks != NULL)
sl@0
   223
		{		
sl@0
   224
		// Remove first callback
sl@0
   225
		TUserModeCallback* callback = iUserModeCallbacks;
sl@0
   226
		iUserModeCallbacks = callback->iNext;
sl@0
   227
sl@0
   228
		// Enter critical section to ensure callback is called
sl@0
   229
		NKern::ThreadEnterCS();
sl@0
   230
		
sl@0
   231
		// Re-enable interrupts and call callback
sl@0
   232
		NKern::EnableAllInterrupts();
sl@0
   233
		callback->iNext = KUserModeCallbackUnqueued;
sl@0
   234
		callback->iFunc(callback, EUserModeCallbackRun);
sl@0
   235
sl@0
   236
		// Leave critical section: thread may die at this point
sl@0
   237
		NKern::ThreadLeaveCS();
sl@0
   238
		
sl@0
   239
		NKern::DisableAllInterrupts();
sl@0
   240
		}
sl@0
   241
	}
sl@0
   242
sl@0
   243
void NKern::CancelUserModeCallbacks()
sl@0
   244
	{
sl@0
   245
	// Call any queued callbacks with the EUserModeCallbackCancel reason code, in the current
sl@0
   246
	// thread.
sl@0
   247
sl@0
   248
	NThreadBase* thread = NCurrentThread();
sl@0
   249
	NKern::Lock();	
sl@0
   250
	TUserModeCallback* listHead = thread->iUserModeCallbacks;
sl@0
   251
	thread->iUserModeCallbacks = NULL;
sl@0
   252
	NKern::Unlock();
sl@0
   253
	
sl@0
   254
	while (listHead != NULL)
sl@0
   255
		{
sl@0
   256
		TUserModeCallback* callback = listHead;
sl@0
   257
		listHead = listHead->iNext;
sl@0
   258
		callback->iNext = KUserModeCallbackUnqueued;
sl@0
   259
		callback->iFunc(callback, EUserModeCallbackCancel);
sl@0
   260
		}
sl@0
   261
	}
sl@0
   262
sl@0
   263
void NKern::MoveUserModeCallbacks(NThreadBase* aDestThread, NThreadBase* aSrcThread)
sl@0
   264
	{
sl@0
   265
	// Move all queued user-mode callbacks from the source thread to the destination thread, and
sl@0
   266
	// prevent any more from being queued.  Used by the kernel thread code so that callbacks get
sl@0
   267
	// cancelled in another thread if the thread they were originally queued on dies.
sl@0
   268
sl@0
   269
	NKern::Lock();	
sl@0
   270
	TUserModeCallback* sourceListStart = aSrcThread->iUserModeCallbacks;
sl@0
   271
	aSrcThread->iUserModeCallbacks = (TUserModeCallback*)1;
sl@0
   272
	NKern::Unlock();
sl@0
   273
	__NK_ASSERT_DEBUG(((TUint)sourceListStart & 3) == 0);  // check this only gets called once per thread
sl@0
   274
sl@0
   275
	if (sourceListStart == NULL)
sl@0
   276
		return;
sl@0
   277
sl@0
   278
	TUserModeCallback* sourceListEnd = sourceListStart;
sl@0
   279
	while (sourceListEnd->iNext != NULL)
sl@0
   280
		sourceListEnd = sourceListEnd->iNext;
sl@0
   281
sl@0
   282
	NKern::Lock();
sl@0
   283
	TUserModeCallback* destListStart = aDestThread->iUserModeCallbacks;
sl@0
   284
	__NK_ASSERT_DEBUG(((TUint)destListStart & 3) == 0);
sl@0
   285
	sourceListEnd->iNext = destListStart;
sl@0
   286
	aDestThread->iUserModeCallbacks = sourceListStart;
sl@0
   287
	NKern::Unlock();
sl@0
   288
	}
sl@0
   289
sl@0
   290
/** Initialise the null thread
sl@0
   291
	@internalComponent
sl@0
   292
*/
sl@0
   293
void NKern::Init(NThread* aThread, SNThreadCreateInfo& aInfo)
sl@0
   294
	{
sl@0
   295
	aInfo.iFunction=NULL;			// irrelevant
sl@0
   296
	aInfo.iPriority=0;				// null thread has lowest priority
sl@0
   297
	aInfo.iTimeslice=0;				// null thread not timesliced
sl@0
   298
	aInfo.iAttributes=0;			// null thread does not require implicit locks
sl@0
   299
	aThread->Create(aInfo,TRUE);	// create the null thread
sl@0
   300
	}
sl@0
   301
sl@0
   302
extern "C" {
sl@0
   303
TUint32 CrashState;
sl@0
   304
}
sl@0
   305
sl@0
   306
EXPORT_C TBool NKern::Crashed()
sl@0
   307
	{
sl@0
   308
	return CrashState!=0;
sl@0
   309
	}
sl@0
   310
sl@0
   311
sl@0
   312
/** @internalTechnology */
sl@0
   313
EXPORT_C void NKern::RecordIntLatency(TInt /*aLatency*/, TInt /*aIntMask*/)
sl@0
   314
	{
sl@0
   315
	}
sl@0
   316
sl@0
   317
sl@0
   318
/** @internalTechnology */
sl@0
   319
EXPORT_C void NKern::RecordThreadLatency(TInt /*aLatency*/)
sl@0
   320
	{
sl@0
   321
	}
sl@0
   322
sl@0
   323
/********************************************
sl@0
   324
 * Deterministic Priority List Implementation
sl@0
   325
 ********************************************/
sl@0
   326
sl@0
   327
sl@0
   328
/** Construct a priority list with the specified number of priorities
sl@0
   329
sl@0
   330
	@param aNumPriorities The number of priorities (must be 1-64).
sl@0
   331
 */
sl@0
   332
EXPORT_C TPriListBase::TPriListBase(TInt aNumPriorities)
sl@0
   333
	{
sl@0
   334
	memclr(this, sizeof(TPriListBase)+(aNumPriorities-1)*sizeof(SDblQueLink*) );
sl@0
   335
	}
sl@0
   336
sl@0
   337
sl@0
   338
/********************************************
sl@0
   339
 * Miscellaneous
sl@0
   340
 ********************************************/
sl@0
   341
sl@0
   342
sl@0
   343
/**	Returns number of nanokernel timer ticks since system started.
sl@0
   344
	@return tick count
sl@0
   345
	@pre any context
sl@0
   346
 */
sl@0
   347
EXPORT_C TUint32 NKern::TickCount()
sl@0
   348
	{
sl@0
   349
	return NTickCount();
sl@0
   350
	}
sl@0
   351
sl@0
   352
sl@0
   353
TUint32 BTrace::BigTraceId = 0;
sl@0
   354
sl@0
   355
TBool BTrace::DoOutBig(TUint32 a0, TUint32 a1, const TAny* aData, TInt aDataSize, TUint32 aContext, TUint32 aPc)
sl@0
   356
	{
sl@0
   357
	SBTraceData& traceData = BTraceData;
sl@0
   358
sl@0
   359
	// see if trace is small enough to fit in single record...
sl@0
   360
	if(TUint(aDataSize)<=TUint(KMaxBTraceDataArray+4))
sl@0
   361
		{
sl@0
   362
		a0 += aDataSize;
sl@0
   363
		TUint32 a2 = 0;
sl@0
   364
		TUint32 a3 = 0;
sl@0
   365
		if(aDataSize)
sl@0
   366
			{
sl@0
   367
			a2 = *((TUint32*&)aData)++; // first 4 bytes into a2
sl@0
   368
			if(aDataSize>=4 && aDataSize<=8)
sl@0
   369
				a3 = *(TUint32*)aData; // only 4 more bytes, so pass by value, not pointer
sl@0
   370
			else
sl@0
   371
				a3 = (TUint32)aData;
sl@0
   372
			}
sl@0
   373
		return traceData.iHandler(a0,0,aContext,a1,a2,a3,0,aPc);
sl@0
   374
		}
sl@0
   375
sl@0
   376
	// adjust for header2, extra, and size word...
sl@0
   377
	a0 |= BTrace::EHeader2Present<<(BTrace::EFlagsIndex*8)|BTrace::EExtraPresent<<(BTrace::EFlagsIndex*8);
sl@0
   378
	a0 += 12;
sl@0
   379
sl@0
   380
	TUint32 traceId = __e32_atomic_add_ord32(&BigTraceId, 1);
sl@0
   381
	TUint32 header2 = BTrace::EMultipartFirst;
sl@0
   382
	TInt offset = 0;
sl@0
   383
	do
sl@0
   384
		{
sl@0
   385
		TUint32 size = aDataSize-offset;
sl@0
   386
		if(size>KMaxBTraceDataArray)
sl@0
   387
			size = KMaxBTraceDataArray;
sl@0
   388
		else
sl@0
   389
			header2 = BTrace::EMultipartLast;
sl@0
   390
		if(size<=4)
sl@0
   391
			*(TUint32*)&aData = *(TUint32*)aData; // 4 bytes or less are passed by value, not pointer
sl@0
   392
sl@0
   393
		TBool result = traceData.iHandler(a0+size,header2,aContext,aDataSize,a1,(TUint32)aData,traceId,aPc);
sl@0
   394
		if(!result)
sl@0
   395
			return result;
sl@0
   396
sl@0
   397
		offset += size;
sl@0
   398
		*(TUint8**)&aData += size;
sl@0
   399
sl@0
   400
		header2 = BTrace::EMultipartMiddle;
sl@0
   401
		a1 = offset;
sl@0
   402
		}
sl@0
   403
	while(offset<aDataSize);
sl@0
   404
sl@0
   405
	return TRUE;
sl@0
   406
	}
sl@0
   407
sl@0
   408
EXPORT_C TSpinLock* BTrace::LockPtr()
sl@0
   409
	{
sl@0
   410
	return 0;
sl@0
   411
	}