source: branches/swt/src/kernel32/initterm.cpp@ 22097

Last change on this file since 22097 was 22097, checked in by rousseau, 11 years ago

Include a bunch of Console Debug calls

File size: 12.2 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 * Project Odin Software License can be found in LICENSE.TXT
9 */
10
11#define INCL_DOSMODULEMGR
12#define INCL_DOSMISC
13#define INCL_DOSPROCESS
14#define INCL_DOSSEMAPHORES
15#include <os2wrap.h> //Odin32 OS/2 api wrappers
16#include <stdlib.h>
17#include <stdio.h>
18#include <string.h>
19#include <misc.h>
20#include <wprocess.h>
21#include "handlemanager.h"
22#include "profile.h"
23#include <options.h>
24#include "initterm.h"
25#include <win32type.h>
26#include <win32api.h>
27#include <odinlx.h>
28#include "oslibmisc.h"
29#include <heapshared.h>
30#include <heapcode.h>
31#include "mmap.h"
32#include "directory.h"
33#include "hmdevio.h"
34#include "hmcomm.h"
35#include "windllbase.h"
36#include "winexepe2lx.h"
37#include <exitlist.h>
38#include "oslibdos.h"
39#include "osliblvm.h"
40#include <cpuhlp.h>
41#include <Win32k.h>
42#include <initdll.h>
43#include <codepage.h>
44#include <process.h>
45#include <stats.h>
46#include <heapshared.h>
47#include <heapstring.h>
48#include <_ras.h>
49
50#define DBG_LOCALLOG DBG_initterm
51#include "dbglocal.h"
52
53PVOID SYSTEM _O32_GetEnvironmentStrings( VOID );
54
55// Win32 resource table (produced by wrc)
56extern DWORD kernel32_PEResTab;
57
58static HMODULE dllHandle = 0;
59
60extern PFN pfnImSetMsgQueueProperty;
61
62BOOL fVersionWarp3 = FALSE;
63BOOL fCustomBuild = FALSE;
64
65ULONG flAllocMem = 0; /* flag to optimize DosAllocMem to use all the memory on SMP machines */
66ULONG ulMaxAddr = 0x20000000; /* end of user address space. */
67char kernel32Path[CCHMAXPATH] = "";
68BOOL fInit = FALSE;
69BOOL fWin32k = FALSE;
70HMODULE imHandle = 0;
71char szModName[ 256 ] = "";
72
73/// Exported dummy function to indicate swt-branch -- to be removed later.
74BOOL WINAPI odin32swt() {
75 return TRUE;
76}
77
78static ULONG DLL_InitKernel32_internal(ULONG hModule)
79{
80 size_t i;
81 APIRET rc;
82 ULONG ulSysinfo, version[2];
83
84 if (fInit)
85 return EXITLIST_KERNEL32; // already initialized
86
87 rc = DosQuerySysInfo(QSV_VERSION_MAJOR, QSV_VERSION_MINOR,
88 version, sizeof(version));
89 if (rc == 0)
90 if(version[0] >= 20 && version[1] <= 30)
91 fVersionWarp3 = TRUE;
92
93 // This always must be the first thing to do.
94 RasInitialize (hModule);
95#ifdef RAS
96 extern void rasInitVirtual (void);
97 rasInitVirtual ();
98#endif
99
100 ParseLogStatusKERNEL32();
101
102 /*
103 * Init the win32k library.
104 * We will also need to tell win32k where the Odin32 environment is
105 * located. Currently that is within Open32. I'm quite sure that it's
106 * not relocated during run, so we're pretty well off.
107 */
108 //Note: we do NOT want to use any win32k services with custom builds
109 if (fCustomBuild == FALSE && !libWin32kInit())
110 {
111 rc = libWin32kSetEnvironment((PSZ)_O32_GetEnvironmentStrings(), 0, 0);
112 if (rc)
113 {
114 dprintf(("KERNEL32: initterm: libWin32kSetEnvironment failed with rc=%d\n", rc));
115 }
116 else fWin32k = TRUE;
117 }
118
119 char *kernel32Name = OSLibGetDllName(hModule);
120 if (!kernel32Name)
121 return -1; // failure
122
123 strcpy(kernel32Path, kernel32Name);
124 char *endofpath = strrchr(kernel32Path, '\\');
125 *(endofpath+1) = 0;
126
127 CheckVersionFromHMOD(PE2LX_VERSION, hModule); /*PLF Wed 98-03-18 05:28:48*/
128
129 /* knut: check for high memory support */
130 rc = DosQuerySysInfo(QSV_VIRTUALADDRESSLIMIT, QSV_VIRTUALADDRESSLIMIT, &ulSysinfo, sizeof(ulSysinfo));
131 if (rc == 0 && ulSysinfo > 512) //VirtualAddresslimit is in MB
132 {
133 flAllocMem = PAG_ANY; // high memory support. Let's use it!
134 ulMaxAddr = ulSysinfo * (1024*1024);
135 }
136 else
137 flAllocMem = 0; // no high memory support
138
139 dprintf(("kernel32 init %s %s (%x) Win32k - %s", __DATE__, __TIME__, DLL_InitKernel32_internal,
140 libWin32kInstalled() ? "Installed" : "Not Installed"));
141
142 OpenPrivateLogFiles();
143
144 //SvL: Do it here instead of during the exe object creation
145 //(std handles can be used in win32 dll initialization routines
146 if (HMInitialize() != NO_ERROR)
147 return -1;
148
149 // VP: Shared heap should be initialized before call to PROFILE_*
150 // because they use a critical section which in turn uses smalloc
151 // in debug build
152 if (!InitializeSharedHeap())
153 return -1;
154
155 // VP: initialize profile internal data (critical section actually).
156 // This was done in PROFILE_LoadOdinIni but PROFILE_GetOdinIniInt
157 // is called earlier and this lead to a handle leak.
158 PROFILE_Initialize();
159
160 if(flAllocMem == PAG_ANY)
161 {
162 OSLibInitWSeBFileIO();
163 if (PROFILE_GetOdinIniInt(ODINSYSTEM_SECTION, HIGHMEM_KEY, 1) == 0)
164 {
165 dprintf(("WARNING: OS/2 kernel supports high memory, but support is DISABLED because of HIGHMEM odin.ini key"));
166 flAllocMem = 0;
167 }
168 }
169
170 if (!InitializeCodeHeap())
171 return -1;
172
173 InitializeMemMaps();
174
175 PROFILE_LoadOdinIni();
176 dllHandle = RegisterLxDll(hModule, 0, (PVOID)&kernel32_PEResTab);
177 if (dllHandle == 0)
178 return -1;
179
180 //SvL: Kernel32 is a special case; pe.exe loads it, so increase
181 // the reference count here
182 Win32DllBase *module = Win32DllBase::findModule(dllHandle);
183 if (module)
184 {
185 module->AddRef();
186 module->DisableUnload();
187 }
188
189 OSLibDosSetInitialMaxFileHandles(ODIN_DEFAULT_MAX_FILEHANDLES);
190
191#ifdef DEBUG
192 {
193 LPSTR WIN32API GetEnvironmentStringsA();
194
195 char *tmpenvnew = GetEnvironmentStringsA();
196 dprintf(("Environment:"));
197 while(*tmpenvnew) {
198 dprintf(("%s", tmpenvnew));
199 tmpenvnew += strlen(tmpenvnew)+1;
200 }
201 }
202#endif
203
204 // Must be done before InitializeTIB (which loads NTDLL -> USER32)
205 InitDirectories();
206
207 // Must be done after HMInitialize!
208 if (InitializeMainThread() == NULL)
209 return -1;
210
211 RegisterDevices();
212 Win32DllBase::setDefaultRenaming();
213
214 rc = DosQuerySysInfo(QSV_NUMPROCESSORS, QSV_NUMPROCESSORS,
215 &ulSysinfo, sizeof(ulSysinfo));
216 if (rc != 0)
217 ulSysinfo = 1;
218
219 // Setup codepage info
220 CODEPAGE_Init();
221
222 if (IsDBCSEnv() && DosLoadModule(szModName, sizeof( szModName ),
223 "OS2IM", &imHandle) == 0)
224 DosQueryProcAddr(imHandle, 140, NULL, &pfnImSetMsgQueueProperty);
225
226 InitSystemInfo(ulSysinfo);
227
228 // Set up environment as found in NT
229 InitEnvironment(ulSysinfo);
230
231 // InitDynamicRegistry creates/changes keys that may change (i.e.
232 // odin.ini keys that affect windows version)
233 InitDynamicRegistry();
234
235 // Set the process affinity mask to the system affinity mask
236 DWORD dwProcessAffinityMask, dwSystemAffinityMask;
237 GetProcessAffinityMask(GetCurrentProcess(), &dwProcessAffinityMask,
238 &dwSystemAffinityMask);
239 SetProcessAffinityMask(GetCurrentProcess(), dwSystemAffinityMask);
240
241 // Set default paths for PE & NE loaders
242 if (!InitLoaders())
243 return -1;
244
245 RasEntry(RAS_EVENT_Kernel32InitComplete,
246 &dllHandle, sizeof (dllHandle));
247
248 fInit = TRUE;
249
250 return EXITLIST_KERNEL32;
251}
252
253ULONG SYSTEM DLL_InitKernel32(ULONG hModule)
254{
255 __con_debug(2,"%s::%s@%08X(%08X)\n","kernel32.dll",__FUNCTION__,DLL_InitKernel32,hModule);
256 ULONG code = DLL_InitKernel32_internal(hModule);
257
258 if (code == -1)
259 ReportFatalDllInitError("KERNEL32");
260
261 return code;
262}
263
264void SYSTEM DLL_TermKernel32(ULONG hModule)
265{
266 __con_debug(2,"%s::%s@%08X(%08X)\n","kernel32.dll",__FUNCTION__,DLL_TermKernel32,hModule);
267 if (!fInit)
268 {
269 // The initialization sequence was not complete; attempting to
270 // uninitialize some things may crash (yeah, dirty code that doesn't
271 // analyze its own state)
272 return;
273 }
274
275 dprintf(("kernel32 exit"));
276
277 if( IsDBCSEnv() && imHandle )
278 DosFreeModule( imHandle );
279
280 //Flush and delete all open memory mapped files
281 Win32MemMap::deleteAll();
282 WinExe = NULL;
283
284 FinalizeMemMaps();
285
286 WriteOutProfiles();
287 //Unload LVM subsystem for volume/mountpoint win32 functions
288 OSLibLVMExit();
289
290 // Note: unwinding win32 exceptions before destroying TEB like we do in
291 // ExitThread()/ExitProcess() is impossible here since the stack is already
292 // vanished at this point. In cases where process termination is not coming
293 // from ExitThread()/ExitProcess(), unwinding is to be done by
294 // OS2ExceptionHandler2ndLevel() in responce to the normal unwind procedure.
295
296 TEB *teb = GetThreadTEB();
297 if(teb) DestroyTEB(teb);
298
299 DestroySharedHeap();
300 DestroyCodeHeap();
301
302 HMTerminate(); /* shutdown handlemanager */
303
304#ifdef DEBUG
305 extern void printCriticalSectionStatistic (void);
306 printCriticalSectionStatistic ();
307#endif
308
309#if defined(DEBUG) && defined(__IBMCPP__) && __IBMCPP__ == 300
310 ULONG totalmemalloc, nrcalls_malloc, nrcalls_free;
311
312 getcrtstat(&nrcalls_malloc, &nrcalls_free, &totalmemalloc);
313 dprintf(("************* KERNEL32 STATISTICS BEGIN *****************"));
314 dprintf(("Total nr of malloc calls %d", nrcalls_malloc));
315 dprintf(("Total nr of free calls %d", nrcalls_free));
316 dprintf(("Leaked memory: %d bytes", totalmemalloc));
317 dprintf(("************* KERNEL32 STATISTICS END *****************"));
318
319 //SvL: This can cause an exitlist hang; disabled for now
320//// _dump_allocated(0);
321#endif
322
323 //NOTE: Must be done after DestroyTIB
324 ClosePrivateLogFiles();
325
326#ifndef DEBUG
327 //if we do a dump of the shared heap, then we'll need the logging facility
328 //for a little while longer
329 CloseLogFile();
330#endif
331
332 /*
333 * Terminate win32k library.
334 */
335 libWin32kSetEnvironment(NULL, 0, 0);
336 libWin32kTerm();
337
338 RasUninitialize ();
339
340 if (dllHandle)
341 UnregisterLxDll(dllHandle);
342}
343
344ULONG SYSTEM DLL_Init(ULONG hModule)
345{
346 if (DLL_InitDefault(hModule) == -1)
347 return -1;
348 return DLL_InitKernel32(hModule);
349}
350
351void SYSTEM DLL_Term(ULONG hModule)
352{
353 DLL_TermKernel32(hModule);
354 DLL_TermDefault(hModule);
355}
356
357ULONG APIENTRY _O32__DLL_InitTerm(ULONG handle, ULONG flag);
358
359BOOL APIENTRY InitializeKernel32()
360{
361 HMODULE hModule;
362
363 BOOL WGSS_OK = FALSE;
364
365 if (DosQueryModuleHandleStrict("WGSS50", &hModule) == NO_ERROR)
366 {
367 if (_O32__DLL_InitTerm(hModule, 0) != 0)
368 {
369 WGSS_OK = TRUE;
370
371 if (DosQueryModuleHandleStrict("KERNEL32", &hModule) == NO_ERROR &&
372 DLL_Init(hModule) != -1)
373 return TRUE;
374
375 ReportFatalDllInitError("KERNEL32");
376 }
377 }
378
379 if (!WGSS_OK)
380 ReportFatalDllInitError("WGSS50");
381
382 return FALSE; // failure
383}
384
385VOID APIENTRY ReportFatalDllInitError(PCSZ pszModName)
386{
387 static const char msg1[] =
388 "Failed to initialize the ";
389 static const char msg2[] =
390 " library while starting \"";
391 static const char msg3[] =
392 "\".\n\r"
393 "\n\r"
394 "It is possible that there is not enough memory in the system to "
395 "run this application. Please close other applications and try "
396 "again. If the problem persists, please report the details by "
397 "creating a ticket at http://svn.netlabs.org/odin32/.\n\r";
398
399 char msg[sizeof(msg1) + 8 + sizeof(msg2) + CCHMAXPATH + sizeof(msg3)];
400
401 strcpy(msg, msg1);
402 strncat(msg, pszModName, 8);
403 strcat(msg, msg2);
404
405 PPIB ppib;
406 DosGetInfoBlocks(NULL, &ppib);
407 if (DosQueryModuleName(ppib->pib_hmte, CCHMAXPATH,
408 msg + strlen(msg)) != NO_ERROR)
409 strcat(msg, "<unknown executable>");
410 strcat(msg, msg3);
411
412 BOOL haveHMQ = FALSE;
413 MQINFO mqinfo;
414 if (WinQueryQueueInfo(1 /*HMQ_CURRENT*/, &mqinfo, sizeof(mqinfo)) == FALSE)
415 {
416 // attempt to initialize PM and try again
417 HAB hab = WinInitialize(0);
418 if (hab)
419 {
420 HMQ hmq = WinCreateMsgQueue(hab, 0);
421 if (hmq)
422 haveHMQ = TRUE;
423 }
424 }
425 else
426 haveHMQ = TRUE;
427
428 WinMessageBox(HWND_DESKTOP, NULL, msg, "Odin: Fatal Error", 0,
429 MB_APPLMODAL | MB_MOVEABLE | MB_ERROR | MB_OK);
430
431 // duplicate the message to the console just in case (PM may be not
432 // available)
433 ULONG dummy;
434 DosWrite((HFILE)1, (PVOID)&msg, strlen(msg), &dummy);
435}
Note: See TracBrowser for help on using the repository browser.