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// ===========================================================================
5// File: CEEMAIN.H
6//
7
8//
9//
10
11// CEEMAIN.H defines the entrypoints into the Virtual Execution Engine and
12// gets the load/run process going.
13// ===========================================================================
14
15#ifndef CEEMain_H
16#define CEEMain_H
17
18#include <windef.h> // for HFILE, HANDLE, HMODULE
19
20class EEDbgInterfaceImpl;
21
22// Ensure the EE is started up.
23HRESULT EnsureEEStarted(COINITIEE flags);
24
25// Wrapper around EnsureEEStarted which also sets startup mode.
26HRESULT InitializeEE(COINITIEE flags);
27
28// Has the EE been started up?
29BOOL IsRuntimeStarted(DWORD *pdwStartupFlags);
30
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.
35//
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.
40enum ShutdownCompleteAction
41{
42 SCA_ExitProcessWhenShutdownComplete,
43 SCA_ReturnWhenShutdownComplete
44};
45
46// Force shutdown of the EE
47void ForceEEShutdown(ShutdownCompleteAction sca = SCA_ExitProcessWhenShutdownComplete);
48void InnerCoEEShutDownCOM();
49
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.
57class CExecutionEngine : public IExecutionEngine, public IEEMemoryManager
58{
59 friend struct _DacGlobals;
60
61 //***************************************************************************
62 // public API:
63 //***************************************************************************
64public:
65
66 // Notification of a DLL_THREAD_DETACH or a Thread Terminate.
67 static void ThreadDetaching(void **pTlsData);
68
69 // Delete on TLS block
70 static void DeleteTLS(void **pTlsData);
71
72 static void **CheckThreadState(DWORD slot, BOOL force = TRUE);
73 static void **CheckThreadStateNoCreate(DWORD slot
74#ifdef _DEBUG
75 , BOOL fForDestruction = FALSE
76#endif // _DEBUG
77 );
78
79 // Setup FLS simulation block, including ClrDebugState and StressLog.
80 static void SetupTLSForThread(Thread *pThread);
81
82 static LPVOID* GetTlsData();
83 static BOOL SetTlsData (void** ppTlsInfo);
84
85 //***************************************************************************
86 // private implementation:
87 //***************************************************************************
88private:
89 static PTLS_CALLBACK_FUNCTION Callbacks[MAX_PREDEFINED_TLS_SLOT];
90
91 //***************************************************************************
92 // IUnknown methods
93 //***************************************************************************
94
95 HRESULT STDMETHODCALLTYPE QueryInterface(
96 REFIID id,
97 void **pInterface);
98
99 ULONG STDMETHODCALLTYPE AddRef();
100
101 ULONG STDMETHODCALLTYPE Release();
102
103 //***************************************************************************
104 // IExecutionEngine methods for TLS
105 //***************************************************************************
106
107 // Associate a callback for cleanup with a TLS slot
108 VOID STDMETHODCALLTYPE TLS_AssociateCallback(
109 DWORD slot,
110 PTLS_CALLBACK_FUNCTION callback);
111
112 // Get the TLS block for fast Get/Set operations
113 LPVOID* STDMETHODCALLTYPE TLS_GetDataBlock();
114
115 // Get the value at a slot
116 LPVOID STDMETHODCALLTYPE TLS_GetValue(DWORD slot);
117
118 // Get the value at a slot, return FALSE if TLS info block doesn't exist
119 BOOL STDMETHODCALLTYPE TLS_CheckValue(DWORD slot, LPVOID * pValue);
120
121 // Set the value at a slot
122 VOID STDMETHODCALLTYPE TLS_SetValue(DWORD slot, LPVOID pData);
123
124 // Free TLS memory block and make callback
125 VOID STDMETHODCALLTYPE TLS_ThreadDetaching();
126
127 //***************************************************************************
128 // IExecutionEngine methods for locking
129 //***************************************************************************
130
131 CRITSEC_COOKIE STDMETHODCALLTYPE CreateLock(LPCSTR szTag, LPCSTR level, CrstFlags flags);
132
133 void STDMETHODCALLTYPE DestroyLock(CRITSEC_COOKIE lock);
134
135 void STDMETHODCALLTYPE AcquireLock(CRITSEC_COOKIE lock);
136
137 void STDMETHODCALLTYPE ReleaseLock(CRITSEC_COOKIE lock);
138
139 EVENT_COOKIE STDMETHODCALLTYPE CreateAutoEvent(BOOL bInitialState);
140 EVENT_COOKIE STDMETHODCALLTYPE CreateManualEvent(BOOL bInitialState);
141 void STDMETHODCALLTYPE CloseEvent(EVENT_COOKIE event);
142 BOOL STDMETHODCALLTYPE ClrSetEvent(EVENT_COOKIE event);
143 BOOL STDMETHODCALLTYPE ClrResetEvent(EVENT_COOKIE event);
144 DWORD STDMETHODCALLTYPE WaitForEvent(EVENT_COOKIE event, DWORD dwMilliseconds, BOOL bAlertable);
145 DWORD STDMETHODCALLTYPE WaitForSingleObject(HANDLE handle, DWORD dwMilliseconds);
146
147 SEMAPHORE_COOKIE STDMETHODCALLTYPE ClrCreateSemaphore(DWORD dwInitial, DWORD dwMax);
148 void STDMETHODCALLTYPE ClrCloseSemaphore(SEMAPHORE_COOKIE semaphore);
149 DWORD STDMETHODCALLTYPE ClrWaitForSemaphore(SEMAPHORE_COOKIE semaphore, DWORD dwMilliseconds, BOOL bAlertable);
150 BOOL STDMETHODCALLTYPE ClrReleaseSemaphore(SEMAPHORE_COOKIE semaphore, LONG lReleaseCount, LONG *lpPreviousCount);
151
152 MUTEX_COOKIE STDMETHODCALLTYPE ClrCreateMutex(LPSECURITY_ATTRIBUTES lpMutexAttributes,
153 BOOL bInitialOwner,
154 LPCTSTR lpName);
155 void STDMETHODCALLTYPE ClrCloseMutex(MUTEX_COOKIE mutex);
156 BOOL STDMETHODCALLTYPE ClrReleaseMutex(MUTEX_COOKIE mutex);
157 DWORD STDMETHODCALLTYPE ClrWaitForMutex(MUTEX_COOKIE mutex,
158 DWORD dwMilliseconds,
159 BOOL bAlertable);
160
161 DWORD STDMETHODCALLTYPE ClrSleepEx(DWORD dwMilliseconds, BOOL bAlertable);
162
163 BOOL STDMETHODCALLTYPE ClrAllocationDisallowed();
164
165 void STDMETHODCALLTYPE GetLastThrownObjectExceptionFromThread(void **ppvException);
166
167 //***************************************************************************
168 // IEEMemoryManager methods for locking
169 //***************************************************************************
170 LPVOID STDMETHODCALLTYPE ClrVirtualAlloc(LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect);
171 BOOL STDMETHODCALLTYPE ClrVirtualFree(LPVOID lpAddress, SIZE_T dwSize, DWORD dwFreeType);
172 SIZE_T STDMETHODCALLTYPE ClrVirtualQuery(LPCVOID lpAddress, PMEMORY_BASIC_INFORMATION lpBuffer, SIZE_T dwLength);
173 BOOL STDMETHODCALLTYPE ClrVirtualProtect(LPVOID lpAddress, SIZE_T dwSize, DWORD flNewProtect, PDWORD lpflOldProtect);
174 HANDLE STDMETHODCALLTYPE ClrGetProcessHeap();
175 HANDLE STDMETHODCALLTYPE ClrHeapCreate(DWORD flOptions, SIZE_T dwInitialSize, SIZE_T dwMaximumSize);
176 BOOL STDMETHODCALLTYPE ClrHeapDestroy(HANDLE hHeap);
177 LPVOID STDMETHODCALLTYPE ClrHeapAlloc(HANDLE hHeap, DWORD dwFlags, SIZE_T dwBytes);
178 BOOL STDMETHODCALLTYPE ClrHeapFree(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem);
179 BOOL STDMETHODCALLTYPE ClrHeapValidate(HANDLE hHeap, DWORD dwFlags, LPCVOID lpMem);
180 HANDLE STDMETHODCALLTYPE ClrGetProcessExecutableHeap();
181
182};
183
184#ifdef _DEBUG
185extern void DisableGlobalAllocStore ();
186#endif //_DEBUG
187
188void SetLatchedExitCode (INT32 code);
189INT32 GetLatchedExitCode (void);
190
191// Tells whether the garbage collector is fully initialized
192// Stronger than IsGCHeapInitialized
193BOOL IsGarbageCollectorFullyInitialized();
194
195
196#endif
197