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