Moved most remaining file functions to dlls/kernel.
[wine/multimedia.git] / dlls / kernel / kernel_main.c
blob43119c14470e79fbd4ddafeffd8a02813307c848
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 <sys/stat.h>
29 #include <signal.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wincon.h"
34 #include "winreg.h"
35 #include "winternl.h"
37 #include "wine/winbase16.h"
38 #include "wine/library.h"
39 #include "miscemu.h"
40 #include "module.h"
41 #include "thread.h"
42 #include "stackframe.h"
43 #include "wincon.h"
44 #include "toolhelp.h"
45 #include "kernel_private.h"
46 #include "console_private.h"
48 extern void LOCALE_InitRegistry(void);
49 extern void COMPUTERNAME_Init(void);
51 extern int __wine_set_signal_handler(unsigned, int (*)(unsigned));
52 /* memory/environ.c */
53 extern void ENV_CopyStartupInformation(void);
55 extern int main_create_flags;
57 static CRITICAL_SECTION ldt_section;
58 static CRITICAL_SECTION_DEBUG critsect_debug =
60 0, 0, &ldt_section,
61 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
62 0, 0, { 0, (DWORD)(__FILE__ ": ldt_section") }
64 static CRITICAL_SECTION ldt_section = { &critsect_debug, -1, 0, 0, 0, 0 };
66 /***********************************************************************
67 * locking for LDT routines
69 static void ldt_lock(void)
71 RtlEnterCriticalSection( &ldt_section );
74 static void ldt_unlock(void)
76 RtlLeaveCriticalSection( &ldt_section );
80 /***********************************************************************
81 * KERNEL thread initialisation routine
83 static void thread_attach(void)
85 /* allocate the 16-bit stack (FIXME: should be done lazily) */
86 HGLOBAL16 hstack = K32WOWGlobalAlloc16( GMEM_FIXED, 0x10000 );
87 NtCurrentTeb()->stack_sel = GlobalHandleToSel16( hstack );
88 NtCurrentTeb()->cur_stack = MAKESEGPTR( NtCurrentTeb()->stack_sel,
89 0x10000 - sizeof(STACK16FRAME) );
93 /***********************************************************************
94 * KERNEL thread finalisation routine
96 static void thread_detach(void)
98 /* free the 16-bit stack */
99 K32WOWGlobalFree16( NtCurrentTeb()->stack_sel );
100 NtCurrentTeb()->cur_stack = 0;
101 if (NtCurrentTeb()->Tib.SubSystemTib) TASK_ExitTask();
105 /***********************************************************************
106 * KERNEL process initialisation routine
108 static BOOL process_attach(void)
110 HMODULE16 hModule;
112 /* Setup registry locale information */
113 LOCALE_InitRegistry();
115 /* Initialize 16-bit thunking entry points */
116 if (!WOWTHUNK_Init()) return FALSE;
118 /* Initialize DOS memory */
119 if (!DOSMEM_Init(0)) return FALSE;
121 /* Setup computer name */
122 COMPUTERNAME_Init();
124 /* copy process information from ntdll */
125 ENV_CopyStartupInformation();
127 if ((hModule = LoadLibrary16( "krnl386.exe" )) >= 32)
129 /* Initialize special KERNEL entry points */
131 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
132 NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
134 /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
135 NE_SetEntryPoint( hModule, 454, wine_get_cs() );
136 NE_SetEntryPoint( hModule, 455, wine_get_ds() );
138 /* Initialize KERNEL.THHOOK */
139 TASK_InstallTHHook(MapSL((SEGPTR)GetProcAddress16( hModule, (LPCSTR)332 )));
141 /* Initialize the real-mode selector entry points */
142 #define SET_ENTRY_POINT( num, addr ) \
143 NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
144 DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
145 WINE_LDT_FLAGS_DATA ))
147 SET_ENTRY_POINT( 174, 0xa0000 ); /* KERNEL.174: __A000H */
148 SET_ENTRY_POINT( 181, 0xb0000 ); /* KERNEL.181: __B000H */
149 SET_ENTRY_POINT( 182, 0xb8000 ); /* KERNEL.182: __B800H */
150 SET_ENTRY_POINT( 195, 0xc0000 ); /* KERNEL.195: __C000H */
151 SET_ENTRY_POINT( 179, 0xd0000 ); /* KERNEL.179: __D000H */
152 SET_ENTRY_POINT( 190, 0xe0000 ); /* KERNEL.190: __E000H */
153 NE_SetEntryPoint( hModule, 183, DOSMEM_0000H ); /* KERNEL.183: __0000H */
154 NE_SetEntryPoint( hModule, 173, DOSMEM_BiosSysSeg ); /* KERNEL.173: __ROMBIOS */
155 NE_SetEntryPoint( hModule, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
156 NE_SetEntryPoint( hModule, 194, DOSMEM_BiosSysSeg ); /* KERNEL.194: __F000H */
157 #undef SET_ENTRY_POINT
159 /* Force loading of some dlls */
160 LoadLibrary16( "system.drv" );
163 /* Create the shared heap for broken win95 native dlls */
164 HeapCreate( HEAP_SHARED, 0, 0 );
166 /* initialize LDT locking */
167 wine_ldt_init_locking( ldt_lock, ldt_unlock );
169 /* finish the process initialisation for console bits, if needed */
170 __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
172 if (main_create_flags & CREATE_NEW_CONSOLE)
174 HMODULE mod = GetModuleHandleA(0);
175 if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
176 AllocConsole();
178 else if (!(main_create_flags & DETACHED_PROCESS))
180 /* 1/ shall inherit console + handles
181 * 2/ shall create std handles, if handles are not inherited
182 * TBD when not using wineserver handles for console handles
186 if (main_create_flags & CREATE_NEW_PROCESS_GROUP)
187 SetConsoleCtrlHandler(NULL, TRUE);
189 /* Create 16-bit task */
190 thread_attach();
191 TASK_CreateMainTask();
192 return TRUE;
195 /***********************************************************************
196 * KERNEL initialisation routine
198 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
200 switch(reason)
202 case DLL_PROCESS_ATTACH:
203 return process_attach();
204 case DLL_THREAD_ATTACH:
205 thread_attach();
206 break;
207 case DLL_THREAD_DETACH:
208 thread_detach();
209 break;
210 case DLL_PROCESS_DETACH:
211 WriteOutProfiles16();
212 break;
214 return TRUE;
217 /***********************************************************************
218 * EnableDos (KERNEL.41)
219 * DisableDos (KERNEL.42)
220 * GetLastDiskChange (KERNEL.98)
221 * ValidateCodeSegments (KERNEL.100)
222 * KbdRst (KERNEL.123)
223 * EnableKernel (KERNEL.124)
224 * DisableKernel (KERNEL.125)
225 * ValidateFreeSpaces (KERNEL.200)
226 * K237 (KERNEL.237)
227 * BUNNY_351 (KERNEL.351)
228 * PIGLET_361 (KERNEL.361)
230 * Entry point for kernel functions that do nothing.
232 LONG WINAPI KERNEL_nop(void)
234 return 0;
237 /***********************************************************************
238 * SwitchToThread (KERNEL32.@)
241 BOOL WINAPI SwitchToThread(void)
243 Sleep(0);
244 return 1;
248 /***********************************************************************
249 * MulDiv (KERNEL32.@)
250 * RETURNS
251 * Result of multiplication and division
252 * -1: Overflow occurred or Divisor was 0
254 INT WINAPI MulDiv( INT nMultiplicand, INT nMultiplier, INT nDivisor)
256 LONGLONG ret;
258 if (!nDivisor) return -1;
260 /* We want to deal with a positive divisor to simplify the logic. */
261 if (nDivisor < 0)
263 nMultiplicand = - nMultiplicand;
264 nDivisor = -nDivisor;
267 /* If the result is positive, we "add" to round. else, we subtract to round. */
268 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
269 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
270 ret = (((LONGLONG)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
271 else
272 ret = (((LONGLONG)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
274 if ((ret > 2147483647) || (ret < -2147483647)) return -1;
275 return ret;