Store the 16-bit stack pointer in the WOW32Reserved TEB field.
[wine] / dlls / kernel / kernel_main.c
1 /*
2  * Kernel initialization code
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #ifdef HAVE_SYS_STAT_H
29 # include <sys/stat.h>
30 #endif
31 #include <signal.h>
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wincon.h"
36 #include "winreg.h"
37 #include "winternl.h"
38
39 #include "wine/winbase16.h"
40 #include "wine/library.h"
41 #include "module.h"
42 #include "thread.h"
43 #include "stackframe.h"
44 #include "wincon.h"
45 #include "toolhelp.h"
46 #include "kernel_private.h"
47 #include "console_private.h"
48
49 extern void LOCALE_InitRegistry(void);
50 extern void COMPUTERNAME_Init(void);
51
52 extern  int __wine_set_signal_handler(unsigned, int (*)(unsigned));
53 /* memory/environ.c */
54 extern void ENV_CopyStartupInformation(void);
55
56 extern int main_create_flags;
57
58 static CRITICAL_SECTION ldt_section;
59 static CRITICAL_SECTION_DEBUG critsect_debug =
60 {
61     0, 0, &ldt_section,
62     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
63       0, 0, { 0, (DWORD)(__FILE__ ": ldt_section") }
64 };
65 static CRITICAL_SECTION ldt_section = { &critsect_debug, -1, 0, 0, 0, 0 };
66
67 /***********************************************************************
68  *           locking for LDT routines
69  */
70 static void ldt_lock(void)
71 {
72     RtlEnterCriticalSection( &ldt_section );
73 }
74
75 static void ldt_unlock(void)
76 {
77     RtlLeaveCriticalSection( &ldt_section );
78 }
79
80
81 /***********************************************************************
82  *           KERNEL thread initialisation routine
83  */
84 static void thread_attach(void)
85 {
86     /* allocate the 16-bit stack (FIXME: should be done lazily) */
87     HGLOBAL16 hstack = K32WOWGlobalAlloc16( GMEM_FIXED, 0x10000 );
88     NtCurrentTeb()->stack_sel = GlobalHandleToSel16( hstack );
89     NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR( NtCurrentTeb()->stack_sel,
90                                                         0x10000 - sizeof(STACK16FRAME) );
91 }
92
93
94 /***********************************************************************
95  *           KERNEL thread finalisation routine
96  */
97 static void thread_detach(void)
98 {
99     /* free the 16-bit stack */
100     K32WOWGlobalFree16( NtCurrentTeb()->stack_sel );
101     NtCurrentTeb()->WOW32Reserved = 0;
102     if (NtCurrentTeb()->Tib.SubSystemTib) TASK_ExitTask();
103 }
104
105
106 /***********************************************************************
107  *           KERNEL process initialisation routine
108  */
109 static BOOL process_attach(void)
110 {
111     HMODULE16 hModule;
112     SYSTEM_INFO si;
113
114     /* FIXME: should probably be done in ntdll */
115     GetSystemInfo( &si );
116     NtCurrentTeb()->Peb->NumberOfProcessors = si.dwNumberOfProcessors;
117
118     /* Setup registry locale information */
119     LOCALE_InitRegistry();
120
121     /* Initialize 16-bit thunking entry points */
122     if (!WOWTHUNK_Init()) return FALSE;
123
124     /* Initialize DOS memory */
125     if (!DOSMEM_Init()) return FALSE;
126
127     /* Setup computer name */
128     COMPUTERNAME_Init();
129
130     /* copy process information from ntdll */
131     ENV_CopyStartupInformation();
132
133     if ((hModule = LoadLibrary16( "krnl386.exe" )) >= 32)
134     {
135         /* Initialize special KERNEL entry points */
136
137         /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
138         NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
139
140         /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
141         NE_SetEntryPoint( hModule, 454, wine_get_cs() );
142         NE_SetEntryPoint( hModule, 455, wine_get_ds() );
143
144         /* Initialize KERNEL.THHOOK */
145         TASK_InstallTHHook(MapSL((SEGPTR)GetProcAddress16( hModule, (LPCSTR)332 )));
146
147         /* Initialize the real-mode selector entry points */
148 #define SET_ENTRY_POINT( num, addr ) \
149     NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
150                       DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
151                       WINE_LDT_FLAGS_DATA ))
152
153         SET_ENTRY_POINT( 174, 0xa0000 );  /* KERNEL.174: __A000H */
154         SET_ENTRY_POINT( 181, 0xb0000 );  /* KERNEL.181: __B000H */
155         SET_ENTRY_POINT( 182, 0xb8000 );  /* KERNEL.182: __B800H */
156         SET_ENTRY_POINT( 195, 0xc0000 );  /* KERNEL.195: __C000H */
157         SET_ENTRY_POINT( 179, 0xd0000 );  /* KERNEL.179: __D000H */
158         SET_ENTRY_POINT( 190, 0xe0000 );  /* KERNEL.190: __E000H */
159         NE_SetEntryPoint( hModule, 183, DOSMEM_0000H );       /* KERNEL.183: __0000H */
160         NE_SetEntryPoint( hModule, 173, DOSMEM_BiosSysSeg );  /* KERNEL.173: __ROMBIOS */
161         NE_SetEntryPoint( hModule, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
162         NE_SetEntryPoint( hModule, 194, DOSMEM_BiosSysSeg );  /* KERNEL.194: __F000H */
163 #undef SET_ENTRY_POINT
164
165         /* Force loading of some dlls */
166         LoadLibrary16( "system.drv" );
167     }
168
169 #ifdef __i386__
170     /* Create the shared heap for broken win95 native dlls */
171     if (GetVersion() & 0x80000000) HeapCreate( HEAP_SHARED, 0, 0 );
172 #endif
173
174     /* initialize LDT locking */
175     wine_ldt_init_locking( ldt_lock, ldt_unlock );
176
177     /* finish the process initialisation for console bits, if needed */
178     __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
179
180     if (main_create_flags & CREATE_NEW_CONSOLE)
181     {
182         HMODULE mod = GetModuleHandleA(0);
183         if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
184             AllocConsole();
185     }
186     else if (!(main_create_flags & DETACHED_PROCESS))
187     {
188         /* 1/ shall inherit console + handles
189          * 2/ shall create std handles, if handles are not inherited
190          * TBD when not using wineserver handles for console handles
191          */
192     }
193
194     if (main_create_flags & CREATE_NEW_PROCESS_GROUP)
195         SetConsoleCtrlHandler(NULL, TRUE);
196
197     /* Create 16-bit task */
198     thread_attach();
199     TASK_CreateMainTask();
200     return TRUE;
201 }
202
203 /***********************************************************************
204  *           KERNEL initialisation routine
205  */
206 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
207 {
208     switch(reason)
209     {
210     case DLL_PROCESS_ATTACH:
211         return process_attach();
212     case DLL_THREAD_ATTACH:
213         thread_attach();
214         break;
215     case DLL_THREAD_DETACH:
216         thread_detach();
217         break;
218     case DLL_PROCESS_DETACH:
219         WriteOutProfiles16();
220         break;
221     }
222     return TRUE;
223 }
224
225 /***********************************************************************
226  *              EnableDos (KERNEL.41)
227  *              DisableDos (KERNEL.42)
228  *              GetLastDiskChange (KERNEL.98)
229  *              ValidateCodeSegments (KERNEL.100)
230  *              KbdRst (KERNEL.123)
231  *              EnableKernel (KERNEL.124)
232  *              DisableKernel (KERNEL.125)
233  *              ValidateFreeSpaces (KERNEL.200)
234  *              K237 (KERNEL.237)
235  *              BUNNY_351 (KERNEL.351)
236  *              PIGLET_361 (KERNEL.361)
237  *
238  * Entry point for kernel functions that do nothing.
239  */
240 LONG WINAPI KERNEL_nop(void)
241 {
242     return 0;
243 }
244
245 /***********************************************************************
246  *           MulDiv   (KERNEL32.@)
247  * RETURNS
248  *      Result of multiplication and division
249  *      -1: Overflow occurred or Divisor was 0
250  */
251 INT WINAPI MulDiv( INT nMultiplicand, INT nMultiplier, INT nDivisor)
252 {
253     LONGLONG ret;
254
255     if (!nDivisor) return -1;
256
257     /* We want to deal with a positive divisor to simplify the logic. */
258     if (nDivisor < 0)
259     {
260       nMultiplicand = - nMultiplicand;
261       nDivisor = -nDivisor;
262     }
263
264     /* If the result is positive, we "add" to round. else, we subtract to round. */
265     if ( ( (nMultiplicand <  0) && (nMultiplier <  0) ) ||
266          ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
267       ret = (((LONGLONG)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
268     else
269       ret = (((LONGLONG)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
270
271     if ((ret > 2147483647) || (ret < -2147483647)) return -1;
272     return ret;
273 }