os/kernelhwsrv/kernel/eka/include/nkern/win32/nk_plat.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
parent 0 bde4ae8d615e
permissions -rw-r--r--
Update contrib.
     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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // e32\include\nkern\win32\nk_plat.h
    15 // 
    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 
    26 #ifndef __NK_WIN32_H__
    27 #define __NK_WIN32_H__
    28 
    29 //SL: Avoid warnings
    30 #ifdef _CRTIMP
    31 #undef _CRTIMP
    32 #endif
    33 
    34 #define _CRTIMP			// we want to use the win32 static runtime library
    35 
    36 #define WIN32_LEAN_AND_MEAN
    37 
    38 //SL: Avoid warnings
    39 #ifdef _WIN32_WINNT
    40 #undef _WIN32_WINNT
    41 #endif
    42 
    43 #define _WIN32_WINNT 0x0400
    44 #include <windows.h>
    45 
    46 typedef void (*TExcHandler)(TAny*,TAny*);
    47 
    48 struct TWin32ExcInfo
    49 	{
    50 	enum {EExcInKernel = 0x1};
    51 public:
    52 	TExcHandler iHandler;
    53 	TAny* iParam[2];
    54 	TUint iFlags;
    55 	// ExcType, iExcId, iFaultAddress, iEax, iEcx, iEdx, iEbx, iEsp, iEbp, iEsi, iEdi,
    56 	// iSs, iDs, iEs, iFs, iGs, iEflags, iEip, iCs
    57 	TUint32	iExcType;	// filled in by EPOC
    58 	TUint32 iExcCode;
    59 	TUint32 iExcDataAddress;
    60 	TUint32 iEax;
    61 	TUint32 iEcx;
    62 	TUint32 iEdx;
    63 	TUint32 iEbx;
    64 	TUint32 iEsp;
    65 	TUint32 iEsi;
    66 	TUint32 iEdi;
    67 	TUint32 iSs;
    68 	TUint32 iDs;
    69 	TUint32 iEs;
    70 	TUint32 iFs;
    71 	TUint32 iGs;
    72 	TUint32 iEflags;
    73 	TUint32 iCs;
    74 	TUint32 iEbp;
    75 	TUint32 iEip;
    76 	};
    77 
    78 enum TEmulThreadType
    79 	{
    80 	EThreadEvent,	// an 'interrupt' thread, interacts with Win32 events
    81 	EThreadNKern	// a nKern thread, identified by a NThread control block
    82 	};
    83 
    84 class NThread : public NThreadBase
    85 	{
    86 public:
    87 	typedef void (*TDivert)();
    88 	enum TWakeup {ERelease,EResume,EResumeLocked,EIdle,EEscaped,EResumeDiverted};
    89 	enum TSelectCpu {ECpuAll=-1,ECpuSingle=-2};
    90 public:
    91 	TInt Create(SNThreadCreateInfo& aInfo, TBool aInitial);
    92 	void Stillborn();
    93 	void DoForceExit();
    94 //
    95 	IMPORT_C static void Idle();
    96 	IMPORT_C static void SetProperties(TBool aTrace, TInt aCpu);
    97 	TBool WakeUp();
    98 	TBool IsSafeToPreempt();
    99 	void Divert(TDivert aDivert);
   100 	void ApplyDiversion();
   101 
   102 private:
   103 	static DWORD WINAPI StartThread(LPVOID aParam);
   104 //
   105 	static void ExitSync();
   106 	static void ExitAsync();
   107 //
   108 	static void Exception();
   109 	static LONG WINAPI ExceptionFilter(EXCEPTION_POINTERS* aExc);
   110 
   111 public:
   112 	static void Diverted();
   113 	// Has to be accessible to code user-side via emulator.lib
   114 	static DWORD ExceptionHandler(EXCEPTION_RECORD* aException, CONTEXT* aContext);
   115 
   116 public:
   117 	HANDLE iWinThread;
   118 	DWORD iWinThreadId;
   119 	HANDLE iScheduleLock;	// event used for scheduling interlock
   120 	TBool iDiverted;		// flag to indicate that the thread is being diverted
   121 	TDivert iDivert;		// function to invoke after reschedule, may be null
   122 	TAny* iDivertReturn;    // return address from diversion
   123 	TInt iInKernel;			// flag to indicate if the thread is running 'in' the kernel
   124 	TWakeup iWakeup;		// indicates how to wake up the thread
   125 	TLinAddr iUserStackBase;
   126 	};
   127 
   128 IMPORT_C HANDLE CreateWin32Thread(TEmulThreadType aType, LPTHREAD_START_ROUTINE aThreadFunc, LPVOID aPtr, TBool aRun);
   129 IMPORT_C void StartOfInterrupt();
   130 IMPORT_C void EndOfInterrupt();
   131 
   132 void SchedulerInit(NThread& aInit);
   133 void SchedulerRegister(NThread& aSelf);
   134 NThread* SchedulerThread();
   135 NThread& CheckedCurrentThread();
   136 void SchedulerLock();
   137 void SchedulerUnlock();
   138 void SchedulerEscape();
   139 void SchedulerReenter();
   140 void Win32FindNonPreemptibleFunctions();
   141 
   142 inline void EnterKernel(TBool aCheck=TRUE)
   143 	{
   144 	if (++CheckedCurrentThread().iInKernel==1 && aCheck)
   145 		{
   146 		NThread& t = CheckedCurrentThread();
   147 		__NK_ASSERT_ALWAYS(t.iCsCount==0);
   148 		__NK_ASSERT_ALWAYS(t.iHeldFastMutex==0);
   149 		__NK_ASSERT_ALWAYS(TheScheduler.iKernCSLocked==0);
   150 		}
   151 	}
   152 
   153 void LeaveKernel();
   154 
   155 IMPORT_C TInt __fastcall Dispatch(TInt aFunction, TInt* aArgs);
   156 
   157 typedef TInt (__cdecl *TExecHandler)(TInt,TInt,TInt,TInt);
   158 typedef void (__cdecl *TPreprocessHandler)(TInt*,TUint32);
   159 
   160 // Emulator nKern scheduling data
   161 class Win32Interrupt
   162 	{
   163 public:
   164 	void Init();
   165 	TInt Mask();
   166 	void Restore(TInt aLevel);
   167 	void Begin();
   168 	void End();
   169 	inline TBool InInterrupt() const
   170 		{return iInterrupted!=0;}	
   171 	void ForceReschedule();
   172 	inline TBool InterruptsStatus(TBool aRequest) const
   173 	{return aRequest?(iLevel==0):(iLevel!=0);}
   174 private:
   175 	static void Reschedule(TAny*);
   176 private:
   177 	TInt iLock;
   178 	HANDLE iQ;
   179 	DWORD iOwner;
   180 	TInt iLevel;
   181 	TBool iRescheduleOnExit;
   182 	NThread* iInterrupted;
   183 	NThread iScheduler;
   184 	};
   185 
   186 extern TBool Win32AtomicSOAW;	// flag to indicate availability of SignalObjectAndWait() API
   187 extern TBool Win32TraceThreadId;
   188 extern TInt Win32SingleCpu;
   189 extern Win32Interrupt Interrupt;
   190 
   191 // Emulator nKern exception data
   192 extern TAny* Win32ExcAddress;
   193 extern TAny* Win32ExcDataAddress;
   194 extern TUint Win32ExcCode;
   195 
   196 void FastCounterInit();
   197 
   198 #endif