Release 960717
[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 "stddebug.h"
16 /* #define DEBUG_INT */
17 #include "debug.h"
18
19
20 /* Structure for real-mode callbacks */
21 typedef struct
22 {
23     DWORD edi;
24     DWORD esi;
25     DWORD ebp;
26     DWORD reserved;
27     DWORD ebx;
28     DWORD edx;
29     DWORD ecx;
30     DWORD eax;
31     WORD  flags;
32     WORD  es;
33     WORD  ds;
34     WORD  fs;
35     WORD  gs;
36     WORD  ip;
37     WORD  cs;
38     WORD  sp;
39     WORD  ss;
40 } REALMODECALL;
41
42 extern void do_mscdex( SIGCONTEXT *context );
43
44 /**********************************************************************
45  *          INT_Int31Handler
46  *
47  * Handler for int 31h (DPMI).
48  */
49 void INT_Int31Handler( SIGCONTEXT *context )
50 {
51     DWORD dw;
52     BYTE *ptr;
53
54     RESET_CFLAG(context);
55     switch(AX_reg(context))
56     {
57     case 0x0000:  /* Allocate LDT descriptors */
58         if (!(AX_reg(context) = AllocSelectorArray( CX_reg(context) )))
59         {
60             AX_reg(context) = 0x8011;  /* descriptor unavailable */
61             SET_CFLAG(context);
62         }
63         break;
64
65     case 0x0001:  /* Free LDT descriptor */
66         if (FreeSelector( BX_reg(context) ))
67         {
68             AX_reg(context) = 0x8022;  /* invalid selector */
69             SET_CFLAG(context);
70         }
71         else
72         {
73             /* If a segment register contains the selector being freed, */
74             /* set it to zero. */
75             if (!((DS_reg(context)^BX_reg(context)) & ~3)) DS_reg(context) = 0;
76             if (!((ES_reg(context)^BX_reg(context)) & ~3)) ES_reg(context) = 0;
77 #ifdef FS_reg
78             if (!((FS_reg(context)^BX_reg(context)) & ~3)) FS_reg(context) = 0;
79 #endif
80 #ifdef GS_reg
81             if (!((GS_reg(context)^BX_reg(context)) & ~3)) GS_reg(context) = 0;
82 #endif
83         }
84         break;
85
86     case 0x0002:  /* Real mode segment to descriptor */
87         {
88             WORD entryPoint = 0;  /* KERNEL entry point for descriptor */
89             switch(BX_reg(context))
90             {
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                 fprintf( stderr, "DPMI: real-mode seg to descriptor %04x not possible\n",
102                          BX_reg(context) );
103                 AX_reg(context) = 0x8011;
104                 SET_CFLAG(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         {
203             REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
204             fprintf(stdnimp,
205                     "RealModeInt %02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
206                     "                ESI=%08lx EDI=%08lx ES=%04x DS=%04x\n",
207                     BL_reg(context), p->eax, p->ebx, p->ecx, p->edx,
208                     p->esi, p->edi, p->es, p->ds );
209
210             /* Compton's 1995 encyclopedia does its MSCDEX calls through
211              * this interrupt.  Why?  Who knows...
212              */
213             if ((BL_reg(context) == 0x2f) && ((p->eax & 0xFF00) == 0x1500))
214             {
215                 do_mscdex( context );
216                 break;
217             }
218             SET_CFLAG(context);
219         }
220         break;
221
222     case 0x0301:  /* Call real mode procedure with far return */
223         {
224             REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
225             fprintf(stdnimp,
226                     "RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
227                     "              ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n",
228                     p->eax, p->ebx, p->ecx, p->edx,
229                     p->esi, p->edi, p->es, p->ds, p->cs, p->ip );
230             SET_CFLAG(context);
231         }
232         break;
233
234     case 0x0302:  /* Call real mode procedure with interrupt return */
235         {
236             REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
237             fprintf(stdnimp,
238                     "RealModeCallIret: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
239                     "                  ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n",
240                     p->eax, p->ebx, p->ecx, p->edx,
241                     p->esi, p->edi, p->es, p->ds, p->cs, p->ip );
242             SET_CFLAG(context);
243         }
244         break;
245
246     case 0x0400:  /* Get DPMI version */
247         AX_reg(context) = 0x005a;  /* DPMI version 0.90 */
248         BX_reg(context) = 0x0005;  /* Flags: 32-bit, virtual memory */
249         CL_reg(context) = runtime_cpu ();
250         DX_reg(context) = 0x0102;  /* Master/slave interrupt controller base*/
251         break;
252
253     case 0x0500:  /* Get free memory information */
254         ptr = (BYTE *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
255         *(DWORD *)ptr = 0x00ff0000; /* Largest block available */
256         memset( ptr + 4, 0xff, 0x2c );  /* No other information supported */
257         break;
258
259     case 0x0501:  /* Allocate memory block */
260         if (!(ptr = (BYTE *)HeapAlloc( SystemHeap, 0,MAKELONG( CX_reg(context),
261                                                            BX_reg(context) ))))
262         {
263             AX_reg(context) = 0x8012;  /* linear memory not available */
264             SET_CFLAG(context);
265         }
266         else
267         {
268             BX_reg(context) = SI_reg(context) = HIWORD(ptr);
269             CX_reg(context) = DI_reg(context) = LOWORD(ptr);
270         }
271         break;
272
273     case 0x0502:  /* Free memory block */
274         HeapFree( SystemHeap, 0,
275                   (void *)MAKELONG( DI_reg(context), SI_reg(context) ) );
276         break;
277
278     case 0x0503:  /* Resize memory block */
279         if (!(ptr = (BYTE *)HeapReAlloc( SystemHeap, 0,
280                            (void *)MAKELONG(DI_reg(context),SI_reg(context)),
281                                    MAKELONG(CX_reg(context),BX_reg(context)))))
282         {
283             AX_reg(context) = 0x8012;  /* linear memory not available */
284             SET_CFLAG(context);
285         }
286         else
287         {
288             BX_reg(context) = SI_reg(context) = HIWORD(ptr);
289             CX_reg(context) = DI_reg(context) = LOWORD(ptr);
290         }
291         break;
292
293     case 0x0600:  /* Lock linear region */
294         break;  /* Just ignore it */
295
296     case 0x0601:  /* Unlock linear region */
297         break;  /* Just ignore it */
298
299     case 0x0602:  /* Unlock real-mode region */
300         break;  /* Just ignore it */
301
302     case 0x0603:  /* Lock real-mode region */
303         break;  /* Just ignore it */
304
305     case 0x0604:  /* Get page size */
306         BX_reg(context) = 0;
307         CX_reg(context) = 4096;
308         break;
309
310     case 0x0702:  /* Mark page as demand-paging candidate */
311         break;  /* Just ignore it */
312
313     case 0x0703:  /* Discard page contents */
314         break;  /* Just ignore it */
315
316     default:
317         INT_BARF( context, 0x31 );
318         AX_reg(context) = 0x8001;  /* unsupported function */
319         SET_CFLAG(context);
320         break;
321     }
322 }