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

Last change on this file since 10606 was 10606, checked in by sandervl, 21 years ago

KOM: Updates

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