wined3d: Fix the nvrc implementation of WINED3DTOP_MULTIPLYADD and WINED3DTOP_LERP.
[wine] / dlls / kernel32 / kernel16.c
1 /*
2  * 16-bit 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 <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winternl.h"
26 #include "wownt32.h"
27
28 #include "toolhelp.h"
29 #include "kernel_private.h"
30 #include "kernel16_private.h"
31 #include "wine/server.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(module);
35
36 /**************************************************************************
37  *              DllEntryPoint   (KERNEL.669)
38  */
39 BOOL WINAPI KERNEL_DllEntryPoint( DWORD reasion, HINSTANCE16 inst, WORD ds,
40                                   WORD heap, DWORD reserved1, WORD reserved2 )
41 {
42     static int done;
43
44     /* the entry point can be called multiple times */
45     if (done) return TRUE;
46     done = 1;
47
48     /* Initialize 16-bit thunking entry points */
49     if (!WOWTHUNK_Init()) return FALSE;
50
51     /* Initialize DOS memory */
52     if (!DOSMEM_Init()) return FALSE;
53
54     /* Initialize special KERNEL entry points */
55
56     NE_SetEntryPoint( inst, 178, GetWinFlags16() );
57
58     NE_SetEntryPoint( inst, 454, wine_get_cs() );
59     NE_SetEntryPoint( inst, 455, wine_get_ds() );
60
61     NE_SetEntryPoint( inst, 183, DOSMEM_0000H );       /* KERNEL.183: __0000H */
62     NE_SetEntryPoint( inst, 173, DOSMEM_BiosSysSeg );  /* KERNEL.173: __ROMBIOS */
63     NE_SetEntryPoint( inst, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
64     NE_SetEntryPoint( inst, 194, DOSMEM_BiosSysSeg );  /* KERNEL.194: __F000H */
65
66     /* Initialize KERNEL.THHOOK */
67     TASK_InstallTHHook(MapSL((SEGPTR)GetProcAddress16( inst, (LPCSTR)332 )));
68
69     /* Initialize the real-mode selector entry points */
70 #define SET_ENTRY_POINT( num, addr ) \
71     NE_SetEntryPoint( inst, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
72                       DOSMEM_MapDosToLinear(addr), 0x10000, inst, \
73                       WINE_LDT_FLAGS_DATA ))
74
75     SET_ENTRY_POINT( 174, 0xa0000 );  /* KERNEL.174: __A000H */
76     SET_ENTRY_POINT( 181, 0xb0000 );  /* KERNEL.181: __B000H */
77     SET_ENTRY_POINT( 182, 0xb8000 );  /* KERNEL.182: __B800H */
78     SET_ENTRY_POINT( 195, 0xc0000 );  /* KERNEL.195: __C000H */
79     SET_ENTRY_POINT( 179, 0xd0000 );  /* KERNEL.179: __D000H */
80     SET_ENTRY_POINT( 190, 0xe0000 );  /* KERNEL.190: __E000H */
81 #undef SET_ENTRY_POINT
82
83     /* Force loading of some dlls */
84     LoadLibrary16( "system.drv" );
85
86     return TRUE;
87 }
88
89 /***********************************************************************
90  *              EnableDos (KERNEL.41)
91  *              DisableDos (KERNEL.42)
92  *              GetLastDiskChange (KERNEL.98)
93  *              ValidateCodeSegments (KERNEL.100)
94  *              KbdRst (KERNEL.123)
95  *              EnableKernel (KERNEL.124)
96  *              DisableKernel (KERNEL.125)
97  *              ValidateFreeSpaces (KERNEL.200)
98  *              K237 (KERNEL.237)
99  *              BUNNY_351 (KERNEL.351)
100  *              PIGLET_361 (KERNEL.361)
101  *
102  * Entry point for kernel functions that do nothing.
103  */
104 LONG WINAPI KERNEL_nop(void)
105 {
106     return 0;
107 }
108
109
110 /* thunk for 16-bit CreateThread */
111 struct thread_args
112 {
113     FARPROC16 proc;
114     DWORD     param;
115 };
116
117 static DWORD CALLBACK start_thread16( LPVOID threadArgs )
118 {
119     struct thread_args args = *(struct thread_args *)threadArgs;
120     HeapFree( GetProcessHeap(), 0, threadArgs );
121     return K32WOWCallback16( (DWORD)args.proc, args.param );
122 }
123
124 /***********************************************************************
125  *           CreateThread16   (KERNEL.441)
126  */
127 HANDLE WINAPI CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
128                               FARPROC16 start, SEGPTR param,
129                               DWORD flags, LPDWORD id )
130 {
131     struct thread_args *args = HeapAlloc( GetProcessHeap(), 0, sizeof(*args) );
132     if (!args) return INVALID_HANDLE_VALUE;
133     args->proc = start;
134     args->param = param;
135     return CreateThread( sa, stack, start_thread16, args, flags, id );
136 }
137
138
139 /***********************************************************************
140  *           wait_input_idle
141  *
142  * user32.WaitForInputIdle releases the win16 lock, so here is a replacement.
143  */
144 static DWORD wait_input_idle( HANDLE process, DWORD timeout )
145 {
146     DWORD ret;
147     HANDLE handles[2];
148
149     handles[0] = process;
150     SERVER_START_REQ( get_process_idle_event )
151     {
152         req->handle = process;
153         if (!(ret = wine_server_call_err( req ))) handles[1] = reply->event;
154     }
155     SERVER_END_REQ;
156     if (ret) return WAIT_FAILED;  /* error */
157     if (!handles[1]) return 0;  /* no event to wait on */
158
159     return WaitForMultipleObjects( 2, handles, FALSE, timeout );
160 }
161
162
163 /**************************************************************************
164  *           WINOLDAP entry point
165  */
166 void WINAPI WINOLDAP_EntryPoint( CONTEXT86 *context )
167 {
168     PDB16 *psp;
169     INT len;
170     LPSTR cmdline;
171     PROCESS_INFORMATION info;
172     STARTUPINFOA startup;
173     DWORD count, exit_code = 1;
174
175     InitTask16( context );
176
177     TRACE( "(ds=%x es=%x fs=%x gs=%x, bx=%04x cx=%04x di=%04x si=%x)\n",
178             context->SegDs, context->SegEs, context->SegFs, context->SegGs,
179             context->Ebx, context->Ecx, context->Edi, context->Esi );
180
181     psp = GlobalLock16( context->SegEs );
182     len = psp->cmdLine[0];
183     cmdline = HeapAlloc( GetProcessHeap(), 0, len + 1 );
184     memcpy( cmdline, psp->cmdLine + 1, len );
185     cmdline[len] = 0;
186
187     memset( &startup, 0, sizeof(startup) );
188     startup.cb = sizeof(startup);
189
190     if (CreateProcessA( NULL, cmdline, NULL, NULL, FALSE,
191                         0, NULL, NULL, &startup, &info ))
192     {
193         /* Give 10 seconds to the app to come up */
194         if (wait_input_idle( info.hProcess, 10000 ) == WAIT_FAILED)
195             WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
196         ReleaseThunkLock( &count );
197
198         WaitForSingleObject( info.hProcess, INFINITE );
199         GetExitCodeProcess( info.hProcess, &exit_code );
200         CloseHandle( info.hThread );
201         CloseHandle( info.hProcess );
202     }
203     else
204         ReleaseThunkLock( &count );
205
206     HeapFree( GetProcessHeap(), 0, cmdline );
207     ExitThread( exit_code );
208 }
209
210
211 /**************************************************************************
212  *           WINHELP entry point
213  *
214  * FIXME: should go into winhlp32.exe, but we don't support 16-bit modules in executables yet.
215  */
216 void WINAPI WINHELP_EntryPoint( CONTEXT86 *context )
217 {
218     static const WCHAR winhlp32W[] = {'\\','w','i','n','h','l','p','3','2','.','e','x','e',0};
219     PDB16 *psp;
220     INT len, total;
221     WCHAR *cmdline, *p;
222     PROCESS_INFORMATION info;
223     STARTUPINFOW startup;
224     DWORD count, exit_code = 1;
225
226     InitTask16( context );
227
228     TRACE( "(ds=%x es=%x fs=%x gs=%x, bx=%04x cx=%04x di=%04x si=%x)\n",
229             context->SegDs, context->SegEs, context->SegFs, context->SegGs,
230             context->Ebx, context->Ecx, context->Edi, context->Esi );
231
232     psp = GlobalLock16( context->SegEs );
233     len = MultiByteToWideChar( CP_ACP, 0, (char *)psp->cmdLine + 1, psp->cmdLine[0], NULL, 0 );
234     total = (GetSystemDirectoryW( NULL, 0 ) + len + 1) * sizeof(WCHAR) + sizeof(winhlp32W);
235     cmdline = HeapAlloc( GetProcessHeap(), 0, total );
236     GetSystemDirectoryW( cmdline, total );
237     lstrcatW( cmdline, winhlp32W );
238     p = cmdline + lstrlenW(cmdline);
239     if (len)
240     {
241         *p++ = ' ';
242         MultiByteToWideChar( CP_ACP, 0, (char *)psp->cmdLine + 1, psp->cmdLine[0], p, len );
243         p[len] = 0;
244     }
245
246     memset( &startup, 0, sizeof(startup) );
247     startup.cb = sizeof(startup);
248
249     if (CreateProcessW( NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info ))
250     {
251         /* Give 10 seconds to the app to come up */
252         if (wait_input_idle( info.hProcess, 10000 ) == WAIT_FAILED)
253             WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
254         ReleaseThunkLock( &count );
255
256         WaitForSingleObject( info.hProcess, INFINITE );
257         GetExitCodeProcess( info.hProcess, &exit_code );
258         CloseHandle( info.hThread );
259         CloseHandle( info.hProcess );
260     }
261     else
262         ReleaseThunkLock( &count );
263
264     HeapFree( GetProcessHeap(), 0, cmdline );
265     ExitThread( exit_code );
266 }