os/kernelhwsrv/kernel/eka/include/nkern/win32/nk_plat.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
child 1 260cb5ec6c19
permissions -rw-r--r--
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".
     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 #define _CRTIMP			// we want to use the win32 static runtime library
    30 
    31 #define WIN32_LEAN_AND_MEAN
    32 #define _WIN32_WINNT 0x0400
    33 #include <windows.h>
    34 
    35 typedef void (*TExcHandler)(TAny*,TAny*);
    36 
    37 struct TWin32ExcInfo
    38 	{
    39 	enum {EExcInKernel = 0x1};
    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
    70 	EThreadNKern	// a nKern thread, identified by a NThread control block
    71 	};
    72 
    73 class NThread : public NThreadBase
    74 	{
    75 public:
    76 	typedef void (*TDivert)();
    77 	enum TWakeup {ERelease,EResume,EResumeLocked,EIdle,EEscaped,EResumeDiverted};
    78 	enum TSelectCpu {ECpuAll=-1,ECpuSingle=-2};
    79 public:
    80 	TInt Create(SNThreadCreateInfo& aInfo, TBool aInitial);
    81 	void Stillborn();
    82 	void DoForceExit();
    83 //
    84 	IMPORT_C static void Idle();
    85 	IMPORT_C static void SetProperties(TBool aTrace, TInt aCpu);
    86 	TBool WakeUp();
    87 	TBool IsSafeToPreempt();
    88 	void Divert(TDivert aDivert);
    89 	void ApplyDiversion();
    90 
    91 private:
    92 	static DWORD WINAPI StartThread(LPVOID aParam);
    93 //
    94 	static void ExitSync();
    95 	static void ExitAsync();
    96 //
    97 	static void Exception();
    98 	static LONG WINAPI ExceptionFilter(EXCEPTION_POINTERS* aExc);
    99 
   100 public:
   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);
   104 
   105 public:
   106 	HANDLE iWinThread;
   107 	DWORD iWinThreadId;
   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;
   115 	};
   116 
   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();
   120 
   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();
   130 
   131 inline void EnterKernel(TBool aCheck=TRUE)
   132 	{
   133 	if (++CheckedCurrentThread().iInKernel==1 && aCheck)
   134 		{
   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);
   139 		}
   140 	}
   141 
   142 void LeaveKernel();
   143 
   144 IMPORT_C TInt __fastcall Dispatch(TInt aFunction, TInt* aArgs);
   145 
   146 typedef TInt (__cdecl *TExecHandler)(TInt,TInt,TInt,TInt);
   147 typedef void (__cdecl *TPreprocessHandler)(TInt*,TUint32);
   148 
   149 // Emulator nKern scheduling data
   150 class Win32Interrupt
   151 	{
   152 public:
   153 	void Init();
   154 	TInt Mask();
   155 	void Restore(TInt aLevel);
   156 	void Begin();
   157 	void End();
   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);}
   163 private:
   164 	static void Reschedule(TAny*);
   165 private:
   166 	TInt iLock;
   167 	HANDLE iQ;
   168 	DWORD iOwner;
   169 	TInt iLevel;
   170 	TBool iRescheduleOnExit;
   171 	NThread* iInterrupted;
   172 	NThread iScheduler;
   173 	};
   174 
   175 extern TBool Win32AtomicSOAW;	// flag to indicate availability of SignalObjectAndWait() API
   176 extern TBool Win32TraceThreadId;
   177 extern TInt Win32SingleCpu;
   178 extern Win32Interrupt Interrupt;
   179 
   180 // Emulator nKern exception data
   181 extern TAny* Win32ExcAddress;
   182 extern TAny* Win32ExcDataAddress;
   183 extern TUint Win32ExcCode;
   184 
   185 void FastCounterInit();
   186 
   187 #endif