Release 970824
[wine/multimedia.git] / msdos / dpmi.c
blobae7a85297e1390f99e971842f477c75573ce6dde
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())
23 void CreateBPB(int drive, BYTE *data); /* defined in int21.c */
26 /* Structure for real-mode callbacks */
27 typedef struct
29 DWORD edi;
30 DWORD esi;
31 DWORD ebp;
32 DWORD reserved;
33 DWORD ebx;
34 DWORD edx;
35 DWORD ecx;
36 DWORD eax;
37 WORD flags;
38 WORD es;
39 WORD ds;
40 WORD fs;
41 WORD gs;
42 WORD ip;
43 WORD cs;
44 WORD sp;
45 WORD ss;
46 } REALMODECALL;
48 extern void do_mscdex( CONTEXT *context );
50 /**********************************************************************
51 * INT_Int31Handler
53 * Handler for int 31h (DPMI).
55 void WINAPI INT_Int31Handler( CONTEXT *context )
57 DWORD dw;
58 BYTE *ptr;
60 RESET_CFLAG(context);
61 switch(AX_reg(context))
63 case 0x0000: /* Allocate LDT descriptors */
64 if (!(AX_reg(context) = AllocSelectorArray( CX_reg(context) )))
66 AX_reg(context) = 0x8011; /* descriptor unavailable */
67 SET_CFLAG(context);
69 break;
71 case 0x0001: /* Free LDT descriptor */
72 if (FreeSelector( BX_reg(context) ))
74 AX_reg(context) = 0x8022; /* invalid selector */
75 SET_CFLAG(context);
77 else
79 /* If a segment register contains the selector being freed, */
80 /* set it to zero. */
81 if (!((DS_reg(context)^BX_reg(context)) & ~3)) DS_reg(context) = 0;
82 if (!((ES_reg(context)^BX_reg(context)) & ~3)) ES_reg(context) = 0;
83 if (!((FS_reg(context)^BX_reg(context)) & ~3)) FS_reg(context) = 0;
84 if (!((GS_reg(context)^BX_reg(context)) & ~3)) GS_reg(context) = 0;
86 break;
88 case 0x0002: /* Real mode segment to descriptor */
90 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
91 switch(BX_reg(context))
93 case 0x0000: entryPoint = 183; break; /* __0000H */
94 case 0x0040: entryPoint = 193; break; /* __0040H */
95 case 0xa000: entryPoint = 174; break; /* __A000H */
96 case 0xb000: entryPoint = 181; break; /* __B000H */
97 case 0xb800: entryPoint = 182; break; /* __B800H */
98 case 0xc000: entryPoint = 195; break; /* __C000H */
99 case 0xd000: entryPoint = 179; break; /* __D000H */
100 case 0xe000: entryPoint = 190; break; /* __E000H */
101 case 0xf000: entryPoint = 194; break; /* __F000H */
102 default:
103 AX_reg(context) = DOSMEM_AllocSelector(BX_reg(context));
104 break;
106 if (entryPoint)
107 AX_reg(context) = LOWORD(MODULE_GetEntryPoint(
108 GetModuleHandle16( "KERNEL" ),
109 entryPoint ));
111 break;
113 case 0x0003: /* Get next selector increment */
114 AX_reg(context) = __AHINCR;
115 break;
117 case 0x0004: /* Lock selector (not supported) */
118 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
119 break;
121 case 0x0005: /* Unlock selector (not supported) */
122 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
123 break;
125 case 0x0006: /* Get selector base address */
126 if (!(dw = GetSelectorBase( BX_reg(context) )))
128 AX_reg(context) = 0x8022; /* invalid selector */
129 SET_CFLAG(context);
131 else
133 CX_reg(context) = HIWORD(dw);
134 DX_reg(context) = LOWORD(dw);
136 break;
138 case 0x0007: /* Set selector base address */
139 SetSelectorBase( BX_reg(context),
140 MAKELONG( DX_reg(context), CX_reg(context) ) );
141 break;
143 case 0x0008: /* Set selector limit */
144 SetSelectorLimit( BX_reg(context),
145 MAKELONG( DX_reg(context), CX_reg(context) ) );
146 break;
148 case 0x0009: /* Set selector access rights */
149 SelectorAccessRights( BX_reg(context), 1, CX_reg(context) );
150 break;
152 case 0x000a: /* Allocate selector alias */
153 if (!(AX_reg(context) = AllocCStoDSAlias( BX_reg(context) )))
155 AX_reg(context) = 0x8011; /* descriptor unavailable */
156 SET_CFLAG(context);
158 break;
160 case 0x000b: /* Get descriptor */
162 ldt_entry entry;
163 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
164 /* FIXME: should use ES:EDI for 32-bit clients */
165 LDT_EntryToBytes( PTR_SEG_OFF_TO_LIN( ES_reg(context),
166 DI_reg(context) ), &entry );
168 break;
170 case 0x000c: /* Set descriptor */
172 ldt_entry entry;
173 LDT_BytesToEntry( PTR_SEG_OFF_TO_LIN( ES_reg(context),
174 DI_reg(context) ), &entry );
175 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
177 break;
179 case 0x000d: /* Allocate specific LDT descriptor */
180 AX_reg(context) = 0x8011; /* descriptor unavailable */
181 SET_CFLAG(context);
182 break;
183 case 0x0200: /* get real mode interrupt vector */
184 fprintf(stdnimp,
185 "int31: get realmode interupt vector(0x%02x) unimplemented.\n",
186 BL_reg(context)
188 SET_CFLAG(context);
189 break;
190 case 0x0201: /* set real mode interrupt vector */
191 fprintf(stdnimp,
192 "int31: set realmode interupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n",
193 BL_reg(context),CX_reg(context),DX_reg(context)
195 SET_CFLAG(context);
196 break;
197 case 0x0204: /* Get protected mode interrupt vector */
198 dw = (DWORD)INT_GetHandler( BL_reg(context) );
199 CX_reg(context) = HIWORD(dw);
200 DX_reg(context) = LOWORD(dw);
201 break;
203 case 0x0205: /* Set protected mode interrupt vector */
204 INT_SetHandler( BL_reg(context),
205 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( CX_reg(context),
206 DX_reg(context) ));
207 break;
209 case 0x0300: /* Simulate real mode interrupt
210 * Interrupt number is in BL, flags are in BH
211 * ES:DI points to real-mode call structure
212 * Currently we just print it out and return error.
214 RESET_CFLAG(context);
216 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
218 switch (BL_reg(context)) {
219 case 0x2f: /* int2f */
220 switch ((p->eax & 0xFF00)>>8) {
221 case 0x15:
222 /* MSCDEX hook */
223 AX_reg(context) = p->eax & 0xFFFF;
224 do_mscdex( context );
225 break;
226 default:
227 SET_CFLAG(context);
228 break;
230 break;
231 case 0x21: /* int21 */
232 switch ((p->eax & 0xFF00)>>8) {
233 case 0x65:
234 switch (p->eax & 0xFF) {
235 case 06:{/* get collate table */
236 char *table;
237 /* ES:DI is a REALMODE pointer to 5 byte dosmem
238 * we fill that with 0x6, realmode pointer to collateTB
240 table = DOSMEM_MapRealToLinear(MAKELONG(p->edi,p->es));
241 *(BYTE*)table = 0x06;
242 *(DWORD*)(table+1) = DOSMEM_CollateTable;
244 CX_reg(context) = 258;/*FIXME: size of table?*/
245 break;
247 default:
248 SET_CFLAG(context);
250 break;
251 case 0x44:
252 switch (p->eax & 0xFF) {
253 case 0x0D:{/* generic block device request */
254 BYTE *dataptr = DOSMEM_MapRealToLinear((p->ds)*0x1000+(p->edx & 0xFFFF));
255 int drive = DOS_GET_DRIVE(p->ebx&0xFF);
257 if ((p->ecx & 0xFF00) != 0x0800) {
258 SET_CFLAG(context);
259 break;
261 switch (p->ecx & 0xFF) {
262 case 0x66:{
264 char label[12],fsname[9],path[4];
265 DWORD serial;
267 strcpy(path,"x:\\");path[0]=drive+'A';
268 GetVolumeInformation32A(path,label,12,&serial,NULL,NULL,fsname,9);
269 *(WORD*)dataptr = 0;
270 memcpy(dataptr+2,&serial,4);
271 memcpy(dataptr+6,label ,11);
272 memcpy(dataptr+17,fsname,8);
273 break;
275 case 0x60: /* get device parameters */
276 /* used by defrag.exe of win95 */
277 memset(dataptr, 0, 0x26);
278 dataptr[0] = 0x04;
279 dataptr[6] = 0; /* media type */
280 if (drive > 1) {
281 dataptr[1] = 0x05; /* fixed disk */
282 setword(&dataptr[2], 0x01); /* non removable */
283 setword(&dataptr[4], 0x300); /* # of cylinders */
284 } else {
285 dataptr[1] = 0x07; /* block dev, floppy */
286 setword(&dataptr[2], 0x02); /* removable */
287 setword(&dataptr[4], 80); /* # of cylinders */
289 CreateBPB(drive, &dataptr[7]);
290 break;
291 default:
292 SET_CFLAG(context);
293 break;
296 break;
297 default:
298 SET_CFLAG(context);
299 break;
301 break;
302 default:
303 SET_CFLAG(context);
304 break;
306 break;
307 default:
308 SET_CFLAG(context);
309 break;
311 if (EFL_reg(context)&1) {
312 fprintf(stdnimp,
313 "RealModeInt %02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
314 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x\n",
315 BL_reg(context), p->eax, p->ebx, p->ecx, p->edx,
316 p->esi, p->edi, p->es, p->ds
320 break;
321 case 0x0301: /* Call real mode procedure with far return */
323 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
324 fprintf(stdnimp,
325 "RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
326 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n",
327 p->eax, p->ebx, p->ecx, p->edx,
328 p->esi, p->edi, p->es, p->ds, p->cs, p->ip );
329 SET_CFLAG(context);
331 break;
333 case 0x0302: /* Call real mode procedure with interrupt return */
335 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
336 fprintf(stdnimp,
337 "RealModeCallIret: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
338 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n",
339 p->eax, p->ebx, p->ecx, p->edx,
340 p->esi, p->edi, p->es, p->ds, p->cs, p->ip );
341 SET_CFLAG(context);
343 break;
345 case 0x0303: /* Allocate Real Mode Callback Address */
347 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
348 fprintf(stdnimp,
349 "AllocRMCB: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
350 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n"
351 " Function to call: %04x:%04x\n",
352 p->eax, p->ebx, p->ecx, p->edx,
353 p->esi, p->edi, p->es, p->ds, p->cs, p->ip,
354 (WORD)DS_reg(context), SI_reg(context) );
355 SET_CFLAG(context);
357 break;
359 case 0x0400: /* Get DPMI version */
361 SYSTEM_INFO si;
363 GetSystemInfo(&si);
364 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
365 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
366 CL_reg(context) = si.wProcessorLevel;
367 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
368 break;
370 case 0x0500: /* Get free memory information */
372 MEMMANINFO mmi;
374 mmi.dwSize = sizeof(mmi);
375 MemManInfo(&mmi);
376 ptr = (BYTE *)PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context));
377 /* the layout is just the same as MEMMANINFO, but without
378 * the dwSize entry.
380 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
381 break;
383 case 0x0501: /* Allocate memory block */
384 if (!(ptr = (BYTE *)HeapAlloc( SystemHeap, 0,MAKELONG( CX_reg(context),
385 BX_reg(context) ))))
387 AX_reg(context) = 0x8012; /* linear memory not available */
388 SET_CFLAG(context);
390 else
392 BX_reg(context) = SI_reg(context) = HIWORD(ptr);
393 CX_reg(context) = DI_reg(context) = LOWORD(ptr);
395 break;
397 case 0x0502: /* Free memory block */
398 HeapFree( SystemHeap, 0,
399 (void *)MAKELONG( DI_reg(context), SI_reg(context) ) );
400 break;
402 case 0x0503: /* Resize memory block */
403 if (!(ptr = (BYTE *)HeapReAlloc( SystemHeap, 0,
404 (void *)MAKELONG(DI_reg(context),SI_reg(context)),
405 MAKELONG(CX_reg(context),BX_reg(context)))))
407 AX_reg(context) = 0x8012; /* linear memory not available */
408 SET_CFLAG(context);
410 else
412 BX_reg(context) = SI_reg(context) = HIWORD(ptr);
413 CX_reg(context) = DI_reg(context) = LOWORD(ptr);
415 break;
417 case 0x0600: /* Lock linear region */
418 break; /* Just ignore it */
420 case 0x0601: /* Unlock linear region */
421 break; /* Just ignore it */
423 case 0x0602: /* Unlock real-mode region */
424 break; /* Just ignore it */
426 case 0x0603: /* Lock real-mode region */
427 break; /* Just ignore it */
429 case 0x0604: /* Get page size */
430 BX_reg(context) = 0;
431 CX_reg(context) = 4096;
432 break;
434 case 0x0702: /* Mark page as demand-paging candidate */
435 break; /* Just ignore it */
437 case 0x0703: /* Discard page contents */
438 break; /* Just ignore it */
440 default:
441 INT_BARF( context, 0x31 );
442 AX_reg(context) = 0x8001; /* unsupported function */
443 SET_CFLAG(context);
444 break;