kernel: Remove no longer needed includes.
[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 <signal.h>
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wincon.h"
33 #include "winternl.h"
34 #include "wownt32.h"
35
36 #include "wine/winbase16.h"
37 #include "wine/library.h"
38 #include "wincon.h"
39 #include "toolhelp.h"
40 #include "kernel_private.h"
41 #include "kernel16_private.h"
42 #include "console_private.h"
43
44 extern  int __wine_set_signal_handler(unsigned, int (*)(unsigned));
45
46 static CRITICAL_SECTION ldt_section;
47 static CRITICAL_SECTION_DEBUG critsect_debug =
48 {
49     0, 0, &ldt_section,
50     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
51       0, 0, { (DWORD_PTR)(__FILE__ ": ldt_section") }
52 };
53 static CRITICAL_SECTION ldt_section = { &critsect_debug, -1, 0, 0, 0, 0 };
54
55 /***********************************************************************
56  *           locking for LDT routines
57  */
58 static void ldt_lock(void)
59 {
60     RtlEnterCriticalSection( &ldt_section );
61 }
62
63 static void ldt_unlock(void)
64 {
65     RtlLeaveCriticalSection( &ldt_section );
66 }
67
68
69 /***********************************************************************
70  *           KERNEL thread initialisation routine
71  */
72 static void thread_attach(void)
73 {
74     /* allocate the 16-bit stack (FIXME: should be done lazily) */
75     HGLOBAL16 hstack = WOWGlobalAlloc16( GMEM_FIXED, 0x10000 );
76     kernel_get_thread_data()->stack_sel = GlobalHandleToSel16( hstack );
77     NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR( kernel_get_thread_data()->stack_sel,
78                                                         0x10000 - sizeof(STACK16FRAME) );
79 }
80
81
82 /***********************************************************************
83  *           KERNEL thread finalisation routine
84  */
85 static void thread_detach(void)
86 {
87     /* free the 16-bit stack */
88     WOWGlobalFree16( kernel_get_thread_data()->stack_sel );
89     NtCurrentTeb()->WOW32Reserved = 0;
90     if (NtCurrentTeb()->Tib.SubSystemTib) TASK_ExitTask();
91 }
92
93
94 /***********************************************************************
95  *           KERNEL process initialisation routine
96  */
97 static BOOL process_attach(void)
98 {
99     SYSTEM_INFO si;
100
101     /* FIXME: should probably be done in ntdll */
102     GetSystemInfo( &si );
103     NtCurrentTeb()->Peb->NumberOfProcessors = si.dwNumberOfProcessors;
104
105     /* Setup registry locale information */
106     LOCALE_InitRegistry();
107
108     /* Setup computer name */
109     COMPUTERNAME_Init();
110
111     /* copy process information from ntdll */
112     ENV_CopyStartupInformation();
113
114 #ifdef __i386__
115     if (GetVersion() & 0x80000000)
116     {
117         /* create the shared heap for broken win95 native dlls */
118         HeapCreate( HEAP_SHARED, 0, 0 );
119         /* setup emulation of protected instructions from 32-bit code */
120         RtlAddVectoredExceptionHandler( TRUE, INSTR_vectored_handler );
121     }
122 #endif
123
124     /* initialize LDT locking */
125     wine_ldt_init_locking( ldt_lock, ldt_unlock );
126
127     /* finish the process initialisation for console bits, if needed */
128     __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
129
130     if (NtCurrentTeb()->Peb->ProcessParameters->ConsoleHandle == (HANDLE)1)  /* FIXME */
131     {
132         HMODULE mod = GetModuleHandleA(0);
133         if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
134             AllocConsole();
135     }
136     /* else TODO for DETACHED_PROCESS:
137      * 1/ inherit console + handles
138      * 2/ create std handles, if handles are not inherited
139      * TBD when not using wineserver handles for console handles
140      */
141
142     /* Create 16-bit task */
143     LoadLibrary16( "krnl386.exe" );
144     thread_attach();
145     TASK_CreateMainTask();
146     return TRUE;
147 }
148
149 /***********************************************************************
150  *           KERNEL initialisation routine
151  */
152 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
153 {
154     switch(reason)
155     {
156     case DLL_PROCESS_ATTACH:
157         return process_attach();
158     case DLL_THREAD_ATTACH:
159         thread_attach();
160         break;
161     case DLL_THREAD_DETACH:
162         thread_detach();
163         break;
164     case DLL_PROCESS_DETACH:
165         WriteOutProfiles16();
166         break;
167     }
168     return TRUE;
169 }
170
171 /***********************************************************************
172  *           MulDiv   (KERNEL32.@)
173  * RETURNS
174  *      Result of multiplication and division
175  *      -1: Overflow occurred or Divisor was 0
176  */
177 INT WINAPI MulDiv( INT nMultiplicand, INT nMultiplier, INT nDivisor)
178 {
179     LONGLONG ret;
180
181     if (!nDivisor) return -1;
182
183     /* We want to deal with a positive divisor to simplify the logic. */
184     if (nDivisor < 0)
185     {
186       nMultiplicand = - nMultiplicand;
187       nDivisor = -nDivisor;
188     }
189
190     /* If the result is positive, we "add" to round. else, we subtract to round. */
191     if ( ( (nMultiplicand <  0) && (nMultiplier <  0) ) ||
192          ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
193       ret = (((LONGLONG)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
194     else
195       ret = (((LONGLONG)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
196
197     if ((ret > 2147483647) || (ret < -2147483647)) return -1;
198     return ret;
199 }
200
201
202 /***********************************************************************
203  *           GetTickCount       (KERNEL32.@)
204  *
205  * Get the number of milliseconds the system has been running.
206  *
207  * PARAMS
208  *  None.
209  *
210  * RETURNS
211  *  The current tick count.
212  *
213  * NOTES
214  *  -The value returned will wrap arounf every 2^32 milliseconds.
215  *  -Under Windows, tick 0 is the moment at which the system is rebooted.
216  *  Under Wine, tick 0 begins at the moment the wineserver process is started,
217  */
218 DWORD WINAPI GetTickCount(void)
219 {
220     return NtGetTickCount();
221 }