Release 960928
[wine/multimedia.git] / miscemu / dpmi.c
blob77a32854ad349392f9ed146154cff29767dcdaab
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 "stddebug.h"
18 #include "debug.h"
20 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
23 /* Structure for real-mode callbacks */
24 typedef struct
26 DWORD edi;
27 DWORD esi;
28 DWORD ebp;
29 DWORD reserved;
30 DWORD ebx;
31 DWORD edx;
32 DWORD ecx;
33 DWORD eax;
34 WORD flags;
35 WORD es;
36 WORD ds;
37 WORD fs;
38 WORD gs;
39 WORD ip;
40 WORD cs;
41 WORD sp;
42 WORD ss;
43 } REALMODECALL;
45 extern void do_mscdex( SIGCONTEXT *context );
47 /**********************************************************************
48 * INT_Int31Handler
50 * Handler for int 31h (DPMI).
52 void INT_Int31Handler( SIGCONTEXT *context )
54 DWORD dw;
55 BYTE *ptr;
57 RESET_CFLAG(context);
58 switch(AX_reg(context))
60 case 0x0000: /* Allocate LDT descriptors */
61 if (!(AX_reg(context) = AllocSelectorArray( CX_reg(context) )))
63 AX_reg(context) = 0x8011; /* descriptor unavailable */
64 SET_CFLAG(context);
66 break;
68 case 0x0001: /* Free LDT descriptor */
69 if (FreeSelector( BX_reg(context) ))
71 AX_reg(context) = 0x8022; /* invalid selector */
72 SET_CFLAG(context);
74 else
76 /* If a segment register contains the selector being freed, */
77 /* set it to zero. */
78 if (!((DS_reg(context)^BX_reg(context)) & ~3)) DS_reg(context) = 0;
79 if (!((ES_reg(context)^BX_reg(context)) & ~3)) ES_reg(context) = 0;
80 #ifdef FS_reg
81 if (!((FS_reg(context)^BX_reg(context)) & ~3)) FS_reg(context) = 0;
82 #endif
83 #ifdef GS_reg
84 if (!((GS_reg(context)^BX_reg(context)) & ~3)) GS_reg(context) = 0;
85 #endif
87 break;
89 case 0x0002: /* Real mode segment to descriptor */
91 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
92 switch(BX_reg(context))
94 case 0x0000: entryPoint = 183; break; /* __0000H */
95 case 0x0040: entryPoint = 193; break; /* __0040H */
96 case 0xa000: entryPoint = 174; break; /* __A000H */
97 case 0xb000: entryPoint = 181; break; /* __B000H */
98 case 0xb800: entryPoint = 182; break; /* __B800H */
99 case 0xc000: entryPoint = 195; break; /* __C000H */
100 case 0xd000: entryPoint = 179; break; /* __D000H */
101 case 0xe000: entryPoint = 190; break; /* __E000H */
102 case 0xf000: entryPoint = 194; break; /* __F000H */
103 default:
104 AX_reg(context) = DOSMEM_AllocSelector(BX_reg(context));
105 break;
107 if (entryPoint)
108 AX_reg(context) = LOWORD(MODULE_GetEntryPoint(
109 GetModuleHandle( "KERNEL" ),
110 entryPoint ));
112 break;
114 case 0x0003: /* Get next selector increment */
115 AX_reg(context) = __AHINCR;
116 break;
118 case 0x0004: /* Lock selector (not supported) */
119 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
120 break;
122 case 0x0005: /* Unlock selector (not supported) */
123 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
124 break;
126 case 0x0006: /* Get selector base address */
127 if (!(dw = GetSelectorBase( BX_reg(context) )))
129 AX_reg(context) = 0x8022; /* invalid selector */
130 SET_CFLAG(context);
132 else
134 CX_reg(context) = HIWORD(dw);
135 DX_reg(context) = LOWORD(dw);
137 break;
139 case 0x0007: /* Set selector base address */
140 SetSelectorBase( BX_reg(context),
141 MAKELONG( DX_reg(context), CX_reg(context) ) );
142 break;
144 case 0x0008: /* Set selector limit */
145 SetSelectorLimit( BX_reg(context),
146 MAKELONG( DX_reg(context), CX_reg(context) ) );
147 break;
149 case 0x0009: /* Set selector access rights */
150 SelectorAccessRights( BX_reg(context), 1, CX_reg(context) );
151 break;
153 case 0x000a: /* Allocate selector alias */
154 if (!(AX_reg(context) = AllocCStoDSAlias( BX_reg(context) )))
156 AX_reg(context) = 0x8011; /* descriptor unavailable */
157 SET_CFLAG(context);
159 break;
161 case 0x000b: /* Get descriptor */
163 ldt_entry entry;
164 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
165 /* FIXME: should use ES:EDI for 32-bit clients */
166 LDT_EntryToBytes( PTR_SEG_OFF_TO_LIN( ES_reg(context),
167 DI_reg(context) ), &entry );
169 break;
171 case 0x000c: /* Set descriptor */
173 ldt_entry entry;
174 LDT_BytesToEntry( PTR_SEG_OFF_TO_LIN( ES_reg(context),
175 DI_reg(context) ), &entry );
176 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
178 break;
180 case 0x000d: /* Allocate specific LDT descriptor */
181 AX_reg(context) = 0x8011; /* descriptor unavailable */
182 SET_CFLAG(context);
183 break;
184 case 0x0200: /* get real mode interrupt vector */
185 fprintf(stdnimp,
186 "int31: get realmode interupt vector(0x%02x) unimplemented.\n",
187 BL_reg(context)
189 SET_CFLAG(context);
190 break;
191 case 0x0201: /* set real mode interrupt vector */
192 fprintf(stdnimp,
193 "int31: set realmode interupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n",
194 BL_reg(context),CX_reg(context),DX_reg(context)
196 SET_CFLAG(context);
197 break;
198 case 0x0204: /* Get protected mode interrupt vector */
199 dw = (DWORD)INT_GetHandler( BL_reg(context) );
200 CX_reg(context) = HIWORD(dw);
201 DX_reg(context) = LOWORD(dw);
202 break;
204 case 0x0205: /* Set protected mode interrupt vector */
205 INT_SetHandler( BL_reg(context),
206 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( CX_reg(context),
207 DX_reg(context) ));
208 break;
210 case 0x0300: /* Simulate real mode interrupt
211 * Interrupt number is in BL, flags are in BH
212 * ES:DI points to real-mode call structure
213 * Currently we just print it out and return error.
215 RESET_CFLAG(context);
217 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
219 switch (BL_reg(context)) {
220 case 0x2f: /* int2f */
221 switch ((p->eax & 0xFF00)>>8) {
222 case 0x15:
223 /* MSCDEX hook */
224 AX_reg(context) = p->eax & 0xFFFF;
225 do_mscdex( context );
226 break;
227 default:
228 SET_CFLAG(context);
229 break;
231 break;
232 case 0x21: /* int21 */
233 switch ((p->eax & 0xFF00)>>8) {
234 case 0x65:
235 switch (p->eax & 0xFF) {
236 case 06:{/* get collate table */
237 extern DWORD DOSMEM_CollateTable;
238 char *table;
239 /* ES:DI is a REALMODE pointer to 5 byte dosmem
240 * we fill that with 0x6, realmode pointer to collateTB
242 table = DOSMEM_RealMode2Linear(MAKELONG(p->edi,p->es));
243 *(BYTE*)table = 0x06;
244 *(DWORD*)(table+1) = DOSMEM_CollateTable;
246 CX_reg(context) = 258;/*FIXME: size of table?*/
247 break;
249 default:
250 SET_CFLAG(context);
252 break;
253 case 0x44:
254 switch (p->eax & 0xFF) {
255 case 0x0D:{/* generic block device request */
256 BYTE *dataptr = DOSMEM_RealMode2Linear((p->ds)*0x1000+(p->edx & 0xFFFF));
257 int drive = DOS_GET_DRIVE(p->ebx&0xFF);
259 if ((p->ecx & 0xFF00) != 0x0800) {
260 SET_CFLAG(context);
261 break;
263 switch (p->ecx & 0xFF) {
264 case 0x66:{
266 char label[12],fsname[9],path[4];
267 DWORD serial;
269 strcpy(path,"x:\\");path[0]=drive+'A';
270 GetVolumeInformation32A(path,label,12,&serial,NULL,NULL,fsname,9);
271 *(WORD*)dataptr = 0;
272 memcpy(dataptr+2,&serial,4);
273 memcpy(dataptr+6,label ,11);
274 memcpy(dataptr+17,fsname,8);
275 break;
277 case 0x60: /* get device parameters */
278 /* used by defrag.exe of win95 */
279 memset(dataptr, 0, 0x26);
280 dataptr[0] = 0x04;
281 dataptr[6] = 0; /* media type */
282 if (drive > 1) {
283 dataptr[1] = 0x05; /* fixed disk */
284 setword(&dataptr[2], 0x01); /* non removable */
285 setword(&dataptr[4], 0x300); /* # of cylinders */
286 } else {
287 dataptr[1] = 0x07; /* block dev, floppy */
288 setword(&dataptr[2], 0x02); /* removable */
289 setword(&dataptr[4], 80); /* # of cylinders */
291 CreateBPB(drive, &dataptr[7]);
292 break;
293 default:
294 SET_CFLAG(context);
295 break;
298 break;
299 default:
300 SET_CFLAG(context);
301 break;
303 default:
304 SET_CFLAG(context);
305 break;
307 break;
308 default:
309 SET_CFLAG(context);
310 break;
312 if (EFL_reg(context)&1) {
313 fprintf(stdnimp,
314 "RealModeInt %02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
315 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x\n",
316 BL_reg(context), p->eax, p->ebx, p->ecx, p->edx,
317 p->esi, p->edi, p->es, p->ds
321 break;
322 case 0x0301: /* Call real mode procedure with far return */
324 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
325 fprintf(stdnimp,
326 "RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
327 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n",
328 p->eax, p->ebx, p->ecx, p->edx,
329 p->esi, p->edi, p->es, p->ds, p->cs, p->ip );
330 SET_CFLAG(context);
332 break;
334 case 0x0302: /* Call real mode procedure with interrupt return */
336 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
337 fprintf(stdnimp,
338 "RealModeCallIret: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
339 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n",
340 p->eax, p->ebx, p->ecx, p->edx,
341 p->esi, p->edi, p->es, p->ds, p->cs, p->ip );
342 SET_CFLAG(context);
344 break;
346 case 0x0303: /* Allocate Real Mode Callback Address */
348 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
349 fprintf(stdnimp,
350 "AllocRMCB: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
351 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n"
352 " Function to call: %04x:%04x\n",
353 p->eax, p->ebx, p->ecx, p->edx,
354 p->esi, p->edi, p->es, p->ds, p->cs, p->ip,
355 DS_reg(context),SI_reg(context)
357 SET_CFLAG(context);
359 break;
361 case 0x0400: /* Get DPMI version */
362 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
363 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
364 CL_reg(context) = runtime_cpu ();
365 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
366 break;
368 case 0x0500: /* Get free memory information */
369 ptr = (BYTE *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
370 *(DWORD *)ptr = 0x00ff0000; /* Largest block available */
371 memset( ptr + 4, 0xff, 0x2c ); /* No other information supported */
372 break;
374 case 0x0501: /* Allocate memory block */
375 if (!(ptr = (BYTE *)HeapAlloc( SystemHeap, 0,MAKELONG( CX_reg(context),
376 BX_reg(context) ))))
378 AX_reg(context) = 0x8012; /* linear memory not available */
379 SET_CFLAG(context);
381 else
383 BX_reg(context) = SI_reg(context) = HIWORD(ptr);
384 CX_reg(context) = DI_reg(context) = LOWORD(ptr);
386 break;
388 case 0x0502: /* Free memory block */
389 HeapFree( SystemHeap, 0,
390 (void *)MAKELONG( DI_reg(context), SI_reg(context) ) );
391 break;
393 case 0x0503: /* Resize memory block */
394 if (!(ptr = (BYTE *)HeapReAlloc( SystemHeap, 0,
395 (void *)MAKELONG(DI_reg(context),SI_reg(context)),
396 MAKELONG(CX_reg(context),BX_reg(context)))))
398 AX_reg(context) = 0x8012; /* linear memory not available */
399 SET_CFLAG(context);
401 else
403 BX_reg(context) = SI_reg(context) = HIWORD(ptr);
404 CX_reg(context) = DI_reg(context) = LOWORD(ptr);
406 break;
408 case 0x0600: /* Lock linear region */
409 break; /* Just ignore it */
411 case 0x0601: /* Unlock linear region */
412 break; /* Just ignore it */
414 case 0x0602: /* Unlock real-mode region */
415 break; /* Just ignore it */
417 case 0x0603: /* Lock real-mode region */
418 break; /* Just ignore it */
420 case 0x0604: /* Get page size */
421 BX_reg(context) = 0;
422 CX_reg(context) = 4096;
423 break;
425 case 0x0702: /* Mark page as demand-paging candidate */
426 break; /* Just ignore it */
428 case 0x0703: /* Discard page contents */
429 break; /* Just ignore it */
431 default:
432 INT_BARF( context, 0x31 );
433 AX_reg(context) = 0x8001; /* unsupported function */
434 SET_CFLAG(context);
435 break;