Release 961215
[wine/multimedia.git] / miscemu / dpmi.c
blob1f8daa498c41c34ba35020e6e9636dabdd08853d
1 /*
2 * DPMI 0.9 emulation
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <string.h>
10 #include "windows.h"
11 #include "heap.h"
12 #include "ldt.h"
13 #include "module.h"
14 #include "miscemu.h"
15 #include "drive.h"
16 #include "msdos.h"
17 #include "toolhelp.h"
18 #include "stddebug.h"
19 #include "debug.h"
21 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
24 /* Structure for real-mode callbacks */
25 typedef struct
27 DWORD edi;
28 DWORD esi;
29 DWORD ebp;
30 DWORD reserved;
31 DWORD ebx;
32 DWORD edx;
33 DWORD ecx;
34 DWORD eax;
35 WORD flags;
36 WORD es;
37 WORD ds;
38 WORD fs;
39 WORD gs;
40 WORD ip;
41 WORD cs;
42 WORD sp;
43 WORD ss;
44 } REALMODECALL;
46 extern void do_mscdex( SIGCONTEXT *context );
48 /**********************************************************************
49 * INT_Int31Handler
51 * Handler for int 31h (DPMI).
53 void INT_Int31Handler( SIGCONTEXT *context )
55 DWORD dw;
56 BYTE *ptr;
58 RESET_CFLAG(context);
59 switch(AX_reg(context))
61 case 0x0000: /* Allocate LDT descriptors */
62 if (!(AX_reg(context) = AllocSelectorArray( CX_reg(context) )))
64 AX_reg(context) = 0x8011; /* descriptor unavailable */
65 SET_CFLAG(context);
67 break;
69 case 0x0001: /* Free LDT descriptor */
70 if (FreeSelector( BX_reg(context) ))
72 AX_reg(context) = 0x8022; /* invalid selector */
73 SET_CFLAG(context);
75 else
77 /* If a segment register contains the selector being freed, */
78 /* set it to zero. */
79 if (!((DS_reg(context)^BX_reg(context)) & ~3)) DS_reg(context) = 0;
80 if (!((ES_reg(context)^BX_reg(context)) & ~3)) ES_reg(context) = 0;
81 #ifdef FS_reg
82 if (!((FS_reg(context)^BX_reg(context)) & ~3)) FS_reg(context) = 0;
83 #endif
84 #ifdef GS_reg
85 if (!((GS_reg(context)^BX_reg(context)) & ~3)) GS_reg(context) = 0;
86 #endif
88 break;
90 case 0x0002: /* Real mode segment to descriptor */
92 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
93 switch(BX_reg(context))
95 case 0x0000: entryPoint = 183; break; /* __0000H */
96 case 0x0040: entryPoint = 193; break; /* __0040H */
97 case 0xa000: entryPoint = 174; break; /* __A000H */
98 case 0xb000: entryPoint = 181; break; /* __B000H */
99 case 0xb800: entryPoint = 182; break; /* __B800H */
100 case 0xc000: entryPoint = 195; break; /* __C000H */
101 case 0xd000: entryPoint = 179; break; /* __D000H */
102 case 0xe000: entryPoint = 190; break; /* __E000H */
103 case 0xf000: entryPoint = 194; break; /* __F000H */
104 default:
105 AX_reg(context) = DOSMEM_AllocSelector(BX_reg(context));
106 break;
108 if (entryPoint)
109 AX_reg(context) = LOWORD(MODULE_GetEntryPoint(
110 GetModuleHandle( "KERNEL" ),
111 entryPoint ));
113 break;
115 case 0x0003: /* Get next selector increment */
116 AX_reg(context) = __AHINCR;
117 break;
119 case 0x0004: /* Lock selector (not supported) */
120 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
121 break;
123 case 0x0005: /* Unlock selector (not supported) */
124 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
125 break;
127 case 0x0006: /* Get selector base address */
128 if (!(dw = GetSelectorBase( BX_reg(context) )))
130 AX_reg(context) = 0x8022; /* invalid selector */
131 SET_CFLAG(context);
133 else
135 CX_reg(context) = HIWORD(dw);
136 DX_reg(context) = LOWORD(dw);
138 break;
140 case 0x0007: /* Set selector base address */
141 SetSelectorBase( BX_reg(context),
142 MAKELONG( DX_reg(context), CX_reg(context) ) );
143 break;
145 case 0x0008: /* Set selector limit */
146 SetSelectorLimit( BX_reg(context),
147 MAKELONG( DX_reg(context), CX_reg(context) ) );
148 break;
150 case 0x0009: /* Set selector access rights */
151 SelectorAccessRights( BX_reg(context), 1, CX_reg(context) );
152 break;
154 case 0x000a: /* Allocate selector alias */
155 if (!(AX_reg(context) = AllocCStoDSAlias( BX_reg(context) )))
157 AX_reg(context) = 0x8011; /* descriptor unavailable */
158 SET_CFLAG(context);
160 break;
162 case 0x000b: /* Get descriptor */
164 ldt_entry entry;
165 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
166 /* FIXME: should use ES:EDI for 32-bit clients */
167 LDT_EntryToBytes( PTR_SEG_OFF_TO_LIN( ES_reg(context),
168 DI_reg(context) ), &entry );
170 break;
172 case 0x000c: /* Set descriptor */
174 ldt_entry entry;
175 LDT_BytesToEntry( PTR_SEG_OFF_TO_LIN( ES_reg(context),
176 DI_reg(context) ), &entry );
177 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
179 break;
181 case 0x000d: /* Allocate specific LDT descriptor */
182 AX_reg(context) = 0x8011; /* descriptor unavailable */
183 SET_CFLAG(context);
184 break;
185 case 0x0200: /* get real mode interrupt vector */
186 fprintf(stdnimp,
187 "int31: get realmode interupt vector(0x%02x) unimplemented.\n",
188 BL_reg(context)
190 SET_CFLAG(context);
191 break;
192 case 0x0201: /* set real mode interrupt vector */
193 fprintf(stdnimp,
194 "int31: set realmode interupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n",
195 BL_reg(context),CX_reg(context),DX_reg(context)
197 SET_CFLAG(context);
198 break;
199 case 0x0204: /* Get protected mode interrupt vector */
200 dw = (DWORD)INT_GetHandler( BL_reg(context) );
201 CX_reg(context) = HIWORD(dw);
202 DX_reg(context) = LOWORD(dw);
203 break;
205 case 0x0205: /* Set protected mode interrupt vector */
206 INT_SetHandler( BL_reg(context),
207 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( CX_reg(context),
208 DX_reg(context) ));
209 break;
211 case 0x0300: /* Simulate real mode interrupt
212 * Interrupt number is in BL, flags are in BH
213 * ES:DI points to real-mode call structure
214 * Currently we just print it out and return error.
216 RESET_CFLAG(context);
218 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
220 switch (BL_reg(context)) {
221 case 0x2f: /* int2f */
222 switch ((p->eax & 0xFF00)>>8) {
223 case 0x15:
224 /* MSCDEX hook */
225 AX_reg(context) = p->eax & 0xFFFF;
226 do_mscdex( context );
227 break;
228 default:
229 SET_CFLAG(context);
230 break;
232 break;
233 case 0x21: /* int21 */
234 switch ((p->eax & 0xFF00)>>8) {
235 case 0x65:
236 switch (p->eax & 0xFF) {
237 case 06:{/* get collate table */
238 extern DWORD DOSMEM_CollateTable;
239 char *table;
240 /* ES:DI is a REALMODE pointer to 5 byte dosmem
241 * we fill that with 0x6, realmode pointer to collateTB
243 table = DOSMEM_RealMode2Linear(MAKELONG(p->edi,p->es));
244 *(BYTE*)table = 0x06;
245 *(DWORD*)(table+1) = DOSMEM_CollateTable;
247 CX_reg(context) = 258;/*FIXME: size of table?*/
248 break;
250 default:
251 SET_CFLAG(context);
253 break;
254 case 0x44:
255 switch (p->eax & 0xFF) {
256 case 0x0D:{/* generic block device request */
257 BYTE *dataptr = DOSMEM_RealMode2Linear((p->ds)*0x1000+(p->edx & 0xFFFF));
258 int drive = DOS_GET_DRIVE(p->ebx&0xFF);
260 if ((p->ecx & 0xFF00) != 0x0800) {
261 SET_CFLAG(context);
262 break;
264 switch (p->ecx & 0xFF) {
265 case 0x66:{
267 char label[12],fsname[9],path[4];
268 DWORD serial;
270 strcpy(path,"x:\\");path[0]=drive+'A';
271 GetVolumeInformation32A(path,label,12,&serial,NULL,NULL,fsname,9);
272 *(WORD*)dataptr = 0;
273 memcpy(dataptr+2,&serial,4);
274 memcpy(dataptr+6,label ,11);
275 memcpy(dataptr+17,fsname,8);
276 break;
278 case 0x60: /* get device parameters */
279 /* used by defrag.exe of win95 */
280 memset(dataptr, 0, 0x26);
281 dataptr[0] = 0x04;
282 dataptr[6] = 0; /* media type */
283 if (drive > 1) {
284 dataptr[1] = 0x05; /* fixed disk */
285 setword(&dataptr[2], 0x01); /* non removable */
286 setword(&dataptr[4], 0x300); /* # of cylinders */
287 } else {
288 dataptr[1] = 0x07; /* block dev, floppy */
289 setword(&dataptr[2], 0x02); /* removable */
290 setword(&dataptr[4], 80); /* # of cylinders */
292 CreateBPB(drive, &dataptr[7]);
293 break;
294 default:
295 SET_CFLAG(context);
296 break;
299 break;
300 default:
301 SET_CFLAG(context);
302 break;
304 default:
305 SET_CFLAG(context);
306 break;
308 break;
309 default:
310 SET_CFLAG(context);
311 break;
313 if (EFL_reg(context)&1) {
314 fprintf(stdnimp,
315 "RealModeInt %02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
316 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x\n",
317 BL_reg(context), p->eax, p->ebx, p->ecx, p->edx,
318 p->esi, p->edi, p->es, p->ds
322 break;
323 case 0x0301: /* Call real mode procedure with far return */
325 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
326 fprintf(stdnimp,
327 "RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
328 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n",
329 p->eax, p->ebx, p->ecx, p->edx,
330 p->esi, p->edi, p->es, p->ds, p->cs, p->ip );
331 SET_CFLAG(context);
333 break;
335 case 0x0302: /* Call real mode procedure with interrupt return */
337 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
338 fprintf(stdnimp,
339 "RealModeCallIret: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
340 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n",
341 p->eax, p->ebx, p->ecx, p->edx,
342 p->esi, p->edi, p->es, p->ds, p->cs, p->ip );
343 SET_CFLAG(context);
345 break;
347 case 0x0303: /* Allocate Real Mode Callback Address */
349 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
350 fprintf(stdnimp,
351 "AllocRMCB: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
352 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n"
353 " Function to call: %04x:%04x\n",
354 p->eax, p->ebx, p->ecx, p->edx,
355 p->esi, p->edi, p->es, p->ds, p->cs, p->ip,
356 DS_reg(context),SI_reg(context)
358 SET_CFLAG(context);
360 break;
362 case 0x0400: /* Get DPMI version */
363 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
364 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
365 CL_reg(context) = runtime_cpu ();
366 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
367 break;
369 case 0x0500: /* Get free memory information */
371 MEMMANINFO mmi;
373 mmi.dwSize = sizeof(mmi);
374 MemManInfo(&mmi);
375 ptr = (BYTE *)PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context));
376 /* the layout is just the same as MEMMANINFO, but without
377 * the dwSize entry.
379 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
380 break;
382 case 0x0501: /* Allocate memory block */
383 if (!(ptr = (BYTE *)HeapAlloc( SystemHeap, 0,MAKELONG( CX_reg(context),
384 BX_reg(context) ))))
386 AX_reg(context) = 0x8012; /* linear memory not available */
387 SET_CFLAG(context);
389 else
391 BX_reg(context) = SI_reg(context) = HIWORD(ptr);
392 CX_reg(context) = DI_reg(context) = LOWORD(ptr);
394 break;
396 case 0x0502: /* Free memory block */
397 HeapFree( SystemHeap, 0,
398 (void *)MAKELONG( DI_reg(context), SI_reg(context) ) );
399 break;
401 case 0x0503: /* Resize memory block */
402 if (!(ptr = (BYTE *)HeapReAlloc( SystemHeap, 0,
403 (void *)MAKELONG(DI_reg(context),SI_reg(context)),
404 MAKELONG(CX_reg(context),BX_reg(context)))))
406 AX_reg(context) = 0x8012; /* linear memory not available */
407 SET_CFLAG(context);
409 else
411 BX_reg(context) = SI_reg(context) = HIWORD(ptr);
412 CX_reg(context) = DI_reg(context) = LOWORD(ptr);
414 break;
416 case 0x0600: /* Lock linear region */
417 break; /* Just ignore it */
419 case 0x0601: /* Unlock linear region */
420 break; /* Just ignore it */
422 case 0x0602: /* Unlock real-mode region */
423 break; /* Just ignore it */
425 case 0x0603: /* Lock real-mode region */
426 break; /* Just ignore it */
428 case 0x0604: /* Get page size */
429 BX_reg(context) = 0;
430 CX_reg(context) = 4096;
431 break;
433 case 0x0702: /* Mark page as demand-paging candidate */
434 break; /* Just ignore it */
436 case 0x0703: /* Discard page contents */
437 break; /* Just ignore it */
439 default:
440 INT_BARF( context, 0x31 );
441 AX_reg(context) = 0x8001; /* unsupported function */
442 SET_CFLAG(context);
443 break;