Release 0.9.4.
[wine/multimedia.git] / dlls / kernel / kernel_main.c
bloba9b76f19db70b6b5f675debfdbf98aa45f0e0c8d
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
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>
37 #include "windef.h"
38 #include "winbase.h"
39 #include "wincon.h"
40 #include "winternl.h"
41 #include "wownt32.h"
43 #include "wine/winbase16.h"
44 #include "wine/library.h"
45 #include "wincon.h"
46 #include "toolhelp.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 =
58 0, 0, &ldt_section,
59 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
60 0, 0, { (DWORD_PTR)(__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)
110 SYSTEM_INFO si;
111 SYSTEM_TIMEOFDAY_INFORMATION sti;
113 NtQuerySystemInformation( SystemTimeOfDayInformation, &sti, sizeof(sti), NULL );
114 RtlTimeToSecondsSince1970( &sti.liKeBootTime, &server_start_time );
116 /* FIXME: should probably be done in ntdll */
117 GetSystemInfo( &si );
118 NtCurrentTeb()->Peb->NumberOfProcessors = si.dwNumberOfProcessors;
120 /* Setup registry locale information */
121 LOCALE_InitRegistry();
123 /* Setup computer name */
124 COMPUTERNAME_Init();
126 /* copy process information from ntdll */
127 ENV_CopyStartupInformation();
129 #ifdef __i386__
130 if (GetVersion() & 0x80000000)
132 /* create the shared heap for broken win95 native dlls */
133 HeapCreate( HEAP_SHARED, 0, 0 );
134 /* setup emulation of protected instructions from 32-bit code */
135 RtlAddVectoredExceptionHandler( TRUE, INSTR_vectored_handler );
137 #endif
139 /* initialize LDT locking */
140 wine_ldt_init_locking( ldt_lock, ldt_unlock );
142 /* finish the process initialisation for console bits, if needed */
143 __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
145 if (main_create_flags & CREATE_NEW_CONSOLE)
147 HMODULE mod = GetModuleHandleA(0);
148 if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
149 AllocConsole();
151 else if (!(main_create_flags & DETACHED_PROCESS))
153 /* 1/ shall inherit console + handles
154 * 2/ shall create std handles, if handles are not inherited
155 * TBD when not using wineserver handles for console handles
159 if (main_create_flags & CREATE_NEW_PROCESS_GROUP)
160 SetConsoleCtrlHandler(NULL, TRUE);
162 /* Create 16-bit task */
163 LoadLibrary16( "krnl386.exe" );
164 thread_attach();
165 TASK_CreateMainTask();
166 return TRUE;
169 /***********************************************************************
170 * KERNEL initialisation routine
172 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
174 switch(reason)
176 case DLL_PROCESS_ATTACH:
177 return process_attach();
178 case DLL_THREAD_ATTACH:
179 thread_attach();
180 break;
181 case DLL_THREAD_DETACH:
182 thread_detach();
183 break;
184 case DLL_PROCESS_DETACH:
185 WriteOutProfiles16();
186 break;
188 return TRUE;
191 /***********************************************************************
192 * MulDiv (KERNEL32.@)
193 * RETURNS
194 * Result of multiplication and division
195 * -1: Overflow occurred or Divisor was 0
197 INT WINAPI MulDiv( INT nMultiplicand, INT nMultiplier, INT nDivisor)
199 LONGLONG ret;
201 if (!nDivisor) return -1;
203 /* We want to deal with a positive divisor to simplify the logic. */
204 if (nDivisor < 0)
206 nMultiplicand = - nMultiplicand;
207 nDivisor = -nDivisor;
210 /* If the result is positive, we "add" to round. else, we subtract to round. */
211 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
212 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
213 ret = (((LONGLONG)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
214 else
215 ret = (((LONGLONG)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
217 if ((ret > 2147483647) || (ret < -2147483647)) return -1;
218 return ret;
222 /***********************************************************************
223 * GetTickCount (KERNEL32.@)
225 * Get the number of milliseconds the system has been running.
227 * PARAMS
228 * None.
230 * RETURNS
231 * The current tick count.
233 * NOTES
234 * -The value returned will wrap arounf every 2^32 milliseconds.
235 * -Under Windows, tick 0 is the moment at which the system is rebooted.
236 * Under Wine, tick 0 begins at the moment the wineserver process is started,
238 DWORD WINAPI GetTickCount(void)
240 struct timeval t;
241 gettimeofday( &t, NULL );
242 return ((t.tv_sec - server_start_time) * 1000) + (t.tv_usec / 1000);