os/kernelhwsrv/kernel/eka/nkernsmp/x86/ncutils.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2006-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\nkernsmp\x86\ncutils.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <x86.h>
sl@0
    19
sl@0
    20
extern "C" {
sl@0
    21
extern SVariantInterfaceBlock* VIB;
sl@0
    22
}
sl@0
    23
sl@0
    24
//#define __DBG_MON_FAULT__
sl@0
    25
//#define __RAM_LOADED_CODE__
sl@0
    26
//#define __EARLY_DEBUG__
sl@0
    27
void InitFpu();
sl@0
    28
sl@0
    29
sl@0
    30
TUint32 NKern::IdleGenerationCount()
sl@0
    31
	{
sl@0
    32
	return TheScheduler.iIdleGenerationCount;
sl@0
    33
	}
sl@0
    34
sl@0
    35
void NKern::Idle()
sl@0
    36
	{
sl@0
    37
	TScheduler& s = TheScheduler;
sl@0
    38
	TSubScheduler& ss = SubScheduler();	// OK since idle thread is locked to CPU
sl@0
    39
	TUint32 m = ss.iCpuMask;
sl@0
    40
sl@0
    41
	s.iIdleSpinLock.LockIrq();	// don't allow any more idle DFCs for now
sl@0
    42
	TUint32 orig_cpus_not_idle = __e32_atomic_and_ord32(&s.iCpusNotIdle, ~m);
sl@0
    43
	if (orig_cpus_not_idle == m)
sl@0
    44
		{
sl@0
    45
		// all CPUs idle
sl@0
    46
		if (!s.iIdleDfcs.IsEmpty())
sl@0
    47
			{
sl@0
    48
			__e32_atomic_ior_ord32(&s.iCpusNotIdle, m);		// we aren't idle after all
sl@0
    49
			s.iIdleGeneration ^= 1;
sl@0
    50
			++s.iIdleGenerationCount;
sl@0
    51
			s.iIdleSpillCpu = (TUint8)ss.iCpuNum;
sl@0
    52
			ss.iDfcs.MoveFrom(&s.iIdleDfcs);
sl@0
    53
			ss.iDfcPendingFlag = 1;
sl@0
    54
			s.iIdleSpinLock.UnlockIrq();
sl@0
    55
			NKern::Lock();
sl@0
    56
			NKern::Unlock();	// process idle DFCs here
sl@0
    57
			return;
sl@0
    58
			}
sl@0
    59
		}
sl@0
    60
	s.iIdleSpinLock.UnlockOnly();	// leave interrupts disabled
sl@0
    61
	NKIdle(0);
sl@0
    62
	}
sl@0
    63
sl@0
    64
TUint32 ContextId()
sl@0
    65
	{
sl@0
    66
	switch(NKern::CurrentContext())
sl@0
    67
		{
sl@0
    68
	case NKern::EThread:
sl@0
    69
		return (TUint32)NKern::CurrentThread();
sl@0
    70
	case NKern::EIDFC:
sl@0
    71
		return 3;
sl@0
    72
	case NKern::EInterrupt:
sl@0
    73
		return 2;
sl@0
    74
	default:
sl@0
    75
		return 0;
sl@0
    76
		}
sl@0
    77
	}
sl@0
    78
sl@0
    79
EXPORT_C TBool BTrace::Out(TUint32 a0, TUint32 a1, TUint32 a2, TUint32 a3)
sl@0
    80
	{
sl@0
    81
	SBTraceData& traceData = BTraceData;
sl@0
    82
	if(!traceData.iFilter[(a0>>BTrace::ECategoryIndex*8)&0xff])
sl@0
    83
		return FALSE;
sl@0
    84
sl@0
    85
	TUint32 pc = (&a0)[-1]; // return address on X86
sl@0
    86
	__ACQUIRE_BTRACE_LOCK();
sl@0
    87
	TBool r = traceData.iHandler(a0,0,0,a1,a2,a3,0,pc);
sl@0
    88
	__RELEASE_BTRACE_LOCK();
sl@0
    89
	return r;
sl@0
    90
	}
sl@0
    91
sl@0
    92
EXPORT_C TBool BTrace::OutX(TUint32 a0, TUint32 a1, TUint32 a2, TUint32 a3)
sl@0
    93
	{
sl@0
    94
	SBTraceData& traceData = BTraceData;
sl@0
    95
	if(!traceData.iFilter[(a0>>BTrace::ECategoryIndex*8)&0xff])
sl@0
    96
		return FALSE;
sl@0
    97
sl@0
    98
	TUint32 context = ContextId();
sl@0
    99
	TUint32 pc = (&a0)[-1]; // return address on X86
sl@0
   100
	__ACQUIRE_BTRACE_LOCK();
sl@0
   101
	TBool r = traceData.iHandler(a0,0,context,a1,a2,a3,0,pc);
sl@0
   102
	__RELEASE_BTRACE_LOCK();
sl@0
   103
	return r;
sl@0
   104
	}
sl@0
   105
sl@0
   106
EXPORT_C TBool BTrace::OutN(TUint32 a0, TUint32 a1, TUint32 a2, const TAny* aData, TInt aDataSize)
sl@0
   107
	{
sl@0
   108
	SBTraceData& traceData = BTraceData;
sl@0
   109
	if(!traceData.iFilter[(a0>>BTrace::ECategoryIndex*8)&0xff])
sl@0
   110
		return FALSE;
sl@0
   111
sl@0
   112
	if(TUint(aDataSize)>KMaxBTraceDataArray)
sl@0
   113
		{
sl@0
   114
		aDataSize = KMaxBTraceDataArray;
sl@0
   115
		a0 |= BTrace::ERecordTruncated<<(BTrace::EFlagsIndex*8);
sl@0
   116
		}
sl@0
   117
	a0 += aDataSize<<(BTrace::ESizeIndex*8);
sl@0
   118
sl@0
   119
	TUint32 pc = (&a0)[-1]; // return address on X86
sl@0
   120
	TBool r;
sl@0
   121
	__ACQUIRE_BTRACE_LOCK();
sl@0
   122
	if (!aDataSize)
sl@0
   123
		r = traceData.iHandler(a0,0,0,a1,a2,0,0,pc);
sl@0
   124
	else if (aDataSize<=4)
sl@0
   125
		r = traceData.iHandler(a0,0,0,a1,a2,*(TUint32*)aData,0,pc);
sl@0
   126
	else
sl@0
   127
		r = traceData.iHandler(a0,0,0,a1,a2,(TUint32)aData,0,pc);
sl@0
   128
	__RELEASE_BTRACE_LOCK();
sl@0
   129
	return r;
sl@0
   130
	}
sl@0
   131
sl@0
   132
EXPORT_C TBool BTrace::OutNX(TUint32 a0, TUint32 a1, TUint32 a2, const TAny* aData, TInt aDataSize)
sl@0
   133
	{
sl@0
   134
	SBTraceData& traceData = BTraceData;
sl@0
   135
	if(!traceData.iFilter[(a0>>BTrace::ECategoryIndex*8)&0xff])
sl@0
   136
		return FALSE;
sl@0
   137
sl@0
   138
	if(TUint(aDataSize)>KMaxBTraceDataArray)
sl@0
   139
		{
sl@0
   140
		aDataSize = KMaxBTraceDataArray;
sl@0
   141
		a0 |= BTrace::ERecordTruncated<<(BTrace::EFlagsIndex*8);
sl@0
   142
		}
sl@0
   143
	a0 += aDataSize<<(BTrace::ESizeIndex*8);
sl@0
   144
sl@0
   145
	TUint32 context = ContextId();
sl@0
   146
	TUint32 pc = (&a0)[-1]; // return address on X86
sl@0
   147
	TBool r;
sl@0
   148
	__ACQUIRE_BTRACE_LOCK();
sl@0
   149
	if(!aDataSize)
sl@0
   150
		r = traceData.iHandler(a0,0,context,a1,a2,0,0,pc);
sl@0
   151
	else if(aDataSize<=4)
sl@0
   152
		r = traceData.iHandler(a0,0,context,a1,a2,*(TUint32*)aData,0,pc);
sl@0
   153
	else
sl@0
   154
		r = traceData.iHandler(a0,0,context,a1,a2,(TUint32)aData,0,pc);
sl@0
   155
	__RELEASE_BTRACE_LOCK();
sl@0
   156
	return r;
sl@0
   157
	}
sl@0
   158
sl@0
   159
EXPORT_C TBool BTrace::OutBig(TUint32 a0, TUint32 a1, const TAny* aData, TInt aDataSize)
sl@0
   160
	{
sl@0
   161
	TUint32 context = ContextId();
sl@0
   162
	TUint32 pc = (&a0)[-1]; // return address on X86
sl@0
   163
	SBTraceData& traceData = BTraceData;
sl@0
   164
	if(!traceData.iFilter[(a0>>BTrace::ECategoryIndex*8)&0xff])
sl@0
   165
		return FALSE;
sl@0
   166
	TBool r = DoOutBig(a0,a1,aData,aDataSize,context,pc);
sl@0
   167
	return r;
sl@0
   168
	}
sl@0
   169
sl@0
   170
EXPORT_C TBool BTrace::OutFiltered(TUint32 a0, TUint32 a1, TUint32 a2, TUint32 a3)
sl@0
   171
	{
sl@0
   172
	SBTraceData& traceData = BTraceData;
sl@0
   173
	if(!traceData.iFilter[(a0>>BTrace::ECategoryIndex*8)&0xff])
sl@0
   174
		return FALSE;
sl@0
   175
	if(!traceData.CheckFilter2(a1))
sl@0
   176
		return FALSE;
sl@0
   177
sl@0
   178
	TUint32 pc = (&a0)[-1]; // return address on X86
sl@0
   179
	__ACQUIRE_BTRACE_LOCK();
sl@0
   180
	TBool r = traceData.iHandler(a0,0,0,a1,a2,a3,0,pc);
sl@0
   181
	__RELEASE_BTRACE_LOCK();
sl@0
   182
	return r;
sl@0
   183
	}
sl@0
   184
sl@0
   185
EXPORT_C TBool BTrace::OutFilteredX(TUint32 a0, TUint32 a1, TUint32 a2, TUint32 a3)
sl@0
   186
	{
sl@0
   187
	SBTraceData& traceData = BTraceData;
sl@0
   188
	if(!traceData.iFilter[(a0>>BTrace::ECategoryIndex*8)&0xff])
sl@0
   189
		return FALSE;
sl@0
   190
	if(!traceData.CheckFilter2(a1))
sl@0
   191
		return FALSE;
sl@0
   192
sl@0
   193
	TUint32 context = ContextId();
sl@0
   194
	TUint32 pc = (&a0)[-1]; // return address on X86
sl@0
   195
	__ACQUIRE_BTRACE_LOCK();
sl@0
   196
	TBool r = traceData.iHandler(a0,0,context,a1,a2,a3,0,pc);
sl@0
   197
	__RELEASE_BTRACE_LOCK();
sl@0
   198
	return r;
sl@0
   199
	}
sl@0
   200
sl@0
   201
EXPORT_C TBool BTrace::OutFilteredN(TUint32 a0, TUint32 a1, TUint32 a2, const TAny* aData, TInt aDataSize)
sl@0
   202
	{
sl@0
   203
	SBTraceData& traceData = BTraceData;
sl@0
   204
	if(!traceData.iFilter[(a0>>BTrace::ECategoryIndex*8)&0xff])
sl@0
   205
		return FALSE;
sl@0
   206
	if(!traceData.CheckFilter2(a1))
sl@0
   207
		return FALSE;
sl@0
   208
sl@0
   209
	if(TUint(aDataSize)>KMaxBTraceDataArray)
sl@0
   210
		{
sl@0
   211
		aDataSize = KMaxBTraceDataArray;
sl@0
   212
		a0 |= BTrace::ERecordTruncated<<(BTrace::EFlagsIndex*8);
sl@0
   213
		}
sl@0
   214
	a0 += aDataSize<<(BTrace::ESizeIndex*8);
sl@0
   215
sl@0
   216
	TUint32 pc = (&a0)[-1]; // return address on X86
sl@0
   217
	TBool r;
sl@0
   218
	__ACQUIRE_BTRACE_LOCK();
sl@0
   219
	if(!aDataSize)
sl@0
   220
		r = traceData.iHandler(a0,0,0,a1,a2,0,0,pc);
sl@0
   221
	else if(aDataSize<=4)
sl@0
   222
		r = traceData.iHandler(a0,0,0,a1,a2,*(TUint32*)aData,0,pc);
sl@0
   223
	else
sl@0
   224
		r = traceData.iHandler(a0,0,0,a1,a2,(TUint32)aData,0,pc);
sl@0
   225
	__RELEASE_BTRACE_LOCK();
sl@0
   226
	return r;
sl@0
   227
	}
sl@0
   228
sl@0
   229
EXPORT_C TBool BTrace::OutFilteredNX(TUint32 a0, TUint32 a1, TUint32 a2, const TAny* aData, TInt aDataSize)
sl@0
   230
	{
sl@0
   231
	SBTraceData& traceData = BTraceData;
sl@0
   232
	if(!traceData.iFilter[(a0>>BTrace::ECategoryIndex*8)&0xff])
sl@0
   233
		return FALSE;
sl@0
   234
	if(!traceData.CheckFilter2(a1))
sl@0
   235
		return FALSE;
sl@0
   236
sl@0
   237
	if(TUint(aDataSize)>KMaxBTraceDataArray)
sl@0
   238
		{
sl@0
   239
		aDataSize = KMaxBTraceDataArray;
sl@0
   240
		a0 |= BTrace::ERecordTruncated<<(BTrace::EFlagsIndex*8);
sl@0
   241
		}
sl@0
   242
	a0 += aDataSize<<(BTrace::ESizeIndex*8);
sl@0
   243
sl@0
   244
	TUint32 context = ContextId();
sl@0
   245
	TUint32 pc = (&a0)[-1]; // return address on X86
sl@0
   246
	TBool r;
sl@0
   247
	__ACQUIRE_BTRACE_LOCK();
sl@0
   248
	if(!aDataSize)
sl@0
   249
		r = traceData.iHandler(a0,0,context,a1,a2,0,0,pc);
sl@0
   250
	else if(aDataSize<=4)
sl@0
   251
		r = traceData.iHandler(a0,0,context,a1,a2,*(TUint32*)aData,0,pc);
sl@0
   252
	else
sl@0
   253
		r = traceData.iHandler(a0,0,context,a1,a2,(TUint32)aData,0,pc);
sl@0
   254
	__RELEASE_BTRACE_LOCK();
sl@0
   255
	return r;
sl@0
   256
	}
sl@0
   257
sl@0
   258
EXPORT_C TBool BTrace::OutFilteredBig(TUint32 a0, TUint32 a1, const TAny* aData, TInt aDataSize)
sl@0
   259
	{
sl@0
   260
	TUint32 context = ContextId();
sl@0
   261
	TUint32 pc = (&a0)[-1]; // return address on X86
sl@0
   262
	SBTraceData& traceData = BTraceData;
sl@0
   263
	if(!traceData.iFilter[(a0>>BTrace::ECategoryIndex*8)&0xff])
sl@0
   264
		return FALSE;
sl@0
   265
	if(!traceData.CheckFilter2(a1))
sl@0
   266
		return FALSE;
sl@0
   267
	TBool r = DoOutBig(a0,a1,aData,aDataSize,context,pc);
sl@0
   268
	return r;
sl@0
   269
	}
sl@0
   270
sl@0
   271
EXPORT_C TBool BTrace::OutFilteredPcFormatBig(TUint32 aHeader, TUint32 aModuleUid, TUint32 aPc, TUint16 aFormatId, const TAny* aData, TInt aDataSize)
sl@0
   272
	{
sl@0
   273
	return EFalse; //kernel side not implemented yet
sl@0
   274
	}
sl@0
   275
sl@0
   276
TInt BTraceDefaultControl(BTrace::TControl /*aFunction*/, TAny* /*aArg1*/, TAny* /*aArg2*/)
sl@0
   277
	{
sl@0
   278
	return KErrNotSupported;
sl@0
   279
	}
sl@0
   280
sl@0
   281
sl@0
   282
EXPORT_C void BTrace::SetHandlers(BTrace::THandler aNewHandler, BTrace::TControlFunction aNewControl, BTrace::THandler& aOldHandler, BTrace::TControlFunction& aOldControl)
sl@0
   283
	{
sl@0
   284
	BTrace::TControlFunction nc = aNewControl ? aNewControl : &BTraceDefaultControl;
sl@0
   285
	__ACQUIRE_BTRACE_LOCK();
sl@0
   286
	BTrace::THandler oldh = (BTrace::THandler)__e32_atomic_swp_ord_ptr(&BTraceData.iHandler, aNewHandler);
sl@0
   287
	BTrace::TControlFunction oldc = (BTrace::TControlFunction)__e32_atomic_swp_ord_ptr(&BTraceData.iControl, nc);
sl@0
   288
	__RELEASE_BTRACE_LOCK();
sl@0
   289
	aOldHandler = oldh;
sl@0
   290
	aOldControl = oldc;
sl@0
   291
	}
sl@0
   292
sl@0
   293
sl@0
   294
EXPORT_C TInt BTrace::SetFilter(TUint aCategory, TInt aValue)
sl@0
   295
	{
sl@0
   296
	if(!IsSupported(aCategory))
sl@0
   297
		return KErrNotSupported;
sl@0
   298
	TUint8* filter = BTraceData.iFilter+aCategory;
sl@0
   299
	TUint oldValue = *filter;
sl@0
   300
	if(TUint(aValue)<=1u)
sl@0
   301
		{
sl@0
   302
		oldValue = __e32_atomic_swp_ord8(filter, (TUint8)aValue);
sl@0
   303
		BTraceContext4(BTrace::EMetaTrace, BTrace::EMetaTraceFilterChange, (TUint8)aCategory | (aValue<<8));
sl@0
   304
		}
sl@0
   305
	return oldValue;
sl@0
   306
	}
sl@0
   307
sl@0
   308
EXPORT_C SCpuIdleHandler* NKern::CpuIdleHandler()
sl@0
   309
	{
sl@0
   310
	return &::CpuIdleHandler;
sl@0
   311
	}
sl@0
   312
sl@0
   313
sl@0
   314
void NKern::Init0(TAny* a)
sl@0
   315
	{
sl@0
   316
	__KTRACE_OPT(KBOOT,DEBUGPRINT("VIB=%08x", a));
sl@0
   317
	VIB = (SVariantInterfaceBlock*)a;
sl@0
   318
	__NK_ASSERT_ALWAYS(VIB && VIB->iVer==0 && VIB->iSize==sizeof(SVariantInterfaceBlock));
sl@0
   319
	__KTRACE_OPT(KBOOT,DEBUGPRINT("iVer=%d iSize=%d", VIB->iVer, VIB->iSize));
sl@0
   320
	__KTRACE_OPT(KBOOT,DEBUGPRINT("iMaxCpuClock=%08x %08x", I64HIGH(VIB->iMaxCpuClock), I64LOW(VIB->iMaxCpuClock)));
sl@0
   321
	__KTRACE_OPT(KBOOT,DEBUGPRINT("iTimestampFreq=%u", VIB->iTimestampFreq));
sl@0
   322
	__KTRACE_OPT(KBOOT,DEBUGPRINT("iMaxTimerClock=%u", VIB->iMaxTimerClock));
sl@0
   323
	TInt i;
sl@0
   324
	for (i=0; i<KMaxCpus; ++i)
sl@0
   325
		{
sl@0
   326
		TSubScheduler& ss = TheSubSchedulers[i];
sl@0
   327
		ss.i_TimerMultF = (TAny*)KMaxTUint32;
sl@0
   328
		ss.i_TimerMultI = (TAny*)0x01000000u;
sl@0
   329
		ss.i_CpuMult = (TAny*)KMaxTUint32;
sl@0
   330
		VIB->iTimerMult[i] = (volatile STimerMult*)&ss.i_TimerMultF;
sl@0
   331
		VIB->iCpuMult[i] = (volatile TUint32*)&ss.i_CpuMult;
sl@0
   332
		}
sl@0
   333
	TheScheduler.i_TimerMax = (TAny*)(VIB->iMaxTimerClock / 128);
sl@0
   334
	InitFpu();
sl@0
   335
	InterruptInit0();
sl@0
   336
	}
sl@0
   337
sl@0
   338
EXPORT_C TUint32 NKern::CpuTimeMeasFreq()
sl@0
   339
	{
sl@0
   340
	return NKern::TimestampFrequency();
sl@0
   341
	}
sl@0
   342
sl@0
   343
sl@0
   344
/**	Converts a time interval in microseconds to thread timeslice ticks
sl@0
   345
sl@0
   346
	@param aMicroseconds time interval in microseconds.
sl@0
   347
	@return Number of thread timeslice ticks.  Non-integral results are rounded up.
sl@0
   348
sl@0
   349
 	@pre aMicroseconds should be nonnegative
sl@0
   350
	@pre any context
sl@0
   351
 */
sl@0
   352
EXPORT_C TInt NKern::TimesliceTicks(TUint32 aMicroseconds)
sl@0
   353
	{
sl@0
   354
	TUint32 mf32 = (TUint32)TheScheduler.i_TimerMax;
sl@0
   355
	TUint64 mf(mf32);
sl@0
   356
	TUint64 ticks = mf*TUint64(aMicroseconds) + UI64LIT(999999);
sl@0
   357
	ticks /= UI64LIT(1000000);
sl@0
   358
	if (ticks > TUint64(TInt(KMaxTInt)))
sl@0
   359
		return KMaxTInt;
sl@0
   360
	else
sl@0
   361
		return (TInt)ticks;
sl@0
   362
	}
sl@0
   363