Update contrib.
1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\nkernsmp\x86\ncutils.cpp
21 extern SVariantInterfaceBlock* VIB;
24 //#define __DBG_MON_FAULT__
25 //#define __RAM_LOADED_CODE__
26 //#define __EARLY_DEBUG__
30 TUint32 NKern::IdleGenerationCount()
32 return TheScheduler.iIdleGenerationCount;
37 TScheduler& s = TheScheduler;
38 TSubScheduler& ss = SubScheduler(); // OK since idle thread is locked to CPU
39 TUint32 m = ss.iCpuMask;
41 s.iIdleSpinLock.LockIrq(); // don't allow any more idle DFCs for now
42 TUint32 orig_cpus_not_idle = __e32_atomic_and_ord32(&s.iCpusNotIdle, ~m);
43 if (orig_cpus_not_idle == m)
46 if (!s.iIdleDfcs.IsEmpty())
48 __e32_atomic_ior_ord32(&s.iCpusNotIdle, m); // we aren't idle after all
49 s.iIdleGeneration ^= 1;
50 ++s.iIdleGenerationCount;
51 s.iIdleSpillCpu = (TUint8)ss.iCpuNum;
52 ss.iDfcs.MoveFrom(&s.iIdleDfcs);
53 ss.iDfcPendingFlag = 1;
54 s.iIdleSpinLock.UnlockIrq();
56 NKern::Unlock(); // process idle DFCs here
60 s.iIdleSpinLock.UnlockOnly(); // leave interrupts disabled
66 switch(NKern::CurrentContext())
69 return (TUint32)NKern::CurrentThread();
72 case NKern::EInterrupt:
79 EXPORT_C TBool BTrace::Out(TUint32 a0, TUint32 a1, TUint32 a2, TUint32 a3)
81 SBTraceData& traceData = BTraceData;
82 if(!traceData.iFilter[(a0>>BTrace::ECategoryIndex*8)&0xff])
85 TUint32 pc = (&a0)[-1]; // return address on X86
86 __ACQUIRE_BTRACE_LOCK();
87 TBool r = traceData.iHandler(a0,0,0,a1,a2,a3,0,pc);
88 __RELEASE_BTRACE_LOCK();
92 EXPORT_C TBool BTrace::OutX(TUint32 a0, TUint32 a1, TUint32 a2, TUint32 a3)
94 SBTraceData& traceData = BTraceData;
95 if(!traceData.iFilter[(a0>>BTrace::ECategoryIndex*8)&0xff])
98 TUint32 context = ContextId();
99 TUint32 pc = (&a0)[-1]; // return address on X86
100 __ACQUIRE_BTRACE_LOCK();
101 TBool r = traceData.iHandler(a0,0,context,a1,a2,a3,0,pc);
102 __RELEASE_BTRACE_LOCK();
106 EXPORT_C TBool BTrace::OutN(TUint32 a0, TUint32 a1, TUint32 a2, const TAny* aData, TInt aDataSize)
108 SBTraceData& traceData = BTraceData;
109 if(!traceData.iFilter[(a0>>BTrace::ECategoryIndex*8)&0xff])
112 if(TUint(aDataSize)>KMaxBTraceDataArray)
114 aDataSize = KMaxBTraceDataArray;
115 a0 |= BTrace::ERecordTruncated<<(BTrace::EFlagsIndex*8);
117 a0 += aDataSize<<(BTrace::ESizeIndex*8);
119 TUint32 pc = (&a0)[-1]; // return address on X86
121 __ACQUIRE_BTRACE_LOCK();
123 r = traceData.iHandler(a0,0,0,a1,a2,0,0,pc);
124 else if (aDataSize<=4)
125 r = traceData.iHandler(a0,0,0,a1,a2,*(TUint32*)aData,0,pc);
127 r = traceData.iHandler(a0,0,0,a1,a2,(TUint32)aData,0,pc);
128 __RELEASE_BTRACE_LOCK();
132 EXPORT_C TBool BTrace::OutNX(TUint32 a0, TUint32 a1, TUint32 a2, const TAny* aData, TInt aDataSize)
134 SBTraceData& traceData = BTraceData;
135 if(!traceData.iFilter[(a0>>BTrace::ECategoryIndex*8)&0xff])
138 if(TUint(aDataSize)>KMaxBTraceDataArray)
140 aDataSize = KMaxBTraceDataArray;
141 a0 |= BTrace::ERecordTruncated<<(BTrace::EFlagsIndex*8);
143 a0 += aDataSize<<(BTrace::ESizeIndex*8);
145 TUint32 context = ContextId();
146 TUint32 pc = (&a0)[-1]; // return address on X86
148 __ACQUIRE_BTRACE_LOCK();
150 r = traceData.iHandler(a0,0,context,a1,a2,0,0,pc);
151 else if(aDataSize<=4)
152 r = traceData.iHandler(a0,0,context,a1,a2,*(TUint32*)aData,0,pc);
154 r = traceData.iHandler(a0,0,context,a1,a2,(TUint32)aData,0,pc);
155 __RELEASE_BTRACE_LOCK();
159 EXPORT_C TBool BTrace::OutBig(TUint32 a0, TUint32 a1, const TAny* aData, TInt aDataSize)
161 TUint32 context = ContextId();
162 TUint32 pc = (&a0)[-1]; // return address on X86
163 SBTraceData& traceData = BTraceData;
164 if(!traceData.iFilter[(a0>>BTrace::ECategoryIndex*8)&0xff])
166 TBool r = DoOutBig(a0,a1,aData,aDataSize,context,pc);
170 EXPORT_C TBool BTrace::OutFiltered(TUint32 a0, TUint32 a1, TUint32 a2, TUint32 a3)
172 SBTraceData& traceData = BTraceData;
173 if(!traceData.iFilter[(a0>>BTrace::ECategoryIndex*8)&0xff])
175 if(!traceData.CheckFilter2(a1))
178 TUint32 pc = (&a0)[-1]; // return address on X86
179 __ACQUIRE_BTRACE_LOCK();
180 TBool r = traceData.iHandler(a0,0,0,a1,a2,a3,0,pc);
181 __RELEASE_BTRACE_LOCK();
185 EXPORT_C TBool BTrace::OutFilteredX(TUint32 a0, TUint32 a1, TUint32 a2, TUint32 a3)
187 SBTraceData& traceData = BTraceData;
188 if(!traceData.iFilter[(a0>>BTrace::ECategoryIndex*8)&0xff])
190 if(!traceData.CheckFilter2(a1))
193 TUint32 context = ContextId();
194 TUint32 pc = (&a0)[-1]; // return address on X86
195 __ACQUIRE_BTRACE_LOCK();
196 TBool r = traceData.iHandler(a0,0,context,a1,a2,a3,0,pc);
197 __RELEASE_BTRACE_LOCK();
201 EXPORT_C TBool BTrace::OutFilteredN(TUint32 a0, TUint32 a1, TUint32 a2, const TAny* aData, TInt aDataSize)
203 SBTraceData& traceData = BTraceData;
204 if(!traceData.iFilter[(a0>>BTrace::ECategoryIndex*8)&0xff])
206 if(!traceData.CheckFilter2(a1))
209 if(TUint(aDataSize)>KMaxBTraceDataArray)
211 aDataSize = KMaxBTraceDataArray;
212 a0 |= BTrace::ERecordTruncated<<(BTrace::EFlagsIndex*8);
214 a0 += aDataSize<<(BTrace::ESizeIndex*8);
216 TUint32 pc = (&a0)[-1]; // return address on X86
218 __ACQUIRE_BTRACE_LOCK();
220 r = traceData.iHandler(a0,0,0,a1,a2,0,0,pc);
221 else if(aDataSize<=4)
222 r = traceData.iHandler(a0,0,0,a1,a2,*(TUint32*)aData,0,pc);
224 r = traceData.iHandler(a0,0,0,a1,a2,(TUint32)aData,0,pc);
225 __RELEASE_BTRACE_LOCK();
229 EXPORT_C TBool BTrace::OutFilteredNX(TUint32 a0, TUint32 a1, TUint32 a2, const TAny* aData, TInt aDataSize)
231 SBTraceData& traceData = BTraceData;
232 if(!traceData.iFilter[(a0>>BTrace::ECategoryIndex*8)&0xff])
234 if(!traceData.CheckFilter2(a1))
237 if(TUint(aDataSize)>KMaxBTraceDataArray)
239 aDataSize = KMaxBTraceDataArray;
240 a0 |= BTrace::ERecordTruncated<<(BTrace::EFlagsIndex*8);
242 a0 += aDataSize<<(BTrace::ESizeIndex*8);
244 TUint32 context = ContextId();
245 TUint32 pc = (&a0)[-1]; // return address on X86
247 __ACQUIRE_BTRACE_LOCK();
249 r = traceData.iHandler(a0,0,context,a1,a2,0,0,pc);
250 else if(aDataSize<=4)
251 r = traceData.iHandler(a0,0,context,a1,a2,*(TUint32*)aData,0,pc);
253 r = traceData.iHandler(a0,0,context,a1,a2,(TUint32)aData,0,pc);
254 __RELEASE_BTRACE_LOCK();
258 EXPORT_C TBool BTrace::OutFilteredBig(TUint32 a0, TUint32 a1, const TAny* aData, TInt aDataSize)
260 TUint32 context = ContextId();
261 TUint32 pc = (&a0)[-1]; // return address on X86
262 SBTraceData& traceData = BTraceData;
263 if(!traceData.iFilter[(a0>>BTrace::ECategoryIndex*8)&0xff])
265 if(!traceData.CheckFilter2(a1))
267 TBool r = DoOutBig(a0,a1,aData,aDataSize,context,pc);
271 EXPORT_C TBool BTrace::OutFilteredPcFormatBig(TUint32 aHeader, TUint32 aModuleUid, TUint32 aPc, TUint16 aFormatId, const TAny* aData, TInt aDataSize)
273 return EFalse; //kernel side not implemented yet
276 TInt BTraceDefaultControl(BTrace::TControl /*aFunction*/, TAny* /*aArg1*/, TAny* /*aArg2*/)
278 return KErrNotSupported;
282 EXPORT_C void BTrace::SetHandlers(BTrace::THandler aNewHandler, BTrace::TControlFunction aNewControl, BTrace::THandler& aOldHandler, BTrace::TControlFunction& aOldControl)
284 BTrace::TControlFunction nc = aNewControl ? aNewControl : &BTraceDefaultControl;
285 __ACQUIRE_BTRACE_LOCK();
286 BTrace::THandler oldh = (BTrace::THandler)__e32_atomic_swp_ord_ptr(&BTraceData.iHandler, aNewHandler);
287 BTrace::TControlFunction oldc = (BTrace::TControlFunction)__e32_atomic_swp_ord_ptr(&BTraceData.iControl, nc);
288 __RELEASE_BTRACE_LOCK();
294 EXPORT_C TInt BTrace::SetFilter(TUint aCategory, TInt aValue)
296 if(!IsSupported(aCategory))
297 return KErrNotSupported;
298 TUint8* filter = BTraceData.iFilter+aCategory;
299 TUint oldValue = *filter;
300 if(TUint(aValue)<=1u)
302 oldValue = __e32_atomic_swp_ord8(filter, (TUint8)aValue);
303 BTraceContext4(BTrace::EMetaTrace, BTrace::EMetaTraceFilterChange, (TUint8)aCategory | (aValue<<8));
308 EXPORT_C SCpuIdleHandler* NKern::CpuIdleHandler()
310 return &::CpuIdleHandler;
314 void NKern::Init0(TAny* a)
316 __KTRACE_OPT(KBOOT,DEBUGPRINT("VIB=%08x", a));
317 VIB = (SVariantInterfaceBlock*)a;
318 __NK_ASSERT_ALWAYS(VIB && VIB->iVer==0 && VIB->iSize==sizeof(SVariantInterfaceBlock));
319 __KTRACE_OPT(KBOOT,DEBUGPRINT("iVer=%d iSize=%d", VIB->iVer, VIB->iSize));
320 __KTRACE_OPT(KBOOT,DEBUGPRINT("iMaxCpuClock=%08x %08x", I64HIGH(VIB->iMaxCpuClock), I64LOW(VIB->iMaxCpuClock)));
321 __KTRACE_OPT(KBOOT,DEBUGPRINT("iTimestampFreq=%u", VIB->iTimestampFreq));
322 __KTRACE_OPT(KBOOT,DEBUGPRINT("iMaxTimerClock=%u", VIB->iMaxTimerClock));
324 for (i=0; i<KMaxCpus; ++i)
326 TSubScheduler& ss = TheSubSchedulers[i];
327 ss.i_TimerMultF = (TAny*)KMaxTUint32;
328 ss.i_TimerMultI = (TAny*)0x01000000u;
329 ss.i_CpuMult = (TAny*)KMaxTUint32;
330 VIB->iTimerMult[i] = (volatile STimerMult*)&ss.i_TimerMultF;
331 VIB->iCpuMult[i] = (volatile TUint32*)&ss.i_CpuMult;
333 TheScheduler.i_TimerMax = (TAny*)(VIB->iMaxTimerClock / 128);
338 EXPORT_C TUint32 NKern::CpuTimeMeasFreq()
340 return NKern::TimestampFrequency();
344 /** Converts a time interval in microseconds to thread timeslice ticks
346 @param aMicroseconds time interval in microseconds.
347 @return Number of thread timeslice ticks. Non-integral results are rounded up.
349 @pre aMicroseconds should be nonnegative
352 EXPORT_C TInt NKern::TimesliceTicks(TUint32 aMicroseconds)
354 TUint32 mf32 = (TUint32)TheScheduler.i_TimerMax;
356 TUint64 ticks = mf*TUint64(aMicroseconds) + UI64LIT(999999);
357 ticks /= UI64LIT(1000000);
358 if (ticks > TUint64(TInt(KMaxTInt)))