source: trunk/src/kernel32/initkernel32.cpp@ 21654

Last change on this file since 21654 was 21654, checked in by dmik, 14 years ago

kernel32: Show an error message box if WGSS50.DLL initialization fails too. See also r21651.

File size: 12.7 KB
Line 
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
68PVOID SYSTEM _O32_GetEnvironmentStrings( VOID );
69
70extern "C" {
71 //Win32 resource table (produced by wrc)
72 extern DWORD kernel32_PEResTab;
73}
74
75// borrowed from ntddk.h
76extern "C"
77void WIN32API RtlUnwind(
78 LPVOID,
79 LPVOID,
80 LPVOID,DWORD);
81
82extern PFN pfnImSetMsgQueueProperty;
83
84 ULONG flAllocMem = 0; /* flag to optimize DosAllocMem to use all the memory on SMP machines */
85 ULONG ulMaxAddr = 0x20000000; /* end of user address space. */
86 int loadNr = 0;
87 char kernel32Path[CCHMAXPATH] = "";
88static HMODULE dllHandle = 0;
89 BOOL fInit = FALSE;
90 BOOL fWin32k = FALSE;
91 HMODULE imHandle = 0;
92 char szModName[ 256 ] = "";
93
94/****************************************************************************/
95/* _DLL_InitTerm is the function that gets called by the operating system */
96/* loader when it loads and frees this DLL for each process that accesses */
97/* this DLL. However, it only gets called the first time the DLL is loaded */
98/* and the last time it is freed for a particular process. The system */
99/* linkage convention MUST be used because the operating system loader is */
100/* calling this function. */
101/****************************************************************************/
102static ULONG APIENTRY inittermKernel32_internal(ULONG hModule, ULONG ulFlag)
103{
104 size_t i;
105 APIRET rc;
106 ULONG ulSysinfo, version[2];
107
108 /*-------------------------------------------------------------------------*/
109 /* If ulFlag is zero then the DLL is being loaded so initialization should */
110 /* be performed. If ulFlag is 1 then the DLL is being freed so */
111 /* termination should be performed. */
112 /*-------------------------------------------------------------------------*/
113
114 switch (ulFlag)
115 {
116 case 0:
117 {
118 if (fInit)
119 return 1; // already initialized
120
121 // This always must be the first thing to do.
122 RasInitialize (hModule);
123#ifdef RAS
124 extern void rasInitVirtual (void);
125 rasInitVirtual ();
126#endif
127
128 ParseLogStatusKERNEL32();
129
130 /*
131 * Init the win32k library.
132 * We will also need to tell win32k where the Odin32 environment is
133 * located. Currently that is within Open32. I'm quite sure that it's
134 * not relocated during run, so we're pretty well off.
135 */
136 //Note: we do NOT want to use any win32k services with custom builds
137 if (fCustomBuild == FALSE && !libWin32kInit())
138 {
139 rc = libWin32kSetEnvironment((PSZ)_O32_GetEnvironmentStrings(), 0, 0);
140 if (rc)
141 {
142 dprintf(("KERNEL32: initterm: libWin32kSetEnvironment failed with rc=%d\n", rc));
143 }
144 else fWin32k = TRUE;
145 }
146
147 char *kernel32Name = OSLibGetDllName(hModule);
148 if (!kernel32Name)
149 return 0; // failure
150
151 strcpy(kernel32Path, kernel32Name);
152 char *endofpath = strrchr(kernel32Path, '\\');
153 *(endofpath+1) = 0;
154
155 CheckVersionFromHMOD(PE2LX_VERSION, hModule); /*PLF Wed 98-03-18 05:28:48*/
156
157 /* knut: check for high memory support */
158 rc = DosQuerySysInfo(QSV_VIRTUALADDRESSLIMIT, QSV_VIRTUALADDRESSLIMIT, &ulSysinfo, sizeof(ulSysinfo));
159 if (rc == 0 && ulSysinfo > 512) //VirtualAddresslimit is in MB
160 {
161 flAllocMem = PAG_ANY; // high memory support. Let's use it!
162 ulMaxAddr = ulSysinfo * (1024*1024);
163 }
164 else
165 flAllocMem = 0; // no high memory support
166
167 dprintf(("kernel32 init %s %s (%x) Win32k - %s", __DATE__, __TIME__, inittermKernel32,
168 libWin32kInstalled() ? "Installed" : "Not Installed"));
169
170 OpenPrivateLogFiles();
171
172 //SvL: Do it here instead of during the exe object creation
173 //(std handles can be used in win32 dll initialization routines
174 if (HMInitialize() != NO_ERROR)
175 return 0;
176
177 // VP: Shared heap should be initialized before call to PROFILE_*
178 // because they use a critical section which in turn uses smalloc
179 // in debug build
180 if (!InitializeSharedHeap())
181 return 0;
182
183 // VP: initialize profile internal data (critical section actually).
184 // This was done in PROFILE_LoadOdinIni but PROFILE_GetOdinIniInt
185 // is called earlier and this lead to a handle leak.
186 PROFILE_Initialize();
187
188 if(flAllocMem == PAG_ANY) {
189 OSLibInitWSeBFileIO();
190 if (PROFILE_GetOdinIniInt(ODINSYSTEM_SECTION, HIGHMEM_KEY, 1) == 0) {
191 dprintf(("WARNING: OS/2 kernel supports high memory, but support is DISABLED because of HIGHMEM odin.ini key"));
192 flAllocMem = 0;
193 }
194 }
195
196 if (!InitializeCodeHeap())
197 return 0;
198
199 InitializeMemMaps();
200
201 PROFILE_LoadOdinIni();
202 dllHandle = RegisterLxDll(hModule, 0, (PVOID)&kernel32_PEResTab);
203 if (dllHandle == 0)
204 return 0;
205
206 //SvL: Kernel32 is a special case; pe.exe loads it, so increase
207 // the reference count here
208 Win32DllBase *module = Win32DllBase::findModule(dllHandle);
209 if (module)
210 {
211 module->AddRef();
212 module->DisableUnload();
213 }
214
215 OSLibDosSetInitialMaxFileHandles(ODIN_DEFAULT_MAX_FILEHANDLES);
216
217#ifdef DEBUG
218 {
219 LPSTR WIN32API GetEnvironmentStringsA();
220
221 char *tmpenvnew = GetEnvironmentStringsA();
222 dprintf(("Environment:"));
223 while(*tmpenvnew) {
224 dprintf(("%s", tmpenvnew));
225 tmpenvnew += strlen(tmpenvnew)+1;
226 }
227 }
228#endif
229
230 // Must be done before InitializeTIB (which loads NTDLL -> USER32)
231 InitDirectories();
232
233 // Must be done after HMInitialize!
234 if (InitializeMainThread() == NULL)
235 return 0;
236
237 RegisterDevices();
238 Win32DllBase::setDefaultRenaming();
239
240 rc = DosQuerySysInfo(QSV_NUMPROCESSORS, QSV_NUMPROCESSORS,
241 &ulSysinfo, sizeof(ulSysinfo));
242 if (rc != 0)
243 ulSysinfo = 1;
244
245 // Setup codepage info
246 CODEPAGE_Init();
247
248 if (IsDBCSEnv() && DosLoadModule(szModName, sizeof( szModName ),
249 "OS2IM", &imHandle) == 0)
250 DosQueryProcAddr(imHandle, 140, NULL, &pfnImSetMsgQueueProperty);
251
252 InitSystemInfo(ulSysinfo);
253
254 // Set up environment as found in NT
255 InitEnvironment(ulSysinfo);
256
257 // InitDynamicRegistry creates/changes keys that may change (i.e.
258 // odin.ini keys that affect windows version)
259 InitDynamicRegistry();
260
261 // Set the process affinity mask to the system affinity mask
262 DWORD dwProcessAffinityMask, dwSystemAffinityMask;
263 GetProcessAffinityMask(GetCurrentProcess(), &dwProcessAffinityMask,
264 &dwSystemAffinityMask);
265 SetProcessAffinityMask(GetCurrentProcess(), dwSystemAffinityMask);
266
267 // Set default paths for PE & NE loaders
268 if (!InitLoaders())
269 return 0;
270
271 RasEntry(RAS_EVENT_Kernel32InitComplete,
272 &dllHandle, sizeof (dllHandle));
273
274 fInit = TRUE;
275
276 break;
277 }
278
279 case 1:
280
281 if (dllHandle)
282 UnregisterLxDll(dllHandle);
283
284 break;
285
286 default:
287 return 0;
288 }
289
290 /***********************************************************/
291 /* A non-zero value must be returned to indicate success. */
292 /***********************************************************/
293 return 1;
294}
295//******************************************************************************
296//******************************************************************************
297ULONG APIENTRY inittermKernel32(ULONG hModule, ULONG ulFlag)
298{
299 ULONG rc = inittermKernel32_internal(hModule, ulFlag);
300
301 if (ulFlag == 0 && rc == 0)
302 ReportFatalDllInitError("KERNEL32");
303
304 return rc;
305}
306//******************************************************************************
307//******************************************************************************
308void APIENTRY cleanupKernel32(ULONG ulReason)
309{
310 if (!fInit)
311 {
312 // The initialization sequence was not complete; attempting to
313 // uninitialize some things may crash (yeah, dirty code that doesn't
314 // analyze its own state)
315 return;
316 }
317
318 dprintf(("kernel32 exit %d", ulReason));
319
320 TEB *teb = GetThreadTEB();
321
322 if (teb)
323 {
324 // make sure the Win32 exception stack (if there is still any) is unwound
325 // before we destroy internal structures including the Win32 TEB
326 RtlUnwind(NULL, 0, 0, 0);
327 }
328
329 if( IsDBCSEnv() && imHandle )
330 DosFreeModule( imHandle );
331
332 //Flush and delete all open memory mapped files
333 Win32MemMap::deleteAll();
334 WinExe = NULL;
335
336 FinalizeMemMaps();
337
338 WriteOutProfiles();
339 //Unload LVM subsystem for volume/mountpoint win32 functions
340 OSLibLVMExit();
341
342 if(teb) DestroyTEB(teb);
343 DestroySharedHeap();
344 DestroyCodeHeap();
345
346 HMTerminate(); /* shutdown handlemanager */
347
348#ifdef DEBUG
349 extern void printCriticalSectionStatistic (void);
350 printCriticalSectionStatistic ();
351#endif
352
353#if defined(DEBUG) && defined(__IBMCPP__) && __IBMCPP__ == 300
354 ULONG totalmemalloc, nrcalls_malloc, nrcalls_free;
355
356 getcrtstat(&nrcalls_malloc, &nrcalls_free, &totalmemalloc);
357 dprintf(("************* KERNEL32 STATISTICS BEGIN *****************"));
358 dprintf(("Total nr of malloc calls %d", nrcalls_malloc));
359 dprintf(("Total nr of free calls %d", nrcalls_free));
360 dprintf(("Leaked memory: %d bytes", totalmemalloc));
361 dprintf(("************* KERNEL32 STATISTICS END *****************"));
362
363 //SvL: This can cause an exitlist hang; disabled for now
364//// _dump_allocated(0);
365#endif
366
367 //NOTE: Must be done after DestroyTIB
368 ClosePrivateLogFiles();
369
370#ifndef DEBUG
371 //if we do a dump of the shared heap, then we'll need the logging facility
372 //for a little while longer
373 CloseLogFile();
374#endif
375
376 /*
377 * Terminate win32k library.
378 */
379 libWin32kSetEnvironment(NULL, 0, 0);
380 libWin32kTerm();
381
382 RasUninitialize ();
383
384 return ;
385}
386//******************************************************************************
387//******************************************************************************
Note: See TracBrowser for help on using the repository browser.