First public contribution.
1 // Copyright (c) 1998-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\include\nkern\win32\nk_plat.h
16 // WARNING: This file contains some APIs which are internal and are subject
17 // to change without notice. Such APIs should therefore not be used
18 // outside the Kernel and Hardware Services package.
26 #ifndef __NK_WIN32_H__
27 #define __NK_WIN32_H__
29 #define _CRTIMP // we want to use the win32 static runtime library
31 #define WIN32_LEAN_AND_MEAN
32 #define _WIN32_WINNT 0x0400
35 typedef void (*TExcHandler)(TAny*,TAny*);
39 enum {EExcInKernel = 0x1};
44 // ExcType, iExcId, iFaultAddress, iEax, iEcx, iEdx, iEbx, iEsp, iEbp, iEsi, iEdi,
45 // iSs, iDs, iEs, iFs, iGs, iEflags, iEip, iCs
46 TUint32 iExcType; // filled in by EPOC
48 TUint32 iExcDataAddress;
69 EThreadEvent, // an 'interrupt' thread, interacts with Win32 events
70 EThreadNKern // a nKern thread, identified by a NThread control block
73 class NThread : public NThreadBase
76 typedef void (*TDivert)();
77 enum TWakeup {ERelease,EResume,EResumeLocked,EIdle,EEscaped,EResumeDiverted};
78 enum TSelectCpu {ECpuAll=-1,ECpuSingle=-2};
80 TInt Create(SNThreadCreateInfo& aInfo, TBool aInitial);
84 IMPORT_C static void Idle();
85 IMPORT_C static void SetProperties(TBool aTrace, TInt aCpu);
87 TBool IsSafeToPreempt();
88 void Divert(TDivert aDivert);
89 void ApplyDiversion();
92 static DWORD WINAPI StartThread(LPVOID aParam);
94 static void ExitSync();
95 static void ExitAsync();
97 static void Exception();
98 static LONG WINAPI ExceptionFilter(EXCEPTION_POINTERS* aExc);
101 static void Diverted();
102 // Has to be accessible to code user-side via emulator.lib
103 static DWORD ExceptionHandler(EXCEPTION_RECORD* aException, CONTEXT* aContext);
108 HANDLE iScheduleLock; // event used for scheduling interlock
109 TBool iDiverted; // flag to indicate that the thread is being diverted
110 TDivert iDivert; // function to invoke after reschedule, may be null
111 TAny* iDivertReturn; // return address from diversion
112 TInt iInKernel; // flag to indicate if the thread is running 'in' the kernel
113 TWakeup iWakeup; // indicates how to wake up the thread
114 TLinAddr iUserStackBase;
117 IMPORT_C HANDLE CreateWin32Thread(TEmulThreadType aType, LPTHREAD_START_ROUTINE aThreadFunc, LPVOID aPtr, TBool aRun);
118 IMPORT_C void StartOfInterrupt();
119 IMPORT_C void EndOfInterrupt();
121 void SchedulerInit(NThread& aInit);
122 void SchedulerRegister(NThread& aSelf);
123 NThread* SchedulerThread();
124 NThread& CheckedCurrentThread();
125 void SchedulerLock();
126 void SchedulerUnlock();
127 void SchedulerEscape();
128 void SchedulerReenter();
129 void Win32FindNonPreemptibleFunctions();
131 inline void EnterKernel(TBool aCheck=TRUE)
133 if (++CheckedCurrentThread().iInKernel==1 && aCheck)
135 NThread& t = CheckedCurrentThread();
136 __NK_ASSERT_ALWAYS(t.iCsCount==0);
137 __NK_ASSERT_ALWAYS(t.iHeldFastMutex==0);
138 __NK_ASSERT_ALWAYS(TheScheduler.iKernCSLocked==0);
144 IMPORT_C TInt __fastcall Dispatch(TInt aFunction, TInt* aArgs);
146 typedef TInt (__cdecl *TExecHandler)(TInt,TInt,TInt,TInt);
147 typedef void (__cdecl *TPreprocessHandler)(TInt*,TUint32);
149 // Emulator nKern scheduling data
155 void Restore(TInt aLevel);
158 inline TBool InInterrupt() const
159 {return iInterrupted!=0;}
160 void ForceReschedule();
161 inline TBool InterruptsStatus(TBool aRequest) const
162 {return aRequest?(iLevel==0):(iLevel!=0);}
164 static void Reschedule(TAny*);
170 TBool iRescheduleOnExit;
171 NThread* iInterrupted;
175 extern TBool Win32AtomicSOAW; // flag to indicate availability of SignalObjectAndWait() API
176 extern TBool Win32TraceThreadId;
177 extern TInt Win32SingleCpu;
178 extern Win32Interrupt Interrupt;
180 // Emulator nKern exception data
181 extern TAny* Win32ExcAddress;
182 extern TAny* Win32ExcDataAddress;
183 extern TUint Win32ExcCode;
185 void FastCounterInit();