2000-06-03 04:49:40 +00:00
|
|
|
/*
|
|
|
|
* Kernel initialization code
|
2002-03-09 23:29:33 +00:00
|
|
|
*
|
|
|
|
* Copyright 2000 Alexandre Julliard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 12:49:52 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2000-06-03 04:49:40 +00:00
|
|
|
*/
|
|
|
|
|
2001-07-18 21:04:23 +00:00
|
|
|
#include "config.h"
|
2002-08-28 23:42:34 +00:00
|
|
|
#include "wine/port.h"
|
2001-07-18 21:04:23 +00:00
|
|
|
|
2000-06-03 04:49:40 +00:00
|
|
|
#include <assert.h>
|
2000-10-17 00:27:47 +00:00
|
|
|
#include <ctype.h>
|
2003-09-05 23:08:26 +00:00
|
|
|
#include <stdarg.h>
|
2001-01-26 20:43:40 +00:00
|
|
|
#include <string.h>
|
2002-07-31 18:46:09 +00:00
|
|
|
#include <signal.h>
|
2000-06-03 04:49:40 +00:00
|
|
|
|
2003-09-05 23:08:26 +00:00
|
|
|
#include "windef.h"
|
2000-06-03 04:49:40 +00:00
|
|
|
#include "winbase.h"
|
2002-07-31 19:19:36 +00:00
|
|
|
#include "wincon.h"
|
2002-09-12 22:07:02 +00:00
|
|
|
#include "winternl.h"
|
2005-08-10 10:59:19 +00:00
|
|
|
#include "wownt32.h"
|
2000-06-03 04:49:40 +00:00
|
|
|
|
2001-07-18 21:04:23 +00:00
|
|
|
#include "wine/winbase16.h"
|
2002-05-16 20:32:16 +00:00
|
|
|
#include "wine/library.h"
|
2004-02-12 00:00:55 +00:00
|
|
|
#include "toolhelp.h"
|
2003-08-25 00:56:37 +00:00
|
|
|
#include "kernel_private.h"
|
2005-05-16 19:44:54 +00:00
|
|
|
#include "kernel16_private.h"
|
2002-09-04 18:41:03 +00:00
|
|
|
#include "console_private.h"
|
2007-05-07 15:10:18 +00:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(process);
|
2000-06-03 04:49:40 +00:00
|
|
|
|
2008-12-16 15:37:46 +00:00
|
|
|
extern int CDECL __wine_set_signal_handler(unsigned, int (*)(unsigned));
|
2000-06-03 04:49:40 +00:00
|
|
|
|
2007-07-11 12:04:54 +00:00
|
|
|
static ULONGLONG server_start_time;
|
|
|
|
|
2003-05-13 00:49:49 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* KERNEL thread initialisation routine
|
|
|
|
*/
|
|
|
|
static void thread_attach(void)
|
|
|
|
{
|
|
|
|
/* allocate the 16-bit stack (FIXME: should be done lazily) */
|
2005-08-10 10:59:19 +00:00
|
|
|
HGLOBAL16 hstack = WOWGlobalAlloc16( GMEM_FIXED, 0x10000 );
|
2005-06-06 20:00:14 +00:00
|
|
|
kernel_get_thread_data()->stack_sel = GlobalHandleToSel16( hstack );
|
|
|
|
NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR( kernel_get_thread_data()->stack_sel,
|
2005-05-14 12:16:46 +00:00
|
|
|
0x10000 - sizeof(STACK16FRAME) );
|
2008-01-17 19:07:06 +00:00
|
|
|
memset( (char *)GlobalLock16(hstack) + 0x10000 - sizeof(STACK16FRAME), 0, sizeof(STACK16FRAME) );
|
2003-05-13 00:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* KERNEL thread finalisation routine
|
|
|
|
*/
|
|
|
|
static void thread_detach(void)
|
|
|
|
{
|
|
|
|
/* free the 16-bit stack */
|
2005-08-10 10:59:19 +00:00
|
|
|
WOWGlobalFree16( kernel_get_thread_data()->stack_sel );
|
2005-05-14 12:16:46 +00:00
|
|
|
NtCurrentTeb()->WOW32Reserved = 0;
|
2004-04-15 05:04:53 +00:00
|
|
|
if (NtCurrentTeb()->Tib.SubSystemTib) TASK_ExitTask();
|
2003-05-13 00:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-07 15:10:18 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* set_entry_point
|
|
|
|
*/
|
|
|
|
static void set_entry_point( HMODULE module, const char *name, DWORD rva )
|
|
|
|
{
|
|
|
|
IMAGE_EXPORT_DIRECTORY *exports;
|
|
|
|
DWORD exp_size;
|
|
|
|
|
|
|
|
if ((exports = RtlImageDirectoryEntryToData( module, TRUE,
|
|
|
|
IMAGE_DIRECTORY_ENTRY_EXPORT, &exp_size )))
|
|
|
|
{
|
|
|
|
DWORD *functions = (DWORD *)((char *)module + exports->AddressOfFunctions);
|
|
|
|
const WORD *ordinals = (const WORD *)((const char *)module + exports->AddressOfNameOrdinals);
|
|
|
|
const DWORD *names = (const DWORD *)((const char *)module + exports->AddressOfNames);
|
|
|
|
int min = 0, max = exports->NumberOfNames - 1;
|
|
|
|
|
|
|
|
while (min <= max)
|
|
|
|
{
|
|
|
|
int res, pos = (min + max) / 2;
|
|
|
|
const char *ename = (const char *)module + names[pos];
|
|
|
|
if (!(res = strcmp( ename, name )))
|
|
|
|
{
|
|
|
|
WORD ordinal = ordinals[pos];
|
|
|
|
assert( ordinal < exports->NumberOfFunctions );
|
|
|
|
TRACE( "setting %s at %p to %08x\n", name, &functions[ordinal], rva );
|
|
|
|
functions[ordinal] = rva;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (res > 0) max = pos - 1;
|
|
|
|
else min = pos + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-03 04:49:40 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* KERNEL process initialisation routine
|
|
|
|
*/
|
2007-05-07 15:10:18 +00:00
|
|
|
static BOOL process_attach( HMODULE module )
|
2000-06-03 04:49:40 +00:00
|
|
|
{
|
2004-06-15 00:52:03 +00:00
|
|
|
SYSTEM_INFO si;
|
2007-07-11 12:04:54 +00:00
|
|
|
SYSTEM_TIMEOFDAY_INFORMATION ti;
|
2006-07-13 12:04:40 +00:00
|
|
|
RTL_USER_PROCESS_PARAMETERS *params = NtCurrentTeb()->Peb->ProcessParameters;
|
2004-06-15 00:52:03 +00:00
|
|
|
|
|
|
|
/* FIXME: should probably be done in ntdll */
|
|
|
|
GetSystemInfo( &si );
|
|
|
|
NtCurrentTeb()->Peb->NumberOfProcessors = si.dwNumberOfProcessors;
|
2000-06-03 04:49:40 +00:00
|
|
|
|
2007-07-11 12:04:54 +00:00
|
|
|
NtQuerySystemInformation( SystemTimeOfDayInformation, &ti, sizeof(ti), NULL );
|
|
|
|
server_start_time = ti.liKeBootTime.QuadPart;
|
|
|
|
|
2003-10-14 05:32:30 +00:00
|
|
|
/* Setup registry locale information */
|
|
|
|
LOCALE_InitRegistry();
|
2000-07-11 22:08:43 +00:00
|
|
|
|
2002-11-15 01:01:47 +00:00
|
|
|
/* Setup computer name */
|
|
|
|
COMPUTERNAME_Init();
|
2004-03-20 02:28:51 +00:00
|
|
|
|
2006-07-13 12:04:40 +00:00
|
|
|
/* convert value from server:
|
|
|
|
* + 0 => INVALID_HANDLE_VALUE
|
|
|
|
* + console handle needs to be mapped
|
|
|
|
*/
|
|
|
|
if (!params->hStdInput)
|
|
|
|
params->hStdInput = INVALID_HANDLE_VALUE;
|
|
|
|
else if (VerifyConsoleIoHandle(console_handle_map(params->hStdInput)))
|
|
|
|
params->hStdInput = console_handle_map(params->hStdInput);
|
|
|
|
|
|
|
|
if (!params->hStdOutput)
|
|
|
|
params->hStdOutput = INVALID_HANDLE_VALUE;
|
|
|
|
else if (VerifyConsoleIoHandle(console_handle_map(params->hStdOutput)))
|
|
|
|
params->hStdOutput = console_handle_map(params->hStdOutput);
|
|
|
|
|
|
|
|
if (!params->hStdError)
|
|
|
|
params->hStdError = INVALID_HANDLE_VALUE;
|
|
|
|
else if (VerifyConsoleIoHandle(console_handle_map(params->hStdError)))
|
|
|
|
params->hStdError = console_handle_map(params->hStdError);
|
|
|
|
|
2003-06-18 03:23:22 +00:00
|
|
|
/* copy process information from ntdll */
|
|
|
|
ENV_CopyStartupInformation();
|
2002-11-15 01:01:47 +00:00
|
|
|
|
2007-05-07 15:10:18 +00:00
|
|
|
if (!(GetVersion() & 0x80000000))
|
|
|
|
{
|
|
|
|
/* Securom checks for this one when version is NT */
|
|
|
|
set_entry_point( module, "FT_Thunk", 0 );
|
|
|
|
}
|
2004-08-13 00:42:09 +00:00
|
|
|
#ifdef __i386__
|
2007-05-07 15:10:18 +00:00
|
|
|
else
|
2005-08-27 10:18:03 +00:00
|
|
|
{
|
|
|
|
/* create the shared heap for broken win95 native dlls */
|
|
|
|
HeapCreate( HEAP_SHARED, 0, 0 );
|
|
|
|
/* setup emulation of protected instructions from 32-bit code */
|
|
|
|
RtlAddVectoredExceptionHandler( TRUE, INSTR_vectored_handler );
|
|
|
|
}
|
2004-08-13 00:42:09 +00:00
|
|
|
#endif
|
2001-03-04 01:06:07 +00:00
|
|
|
|
2002-09-04 18:41:03 +00:00
|
|
|
/* finish the process initialisation for console bits, if needed */
|
2002-07-31 18:46:09 +00:00
|
|
|
__wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
|
|
|
|
|
2006-07-13 12:04:40 +00:00
|
|
|
if (params->ConsoleHandle == (HANDLE)1) /* FIXME */
|
2002-07-31 19:19:36 +00:00
|
|
|
{
|
|
|
|
HMODULE mod = GetModuleHandleA(0);
|
|
|
|
if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
|
|
|
|
AllocConsole();
|
|
|
|
}
|
2006-02-05 11:24:43 +00:00
|
|
|
/* else TODO for DETACHED_PROCESS:
|
|
|
|
* 1/ inherit console + handles
|
|
|
|
* 2/ create std handles, if handles are not inherited
|
|
|
|
* TBD when not using wineserver handles for console handles
|
|
|
|
*/
|
2003-06-18 03:23:22 +00:00
|
|
|
|
2003-09-18 04:28:22 +00:00
|
|
|
/* Create 16-bit task */
|
2005-08-29 10:20:51 +00:00
|
|
|
LoadLibrary16( "krnl386.exe" );
|
2003-05-13 00:49:49 +00:00
|
|
|
thread_attach();
|
2003-09-18 04:28:22 +00:00
|
|
|
TASK_CreateMainTask();
|
2000-06-03 04:49:40 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* KERNEL initialisation routine
|
|
|
|
*/
|
2003-10-10 00:50:56 +00:00
|
|
|
BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
|
2000-06-03 04:49:40 +00:00
|
|
|
{
|
|
|
|
switch(reason)
|
|
|
|
{
|
|
|
|
case DLL_PROCESS_ATTACH:
|
2007-05-07 15:10:18 +00:00
|
|
|
return process_attach( hinst );
|
2003-05-13 00:49:49 +00:00
|
|
|
case DLL_THREAD_ATTACH:
|
|
|
|
thread_attach();
|
|
|
|
break;
|
|
|
|
case DLL_THREAD_DETACH:
|
|
|
|
thread_detach();
|
|
|
|
break;
|
2000-06-03 04:49:40 +00:00
|
|
|
case DLL_PROCESS_DETACH:
|
2000-08-21 03:33:31 +00:00
|
|
|
WriteOutProfiles16();
|
2000-06-03 04:49:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
2000-08-06 02:42:46 +00:00
|
|
|
|
2004-04-28 03:53:19 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* MulDiv (KERNEL32.@)
|
|
|
|
* RETURNS
|
|
|
|
* Result of multiplication and division
|
|
|
|
* -1: Overflow occurred or Divisor was 0
|
|
|
|
*/
|
|
|
|
INT WINAPI MulDiv( INT nMultiplicand, INT nMultiplier, INT nDivisor)
|
|
|
|
{
|
|
|
|
LONGLONG ret;
|
|
|
|
|
|
|
|
if (!nDivisor) return -1;
|
|
|
|
|
|
|
|
/* We want to deal with a positive divisor to simplify the logic. */
|
|
|
|
if (nDivisor < 0)
|
|
|
|
{
|
|
|
|
nMultiplicand = - nMultiplicand;
|
|
|
|
nDivisor = -nDivisor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the result is positive, we "add" to round. else, we subtract to round. */
|
|
|
|
if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
|
|
|
|
( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
|
|
|
|
ret = (((LONGLONG)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
|
|
|
|
else
|
|
|
|
ret = (((LONGLONG)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
|
|
|
|
|
|
|
|
if ((ret > 2147483647) || (ret < -2147483647)) return -1;
|
|
|
|
return ret;
|
|
|
|
}
|
2005-07-14 10:32:46 +00:00
|
|
|
|
|
|
|
|
2007-07-11 12:04:54 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* GetTickCount64 (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
ULONGLONG WINAPI GetTickCount64(void)
|
|
|
|
{
|
|
|
|
LARGE_INTEGER now;
|
|
|
|
|
|
|
|
NtQuerySystemTime( &now );
|
|
|
|
return (now.QuadPart - server_start_time) / 10000;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-14 10:32:46 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* GetTickCount (KERNEL32.@)
|
|
|
|
*
|
|
|
|
* Get the number of milliseconds the system has been running.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* The current tick count.
|
|
|
|
*
|
|
|
|
* NOTES
|
2008-03-17 20:51:39 +00:00
|
|
|
* The value returned will wrap around every 2^32 milliseconds.
|
|
|
|
* Under Windows, tick 0 is the moment at which the system is rebooted.
|
|
|
|
* Under Wine, tick 0 begins at the moment the wineserver process is started.
|
2005-07-14 10:32:46 +00:00
|
|
|
*/
|
|
|
|
DWORD WINAPI GetTickCount(void)
|
|
|
|
{
|
2007-07-11 12:04:54 +00:00
|
|
|
return GetTickCount64();
|
2005-07-14 10:32:46 +00:00
|
|
|
}
|