Pass characters to get_file_name() helper, not bytes.
[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 extern int main_create_flags;
54
55 static CRITICAL_SECTION ldt_section;
56 static CRITICAL_SECTION_DEBUG critsect_debug =
57 {
58     0, 0, &ldt_section,
59     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
60       0, 0, { (DWORD_PTR)(__FILE__ ": ldt_section") }
61 };
62 static CRITICAL_SECTION ldt_section = { &critsect_debug, -1, 0, 0, 0, 0 };
63
64 static DWORD server_start_time;
65
66 /***********************************************************************
67  *           locking for LDT routines
68  */
69 static void ldt_lock(void)
70 {
71     RtlEnterCriticalSection( &ldt_section );
72 }
73
74 static void ldt_unlock(void)
75 {
76     RtlLeaveCriticalSection( &ldt_section );
77 }
78
79
80 /***********************************************************************
81  *           KERNEL thread initialisation routine
82  */
83 static void thread_attach(void)
84 {
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) );
90 }
91
92
93 /***********************************************************************
94  *           KERNEL thread finalisation routine
95  */
96 static void thread_detach(void)
97 {
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();
102 }
103
104
105 /***********************************************************************
106  *           KERNEL process initialisation routine
107  */
108 static BOOL process_attach(void)
109 {
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     /* Setup computer name */
124     COMPUTERNAME_Init();
125
126     /* copy process information from ntdll */
127     ENV_CopyStartupInformation();
128
129 #ifdef __i386__
130     if (GetVersion() & 0x80000000)
131     {
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 );
136     }
137 #endif
138
139     /* initialize LDT locking */
140     wine_ldt_init_locking( ldt_lock, ldt_unlock );
141
142     /* finish the process initialisation for console bits, if needed */
143     __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
144
145     if (main_create_flags & CREATE_NEW_CONSOLE)
146     {
147         HMODULE mod = GetModuleHandleA(0);
148         if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
149             AllocConsole();
150     }
151     else if (!(main_create_flags & DETACHED_PROCESS))
152     {
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
156          */
157     }
158
159     if (main_create_flags & CREATE_NEW_PROCESS_GROUP)
160         SetConsoleCtrlHandler(NULL, TRUE);
161
162     /* Create 16-bit task */
163     LoadLibrary16( "krnl386.exe" );
164     thread_attach();
165     TASK_CreateMainTask();
166     return TRUE;
167 }
168
169 /***********************************************************************
170  *           KERNEL initialisation routine
171  */
172 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
173 {
174     switch(reason)
175     {
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;
187     }
188     return TRUE;
189 }
190
191 /***********************************************************************
192  *           MulDiv   (KERNEL32.@)
193  * RETURNS
194  *      Result of multiplication and division
195  *      -1: Overflow occurred or Divisor was 0
196  */
197 INT WINAPI MulDiv( INT nMultiplicand, INT nMultiplier, INT nDivisor)
198 {
199     LONGLONG ret;
200
201     if (!nDivisor) return -1;
202
203     /* We want to deal with a positive divisor to simplify the logic. */
204     if (nDivisor < 0)
205     {
206       nMultiplicand = - nMultiplicand;
207       nDivisor = -nDivisor;
208     }
209
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;
216
217     if ((ret > 2147483647) || (ret < -2147483647)) return -1;
218     return ret;
219 }
220
221
222 /***********************************************************************
223  *           GetTickCount       (KERNEL32.@)
224  *
225  * Get the number of milliseconds the system has been running.
226  *
227  * PARAMS
228  *  None.
229  *
230  * RETURNS
231  *  The current tick count.
232  *
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,
237  */
238 DWORD WINAPI GetTickCount(void)
239 {
240     struct timeval t;
241     gettimeofday( &t, NULL );
242     return ((t.tv_sec - server_start_time) * 1000) + (t.tv_usec / 1000);
243 }