Spelling fix.
[wine/wine-kai.git] / dlls / kernel / kernel_main.c
blobab5974bcb44092f9e7dd8e5d54147cb4a5f5b637
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 "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));
51 /* memory/environ.c */
52 extern void ENV_CopyStartupInformation(void);
54 extern int main_create_flags;
56 static CRITICAL_SECTION ldt_section = CRITICAL_SECTION_INIT("ldt_section");
58 /***********************************************************************
59 * locking for LDT routines
61 static void ldt_lock(void)
63 RtlEnterCriticalSection( &ldt_section );
66 static void ldt_unlock(void)
68 RtlLeaveCriticalSection( &ldt_section );
72 /***********************************************************************
73 * KERNEL thread initialisation routine
75 static void thread_attach(void)
77 /* allocate the 16-bit stack (FIXME: should be done lazily) */
78 HGLOBAL16 hstack = K32WOWGlobalAlloc16( GMEM_FIXED, 0x10000 );
79 NtCurrentTeb()->stack_sel = GlobalHandleToSel16( hstack );
80 NtCurrentTeb()->cur_stack = MAKESEGPTR( NtCurrentTeb()->stack_sel,
81 0x10000 - sizeof(STACK16FRAME) );
85 /***********************************************************************
86 * KERNEL thread finalisation routine
88 static void thread_detach(void)
90 /* free the 16-bit stack */
91 K32WOWGlobalFree16( NtCurrentTeb()->stack_sel );
92 NtCurrentTeb()->cur_stack = 0;
96 /***********************************************************************
97 * KERNEL process initialisation routine
99 static BOOL process_attach(void)
101 HMODULE16 hModule;
103 /* Get the umask */
104 FILE_umask = umask(0777);
105 umask( FILE_umask );
107 /* Setup codepage info */
108 LOCALE_Init();
110 /* Initialize relay entry points */
111 if (!RELAY_Init()) return FALSE;
113 /* Initialize DOS memory */
114 if (!DOSMEM_Init(0)) return FALSE;
116 /* Setup computer name */
117 COMPUTERNAME_Init();
119 /* copy process information from ntdll */
120 ENV_CopyStartupInformation();
122 if ((hModule = LoadLibrary16( "krnl386.exe" )) >= 32)
124 /* Initialize special KERNEL entry points */
126 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
127 NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
129 /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
130 NE_SetEntryPoint( hModule, 454, wine_get_cs() );
131 NE_SetEntryPoint( hModule, 455, wine_get_ds() );
133 /* Initialize KERNEL.THHOOK */
134 TASK_InstallTHHook(MapSL((SEGPTR)GetProcAddress16( hModule, (LPCSTR)332 )));
136 /* Initialize the real-mode selector entry points */
137 #define SET_ENTRY_POINT( num, addr ) \
138 NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
139 DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
140 WINE_LDT_FLAGS_DATA ))
142 SET_ENTRY_POINT( 174, 0xa0000 ); /* KERNEL.174: __A000H */
143 SET_ENTRY_POINT( 181, 0xb0000 ); /* KERNEL.181: __B000H */
144 SET_ENTRY_POINT( 182, 0xb8000 ); /* KERNEL.182: __B800H */
145 SET_ENTRY_POINT( 195, 0xc0000 ); /* KERNEL.195: __C000H */
146 SET_ENTRY_POINT( 179, 0xd0000 ); /* KERNEL.179: __D000H */
147 SET_ENTRY_POINT( 190, 0xe0000 ); /* KERNEL.190: __E000H */
148 NE_SetEntryPoint( hModule, 183, DOSMEM_0000H ); /* KERNEL.183: __0000H */
149 NE_SetEntryPoint( hModule, 173, DOSMEM_BiosSysSeg ); /* KERNEL.173: __ROMBIOS */
150 NE_SetEntryPoint( hModule, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
151 NE_SetEntryPoint( hModule, 194, DOSMEM_BiosSysSeg ); /* KERNEL.194: __F000H */
152 #undef SET_ENTRY_POINT
155 /* Force loading of some dlls */
156 LoadLibrary16( "system" );
158 /* Create 16-bit task */
159 TASK_CreateMainTask();
161 /* Create the shared heap for broken win95 native dlls */
162 HeapCreate( HEAP_SHARED, 0, 0 );
164 /* initialize LDT locking */
165 wine_ldt_init_locking( ldt_lock, ldt_unlock );
167 /* finish the process initialisation for console bits, if needed */
168 __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
170 if (main_create_flags & CREATE_NEW_CONSOLE)
172 HMODULE mod = GetModuleHandleA(0);
173 if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
174 AllocConsole();
176 else if (!(main_create_flags & DETACHED_PROCESS))
178 /* 1/ shall inherit console + handles
179 * 2/ shall create std handles, if handles are not inherited
180 * TBD when not using wineserver handles for console handles
184 if (main_create_flags & CREATE_NEW_PROCESS_GROUP)
185 SetConsoleCtrlHandler(NULL, TRUE);
187 thread_attach();
188 return TRUE;
191 /***********************************************************************
192 * KERNEL initialisation routine
194 BOOL WINAPI MAIN_KernelInit( HINSTANCE hinst, DWORD reason, LPVOID reserved )
196 switch(reason)
198 case DLL_PROCESS_ATTACH:
199 return process_attach();
200 case DLL_THREAD_ATTACH:
201 thread_attach();
202 break;
203 case DLL_THREAD_DETACH:
204 thread_detach();
205 break;
206 case DLL_PROCESS_DETACH:
207 WriteOutProfiles16();
208 break;
210 return TRUE;
213 /***********************************************************************
214 * EnableDos (KERNEL.41)
215 * DisableDos (KERNEL.42)
216 * GetLastDiskChange (KERNEL.98)
217 * ValidateCodeSegments (KERNEL.100)
218 * KbdRst (KERNEL.123)
219 * EnableKernel (KERNEL.124)
220 * DisableKernel (KERNEL.125)
221 * ValidateFreeSpaces (KERNEL.200)
222 * K237 (KERNEL.237)
223 * BUNNY_351 (KERNEL.351)
224 * PIGLET_361 (KERNEL.361)
226 * Entry point for kernel functions that do nothing.
228 LONG WINAPI KERNEL_nop(void)
230 return 0;
233 /***********************************************************************
234 * SwitchToThread (KERNEL32.@)
237 BOOL WINAPI SwitchToThread(void)
239 Sleep(0);
240 return 1;