Release 970509
[wine/multimedia.git] / msdos / dpmi.c
blob408e6dc6966da8613fe93cb9390793976f620fb3
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( CONTEXT *context );
48 /**********************************************************************
49 * INT_Int31Handler
51 * Handler for int 31h (DPMI).
53 void INT_Int31Handler( CONTEXT *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 if (!((FS_reg(context)^BX_reg(context)) & ~3)) FS_reg(context) = 0;
82 if (!((GS_reg(context)^BX_reg(context)) & ~3)) GS_reg(context) = 0;
84 break;
86 case 0x0002: /* Real mode segment to descriptor */
88 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
89 switch(BX_reg(context))
91 case 0x0000: entryPoint = 183; break; /* __0000H */
92 case 0x0040: entryPoint = 193; break; /* __0040H */
93 case 0xa000: entryPoint = 174; break; /* __A000H */
94 case 0xb000: entryPoint = 181; break; /* __B000H */
95 case 0xb800: entryPoint = 182; break; /* __B800H */
96 case 0xc000: entryPoint = 195; break; /* __C000H */
97 case 0xd000: entryPoint = 179; break; /* __D000H */
98 case 0xe000: entryPoint = 190; break; /* __E000H */
99 case 0xf000: entryPoint = 194; break; /* __F000H */
100 default:
101 AX_reg(context) = DOSMEM_AllocSelector(BX_reg(context));
102 break;
104 if (entryPoint)
105 AX_reg(context) = LOWORD(MODULE_GetEntryPoint(
106 GetModuleHandle16( "KERNEL" ),
107 entryPoint ));
109 break;
111 case 0x0003: /* Get next selector increment */
112 AX_reg(context) = __AHINCR;
113 break;
115 case 0x0004: /* Lock selector (not supported) */
116 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
117 break;
119 case 0x0005: /* Unlock selector (not supported) */
120 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
121 break;
123 case 0x0006: /* Get selector base address */
124 if (!(dw = GetSelectorBase( BX_reg(context) )))
126 AX_reg(context) = 0x8022; /* invalid selector */
127 SET_CFLAG(context);
129 else
131 CX_reg(context) = HIWORD(dw);
132 DX_reg(context) = LOWORD(dw);
134 break;
136 case 0x0007: /* Set selector base address */
137 SetSelectorBase( BX_reg(context),
138 MAKELONG( DX_reg(context), CX_reg(context) ) );
139 break;
141 case 0x0008: /* Set selector limit */
142 SetSelectorLimit( BX_reg(context),
143 MAKELONG( DX_reg(context), CX_reg(context) ) );
144 break;
146 case 0x0009: /* Set selector access rights */
147 SelectorAccessRights( BX_reg(context), 1, CX_reg(context) );
148 break;
150 case 0x000a: /* Allocate selector alias */
151 if (!(AX_reg(context) = AllocCStoDSAlias( BX_reg(context) )))
153 AX_reg(context) = 0x8011; /* descriptor unavailable */
154 SET_CFLAG(context);
156 break;
158 case 0x000b: /* Get descriptor */
160 ldt_entry entry;
161 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
162 /* FIXME: should use ES:EDI for 32-bit clients */
163 LDT_EntryToBytes( PTR_SEG_OFF_TO_LIN( ES_reg(context),
164 DI_reg(context) ), &entry );
166 break;
168 case 0x000c: /* Set descriptor */
170 ldt_entry entry;
171 LDT_BytesToEntry( PTR_SEG_OFF_TO_LIN( ES_reg(context),
172 DI_reg(context) ), &entry );
173 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
175 break;
177 case 0x000d: /* Allocate specific LDT descriptor */
178 AX_reg(context) = 0x8011; /* descriptor unavailable */
179 SET_CFLAG(context);
180 break;
181 case 0x0200: /* get real mode interrupt vector */
182 fprintf(stdnimp,
183 "int31: get realmode interupt vector(0x%02x) unimplemented.\n",
184 BL_reg(context)
186 SET_CFLAG(context);
187 break;
188 case 0x0201: /* set real mode interrupt vector */
189 fprintf(stdnimp,
190 "int31: set realmode interupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n",
191 BL_reg(context),CX_reg(context),DX_reg(context)
193 SET_CFLAG(context);
194 break;
195 case 0x0204: /* Get protected mode interrupt vector */
196 dw = (DWORD)INT_GetHandler( BL_reg(context) );
197 CX_reg(context) = HIWORD(dw);
198 DX_reg(context) = LOWORD(dw);
199 break;
201 case 0x0205: /* Set protected mode interrupt vector */
202 INT_SetHandler( BL_reg(context),
203 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( CX_reg(context),
204 DX_reg(context) ));
205 break;
207 case 0x0300: /* Simulate real mode interrupt
208 * Interrupt number is in BL, flags are in BH
209 * ES:DI points to real-mode call structure
210 * Currently we just print it out and return error.
212 RESET_CFLAG(context);
214 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
216 switch (BL_reg(context)) {
217 case 0x2f: /* int2f */
218 switch ((p->eax & 0xFF00)>>8) {
219 case 0x15:
220 /* MSCDEX hook */
221 AX_reg(context) = p->eax & 0xFFFF;
222 do_mscdex( context );
223 break;
224 default:
225 SET_CFLAG(context);
226 break;
228 break;
229 case 0x21: /* int21 */
230 switch ((p->eax & 0xFF00)>>8) {
231 case 0x65:
232 switch (p->eax & 0xFF) {
233 case 06:{/* get collate table */
234 char *table;
235 /* ES:DI is a REALMODE pointer to 5 byte dosmem
236 * we fill that with 0x6, realmode pointer to collateTB
238 table = DOSMEM_MapRealToLinear(MAKELONG(p->edi,p->es));
239 *(BYTE*)table = 0x06;
240 *(DWORD*)(table+1) = DOSMEM_CollateTable;
242 CX_reg(context) = 258;/*FIXME: size of table?*/
243 break;
245 default:
246 SET_CFLAG(context);
248 break;
249 case 0x44:
250 switch (p->eax & 0xFF) {
251 case 0x0D:{/* generic block device request */
252 BYTE *dataptr = DOSMEM_MapRealToLinear((p->ds)*0x1000+(p->edx & 0xFFFF));
253 int drive = DOS_GET_DRIVE(p->ebx&0xFF);
255 if ((p->ecx & 0xFF00) != 0x0800) {
256 SET_CFLAG(context);
257 break;
259 switch (p->ecx & 0xFF) {
260 case 0x66:{
262 char label[12],fsname[9],path[4];
263 DWORD serial;
265 strcpy(path,"x:\\");path[0]=drive+'A';
266 GetVolumeInformation32A(path,label,12,&serial,NULL,NULL,fsname,9);
267 *(WORD*)dataptr = 0;
268 memcpy(dataptr+2,&serial,4);
269 memcpy(dataptr+6,label ,11);
270 memcpy(dataptr+17,fsname,8);
271 break;
273 case 0x60: /* get device parameters */
274 /* used by defrag.exe of win95 */
275 memset(dataptr, 0, 0x26);
276 dataptr[0] = 0x04;
277 dataptr[6] = 0; /* media type */
278 if (drive > 1) {
279 dataptr[1] = 0x05; /* fixed disk */
280 setword(&dataptr[2], 0x01); /* non removable */
281 setword(&dataptr[4], 0x300); /* # of cylinders */
282 } else {
283 dataptr[1] = 0x07; /* block dev, floppy */
284 setword(&dataptr[2], 0x02); /* removable */
285 setword(&dataptr[4], 80); /* # of cylinders */
287 CreateBPB(drive, &dataptr[7]);
288 break;
289 default:
290 SET_CFLAG(context);
291 break;
294 break;
295 default:
296 SET_CFLAG(context);
297 break;
299 break;
300 default:
301 SET_CFLAG(context);
302 break;
304 break;
305 default:
306 SET_CFLAG(context);
307 break;
309 if (EFL_reg(context)&1) {
310 fprintf(stdnimp,
311 "RealModeInt %02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
312 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x\n",
313 BL_reg(context), p->eax, p->ebx, p->ecx, p->edx,
314 p->esi, p->edi, p->es, p->ds
318 break;
319 case 0x0301: /* Call real mode procedure with far return */
321 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
322 fprintf(stdnimp,
323 "RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
324 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n",
325 p->eax, p->ebx, p->ecx, p->edx,
326 p->esi, p->edi, p->es, p->ds, p->cs, p->ip );
327 SET_CFLAG(context);
329 break;
331 case 0x0302: /* Call real mode procedure with interrupt return */
333 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
334 fprintf(stdnimp,
335 "RealModeCallIret: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
336 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n",
337 p->eax, p->ebx, p->ecx, p->edx,
338 p->esi, p->edi, p->es, p->ds, p->cs, p->ip );
339 SET_CFLAG(context);
341 break;
343 case 0x0303: /* Allocate Real Mode Callback Address */
345 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
346 fprintf(stdnimp,
347 "AllocRMCB: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
348 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n"
349 " Function to call: %04x:%04x\n",
350 p->eax, p->ebx, p->ecx, p->edx,
351 p->esi, p->edi, p->es, p->ds, p->cs, p->ip,
352 (WORD)DS_reg(context), SI_reg(context) );
353 SET_CFLAG(context);
355 break;
357 case 0x0400: /* Get DPMI version */
359 SYSTEM_INFO si;
361 GetSystemInfo(&si);
362 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
363 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
364 CL_reg(context) = si.wProcessorLevel;
365 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
366 break;
368 case 0x0500: /* Get free memory information */
370 MEMMANINFO mmi;
372 mmi.dwSize = sizeof(mmi);
373 MemManInfo(&mmi);
374 ptr = (BYTE *)PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context));
375 /* the layout is just the same as MEMMANINFO, but without
376 * the dwSize entry.
378 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
379 break;
381 case 0x0501: /* Allocate memory block */
382 if (!(ptr = (BYTE *)HeapAlloc( SystemHeap, 0,MAKELONG( CX_reg(context),
383 BX_reg(context) ))))
385 AX_reg(context) = 0x8012; /* linear memory not available */
386 SET_CFLAG(context);
388 else
390 BX_reg(context) = SI_reg(context) = HIWORD(ptr);
391 CX_reg(context) = DI_reg(context) = LOWORD(ptr);
393 break;
395 case 0x0502: /* Free memory block */
396 HeapFree( SystemHeap, 0,
397 (void *)MAKELONG( DI_reg(context), SI_reg(context) ) );
398 break;
400 case 0x0503: /* Resize memory block */
401 if (!(ptr = (BYTE *)HeapReAlloc( SystemHeap, 0,
402 (void *)MAKELONG(DI_reg(context),SI_reg(context)),
403 MAKELONG(CX_reg(context),BX_reg(context)))))
405 AX_reg(context) = 0x8012; /* linear memory not available */
406 SET_CFLAG(context);
408 else
410 BX_reg(context) = SI_reg(context) = HIWORD(ptr);
411 CX_reg(context) = DI_reg(context) = LOWORD(ptr);
413 break;
415 case 0x0600: /* Lock linear region */
416 break; /* Just ignore it */
418 case 0x0601: /* Unlock linear region */
419 break; /* Just ignore it */
421 case 0x0602: /* Unlock real-mode region */
422 break; /* Just ignore it */
424 case 0x0603: /* Lock real-mode region */
425 break; /* Just ignore it */
427 case 0x0604: /* Get page size */
428 BX_reg(context) = 0;
429 CX_reg(context) = 4096;
430 break;
432 case 0x0702: /* Mark page as demand-paging candidate */
433 break; /* Just ignore it */
435 case 0x0703: /* Discard page contents */
436 break; /* Just ignore it */
438 default:
439 INT_BARF( context, 0x31 );
440 AX_reg(context) = 0x8001; /* unsupported function */
441 SET_CFLAG(context);
442 break;