Added NONAMELESSUNION/STRUCT defines.
[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 <string.h>
27 #include <sys/stat.h>
28 #include <signal.h>
29
30 #include "winbase.h"
31 #include "wincon.h"
32 #include "winternl.h"
33
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 "wincon.h"
42 #include "console_private.h"
43
44 extern void LOCALE_Init(void);
45 extern BOOL RELAY_Init(void);
46 extern void COMPUTERNAME_Init(void);
47
48 extern  int __wine_set_signal_handler(unsigned, int (*)(unsigned));
49
50 extern int main_create_flags;
51
52 static CRITICAL_SECTION ldt_section = CRITICAL_SECTION_INIT("ldt_section");
53
54 /***********************************************************************
55  *           locking for LDT routines
56  */
57 static void ldt_lock(void)
58 {
59     EnterCriticalSection( &ldt_section );
60 }
61
62 static void ldt_unlock(void)
63 {
64     LeaveCriticalSection( &ldt_section );
65 }
66
67 /***********************************************************************
68  *           KERNEL process initialisation routine
69  */
70 static BOOL process_attach(void)
71 {
72     HMODULE16 hModule;
73
74     /* Get the umask */
75     FILE_umask = umask(0777);
76     umask( FILE_umask );
77
78     /* Setup codepage info */
79     LOCALE_Init();
80
81     /* Initialize relay entry points */
82     if (!RELAY_Init()) return FALSE;
83
84     /* Initialize DOS memory */
85     if (!DOSMEM_Init(0)) return FALSE;
86
87     /* Setup computer name */
88     COMPUTERNAME_Init();
89
90     if ((hModule = LoadLibrary16( "krnl386.exe" )) >= 32)
91     {
92         /* Initialize special KERNEL entry points */
93
94         /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
95         NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
96
97         /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
98         NE_SetEntryPoint( hModule, 454, wine_get_cs() );
99         NE_SetEntryPoint( hModule, 455, wine_get_ds() );
100
101         /* Initialize KERNEL.THHOOK */
102         TASK_InstallTHHook(MapSL((SEGPTR)GetProcAddress16( hModule, (LPCSTR)332 )));
103
104         /* Initialize the real-mode selector entry points */
105 #define SET_ENTRY_POINT( num, addr ) \
106     NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
107                       DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
108                       WINE_LDT_FLAGS_DATA ))
109
110         SET_ENTRY_POINT( 174, 0xa0000 );  /* KERNEL.174: __A000H */
111         SET_ENTRY_POINT( 181, 0xb0000 );  /* KERNEL.181: __B000H */
112         SET_ENTRY_POINT( 182, 0xb8000 );  /* KERNEL.182: __B800H */
113         SET_ENTRY_POINT( 195, 0xc0000 );  /* KERNEL.195: __C000H */
114         SET_ENTRY_POINT( 179, 0xd0000 );  /* KERNEL.179: __D000H */
115         SET_ENTRY_POINT( 190, 0xe0000 );  /* KERNEL.190: __E000H */
116         NE_SetEntryPoint( hModule, 183, DOSMEM_0000H );       /* KERNEL.183: __0000H */
117         NE_SetEntryPoint( hModule, 173, DOSMEM_BiosSysSeg );  /* KERNEL.173: __ROMBIOS */
118         NE_SetEntryPoint( hModule, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
119         NE_SetEntryPoint( hModule, 194, DOSMEM_BiosSysSeg );  /* KERNEL.194: __F000H */
120 #undef SET_ENTRY_POINT
121     }
122
123     /* Force loading of some dlls */
124     LoadLibrary16( "system" );
125
126     /* Create 16-bit task */
127     TASK_CreateMainTask();
128
129     /* Create the shared heap for broken win95 native dlls */
130     HeapCreate( HEAP_SHARED, 0, 0 );
131
132     /* initialize LDT locking */
133     wine_ldt_init_locking( ldt_lock, ldt_unlock );
134
135     /* finish the process initialisation for console bits, if needed */
136     __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
137
138     if (main_create_flags & CREATE_NEW_CONSOLE)
139     {
140         HMODULE mod = GetModuleHandleA(0);
141         if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
142             AllocConsole();
143     }
144     if (main_create_flags & CREATE_NEW_PROCESS_GROUP)
145         SetConsoleCtrlHandler(NULL, TRUE);
146
147     return TRUE;
148 }
149
150 /***********************************************************************
151  *           KERNEL initialisation routine
152  */
153 BOOL WINAPI MAIN_KernelInit( HINSTANCE hinst, DWORD reason, LPVOID reserved )
154 {
155     switch(reason)
156     {
157     case DLL_PROCESS_ATTACH:
158         return process_attach();
159     case DLL_PROCESS_DETACH:
160         WriteOutProfiles16();
161         break;
162     }
163     return TRUE;
164 }
165
166 /***********************************************************************
167  *              EnableDos (KERNEL.41)
168  *              DisableDos (KERNEL.42)
169  *              GetLastDiskChange (KERNEL.98)
170  *              ValidateCodeSegments (KERNEL.100)
171  *              KbdRst (KERNEL.123)
172  *              EnableKernel (KERNEL.124)
173  *              DisableKernel (KERNEL.125)
174  *              ValidateFreeSpaces (KERNEL.200)
175  *              K237 (KERNEL.237)
176  *              BUNNY_351 (KERNEL.351)
177  *              PIGLET_361 (KERNEL.361)
178  *
179  * Entry point for kernel functions that do nothing.
180  */
181 LONG WINAPI KERNEL_nop(void)
182 {
183     return 0;
184 }
185
186 /***********************************************************************
187  *              SwitchToThread (KERNEL32.@)
188  */
189
190 BOOL WINAPI SwitchToThread(void)
191 {
192     Sleep(0);
193     return 1;
194 }