273
|
1 |
// Copyright (c) 1998-2010 Nokia Corporation and/or its subsidiary(-ies).
|
0
|
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".
|
|
7 |
//
|
|
8 |
// Initial Contributors:
|
|
9 |
// Nokia Corporation - initial contribution.
|
|
10 |
//
|
|
11 |
// Contributors:
|
|
12 |
//
|
|
13 |
// Description:
|
|
14 |
// e32\include\nkern\win32\nk_plat.h
|
273
|
15 |
//
|
0
|
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.
|
|
19 |
//
|
|
20 |
|
|
21 |
/**
|
|
22 |
@file
|
|
23 |
@internalComponent
|
|
24 |
*/
|
|
25 |
|
273
|
26 |
#ifndef __NK_WIN32_H__
|
|
27 |
#define __NK_WIN32_H__
|
0
|
28 |
|
273
|
29 |
#define _CRTIMP // we want to use the win32 static runtime library
|
0
|
30 |
|
273
|
31 |
#define WIN32_LEAN_AND_MEAN
|
|
32 |
#define _WIN32_WINNT 0x0400
|
0
|
33 |
#include <windows.h>
|
|
34 |
|
273
|
35 |
typedef void (*TExcHandler)(TAny*, TAny*);
|
0
|
36 |
|
|
37 |
struct TWin32ExcInfo
|
|
38 |
{
|
273
|
39 |
enum { EExcInKernel = 0x1 };
|
0
|
40 |
public:
|
|
41 |
TExcHandler iHandler;
|
|
42 |
TAny* iParam[2];
|
|
43 |
TUint iFlags;
|
|
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
|
|
47 |
TUint32 iExcCode;
|
|
48 |
TUint32 iExcDataAddress;
|
|
49 |
TUint32 iEax;
|
|
50 |
TUint32 iEcx;
|
|
51 |
TUint32 iEdx;
|
|
52 |
TUint32 iEbx;
|
|
53 |
TUint32 iEsp;
|
|
54 |
TUint32 iEsi;
|
|
55 |
TUint32 iEdi;
|
|
56 |
TUint32 iSs;
|
|
57 |
TUint32 iDs;
|
|
58 |
TUint32 iEs;
|
|
59 |
TUint32 iFs;
|
|
60 |
TUint32 iGs;
|
|
61 |
TUint32 iEflags;
|
|
62 |
TUint32 iCs;
|
|
63 |
TUint32 iEbp;
|
|
64 |
TUint32 iEip;
|
|
65 |
};
|
|
66 |
|
|
67 |
enum TEmulThreadType
|
|
68 |
{
|
|
69 |
EThreadEvent, // an 'interrupt' thread, interacts with Win32 events
|
273
|
70 |
EThreadNKern // an nKern thread, identified by an NThread control block
|
0
|
71 |
};
|
|
72 |
|
|
73 |
class NThread : public NThreadBase
|
|
74 |
{
|
|
75 |
public:
|
|
76 |
typedef void (*TDivert)();
|
273
|
77 |
enum TWakeup { ERelease, EResume, EResumeLocked, EIdle, EEscaped, EResumeDiverted };
|
|
78 |
enum TSelectCpu { ECpuAll = -1, ECpuSingle = -2 };
|
|
79 |
|
0
|
80 |
public:
|
|
81 |
TInt Create(SNThreadCreateInfo& aInfo, TBool aInitial);
|
|
82 |
void Stillborn();
|
|
83 |
void DoForceExit();
|
273
|
84 |
|
0
|
85 |
IMPORT_C static void Idle();
|
|
86 |
IMPORT_C static void SetProperties(TBool aTrace, TInt aCpu);
|
|
87 |
TBool WakeUp();
|
|
88 |
TBool IsSafeToPreempt();
|
|
89 |
void Divert(TDivert aDivert);
|
|
90 |
void ApplyDiversion();
|
|
91 |
|
|
92 |
private:
|
|
93 |
static DWORD WINAPI StartThread(LPVOID aParam);
|
273
|
94 |
|
0
|
95 |
static void ExitSync();
|
|
96 |
static void ExitAsync();
|
273
|
97 |
|
0
|
98 |
static void Exception();
|
|
99 |
static LONG WINAPI ExceptionFilter(EXCEPTION_POINTERS* aExc);
|
|
100 |
|
|
101 |
public:
|
|
102 |
static void Diverted();
|
|
103 |
// Has to be accessible to code user-side via emulator.lib
|
|
104 |
static DWORD ExceptionHandler(EXCEPTION_RECORD* aException, CONTEXT* aContext);
|
|
105 |
|
|
106 |
public:
|
|
107 |
HANDLE iWinThread;
|
|
108 |
DWORD iWinThreadId;
|
273
|
109 |
HANDLE iScheduleLock; // event used for scheduling interlock
|
|
110 |
volatile TInt iInKernel; // flag to indicate if the thread is running 'in' the kernel
|
|
111 |
TWakeup iWakeup; // indicates how to wake up the thread
|
|
112 |
TBool iDiverting; // flag to indicate that the thread is being diverted
|
|
113 |
TDivert iDivertFn; // function to invoke after reschedule, may be null
|
|
114 |
TAny* iDivertReturn; // return address from diversion
|
0
|
115 |
TLinAddr iUserStackBase;
|
|
116 |
};
|
|
117 |
|
|
118 |
IMPORT_C HANDLE CreateWin32Thread(TEmulThreadType aType, LPTHREAD_START_ROUTINE aThreadFunc, LPVOID aPtr, TBool aRun);
|
|
119 |
|
|
120 |
void SchedulerInit(NThread& aInit);
|
|
121 |
void SchedulerRegister(NThread& aSelf);
|
|
122 |
void SchedulerLock();
|
|
123 |
void SchedulerUnlock();
|
|
124 |
void SchedulerEscape();
|
|
125 |
void SchedulerReenter();
|
273
|
126 |
NThread* RunningThread();
|
|
127 |
NThread& CheckedCurrentThread();
|
0
|
128 |
|
273
|
129 |
void EnterKernel(TBool aDiversion = FALSE);
|
|
130 |
void LeaveKernel();
|
|
131 |
IMPORT_C void StartOfInterrupt();
|
|
132 |
IMPORT_C void EndOfInterrupt();
|
0
|
133 |
|
273
|
134 |
void Win32FindNonPreemptibleFunctions();
|
0
|
135 |
|
|
136 |
IMPORT_C TInt __fastcall Dispatch(TInt aFunction, TInt* aArgs);
|
|
137 |
|
273
|
138 |
typedef TInt (__cdecl* TExecHandler)(TInt, TInt, TInt, TInt);
|
|
139 |
typedef void (__cdecl* TPreprocessHandler)(TInt*, TUint32);
|
0
|
140 |
|
|
141 |
// Emulator nKern scheduling data
|
|
142 |
class Win32Interrupt
|
|
143 |
{
|
|
144 |
public:
|
|
145 |
void Init();
|
273
|
146 |
void BeginInterrupt();
|
|
147 |
void EndInterrupt();
|
|
148 |
TInt MaskInterrupts(TBool aPreempt);
|
|
149 |
void RestoreInterruptMask(TInt aLevel);
|
|
150 |
void ForceReschedule();
|
|
151 |
|
0
|
152 |
inline TBool InInterrupt() const
|
273
|
153 |
{
|
|
154 |
return iInterrupted != 0;
|
|
155 |
}
|
0
|
156 |
inline TBool InterruptsStatus(TBool aRequest) const
|
273
|
157 |
{
|
|
158 |
return aRequest ? (iLevel == 0) : (iLevel != 0);
|
|
159 |
}
|
|
160 |
|
0
|
161 |
private:
|
273
|
162 |
static void SchedulerThreadFunction(TAny*);
|
|
163 |
|
0
|
164 |
private:
|
273
|
165 |
CRITICAL_SECTION iCS; // protects data below
|
|
166 |
HANDLE iQ; // semaphore to wait on
|
|
167 |
|
|
168 |
DWORD iOwner; // controller of the mask
|
|
169 |
TInt iLevel; // owner's recursion count
|
|
170 |
TInt iWaiting; // number of waiters
|
|
171 |
|
0
|
172 |
TBool iRescheduleOnExit;
|
273
|
173 |
NThread* iInterrupted; // the thread preempted by Begin()
|
|
174 |
NThread iScheduler; // the dedicated scheduler thread
|
0
|
175 |
};
|
|
176 |
|
273
|
177 |
extern TBool Win32AtomicSOAW; // flag to indicate availability of SignalObjectAndWait() API
|
0
|
178 |
extern TBool Win32TraceThreadId;
|
|
179 |
extern TInt Win32SingleCpu;
|
|
180 |
extern Win32Interrupt Interrupt;
|
|
181 |
|
|
182 |
// Emulator nKern exception data
|
|
183 |
extern TAny* Win32ExcAddress;
|
|
184 |
extern TAny* Win32ExcDataAddress;
|
|
185 |
extern TUint Win32ExcCode;
|
|
186 |
|
|
187 |
void FastCounterInit();
|
|
188 |
|
273
|
189 |
// Wrappers round Win32 thread control & synchronisation functions ...
|
|
190 |
inline void CheckedSuspendThread(HANDLE aWinThread)
|
|
191 |
{
|
|
192 |
TInt suspLevel = (TInt)SuspendThread(aWinThread);
|
|
193 |
__NK_ASSERT_ALWAYS(suspLevel >= 0);
|
|
194 |
}
|
|
195 |
|
|
196 |
inline void CheckedResumeThread(HANDLE aWinThread, BOOL once = EFalse)
|
|
197 |
{
|
|
198 |
TInt suspLevel = (TInt)ResumeThread(aWinThread);
|
|
199 |
__NK_ASSERT_ALWAYS(once ? suspLevel == 1 : suspLevel > 0); // check thread was previously suspended
|
|
200 |
}
|
|
201 |
|
|
202 |
inline void CheckedGetThreadContext(HANDLE aWinThread, CONTEXT* aContext)
|
|
203 |
{
|
|
204 |
DWORD r = GetThreadContext(aWinThread, aContext);
|
|
205 |
__NK_ASSERT_ALWAYS(r != 0);
|
|
206 |
}
|
|
207 |
|
|
208 |
inline void CheckedSetThreadContext(HANDLE aWinThread, CONTEXT* aContext)
|
|
209 |
{
|
|
210 |
DWORD r = SetThreadContext(aWinThread, aContext);
|
|
211 |
__NK_ASSERT_ALWAYS(r != 0);
|
|
212 |
}
|
|
213 |
|
|
214 |
inline void CheckedSetThreadPriority(HANDLE aWinThread, TInt aPriority)
|
|
215 |
{
|
|
216 |
DWORD r = SetThreadPriority(aWinThread, aPriority);
|
|
217 |
__NK_ASSERT_ALWAYS(r != 0);
|
|
218 |
}
|
|
219 |
|
|
220 |
inline void CheckedWaitForSingleObject(HANDLE aWaitObject)
|
|
221 |
{
|
|
222 |
DWORD r = WaitForSingleObject(aWaitObject, INFINITE);
|
|
223 |
__NK_ASSERT_ALWAYS(r == WAIT_OBJECT_0);
|
|
224 |
}
|
|
225 |
|
|
226 |
inline void CheckedSignalObjectAndWait(HANDLE aToWake, HANDLE aToWaitOn)
|
|
227 |
{
|
|
228 |
DWORD r = SignalObjectAndWait(aToWake, aToWaitOn, INFINITE, FALSE);
|
|
229 |
__NK_ASSERT_ALWAYS(r == WAIT_OBJECT_0);
|
|
230 |
}
|
|
231 |
|
|
232 |
inline void CheckedSetEvent(HANDLE aWaitObject)
|
|
233 |
{
|
|
234 |
DWORD r = SetEvent(aWaitObject);
|
|
235 |
__NK_ASSERT_ALWAYS(r != 0);
|
|
236 |
}
|
|
237 |
|
|
238 |
inline void CheckedReleaseSemaphore(HANDLE aSemaphore)
|
|
239 |
{
|
|
240 |
DWORD r = ReleaseSemaphore(aSemaphore, 1, NULL);
|
|
241 |
__NK_ASSERT_ALWAYS(r != 0);
|
|
242 |
}
|
|
243 |
|
|
244 |
|
|
245 |
#endif // __NK_WIN32_H__
|