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

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

Mark kernel32 and user32 as built from the swt-branch

To prevent testing against an already loaded stock kernel32,
local tests and the swt-os2-demo check for odin32swt() to be exported.
Also, the AboutBox shows this is an experimental build based upon the
swt-branch.

File size: 12.0 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 ULONG code = DLL_InitKernel32_internal(hModule);
256
257 if (code == -1)
258 ReportFatalDllInitError("KERNEL32");
259
260 return code;
261}
262
263void SYSTEM DLL_TermKernel32(ULONG hModule)
264{
265 if (!fInit)
266 {
267 // The initialization sequence was not complete; attempting to
268 // uninitialize some things may crash (yeah, dirty code that doesn't
269 // analyze its own state)
270 return;
271 }
272
273 dprintf(("kernel32 exit"));
274
275 if( IsDBCSEnv() && imHandle )
276 DosFreeModule( imHandle );
277
278 //Flush and delete all open memory mapped files
279 Win32MemMap::deleteAll();
280 WinExe = NULL;
281
282 FinalizeMemMaps();
283
284 WriteOutProfiles();
285 //Unload LVM subsystem for volume/mountpoint win32 functions
286 OSLibLVMExit();
287
288 // Note: unwinding win32 exceptions before destroying TEB like we do in
289 // ExitThread()/ExitProcess() is impossible here since the stack is already
290 // vanished at this point. In cases where process termination is not coming
291 // from ExitThread()/ExitProcess(), unwinding is to be done by
292 // OS2ExceptionHandler2ndLevel() in responce to the normal unwind procedure.
293
294 TEB *teb = GetThreadTEB();
295 if(teb) DestroyTEB(teb);
296
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 if (dllHandle)
339 UnregisterLxDll(dllHandle);
340}
341
342ULONG SYSTEM DLL_Init(ULONG hModule)
343{
344 if (DLL_InitDefault(hModule) == -1)
345 return -1;
346 return DLL_InitKernel32(hModule);
347}
348
349void SYSTEM DLL_Term(ULONG hModule)
350{
351 DLL_TermKernel32(hModule);
352 DLL_TermDefault(hModule);
353}
354
355ULONG APIENTRY _O32__DLL_InitTerm(ULONG handle, ULONG flag);
356
357BOOL APIENTRY InitializeKernel32()
358{
359 HMODULE hModule;
360
361 BOOL WGSS_OK = FALSE;
362
363 if (DosQueryModuleHandleStrict("WGSS50", &hModule) == NO_ERROR)
364 {
365 if (_O32__DLL_InitTerm(hModule, 0) != 0)
366 {
367 WGSS_OK = TRUE;
368
369 if (DosQueryModuleHandleStrict("KERNEL32", &hModule) == NO_ERROR &&
370 DLL_Init(hModule) != -1)
371 return TRUE;
372
373 ReportFatalDllInitError("KERNEL32");
374 }
375 }
376
377 if (!WGSS_OK)
378 ReportFatalDllInitError("WGSS50");
379
380 return FALSE; // failure
381}
382
383VOID APIENTRY ReportFatalDllInitError(PCSZ pszModName)
384{
385 static const char msg1[] =
386 "Failed to initialize the ";
387 static const char msg2[] =
388 " library while starting \"";
389 static const char msg3[] =
390 "\".\n\r"
391 "\n\r"
392 "It is possible that there is not enough memory in the system to "
393 "run this application. Please close other applications and try "
394 "again. If the problem persists, please report the details by "
395 "creating a ticket at http://svn.netlabs.org/odin32/.\n\r";
396
397 char msg[sizeof(msg1) + 8 + sizeof(msg2) + CCHMAXPATH + sizeof(msg3)];
398
399 strcpy(msg, msg1);
400 strncat(msg, pszModName, 8);
401 strcat(msg, msg2);
402
403 PPIB ppib;
404 DosGetInfoBlocks(NULL, &ppib);
405 if (DosQueryModuleName(ppib->pib_hmte, CCHMAXPATH,
406 msg + strlen(msg)) != NO_ERROR)
407 strcat(msg, "<unknown executable>");
408 strcat(msg, msg3);
409
410 BOOL haveHMQ = FALSE;
411 MQINFO mqinfo;
412 if (WinQueryQueueInfo(1 /*HMQ_CURRENT*/, &mqinfo, sizeof(mqinfo)) == FALSE)
413 {
414 // attempt to initialize PM and try again
415 HAB hab = WinInitialize(0);
416 if (hab)
417 {
418 HMQ hmq = WinCreateMsgQueue(hab, 0);
419 if (hmq)
420 haveHMQ = TRUE;
421 }
422 }
423 else
424 haveHMQ = TRUE;
425
426 WinMessageBox(HWND_DESKTOP, NULL, msg, "Odin: Fatal Error", 0,
427 MB_APPLMODAL | MB_MOVEABLE | MB_ERROR | MB_OK);
428
429 // duplicate the message to the console just in case (PM may be not
430 // available)
431 ULONG dummy;
432 DosWrite((HFILE)1, (PVOID)&msg, strlen(msg), &dummy);
433}
434
Note: See TracBrowser for help on using the repository browser.