Moved 16-bit calls initialization and exception handling to kernel32.
[wine/multimedia.git] / dlls / kernel / kernel_main.c
blob34dd9b1f1f00fc9b220cfd78e7dddf56a48be8bd
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 <string.h>
27 #include <sys/stat.h>
28 #include <signal.h>
30 #include "winbase.h"
31 #include "wincon.h"
32 #include "winternl.h"
34 #include "wine/winbase16.h"
35 #include "wine/library.h"
36 #include "file.h"
37 #include "global.h"
38 #include "miscemu.h"
39 #include "module.h"
40 #include "task.h"
41 #include "thread.h"
42 #include "stackframe.h"
43 #include "wincon.h"
44 #include "kernel_private.h"
45 #include "console_private.h"
47 extern void LOCALE_Init(void);
48 extern void COMPUTERNAME_Init(void);
50 extern int __wine_set_signal_handler(unsigned, int (*)(unsigned));
51 /* memory/environ.c */
52 extern void ENV_CopyStartupInformation(void);
54 extern int main_create_flags;
56 static CRITICAL_SECTION ldt_section;
57 static CRITICAL_SECTION_DEBUG critsect_debug =
59 0, 0, &ldt_section,
60 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
61 0, 0, { 0, (DWORD)(__FILE__ ": ldt_section") }
63 static CRITICAL_SECTION ldt_section = { &critsect_debug, -1, 0, 0, 0, 0 };
65 /***********************************************************************
66 * locking for LDT routines
68 static void ldt_lock(void)
70 RtlEnterCriticalSection( &ldt_section );
73 static void ldt_unlock(void)
75 RtlLeaveCriticalSection( &ldt_section );
79 /***********************************************************************
80 * KERNEL thread initialisation routine
82 static void thread_attach(void)
84 /* allocate the 16-bit stack (FIXME: should be done lazily) */
85 HGLOBAL16 hstack = K32WOWGlobalAlloc16( GMEM_FIXED, 0x10000 );
86 NtCurrentTeb()->stack_sel = GlobalHandleToSel16( hstack );
87 NtCurrentTeb()->cur_stack = MAKESEGPTR( NtCurrentTeb()->stack_sel,
88 0x10000 - sizeof(STACK16FRAME) );
92 /***********************************************************************
93 * KERNEL thread finalisation routine
95 static void thread_detach(void)
97 /* free the 16-bit stack */
98 K32WOWGlobalFree16( NtCurrentTeb()->stack_sel );
99 NtCurrentTeb()->cur_stack = 0;
100 if (!(NtCurrentTeb()->tibflags & TEBF_WIN32)) TASK_ExitTask();
104 /***********************************************************************
105 * KERNEL process initialisation routine
107 static BOOL process_attach(void)
109 HMODULE16 hModule;
111 /* Get the umask */
112 FILE_umask = umask(0777);
113 umask( FILE_umask );
115 /* Setup codepage info */
116 LOCALE_Init();
118 /* Initialize 16-bit thunking entry points */
119 if (!WOWTHUNK_Init()) return FALSE;
121 /* Initialize DOS memory */
122 if (!DOSMEM_Init(0)) return FALSE;
124 /* Setup computer name */
125 COMPUTERNAME_Init();
127 /* copy process information from ntdll */
128 ENV_CopyStartupInformation();
130 if ((hModule = LoadLibrary16( "krnl386.exe" )) >= 32)
132 /* Initialize special KERNEL entry points */
134 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
135 NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
137 /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
138 NE_SetEntryPoint( hModule, 454, wine_get_cs() );
139 NE_SetEntryPoint( hModule, 455, wine_get_ds() );
141 /* Initialize KERNEL.THHOOK */
142 TASK_InstallTHHook(MapSL((SEGPTR)GetProcAddress16( hModule, (LPCSTR)332 )));
144 /* Initialize the real-mode selector entry points */
145 #define SET_ENTRY_POINT( num, addr ) \
146 NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
147 DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
148 WINE_LDT_FLAGS_DATA ))
150 SET_ENTRY_POINT( 174, 0xa0000 ); /* KERNEL.174: __A000H */
151 SET_ENTRY_POINT( 181, 0xb0000 ); /* KERNEL.181: __B000H */
152 SET_ENTRY_POINT( 182, 0xb8000 ); /* KERNEL.182: __B800H */
153 SET_ENTRY_POINT( 195, 0xc0000 ); /* KERNEL.195: __C000H */
154 SET_ENTRY_POINT( 179, 0xd0000 ); /* KERNEL.179: __D000H */
155 SET_ENTRY_POINT( 190, 0xe0000 ); /* KERNEL.190: __E000H */
156 NE_SetEntryPoint( hModule, 183, DOSMEM_0000H ); /* KERNEL.183: __0000H */
157 NE_SetEntryPoint( hModule, 173, DOSMEM_BiosSysSeg ); /* KERNEL.173: __ROMBIOS */
158 NE_SetEntryPoint( hModule, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
159 NE_SetEntryPoint( hModule, 194, DOSMEM_BiosSysSeg ); /* KERNEL.194: __F000H */
160 #undef SET_ENTRY_POINT
162 /* Force loading of some dlls */
163 LoadLibrary16( "system.drv" );
166 /* Create 16-bit task */
167 TASK_CreateMainTask();
169 /* Create the shared heap for broken win95 native dlls */
170 HeapCreate( HEAP_SHARED, 0, 0 );
172 /* initialize LDT locking */
173 wine_ldt_init_locking( ldt_lock, ldt_unlock );
175 /* finish the process initialisation for console bits, if needed */
176 __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
178 if (main_create_flags & CREATE_NEW_CONSOLE)
180 HMODULE mod = GetModuleHandleA(0);
181 if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
182 AllocConsole();
184 else if (!(main_create_flags & DETACHED_PROCESS))
186 /* 1/ shall inherit console + handles
187 * 2/ shall create std handles, if handles are not inherited
188 * TBD when not using wineserver handles for console handles
192 if (main_create_flags & CREATE_NEW_PROCESS_GROUP)
193 SetConsoleCtrlHandler(NULL, TRUE);
195 thread_attach();
196 return TRUE;
199 /***********************************************************************
200 * KERNEL initialisation routine
202 BOOL WINAPI MAIN_KernelInit( HINSTANCE hinst, DWORD reason, LPVOID reserved )
204 switch(reason)
206 case DLL_PROCESS_ATTACH:
207 return process_attach();
208 case DLL_THREAD_ATTACH:
209 thread_attach();
210 break;
211 case DLL_THREAD_DETACH:
212 thread_detach();
213 break;
214 case DLL_PROCESS_DETACH:
215 WriteOutProfiles16();
216 break;
218 return TRUE;
221 /***********************************************************************
222 * EnableDos (KERNEL.41)
223 * DisableDos (KERNEL.42)
224 * GetLastDiskChange (KERNEL.98)
225 * ValidateCodeSegments (KERNEL.100)
226 * KbdRst (KERNEL.123)
227 * EnableKernel (KERNEL.124)
228 * DisableKernel (KERNEL.125)
229 * ValidateFreeSpaces (KERNEL.200)
230 * K237 (KERNEL.237)
231 * BUNNY_351 (KERNEL.351)
232 * PIGLET_361 (KERNEL.361)
234 * Entry point for kernel functions that do nothing.
236 LONG WINAPI KERNEL_nop(void)
238 return 0;
241 /***********************************************************************
242 * SwitchToThread (KERNEL32.@)
245 BOOL WINAPI SwitchToThread(void)
247 Sleep(0);
248 return 1;