Release 960131
[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 <stdlib.h>
10 #include <string.h>
11 #include "windows.h"
12 #include "ldt.h"
13 #include "module.h"
14 #include "registers.h"
15 #include "wine.h"
16 #include "miscemu.h"
17 #include "stddebug.h"
18 /* #define DEBUG_INT */
19 #include "debug.h"
20
21
22 /* Structure for real-mode callbacks */
23 typedef struct
24 {
25     DWORD edi;
26     DWORD esi;
27     DWORD ebp;
28     DWORD reserved;
29     DWORD ebx;
30     DWORD edx;
31     DWORD ecx;
32     DWORD eax;
33     WORD  flags;
34     WORD  es;
35     WORD  ds;
36     WORD  fs;
37     WORD  gs;
38     WORD  ip;
39     WORD  cs;
40     WORD  sp;
41     WORD  ss;
42 } REALMODECALL;
43
44 extern void do_mscdex(struct sigcontext_struct *context);
45
46 /**********************************************************************
47  *          INT_Int31Handler
48  *
49  * Handler for int 31h (DPMI).
50  */
51 void INT_Int31Handler( struct sigcontext_struct context )
52 {
53     DWORD dw;
54     BYTE *ptr;
55
56     RESET_CFLAG(&context);
57     switch(AX_reg(&context))
58     {
59     case 0x0000:  /* Allocate LDT descriptors */
60         if (!(AX_reg(&context) = AllocSelectorArray( CX_reg(&context) )))
61         {
62             AX_reg(&context) = 0x8011;  /* descriptor unavailable */
63             SET_CFLAG(&context);
64         }
65         break;
66
67     case 0x0001:  /* Free LDT descriptor */
68         if (FreeSelector( BX_reg(&context) ))
69         {
70             AX_reg(&context) = 0x8022;  /* invalid selector */
71             SET_CFLAG(&context);
72         }
73         break;
74
75     case 0x0002:  /* Real mode segment to descriptor */
76         {
77             WORD entryPoint = 0;  /* KERNEL entry point for descriptor */
78             switch(BX_reg(&context))
79             {
80             case 0x0000: entryPoint = 183; break;  /* __0000H */
81             case 0x0040: entryPoint = 193; break;  /* __0040H */
82             case 0xa000: entryPoint = 174; break;  /* __A000H */
83             case 0xb000: entryPoint = 181; break;  /* __B000H */
84             case 0xb800: entryPoint = 182; break;  /* __B800H */
85             case 0xc000: entryPoint = 195; break;  /* __C000H */
86             case 0xd000: entryPoint = 179; break;  /* __D000H */
87             case 0xe000: entryPoint = 190; break;  /* __E000H */
88             case 0xf000: entryPoint = 194; break;  /* __F000H */
89             default:
90                 fprintf( stderr, "DPMI: real-mode seg to descriptor %04x not possible\n",
91                          BX_reg(&context) );
92                 AX_reg(&context) = 0x8011;
93                 SET_CFLAG(&context);
94                 break;
95             }
96             if (entryPoint) 
97                 AX_reg(&context) = LOWORD(MODULE_GetEntryPoint( 
98                                                    GetModuleHandle( "KERNEL" ),
99                                                    entryPoint ));
100         }
101         break;
102
103     case 0x0003:  /* Get next selector increment */
104         AX_reg(&context) = __AHINCR;
105         break;
106
107     case 0x0004:  /* Lock selector (not supported) */
108         AX_reg(&context) = 0;  /* FIXME: is this a correct return value? */
109         break;
110
111     case 0x0005:  /* Unlock selector (not supported) */
112         AX_reg(&context) = 0;  /* FIXME: is this a correct return value? */
113         break;
114
115     case 0x0006:  /* Get selector base address */
116         if (!(dw = GetSelectorBase( BX_reg(&context) )))
117         {
118             AX_reg(&context) = 0x8022;  /* invalid selector */
119             SET_CFLAG(&context);
120         }
121         else
122         {
123             CX_reg(&context) = HIWORD(dw);
124             DX_reg(&context) = LOWORD(dw);
125         }
126         break;
127
128     case 0x0007:  /* Set selector base address */
129         SetSelectorBase( BX_reg(&context),
130                          MAKELONG( DX_reg(&context), CX_reg(&context) ) );
131         break;
132
133     case 0x0008:  /* Set selector limit */
134         SetSelectorLimit( BX_reg(&context),
135                           MAKELONG( DX_reg(&context), CX_reg(&context) ) );
136         break;
137
138     case 0x0009:  /* Set selector access rights */
139         SelectorAccessRights( BX_reg(&context), 1, CX_reg(&context) );
140
141     case 0x000a:  /* Allocate selector alias */
142         if (!(AX_reg(&context) = AllocCStoDSAlias( BX_reg(&context) )))
143         {
144             AX_reg(&context) = 0x8011;  /* descriptor unavailable */
145             SET_CFLAG(&context);
146         }
147         break;
148
149     case 0x000b:  /* Get descriptor */
150         {
151             ldt_entry entry;
152             LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(&context) ), &entry );
153             /* FIXME: should use ES:EDI for 32-bit clients */
154             LDT_EntryToBytes( PTR_SEG_OFF_TO_LIN( ES_reg(&context),
155                                                   DI_reg(&context) ), &entry );
156         }
157         break;
158
159     case 0x000c:  /* Set descriptor */
160         {
161             ldt_entry entry;
162             LDT_BytesToEntry( PTR_SEG_OFF_TO_LIN( ES_reg(&context),
163                                                   DI_reg(&context) ), &entry );
164             LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(&context) ), &entry );
165         }
166         break;
167
168     case 0x000d:  /* Allocate specific LDT descriptor */
169         AX_reg(&context) = 0x8011; /* descriptor unavailable */
170         SET_CFLAG(&context);
171         break;
172
173     case 0x0204:  /* Get protected mode interrupt vector */
174         dw = (DWORD)INT_GetHandler( BL_reg(&context) );
175         CX_reg(&context) = HIWORD(dw);
176         DX_reg(&context) = LOWORD(dw);
177         break;
178
179     case 0x0205:  /* Set protected mode interrupt vector */
180         INT_SetHandler( BL_reg(&context),
181                        (SEGPTR)MAKELONG( DX_reg(&context), CX_reg(&context) ));
182         break;
183
184     case 0x0300:  /* Simulate real mode interrupt 
185         *  Interrupt number is in BL, flags are in BH
186         *  ES:DI points to real-mode call structure  
187         *  Currently we just print it out and return error.
188         */
189         {
190             REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(&context), DI_reg(&context) );
191             fprintf(stdnimp,
192                     "RealModeInt %02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
193                     "                ESI=%08lx EDI=%08lx ES=%04x DS=%04x\n",
194                     BL_reg(&context), p->eax, p->ebx, p->ecx, p->edx,
195                     p->esi, p->edi, p->es, p->ds );
196
197             /* Compton's 1995 encyclopedia does its MSCDEX calls through
198              * this interrupt.  Why?  Who knows...
199              */
200             if ((BL_reg(&context) == 0x2f) && ((p->eax & 0xFF00) == 0x1500))
201             {
202                 struct sigcontext_struct context2;
203                 EAX_reg(&context2) = p->eax;
204                 EBX_reg(&context2) = p->ebx;
205                 ECX_reg(&context2) = p->ecx;
206                 EDX_reg(&context2) = p->edx;
207                 ES_reg(&context2) = p->es;
208                 do_mscdex(&context2);
209                 p->eax = EAX_reg(&context2);
210                 p->ebx = EBX_reg(&context2);
211                 p->ecx = ECX_reg(&context2);
212                 p->edx = EDX_reg(&context2);
213             }
214             else SET_CFLAG(&context);
215         }
216         break;
217
218     case 0x0301:  /* Call real mode procedure with far return */
219         {
220             REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(&context), DI_reg(&context) );
221             fprintf(stdnimp,
222                     "RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
223                     "              ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n",
224                     p->eax, p->ebx, p->ecx, p->edx,
225                     p->esi, p->edi, p->es, p->ds, p->cs, p->ip );
226             SET_CFLAG(&context);
227         }
228         break;
229
230     case 0x0302:  /* Call real mode procedure with interrupt return */
231         {
232             REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(&context), DI_reg(&context) );
233             fprintf(stdnimp,
234                     "RealModeCallIret: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
235                     "                  ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n",
236                     p->eax, p->ebx, p->ecx, p->edx,
237                     p->esi, p->edi, p->es, p->ds, p->cs, p->ip );
238             SET_CFLAG(&context);
239         }
240         break;
241
242     case 0x0400:  /* Get DPMI version */
243         AX_reg(&context) = 0x005a;  /* DPMI version 0.90 */
244         BX_reg(&context) = 0x0005;  /* Flags: 32-bit, virtual memory */
245         CL_reg(&context) = runtime_cpu ();
246         DX_reg(&context) = 0x0102;  /* Master/slave interrupt controller base*/
247         break;
248
249     case 0x0500:  /* Get free memory information */
250         ptr = (BYTE *)PTR_SEG_OFF_TO_LIN( ES_reg(&context), DI_reg(&context) );
251         *(DWORD *)ptr = 0x00ff0000; /* Largest block available */
252         memset( ptr + 4, 0xff, 0x2c );  /* No other information supported */
253         break;
254
255     case 0x0501:  /* Allocate memory block */
256         if (!(ptr = (BYTE *)malloc( MAKELONG( CX_reg(&context),
257                                               BX_reg(&context) ) )))
258         {
259             AX_reg(&context) = 0x8012;  /* linear memory not available */
260             SET_CFLAG(&context);
261         }
262         else
263         {
264             BX_reg(&context) = SI_reg(&context) = HIWORD(ptr);
265             CX_reg(&context) = DI_reg(&context) = LOWORD(ptr);
266         }
267         break;
268
269     case 0x0502:  /* Free memory block */
270         free( (void *)MAKELONG( DI_reg(&context), SI_reg(&context) ) );
271         break;
272
273     case 0x0503:  /* Resize memory block */
274         if (!(ptr = (BYTE *)realloc( (void *)MAKELONG(DI_reg(&context),SI_reg(&context)),
275                                      MAKELONG(CX_reg(&context),BX_reg(&context)))))
276         {
277             AX_reg(&context) = 0x8012;  /* linear memory not available */
278             SET_CFLAG(&context);
279         }
280         else
281         {
282             BX_reg(&context) = SI_reg(&context) = HIWORD(ptr);
283             CX_reg(&context) = DI_reg(&context) = LOWORD(ptr);
284         }
285         break;
286
287     case 0x0600:  /* Lock linear region */
288         break;  /* Just ignore it */
289
290     case 0x0601:  /* Unlock linear region */
291         break;  /* Just ignore it */
292
293     case 0x0604:  /* Get page size */
294         BX_reg(&context) = 0;
295         CX_reg(&context) = 4096;
296         break;
297
298     default:
299         INT_BARF( &context, 0x31 );
300         AX_reg(&context) = 0x8001;  /* unsupported function */
301         SET_CFLAG(&context);
302         break;
303     }
304 }