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"
34 #include "wine/winbase16.h"
35 #include "wine/library.h"
42 #include "stackframe.h"
44 #include "console_private.h"
46 extern void LOCALE_Init(void);
47 extern BOOL RELAY_Init(void);
48 extern void COMPUTERNAME_Init(void);
50 extern int __wine_set_signal_handler(unsigned, int (*)(unsigned));
52 extern int main_create_flags;
54 static CRITICAL_SECTION ldt_section = CRITICAL_SECTION_INIT("ldt_section");
56 /***********************************************************************
57 * locking for LDT routines
59 static void ldt_lock(void)
61 EnterCriticalSection( &ldt_section );
64 static void ldt_unlock(void)
66 LeaveCriticalSection( &ldt_section );
70 /***********************************************************************
71 * KERNEL thread initialisation routine
73 static void thread_attach(void)
75 /* allocate the 16-bit stack (FIXME: should be done lazily) */
76 HGLOBAL16 hstack = K32WOWGlobalAlloc16( GMEM_FIXED, 0x10000 );
77 NtCurrentTeb()->stack_sel = GlobalHandleToSel16( hstack );
78 NtCurrentTeb()->cur_stack = MAKESEGPTR( NtCurrentTeb()->stack_sel,
79 0x10000 - sizeof(STACK16FRAME) );
83 /***********************************************************************
84 * KERNEL thread finalisation routine
86 static void thread_detach(void)
88 /* free the 16-bit stack */
89 K32WOWGlobalFree16( NtCurrentTeb()->stack_sel );
90 NtCurrentTeb()->cur_stack = 0;
94 /***********************************************************************
95 * KERNEL process initialisation routine
97 static BOOL process_attach(void)
102 FILE_umask = umask(0777);
105 /* Setup codepage info */
108 /* Initialize relay entry points */
109 if (!RELAY_Init()) return FALSE;
111 /* Initialize DOS memory */
112 if (!DOSMEM_Init(0)) return FALSE;
114 /* Setup computer name */
117 if ((hModule = LoadLibrary16( "krnl386.exe" )) >= 32)
119 /* Initialize special KERNEL entry points */
121 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
122 NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
124 /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
125 NE_SetEntryPoint( hModule, 454, wine_get_cs() );
126 NE_SetEntryPoint( hModule, 455, wine_get_ds() );
128 /* Initialize KERNEL.THHOOK */
129 TASK_InstallTHHook(MapSL((SEGPTR)GetProcAddress16( hModule, (LPCSTR)332 )));
131 /* Initialize the real-mode selector entry points */
132 #define SET_ENTRY_POINT( num, addr ) \
133 NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
134 DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
135 WINE_LDT_FLAGS_DATA ))
137 SET_ENTRY_POINT( 174, 0xa0000 ); /* KERNEL.174: __A000H */
138 SET_ENTRY_POINT( 181, 0xb0000 ); /* KERNEL.181: __B000H */
139 SET_ENTRY_POINT( 182, 0xb8000 ); /* KERNEL.182: __B800H */
140 SET_ENTRY_POINT( 195, 0xc0000 ); /* KERNEL.195: __C000H */
141 SET_ENTRY_POINT( 179, 0xd0000 ); /* KERNEL.179: __D000H */
142 SET_ENTRY_POINT( 190, 0xe0000 ); /* KERNEL.190: __E000H */
143 NE_SetEntryPoint( hModule, 183, DOSMEM_0000H ); /* KERNEL.183: __0000H */
144 NE_SetEntryPoint( hModule, 173, DOSMEM_BiosSysSeg ); /* KERNEL.173: __ROMBIOS */
145 NE_SetEntryPoint( hModule, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
146 NE_SetEntryPoint( hModule, 194, DOSMEM_BiosSysSeg ); /* KERNEL.194: __F000H */
147 #undef SET_ENTRY_POINT
150 /* Force loading of some dlls */
151 LoadLibrary16( "system" );
153 /* Create 16-bit task */
154 TASK_CreateMainTask();
156 /* Create the shared heap for broken win95 native dlls */
157 HeapCreate( HEAP_SHARED, 0, 0 );
159 /* initialize LDT locking */
160 wine_ldt_init_locking( ldt_lock, ldt_unlock );
162 /* finish the process initialisation for console bits, if needed */
163 __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
165 if (main_create_flags & CREATE_NEW_CONSOLE)
167 HMODULE mod = GetModuleHandleA(0);
168 if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
171 if (main_create_flags & CREATE_NEW_PROCESS_GROUP)
172 SetConsoleCtrlHandler(NULL, TRUE);
178 /***********************************************************************
179 * KERNEL initialisation routine
181 BOOL WINAPI MAIN_KernelInit( HINSTANCE hinst, DWORD reason, LPVOID reserved )
185 case DLL_PROCESS_ATTACH:
186 return process_attach();
187 case DLL_THREAD_ATTACH:
190 case DLL_THREAD_DETACH:
193 case DLL_PROCESS_DETACH:
194 WriteOutProfiles16();
200 /***********************************************************************
201 * EnableDos (KERNEL.41)
202 * DisableDos (KERNEL.42)
203 * GetLastDiskChange (KERNEL.98)
204 * ValidateCodeSegments (KERNEL.100)
205 * KbdRst (KERNEL.123)
206 * EnableKernel (KERNEL.124)
207 * DisableKernel (KERNEL.125)
208 * ValidateFreeSpaces (KERNEL.200)
210 * BUNNY_351 (KERNEL.351)
211 * PIGLET_361 (KERNEL.361)
213 * Entry point for kernel functions that do nothing.
215 LONG WINAPI KERNEL_nop(void)
220 /***********************************************************************
221 * SwitchToThread (KERNEL32.@)
224 BOOL WINAPI SwitchToThread(void)