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