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

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

CreateProcess & memory map fixes

File size: 7.8 KB
Line 
1/*
2 * KERNEL32 DLL entry point
3 *
4 * Copyright 1998 Sander van Leeuwen
5 * Copyright 1998 Peter Fitzsimmons
6 *
7 *
8 * Project Odin Software License can be found in LICENSE.TXT
9 *
10 */
11
12/*-------------------------------------------------------------*/
13/* INITERM.C -- Source for a custom dynamic link library */
14/* initialization and termination (_DLL_InitTerm) */
15/* function. */
16/* */
17/* When called to perform initialization, this sample function */
18/* gets storage for an array of integers, and initializes its */
19/* elements with random integers. At termination time, it */
20/* frees the array. Substitute your own special processing. */
21/*-------------------------------------------------------------*/
22
23
24/* Include files */
25#define INCL_DOSMODULEMGR
26#define INCL_DOSMISC
27#define INCL_DOSPROCESS
28#define INCL_DOSSEMAPHORES
29#include <os2wrap.h> //Odin32 OS/2 api wrappers
30#include <stdlib.h>
31#include <stdio.h>
32#include <string.h>
33#include <misc.h>
34#include <wprocess.h>
35#include "handlemanager.h"
36#include "profile.h"
37#include <options.h>
38#include "initterm.h"
39#include <win32type.h>
40#include <odinlx.h>
41#include "oslibmisc.h"
42#include <heapshared.h>
43#include <heapcode.h>
44#include "mmap.h"
45#include "directory.h"
46#include "hmdevio.h"
47#include <windllbase.h>
48#include "winexepe2lx.h"
49#include <exitlist.h>
50#include "oslibdos.h"
51#include <cpuhlp.h>
52#include <Win32k.h>
53#include <initdll.h>
54#include <codepage.h>
55#include <process.h>
56
57#define DBG_LOCALLOG DBG_initterm
58#include "dbglocal.h"
59
60PVOID SYSTEM _O32_GetEnvironmentStrings( VOID );
61
62extern "C" {
63 //Win32 resource table (produced by wrc)
64 extern DWORD kernel32_PEResTab;
65}
66
67 ULONG flAllocMem = 0; /* flag to optimize DosAllocMem to use all the memory on SMP machines */
68 ULONG ulMaxAddr = 0x20000000; /* end of user address space. */
69 int loadNr = 0;
70 char kernel32Path[CCHMAXPATH] = "";
71static HMODULE dllHandle = 0;
72
73/****************************************************************************/
74/* _DLL_InitTerm is the function that gets called by the operating system */
75/* loader when it loads and frees this DLL for each process that accesses */
76/* this DLL. However, it only gets called the first time the DLL is loaded */
77/* and the last time it is freed for a particular process. The system */
78/* linkage convention MUST be used because the operating system loader is */
79/* calling this function. */
80/****************************************************************************/
81ULONG APIENTRY inittermKernel32(ULONG hModule, ULONG ulFlag)
82{
83 size_t i;
84 APIRET rc;
85 ULONG ulSysinfo, version[2];
86
87 /*-------------------------------------------------------------------------*/
88 /* If ulFlag is zero then the DLL is being loaded so initialization should */
89 /* be performed. If ulFlag is 1 then the DLL is being freed so */
90 /* termination should be performed. */
91 /*-------------------------------------------------------------------------*/
92
93 switch (ulFlag)
94 {
95 case 0 :
96 {
97 ParseLogStatusKERNEL32();
98
99 /*
100 * Init the win32k library.
101 * We will also need to tell win32k where the Odin32 environment is
102 * located. Currently that is within Open32. I'm quite sure that it's
103 * not relocated during run, so we're pretty well off.
104 */
105 if (!libWin32kInit())
106 {
107 rc = libWin32kSetEnvironment((PSZ)_O32_GetEnvironmentStrings(), 0, 0);
108 if (rc)
109 {
110 dprintf(("KERNEL32: initterm: libWin32kSetEnvironment failed with rc=%d\n", rc));
111 }
112 }
113
114 strcpy(kernel32Path, OSLibGetDllName(hModule));
115 char *endofpath = strrchr(kernel32Path, '\\');
116 *(endofpath+1) = 0;
117 dprintf(("kernel32 init %s %s (%x) Win32k - %s", __DATE__, __TIME__, inittermKernel32,
118 libWin32kInstalled() ? "Installed" : "Not Installed"));
119
120 OpenPrivateLogFiles();
121
122 if (InitializeSharedHeap() == FALSE)
123 return 0UL;
124
125 if (InitializeCodeHeap() == FALSE)
126 return 0UL;
127
128 PROFILE_LoadOdinIni();
129 dllHandle = RegisterLxDll(hModule, 0, (PVOID)&kernel32_PEResTab);
130 if (dllHandle == 0)
131 return 0UL;
132
133 //SvL: Kernel32 is a special case; pe.exe loads it, so increase
134 // the reference count here
135 Win32DllBase *module = Win32DllBase::findModule(dllHandle);
136 if (module)
137 {
138 module->AddRef();
139 module->DisableUnload();
140 }
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 OSLibDosSetInitialMaxFileHandles(ODIN_DEFAULT_MAX_FILEHANDLES);
158
159 //SvL: Do it here instead of during the exe object creation
160 //(std handles can be used in win32 dll initialization routines
161 HMInitialize(); /* store standard handles within HandleManager */
162 InitDirectories(); //Must be done before InitializeTIB (which loads NTDLL -> USER32)
163 InitializeTIB(TRUE); //Must be done after HMInitialize!
164 RegisterDevices();
165 Win32DllBase::setDefaultRenaming();
166 rc = DosQuerySysInfo(QSV_NUMPROCESSORS, QSV_NUMPROCESSORS, &ulSysinfo, sizeof(ulSysinfo));
167 if (rc != 0)
168 ulSysinfo = 1;
169
170 /* Setup codepage info */
171 CODEPAGE_Init();
172
173 InitSystemInfo(ulSysinfo);
174 //Set up environment as found in NT
175 InitEnvironment(ulSysinfo);
176
177 //InitDynamicRegistry creates/changes keys that may change (i.e. odin.ini
178 //keys that affect windows version)
179 InitDynamicRegistry();
180 break;
181 }
182
183 case 1 :
184 if (dllHandle)
185 {
186 UnregisterLxDll(dllHandle);
187 }
188 break;
189
190 default :
191 return 0UL;
192 }
193
194 /***********************************************************/
195 /* A non-zero value must be returned to indicate success. */
196 /***********************************************************/
197 return 1UL;
198}
199
200
201void APIENTRY cleanupKernel32(ULONG ulReason)
202{
203 dprintf(("kernel32 exit %d\n", ulReason));
204 //Flush and delete all open memory mapped files
205 Win32MemMap::deleteAll();
206 WinExe = NULL;
207
208 WriteOutProfiles();
209 DestroyTIB();
210 DestroySharedHeap();
211 DestroyCodeHeap();
212
213 //NOTE: Must be done after DestroyTIB
214 ClosePrivateLogFiles();
215 CloseLogFile();
216
217 /*
218 * Terminate win32k library.
219 */
220 libWin32kSetEnvironment(NULL, 0, 0);
221 libWin32kTerm();
222 return ;
223}
224//******************************************************************************
225//******************************************************************************
Note: See TracBrowser for help on using the repository browser.