ole32: Remove superfluous pointer casts.
[wine] / dlls / kernel32 / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #include <signal.h>
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wincon.h"
33 #include "winternl.h"
34 #include "wownt32.h"
35
36 #include "wine/winbase16.h"
37 #include "wine/library.h"
38 #include "toolhelp.h"
39 #include "kernel_private.h"
40 #include "kernel16_private.h"
41 #include "console_private.h"
42 #include "wine/debug.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(process);
45
46 extern int CDECL __wine_set_signal_handler(unsigned, int (*)(unsigned));
47
48 static ULONGLONG server_start_time;
49
50 /***********************************************************************
51  *           KERNEL thread initialisation routine
52  */
53 static void thread_attach(void)
54 {
55     /* allocate the 16-bit stack (FIXME: should be done lazily) */
56     HGLOBAL16 hstack = WOWGlobalAlloc16( GMEM_FIXED, 0x10000 );
57     kernel_get_thread_data()->stack_sel = GlobalHandleToSel16( hstack );
58     NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR( kernel_get_thread_data()->stack_sel,
59                                                         0x10000 - sizeof(STACK16FRAME) );
60     memset( (char *)GlobalLock16(hstack) + 0x10000 - sizeof(STACK16FRAME), 0, sizeof(STACK16FRAME) );
61 }
62
63
64 /***********************************************************************
65  *           KERNEL thread finalisation routine
66  */
67 static void thread_detach(void)
68 {
69     /* free the 16-bit stack */
70     WOWGlobalFree16( kernel_get_thread_data()->stack_sel );
71     NtCurrentTeb()->WOW32Reserved = 0;
72     if (NtCurrentTeb()->Tib.SubSystemTib) TASK_ExitTask();
73 }
74
75
76 /***********************************************************************
77  *           set_entry_point
78  */
79 static void set_entry_point( HMODULE module, const char *name, DWORD rva )
80 {
81     IMAGE_EXPORT_DIRECTORY *exports;
82     DWORD exp_size;
83
84     if ((exports = RtlImageDirectoryEntryToData( module, TRUE,
85                                                   IMAGE_DIRECTORY_ENTRY_EXPORT, &exp_size )))
86     {
87         DWORD *functions = (DWORD *)((char *)module + exports->AddressOfFunctions);
88         const WORD *ordinals = (const WORD *)((const char *)module + exports->AddressOfNameOrdinals);
89         const DWORD *names = (const DWORD *)((const char *)module +  exports->AddressOfNames);
90         int min = 0, max = exports->NumberOfNames - 1;
91
92         while (min <= max)
93         {
94             int res, pos = (min + max) / 2;
95             const char *ename = (const char *)module + names[pos];
96             if (!(res = strcmp( ename, name )))
97             {
98                 WORD ordinal = ordinals[pos];
99                 assert( ordinal < exports->NumberOfFunctions );
100                 TRACE( "setting %s at %p to %08x\n", name, &functions[ordinal], rva );
101                 functions[ordinal] = rva;
102                 return;
103             }
104             if (res > 0) max = pos - 1;
105             else min = pos + 1;
106         }
107     }
108 }
109
110
111 /***********************************************************************
112  *           KERNEL process initialisation routine
113  */
114 static BOOL process_attach( HMODULE module )
115 {
116     SYSTEM_INFO si;
117     SYSTEM_TIMEOFDAY_INFORMATION ti;
118     RTL_USER_PROCESS_PARAMETERS *params = NtCurrentTeb()->Peb->ProcessParameters;
119
120     /* FIXME: should probably be done in ntdll */
121     GetSystemInfo( &si );
122     NtCurrentTeb()->Peb->NumberOfProcessors = si.dwNumberOfProcessors;
123
124     NtQuerySystemInformation( SystemTimeOfDayInformation, &ti, sizeof(ti), NULL );
125     server_start_time = ti.liKeBootTime.QuadPart;
126
127     /* Setup registry locale information */
128     LOCALE_InitRegistry();
129
130     /* Setup computer name */
131     COMPUTERNAME_Init();
132
133     /* convert value from server:
134      * + 0 => INVALID_HANDLE_VALUE
135      * + console handle needs to be mapped
136      */
137     if (!params->hStdInput)
138         params->hStdInput = INVALID_HANDLE_VALUE;
139     else if (VerifyConsoleIoHandle(console_handle_map(params->hStdInput)))
140         params->hStdInput = console_handle_map(params->hStdInput);
141
142     if (!params->hStdOutput)
143         params->hStdOutput = INVALID_HANDLE_VALUE;
144     else if (VerifyConsoleIoHandle(console_handle_map(params->hStdOutput)))
145         params->hStdOutput = console_handle_map(params->hStdOutput);
146
147     if (!params->hStdError)
148         params->hStdError = INVALID_HANDLE_VALUE;
149     else if (VerifyConsoleIoHandle(console_handle_map(params->hStdError)))
150         params->hStdError = console_handle_map(params->hStdError);
151
152     /* copy process information from ntdll */
153     ENV_CopyStartupInformation();
154
155     if (!(GetVersion() & 0x80000000))
156     {
157         /* Securom checks for this one when version is NT */
158         set_entry_point( module, "FT_Thunk", 0 );
159     }
160 #ifdef __i386__
161     else
162     {
163         /* create the shared heap for broken win95 native dlls */
164         HeapCreate( HEAP_SHARED, 0, 0 );
165         /* setup emulation of protected instructions from 32-bit code */
166         RtlAddVectoredExceptionHandler( TRUE, INSTR_vectored_handler );
167     }
168 #endif
169
170     /* finish the process initialisation for console bits, if needed */
171     __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
172
173     if (params->ConsoleHandle == (HANDLE)1)  /* FIXME */
174     {
175         HMODULE mod = GetModuleHandleA(0);
176         if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
177             AllocConsole();
178     }
179     /* else TODO for DETACHED_PROCESS:
180      * 1/ inherit console + handles
181      * 2/ create std handles, if handles are not inherited
182      * TBD when not using wineserver handles for console handles
183      */
184
185     /* Create 16-bit task */
186     LoadLibrary16( "krnl386.exe" );
187     thread_attach();
188     TASK_CreateMainTask();
189     return TRUE;
190 }
191
192 /***********************************************************************
193  *           KERNEL initialisation routine
194  */
195 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
196 {
197     switch(reason)
198     {
199     case DLL_PROCESS_ATTACH:
200         return process_attach( hinst );
201     case DLL_THREAD_ATTACH:
202         thread_attach();
203         break;
204     case DLL_THREAD_DETACH:
205         thread_detach();
206         break;
207     case DLL_PROCESS_DETACH:
208         WriteOutProfiles16();
209         break;
210     }
211     return TRUE;
212 }
213
214 /***********************************************************************
215  *           MulDiv   (KERNEL32.@)
216  * RETURNS
217  *      Result of multiplication and division
218  *      -1: Overflow occurred or Divisor was 0
219  */
220 INT WINAPI MulDiv( INT nMultiplicand, INT nMultiplier, INT nDivisor)
221 {
222     LONGLONG ret;
223
224     if (!nDivisor) return -1;
225
226     /* We want to deal with a positive divisor to simplify the logic. */
227     if (nDivisor < 0)
228     {
229       nMultiplicand = - nMultiplicand;
230       nDivisor = -nDivisor;
231     }
232
233     /* If the result is positive, we "add" to round. else, we subtract to round. */
234     if ( ( (nMultiplicand <  0) && (nMultiplier <  0) ) ||
235          ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
236       ret = (((LONGLONG)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
237     else
238       ret = (((LONGLONG)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
239
240     if ((ret > 2147483647) || (ret < -2147483647)) return -1;
241     return ret;
242 }
243
244
245 /******************************************************************************
246  *           GetTickCount64       (KERNEL32.@)
247  */
248 ULONGLONG WINAPI GetTickCount64(void)
249 {
250     LARGE_INTEGER now;
251
252     NtQuerySystemTime( &now );
253     return (now.QuadPart - server_start_time) / 10000;
254 }
255
256
257 /***********************************************************************
258  *           GetTickCount       (KERNEL32.@)
259  *
260  * Get the number of milliseconds the system has been running.
261  *
262  * PARAMS
263  *  None.
264  *
265  * RETURNS
266  *  The current tick count.
267  *
268  * NOTES
269  *  The value returned will wrap around every 2^32 milliseconds.
270  *  Under Windows, tick 0 is the moment at which the system is rebooted.
271  *  Under Wine, tick 0 begins at the moment the wineserver process is started.
272  */
273 DWORD WINAPI GetTickCount(void)
274 {
275     return GetTickCount64();
276 }