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"
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 =
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)
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 */
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();
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 );
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)
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" );
165 TASK_CreateMainTask();
169 /***********************************************************************
170 * KERNEL initialisation routine
172 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
176 case DLL_PROCESS_ATTACH:
177 return process_attach();
178 case DLL_THREAD_ATTACH:
181 case DLL_THREAD_DETACH:
184 case DLL_PROCESS_DETACH:
185 WriteOutProfiles16();
191 /***********************************************************************
192 * MulDiv (KERNEL32.@)
194 * Result of multiplication and division
195 * -1: Overflow occurred or Divisor was 0
197 INT WINAPI MulDiv( INT nMultiplicand, INT nMultiplier, INT nDivisor)
201 if (!nDivisor) return -1;
203 /* We want to deal with a positive divisor to simplify the logic. */
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;
215 ret = (((LONGLONG)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
217 if ((ret > 2147483647) || (ret < -2147483647)) return -1;
222 /***********************************************************************
223 * GetTickCount (KERNEL32.@)
225 * Get the number of milliseconds the system has been running.
231 * The current tick count.
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();