Release 1.5.29.
[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
35 #include "wine/library.h"
36 #include "kernel_private.h"
37 #include "console_private.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(process);
41
42 extern int CDECL __wine_set_signal_handler(unsigned, int (*)(unsigned));
43
44 /***********************************************************************
45  *           set_entry_point
46  */
47 static void set_entry_point( HMODULE module, const char *name, DWORD rva )
48 {
49     IMAGE_EXPORT_DIRECTORY *exports;
50     DWORD exp_size;
51
52     if ((exports = RtlImageDirectoryEntryToData( module, TRUE,
53                                                   IMAGE_DIRECTORY_ENTRY_EXPORT, &exp_size )))
54     {
55         DWORD *functions = (DWORD *)((char *)module + exports->AddressOfFunctions);
56         const WORD *ordinals = (const WORD *)((const char *)module + exports->AddressOfNameOrdinals);
57         const DWORD *names = (const DWORD *)((const char *)module +  exports->AddressOfNames);
58         int min = 0, max = exports->NumberOfNames - 1;
59
60         while (min <= max)
61         {
62             int res, pos = (min + max) / 2;
63             const char *ename = (const char *)module + names[pos];
64             if (!(res = strcmp( ename, name )))
65             {
66                 WORD ordinal = ordinals[pos];
67                 assert( ordinal < exports->NumberOfFunctions );
68                 TRACE( "setting %s at %p to %08x\n", name, &functions[ordinal], rva );
69                 functions[ordinal] = rva;
70                 return;
71             }
72             if (res > 0) max = pos - 1;
73             else min = pos + 1;
74         }
75     }
76 }
77
78
79 /***********************************************************************
80  *           KERNEL process initialisation routine
81  */
82 static BOOL process_attach( HMODULE module )
83 {
84     RTL_USER_PROCESS_PARAMETERS *params = NtCurrentTeb()->Peb->ProcessParameters;
85
86     NtQuerySystemInformation( SystemBasicInformation, &system_info, sizeof(system_info), NULL );
87
88     /* Setup registry locale information */
89     LOCALE_InitRegistry();
90
91     /* Setup computer name */
92     COMPUTERNAME_Init();
93
94     CONSOLE_Init(params);
95
96     /* copy process information from ntdll */
97     ENV_CopyStartupInformation();
98
99     if (!(GetVersion() & 0x80000000))
100     {
101         /* Securom checks for this one when version is NT */
102         set_entry_point( module, "FT_Thunk", 0 );
103     }
104     else LoadLibraryA( "krnl386.exe16" );
105
106     /* finish the process initialisation for console bits, if needed */
107     __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
108
109     if (params->ConsoleHandle == KERNEL32_CONSOLE_ALLOC)
110     {
111         HMODULE mod = GetModuleHandleA(0);
112         if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
113             AllocConsole();
114     }
115     /* else TODO for DETACHED_PROCESS:
116      * 1/ inherit console + handles
117      * 2/ create std handles, if handles are not inherited
118      * TBD when not using wineserver handles for console handles
119      */
120
121     return TRUE;
122 }
123
124 /***********************************************************************
125  *           KERNEL initialisation routine
126  */
127 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
128 {
129     switch(reason)
130     {
131     case DLL_PROCESS_ATTACH:
132         DisableThreadLibraryCalls( hinst );
133         return process_attach( hinst );
134     case DLL_PROCESS_DETACH:
135         WritePrivateProfileSectionW( NULL, NULL, NULL );
136         CONSOLE_Exit();
137         break;
138     }
139     return TRUE;
140 }
141
142 /***********************************************************************
143  *           MulDiv   (KERNEL32.@)
144  * RETURNS
145  *      Result of multiplication and division
146  *      -1: Overflow occurred or Divisor was 0
147  */
148 INT WINAPI MulDiv( INT nMultiplicand, INT nMultiplier, INT nDivisor)
149 {
150     LONGLONG ret;
151
152     if (!nDivisor) return -1;
153
154     /* We want to deal with a positive divisor to simplify the logic. */
155     if (nDivisor < 0)
156     {
157       nMultiplicand = - nMultiplicand;
158       nDivisor = -nDivisor;
159     }
160
161     /* If the result is positive, we "add" to round. else, we subtract to round. */
162     if ( ( (nMultiplicand <  0) && (nMultiplier <  0) ) ||
163          ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
164       ret = (((LONGLONG)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
165     else
166       ret = (((LONGLONG)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
167
168     if ((ret > 2147483647) || (ret < -2147483647)) return -1;
169     return ret;
170 }
171
172
173 /******************************************************************************
174  *           GetTickCount64       (KERNEL32.@)
175  */
176 ULONGLONG WINAPI GetTickCount64(void)
177 {
178     LARGE_INTEGER counter, frequency;
179
180     NtQueryPerformanceCounter( &counter, &frequency );
181     return counter.QuadPart * 1000 / frequency.QuadPart;
182 }
183
184
185 /***********************************************************************
186  *           GetTickCount       (KERNEL32.@)
187  *
188  * Get the number of milliseconds the system has been running.
189  *
190  * PARAMS
191  *  None.
192  *
193  * RETURNS
194  *  The current tick count.
195  *
196  * NOTES
197  *  The value returned will wrap around every 2^32 milliseconds.
198  */
199 DWORD WINAPI GetTickCount(void)
200 {
201     return GetTickCount64();
202 }
203
204 /******************************************************************************
205  *           GetSystemRegistryQuota       (KERNEL32.@)
206  */
207 BOOL WINAPI GetSystemRegistryQuota(PDWORD pdwQuotaAllowed, PDWORD pdwQuotaUsed)
208 {
209     FIXME("(%p, %p) faking reported quota values\n", pdwQuotaAllowed, pdwQuotaUsed);
210
211     if (pdwQuotaAllowed)
212         *pdwQuotaAllowed = 2 * 1000 * 1000 * 1000; /* 2 GB */
213
214     if (pdwQuotaUsed)
215         *pdwQuotaUsed = 100 * 1000 * 1000; /* 100 MB */
216
217     return TRUE;
218 }