push b8b0d21e31a2078e72f3e29b2ec7fde29873cc3e
[wine/hacks.git] / dlls / kernel32 / kernel_main.c
blob2308d60fe2a5dc4f9cf555b5817457a2dd486569
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <signal.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wincon.h"
33 #include "winternl.h"
34 #include "wownt32.h"
36 #include "wine/library.h"
37 #include "kernel_private.h"
38 #include "console_private.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(process);
43 extern int CDECL __wine_set_signal_handler(unsigned, int (*)(unsigned));
45 static ULONGLONG server_start_time;
47 /***********************************************************************
48 * set_entry_point
50 static void set_entry_point( HMODULE module, const char *name, DWORD rva )
52 IMAGE_EXPORT_DIRECTORY *exports;
53 DWORD exp_size;
55 if ((exports = RtlImageDirectoryEntryToData( module, TRUE,
56 IMAGE_DIRECTORY_ENTRY_EXPORT, &exp_size )))
58 DWORD *functions = (DWORD *)((char *)module + exports->AddressOfFunctions);
59 const WORD *ordinals = (const WORD *)((const char *)module + exports->AddressOfNameOrdinals);
60 const DWORD *names = (const DWORD *)((const char *)module + exports->AddressOfNames);
61 int min = 0, max = exports->NumberOfNames - 1;
63 while (min <= max)
65 int res, pos = (min + max) / 2;
66 const char *ename = (const char *)module + names[pos];
67 if (!(res = strcmp( ename, name )))
69 WORD ordinal = ordinals[pos];
70 assert( ordinal < exports->NumberOfFunctions );
71 TRACE( "setting %s at %p to %08x\n", name, &functions[ordinal], rva );
72 functions[ordinal] = rva;
73 return;
75 if (res > 0) max = pos - 1;
76 else min = pos + 1;
82 /***********************************************************************
83 * KERNEL process initialisation routine
85 static BOOL process_attach( HMODULE module )
87 SYSTEM_TIMEOFDAY_INFORMATION ti;
88 RTL_USER_PROCESS_PARAMETERS *params = NtCurrentTeb()->Peb->ProcessParameters;
90 NtQuerySystemInformation( SystemTimeOfDayInformation, &ti, sizeof(ti), NULL );
91 server_start_time = ti.liKeBootTime.QuadPart;
93 /* Setup registry locale information */
94 LOCALE_InitRegistry();
96 /* Setup computer name */
97 COMPUTERNAME_Init();
99 /* convert value from server:
100 * + 0 => INVALID_HANDLE_VALUE
101 * + console handle needs to be mapped
103 if (!params->hStdInput)
104 params->hStdInput = INVALID_HANDLE_VALUE;
105 else if (VerifyConsoleIoHandle(console_handle_map(params->hStdInput)))
106 params->hStdInput = console_handle_map(params->hStdInput);
108 if (!params->hStdOutput)
109 params->hStdOutput = INVALID_HANDLE_VALUE;
110 else if (VerifyConsoleIoHandle(console_handle_map(params->hStdOutput)))
111 params->hStdOutput = console_handle_map(params->hStdOutput);
113 if (!params->hStdError)
114 params->hStdError = INVALID_HANDLE_VALUE;
115 else if (VerifyConsoleIoHandle(console_handle_map(params->hStdError)))
116 params->hStdError = console_handle_map(params->hStdError);
118 /* copy process information from ntdll */
119 ENV_CopyStartupInformation();
121 if (!(GetVersion() & 0x80000000))
123 /* Securom checks for this one when version is NT */
124 set_entry_point( module, "FT_Thunk", 0 );
126 else LoadLibraryA( "krnl386.exe16" );
128 /* finish the process initialisation for console bits, if needed */
129 __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
131 if (params->ConsoleHandle == (HANDLE)1) /* FIXME */
133 HMODULE mod = GetModuleHandleA(0);
134 if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
135 AllocConsole();
137 /* else TODO for DETACHED_PROCESS:
138 * 1/ inherit console + handles
139 * 2/ create std handles, if handles are not inherited
140 * TBD when not using wineserver handles for console handles
143 return TRUE;
146 /***********************************************************************
147 * KERNEL initialisation routine
149 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
151 switch(reason)
153 case DLL_PROCESS_ATTACH:
154 DisableThreadLibraryCalls( hinst );
155 return process_attach( hinst );
156 case DLL_PROCESS_DETACH:
157 WritePrivateProfileSectionW( NULL, NULL, NULL );
158 break;
160 return TRUE;
163 /***********************************************************************
164 * MulDiv (KERNEL32.@)
165 * RETURNS
166 * Result of multiplication and division
167 * -1: Overflow occurred or Divisor was 0
169 INT WINAPI MulDiv( INT nMultiplicand, INT nMultiplier, INT nDivisor)
171 LONGLONG ret;
173 if (!nDivisor) return -1;
175 /* We want to deal with a positive divisor to simplify the logic. */
176 if (nDivisor < 0)
178 nMultiplicand = - nMultiplicand;
179 nDivisor = -nDivisor;
182 /* If the result is positive, we "add" to round. else, we subtract to round. */
183 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
184 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
185 ret = (((LONGLONG)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
186 else
187 ret = (((LONGLONG)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
189 if ((ret > 2147483647) || (ret < -2147483647)) return -1;
190 return ret;
194 /******************************************************************************
195 * GetTickCount64 (KERNEL32.@)
197 ULONGLONG WINAPI GetTickCount64(void)
199 LARGE_INTEGER now;
201 NtQuerySystemTime( &now );
202 return (now.QuadPart - server_start_time) / 10000;
206 /***********************************************************************
207 * GetTickCount (KERNEL32.@)
209 * Get the number of milliseconds the system has been running.
211 * PARAMS
212 * None.
214 * RETURNS
215 * The current tick count.
217 * NOTES
218 * The value returned will wrap around every 2^32 milliseconds.
219 * Under Windows, tick 0 is the moment at which the system is rebooted.
220 * Under Wine, tick 0 begins at the moment the wineserver process is started.
222 DWORD WINAPI GetTickCount(void)
224 return GetTickCount64();