Release 960114
[wine/gsoc-2012-control.git] / miscemu / dpmi.c
blob760be10304d6bddc9b6d7d86d51f817b5e6f18fb
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 "registers.h"
14 #include "wine.h"
15 #include "miscemu.h"
16 #include "stddebug.h"
17 /* #define DEBUG_INT */
18 #include "debug.h"
21 /* Structure for real-mode callbacks */
22 typedef struct
24 DWORD edi;
25 DWORD esi;
26 DWORD ebp;
27 DWORD reserved;
28 DWORD ebx;
29 DWORD edx;
30 DWORD ecx;
31 DWORD eax;
32 WORD flags;
33 WORD es;
34 WORD ds;
35 WORD fs;
36 WORD gs;
37 WORD ip;
38 WORD cs;
39 WORD sp;
40 WORD ss;
41 } REALMODECALL;
43 /**********************************************************************
44 * INT_Int31Handler
46 * Handler for int 31h (DPMI).
48 void INT_Int31Handler( struct sigcontext_struct context )
50 DWORD dw;
51 BYTE *ptr;
53 RESET_CFLAG(&context);
54 switch(AX_reg(&context))
56 case 0x0000: /* Allocate LDT descriptors */
57 if (!(AX_reg(&context) = AllocSelectorArray( CX_reg(&context) )))
59 AX_reg(&context) = 0x8011; /* descriptor unavailable */
60 SET_CFLAG(&context);
62 break;
64 case 0x0001: /* Free LDT descriptor */
65 if (FreeSelector( BX_reg(&context) ))
67 AX_reg(&context) = 0x8022; /* invalid selector */
68 SET_CFLAG(&context);
70 break;
72 case 0x0003: /* Get next selector increment */
73 AX_reg(&context) = __AHINCR;
74 break;
76 case 0x0004: /* Lock selector (not supported) */
77 AX_reg(&context) = 0; /* FIXME: is this a correct return value? */
78 break;
80 case 0x0005: /* Unlock selector (not supported) */
81 AX_reg(&context) = 0; /* FIXME: is this a correct return value? */
82 break;
84 case 0x0006: /* Get selector base address */
85 if (!(dw = GetSelectorBase( BX_reg(&context) )))
87 AX_reg(&context) = 0x8022; /* invalid selector */
88 SET_CFLAG(&context);
90 else
92 CX_reg(&context) = HIWORD(dw);
93 DX_reg(&context) = LOWORD(dw);
95 break;
97 case 0x0007: /* Set selector base address */
98 SetSelectorBase( BX_reg(&context),
99 MAKELONG( DX_reg(&context), CX_reg(&context) ) );
100 break;
102 case 0x0008: /* Set selector limit */
103 SetSelectorLimit( BX_reg(&context),
104 MAKELONG( DX_reg(&context), CX_reg(&context) ) );
105 break;
107 case 0x0009: /* Set selector access rights */
108 SelectorAccessRights( BX_reg(&context), 1, CX_reg(&context) );
110 case 0x000a: /* Allocate selector alias */
111 if (!(AX_reg(&context) = AllocCStoDSAlias( BX_reg(&context) )))
113 AX_reg(&context) = 0x8011; /* descriptor unavailable */
114 SET_CFLAG(&context);
116 break;
118 case 0x000b: /* Get descriptor */
120 ldt_entry entry;
121 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(&context) ), &entry );
122 /* FIXME: should use ES:EDI for 32-bit clients */
123 LDT_EntryToBytes( PTR_SEG_OFF_TO_LIN( ES_reg(&context),
124 DI_reg(&context) ), &entry );
126 break;
128 case 0x000c: /* Set descriptor */
130 ldt_entry entry;
131 LDT_BytesToEntry( PTR_SEG_OFF_TO_LIN( ES_reg(&context),
132 DI_reg(&context) ), &entry );
133 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(&context) ), &entry );
135 break;
137 case 0x000d: /* Allocate specific LDT descriptor */
138 AX_reg(&context) = 0x8011; /* descriptor unavailable */
139 SET_CFLAG(&context);
140 break;
142 case 0x0204: /* Get protected mode interrupt vector */
143 dw = (DWORD)INT_GetHandler( BL_reg(&context) );
144 CX_reg(&context) = HIWORD(dw);
145 DX_reg(&context) = LOWORD(dw);
146 break;
148 case 0x0205: /* Set protected mode interrupt vector */
149 INT_SetHandler( BL_reg(&context),
150 (SEGPTR)MAKELONG( DX_reg(&context), CX_reg(&context) ));
151 break;
153 case 0x0300: /* Simulate real mode interrupt
154 * Interrupt number is in BL, flags are in BH
155 * ES:DI points to real-mode call structure
156 * Currently we just print it out and return error.
159 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(&context), DI_reg(&context) );
160 fprintf(stdnimp,
161 "RealModeInt %02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
162 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x\n",
163 BL_reg(&context), p->eax, p->ebx, p->ecx, p->edx,
164 p->esi, p->edi, p->es, p->ds );
165 SET_CFLAG(&context);
167 break;
169 case 0x0301: /* Call real mode procedure with far return */
171 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(&context), DI_reg(&context) );
172 fprintf(stdnimp,
173 "RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
174 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n",
175 p->eax, p->ebx, p->ecx, p->edx,
176 p->esi, p->edi, p->es, p->ds, p->cs, p->ip );
177 SET_CFLAG(&context);
179 break;
181 case 0x0302: /* Call real mode procedure with interrupt return */
183 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(&context), DI_reg(&context) );
184 fprintf(stdnimp,
185 "RealModeCallIret: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n"
186 " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n",
187 p->eax, p->ebx, p->ecx, p->edx,
188 p->esi, p->edi, p->es, p->ds, p->cs, p->ip );
189 SET_CFLAG(&context);
191 break;
193 case 0x0400: /* Get DPMI version */
194 AX_reg(&context) = 0x005a; /* DPMI version 0.90 */
195 BX_reg(&context) = 0x0005; /* Flags: 32-bit, virtual memory */
196 CL_reg(&context) = runtime_cpu ();
197 DX_reg(&context) = 0x0102; /* Master/slave interrupt controller base*/
198 break;
200 case 0x0500: /* Get free memory information */
201 ptr = (BYTE *)PTR_SEG_OFF_TO_LIN( ES_reg(&context), DI_reg(&context) );
202 *(DWORD *)ptr = 0x00ff0000; /* Largest block available */
203 memset( ptr + 4, 0xff, 0x2c ); /* No other information supported */
204 break;
206 case 0x0501: /* Allocate memory block */
207 if (!(ptr = (BYTE *)malloc( MAKELONG( CX_reg(&context),
208 BX_reg(&context) ) )))
210 AX_reg(&context) = 0x8012; /* linear memory not available */
211 SET_CFLAG(&context);
213 else
215 BX_reg(&context) = SI_reg(&context) = HIWORD(ptr);
216 CX_reg(&context) = DI_reg(&context) = LOWORD(ptr);
218 break;
220 case 0x0502: /* Free memory block */
221 free( (void *)MAKELONG( DI_reg(&context), SI_reg(&context) ) );
222 break;
224 case 0x0503: /* Resize memory block */
225 if (!(ptr = (BYTE *)realloc( (void *)MAKELONG(DI_reg(&context),SI_reg(&context)),
226 MAKELONG(CX_reg(&context),BX_reg(&context)))))
228 AX_reg(&context) = 0x8012; /* linear memory not available */
229 SET_CFLAG(&context);
231 else
233 BX_reg(&context) = SI_reg(&context) = HIWORD(ptr);
234 CX_reg(&context) = DI_reg(&context) = LOWORD(ptr);
236 break;
238 case 0x0600: /* Lock linear region */
239 break; /* Just ignore it */
241 case 0x0601: /* Unlock linear region */
242 break; /* Just ignore it */
244 case 0x0604: /* Get page size */
245 BX_reg(&context) = 0;
246 CX_reg(&context) = 4096;
247 break;
249 default:
250 INT_BARF( &context, 0x31 );
251 AX_reg(&context) = 0x8001; /* unsupported function */
252 SET_CFLAG(&context);
253 break;