source: branches/gcc-kmk/src/kernel32/initkernel32.cpp@ 21716

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

Place local includes in quotes.

GCC doesn't search for <> includes in the current directory (intentionally).

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