Fixed bug in SwitchStackTo.
[wine/multimedia.git] / if1632 / builtin.c
blobaf3996b4c6e34b502b76cebfce3c55f40fb05254
1 /*
2 * Built-in modules
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <ctype.h>
9 #include <string.h>
10 #include "windows.h"
11 #include "builtin32.h"
12 #include "gdi.h"
13 #include "global.h"
14 #include "heap.h"
15 #include "module.h"
16 #include "miscemu.h"
17 #include "neexe.h"
18 #include "stackframe.h"
19 #include "user.h"
20 #include "process.h"
21 #include "snoop.h"
22 #include "task.h"
23 #include "debug.h"
25 /* Built-in modules descriptors */
26 /* Don't change these structures! (see tools/build.c) */
28 typedef struct
30 const char *name; /* DLL name */
31 void *module_start; /* 32-bit address of the module data */
32 int module_size; /* Size of the module data */
33 const BYTE *code_start; /* 32-bit address of DLL code */
34 const BYTE *data_start; /* 32-bit address of DLL data */
35 } WIN16_DESCRIPTOR;
37 typedef struct
39 const WIN16_DESCRIPTOR *descr; /* DLL descriptor */
40 int flags; /* flags (see below) */
41 } BUILTIN16_DLL;
43 /* DLL flags */
44 #define DLL_FLAG_NOT_USED 0x01 /* Use original Windows DLL if possible */
45 #define DLL_FLAG_ALWAYS_USED 0x02 /* Always use built-in DLL */
47 /* 16-bit DLLs */
49 extern const WIN16_DESCRIPTOR AVIFILE_Descriptor;
50 extern const WIN16_DESCRIPTOR COMM_Descriptor;
51 extern const WIN16_DESCRIPTOR COMMDLG_Descriptor;
52 extern const WIN16_DESCRIPTOR COMPOBJ_Descriptor;
53 extern const WIN16_DESCRIPTOR DDEML_Descriptor;
54 extern const WIN16_DESCRIPTOR DISPDIB_Descriptor;
55 extern const WIN16_DESCRIPTOR DISPLAY_Descriptor;
56 extern const WIN16_DESCRIPTOR GDI_Descriptor;
57 extern const WIN16_DESCRIPTOR KERNEL_Descriptor;
58 extern const WIN16_DESCRIPTOR KEYBOARD_Descriptor;
59 extern const WIN16_DESCRIPTOR LZEXPAND_Descriptor;
60 extern const WIN16_DESCRIPTOR MMSYSTEM_Descriptor;
61 extern const WIN16_DESCRIPTOR MOUSE_Descriptor;
62 extern const WIN16_DESCRIPTOR MSACM_Descriptor;
63 extern const WIN16_DESCRIPTOR MSVIDEO_Descriptor;
64 extern const WIN16_DESCRIPTOR OLE2CONV_Descriptor;
65 extern const WIN16_DESCRIPTOR OLE2DISP_Descriptor;
66 extern const WIN16_DESCRIPTOR OLE2NLS_Descriptor;
67 extern const WIN16_DESCRIPTOR OLE2PROX_Descriptor;
68 extern const WIN16_DESCRIPTOR OLE2THK_Descriptor;
69 extern const WIN16_DESCRIPTOR OLE2_Descriptor;
70 extern const WIN16_DESCRIPTOR OLECLI_Descriptor;
71 extern const WIN16_DESCRIPTOR OLESVR_Descriptor;
72 extern const WIN16_DESCRIPTOR RASAPI16_Descriptor;
73 extern const WIN16_DESCRIPTOR SHELL_Descriptor;
74 extern const WIN16_DESCRIPTOR SOUND_Descriptor;
75 extern const WIN16_DESCRIPTOR STORAGE_Descriptor;
76 extern const WIN16_DESCRIPTOR STRESS_Descriptor;
77 extern const WIN16_DESCRIPTOR SYSTEM_Descriptor;
78 extern const WIN16_DESCRIPTOR TOOLHELP_Descriptor;
79 extern const WIN16_DESCRIPTOR TYPELIB_Descriptor;
80 extern const WIN16_DESCRIPTOR USER_Descriptor;
81 extern const WIN16_DESCRIPTOR VER_Descriptor;
82 extern const WIN16_DESCRIPTOR W32SYS_Descriptor;
83 extern const WIN16_DESCRIPTOR WIN32S16_Descriptor;
84 extern const WIN16_DESCRIPTOR WIN87EM_Descriptor;
85 extern const WIN16_DESCRIPTOR WINASPI_Descriptor;
86 extern const WIN16_DESCRIPTOR WINDEBUG_Descriptor;
87 extern const WIN16_DESCRIPTOR WINEPS_Descriptor;
88 extern const WIN16_DESCRIPTOR WING_Descriptor;
89 extern const WIN16_DESCRIPTOR WINSOCK_Descriptor;
90 extern const WIN16_DESCRIPTOR WPROCS_Descriptor;
92 /* Table of all built-in DLLs */
94 static BUILTIN16_DLL BuiltinDLLs[] =
96 { &KERNEL_Descriptor, DLL_FLAG_ALWAYS_USED },
97 { &USER_Descriptor, DLL_FLAG_ALWAYS_USED },
98 { &GDI_Descriptor, DLL_FLAG_ALWAYS_USED },
99 { &SYSTEM_Descriptor, DLL_FLAG_ALWAYS_USED },
100 { &DISPLAY_Descriptor, DLL_FLAG_ALWAYS_USED },
101 { &WPROCS_Descriptor, DLL_FLAG_ALWAYS_USED },
102 { &WINDEBUG_Descriptor, DLL_FLAG_NOT_USED },
103 { &AVIFILE_Descriptor, DLL_FLAG_NOT_USED },
104 { &COMMDLG_Descriptor, DLL_FLAG_NOT_USED },
105 { &COMPOBJ_Descriptor, DLL_FLAG_NOT_USED },
106 { &DDEML_Descriptor, DLL_FLAG_NOT_USED },
107 { &DISPDIB_Descriptor, 0 },
108 { &KEYBOARD_Descriptor, 0 },
109 { &COMM_Descriptor, 0 },
110 { &LZEXPAND_Descriptor, 0 },
111 { &MMSYSTEM_Descriptor, 0 },
112 { &MOUSE_Descriptor, 0 },
113 { &MSACM_Descriptor, 0 },
114 { &MSVIDEO_Descriptor, 0 },
115 { &OLE2CONV_Descriptor, DLL_FLAG_NOT_USED },
116 { &OLE2DISP_Descriptor, DLL_FLAG_NOT_USED },
117 { &OLE2NLS_Descriptor, DLL_FLAG_NOT_USED },
118 { &OLE2PROX_Descriptor, DLL_FLAG_NOT_USED },
119 { &OLE2THK_Descriptor, DLL_FLAG_NOT_USED },
120 { &OLE2_Descriptor, DLL_FLAG_NOT_USED },
121 { &OLECLI_Descriptor, DLL_FLAG_NOT_USED },
122 { &OLESVR_Descriptor, DLL_FLAG_NOT_USED },
123 { &RASAPI16_Descriptor, 0 },
124 { &SHELL_Descriptor, 0 },
125 { &SOUND_Descriptor, 0 },
126 { &STORAGE_Descriptor, DLL_FLAG_NOT_USED },
127 { &STRESS_Descriptor, 0 },
128 { &TOOLHELP_Descriptor, 0 },
129 { &TYPELIB_Descriptor, DLL_FLAG_NOT_USED },
130 { &VER_Descriptor, 0 },
131 { &W32SYS_Descriptor, DLL_FLAG_NOT_USED },
132 { &WIN32S16_Descriptor, DLL_FLAG_NOT_USED },
133 { &WIN87EM_Descriptor, DLL_FLAG_NOT_USED },
134 { &WINASPI_Descriptor, 0 },
135 { &WINEPS_Descriptor, DLL_FLAG_ALWAYS_USED },
136 { &WING_Descriptor, 0 },
137 { &WINSOCK_Descriptor, 0 },
138 /* Last entry */
139 { NULL, 0 }
142 /* Ordinal number for interrupt 0 handler in WPROCS.DLL */
143 #define FIRST_INTERRUPT_ORDINAL 100
146 /***********************************************************************
147 * BUILTIN_DoLoadModule16
149 * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule
150 * and BUILTIN_Init.
152 static HMODULE16 BUILTIN_DoLoadModule16( const WIN16_DESCRIPTOR *descr )
154 NE_MODULE *pModule;
155 int minsize;
156 SEGTABLEENTRY *pSegTable;
158 HMODULE16 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
159 descr->module_size, 0,
160 FALSE, FALSE, FALSE, NULL );
161 if (!hModule) return 0;
162 FarSetOwner( hModule, hModule );
164 TRACE(module, "Built-in %s: hmodule=%04x\n",
165 descr->name, hModule );
166 pModule = (NE_MODULE *)GlobalLock16( hModule );
167 pModule->self = hModule;
169 /* Allocate the code segment */
171 pSegTable = NE_SEG_TABLE( pModule );
172 pSegTable->hSeg = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
173 pSegTable->minsize, hModule,
174 TRUE, TRUE, FALSE, NULL );
175 if (!pSegTable->hSeg) return 0;
176 pSegTable++;
178 /* Allocate the data segment */
180 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
181 minsize += pModule->heap_size;
182 if (minsize > 0x10000) minsize = 0x10000;
183 pSegTable->hSeg = GLOBAL_Alloc( GMEM_FIXED, minsize,
184 hModule, FALSE, FALSE, FALSE );
185 if (!pSegTable->hSeg) return 0;
186 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->hSeg ),
187 descr->data_start, pSegTable->minsize);
188 if (pModule->heap_size)
189 LocalInit( GlobalHandleToSel(pSegTable->hSeg),
190 pSegTable->minsize, minsize );
192 NE_RegisterModule( pModule );
193 return hModule;
197 /***********************************************************************
198 * BUILTIN_Init
200 * Load all built-in modules marked as 'always used'.
202 BOOL32 BUILTIN_Init(void)
204 BUILTIN16_DLL *dll;
205 NE_MODULE *pModule;
206 WORD vector;
207 HMODULE16 hModule;
208 WORD cs, ds;
210 fnBUILTIN_LoadModule = BUILTIN_LoadModule;
212 for (dll = BuiltinDLLs; dll->descr; dll++)
214 if (dll->flags & DLL_FLAG_ALWAYS_USED)
215 if (!BUILTIN_DoLoadModule16( dll->descr )) return FALSE;
218 /* Set the USER and GDI heap selectors */
220 pModule = NE_GetPtr( GetModuleHandle16( "USER" ));
221 USER_HeapSel = pModule ? GlobalHandleToSel((NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->hSeg) : 0;
222 pModule = NE_GetPtr( GetModuleHandle16( "GDI" ));
223 GDI_HeapSel = pModule ? GlobalHandleToSel((NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->hSeg) : 0;
225 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
227 hModule = GetModuleHandle16( "KERNEL" );
228 NE_SetEntryPoint( hModule, 178, GetWinFlags() );
230 /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
232 GET_CS(cs); GET_DS(ds);
233 NE_SetEntryPoint( hModule, 454, cs );
234 NE_SetEntryPoint( hModule, 455, ds );
236 /* Initialize KERNEL.THHOOK */
238 TASK_InstallTHHook((THHOOK *)PTR_SEG_TO_LIN(
239 (SEGPTR)NE_GetEntryPoint( hModule, 332 )));
241 /* Initialize the real-mode selector entry points */
243 #define SET_ENTRY_POINT( num, addr ) \
244 NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
245 DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
246 FALSE, FALSE, FALSE, NULL ))
248 SET_ENTRY_POINT( 183, 0x00000 ); /* KERNEL.183: __0000H */
249 SET_ENTRY_POINT( 174, 0xa0000 ); /* KERNEL.174: __A000H */
250 SET_ENTRY_POINT( 181, 0xb0000 ); /* KERNEL.181: __B000H */
251 SET_ENTRY_POINT( 182, 0xb8000 ); /* KERNEL.182: __B800H */
252 SET_ENTRY_POINT( 195, 0xc0000 ); /* KERNEL.195: __C000H */
253 SET_ENTRY_POINT( 179, 0xd0000 ); /* KERNEL.179: __D000H */
254 SET_ENTRY_POINT( 190, 0xe0000 ); /* KERNEL.190: __E000H */
255 SET_ENTRY_POINT( 173, 0xf0000 ); /* KERNEL.173: __ROMBIOS */
256 SET_ENTRY_POINT( 194, 0xf0000 ); /* KERNEL.194: __F000H */
257 NE_SetEntryPoint( hModule, 193, DOSMEM_BiosSeg ); /* KERNEL.193: __0040H */
258 #undef SET_ENTRY_POINT
260 /* Set interrupt vectors from entry points in WPROCS.DLL */
262 hModule = GetModuleHandle16( "WPROCS" );
263 for (vector = 0; vector < 256; vector++)
265 FARPROC16 proc = NE_GetEntryPoint( hModule,
266 FIRST_INTERRUPT_ORDINAL + vector );
267 assert(proc);
268 INT_SetPMHandler( vector, proc );
271 SNOOP16_Init();
273 return TRUE;
277 /***********************************************************************
278 * BUILTIN_LoadModule
280 * Load a built-in module. If the 'force' parameter is FALSE, we only
281 * load the module if it has not been disabled via the -dll option.
283 HMODULE16 BUILTIN_LoadModule( LPCSTR name, BOOL32 force )
285 BUILTIN16_DLL *table;
286 char dllname[16], *p;
288 /* Fix the name in case we have a full path and extension */
290 if ((p = strrchr( name, '\\' ))) name = p + 1;
291 lstrcpyn32A( dllname, name, sizeof(dllname) );
292 if ((p = strrchr( dllname, '.' ))) *p = '\0';
294 for (table = BuiltinDLLs; table->descr; table++)
295 if (!lstrcmpi32A( table->descr->name, dllname )) break;
296 if (!table->descr) return 0;
297 if ((table->flags & DLL_FLAG_NOT_USED) && !force) return 0;
299 return BUILTIN_DoLoadModule16( table->descr );
303 /***********************************************************************
304 * BUILTIN_GetEntryPoint16
306 * Return the ordinal and name corresponding to a CS:IP address.
307 * This is used only by relay debugging.
309 LPCSTR BUILTIN_GetEntryPoint16( WORD cs, WORD ip, WORD *pOrd )
311 static char buffer[80];
312 WORD ordinal, i, max_offset;
313 register BYTE *p;
314 NE_MODULE *pModule;
316 if (!(pModule = NE_GetPtr( FarGetOwner( GlobalHandle16(cs) ))))
317 return NULL;
319 /* Search for the ordinal */
321 p = (BYTE *)pModule + pModule->entry_table;
322 max_offset = 0;
323 ordinal = 1;
324 *pOrd = 0;
325 while (*p)
327 switch(p[1])
329 case 0: /* unused */
330 ordinal += *p;
331 p += 2;
332 break;
333 case 1: /* code segment */
334 i = *p;
335 p += 2;
336 while (i-- > 0)
338 p++;
339 if ((*(WORD *)p <= ip) && (*(WORD *)p >= max_offset))
341 max_offset = *(WORD *)p;
342 *pOrd = ordinal;
344 p += 2;
345 ordinal++;
347 break;
348 case 0xff: /* moveable (should not happen in built-in modules) */
349 TRACE( relay, "Built-in module has moveable entry\n" );
350 ordinal += *p;
351 p += 2 + *p * 6;
352 break;
353 default: /* other segment */
354 ordinal += *p;
355 p += 2 + *p * 3;
356 break;
360 /* Search for the name in the resident names table */
361 /* (built-in modules have no non-resident table) */
363 p = (BYTE *)pModule + pModule->name_table;
364 while (*p)
366 p += *p + 1 + sizeof(WORD);
367 if (*(WORD *)(p + *p + 1) == *pOrd) break;
370 sprintf( buffer, "%.*s.%d: %.*s",
371 *((BYTE *)pModule + pModule->name_table),
372 (char *)pModule + pModule->name_table + 1,
373 *pOrd, *p, (char *)(p + 1) );
374 return buffer;
378 /**********************************************************************
379 * BUILTIN_DefaultIntHandler
381 * Default interrupt handler.
383 void BUILTIN_DefaultIntHandler( CONTEXT *context )
385 WORD ordinal;
386 STACK16FRAME *frame = CURRENT_STACK16;
387 BUILTIN_GetEntryPoint16( frame->entry_cs, frame->entry_ip, &ordinal );
388 INT_BARF( context, ordinal - FIRST_INTERRUPT_ORDINAL );
392 /***********************************************************************
393 * BUILTIN_ParseDLLOptions
395 * Set runtime DLL usage flags
397 BOOL32 BUILTIN_ParseDLLOptions( const char *str )
399 BUILTIN16_DLL *dll;
400 const char *p;
402 while (*str)
404 while (*str && isspace(*str)) str++;
405 if (!*str) return TRUE;
406 if ((*str != '+') && (*str != '-')) return FALSE;
407 str++;
408 if (!(p = strchr( str, ',' ))) p = str + strlen(str);
409 while ((p > str) && isspace(p[-1])) p--;
410 if (p == str) return FALSE;
411 for (dll = BuiltinDLLs; dll->descr; dll++)
413 if (!lstrncmpi32A( str, dll->descr->name, (int)(p - str) ))
415 if (dll->descr->name[(int)(p-str)]) /* only partial match */
416 continue;
417 if (str[-1] == '-')
419 if (dll->flags & DLL_FLAG_ALWAYS_USED) return FALSE;
420 dll->flags |= DLL_FLAG_NOT_USED;
422 else dll->flags &= ~DLL_FLAG_NOT_USED;
423 break;
426 if (!dll->descr)
427 if (!BUILTIN32_EnableDLL( str, (int)(p - str), (str[-1] == '+') ))
428 return FALSE;
429 str = p;
430 while (*str && (isspace(*str) || (*str == ','))) str++;
432 return TRUE;
436 /***********************************************************************
437 * BUILTIN_PrintDLLs
439 * Print the list of built-in DLLs that can be disabled.
441 void BUILTIN_PrintDLLs(void)
443 int i;
444 BUILTIN16_DLL *dll;
446 MSG("Example: -dll -ole2 Do not use emulated OLE2.DLL\n");
447 MSG("Available Win16 DLLs:\n");
448 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
450 if (!(dll->flags & DLL_FLAG_ALWAYS_USED))
451 MSG("%-9s%c", dll->descr->name,
452 ((++i) % 8) ? ' ' : '\n' );
454 MSG("\n");
455 BUILTIN32_PrintDLLs();