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