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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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)
101 /* FIXME: should probably be done in ntdll */
102 GetSystemInfo( &si );
103 NtCurrentTeb()->Peb->NumberOfProcessors = si.dwNumberOfProcessors;
105 /* Setup registry locale information */
106 LOCALE_InitRegistry();
108 /* Setup computer name */
111 /* copy process information from ntdll */
112 ENV_CopyStartupInformation();
115 if (GetVersion() & 0x80000000)
117 /* create the shared heap for broken win95 native dlls */
118 HeapCreate( HEAP_SHARED, 0, 0 );
119 /* setup emulation of protected instructions from 32-bit code */
120 RtlAddVectoredExceptionHandler( TRUE, INSTR_vectored_handler );
124 /* initialize LDT locking */
125 wine_ldt_init_locking( ldt_lock, ldt_unlock );
127 /* finish the process initialisation for console bits, if needed */
128 __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
130 if (NtCurrentTeb()->Peb->ProcessParameters->ConsoleHandle == (HANDLE)1) /* FIXME */
132 HMODULE mod = GetModuleHandleA(0);
133 if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
136 /* else TODO for DETACHED_PROCESS:
137 * 1/ inherit console + handles
138 * 2/ create std handles, if handles are not inherited
139 * TBD when not using wineserver handles for console handles
142 /* Create 16-bit task */
143 LoadLibrary16( "krnl386.exe" );
145 TASK_CreateMainTask();
149 /***********************************************************************
150 * KERNEL initialisation routine
152 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
156 case DLL_PROCESS_ATTACH:
157 return process_attach();
158 case DLL_THREAD_ATTACH:
161 case DLL_THREAD_DETACH:
164 case DLL_PROCESS_DETACH:
165 WriteOutProfiles16();
171 /***********************************************************************
172 * MulDiv (KERNEL32.@)
174 * Result of multiplication and division
175 * -1: Overflow occurred or Divisor was 0
177 INT WINAPI MulDiv( INT nMultiplicand, INT nMultiplier, INT nDivisor)
181 if (!nDivisor) return -1;
183 /* We want to deal with a positive divisor to simplify the logic. */
186 nMultiplicand = - nMultiplicand;
187 nDivisor = -nDivisor;
190 /* If the result is positive, we "add" to round. else, we subtract to round. */
191 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
192 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
193 ret = (((LONGLONG)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
195 ret = (((LONGLONG)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
197 if ((ret > 2147483647) || (ret < -2147483647)) return -1;
202 /***********************************************************************
203 * GetTickCount (KERNEL32.@)
205 * Get the number of milliseconds the system has been running.
211 * The current tick count.
214 * -The value returned will wrap arounf every 2^32 milliseconds.
215 * -Under Windows, tick 0 is the moment at which the system is rebooted.
216 * Under Wine, tick 0 begins at the moment the wineserver process is started,
218 DWORD WINAPI GetTickCount(void)
220 return NtGetTickCount();