Release 960913
[wine] / miscemu / dpmi.c
1 /*
2  * DPMI 0.9 emulation
3  *
4  * Copyright 1995 Alexandre Julliard
5  */
6
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"
19
20 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
21
22
23 /* Structure for real-mode callbacks */
24 typedef struct
25 {
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;
44
45 extern void do_mscdex( SIGCONTEXT *context );
46
47 /**********************************************************************
48  *          INT_Int31Handler
49  *
50  * Handler for int 31h (DPMI).
51  */
52 void INT_Int31Handler( SIGCONTEXT *context )
53 {
54     DWORD dw;
55     BYTE *ptr;
56
57     RESET_CFLAG(context);
58     switch(AX_reg(context))
59     {
60     case 0x0000:  /* Allocate LDT descriptors */
61         if (!(AX_reg(context) = AllocSelectorArray( CX_reg(context) )))
62         {
63             AX_reg(context) = 0x8011;  /* descriptor unavailable */
64             SET_CFLAG(context);
65         }
66         break;
67
68     case 0x0001:  /* Free LDT descriptor */
69         if (FreeSelector( BX_reg(context) ))
70         {
71             AX_reg(context) = 0x8022;  /* invalid selector */
72             SET_CFLAG(context);
73         }
74         else
75         {
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
86         }
87         break;
88
89     case 0x0002:  /* Real mode segment to descriptor */
90         {
91             WORD entryPoint = 0;  /* KERNEL entry point for descriptor */
92             switch(BX_reg(context))
93             {
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;
106             }
107             if (entryPoint) 
108                 AX_reg(context) = LOWORD(MODULE_GetEntryPoint( 
109                                                    GetModuleHandle( "KERNEL" ),
110                                                    entryPoint ));
111         }
112         break;
113
114     case 0x0003:  /* Get next selector increment */
115         AX_reg(context) = __AHINCR;
116         break;
117
118     case 0x0004:  /* Lock selector (not supported) */
119         AX_reg(context) = 0;  /* FIXME: is this a correct return value? */
120         break;
121
122     case 0x0005:  /* Unlock selector (not supported) */
123         AX_reg(context) = 0;  /* FIXME: is this a correct return value? */
124         break;
125
126     case 0x0006:  /* Get selector base address */
127         if (!(dw = GetSelectorBase( BX_reg(context) )))
128         {
129             AX_reg(context) = 0x8022;  /* invalid selector */
130             SET_CFLAG(context);
131         }
132         else
133         {
134             CX_reg(context) = HIWORD(dw);
135             DX_reg(context) = LOWORD(dw);
136         }
137         break;
138
139     case 0x0007:  /* Set selector base address */
140         SetSelectorBase( BX_reg(context),
141                          MAKELONG( DX_reg(context), CX_reg(context) ) );
142         break;
143
144     case 0x0008:  /* Set selector limit */
145         SetSelectorLimit( BX_reg(context),
146                           MAKELONG( DX_reg(context), CX_reg(context) ) );
147         break;
148
149     case 0x0009:  /* Set selector access rights */
150         SelectorAccessRights( BX_reg(context), 1, CX_reg(context) );
151         break;
152
153     case 0x000a:  /* Allocate selector alias */
154         if (!(AX_reg(context) = AllocCStoDSAlias( BX_reg(context) )))
155         {
156             AX_reg(context) = 0x8011;  /* descriptor unavailable */
157             SET_CFLAG(context);
158         }
159         break;
160
161     case 0x000b:  /* Get descriptor */
162         {
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 );
168         }
169         break;
170
171     case 0x000c:  /* Set descriptor */
172         {
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 );
177         }
178         break;
179
180     case 0x000d:  /* Allocate specific LDT descriptor */
181         AX_reg(context) = 0x8011; /* descriptor unavailable */
182         SET_CFLAG(context);
183         break;
184
185     case 0x0204:  /* Get protected mode interrupt vector */
186         dw = (DWORD)INT_GetHandler( BL_reg(context) );
187         CX_reg(context) = HIWORD(dw);
188         DX_reg(context) = LOWORD(dw);
189         break;
190
191     case 0x0205:  /* Set protected mode interrupt vector */
192         INT_SetHandler( BL_reg(context),
193                         (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( CX_reg(context),
194                                                           DX_reg(context) ));
195         break;
196
197     case 0x0300:  /* Simulate real mode interrupt 
198         *  Interrupt number is in BL, flags are in BH
199         *  ES:DI points to real-mode call structure  
200         *  Currently we just print it out and return error.
201         */
202         RESET_CFLAG(context);
203         {
204             REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
205
206             switch (BL_reg(context)) {
207             case 0x2f:  /* int2f */
208                 switch ((p->eax & 0xFF00)>>8) {
209                 case 0x15:
210                         /* MSCDEX hook */
211                         AX_reg(context) = p->eax & 0xFFFF;
212                         do_mscdex( context );
213                         break;
214                 default:
215                         SET_CFLAG(context);
216                         break;
217                 }
218                 break;
219             case 0x21:  /* int21 */
220                 switch ((p->eax & 0xFF00)>>8) {
221                 case 0x65:
222                     switch (p->eax & 0xFF) {
223                     case 06:{/* get collate table */
224                         extern  DWORD   DOSMEM_CollateTable;
225                         char    *table;
226                         /* ES:DI is a REALMODE pointer to 5 byte dosmem 
227                          * we fill that with 0x6, realmode pointer to collateTB
228                          */
229                         table = DOSMEM_RealMode2Linear(MAKELONG(p->edi,p->es));
230                         *(BYTE*)table           = 0x06;
231                         *(DWORD*)(table+1)      = DOSMEM_CollateTable;
232
233                         CX_reg(context)         = 258;/*FIXME: size of table?*/
234                         break;
235                     }
236                     default:
237                         SET_CFLAG(context);
238                     }
239                     break;
240                 case 0x44:
241                     switch (p->eax & 0xFF) {
242                     case 0x0D:{/* generic block device request */
243                         BYTE    *dataptr = DOSMEM_RealMode2Linear((p->ds)*0x1000+(p->edx & 0xFFFF));
244                         int     drive = DOS_GET_DRIVE(p->ebx&0xFF); 
245
246                         if ((p->ecx & 0xFF00) != 0x0800) {
247                                 SET_CFLAG(context);
248                                 break;
249                         }
250                         switch (p->ecx & 0xFF) {
251                         case 0x66:{ 
252
253                             char    label[12],fsname[9],path[4];
254                             DWORD   serial;
255
256                             strcpy(path,"x:\\");path[0]=drive+'A';
257                             GetVolumeInformation32A(path,label,12,&serial,NULL,NULL,fsname,9);
258                             *(WORD*)dataptr         = 0;
259                             memcpy(dataptr+2,&serial,4);
260                             memcpy(dataptr+6,label  ,11);
261                             memcpy(dataptr+17,fsname,8);
262                             break;
263                         }
264                         case 0x60:      /* get device parameters */
265                                         /* used by defrag.exe of win95 */
266                             memset(dataptr, 0, 0x26);
267                             dataptr[0] = 0x04;
268                             dataptr[6] = 0; /* media type */
269                             if (drive > 1) {
270                                 dataptr[1] = 0x05; /* fixed disk */
271                                 setword(&dataptr[2], 0x01); /* non removable */
272                                 setword(&dataptr[4], 0x300); /* # of cylinders */
273                             } else {
274                                 dataptr[1] = 0x07; /* block dev, floppy */
275                                 setword(&dataptr[2], 0x02); /* removable */
276                                 setword(&dataptr[4], 80); /* # of cylinders */
277                             }
278                             CreateBPB(drive, &dataptr[7]);
279                             break;
280                         default:
281                             SET_CFLAG(context);
282                             break;
283                         }
284                     }
285                         break;
286                     default:
287                         SET_CFLAG(context);
288                         break;
289                     }
290                 default:
291                     SET_CFLAG(context);
292                     break;
293                 }
294                 break;
295             default:
296                 SET_CFLAG(context);
297                 break;
298             }
299             if (EFL_reg(context)&1) {
300                 fprintf(stdnimp,
301                     "RealModeInt %02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
302                     "                ESI=%08lx EDI=%08lx ES=%04x DS=%04x\n",
303                     BL_reg(context), p->eax, p->ebx, p->ecx, p->edx,
304                     p->esi, p->edi, p->es, p->ds
305                 );
306             }
307         }
308         break;
309     case 0x0301:  /* Call real mode procedure with far return */
310         {
311             REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
312             fprintf(stdnimp,
313                     "RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
314                     "              ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n",
315                     p->eax, p->ebx, p->ecx, p->edx,
316                     p->esi, p->edi, p->es, p->ds, p->cs, p->ip );
317             SET_CFLAG(context);
318         }
319         break;
320
321     case 0x0302:  /* Call real mode procedure with interrupt return */
322         {
323             REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
324             fprintf(stdnimp,
325                     "RealModeCallIret: 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);
330         }
331         break;
332
333     case 0x0303:  /* Allocate Real Mode Callback Address */
334         {
335             REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
336             fprintf(stdnimp,
337                     "AllocRMCB: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
338                     "           ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n"
339                     "   Function to call: %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                     DS_reg(context),SI_reg(context)
343             );
344             SET_CFLAG(context);
345         }
346         break;
347
348     case 0x0400:  /* Get DPMI version */
349         AX_reg(context) = 0x005a;  /* DPMI version 0.90 */
350         BX_reg(context) = 0x0005;  /* Flags: 32-bit, virtual memory */
351         CL_reg(context) = runtime_cpu ();
352         DX_reg(context) = 0x0102;  /* Master/slave interrupt controller base*/
353         break;
354
355     case 0x0500:  /* Get free memory information */
356         ptr = (BYTE *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
357         *(DWORD *)ptr = 0x00ff0000; /* Largest block available */
358         memset( ptr + 4, 0xff, 0x2c );  /* No other information supported */
359         break;
360
361     case 0x0501:  /* Allocate memory block */
362         if (!(ptr = (BYTE *)HeapAlloc( SystemHeap, 0,MAKELONG( CX_reg(context),
363                                                            BX_reg(context) ))))
364         {
365             AX_reg(context) = 0x8012;  /* linear memory not available */
366             SET_CFLAG(context);
367         }
368         else
369         {
370             BX_reg(context) = SI_reg(context) = HIWORD(ptr);
371             CX_reg(context) = DI_reg(context) = LOWORD(ptr);
372         }
373         break;
374
375     case 0x0502:  /* Free memory block */
376         HeapFree( SystemHeap, 0,
377                   (void *)MAKELONG( DI_reg(context), SI_reg(context) ) );
378         break;
379
380     case 0x0503:  /* Resize memory block */
381         if (!(ptr = (BYTE *)HeapReAlloc( SystemHeap, 0,
382                            (void *)MAKELONG(DI_reg(context),SI_reg(context)),
383                                    MAKELONG(CX_reg(context),BX_reg(context)))))
384         {
385             AX_reg(context) = 0x8012;  /* linear memory not available */
386             SET_CFLAG(context);
387         }
388         else
389         {
390             BX_reg(context) = SI_reg(context) = HIWORD(ptr);
391             CX_reg(context) = DI_reg(context) = LOWORD(ptr);
392         }
393         break;
394
395     case 0x0600:  /* Lock linear region */
396         break;  /* Just ignore it */
397
398     case 0x0601:  /* Unlock linear region */
399         break;  /* Just ignore it */
400
401     case 0x0602:  /* Unlock real-mode region */
402         break;  /* Just ignore it */
403
404     case 0x0603:  /* Lock real-mode region */
405         break;  /* Just ignore it */
406
407     case 0x0604:  /* Get page size */
408         BX_reg(context) = 0;
409         CX_reg(context) = 4096;
410         break;
411
412     case 0x0702:  /* Mark page as demand-paging candidate */
413         break;  /* Just ignore it */
414
415     case 0x0703:  /* Discard page contents */
416         break;  /* Just ignore it */
417
418     default:
419         INT_BARF( context, 0x31 );
420         AX_reg(context) = 0x8001;  /* unsupported function */
421         SET_CFLAG(context);
422         break;
423     }
424 }