oleaut32: Added Esperanto language.
[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 #include "wownt32.h"
42
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"
50
51 extern  int __wine_set_signal_handler(unsigned, int (*)(unsigned));
52
53 static CRITICAL_SECTION ldt_section;
54 static CRITICAL_SECTION_DEBUG critsect_debug =
55 {
56     0, 0, &ldt_section,
57     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
58       0, 0, { (DWORD_PTR)(__FILE__ ": ldt_section") }
59 };
60 static CRITICAL_SECTION ldt_section = { &critsect_debug, -1, 0, 0, 0, 0 };
61
62 static DWORD server_start_time;
63
64 /***********************************************************************
65  *           locking for LDT routines
66  */
67 static void ldt_lock(void)
68 {
69     RtlEnterCriticalSection( &ldt_section );
70 }
71
72 static void ldt_unlock(void)
73 {
74     RtlLeaveCriticalSection( &ldt_section );
75 }
76
77
78 /***********************************************************************
79  *           KERNEL thread initialisation routine
80  */
81 static void thread_attach(void)
82 {
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) );
88 }
89
90
91 /***********************************************************************
92  *           KERNEL thread finalisation routine
93  */
94 static void thread_detach(void)
95 {
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();
100 }
101
102
103 /***********************************************************************
104  *           KERNEL process initialisation routine
105  */
106 static BOOL process_attach(void)
107 {
108     SYSTEM_INFO si;
109     SYSTEM_TIMEOFDAY_INFORMATION sti;
110
111     NtQuerySystemInformation( SystemTimeOfDayInformation, &sti, sizeof(sti), NULL );
112     RtlTimeToSecondsSince1970( &sti.liKeBootTime, &server_start_time );
113
114     /* FIXME: should probably be done in ntdll */
115     GetSystemInfo( &si );
116     NtCurrentTeb()->Peb->NumberOfProcessors = si.dwNumberOfProcessors;
117
118     /* Setup registry locale information */
119     LOCALE_InitRegistry();
120
121     /* Setup computer name */
122     COMPUTERNAME_Init();
123
124     /* copy process information from ntdll */
125     ENV_CopyStartupInformation();
126
127 #ifdef __i386__
128     if (GetVersion() & 0x80000000)
129     {
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 );
134     }
135 #endif
136
137     /* initialize LDT locking */
138     wine_ldt_init_locking( ldt_lock, ldt_unlock );
139
140     /* finish the process initialisation for console bits, if needed */
141     __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
142
143     if (NtCurrentTeb()->Peb->ProcessParameters->ConsoleHandle == (HANDLE)1)  /* FIXME */
144     {
145         HMODULE mod = GetModuleHandleA(0);
146         if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
147             AllocConsole();
148     }
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
153      */
154
155     /* Create 16-bit task */
156     LoadLibrary16( "krnl386.exe" );
157     thread_attach();
158     TASK_CreateMainTask();
159     return TRUE;
160 }
161
162 /***********************************************************************
163  *           KERNEL initialisation routine
164  */
165 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
166 {
167     switch(reason)
168     {
169     case DLL_PROCESS_ATTACH:
170         return process_attach();
171     case DLL_THREAD_ATTACH:
172         thread_attach();
173         break;
174     case DLL_THREAD_DETACH:
175         thread_detach();
176         break;
177     case DLL_PROCESS_DETACH:
178         WriteOutProfiles16();
179         break;
180     }
181     return TRUE;
182 }
183
184 /***********************************************************************
185  *           MulDiv   (KERNEL32.@)
186  * RETURNS
187  *      Result of multiplication and division
188  *      -1: Overflow occurred or Divisor was 0
189  */
190 INT WINAPI MulDiv( INT nMultiplicand, INT nMultiplier, INT nDivisor)
191 {
192     LONGLONG ret;
193
194     if (!nDivisor) return -1;
195
196     /* We want to deal with a positive divisor to simplify the logic. */
197     if (nDivisor < 0)
198     {
199       nMultiplicand = - nMultiplicand;
200       nDivisor = -nDivisor;
201     }
202
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;
207     else
208       ret = (((LONGLONG)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
209
210     if ((ret > 2147483647) || (ret < -2147483647)) return -1;
211     return ret;
212 }
213
214
215 /***********************************************************************
216  *           GetTickCount       (KERNEL32.@)
217  *
218  * Get the number of milliseconds the system has been running.
219  *
220  * PARAMS
221  *  None.
222  *
223  * RETURNS
224  *  The current tick count.
225  *
226  * NOTES
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,
230  */
231 DWORD WINAPI GetTickCount(void)
232 {
233     struct timeval t;
234     gettimeofday( &t, NULL );
235     return ((t.tv_sec - server_start_time) * 1000) + (t.tv_usec / 1000);
236 }