Release 980315
[wine/multimedia.git] / msdos / dpmi.c
blob91466c11e50910c603604f898aef1c900a4bc090
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 "debug.h"
20 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
22 void CreateBPB(int drive, BYTE *data); /* defined in int21.c */
25 /* Structure for real-mode callbacks */
26 typedef struct
28 DWORD edi;
29 DWORD esi;
30 DWORD ebp;
31 DWORD reserved;
32 DWORD ebx;
33 DWORD edx;
34 DWORD ecx;
35 DWORD eax;
36 WORD fl;
37 WORD es;
38 WORD ds;
39 WORD fs;
40 WORD gs;
41 WORD ip;
42 WORD cs;
43 WORD sp;
44 WORD ss;
45 } REALMODECALL;
49 /**********************************************************************
50 * INT_GetRealModeContext
52 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT *context )
54 EAX_reg(context) = call->eax;
55 EBX_reg(context) = call->ebx;
56 ECX_reg(context) = call->ecx;
57 EDX_reg(context) = call->edx;
58 ESI_reg(context) = call->esi;
59 EDI_reg(context) = call->edi;
60 EBP_reg(context) = call->ebp;
61 EFL_reg(context) = call->fl;
62 EIP_reg(context) = call->ip;
63 ESP_reg(context) = call->sp;
64 CS_reg(context) = call->cs;
65 DS_reg(context) = call->ds;
66 ES_reg(context) = call->es;
67 FS_reg(context) = call->fs;
68 GS_reg(context) = call->gs;
72 /**********************************************************************
73 * INT_SetRealModeContext
75 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT *context )
77 call->eax = EAX_reg(context);
78 call->ebx = EBX_reg(context);
79 call->ecx = ECX_reg(context);
80 call->edx = EDX_reg(context);
81 call->esi = ESI_reg(context);
82 call->edi = EDI_reg(context);
83 call->ebp = EBP_reg(context);
84 call->fl = FL_reg(context);
85 call->ip = IP_reg(context);
86 call->sp = SP_reg(context);
87 call->cs = CS_reg(context);
88 call->ds = DS_reg(context);
89 call->es = ES_reg(context);
90 call->fs = FS_reg(context);
91 call->gs = GS_reg(context);
95 /**********************************************************************
96 * INT_DoRealModeInt
98 static void INT_DoRealModeInt( CONTEXT *context )
100 CONTEXT realmode_ctx;
101 REALMODECALL *call = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context),
102 DI_reg(context) );
103 INT_GetRealModeContext( call, &realmode_ctx );
105 RESET_CFLAG(context);
106 switch (BL_reg(context))
108 case 0x2f: /* int2f */
109 switch (AH_reg(&realmode_ctx))
111 case 0x15:
112 /* MSCDEX hook */
113 do_mscdex( &realmode_ctx );
114 break;
115 default:
116 SET_CFLAG(context);
117 break;
119 break;
120 case 0x21: /* int21 */
121 switch (AH_reg(&realmode_ctx))
123 case 0x52:
124 ES_reg(&realmode_ctx) = 0;
125 EBX_reg(&realmode_ctx) = 0;
126 break;
127 case 0x65:
128 switch (AL_reg(&realmode_ctx))
130 case 0x06:
131 {/* get collate table */
132 /* ES:DI is a REALMODE pointer to 5 byte dosmem
133 * we fill that with 0x6, realmode pointer to collateTB
135 char *table = DOSMEM_MapRealToLinear(
136 MAKELONG(EDI_reg(&realmode_ctx),ES_reg(&realmode_ctx)));
137 *(BYTE*)table = 0x06;
138 *(DWORD*)(table+1) = DOSMEM_CollateTable;
139 CX_reg(&realmode_ctx) = 258;/*FIXME: size of table?*/
140 break;
142 default:
143 SET_CFLAG(context);
144 break;
146 break;
147 case 0x44:
148 switch (AL_reg(&realmode_ctx))
150 case 0x0D:
151 {/* generic block device request */
152 BYTE *dataptr = DOSMEM_MapRealToLinear(
153 MAKELONG(EDX_reg(&realmode_ctx),DS_reg(&realmode_ctx)));
154 int drive = DOS_GET_DRIVE(BL_reg(&realmode_ctx));
155 if (CH_reg(&realmode_ctx) != 0x08)
157 SET_CFLAG(context);
158 break;
160 switch (CL_reg(&realmode_ctx))
162 case 0x66:
164 char label[12],fsname[9],path[4];
165 DWORD serial;
167 strcpy(path,"x:\\");path[0]=drive+'A';
168 GetVolumeInformation32A(path,label,12,&serial,NULL,NULL,fsname,9);
169 *(WORD*)dataptr = 0;
170 memcpy(dataptr+2,&serial,4);
171 memcpy(dataptr+6,label ,11);
172 memcpy(dataptr+17,fsname,8);
173 break;
175 case 0x60: /* get device parameters */
176 /* used by defrag.exe of win95 */
177 memset(dataptr, 0, 0x26);
178 dataptr[0] = 0x04;
179 dataptr[6] = 0; /* media type */
180 if (drive > 1)
182 dataptr[1] = 0x05; /* fixed disk */
183 setword(&dataptr[2], 0x01); /* non removable */
184 setword(&dataptr[4], 0x300); /* # of cylinders */
186 else
188 dataptr[1] = 0x07; /* block dev, floppy */
189 setword(&dataptr[2], 0x02); /* removable */
190 setword(&dataptr[4], 80); /* # of cylinders */
192 CreateBPB(drive, &dataptr[7]);
193 break;
194 default:
195 SET_CFLAG(context);
196 break;
199 break;
200 default:
201 SET_CFLAG(context);
202 break;
204 break;
205 default:
206 SET_CFLAG(context);
207 break;
209 break;
210 default:
211 SET_CFLAG(context);
212 break;
215 if (EFL_reg(context)&1)
216 fprintf(stdnimp,
217 "RealModeInt %02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
218 " ESI=%08lx EDI=%08lx DS=%04lx ES=%04lx\n",
219 BL_reg(context), EAX_reg(&realmode_ctx),
220 EBX_reg(&realmode_ctx), ECX_reg(&realmode_ctx),
221 EDX_reg(&realmode_ctx), ESI_reg(&realmode_ctx),
222 EDI_reg(&realmode_ctx), DS_reg(&realmode_ctx),
223 ES_reg(&realmode_ctx) );
224 INT_SetRealModeContext( call, &realmode_ctx );
228 /**********************************************************************
229 * INT_Int31Handler
231 * Handler for int 31h (DPMI).
233 void WINAPI INT_Int31Handler( CONTEXT *context )
235 DWORD dw;
236 BYTE *ptr;
238 RESET_CFLAG(context);
239 switch(AX_reg(context))
241 case 0x0000: /* Allocate LDT descriptors */
242 if (!(AX_reg(context) = AllocSelectorArray( CX_reg(context) )))
244 AX_reg(context) = 0x8011; /* descriptor unavailable */
245 SET_CFLAG(context);
247 break;
249 case 0x0001: /* Free LDT descriptor */
250 if (FreeSelector( BX_reg(context) ))
252 AX_reg(context) = 0x8022; /* invalid selector */
253 SET_CFLAG(context);
255 else
257 /* If a segment register contains the selector being freed, */
258 /* set it to zero. */
259 if (!((DS_reg(context)^BX_reg(context)) & ~3)) DS_reg(context) = 0;
260 if (!((ES_reg(context)^BX_reg(context)) & ~3)) ES_reg(context) = 0;
261 if (!((FS_reg(context)^BX_reg(context)) & ~3)) FS_reg(context) = 0;
262 if (!((GS_reg(context)^BX_reg(context)) & ~3)) GS_reg(context) = 0;
264 break;
266 case 0x0002: /* Real mode segment to descriptor */
268 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
269 switch(BX_reg(context))
271 case 0x0000: entryPoint = 183; break; /* __0000H */
272 case 0x0040: entryPoint = 193; break; /* __0040H */
273 case 0xa000: entryPoint = 174; break; /* __A000H */
274 case 0xb000: entryPoint = 181; break; /* __B000H */
275 case 0xb800: entryPoint = 182; break; /* __B800H */
276 case 0xc000: entryPoint = 195; break; /* __C000H */
277 case 0xd000: entryPoint = 179; break; /* __D000H */
278 case 0xe000: entryPoint = 190; break; /* __E000H */
279 case 0xf000: entryPoint = 194; break; /* __F000H */
280 default:
281 AX_reg(context) = DOSMEM_AllocSelector(BX_reg(context));
282 break;
284 if (entryPoint)
285 AX_reg(context) = LOWORD(MODULE_GetEntryPoint(
286 GetModuleHandle16( "KERNEL" ),
287 entryPoint ));
289 break;
291 case 0x0003: /* Get next selector increment */
292 AX_reg(context) = __AHINCR;
293 break;
295 case 0x0004: /* Lock selector (not supported) */
296 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
297 break;
299 case 0x0005: /* Unlock selector (not supported) */
300 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
301 break;
303 case 0x0006: /* Get selector base address */
304 if (!(dw = GetSelectorBase( BX_reg(context) )))
306 AX_reg(context) = 0x8022; /* invalid selector */
307 SET_CFLAG(context);
309 else
311 CX_reg(context) = HIWORD(dw);
312 DX_reg(context) = LOWORD(dw);
314 break;
316 case 0x0007: /* Set selector base address */
317 SetSelectorBase( BX_reg(context),
318 MAKELONG( DX_reg(context), CX_reg(context) ) );
319 break;
321 case 0x0008: /* Set selector limit */
322 SetSelectorLimit( BX_reg(context),
323 MAKELONG( DX_reg(context), CX_reg(context) ) );
324 break;
326 case 0x0009: /* Set selector access rights */
327 SelectorAccessRights( BX_reg(context), 1, CX_reg(context) );
328 break;
330 case 0x000a: /* Allocate selector alias */
331 if (!(AX_reg(context) = AllocCStoDSAlias( BX_reg(context) )))
333 AX_reg(context) = 0x8011; /* descriptor unavailable */
334 SET_CFLAG(context);
336 break;
338 case 0x000b: /* Get descriptor */
340 ldt_entry entry;
341 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
342 /* FIXME: should use ES:EDI for 32-bit clients */
343 LDT_EntryToBytes( PTR_SEG_OFF_TO_LIN( ES_reg(context),
344 DI_reg(context) ), &entry );
346 break;
348 case 0x000c: /* Set descriptor */
350 ldt_entry entry;
351 LDT_BytesToEntry( PTR_SEG_OFF_TO_LIN( ES_reg(context),
352 DI_reg(context) ), &entry );
353 LDT_SetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
355 break;
357 case 0x000d: /* Allocate specific LDT descriptor */
358 AX_reg(context) = 0x8011; /* descriptor unavailable */
359 SET_CFLAG(context);
360 break;
361 case 0x0200: /* get real mode interrupt vector */
362 fprintf(stdnimp,
363 "int31: get realmode interupt vector(0x%02x) unimplemented.\n",
364 BL_reg(context)
366 SET_CFLAG(context);
367 break;
368 case 0x0201: /* set real mode interrupt vector */
369 fprintf(stdnimp,
370 "int31: set realmode interupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n",
371 BL_reg(context),CX_reg(context),DX_reg(context)
373 SET_CFLAG(context);
374 break;
375 case 0x0204: /* Get protected mode interrupt vector */
376 dw = (DWORD)INT_GetHandler( BL_reg(context) );
377 CX_reg(context) = HIWORD(dw);
378 DX_reg(context) = LOWORD(dw);
379 break;
381 case 0x0205: /* Set protected mode interrupt vector */
382 INT_SetHandler( BL_reg(context),
383 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( CX_reg(context),
384 DX_reg(context) ));
385 break;
387 case 0x0300: /* Simulate real mode interrupt */
388 INT_DoRealModeInt( context );
389 break;
391 case 0x0301: /* Call real mode procedure with far return */
393 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
394 fprintf(stdnimp,
395 "RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
396 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n",
397 p->eax, p->ebx, p->ecx, p->edx,
398 p->esi, p->edi, p->es, p->ds, p->cs, p->ip );
399 SET_CFLAG(context);
401 break;
403 case 0x0302: /* Call real mode procedure with interrupt return */
405 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
406 fprintf(stdnimp,
407 "RealModeCallIret: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
408 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n",
409 p->eax, p->ebx, p->ecx, p->edx,
410 p->esi, p->edi, p->es, p->ds, p->cs, p->ip );
411 SET_CFLAG(context);
413 break;
415 case 0x0303: /* Allocate Real Mode Callback Address */
417 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
418 fprintf(stdnimp,
419 "AllocRMCB: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
420 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n"
421 " Function to call: %04x:%04x\n",
422 p->eax, p->ebx, p->ecx, p->edx,
423 p->esi, p->edi, p->es, p->ds, p->cs, p->ip,
424 (WORD)DS_reg(context), SI_reg(context) );
425 SET_CFLAG(context);
427 break;
429 case 0x0304: /* Free Real Mode Callback Address */
431 fprintf(stdnimp,
432 "FreeRMCB: callback address: %04x:%04x\n",
433 CX_reg(context), DX_reg(context));
434 SET_CFLAG(context);
436 break;
438 case 0x0400: /* Get DPMI version */
440 SYSTEM_INFO si;
442 GetSystemInfo(&si);
443 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
444 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
445 CL_reg(context) = si.wProcessorLevel;
446 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
447 break;
449 case 0x0500: /* Get free memory information */
451 MEMMANINFO mmi;
453 mmi.dwSize = sizeof(mmi);
454 MemManInfo(&mmi);
455 ptr = (BYTE *)PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context));
456 /* the layout is just the same as MEMMANINFO, but without
457 * the dwSize entry.
459 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
460 break;
462 case 0x0501: /* Allocate memory block */
463 if (!(ptr = (BYTE *)VirtualAlloc(NULL, MAKELONG(CX_reg(context), BX_reg(context)), MEM_COMMIT, PAGE_EXECUTE_READWRITE)))
465 AX_reg(context) = 0x8012; /* linear memory not available */
466 SET_CFLAG(context);
468 else
470 BX_reg(context) = SI_reg(context) = HIWORD(ptr);
471 CX_reg(context) = DI_reg(context) = LOWORD(ptr);
473 break;
475 case 0x0502: /* Free memory block */
476 VirtualFree((void *)MAKELONG(DI_reg(context), SI_reg(context)), 0, MEM_RELEASE);
477 break;
479 case 0x0503: /* Resize memory block */
480 if (!(ptr = (BYTE *)HeapReAlloc( GetProcessHeap(), 0,
481 (void *)MAKELONG(DI_reg(context),SI_reg(context)),
482 MAKELONG(CX_reg(context),BX_reg(context)))))
484 AX_reg(context) = 0x8012; /* linear memory not available */
485 SET_CFLAG(context);
487 else
489 BX_reg(context) = SI_reg(context) = HIWORD(ptr);
490 CX_reg(context) = DI_reg(context) = LOWORD(ptr);
492 break;
494 case 0x0600: /* Lock linear region */
495 break; /* Just ignore it */
497 case 0x0601: /* Unlock linear region */
498 break; /* Just ignore it */
500 case 0x0602: /* Unlock real-mode region */
501 break; /* Just ignore it */
503 case 0x0603: /* Lock real-mode region */
504 break; /* Just ignore it */
506 case 0x0604: /* Get page size */
507 BX_reg(context) = 0;
508 CX_reg(context) = 4096;
509 break;
511 case 0x0702: /* Mark page as demand-paging candidate */
512 break; /* Just ignore it */
514 case 0x0703: /* Discard page contents */
515 break; /* Just ignore it */
517 case 0x0800: /* Physical address mapping */
518 if(!(ptr=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context),BX_reg(context)))))
520 AX_reg(context) = 0x8021;
521 SET_CFLAG(context);
523 else
525 BX_reg(context) = HIWORD(ptr);
526 CX_reg(context) = LOWORD(ptr);
527 RESET_CFLAG(context);
529 break;
531 default:
532 INT_BARF( context, 0x31 );
533 AX_reg(context) = 0x8001; /* unsupported function */
534 SET_CFLAG(context);
535 break;