- AddRef type info stored in StdDispatch.
[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 "thread.h"
42 #include "stackframe.h"
43 #include "wincon.h"
44 #include "console_private.h"
45
46 extern void LOCALE_Init(void);
47 extern BOOL RELAY_Init(void);
48 extern void COMPUTERNAME_Init(void);
49
50 extern  int __wine_set_signal_handler(unsigned, int (*)(unsigned));
51 /* memory/environ.c */
52 extern void ENV_CopyStartupInformation(void);
53
54 extern int main_create_flags;
55
56 static CRITICAL_SECTION ldt_section = CRITICAL_SECTION_INIT("ldt_section");
57
58 /***********************************************************************
59  *           locking for LDT routines
60  */
61 static void ldt_lock(void)
62 {
63     RtlEnterCriticalSection( &ldt_section );
64 }
65
66 static void ldt_unlock(void)
67 {
68     RtlLeaveCriticalSection( &ldt_section );
69 }
70
71
72 /***********************************************************************
73  *           KERNEL thread initialisation routine
74  */
75 static void thread_attach(void)
76 {
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) );
82 }
83
84
85 /***********************************************************************
86  *           KERNEL thread finalisation routine
87  */
88 static void thread_detach(void)
89 {
90     /* free the 16-bit stack */
91     K32WOWGlobalFree16( NtCurrentTeb()->stack_sel );
92     NtCurrentTeb()->cur_stack = 0;
93 }
94
95
96 /***********************************************************************
97  *           KERNEL process initialisation routine
98  */
99 static BOOL process_attach(void)
100 {
101     HMODULE16 hModule;
102
103     /* Get the umask */
104     FILE_umask = umask(0777);
105     umask( FILE_umask );
106
107     /* Setup codepage info */
108     LOCALE_Init();
109
110     /* Initialize relay entry points */
111     if (!RELAY_Init()) return FALSE;
112
113     /* Initialize DOS memory */
114     if (!DOSMEM_Init(0)) return FALSE;
115
116     /* Setup computer name */
117     COMPUTERNAME_Init();
118     
119     /* copy process information from ntdll */
120     ENV_CopyStartupInformation();
121
122     if ((hModule = LoadLibrary16( "krnl386.exe" )) >= 32)
123     {
124         /* Initialize special KERNEL entry points */
125
126         /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
127         NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
128
129         /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
130         NE_SetEntryPoint( hModule, 454, wine_get_cs() );
131         NE_SetEntryPoint( hModule, 455, wine_get_ds() );
132
133         /* Initialize KERNEL.THHOOK */
134         TASK_InstallTHHook(MapSL((SEGPTR)GetProcAddress16( hModule, (LPCSTR)332 )));
135
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 ))
141
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
153     }
154
155     /* Force loading of some dlls */
156     LoadLibrary16( "system" );
157
158     /* Create 16-bit task */
159     TASK_CreateMainTask();
160
161     /* Create the shared heap for broken win95 native dlls */
162     HeapCreate( HEAP_SHARED, 0, 0 );
163
164     /* initialize LDT locking */
165     wine_ldt_init_locking( ldt_lock, ldt_unlock );
166
167     /* finish the process initialisation for console bits, if needed */
168     __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
169
170     if (main_create_flags & CREATE_NEW_CONSOLE)
171     {
172         HMODULE mod = GetModuleHandleA(0);
173         if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
174             AllocConsole();
175     }
176     else if (!(main_create_flags & DETACHED_PROCESS))
177     {
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
181          */
182     }
183
184     if (main_create_flags & CREATE_NEW_PROCESS_GROUP)
185         SetConsoleCtrlHandler(NULL, TRUE);
186
187     thread_attach();
188     return TRUE;
189 }
190
191 /***********************************************************************
192  *           KERNEL initialisation routine
193  */
194 BOOL WINAPI MAIN_KernelInit( HINSTANCE hinst, DWORD reason, LPVOID reserved )
195 {
196     switch(reason)
197     {
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;
209     }
210     return TRUE;
211 }
212
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)
225  *
226  * Entry point for kernel functions that do nothing.
227  */
228 LONG WINAPI KERNEL_nop(void)
229 {
230     return 0;
231 }
232
233 /***********************************************************************
234  *              SwitchToThread (KERNEL32.@)
235  */
236
237 BOOL WINAPI SwitchToThread(void)
238 {
239     Sleep(0);
240     return 1;
241 }