1 | /* $Id: initkernel32.cpp,v 1.28 2004-05-24 08:56:06 sandervl Exp $
|
---|
2 | *
|
---|
3 | * KERNEL32 DLL entry point
|
---|
4 | *
|
---|
5 | * Copyright 1998 Sander van Leeuwen
|
---|
6 | * Copyright 1998 Peter Fitzsimmons
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
10 | *
|
---|
11 | */
|
---|
12 |
|
---|
13 | /*-------------------------------------------------------------*/
|
---|
14 | /* INITERM.C -- Source for a custom dynamic link library */
|
---|
15 | /* initialization and termination (_DLL_InitTerm) */
|
---|
16 | /* function. */
|
---|
17 | /* */
|
---|
18 | /* When called to perform initialization, this sample function */
|
---|
19 | /* gets storage for an array of integers, and initializes its */
|
---|
20 | /* elements with random integers. At termination time, it */
|
---|
21 | /* frees the array. Substitute your own special processing. */
|
---|
22 | /*-------------------------------------------------------------*/
|
---|
23 |
|
---|
24 |
|
---|
25 | /* Include files */
|
---|
26 | #define INCL_DOSMODULEMGR
|
---|
27 | #define INCL_DOSMISC
|
---|
28 | #define INCL_DOSPROCESS
|
---|
29 | #define INCL_DOSSEMAPHORES
|
---|
30 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
---|
31 | #include <stdlib.h>
|
---|
32 | #include <stdio.h>
|
---|
33 | #include <string.h>
|
---|
34 | #include <misc.h>
|
---|
35 | #include <wprocess.h>
|
---|
36 | #include "handlemanager.h"
|
---|
37 | #include "profile.h"
|
---|
38 | #include <options.h>
|
---|
39 | #include "initterm.h"
|
---|
40 | #include <win32type.h>
|
---|
41 | #include <win32api.h>
|
---|
42 | #include <odinlx.h>
|
---|
43 | #include "oslibmisc.h"
|
---|
44 | #include <heapshared.h>
|
---|
45 | #include <heapcode.h>
|
---|
46 | #include "mmap.h"
|
---|
47 | #include "directory.h"
|
---|
48 | #include "hmdevio.h"
|
---|
49 | #include "hmcomm.h"
|
---|
50 | #include <windllbase.h>
|
---|
51 | #include "winexepe2lx.h"
|
---|
52 | #include <exitlist.h>
|
---|
53 | #include "oslibdos.h"
|
---|
54 | #include "osliblvm.h"
|
---|
55 | #include <cpuhlp.h>
|
---|
56 | #include <Win32k.h>
|
---|
57 | #include <initdll.h>
|
---|
58 | #include <codepage.h>
|
---|
59 | #include <process.h>
|
---|
60 | #include <stats.h>
|
---|
61 | #include <heapshared.h>
|
---|
62 | #include <heapstring.h>
|
---|
63 | #include <_ras.h>
|
---|
64 |
|
---|
65 | #define DBG_LOCALLOG DBG_initterm
|
---|
66 | #include "dbglocal.h"
|
---|
67 |
|
---|
68 | PVOID SYSTEM _O32_GetEnvironmentStrings( VOID );
|
---|
69 |
|
---|
70 | extern "C" {
|
---|
71 | //Win32 resource table (produced by wrc)
|
---|
72 | extern DWORD kernel32_PEResTab;
|
---|
73 | }
|
---|
74 |
|
---|
75 | extern PFN pfnImSetMsgQueueProperty;
|
---|
76 |
|
---|
77 | ULONG flAllocMem = 0; /* flag to optimize DosAllocMem to use all the memory on SMP machines */
|
---|
78 | ULONG ulMaxAddr = 0x20000000; /* end of user address space. */
|
---|
79 | int loadNr = 0;
|
---|
80 | char kernel32Path[CCHMAXPATH] = "";
|
---|
81 | static HMODULE dllHandle = 0;
|
---|
82 | BOOL fInit = FALSE;
|
---|
83 | BOOL fWin32k = FALSE;
|
---|
84 | HMODULE imHandle = 0;
|
---|
85 | char szModName[ 256 ] = "";
|
---|
86 |
|
---|
87 | /****************************************************************************/
|
---|
88 | /* _DLL_InitTerm is the function that gets called by the operating system */
|
---|
89 | /* loader when it loads and frees this DLL for each process that accesses */
|
---|
90 | /* this DLL. However, it only gets called the first time the DLL is loaded */
|
---|
91 | /* and the last time it is freed for a particular process. The system */
|
---|
92 | /* linkage convention MUST be used because the operating system loader is */
|
---|
93 | /* calling this function. */
|
---|
94 | /****************************************************************************/
|
---|
95 | ULONG APIENTRY inittermKernel32(ULONG hModule, ULONG ulFlag)
|
---|
96 | {
|
---|
97 | size_t i;
|
---|
98 | APIRET rc;
|
---|
99 | ULONG ulSysinfo, version[2];
|
---|
100 |
|
---|
101 | /*-------------------------------------------------------------------------*/
|
---|
102 | /* If ulFlag is zero then the DLL is being loaded so initialization should */
|
---|
103 | /* be performed. If ulFlag is 1 then the DLL is being freed so */
|
---|
104 | /* termination should be performed. */
|
---|
105 | /*-------------------------------------------------------------------------*/
|
---|
106 |
|
---|
107 | if(fInit == TRUE && ulFlag == 0) {
|
---|
108 | return 1; //already initialized
|
---|
109 | }
|
---|
110 | fInit = TRUE;
|
---|
111 |
|
---|
112 | switch (ulFlag)
|
---|
113 | {
|
---|
114 | case 0 :
|
---|
115 | {
|
---|
116 | // This always must be the first thing to do.
|
---|
117 | RasInitialize (hModule);
|
---|
118 | #ifdef RAS
|
---|
119 | extern void rasInitVirtual (void);
|
---|
120 | rasInitVirtual ();
|
---|
121 | #endif
|
---|
122 |
|
---|
123 | ParseLogStatusKERNEL32();
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * Init the win32k library.
|
---|
127 | * We will also need to tell win32k where the Odin32 environment is
|
---|
128 | * located. Currently that is within Open32. I'm quite sure that it's
|
---|
129 | * not relocated during run, so we're pretty well off.
|
---|
130 | */
|
---|
131 | //Note: we do NOT want to use any win32k services with custom builds
|
---|
132 | if (fCustomBuild == FALSE && !libWin32kInit())
|
---|
133 | {
|
---|
134 | rc = libWin32kSetEnvironment((PSZ)_O32_GetEnvironmentStrings(), 0, 0);
|
---|
135 | if (rc)
|
---|
136 | {
|
---|
137 | dprintf(("KERNEL32: initterm: libWin32kSetEnvironment failed with rc=%d\n", rc));
|
---|
138 | }
|
---|
139 | else fWin32k = TRUE;
|
---|
140 | }
|
---|
141 | strcpy(kernel32Path, OSLibGetDllName(hModule));
|
---|
142 | char *endofpath = strrchr(kernel32Path, '\\');
|
---|
143 | *(endofpath+1) = 0;
|
---|
144 |
|
---|
145 | CheckVersionFromHMOD(PE2LX_VERSION, hModule); /*PLF Wed 98-03-18 05:28:48*/
|
---|
146 |
|
---|
147 | /* knut: check for high memory support */
|
---|
148 | rc = DosQuerySysInfo(QSV_VIRTUALADDRESSLIMIT, QSV_VIRTUALADDRESSLIMIT, &ulSysinfo, sizeof(ulSysinfo));
|
---|
149 | if (rc == 0 && ulSysinfo > 512) //VirtualAddresslimit is in MB
|
---|
150 | {
|
---|
151 | flAllocMem = PAG_ANY; // high memory support. Let's use it!
|
---|
152 | ulMaxAddr = ulSysinfo * (1024*1024);
|
---|
153 | }
|
---|
154 | else
|
---|
155 | flAllocMem = 0; // no high memory support
|
---|
156 |
|
---|
157 | dprintf(("kernel32 init %s %s (%x) Win32k - %s", __DATE__, __TIME__, inittermKernel32,
|
---|
158 | libWin32kInstalled() ? "Installed" : "Not Installed"));
|
---|
159 |
|
---|
160 | OpenPrivateLogFiles();
|
---|
161 |
|
---|
162 | //SvL: Do it here instead of during the exe object creation
|
---|
163 | //(std handles can be used in win32 dll initialization routines
|
---|
164 | HMInitialize(); /* store standard handles within HandleManager */
|
---|
165 |
|
---|
166 | // VP: Shared heap should be initialized before call to PROFILE_*
|
---|
167 | // because they use a critical section which in turn uses smalloc
|
---|
168 | // in debug build
|
---|
169 | if (InitializeSharedHeap() == FALSE)
|
---|
170 | return 0UL;
|
---|
171 |
|
---|
172 | // VP: initialize profile internal data (critical section actually).
|
---|
173 | // This was done in PROFILE_LoadOdinIni but PROFILE_GetOdinIniInt
|
---|
174 | // is called earlier and this lead to a handle leak.
|
---|
175 | PROFILE_Initialize();
|
---|
176 |
|
---|
177 | if(flAllocMem == PAG_ANY) {
|
---|
178 | OSLibInitWSeBFileIO();
|
---|
179 | if (PROFILE_GetOdinIniInt(ODINSYSTEM_SECTION, HIGHMEM_KEY, 1) == 0) {
|
---|
180 | dprintf(("WARNING: OS/2 kernel supports high memory, but support is DISABLED because of HIGHMEM odin.ini key"));
|
---|
181 | flAllocMem = 0;
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | if (InitializeCodeHeap() == FALSE)
|
---|
186 | return 0UL;
|
---|
187 |
|
---|
188 | InitializeMemMaps();
|
---|
189 |
|
---|
190 | PROFILE_LoadOdinIni();
|
---|
191 | dllHandle = RegisterLxDll(hModule, 0, (PVOID)&kernel32_PEResTab);
|
---|
192 | if (dllHandle == 0)
|
---|
193 | return 0UL;
|
---|
194 |
|
---|
195 | //SvL: Kernel32 is a special case; pe.exe loads it, so increase
|
---|
196 | // the reference count here
|
---|
197 | Win32DllBase *module = Win32DllBase::findModule(dllHandle);
|
---|
198 | if (module)
|
---|
199 | {
|
---|
200 | module->AddRef();
|
---|
201 | module->DisableUnload();
|
---|
202 | }
|
---|
203 |
|
---|
204 | OSLibDosSetInitialMaxFileHandles(ODIN_DEFAULT_MAX_FILEHANDLES);
|
---|
205 |
|
---|
206 |
|
---|
207 | #ifdef DEBUG
|
---|
208 | {
|
---|
209 | LPSTR WIN32API GetEnvironmentStringsA();
|
---|
210 |
|
---|
211 | char *tmpenvnew = GetEnvironmentStringsA();
|
---|
212 | dprintf(("Environment:"));
|
---|
213 | while(*tmpenvnew) {
|
---|
214 | dprintf(("%s", tmpenvnew));
|
---|
215 | tmpenvnew += strlen(tmpenvnew)+1;
|
---|
216 | }
|
---|
217 | }
|
---|
218 | #endif
|
---|
219 |
|
---|
220 | InitDirectories(); //Must be done before InitializeTIB (which loads NTDLL -> USER32)
|
---|
221 | InitializeMainThread(); //Must be done after HMInitialize!
|
---|
222 | RegisterDevices();
|
---|
223 | Win32DllBase::setDefaultRenaming();
|
---|
224 | rc = DosQuerySysInfo(QSV_NUMPROCESSORS, QSV_NUMPROCESSORS, &ulSysinfo, sizeof(ulSysinfo));
|
---|
225 | if (rc != 0)
|
---|
226 | ulSysinfo = 1;
|
---|
227 |
|
---|
228 | /* Setup codepage info */
|
---|
229 | CODEPAGE_Init();
|
---|
230 |
|
---|
231 | if( IsDBCSEnv() && DosLoadModule( szModName, sizeof( szModName ), "OS2IM.DLL", &imHandle ) == 0 )
|
---|
232 | DosQueryProcAddr( imHandle, 140, NULL, &pfnImSetMsgQueueProperty );
|
---|
233 |
|
---|
234 | InitSystemInfo(ulSysinfo);
|
---|
235 | //Set up environment as found in NT
|
---|
236 | InitEnvironment(ulSysinfo);
|
---|
237 |
|
---|
238 | //InitDynamicRegistry creates/changes keys that may change (i.e. odin.ini
|
---|
239 | //keys that affect windows version)
|
---|
240 | InitDynamicRegistry();
|
---|
241 |
|
---|
242 | //Set the process affinity mask to the system affinity mask
|
---|
243 | DWORD dwProcessAffinityMask, dwSystemAffinityMask;
|
---|
244 | GetProcessAffinityMask(GetCurrentProcess(), &dwProcessAffinityMask, &dwSystemAffinityMask);
|
---|
245 | SetProcessAffinityMask(GetCurrentProcess(), dwSystemAffinityMask);
|
---|
246 |
|
---|
247 | //Activate current timezone information
|
---|
248 | TIME_ZONE_INFORMATION tzone;
|
---|
249 | GetTimeZoneInformation(&tzone);
|
---|
250 | SetTimeZoneInformation(&tzone);
|
---|
251 |
|
---|
252 | //Set default paths for PE & NE loaders
|
---|
253 | InitLoaders();
|
---|
254 |
|
---|
255 | RasEntry (RAS_EVENT_Kernel32InitComplete, &dllHandle, sizeof (dllHandle));
|
---|
256 |
|
---|
257 | break;
|
---|
258 | }
|
---|
259 |
|
---|
260 | case 1 :
|
---|
261 | if (dllHandle)
|
---|
262 | {
|
---|
263 | UnregisterLxDll(dllHandle);
|
---|
264 | }
|
---|
265 | break;
|
---|
266 |
|
---|
267 | default :
|
---|
268 | return 0UL;
|
---|
269 | }
|
---|
270 |
|
---|
271 | /***********************************************************/
|
---|
272 | /* A non-zero value must be returned to indicate success. */
|
---|
273 | /***********************************************************/
|
---|
274 | return 1UL;
|
---|
275 | }
|
---|
276 | //******************************************************************************
|
---|
277 | //******************************************************************************
|
---|
278 | void APIENTRY cleanupKernel32(ULONG ulReason)
|
---|
279 | {
|
---|
280 | dprintf(("kernel32 exit %d\n", ulReason));
|
---|
281 |
|
---|
282 | if( IsDBCSEnv() && imHandle )
|
---|
283 | DosFreeModule( imHandle );
|
---|
284 |
|
---|
285 | //Flush and delete all open memory mapped files
|
---|
286 | Win32MemMap::deleteAll();
|
---|
287 | WinExe = NULL;
|
---|
288 |
|
---|
289 | FinalizeMemMaps();
|
---|
290 |
|
---|
291 | WriteOutProfiles();
|
---|
292 | //Unload LVM subsystem for volume/mountpoint win32 functions
|
---|
293 | OSLibLVMExit();
|
---|
294 |
|
---|
295 | TEB *teb = GetThreadTEB();
|
---|
296 | if(teb) DestroyTEB(teb);
|
---|
297 | DestroySharedHeap();
|
---|
298 | DestroyCodeHeap();
|
---|
299 |
|
---|
300 | HMTerminate(); /* shutdown handlemanager */
|
---|
301 |
|
---|
302 | #ifdef DEBUG
|
---|
303 | extern void printCriticalSectionStatistic (void);
|
---|
304 | printCriticalSectionStatistic ();
|
---|
305 | #endif
|
---|
306 |
|
---|
307 | #if defined(DEBUG) && defined(__IBMCPP__) && __IBMCPP__ == 300
|
---|
308 | ULONG totalmemalloc, nrcalls_malloc, nrcalls_free;
|
---|
309 |
|
---|
310 | getcrtstat(&nrcalls_malloc, &nrcalls_free, &totalmemalloc);
|
---|
311 | dprintf(("************* KERNEL32 STATISTICS BEGIN *****************"));
|
---|
312 | dprintf(("Total nr of malloc calls %d", nrcalls_malloc));
|
---|
313 | dprintf(("Total nr of free calls %d", nrcalls_free));
|
---|
314 | dprintf(("Leaked memory: %d bytes", totalmemalloc));
|
---|
315 | dprintf(("************* KERNEL32 STATISTICS END *****************"));
|
---|
316 |
|
---|
317 | //SvL: This can cause an exitlist hang; disabled for now
|
---|
318 | //// _dump_allocated(0);
|
---|
319 | #endif
|
---|
320 |
|
---|
321 | //NOTE: Must be done after DestroyTIB
|
---|
322 | ClosePrivateLogFiles();
|
---|
323 |
|
---|
324 | #ifndef DEBUG
|
---|
325 | //if we do a dump of the shared heap, then we'll need the logging facility
|
---|
326 | //for a little while longer
|
---|
327 | CloseLogFile();
|
---|
328 | #endif
|
---|
329 |
|
---|
330 | /*
|
---|
331 | * Terminate win32k library.
|
---|
332 | */
|
---|
333 | libWin32kSetEnvironment(NULL, 0, 0);
|
---|
334 | libWin32kTerm();
|
---|
335 |
|
---|
336 | RasUninitialize ();
|
---|
337 |
|
---|
338 | return ;
|
---|
339 | }
|
---|
340 | //******************************************************************************
|
---|
341 | //******************************************************************************
|
---|