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

Last change on this file since 8010 was 8010, checked in by sandervl, 23 years ago

vac 3.6.5 compile fix

File size: 8.8 KB
Line 
1/* $Id: initkernel32.cpp,v 1.13 2002-02-24 20:29:56 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 <cpuhlp.h>
55#include <Win32k.h>
56#include <initdll.h>
57#include <codepage.h>
58#include <process.h>
59#include <stats.h>
60
61#define DBG_LOCALLOG DBG_initterm
62#include "dbglocal.h"
63
64PVOID SYSTEM _O32_GetEnvironmentStrings( VOID );
65
66extern "C" {
67 //Win32 resource table (produced by wrc)
68 extern DWORD kernel32_PEResTab;
69}
70
71 ULONG flAllocMem = 0; /* flag to optimize DosAllocMem to use all the memory on SMP machines */
72 ULONG ulMaxAddr = 0x20000000; /* end of user address space. */
73 int loadNr = 0;
74 char kernel32Path[CCHMAXPATH] = "";
75static HMODULE dllHandle = 0;
76 BOOL fInit = FALSE;
77
78/****************************************************************************/
79/* _DLL_InitTerm is the function that gets called by the operating system */
80/* loader when it loads and frees this DLL for each process that accesses */
81/* this DLL. However, it only gets called the first time the DLL is loaded */
82/* and the last time it is freed for a particular process. The system */
83/* linkage convention MUST be used because the operating system loader is */
84/* calling this function. */
85/****************************************************************************/
86ULONG APIENTRY inittermKernel32(ULONG hModule, ULONG ulFlag)
87{
88 size_t i;
89 APIRET rc;
90 ULONG ulSysinfo, version[2];
91
92 /*-------------------------------------------------------------------------*/
93 /* If ulFlag is zero then the DLL is being loaded so initialization should */
94 /* be performed. If ulFlag is 1 then the DLL is being freed so */
95 /* termination should be performed. */
96 /*-------------------------------------------------------------------------*/
97
98 if(fInit == TRUE && ulFlag == 0) {
99 return 1; //already initialized
100 }
101 fInit = TRUE;
102
103 switch (ulFlag)
104 {
105 case 0 :
106 {
107 ParseLogStatusKERNEL32();
108
109 /*
110 * Init the win32k library.
111 * We will also need to tell win32k where the Odin32 environment is
112 * located. Currently that is within Open32. I'm quite sure that it's
113 * not relocated during run, so we're pretty well off.
114 */
115 if (!libWin32kInit())
116 {
117 rc = libWin32kSetEnvironment((PSZ)_O32_GetEnvironmentStrings(), 0, 0);
118 if (rc)
119 {
120 dprintf(("KERNEL32: initterm: libWin32kSetEnvironment failed with rc=%d\n", rc));
121 }
122 }
123
124 strcpy(kernel32Path, OSLibGetDllName(hModule));
125 char *endofpath = strrchr(kernel32Path, '\\');
126 *(endofpath+1) = 0;
127 dprintf(("kernel32 init %s %s (%x) Win32k - %s", __DATE__, __TIME__, inittermKernel32,
128 libWin32kInstalled() ? "Installed" : "Not Installed"));
129
130 OpenPrivateLogFiles();
131
132 if (InitializeSharedHeap() == FALSE)
133 return 0UL;
134
135 if (InitializeCodeHeap() == FALSE)
136 return 0UL;
137
138 PROFILE_LoadOdinIni();
139 dllHandle = RegisterLxDll(hModule, 0, (PVOID)&kernel32_PEResTab);
140 if (dllHandle == 0)
141 return 0UL;
142
143 //SvL: Kernel32 is a special case; pe.exe loads it, so increase
144 // the reference count here
145 Win32DllBase *module = Win32DllBase::findModule(dllHandle);
146 if (module)
147 {
148 module->AddRef();
149 module->DisableUnload();
150 }
151
152 /* knut: check for high memory support */
153 rc = DosQuerySysInfo(QSV_VIRTUALADDRESSLIMIT, QSV_VIRTUALADDRESSLIMIT, &ulSysinfo, sizeof(ulSysinfo));
154 if (rc == 0 && ulSysinfo > 512) //VirtualAddresslimit is in MB
155 {
156 flAllocMem = PAG_ANY; // high memory support. Let's use it!
157 ulMaxAddr = ulSysinfo * (1024*1024);
158 OSLibInitWSeBFileIO();
159 if (PROFILE_GetOdinIniInt(ODINSYSTEM_SECTION, HIGHMEM_KEY, 1) == 0) {
160 dprintf(("WARNING: OS/2 kernel supports high memory, but support is DISABLED because of HIGHMEM odin.ini key"));
161 flAllocMem = 0;
162 }
163 }
164 else
165 flAllocMem = 0; // no high memory support
166
167 OSLibDosSetInitialMaxFileHandles(ODIN_DEFAULT_MAX_FILEHANDLES);
168
169 //SvL: Do it here instead of during the exe object creation
170 //(std handles can be used in win32 dll initialization routines
171 HMInitialize(); /* store standard handles within HandleManager */
172 InitDirectories(); //Must be done before InitializeTIB (which loads NTDLL -> USER32)
173 InitializeTIB(TRUE); //Must be done after HMInitialize!
174 RegisterDevices();
175 Win32DllBase::setDefaultRenaming();
176 rc = DosQuerySysInfo(QSV_NUMPROCESSORS, QSV_NUMPROCESSORS, &ulSysinfo, sizeof(ulSysinfo));
177 if (rc != 0)
178 ulSysinfo = 1;
179
180 /* Setup codepage info */
181 CODEPAGE_Init();
182
183 InitSystemInfo(ulSysinfo);
184 //Set up environment as found in NT
185 InitEnvironment(ulSysinfo);
186
187 //InitDynamicRegistry creates/changes keys that may change (i.e. odin.ini
188 //keys that affect windows version)
189 InitDynamicRegistry();
190
191 //Set the process affinity mask to the system affinity mask
192 DWORD dwProcessAffinityMask, dwSystemAffinityMask;
193 GetProcessAffinityMask(GetCurrentProcess(), &dwProcessAffinityMask, &dwSystemAffinityMask);
194 SetProcessAffinityMask(GetCurrentProcess(), dwSystemAffinityMask);
195 break;
196 }
197
198 case 1 :
199 if (dllHandle)
200 {
201 UnregisterLxDll(dllHandle);
202 }
203 break;
204
205 default :
206 return 0UL;
207 }
208
209 /***********************************************************/
210 /* A non-zero value must be returned to indicate success. */
211 /***********************************************************/
212 return 1UL;
213}
214
215
216void APIENTRY cleanupKernel32(ULONG ulReason)
217{
218 dprintf(("kernel32 exit %d\n", ulReason));
219
220 //Flush and delete all open memory mapped files
221 Win32MemMap::deleteAll();
222 WinExe = NULL;
223
224 WriteOutProfiles();
225 DestroyTIB();
226 DestroySharedHeap();
227 DestroyCodeHeap();
228
229#if defined(DEBUG) && defined(__IBMCPP__) && __IBMCPP__ == 300
230 ULONG totalmemalloc, nrcalls_malloc, nrcalls_free;
231
232 getcrtstat(&nrcalls_malloc, &nrcalls_free, &totalmemalloc);
233 dprintf(("************* KERNEL32 STATISTICS BEGIN *****************"));
234 dprintf(("Total nr of malloc calls %d", nrcalls_malloc));
235 dprintf(("Total nr of free calls %d", nrcalls_free));
236 dprintf(("Leaked memory: %d bytes", totalmemalloc));
237 dprintf(("************* KERNEL32 STATISTICS END *****************"));
238#endif
239
240 //NOTE: Must be done after DestroyTIB
241 ClosePrivateLogFiles();
242 CloseLogFile();
243
244 /*
245 * Terminate win32k library.
246 */
247 libWin32kSetEnvironment(NULL, 0, 0);
248 libWin32kTerm();
249 return ;
250}
251//******************************************************************************
252//******************************************************************************
Note: See TracBrowser for help on using the repository browser.