Added a generic mechanism to set up hooks for dispatching signal
[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
23 #include <assert.h>
24 #include <ctype.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <signal.h>
28
29 #include "winbase.h"
30
31 #include "wine/winbase16.h"
32 #include "wine/library.h"
33 #include "file.h"
34 #include "global.h"
35 #include "miscemu.h"
36 #include "module.h"
37 #include "task.h"
38
39 extern void CODEPAGE_Init(void);
40 extern BOOL RELAY_Init(void);
41
42 extern  int __wine_set_signal_handler(unsigned, int (*)(unsigned));
43 extern  int CONSOLE_HandleCtrlC(unsigned);
44
45 /***********************************************************************
46  *           KERNEL process initialisation routine
47  */
48 static BOOL process_attach(void)
49 {
50     HMODULE16 hModule;
51
52     /* Get the umask */
53     FILE_umask = umask(0777);
54     umask( FILE_umask );
55
56     /* Setup codepage info */
57     CODEPAGE_Init();
58
59     /* Initialize relay entry points */
60     if (!RELAY_Init()) return FALSE;
61
62     /* Initialize DOS memory */
63     if (!DOSMEM_Init(0)) return FALSE;
64
65     if ((hModule = LoadLibrary16( "krnl386.exe" )) < 32) return FALSE;
66
67     /* Initialize special KERNEL entry points */
68
69     /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
70     NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
71
72     /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
73     NE_SetEntryPoint( hModule, 454, wine_get_cs() );
74     NE_SetEntryPoint( hModule, 455, wine_get_ds() );
75
76     /* Initialize KERNEL.THHOOK */
77     TASK_InstallTHHook(MapSL((SEGPTR)GetProcAddress16( hModule, (LPCSTR)332 )));
78
79     /* Initialize the real-mode selector entry points */
80 #define SET_ENTRY_POINT( num, addr ) \
81     NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
82                       DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
83                       WINE_LDT_FLAGS_DATA ))
84
85     SET_ENTRY_POINT( 174, 0xa0000 );  /* KERNEL.174: __A000H */
86     SET_ENTRY_POINT( 181, 0xb0000 );  /* KERNEL.181: __B000H */
87     SET_ENTRY_POINT( 182, 0xb8000 );  /* KERNEL.182: __B800H */
88     SET_ENTRY_POINT( 195, 0xc0000 );  /* KERNEL.195: __C000H */
89     SET_ENTRY_POINT( 179, 0xd0000 );  /* KERNEL.179: __D000H */
90     SET_ENTRY_POINT( 190, 0xe0000 );  /* KERNEL.190: __E000H */
91     NE_SetEntryPoint( hModule, 183, DOSMEM_0000H );       /* KERNEL.183: __0000H */
92     NE_SetEntryPoint( hModule, 173, DOSMEM_BiosSysSeg );  /* KERNEL.173: __ROMBIOS */
93     NE_SetEntryPoint( hModule, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
94     NE_SetEntryPoint( hModule, 194, DOSMEM_BiosSysSeg );  /* KERNEL.194: __F000H */
95 #undef SET_ENTRY_POINT
96
97     /* Force loading of some dlls */
98     if (LoadLibrary16( "system" ) < 32) return FALSE;
99
100     /* Create 16-bit task */
101     TASK_CreateMainTask();
102
103     /* Create the shared heap for broken win95 native dlls */
104     HeapCreate( HEAP_SHARED, 0, 0 );
105
106     /* finish the process initialisation, if needed */
107     __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
108
109     return TRUE;
110 }
111
112 /***********************************************************************
113  *           KERNEL initialisation routine
114  */
115 BOOL WINAPI MAIN_KernelInit( HINSTANCE hinst, DWORD reason, LPVOID reserved )
116 {
117     switch(reason)
118     {
119     case DLL_PROCESS_ATTACH:
120         return process_attach();
121     case DLL_PROCESS_DETACH:
122         WriteOutProfiles16();
123         break;
124     }
125     return TRUE;
126 }
127
128 /***********************************************************************
129  *              EnableDos (KERNEL.41)
130  *              DisableDos (KERNEL.42)
131  *              GetLastDiskChange (KERNEL.98)
132  *              ValidateCodeSegments (KERNEL.100)
133  *              KbdRst (KERNEL.123)
134  *              EnableKernel (KERNEL.124)
135  *              DisableKernel (KERNEL.125)
136  *              ValidateFreeSpaces (KERNEL.200)
137  *              K237 (KERNEL.237)
138  *              BUNNY_351 (KERNEL.351)
139  *              PIGLET_361 (KERNEL.361)
140  *
141  * Entry point for kernel functions that do nothing.
142  */
143 LONG WINAPI KERNEL_nop(void)
144 {
145     return 0;
146 }