2 * Kernel initialization code
4 * Copyright 2000 Alexandre Julliard
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.
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.
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
22 #include "wine/port.h"
28 #ifdef HAVE_SYS_STAT_H
29 # include <sys/stat.h>
32 #ifdef HAVE_SYS_TIME_H
33 # include <sys/time.h>
43 #include "wine/winbase16.h"
44 #include "wine/library.h"
47 #include "kernel_private.h"
48 #include "kernel16_private.h"
49 #include "console_private.h"
51 extern int __wine_set_signal_handler(unsigned, int (*)(unsigned));
53 extern int main_create_flags;
55 static CRITICAL_SECTION ldt_section;
56 static CRITICAL_SECTION_DEBUG critsect_debug =
59 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
60 0, 0, { 0, (DWORD)(__FILE__ ": ldt_section") }
62 static CRITICAL_SECTION ldt_section = { &critsect_debug, -1, 0, 0, 0, 0 };
64 static DWORD server_start_time;
66 /***********************************************************************
67 * locking for LDT routines
69 static void ldt_lock(void)
71 RtlEnterCriticalSection( &ldt_section );
74 static void ldt_unlock(void)
76 RtlLeaveCriticalSection( &ldt_section );
80 /***********************************************************************
81 * KERNEL thread initialisation routine
83 static void thread_attach(void)
85 /* allocate the 16-bit stack (FIXME: should be done lazily) */
86 HGLOBAL16 hstack = WOWGlobalAlloc16( GMEM_FIXED, 0x10000 );
87 kernel_get_thread_data()->stack_sel = GlobalHandleToSel16( hstack );
88 NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR( kernel_get_thread_data()->stack_sel,
89 0x10000 - sizeof(STACK16FRAME) );
93 /***********************************************************************
94 * KERNEL thread finalisation routine
96 static void thread_detach(void)
98 /* free the 16-bit stack */
99 WOWGlobalFree16( kernel_get_thread_data()->stack_sel );
100 NtCurrentTeb()->WOW32Reserved = 0;
101 if (NtCurrentTeb()->Tib.SubSystemTib) TASK_ExitTask();
105 /***********************************************************************
106 * KERNEL process initialisation routine
108 static BOOL process_attach(void)
112 SYSTEM_TIMEOFDAY_INFORMATION sti;
114 NtQuerySystemInformation( SystemTimeOfDayInformation, &sti, sizeof(sti), NULL );
115 RtlTimeToSecondsSince1970( &sti.liKeBootTime, &server_start_time );
117 /* FIXME: should probably be done in ntdll */
118 GetSystemInfo( &si );
119 NtCurrentTeb()->Peb->NumberOfProcessors = si.dwNumberOfProcessors;
121 /* Setup registry locale information */
122 LOCALE_InitRegistry();
124 /* Initialize 16-bit thunking entry points */
125 if (!WOWTHUNK_Init()) return FALSE;
127 /* Initialize DOS memory */
128 if (!DOSMEM_Init()) return FALSE;
130 /* Setup computer name */
133 /* copy process information from ntdll */
134 ENV_CopyStartupInformation();
136 if ((hModule = LoadLibrary16( "krnl386.exe" )) >= 32)
138 /* Initialize special KERNEL entry points */
140 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
141 NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
143 /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
144 NE_SetEntryPoint( hModule, 454, wine_get_cs() );
145 NE_SetEntryPoint( hModule, 455, wine_get_ds() );
147 /* Initialize KERNEL.THHOOK */
148 TASK_InstallTHHook(MapSL((SEGPTR)GetProcAddress16( hModule, (LPCSTR)332 )));
150 /* Initialize the real-mode selector entry points */
151 #define SET_ENTRY_POINT( num, addr ) \
152 NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
153 DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
154 WINE_LDT_FLAGS_DATA ))
156 SET_ENTRY_POINT( 174, 0xa0000 ); /* KERNEL.174: __A000H */
157 SET_ENTRY_POINT( 181, 0xb0000 ); /* KERNEL.181: __B000H */
158 SET_ENTRY_POINT( 182, 0xb8000 ); /* KERNEL.182: __B800H */
159 SET_ENTRY_POINT( 195, 0xc0000 ); /* KERNEL.195: __C000H */
160 SET_ENTRY_POINT( 179, 0xd0000 ); /* KERNEL.179: __D000H */
161 SET_ENTRY_POINT( 190, 0xe0000 ); /* KERNEL.190: __E000H */
162 NE_SetEntryPoint( hModule, 183, DOSMEM_0000H ); /* KERNEL.183: __0000H */
163 NE_SetEntryPoint( hModule, 173, DOSMEM_BiosSysSeg ); /* KERNEL.173: __ROMBIOS */
164 NE_SetEntryPoint( hModule, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
165 NE_SetEntryPoint( hModule, 194, DOSMEM_BiosSysSeg ); /* KERNEL.194: __F000H */
166 #undef SET_ENTRY_POINT
168 /* Force loading of some dlls */
169 LoadLibrary16( "system.drv" );
173 /* Create the shared heap for broken win95 native dlls */
174 if (GetVersion() & 0x80000000) HeapCreate( HEAP_SHARED, 0, 0 );
177 /* initialize LDT locking */
178 wine_ldt_init_locking( ldt_lock, ldt_unlock );
180 /* finish the process initialisation for console bits, if needed */
181 __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
183 if (main_create_flags & CREATE_NEW_CONSOLE)
185 HMODULE mod = GetModuleHandleA(0);
186 if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
189 else if (!(main_create_flags & DETACHED_PROCESS))
191 /* 1/ shall inherit console + handles
192 * 2/ shall create std handles, if handles are not inherited
193 * TBD when not using wineserver handles for console handles
197 if (main_create_flags & CREATE_NEW_PROCESS_GROUP)
198 SetConsoleCtrlHandler(NULL, TRUE);
200 /* Create 16-bit task */
202 TASK_CreateMainTask();
206 /***********************************************************************
207 * KERNEL initialisation routine
209 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
213 case DLL_PROCESS_ATTACH:
214 return process_attach();
215 case DLL_THREAD_ATTACH:
218 case DLL_THREAD_DETACH:
221 case DLL_PROCESS_DETACH:
222 WriteOutProfiles16();
228 /***********************************************************************
229 * EnableDos (KERNEL.41)
230 * DisableDos (KERNEL.42)
231 * GetLastDiskChange (KERNEL.98)
232 * ValidateCodeSegments (KERNEL.100)
233 * KbdRst (KERNEL.123)
234 * EnableKernel (KERNEL.124)
235 * DisableKernel (KERNEL.125)
236 * ValidateFreeSpaces (KERNEL.200)
238 * BUNNY_351 (KERNEL.351)
239 * PIGLET_361 (KERNEL.361)
241 * Entry point for kernel functions that do nothing.
243 LONG WINAPI KERNEL_nop(void)
248 /***********************************************************************
249 * MulDiv (KERNEL32.@)
251 * Result of multiplication and division
252 * -1: Overflow occurred or Divisor was 0
254 INT WINAPI MulDiv( INT nMultiplicand, INT nMultiplier, INT nDivisor)
258 if (!nDivisor) return -1;
260 /* We want to deal with a positive divisor to simplify the logic. */
263 nMultiplicand = - nMultiplicand;
264 nDivisor = -nDivisor;
267 /* If the result is positive, we "add" to round. else, we subtract to round. */
268 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
269 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
270 ret = (((LONGLONG)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
272 ret = (((LONGLONG)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
274 if ((ret > 2147483647) || (ret < -2147483647)) return -1;
279 /***********************************************************************
280 * GetSystemMSecCount (SYSTEM.6)
281 * GetTickCount (KERNEL32.@)
283 * Get the number of milliseconds the system has been running.
289 * The current tick count.
292 * -The value returned will wrap arounf every 2^32 milliseconds.
293 * -Under Windows, tick 0 is the moment at which the system is rebooted.
294 * Under Wine, tick 0 begins at the moment the wineserver process is started,
296 DWORD WINAPI GetTickCount(void)
299 gettimeofday( &t, NULL );
300 return ((t.tv_sec - server_start_time) * 1000) + (t.tv_usec / 1000);