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 static CRITICAL_SECTION ldt_section;
54 static CRITICAL_SECTION_DEBUG critsect_debug =
57 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
58 0, 0, { (DWORD_PTR)(__FILE__ ": ldt_section") }
60 static CRITICAL_SECTION ldt_section = { &critsect_debug, -1, 0, 0, 0, 0 };
62 static DWORD server_start_time;
64 /***********************************************************************
65 * locking for LDT routines
67 static void ldt_lock(void)
69 RtlEnterCriticalSection( &ldt_section );
72 static void ldt_unlock(void)
74 RtlLeaveCriticalSection( &ldt_section );
78 /***********************************************************************
79 * KERNEL thread initialisation routine
81 static void thread_attach(void)
83 /* allocate the 16-bit stack (FIXME: should be done lazily) */
84 HGLOBAL16 hstack = WOWGlobalAlloc16( GMEM_FIXED, 0x10000 );
85 kernel_get_thread_data()->stack_sel = GlobalHandleToSel16( hstack );
86 NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR( kernel_get_thread_data()->stack_sel,
87 0x10000 - sizeof(STACK16FRAME) );
91 /***********************************************************************
92 * KERNEL thread finalisation routine
94 static void thread_detach(void)
96 /* free the 16-bit stack */
97 WOWGlobalFree16( kernel_get_thread_data()->stack_sel );
98 NtCurrentTeb()->WOW32Reserved = 0;
99 if (NtCurrentTeb()->Tib.SubSystemTib) TASK_ExitTask();
103 /***********************************************************************
104 * KERNEL process initialisation routine
106 static BOOL process_attach(void)
109 SYSTEM_TIMEOFDAY_INFORMATION sti;
111 NtQuerySystemInformation( SystemTimeOfDayInformation, &sti, sizeof(sti), NULL );
112 RtlTimeToSecondsSince1970( &sti.liKeBootTime, &server_start_time );
114 /* FIXME: should probably be done in ntdll */
115 GetSystemInfo( &si );
116 NtCurrentTeb()->Peb->NumberOfProcessors = si.dwNumberOfProcessors;
118 /* Setup registry locale information */
119 LOCALE_InitRegistry();
121 /* Setup computer name */
124 /* copy process information from ntdll */
125 ENV_CopyStartupInformation();
128 if (GetVersion() & 0x80000000)
130 /* create the shared heap for broken win95 native dlls */
131 HeapCreate( HEAP_SHARED, 0, 0 );
132 /* setup emulation of protected instructions from 32-bit code */
133 RtlAddVectoredExceptionHandler( TRUE, INSTR_vectored_handler );
137 /* initialize LDT locking */
138 wine_ldt_init_locking( ldt_lock, ldt_unlock );
140 /* finish the process initialisation for console bits, if needed */
141 __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
143 if (NtCurrentTeb()->Peb->ProcessParameters->ConsoleHandle == (HANDLE)1) /* FIXME */
145 HMODULE mod = GetModuleHandleA(0);
146 if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
149 /* else TODO for DETACHED_PROCESS:
150 * 1/ inherit console + handles
151 * 2/ create std handles, if handles are not inherited
152 * TBD when not using wineserver handles for console handles
155 /* Create 16-bit task */
156 LoadLibrary16( "krnl386.exe" );
158 TASK_CreateMainTask();
162 /***********************************************************************
163 * KERNEL initialisation routine
165 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
169 case DLL_PROCESS_ATTACH:
170 return process_attach();
171 case DLL_THREAD_ATTACH:
174 case DLL_THREAD_DETACH:
177 case DLL_PROCESS_DETACH:
178 WriteOutProfiles16();
184 /***********************************************************************
185 * MulDiv (KERNEL32.@)
187 * Result of multiplication and division
188 * -1: Overflow occurred or Divisor was 0
190 INT WINAPI MulDiv( INT nMultiplicand, INT nMultiplier, INT nDivisor)
194 if (!nDivisor) return -1;
196 /* We want to deal with a positive divisor to simplify the logic. */
199 nMultiplicand = - nMultiplicand;
200 nDivisor = -nDivisor;
203 /* If the result is positive, we "add" to round. else, we subtract to round. */
204 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
205 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
206 ret = (((LONGLONG)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
208 ret = (((LONGLONG)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
210 if ((ret > 2147483647) || (ret < -2147483647)) return -1;
215 /***********************************************************************
216 * GetTickCount (KERNEL32.@)
218 * Get the number of milliseconds the system has been running.
224 * The current tick count.
227 * -The value returned will wrap arounf every 2^32 milliseconds.
228 * -Under Windows, tick 0 is the moment at which the system is rebooted.
229 * Under Wine, tick 0 begins at the moment the wineserver process is started,
231 DWORD WINAPI GetTickCount(void)
234 gettimeofday( &t, NULL );
235 return ((t.tv_sec - server_start_time) * 1000) + (t.tv_usec / 1000);