Add Traditional Chinese Big5 and Simplified Chinese GBK mappings.
[wine] / dlls / kernel / kernel_main.c
1 /*
2  * Kernel initialization code
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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>
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wincon.h"
34 #include "winreg.h"
35 #include "winternl.h"
36
37 #include "wine/winbase16.h"
38 #include "wine/library.h"
39 #include "file.h"
40 #include "miscemu.h"
41 #include "module.h"
42 #include "thread.h"
43 #include "stackframe.h"
44 #include "wincon.h"
45 #include "toolhelp.h"
46 #include "kernel_private.h"
47 #include "console_private.h"
48
49 extern void LOCALE_InitRegistry(void);
50 extern void COMPUTERNAME_Init(void);
51
52 extern  int __wine_set_signal_handler(unsigned, int (*)(unsigned));
53 extern void VOLUME_CreateDevices(void);
54 /* memory/environ.c */
55 extern void ENV_CopyStartupInformation(void);
56
57 extern int main_create_flags;
58
59 static CRITICAL_SECTION ldt_section;
60 static CRITICAL_SECTION_DEBUG critsect_debug =
61 {
62     0, 0, &ldt_section,
63     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
64       0, 0, { 0, (DWORD)(__FILE__ ": ldt_section") }
65 };
66 static CRITICAL_SECTION ldt_section = { &critsect_debug, -1, 0, 0, 0, 0 };
67
68 /***********************************************************************
69  *           locking for LDT routines
70  */
71 static void ldt_lock(void)
72 {
73     RtlEnterCriticalSection( &ldt_section );
74 }
75
76 static void ldt_unlock(void)
77 {
78     RtlLeaveCriticalSection( &ldt_section );
79 }
80
81
82 /***********************************************************************
83  *           KERNEL thread initialisation routine
84  */
85 static void thread_attach(void)
86 {
87     /* allocate the 16-bit stack (FIXME: should be done lazily) */
88     HGLOBAL16 hstack = K32WOWGlobalAlloc16( GMEM_FIXED, 0x10000 );
89     NtCurrentTeb()->stack_sel = GlobalHandleToSel16( hstack );
90     NtCurrentTeb()->cur_stack = MAKESEGPTR( NtCurrentTeb()->stack_sel,
91                                             0x10000 - sizeof(STACK16FRAME) );
92 }
93
94
95 /***********************************************************************
96  *           KERNEL thread finalisation routine
97  */
98 static void thread_detach(void)
99 {
100     /* free the 16-bit stack */
101     K32WOWGlobalFree16( NtCurrentTeb()->stack_sel );
102     NtCurrentTeb()->cur_stack = 0;
103     if (!(NtCurrentTeb()->tibflags & TEBF_WIN32)) TASK_ExitTask();
104 }
105
106
107 /***********************************************************************
108  *           KERNEL process initialisation routine
109  */
110 static BOOL process_attach(void)
111 {
112     HMODULE16 hModule;
113
114     /* Get the umask */
115     FILE_umask = umask(0777);
116     umask( FILE_umask );
117
118     /* Setup registry locale information */
119     LOCALE_InitRegistry();
120
121     /* Initialize 16-bit thunking entry points */
122     if (!WOWTHUNK_Init()) return FALSE;
123
124     /* Initialize DOS memory */
125     if (!DOSMEM_Init(0)) return FALSE;
126
127     /* Setup computer name */
128     COMPUTERNAME_Init();
129
130     /* Create device symlinks */
131     VOLUME_CreateDevices();
132
133     /* copy process information from ntdll */
134     ENV_CopyStartupInformation();
135
136     if ((hModule = LoadLibrary16( "krnl386.exe" )) >= 32)
137     {
138         /* Initialize special KERNEL entry points */
139
140         /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
141         NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
142
143         /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
144         NE_SetEntryPoint( hModule, 454, wine_get_cs() );
145         NE_SetEntryPoint( hModule, 455, wine_get_ds() );
146
147         /* Initialize KERNEL.THHOOK */
148         TASK_InstallTHHook(MapSL((SEGPTR)GetProcAddress16( hModule, (LPCSTR)332 )));
149
150         /* Initialize the real-mode selector entry points */
151 #define SET_ENTRY_POINT( num, addr ) \
152     NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
153                       DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
154                       WINE_LDT_FLAGS_DATA ))
155
156         SET_ENTRY_POINT( 174, 0xa0000 );  /* KERNEL.174: __A000H */
157         SET_ENTRY_POINT( 181, 0xb0000 );  /* KERNEL.181: __B000H */
158         SET_ENTRY_POINT( 182, 0xb8000 );  /* KERNEL.182: __B800H */
159         SET_ENTRY_POINT( 195, 0xc0000 );  /* KERNEL.195: __C000H */
160         SET_ENTRY_POINT( 179, 0xd0000 );  /* KERNEL.179: __D000H */
161         SET_ENTRY_POINT( 190, 0xe0000 );  /* KERNEL.190: __E000H */
162         NE_SetEntryPoint( hModule, 183, DOSMEM_0000H );       /* KERNEL.183: __0000H */
163         NE_SetEntryPoint( hModule, 173, DOSMEM_BiosSysSeg );  /* KERNEL.173: __ROMBIOS */
164         NE_SetEntryPoint( hModule, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
165         NE_SetEntryPoint( hModule, 194, DOSMEM_BiosSysSeg );  /* KERNEL.194: __F000H */
166 #undef SET_ENTRY_POINT
167
168         /* Force loading of some dlls */
169         LoadLibrary16( "system.drv" );
170     }
171
172     /* Create the shared heap for broken win95 native dlls */
173     HeapCreate( HEAP_SHARED, 0, 0 );
174
175     /* initialize LDT locking */
176     wine_ldt_init_locking( ldt_lock, ldt_unlock );
177
178     /* finish the process initialisation for console bits, if needed */
179     __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
180
181     if (main_create_flags & CREATE_NEW_CONSOLE)
182     {
183         HMODULE mod = GetModuleHandleA(0);
184         if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
185             AllocConsole();
186     }
187     else if (!(main_create_flags & DETACHED_PROCESS))
188     {
189         /* 1/ shall inherit console + handles
190          * 2/ shall create std handles, if handles are not inherited
191          * TBD when not using wineserver handles for console handles
192          */
193     }
194
195     if (main_create_flags & CREATE_NEW_PROCESS_GROUP)
196         SetConsoleCtrlHandler(NULL, TRUE);
197
198     /* Create 16-bit task */
199     thread_attach();
200     TASK_CreateMainTask();
201     return TRUE;
202 }
203
204 /***********************************************************************
205  *           KERNEL initialisation routine
206  */
207 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
208 {
209     switch(reason)
210     {
211     case DLL_PROCESS_ATTACH:
212         return process_attach();
213     case DLL_THREAD_ATTACH:
214         thread_attach();
215         break;
216     case DLL_THREAD_DETACH:
217         thread_detach();
218         break;
219     case DLL_PROCESS_DETACH:
220         WriteOutProfiles16();
221         break;
222     }
223     return TRUE;
224 }
225
226 /***********************************************************************
227  *              EnableDos (KERNEL.41)
228  *              DisableDos (KERNEL.42)
229  *              GetLastDiskChange (KERNEL.98)
230  *              ValidateCodeSegments (KERNEL.100)
231  *              KbdRst (KERNEL.123)
232  *              EnableKernel (KERNEL.124)
233  *              DisableKernel (KERNEL.125)
234  *              ValidateFreeSpaces (KERNEL.200)
235  *              K237 (KERNEL.237)
236  *              BUNNY_351 (KERNEL.351)
237  *              PIGLET_361 (KERNEL.361)
238  *
239  * Entry point for kernel functions that do nothing.
240  */
241 LONG WINAPI KERNEL_nop(void)
242 {
243     return 0;
244 }
245
246 /***********************************************************************
247  *              SwitchToThread (KERNEL32.@)
248  */
249
250 BOOL WINAPI SwitchToThread(void)
251 {
252     Sleep(0);
253     return 1;
254 }