Implemented OpenThread on top of NtOpenThread.
[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 "winternl.h"
37
38 #include "wine/winbase16.h"
39 #include "wine/library.h"
40 #include "wincon.h"
41 #include "toolhelp.h"
42 #include "kernel_private.h"
43 #include "kernel16_private.h"
44 #include "console_private.h"
45
46 extern  int __wine_set_signal_handler(unsigned, int (*)(unsigned));
47
48 extern int main_create_flags;
49
50 static CRITICAL_SECTION ldt_section;
51 static CRITICAL_SECTION_DEBUG critsect_debug =
52 {
53     0, 0, &ldt_section,
54     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
55       0, 0, { 0, (DWORD)(__FILE__ ": ldt_section") }
56 };
57 static CRITICAL_SECTION ldt_section = { &critsect_debug, -1, 0, 0, 0, 0 };
58
59 /***********************************************************************
60  *           locking for LDT routines
61  */
62 static void ldt_lock(void)
63 {
64     RtlEnterCriticalSection( &ldt_section );
65 }
66
67 static void ldt_unlock(void)
68 {
69     RtlLeaveCriticalSection( &ldt_section );
70 }
71
72
73 /***********************************************************************
74  *           KERNEL thread initialisation routine
75  */
76 static void thread_attach(void)
77 {
78     /* allocate the 16-bit stack (FIXME: should be done lazily) */
79     HGLOBAL16 hstack = K32WOWGlobalAlloc16( GMEM_FIXED, 0x10000 );
80     kernel_get_thread_data()->stack_sel = GlobalHandleToSel16( hstack );
81     NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR( kernel_get_thread_data()->stack_sel,
82                                                         0x10000 - sizeof(STACK16FRAME) );
83 }
84
85
86 /***********************************************************************
87  *           KERNEL thread finalisation routine
88  */
89 static void thread_detach(void)
90 {
91     /* free the 16-bit stack */
92     K32WOWGlobalFree16( kernel_get_thread_data()->stack_sel );
93     NtCurrentTeb()->WOW32Reserved = 0;
94     if (NtCurrentTeb()->Tib.SubSystemTib) TASK_ExitTask();
95 }
96
97
98 /***********************************************************************
99  *           KERNEL process initialisation routine
100  */
101 static BOOL process_attach(void)
102 {
103     HMODULE16 hModule;
104     SYSTEM_INFO si;
105
106     /* FIXME: should probably be done in ntdll */
107     GetSystemInfo( &si );
108     NtCurrentTeb()->Peb->NumberOfProcessors = si.dwNumberOfProcessors;
109
110     /* Setup registry locale information */
111     LOCALE_InitRegistry();
112
113     /* Initialize 16-bit thunking entry points */
114     if (!WOWTHUNK_Init()) return FALSE;
115
116     /* Initialize DOS memory */
117     if (!DOSMEM_Init()) return FALSE;
118
119     /* Setup computer name */
120     COMPUTERNAME_Init();
121
122     /* copy process information from ntdll */
123     ENV_CopyStartupInformation();
124
125     if ((hModule = LoadLibrary16( "krnl386.exe" )) >= 32)
126     {
127         /* Initialize special KERNEL entry points */
128
129         /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
130         NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
131
132         /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
133         NE_SetEntryPoint( hModule, 454, wine_get_cs() );
134         NE_SetEntryPoint( hModule, 455, wine_get_ds() );
135
136         /* Initialize KERNEL.THHOOK */
137         TASK_InstallTHHook(MapSL((SEGPTR)GetProcAddress16( hModule, (LPCSTR)332 )));
138
139         /* Initialize the real-mode selector entry points */
140 #define SET_ENTRY_POINT( num, addr ) \
141     NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
142                       DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
143                       WINE_LDT_FLAGS_DATA ))
144
145         SET_ENTRY_POINT( 174, 0xa0000 );  /* KERNEL.174: __A000H */
146         SET_ENTRY_POINT( 181, 0xb0000 );  /* KERNEL.181: __B000H */
147         SET_ENTRY_POINT( 182, 0xb8000 );  /* KERNEL.182: __B800H */
148         SET_ENTRY_POINT( 195, 0xc0000 );  /* KERNEL.195: __C000H */
149         SET_ENTRY_POINT( 179, 0xd0000 );  /* KERNEL.179: __D000H */
150         SET_ENTRY_POINT( 190, 0xe0000 );  /* KERNEL.190: __E000H */
151         NE_SetEntryPoint( hModule, 183, DOSMEM_0000H );       /* KERNEL.183: __0000H */
152         NE_SetEntryPoint( hModule, 173, DOSMEM_BiosSysSeg );  /* KERNEL.173: __ROMBIOS */
153         NE_SetEntryPoint( hModule, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
154         NE_SetEntryPoint( hModule, 194, DOSMEM_BiosSysSeg );  /* KERNEL.194: __F000H */
155 #undef SET_ENTRY_POINT
156
157         /* Force loading of some dlls */
158         LoadLibrary16( "system.drv" );
159     }
160
161 #ifdef __i386__
162     /* Create the shared heap for broken win95 native dlls */
163     if (GetVersion() & 0x80000000) HeapCreate( HEAP_SHARED, 0, 0 );
164 #endif
165
166     /* initialize LDT locking */
167     wine_ldt_init_locking( ldt_lock, ldt_unlock );
168
169     /* finish the process initialisation for console bits, if needed */
170     __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
171
172     if (main_create_flags & CREATE_NEW_CONSOLE)
173     {
174         HMODULE mod = GetModuleHandleA(0);
175         if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
176             AllocConsole();
177     }
178     else if (!(main_create_flags & DETACHED_PROCESS))
179     {
180         /* 1/ shall inherit console + handles
181          * 2/ shall create std handles, if handles are not inherited
182          * TBD when not using wineserver handles for console handles
183          */
184     }
185
186     if (main_create_flags & CREATE_NEW_PROCESS_GROUP)
187         SetConsoleCtrlHandler(NULL, TRUE);
188
189     /* Create 16-bit task */
190     thread_attach();
191     TASK_CreateMainTask();
192     return TRUE;
193 }
194
195 /***********************************************************************
196  *           KERNEL initialisation routine
197  */
198 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
199 {
200     switch(reason)
201     {
202     case DLL_PROCESS_ATTACH:
203         return process_attach();
204     case DLL_THREAD_ATTACH:
205         thread_attach();
206         break;
207     case DLL_THREAD_DETACH:
208         thread_detach();
209         break;
210     case DLL_PROCESS_DETACH:
211         WriteOutProfiles16();
212         break;
213     }
214     return TRUE;
215 }
216
217 /***********************************************************************
218  *              EnableDos (KERNEL.41)
219  *              DisableDos (KERNEL.42)
220  *              GetLastDiskChange (KERNEL.98)
221  *              ValidateCodeSegments (KERNEL.100)
222  *              KbdRst (KERNEL.123)
223  *              EnableKernel (KERNEL.124)
224  *              DisableKernel (KERNEL.125)
225  *              ValidateFreeSpaces (KERNEL.200)
226  *              K237 (KERNEL.237)
227  *              BUNNY_351 (KERNEL.351)
228  *              PIGLET_361 (KERNEL.361)
229  *
230  * Entry point for kernel functions that do nothing.
231  */
232 LONG WINAPI KERNEL_nop(void)
233 {
234     return 0;
235 }
236
237 /***********************************************************************
238  *           MulDiv   (KERNEL32.@)
239  * RETURNS
240  *      Result of multiplication and division
241  *      -1: Overflow occurred or Divisor was 0
242  */
243 INT WINAPI MulDiv( INT nMultiplicand, INT nMultiplier, INT nDivisor)
244 {
245     LONGLONG ret;
246
247     if (!nDivisor) return -1;
248
249     /* We want to deal with a positive divisor to simplify the logic. */
250     if (nDivisor < 0)
251     {
252       nMultiplicand = - nMultiplicand;
253       nDivisor = -nDivisor;
254     }
255
256     /* If the result is positive, we "add" to round. else, we subtract to round. */
257     if ( ( (nMultiplicand <  0) && (nMultiplier <  0) ) ||
258          ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
259       ret = (((LONGLONG)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
260     else
261       ret = (((LONGLONG)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
262
263     if ((ret > 2147483647) || (ret < -2147483647)) return -1;
264     return ret;
265 }