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