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