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