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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
36 #include "wine/winbase16.h"
37 #include "wine/library.h"
39 #include "kernel_private.h"
40 #include "kernel16_private.h"
41 #include "console_private.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(process
);
46 extern int __wine_set_signal_handler(unsigned, int (*)(unsigned));
48 static ULONGLONG server_start_time
;
50 /***********************************************************************
51 * KERNEL thread initialisation routine
53 static void thread_attach(void)
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
) );
63 /***********************************************************************
64 * KERNEL thread finalisation routine
66 static void thread_detach(void)
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();
75 /***********************************************************************
78 static void set_entry_point( HMODULE module
, const char *name
, DWORD rva
)
80 IMAGE_EXPORT_DIRECTORY
*exports
;
83 if ((exports
= RtlImageDirectoryEntryToData( module
, TRUE
,
84 IMAGE_DIRECTORY_ENTRY_EXPORT
, &exp_size
)))
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;
93 int res
, pos
= (min
+ max
) / 2;
94 const char *ename
= (const char *)module
+ names
[pos
];
95 if (!(res
= strcmp( ename
, name
)))
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
;
103 if (res
> 0) max
= pos
- 1;
110 /***********************************************************************
111 * KERNEL process initialisation routine
113 static BOOL
process_attach( HMODULE module
)
116 SYSTEM_TIMEOFDAY_INFORMATION ti
;
117 RTL_USER_PROCESS_PARAMETERS
*params
= NtCurrentTeb()->Peb
->ProcessParameters
;
119 /* FIXME: should probably be done in ntdll */
120 GetSystemInfo( &si
);
121 NtCurrentTeb()->Peb
->NumberOfProcessors
= si
.dwNumberOfProcessors
;
123 NtQuerySystemInformation( SystemTimeOfDayInformation
, &ti
, sizeof(ti
), NULL
);
124 server_start_time
= ti
.liKeBootTime
.QuadPart
;
126 /* Setup registry locale information */
127 LOCALE_InitRegistry();
129 /* Setup computer name */
132 /* convert value from server:
133 * + 0 => INVALID_HANDLE_VALUE
134 * + console handle needs to be mapped
136 if (!params
->hStdInput
)
137 params
->hStdInput
= INVALID_HANDLE_VALUE
;
138 else if (VerifyConsoleIoHandle(console_handle_map(params
->hStdInput
)))
139 params
->hStdInput
= console_handle_map(params
->hStdInput
);
141 if (!params
->hStdOutput
)
142 params
->hStdOutput
= INVALID_HANDLE_VALUE
;
143 else if (VerifyConsoleIoHandle(console_handle_map(params
->hStdOutput
)))
144 params
->hStdOutput
= console_handle_map(params
->hStdOutput
);
146 if (!params
->hStdError
)
147 params
->hStdError
= INVALID_HANDLE_VALUE
;
148 else if (VerifyConsoleIoHandle(console_handle_map(params
->hStdError
)))
149 params
->hStdError
= console_handle_map(params
->hStdError
);
151 /* copy process information from ntdll */
152 ENV_CopyStartupInformation();
154 if (!(GetVersion() & 0x80000000))
156 /* Securom checks for this one when version is NT */
157 set_entry_point( module
, "FT_Thunk", 0 );
162 /* create the shared heap for broken win95 native dlls */
163 HeapCreate( HEAP_SHARED
, 0, 0 );
164 /* setup emulation of protected instructions from 32-bit code */
165 RtlAddVectoredExceptionHandler( TRUE
, INSTR_vectored_handler
);
169 /* finish the process initialisation for console bits, if needed */
170 __wine_set_signal_handler(SIGINT
, CONSOLE_HandleCtrlC
);
172 if (params
->ConsoleHandle
== (HANDLE
)1) /* FIXME */
174 HMODULE mod
= GetModuleHandleA(0);
175 if (RtlImageNtHeader(mod
)->OptionalHeader
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_CUI
)
178 /* else TODO for DETACHED_PROCESS:
179 * 1/ inherit console + handles
180 * 2/ create std handles, if handles are not inherited
181 * TBD when not using wineserver handles for console handles
184 /* Create 16-bit task */
185 LoadLibrary16( "krnl386.exe" );
187 TASK_CreateMainTask();
191 /***********************************************************************
192 * KERNEL initialisation routine
194 BOOL WINAPI
DllMain( HINSTANCE hinst
, DWORD reason
, LPVOID reserved
)
198 case DLL_PROCESS_ATTACH
:
199 return process_attach( hinst
);
200 case DLL_THREAD_ATTACH
:
203 case DLL_THREAD_DETACH
:
206 case DLL_PROCESS_DETACH
:
207 WriteOutProfiles16();
213 /***********************************************************************
214 * MulDiv (KERNEL32.@)
216 * Result of multiplication and division
217 * -1: Overflow occurred or Divisor was 0
219 INT WINAPI
MulDiv( INT nMultiplicand
, INT nMultiplier
, INT nDivisor
)
223 if (!nDivisor
) return -1;
225 /* We want to deal with a positive divisor to simplify the logic. */
228 nMultiplicand
= - nMultiplicand
;
229 nDivisor
= -nDivisor
;
232 /* If the result is positive, we "add" to round. else, we subtract to round. */
233 if ( ( (nMultiplicand
< 0) && (nMultiplier
< 0) ) ||
234 ( (nMultiplicand
>= 0) && (nMultiplier
>= 0) ) )
235 ret
= (((LONGLONG
)nMultiplicand
* nMultiplier
) + (nDivisor
/2)) / nDivisor
;
237 ret
= (((LONGLONG
)nMultiplicand
* nMultiplier
) - (nDivisor
/2)) / nDivisor
;
239 if ((ret
> 2147483647) || (ret
< -2147483647)) return -1;
244 /******************************************************************************
245 * GetTickCount64 (KERNEL32.@)
247 ULONGLONG WINAPI
GetTickCount64(void)
251 NtQuerySystemTime( &now
);
252 return (now
.QuadPart
- server_start_time
) / 10000;
256 /***********************************************************************
257 * GetTickCount (KERNEL32.@)
259 * Get the number of milliseconds the system has been running.
265 * The current tick count.
268 * -The value returned will wrap arounf every 2^32 milliseconds.
269 * -Under Windows, tick 0 is the moment at which the system is rebooted.
270 * Under Wine, tick 0 begins at the moment the wineserver process is started,
272 DWORD WINAPI
GetTickCount(void)
274 return GetTickCount64();