2000-06-03 06:49:40 +02:00
|
|
|
/*
|
|
|
|
* Kernel initialization code
|
2002-03-10 00:29:33 +01: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 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2000-06-03 06:49:40 +02:00
|
|
|
*/
|
|
|
|
|
2001-07-18 23:04:23 +02:00
|
|
|
#include "config.h"
|
2002-08-29 01:42:34 +02:00
|
|
|
#include "wine/port.h"
|
2001-07-18 23:04:23 +02:00
|
|
|
|
2000-06-03 06:49:40 +02:00
|
|
|
#include <assert.h>
|
2000-10-17 02:27:47 +02:00
|
|
|
#include <ctype.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2001-01-26 21:43:40 +01:00
|
|
|
#include <string.h>
|
2002-07-31 20:46:09 +02:00
|
|
|
#include <signal.h>
|
2000-06-03 06:49:40 +02:00
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
2000-06-03 06:49:40 +02:00
|
|
|
#include "winbase.h"
|
2002-07-31 21:19:36 +02:00
|
|
|
#include "wincon.h"
|
2002-09-13 00:07:02 +02:00
|
|
|
#include "winternl.h"
|
2005-08-10 12:59:19 +02:00
|
|
|
#include "wownt32.h"
|
2000-06-03 06:49:40 +02:00
|
|
|
|
2001-07-18 23:04:23 +02:00
|
|
|
#include "wine/winbase16.h"
|
2002-05-16 22:32:16 +02:00
|
|
|
#include "wine/library.h"
|
2002-09-04 20:41:03 +02:00
|
|
|
#include "wincon.h"
|
2004-02-12 01:00:55 +01:00
|
|
|
#include "toolhelp.h"
|
2003-08-25 02:56:37 +02:00
|
|
|
#include "kernel_private.h"
|
2005-05-16 21:44:54 +02:00
|
|
|
#include "kernel16_private.h"
|
2002-09-04 20:41:03 +02:00
|
|
|
#include "console_private.h"
|
2007-05-07 17:10:18 +02:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(process);
|
2000-06-03 06:49:40 +02:00
|
|
|
|
2002-07-31 20:46:09 +02:00
|
|
|
extern int __wine_set_signal_handler(unsigned, int (*)(unsigned));
|
2000-06-03 06:49:40 +02:00
|
|
|
|
2003-05-13 02:49:49 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* KERNEL thread initialisation routine
|
|
|
|
*/
|
|
|
|
static void thread_attach(void)
|
|
|
|
{
|
|
|
|
/* allocate the 16-bit stack (FIXME: should be done lazily) */
|
2005-08-10 12:59:19 +02:00
|
|
|
HGLOBAL16 hstack = WOWGlobalAlloc16( GMEM_FIXED, 0x10000 );
|
2005-06-06 22:00:14 +02:00
|
|
|
kernel_get_thread_data()->stack_sel = GlobalHandleToSel16( hstack );
|
|
|
|
NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR( kernel_get_thread_data()->stack_sel,
|
2005-05-14 14:16:46 +02:00
|
|
|
0x10000 - sizeof(STACK16FRAME) );
|
2003-05-13 02:49:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* KERNEL thread finalisation routine
|
|
|
|
*/
|
|
|
|
static void thread_detach(void)
|
|
|
|
{
|
|
|
|
/* free the 16-bit stack */
|
2005-08-10 12:59:19 +02:00
|
|
|
WOWGlobalFree16( kernel_get_thread_data()->stack_sel );
|
2005-05-14 14:16:46 +02:00
|
|
|
NtCurrentTeb()->WOW32Reserved = 0;
|
2004-04-15 07:04:53 +02:00
|
|
|
if (NtCurrentTeb()->Tib.SubSystemTib) TASK_ExitTask();
|
2003-05-13 02:49:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-07 17:10:18 +02: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 06:49:40 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* KERNEL process initialisation routine
|
|
|
|
*/
|
2007-05-07 17:10:18 +02:00
|
|
|
static BOOL process_attach( HMODULE module )
|
2000-06-03 06:49:40 +02:00
|
|
|
{
|
2004-06-15 02:52:03 +02:00
|
|
|
SYSTEM_INFO si;
|
2006-07-13 14:04:40 +02:00
|
|
|
RTL_USER_PROCESS_PARAMETERS *params = NtCurrentTeb()->Peb->ProcessParameters;
|
2004-06-15 02:52:03 +02:00
|
|
|
|
|
|
|
/* FIXME: should probably be done in ntdll */
|
|
|
|
GetSystemInfo( &si );
|
|
|
|
NtCurrentTeb()->Peb->NumberOfProcessors = si.dwNumberOfProcessors;
|
2000-06-03 06:49:40 +02:00
|
|
|
|
2003-10-14 07:32:30 +02:00
|
|
|
/* Setup registry locale information */
|
|
|
|
LOCALE_InitRegistry();
|
2000-07-12 00:08:43 +02:00
|
|
|
|
2002-11-15 02:01:47 +01:00
|
|
|
/* Setup computer name */
|
|
|
|
COMPUTERNAME_Init();
|
2004-03-20 03:28:51 +01:00
|
|
|
|
2006-07-13 14:04:40 +02: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 05:23:22 +02:00
|
|
|
/* copy process information from ntdll */
|
|
|
|
ENV_CopyStartupInformation();
|
2002-11-15 02:01:47 +01:00
|
|
|
|
2007-05-07 17:10:18 +02:00
|
|
|
if (!(GetVersion() & 0x80000000))
|
|
|
|
{
|
|
|
|
/* Securom checks for this one when version is NT */
|
|
|
|
set_entry_point( module, "FT_Thunk", 0 );
|
|
|
|
}
|
2004-08-13 02:42:09 +02:00
|
|
|
#ifdef __i386__
|
2007-05-07 17:10:18 +02:00
|
|
|
else
|
2005-08-27 12:18:03 +02: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 02:42:09 +02:00
|
|
|
#endif
|
2001-03-04 02:06:07 +01:00
|
|
|
|
2002-09-04 20:41:03 +02:00
|
|
|
/* finish the process initialisation for console bits, if needed */
|
2002-07-31 20:46:09 +02:00
|
|
|
__wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
|
|
|
|
|
2006-07-13 14:04:40 +02:00
|
|
|
if (params->ConsoleHandle == (HANDLE)1) /* FIXME */
|
2002-07-31 21:19:36 +02:00
|
|
|
{
|
|
|
|
HMODULE mod = GetModuleHandleA(0);
|
|
|
|
if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
|
|
|
|
AllocConsole();
|
|
|
|
}
|
2006-02-05 12:24:43 +01: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 05:23:22 +02:00
|
|
|
|
2003-09-18 06:28:22 +02:00
|
|
|
/* Create 16-bit task */
|
2005-08-29 12:20:51 +02:00
|
|
|
LoadLibrary16( "krnl386.exe" );
|
2003-05-13 02:49:49 +02:00
|
|
|
thread_attach();
|
2003-09-18 06:28:22 +02:00
|
|
|
TASK_CreateMainTask();
|
2000-06-03 06:49:40 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* KERNEL initialisation routine
|
|
|
|
*/
|
2003-10-10 02:50:56 +02:00
|
|
|
BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
|
2000-06-03 06:49:40 +02:00
|
|
|
{
|
|
|
|
switch(reason)
|
|
|
|
{
|
|
|
|
case DLL_PROCESS_ATTACH:
|
2007-05-07 17:10:18 +02:00
|
|
|
return process_attach( hinst );
|
2003-05-13 02:49:49 +02:00
|
|
|
case DLL_THREAD_ATTACH:
|
|
|
|
thread_attach();
|
|
|
|
break;
|
|
|
|
case DLL_THREAD_DETACH:
|
|
|
|
thread_detach();
|
|
|
|
break;
|
2000-06-03 06:49:40 +02:00
|
|
|
case DLL_PROCESS_DETACH:
|
2000-08-21 05:33:31 +02:00
|
|
|
WriteOutProfiles16();
|
2000-06-03 06:49:40 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
2000-08-06 04:42:46 +02:00
|
|
|
|
2004-04-28 05:53:19 +02: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 12:32:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* GetTickCount (KERNEL32.@)
|
|
|
|
*
|
|
|
|
* Get the number of milliseconds the system has been running.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* The current tick count.
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* -The value returned will wrap arounf 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,
|
|
|
|
*/
|
|
|
|
DWORD WINAPI GetTickCount(void)
|
|
|
|
{
|
2006-04-20 20:13:12 +02:00
|
|
|
return NtGetTickCount();
|
2005-07-14 12:32:46 +02:00
|
|
|
}
|