d3d9/tests: Warn that tests were skipped if we could not load d3d9.dll.
[wine/wine64.git] / dlls / kernel32 / kernel_main.c
blobf6eeba99ba799e231a5a0a51f7b7a6a6be798dbf
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/winbase16.h"
37 #include "wine/library.h"
38 #include "wincon.h"
39 #include "toolhelp.h"
40 #include "kernel_private.h"
41 #include "kernel16_private.h"
42 #include "console_private.h"
44 extern int __wine_set_signal_handler(unsigned, int (*)(unsigned));
46 static CRITICAL_SECTION ldt_section;
47 static CRITICAL_SECTION_DEBUG critsect_debug =
49 0, 0, &ldt_section,
50 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
51 0, 0, { (DWORD_PTR)(__FILE__ ": ldt_section") }
53 static CRITICAL_SECTION ldt_section = { &critsect_debug, -1, 0, 0, 0, 0 };
55 /***********************************************************************
56 * locking for LDT routines
58 static void ldt_lock(void)
60 RtlEnterCriticalSection( &ldt_section );
63 static void ldt_unlock(void)
65 RtlLeaveCriticalSection( &ldt_section );
69 /***********************************************************************
70 * KERNEL thread initialisation routine
72 static void thread_attach(void)
74 /* allocate the 16-bit stack (FIXME: should be done lazily) */
75 HGLOBAL16 hstack = WOWGlobalAlloc16( GMEM_FIXED, 0x10000 );
76 kernel_get_thread_data()->stack_sel = GlobalHandleToSel16( hstack );
77 NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR( kernel_get_thread_data()->stack_sel,
78 0x10000 - sizeof(STACK16FRAME) );
82 /***********************************************************************
83 * KERNEL thread finalisation routine
85 static void thread_detach(void)
87 /* free the 16-bit stack */
88 WOWGlobalFree16( kernel_get_thread_data()->stack_sel );
89 NtCurrentTeb()->WOW32Reserved = 0;
90 if (NtCurrentTeb()->Tib.SubSystemTib) TASK_ExitTask();
94 /***********************************************************************
95 * KERNEL process initialisation routine
97 static BOOL process_attach(void)
99 SYSTEM_INFO si;
100 RTL_USER_PROCESS_PARAMETERS *params = NtCurrentTeb()->Peb->ProcessParameters;
102 /* FIXME: should probably be done in ntdll */
103 GetSystemInfo( &si );
104 NtCurrentTeb()->Peb->NumberOfProcessors = si.dwNumberOfProcessors;
106 /* Setup registry locale information */
107 LOCALE_InitRegistry();
109 /* Setup computer name */
110 COMPUTERNAME_Init();
112 /* convert value from server:
113 * + 0 => INVALID_HANDLE_VALUE
114 * + console handle needs to be mapped
116 if (!params->hStdInput)
117 params->hStdInput = INVALID_HANDLE_VALUE;
118 else if (VerifyConsoleIoHandle(console_handle_map(params->hStdInput)))
119 params->hStdInput = console_handle_map(params->hStdInput);
121 if (!params->hStdOutput)
122 params->hStdOutput = INVALID_HANDLE_VALUE;
123 else if (VerifyConsoleIoHandle(console_handle_map(params->hStdOutput)))
124 params->hStdOutput = console_handle_map(params->hStdOutput);
126 if (!params->hStdError)
127 params->hStdError = INVALID_HANDLE_VALUE;
128 else if (VerifyConsoleIoHandle(console_handle_map(params->hStdError)))
129 params->hStdError = console_handle_map(params->hStdError);
131 /* copy process information from ntdll */
132 ENV_CopyStartupInformation();
134 #ifdef __i386__
135 if (GetVersion() & 0x80000000)
137 /* create the shared heap for broken win95 native dlls */
138 HeapCreate( HEAP_SHARED, 0, 0 );
139 /* setup emulation of protected instructions from 32-bit code */
140 RtlAddVectoredExceptionHandler( TRUE, INSTR_vectored_handler );
142 #endif
144 /* initialize LDT locking */
145 wine_ldt_init_locking( ldt_lock, ldt_unlock );
147 /* finish the process initialisation for console bits, if needed */
148 __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
150 if (params->ConsoleHandle == (HANDLE)1) /* FIXME */
152 HMODULE mod = GetModuleHandleA(0);
153 if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
154 AllocConsole();
156 /* else TODO for DETACHED_PROCESS:
157 * 1/ inherit console + handles
158 * 2/ create std handles, if handles are not inherited
159 * TBD when not using wineserver handles for console handles
162 /* Create 16-bit task */
163 LoadLibrary16( "krnl386.exe" );
164 thread_attach();
165 TASK_CreateMainTask();
166 return TRUE;
169 /***********************************************************************
170 * KERNEL initialisation routine
172 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
174 switch(reason)
176 case DLL_PROCESS_ATTACH:
177 return process_attach();
178 case DLL_THREAD_ATTACH:
179 thread_attach();
180 break;
181 case DLL_THREAD_DETACH:
182 thread_detach();
183 break;
184 case DLL_PROCESS_DETACH:
185 WriteOutProfiles16();
186 break;
188 return TRUE;
191 /***********************************************************************
192 * MulDiv (KERNEL32.@)
193 * RETURNS
194 * Result of multiplication and division
195 * -1: Overflow occurred or Divisor was 0
197 INT WINAPI MulDiv( INT nMultiplicand, INT nMultiplier, INT nDivisor)
199 LONGLONG ret;
201 if (!nDivisor) return -1;
203 /* We want to deal with a positive divisor to simplify the logic. */
204 if (nDivisor < 0)
206 nMultiplicand = - nMultiplicand;
207 nDivisor = -nDivisor;
210 /* If the result is positive, we "add" to round. else, we subtract to round. */
211 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
212 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
213 ret = (((LONGLONG)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
214 else
215 ret = (((LONGLONG)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
217 if ((ret > 2147483647) || (ret < -2147483647)) return -1;
218 return ret;
222 /***********************************************************************
223 * GetTickCount (KERNEL32.@)
225 * Get the number of milliseconds the system has been running.
227 * PARAMS
228 * None.
230 * RETURNS
231 * The current tick count.
233 * NOTES
234 * -The value returned will wrap arounf every 2^32 milliseconds.
235 * -Under Windows, tick 0 is the moment at which the system is rebooted.
236 * Under Wine, tick 0 begins at the moment the wineserver process is started,
238 DWORD WINAPI GetTickCount(void)
240 return NtGetTickCount();