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

Last change on this file since 7447 was 7447, checked in by sandervl, 24 years ago

check for double calls to inittermKernel32

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