Moved atom functions to dlls/kernel.
[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
52 extern int main_create_flags;
53
54 static CRITICAL_SECTION ldt_section = CRITICAL_SECTION_INIT("ldt_section");
55
56 /***********************************************************************
57  *           locking for LDT routines
58  */
59 static void ldt_lock(void)
60 {
61     EnterCriticalSection( &ldt_section );
62 }
63
64 static void ldt_unlock(void)
65 {
66     LeaveCriticalSection( &ldt_section );
67 }
68
69
70 /***********************************************************************
71  *           KERNEL thread initialisation routine
72  */
73 static void thread_attach(void)
74 {
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) );
80 }
81
82
83 /***********************************************************************
84  *           KERNEL thread finalisation routine
85  */
86 static void thread_detach(void)
87 {
88     /* free the 16-bit stack */
89     K32WOWGlobalFree16( NtCurrentTeb()->stack_sel );
90     NtCurrentTeb()->cur_stack = 0;
91 }
92
93
94 /***********************************************************************
95  *           KERNEL process initialisation routine
96  */
97 static BOOL process_attach(void)
98 {
99     HMODULE16 hModule;
100
101     /* Get the umask */
102     FILE_umask = umask(0777);
103     umask( FILE_umask );
104
105     /* Setup codepage info */
106     LOCALE_Init();
107
108     /* Initialize relay entry points */
109     if (!RELAY_Init()) return FALSE;
110
111     /* Initialize DOS memory */
112     if (!DOSMEM_Init(0)) return FALSE;
113
114     /* Setup computer name */
115     COMPUTERNAME_Init();
116
117     if ((hModule = LoadLibrary16( "krnl386.exe" )) >= 32)
118     {
119         /* Initialize special KERNEL entry points */
120
121         /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
122         NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
123
124         /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
125         NE_SetEntryPoint( hModule, 454, wine_get_cs() );
126         NE_SetEntryPoint( hModule, 455, wine_get_ds() );
127
128         /* Initialize KERNEL.THHOOK */
129         TASK_InstallTHHook(MapSL((SEGPTR)GetProcAddress16( hModule, (LPCSTR)332 )));
130
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 ))
136
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
148     }
149
150     /* Force loading of some dlls */
151     LoadLibrary16( "system" );
152
153     /* Create 16-bit task */
154     TASK_CreateMainTask();
155
156     /* Create the shared heap for broken win95 native dlls */
157     HeapCreate( HEAP_SHARED, 0, 0 );
158
159     /* initialize LDT locking */
160     wine_ldt_init_locking( ldt_lock, ldt_unlock );
161
162     /* finish the process initialisation for console bits, if needed */
163     __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
164
165     if (main_create_flags & CREATE_NEW_CONSOLE)
166     {
167         HMODULE mod = GetModuleHandleA(0);
168         if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
169             AllocConsole();
170     }
171     if (main_create_flags & CREATE_NEW_PROCESS_GROUP)
172         SetConsoleCtrlHandler(NULL, TRUE);
173
174     thread_attach();
175     return TRUE;
176 }
177
178 /***********************************************************************
179  *           KERNEL initialisation routine
180  */
181 BOOL WINAPI MAIN_KernelInit( HINSTANCE hinst, DWORD reason, LPVOID reserved )
182 {
183     switch(reason)
184     {
185     case DLL_PROCESS_ATTACH:
186         return process_attach();
187     case DLL_THREAD_ATTACH:
188         thread_attach();
189         break;
190     case DLL_THREAD_DETACH:
191         thread_detach();
192         break;
193     case DLL_PROCESS_DETACH:
194         WriteOutProfiles16();
195         break;
196     }
197     return TRUE;
198 }
199
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)
209  *              K237 (KERNEL.237)
210  *              BUNNY_351 (KERNEL.351)
211  *              PIGLET_361 (KERNEL.361)
212  *
213  * Entry point for kernel functions that do nothing.
214  */
215 LONG WINAPI KERNEL_nop(void)
216 {
217     return 0;
218 }
219
220 /***********************************************************************
221  *              SwitchToThread (KERNEL32.@)
222  */
223
224 BOOL WINAPI SwitchToThread(void)
225 {
226     Sleep(0);
227     return 1;
228 }