Release 960506
[wine.git] / miscemu / dpmi.c
blob291af759e6be872b815cb7a665861352aebe212a
1 /*
2 * DPMI 0.9 emulation
4 * Copyright 1995 Alexandre Julliard
5 */
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"
22 /* Structure for real-mode callbacks */
23 typedef struct
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;
44 extern void do_mscdex(struct sigcontext_struct *context);
46 /**********************************************************************
47 * INT_Int31Handler
49 * Handler for int 31h (DPMI).
51 void INT_Int31Handler( struct sigcontext_struct context )
53 DWORD dw;
54 BYTE *ptr;
56 RESET_CFLAG(&context);
57 switch(AX_reg(&context))
59 case 0x0000: /* Allocate LDT descriptors */
60 if (!(AX_reg(&context) = AllocSelectorArray( CX_reg(&context) )))
62 AX_reg(&context) = 0x8011; /* descriptor unavailable */
63 SET_CFLAG(&context);
65 break;
67 case 0x0001: /* Free LDT descriptor */
68 if (FreeSelector( BX_reg(&context) ))
70 AX_reg(&context) = 0x8022; /* invalid selector */
71 SET_CFLAG(&context);
73 break;
75 case 0x0002: /* Real mode segment to descriptor */
77 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
78 switch(BX_reg(&context))
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;
96 if (entryPoint)
97 AX_reg(&context) = LOWORD(MODULE_GetEntryPoint(
98 GetModuleHandle( "KERNEL" ),
99 entryPoint ));
101 break;
103 case 0x0003: /* Get next selector increment */
104 AX_reg(&context) = __AHINCR;
105 break;
107 case 0x0004: /* Lock selector (not supported) */
108 AX_reg(&context) = 0; /* FIXME: is this a correct return value? */
109 break;
111 case 0x0005: /* Unlock selector (not supported) */
112 AX_reg(&context) = 0; /* FIXME: is this a correct return value? */
113 break;
115 case 0x0006: /* Get selector base address */
116 if (!(dw = GetSelectorBase( BX_reg(&context) )))
118 AX_reg(&context) = 0x8022; /* invalid selector */
119 SET_CFLAG(&context);
121 else
123 CX_reg(&context) = HIWORD(dw);
124 DX_reg(&context) = LOWORD(dw);
126 break;
128 case 0x0007: /* Set selector base address */
129 SetSelectorBase( BX_reg(&context),
130 MAKELONG( DX_reg(&context), CX_reg(&context) ) );
131 break;
133 case 0x0008: /* Set selector limit */
134 SetSelectorLimit( BX_reg(&context),
135 MAKELONG( DX_reg(&context), CX_reg(&context) ) );
136 break;
138 case 0x0009: /* Set selector access rights */
139 SelectorAccessRights( BX_reg(&context), 1, CX_reg(&context) );
141 case 0x000a: /* Allocate selector alias */
142 if (!(AX_reg(&context) = AllocCStoDSAlias( BX_reg(&context) )))
144 AX_reg(&context) = 0x8011; /* descriptor unavailable */
145 SET_CFLAG(&context);
147 break;
149 case 0x000b: /* Get descriptor */
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 );
157 break;
159 case 0x000c: /* Set descriptor */
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 );
166 break;
168 case 0x000d: /* Allocate specific LDT descriptor */
169 AX_reg(&context) = 0x8011; /* descriptor unavailable */
170 SET_CFLAG(&context);
171 break;
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;
179 case 0x0205: /* Set protected mode interrupt vector */
180 INT_SetHandler( BL_reg(&context),
181 PTR_SEG_OFF_TO_SEGPTR(CX_reg(&context),DX_reg(&context)));
182 break;
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.
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 );
197 /* Compton's 1995 encyclopedia does its MSCDEX calls through
198 * this interrupt. Why? Who knows...
200 if ((BL_reg(&context) == 0x2f) && ((p->eax & 0xFF00) == 0x1500))
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);
214 else SET_CFLAG(&context);
216 break;
218 case 0x0301: /* Call real mode procedure with far return */
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);
228 break;
230 case 0x0302: /* Call real mode procedure with interrupt return */
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);
240 break;
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;
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;
255 case 0x0501: /* Allocate memory block */
256 if (!(ptr = (BYTE *)malloc( MAKELONG( CX_reg(&context),
257 BX_reg(&context) ) )))
259 AX_reg(&context) = 0x8012; /* linear memory not available */
260 SET_CFLAG(&context);
262 else
264 BX_reg(&context) = SI_reg(&context) = HIWORD(ptr);
265 CX_reg(&context) = DI_reg(&context) = LOWORD(ptr);
267 break;
269 case 0x0502: /* Free memory block */
270 free( (void *)MAKELONG( DI_reg(&context), SI_reg(&context) ) );
271 break;
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)))))
277 AX_reg(&context) = 0x8012; /* linear memory not available */
278 SET_CFLAG(&context);
280 else
282 BX_reg(&context) = SI_reg(&context) = HIWORD(ptr);
283 CX_reg(&context) = DI_reg(&context) = LOWORD(ptr);
285 break;
287 case 0x0600: /* Lock linear region */
288 break; /* Just ignore it */
290 case 0x0601: /* Unlock linear region */
291 break; /* Just ignore it */
293 case 0x0602: /* Unlock real-mode region */
294 break; /* Just ignore it */
296 case 0x0603: /* Lock real-mode region */
297 break; /* Just ignore it */
299 case 0x0604: /* Get page size */
300 BX_reg(&context) = 0;
301 CX_reg(&context) = 4096;
302 break;
304 case 0x0702: /* Mark page as demand-paging candidate */
305 break; /* Just ignore it */
307 case 0x0703: /* Discard page contents */
308 break; /* Just ignore it */
310 default:
311 INT_BARF( &context, 0x31 );
312 AX_reg(&context) = 0x8001; /* unsupported function */
313 SET_CFLAG(&context);
314 break;