1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 // ===========================================================================
11 // CEEMAIN.H defines the entrypoints into the Virtual Execution Engine and
12 // gets the load/run process going.
13 // ===========================================================================
18 #include <windef.h> // for HFILE, HANDLE, HMODULE
20 class EEDbgInterfaceImpl;
22 // Ensure the EE is started up.
23 HRESULT EnsureEEStarted(COINITIEE flags);
25 // Wrapper around EnsureEEStarted which also sets startup mode.
26 HRESULT InitializeEE(COINITIEE flags);
28 // Has the EE been started up?
29 BOOL IsRuntimeStarted(DWORD *pdwStartupFlags);
31 // Enum to control what happens at the end of EE shutdown. There are two options:
32 // 1. Call ::ExitProcess to cause the process to terminate gracefully. This is how
33 // shutdown normally ends. "Shutdown" methods that take this action as an argument
34 // do not return when SCA_ExitProcessWhenShutdownComplete is passed.
36 // 2. Return after performing all shutdown processing. This is a special case used
37 // by a shutdown initiated via the Shim, and is used to ensure that all runtimes
38 // loaded SxS are shutdown gracefully. "Shutdown" methods that take this action
39 // as an argument return when SCA_ReturnWhenShutdownComplete is passed.
40 enum ShutdownCompleteAction
42 SCA_ExitProcessWhenShutdownComplete,
43 SCA_ReturnWhenShutdownComplete
46 // Force shutdown of the EE
47 void ForceEEShutdown(ShutdownCompleteAction sca = SCA_ExitProcessWhenShutdownComplete);
48 void InnerCoEEShutDownCOM();
50 // We have an internal class that can be used to expose EE functionality to other CLR
51 // DLLs, via the deliberately obscure IEE DLL exports from the shim and the EE
52 // NOTE: This class must not ever contain any instance variables. The reason for
53 // this is that the IEE function (corhost.cpp) relies on the fact that you
54 // may initialize the object more than once without ill effects. If you
55 // change this class so that this condition is violated, you must rewrite
56 // how the g_pCEE and related variables are initialized.
57 class CExecutionEngine : public IExecutionEngine, public IEEMemoryManager
59 friend struct _DacGlobals;
61 //***************************************************************************
63 //***************************************************************************
66 // Notification of a DLL_THREAD_DETACH or a Thread Terminate.
67 static void ThreadDetaching(void **pTlsData);
69 // Delete on TLS block
70 static void DeleteTLS(void **pTlsData);
72 // Fiber switch notifications
73 static void SwitchIn();
74 static void SwitchOut();
76 static void **CheckThreadState(DWORD slot, BOOL force = TRUE);
77 static void **CheckThreadStateNoCreate(DWORD slot
79 , BOOL fForDestruction = FALSE
83 // Setup FLS simulation block, including ClrDebugState and StressLog.
84 static void SetupTLSForThread(Thread *pThread);
86 static LPVOID* GetTlsData();
87 static BOOL SetTlsData (void** ppTlsInfo);
89 //***************************************************************************
90 // private implementation:
91 //***************************************************************************
93 static PTLS_CALLBACK_FUNCTION Callbacks[MAX_PREDEFINED_TLS_SLOT];
95 //***************************************************************************
97 //***************************************************************************
99 HRESULT STDMETHODCALLTYPE QueryInterface(
103 ULONG STDMETHODCALLTYPE AddRef();
105 ULONG STDMETHODCALLTYPE Release();
107 //***************************************************************************
108 // IExecutionEngine methods for TLS
109 //***************************************************************************
111 // Associate a callback for cleanup with a TLS slot
112 VOID STDMETHODCALLTYPE TLS_AssociateCallback(
114 PTLS_CALLBACK_FUNCTION callback);
116 // Get the TLS block for fast Get/Set operations
117 LPVOID* STDMETHODCALLTYPE TLS_GetDataBlock();
119 // Get the value at a slot
120 LPVOID STDMETHODCALLTYPE TLS_GetValue(DWORD slot);
122 // Get the value at a slot, return FALSE if TLS info block doesn't exist
123 BOOL STDMETHODCALLTYPE TLS_CheckValue(DWORD slot, LPVOID * pValue);
125 // Set the value at a slot
126 VOID STDMETHODCALLTYPE TLS_SetValue(DWORD slot, LPVOID pData);
128 // Free TLS memory block and make callback
129 VOID STDMETHODCALLTYPE TLS_ThreadDetaching();
131 //***************************************************************************
132 // IExecutionEngine methods for locking
133 //***************************************************************************
135 CRITSEC_COOKIE STDMETHODCALLTYPE CreateLock(LPCSTR szTag, LPCSTR level, CrstFlags flags);
137 void STDMETHODCALLTYPE DestroyLock(CRITSEC_COOKIE lock);
139 void STDMETHODCALLTYPE AcquireLock(CRITSEC_COOKIE lock);
141 void STDMETHODCALLTYPE ReleaseLock(CRITSEC_COOKIE lock);
143 EVENT_COOKIE STDMETHODCALLTYPE CreateAutoEvent(BOOL bInitialState);
144 EVENT_COOKIE STDMETHODCALLTYPE CreateManualEvent(BOOL bInitialState);
145 void STDMETHODCALLTYPE CloseEvent(EVENT_COOKIE event);
146 BOOL STDMETHODCALLTYPE ClrSetEvent(EVENT_COOKIE event);
147 BOOL STDMETHODCALLTYPE ClrResetEvent(EVENT_COOKIE event);
148 DWORD STDMETHODCALLTYPE WaitForEvent(EVENT_COOKIE event, DWORD dwMilliseconds, BOOL bAlertable);
149 DWORD STDMETHODCALLTYPE WaitForSingleObject(HANDLE handle, DWORD dwMilliseconds);
151 SEMAPHORE_COOKIE STDMETHODCALLTYPE ClrCreateSemaphore(DWORD dwInitial, DWORD dwMax);
152 void STDMETHODCALLTYPE ClrCloseSemaphore(SEMAPHORE_COOKIE semaphore);
153 DWORD STDMETHODCALLTYPE ClrWaitForSemaphore(SEMAPHORE_COOKIE semaphore, DWORD dwMilliseconds, BOOL bAlertable);
154 BOOL STDMETHODCALLTYPE ClrReleaseSemaphore(SEMAPHORE_COOKIE semaphore, LONG lReleaseCount, LONG *lpPreviousCount);
156 MUTEX_COOKIE STDMETHODCALLTYPE ClrCreateMutex(LPSECURITY_ATTRIBUTES lpMutexAttributes,
159 void STDMETHODCALLTYPE ClrCloseMutex(MUTEX_COOKIE mutex);
160 BOOL STDMETHODCALLTYPE ClrReleaseMutex(MUTEX_COOKIE mutex);
161 DWORD STDMETHODCALLTYPE ClrWaitForMutex(MUTEX_COOKIE mutex,
162 DWORD dwMilliseconds,
165 DWORD STDMETHODCALLTYPE ClrSleepEx(DWORD dwMilliseconds, BOOL bAlertable);
167 BOOL STDMETHODCALLTYPE ClrAllocationDisallowed();
169 void STDMETHODCALLTYPE GetLastThrownObjectExceptionFromThread(void **ppvException);
171 //***************************************************************************
172 // IEEMemoryManager methods for locking
173 //***************************************************************************
174 LPVOID STDMETHODCALLTYPE ClrVirtualAlloc(LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect);
175 BOOL STDMETHODCALLTYPE ClrVirtualFree(LPVOID lpAddress, SIZE_T dwSize, DWORD dwFreeType);
176 SIZE_T STDMETHODCALLTYPE ClrVirtualQuery(LPCVOID lpAddress, PMEMORY_BASIC_INFORMATION lpBuffer, SIZE_T dwLength);
177 BOOL STDMETHODCALLTYPE ClrVirtualProtect(LPVOID lpAddress, SIZE_T dwSize, DWORD flNewProtect, PDWORD lpflOldProtect);
178 HANDLE STDMETHODCALLTYPE ClrGetProcessHeap();
179 HANDLE STDMETHODCALLTYPE ClrHeapCreate(DWORD flOptions, SIZE_T dwInitialSize, SIZE_T dwMaximumSize);
180 BOOL STDMETHODCALLTYPE ClrHeapDestroy(HANDLE hHeap);
181 LPVOID STDMETHODCALLTYPE ClrHeapAlloc(HANDLE hHeap, DWORD dwFlags, SIZE_T dwBytes);
182 BOOL STDMETHODCALLTYPE ClrHeapFree(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem);
183 BOOL STDMETHODCALLTYPE ClrHeapValidate(HANDLE hHeap, DWORD dwFlags, LPCVOID lpMem);
184 HANDLE STDMETHODCALLTYPE ClrGetProcessExecutableHeap();
189 extern void DisableGlobalAllocStore ();
192 void SetLatchedExitCode (INT32 code);
193 INT32 GetLatchedExitCode (void);
195 // Tells whether the garbage collector is fully initialized
196 // Stronger than IsGCHeapInitialized
197 BOOL IsGarbageCollectorFullyInitialized();