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 /***********************************************************************
47 * KERNEL thread initialisation routine
49 static void thread_attach(void)
51 /* allocate the 16-bit stack (FIXME: should be done lazily) */
52 HGLOBAL16 hstack = WOWGlobalAlloc16( GMEM_FIXED, 0x10000 );
53 kernel_get_thread_data()->stack_sel = GlobalHandleToSel16( hstack );
54 NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR( kernel_get_thread_data()->stack_sel,
55 0x10000 - sizeof(STACK16FRAME) );
59 /***********************************************************************
60 * KERNEL thread finalisation routine
62 static void thread_detach(void)
64 /* free the 16-bit stack */
65 WOWGlobalFree16( kernel_get_thread_data()->stack_sel );
66 NtCurrentTeb()->WOW32Reserved = 0;
67 if (NtCurrentTeb()->Tib.SubSystemTib) TASK_ExitTask();
71 /***********************************************************************
72 * KERNEL process initialisation routine
74 static BOOL process_attach(void)
77 RTL_USER_PROCESS_PARAMETERS *params = NtCurrentTeb()->Peb->ProcessParameters;
79 /* FIXME: should probably be done in ntdll */
81 NtCurrentTeb()->Peb->NumberOfProcessors = si.dwNumberOfProcessors;
83 /* Setup registry locale information */
84 LOCALE_InitRegistry();
86 /* Setup computer name */
89 /* convert value from server:
90 * + 0 => INVALID_HANDLE_VALUE
91 * + console handle needs to be mapped
93 if (!params->hStdInput)
94 params->hStdInput = INVALID_HANDLE_VALUE;
95 else if (VerifyConsoleIoHandle(console_handle_map(params->hStdInput)))
96 params->hStdInput = console_handle_map(params->hStdInput);
98 if (!params->hStdOutput)
99 params->hStdOutput = INVALID_HANDLE_VALUE;
100 else if (VerifyConsoleIoHandle(console_handle_map(params->hStdOutput)))
101 params->hStdOutput = console_handle_map(params->hStdOutput);
103 if (!params->hStdError)
104 params->hStdError = INVALID_HANDLE_VALUE;
105 else if (VerifyConsoleIoHandle(console_handle_map(params->hStdError)))
106 params->hStdError = console_handle_map(params->hStdError);
108 /* copy process information from ntdll */
109 ENV_CopyStartupInformation();
112 if (GetVersion() & 0x80000000)
114 /* create the shared heap for broken win95 native dlls */
115 HeapCreate( HEAP_SHARED, 0, 0 );
116 /* setup emulation of protected instructions from 32-bit code */
117 RtlAddVectoredExceptionHandler( TRUE, INSTR_vectored_handler );
121 /* finish the process initialisation for console bits, if needed */
122 __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
124 if (params->ConsoleHandle == (HANDLE)1) /* FIXME */
126 HMODULE mod = GetModuleHandleA(0);
127 if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
130 /* else TODO for DETACHED_PROCESS:
131 * 1/ inherit console + handles
132 * 2/ create std handles, if handles are not inherited
133 * TBD when not using wineserver handles for console handles
136 /* Create 16-bit task */
137 LoadLibrary16( "krnl386.exe" );
139 TASK_CreateMainTask();
143 /***********************************************************************
144 * KERNEL initialisation routine
146 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
150 case DLL_PROCESS_ATTACH:
151 return process_attach();
152 case DLL_THREAD_ATTACH:
155 case DLL_THREAD_DETACH:
158 case DLL_PROCESS_DETACH:
159 WriteOutProfiles16();
165 /***********************************************************************
166 * MulDiv (KERNEL32.@)
168 * Result of multiplication and division
169 * -1: Overflow occurred or Divisor was 0
171 INT WINAPI MulDiv( INT nMultiplicand, INT nMultiplier, INT nDivisor)
175 if (!nDivisor) return -1;
177 /* We want to deal with a positive divisor to simplify the logic. */
180 nMultiplicand = - nMultiplicand;
181 nDivisor = -nDivisor;
184 /* If the result is positive, we "add" to round. else, we subtract to round. */
185 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
186 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
187 ret = (((LONGLONG)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
189 ret = (((LONGLONG)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
191 if ((ret > 2147483647) || (ret < -2147483647)) return -1;
196 /***********************************************************************
197 * GetTickCount (KERNEL32.@)
199 * Get the number of milliseconds the system has been running.
205 * The current tick count.
208 * -The value returned will wrap arounf every 2^32 milliseconds.
209 * -Under Windows, tick 0 is the moment at which the system is rebooted.
210 * Under Wine, tick 0 begins at the moment the wineserver process is started,
212 DWORD WINAPI GetTickCount(void)
214 return NtGetTickCount();