Added David Luyer.
[wine/dcerpc.git] / msdos / dpmi.c
blob4d92546e883a01bbef60fde4d000a069c1b2af0c
1 /*
2 * DPMI 0.9 emulation
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <unistd.h>
8 #include <string.h>
9 #include "windows.h"
10 #include "heap.h"
11 #include "global.h"
12 #include "ldt.h"
13 #include "module.h"
14 #include "miscemu.h"
15 #include "drive.h"
16 #include "msdos.h"
17 #include "task.h"
18 #include "dosexe.h"
19 #include "toolhelp.h"
20 #include "debug.h"
21 #include "selectors.h"
22 #include "thread.h"
23 #include "process.h"
24 #include "stackframe.h"
25 #include "callback.h"
27 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
29 void CreateBPB(int drive, BYTE *data, BOOL16 limited); /* defined in int21.c */
31 static void* lastvalloced = NULL;
33 /* Structure for real-mode callbacks */
34 typedef struct
36 DWORD edi;
37 DWORD esi;
38 DWORD ebp;
39 DWORD reserved;
40 DWORD ebx;
41 DWORD edx;
42 DWORD ecx;
43 DWORD eax;
44 WORD fl;
45 WORD es;
46 WORD ds;
47 WORD fs;
48 WORD gs;
49 WORD ip;
50 WORD cs;
51 WORD sp;
52 WORD ss;
53 } REALMODECALL;
57 typedef struct tagRMCB {
58 DWORD address;
59 struct tagRMCB *next;
61 } RMCB;
63 static RMCB *FirstRMCB = NULL;
65 /**********************************************************************
66 * DPMI_xalloc
67 * special virtualalloc, allocates lineary monoton growing memory.
68 * (the usual VirtualAlloc does not satisfy that restriction)
70 static LPVOID
71 DPMI_xalloc(int len) {
72 LPVOID ret;
73 LPVOID oldlastv = lastvalloced;
75 if (lastvalloced) {
76 int xflag = 0;
77 ret = NULL;
78 while (!ret) {
79 ret=VirtualAlloc(lastvalloced,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
80 if (!ret)
81 lastvalloced+=0x10000;
82 /* we failed to allocate one in the first round.
83 * try non-linear
85 if (!xflag && (lastvalloced<oldlastv)) { /* wrapped */
86 FIXME(int31,"failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len);
87 xflag++;
89 /* if we even fail to allocate something in the next
90 * round, return NULL
92 if ((xflag==1) && (lastvalloced >= oldlastv))
93 xflag++;
94 if ((xflag==2) && (lastvalloced < oldlastv)) {
95 FIXME(int31,"failed to allocate any memory of %d bytes!\n",len);
96 return NULL;
99 } else
100 ret=VirtualAlloc(NULL,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
101 lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
102 return ret;
105 static void
106 DPMI_xfree(LPVOID ptr) {
107 VirtualFree(ptr,0,MEM_RELEASE);
110 /* FIXME: perhaps we could grow this mapped area... */
111 static LPVOID
112 DPMI_xrealloc(LPVOID ptr,int newsize) {
113 MEMORY_BASIC_INFORMATION mbi;
114 LPVOID newptr;
116 newptr = DPMI_xalloc(newsize);
117 if (ptr) {
118 if (!VirtualQuery(ptr,&mbi,sizeof(mbi))) {
119 FIXME(int31,"realloc of DPMI_xallocd region %p?\n",ptr);
120 return NULL;
122 if (mbi.State == MEM_FREE) {
123 FIXME(int31,"realloc of DPMI_xallocd region %p?\n",ptr);
124 return NULL;
126 /* We do not shrink allocated memory. most reallocs
127 * only do grows anyway
129 if (newsize<=mbi.RegionSize)
130 return ptr;
131 memcpy(newptr,ptr,mbi.RegionSize);
132 DPMI_xfree(ptr);
134 return newptr;
136 /**********************************************************************
137 * INT_GetRealModeContext
139 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT *context )
141 EAX_reg(context) = call->eax;
142 EBX_reg(context) = call->ebx;
143 ECX_reg(context) = call->ecx;
144 EDX_reg(context) = call->edx;
145 ESI_reg(context) = call->esi;
146 EDI_reg(context) = call->edi;
147 EBP_reg(context) = call->ebp;
148 EFL_reg(context) = call->fl | V86_FLAG;
149 EIP_reg(context) = call->ip;
150 ESP_reg(context) = call->sp;
151 CS_reg(context) = call->cs;
152 DS_reg(context) = call->ds;
153 ES_reg(context) = call->es;
154 FS_reg(context) = call->fs;
155 GS_reg(context) = call->gs;
156 (char*)V86BASE(context) = DOSMEM_MemoryBase(0);
160 /**********************************************************************
161 * INT_SetRealModeContext
163 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT *context )
165 call->eax = EAX_reg(context);
166 call->ebx = EBX_reg(context);
167 call->ecx = ECX_reg(context);
168 call->edx = EDX_reg(context);
169 call->esi = ESI_reg(context);
170 call->edi = EDI_reg(context);
171 call->ebp = EBP_reg(context);
172 call->fl = FL_reg(context);
173 call->ip = IP_reg(context);
174 call->sp = SP_reg(context);
175 call->cs = CS_reg(context);
176 call->ds = DS_reg(context);
177 call->es = ES_reg(context);
178 call->fs = FS_reg(context);
179 call->gs = GS_reg(context);
183 /**********************************************************************
184 * DPMI_CallRMProc
186 * This routine does the hard work of calling a real mode procedure.
188 int DPMI_CallRMProc( CONTEXT *context, LPWORD stack, int args, int iret )
190 LPWORD stack16;
191 THDB *thdb = THREAD_Current();
192 LPVOID addr;
193 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
194 NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
195 int alloc = 0;
196 WORD sel;
197 SEGPTR seg_addr;
199 GlobalUnlock16( GetCurrentTask() );
201 TRACE(int31, "EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
202 EAX_reg(context), EBX_reg(context), ECX_reg(context), EDX_reg(context) );
203 TRACE(int31, "ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments\n",
204 ESI_reg(context), EDI_reg(context), ES_reg(context), DS_reg(context),
205 CS_reg(context), IP_reg(context), args );
207 #ifdef MZ_SUPPORTED
208 FIXME(int31,"DPMI real-mode call using DOS VM task system, untested!\n");
209 if (!pModule->lpDosTask) {
210 TRACE(int31,"creating VM86 task\n");
211 if (MZ_InitTask( MZ_AllocDPMITask( pModule->self ) ) < 32) {
212 ERR(int31,"could not setup VM86 task\n");
213 return 1;
216 if (!SS_reg(context)) {
217 alloc = 1; /* allocate default stack */
218 stack16 = addr = DOSMEM_GetBlock( pModule->self, 64, &(SS_reg(context)) );
219 SP_reg(context) = 64-2;
220 if (!stack16) {
221 ERR(int31,"could not allocate default stack\n");
222 return 1;
224 } else {
225 stack16 = CTX_SEG_OFF_TO_LIN(context, SS_reg(context), SP_reg(context));
226 addr = NULL; /* avoid gcc warning */
228 SP_reg(context) -= args*sizeof(WORD) + (iret?1:0);
229 #else
230 stack16 = THREAD_STACK16(thdb);
231 #endif
232 stack16 -= args;
233 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
234 /* push flags if iret */
235 if (iret) {
236 stack16--; args++;
237 *stack16 = FL_reg(context);
239 #ifdef MZ_SUPPORTED
240 /* push return address (return to interrupt wrapper) */
241 *(--stack16) = pModule->lpDosTask->dpmi_seg;
242 *(--stack16) = pModule->lpDosTask->wrap_ofs;
243 /* push call address */
244 *(--stack16) = CS_reg(context);
245 *(--stack16) = IP_reg(context);
246 /* adjust stack */
247 SP_reg(context) -= 4*sizeof(WORD);
248 /* set initial CS:IP to the wrapper's "lret" */
249 CS_reg(context) = pModule->lpDosTask->dpmi_seg;
250 IP_reg(context) = pModule->lpDosTask->call_ofs;
251 TRACE(int31,"entering real mode...\n");
252 DOSVM_Enter( context );
253 TRACE(int31,"returned from real-mode call\n");
254 if (alloc) DOSMEM_FreeBlock( pModule->self, addr );
255 #else
256 addr = CTX_SEG_OFF_TO_LIN(context, CS_reg(context), IP_reg(context));
257 sel = SELECTOR_AllocBlock( addr, 0x10000, SEGMENT_CODE, FALSE, FALSE );
258 seg_addr = PTR_SEG_OFF_TO_SEGPTR( sel, 0 );
260 CS_reg(context) = HIWORD(seg_addr);
261 IP_reg(context) = LOWORD(seg_addr);
262 EBP_reg(context) = OFFSETOF( thdb->cur_stack )
263 + (WORD)&((STACK16FRAME*)0)->bp;
264 Callbacks->CallRegisterShortProc(context, args*sizeof(WORD));
265 UnMapLS(seg_addr);
266 #endif
267 return 0;
271 /**********************************************************************
272 * INT_DoRealModeInt
274 static void INT_DoRealModeInt( CONTEXT *context )
276 CONTEXT realmode_ctx;
277 FARPROC16 rm_int = INT_GetRMHandler( BL_reg(context) );
278 REALMODECALL *call = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context),
279 DI_reg(context) );
280 INT_GetRealModeContext( call, &realmode_ctx );
282 #ifdef MZ_SUPPORTED
283 /* we need to check if a real-mode program has hooked the interrupt */
284 if (HIWORD(rm_int)!=0xF000) {
285 /* yup, which means we need to switch to real mode... */
286 CS_reg(&realmode_ctx) = HIWORD(rm_int);
287 EIP_reg(&realmode_ctx) = LOWORD(rm_int);
288 if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
289 SET_CFLAG(context);
290 } else
291 #endif
293 RESET_CFLAG(context);
294 if (INT_RealModeInterrupt( BL_reg(context), &realmode_ctx ))
295 SET_CFLAG(context);
296 if (EFL_reg(context)&1) {
297 FIXME(int31,"%02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
298 BL_reg(context), EAX_reg(&realmode_ctx), EBX_reg(&realmode_ctx),
299 ECX_reg(&realmode_ctx), EDX_reg(&realmode_ctx));
300 FIXME(int31," ESI=%08lx EDI=%08lx DS=%04lx ES=%04lx\n",
301 ESI_reg(&realmode_ctx), EDI_reg(&realmode_ctx),
302 DS_reg(&realmode_ctx), ES_reg(&realmode_ctx) );
305 INT_SetRealModeContext( call, &realmode_ctx );
309 static void CallRMProcFar( CONTEXT *context )
311 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
312 CONTEXT context16;
313 THDB *thdb = THREAD_Current();
314 WORD argsize, sel;
315 LPVOID addr;
316 SEGPTR seg_addr;
318 TRACE(int31, "RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
319 p->eax, p->ebx, p->ecx, p->edx);
320 TRACE(int31, " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments\n",
321 p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context) );
323 if (!(p->cs) && !(p->ip)) { /* remove this check
324 if Int21/6501 case map function
325 has been implemented */
326 SET_CFLAG(context);
327 return;
329 INT_GetRealModeContext(p, &context16);
331 #if 0
332 addr = DOSMEM_MapRealToLinear(MAKELONG(p->ip, p->cs));
333 sel = SELECTOR_AllocBlock( addr, 0x10000, SEGMENT_CODE, FALSE, FALSE );
334 seg_addr = PTR_SEG_OFF_TO_SEGPTR( sel, 0 );
336 CS_reg(&context16) = HIWORD(seg_addr);
337 IP_reg(&context16) = LOWORD(seg_addr);
338 EBP_reg(&context16) = OFFSETOF( thdb->cur_stack )
339 + (WORD)&((STACK16FRAME*)0)->bp;
341 argsize = CX_reg(context)*sizeof(WORD);
342 memcpy( ((LPBYTE)THREAD_STACK16(thdb))-argsize,
343 (LPBYTE)PTR_SEG_OFF_TO_LIN(SS_reg(context), SP_reg(context))+6, argsize );
345 Callbacks->CallRegisterShortProc(&context16, argsize);
347 UnMapLS(seg_addr);
348 #else
349 DPMI_CallRMProc( &context16, ((LPWORD)PTR_SEG_OFF_TO_LIN(SS_reg(context), SP_reg(context)))+3,
350 CX_reg(context), 0 );
351 #endif
352 INT_SetRealModeContext(p, &context16);
356 void WINAPI RMCallbackProc( FARPROC16 pmProc, REALMODECALL *rmc )
358 CONTEXT ctx;
359 INT_GetRealModeContext(rmc, &ctx);
360 Callbacks->CallRegisterShortProc(&ctx, 0);
364 static void AllocRMCB( CONTEXT *context )
366 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
367 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
368 UINT16 uParagraph;
370 FIXME(int31, "EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n", p->eax, p->ebx, p->ecx, p->edx);
371 FIXME(int31, " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n", p->esi, p->edi, p->es, p->ds, p->cs, p->ip);
372 FIXME(int31, " Function to call: %04x:%04x\n",
373 (WORD)DS_reg(context), SI_reg(context) );
375 if (NewRMCB)
377 LPVOID RMCBmem = DOSMEM_GetBlock(0, 20, &uParagraph);
378 LPBYTE p = RMCBmem;
380 *p++ = 0x68; /* pushl */
381 *(FARPROC16 *)p =
382 PTR_SEG_OFF_TO_LIN(ES_reg(context), SI_reg(context)); /* pmode proc to call */
383 p+=4;
384 *p++ = 0x68; /* pushl */
385 *(LPVOID *)p =
386 PTR_SEG_OFF_TO_LIN(ES_reg(context), DI_reg(context));
387 p+=4;
388 *p++ = 0x9a; /* lcall */
389 *(FARPROC16 *)p = (FARPROC16)RMCallbackProc; /* FIXME ? */
390 p+=4;
391 GET_CS(*(WORD *)p);
392 p+=2;
393 *p++=0xc3; /* retf */
394 NewRMCB->address = MAKELONG(0, uParagraph);
395 NewRMCB->next = FirstRMCB;
396 FirstRMCB = NewRMCB;
397 CX_reg(context) = uParagraph;
398 DX_reg(context) = 0;
400 else
402 AX_reg(context) = 0x8015; /* callback unavailable */
403 SET_CFLAG(context);
408 static void FreeRMCB( CONTEXT *context )
410 RMCB *CurrRMCB = FirstRMCB;
411 RMCB *PrevRMCB = NULL;
413 FIXME(int31, "callback address: %04x:%04x\n",
414 CX_reg(context), DX_reg(context));
416 while (CurrRMCB && (CurrRMCB->address != MAKELONG(DX_reg(context), CX_reg(context))))
418 PrevRMCB = CurrRMCB;
419 CurrRMCB = CurrRMCB->next;
421 if (CurrRMCB)
423 if (PrevRMCB)
424 PrevRMCB->next = CurrRMCB->next;
425 else
426 FirstRMCB = CurrRMCB->next;
427 DOSMEM_FreeBlock(0, DOSMEM_MapRealToLinear(CurrRMCB->address));
428 HeapFree(GetProcessHeap(), 0, CurrRMCB);
430 else
432 AX_reg(context) = 0x8024; /* invalid callback address */
433 SET_CFLAG(context);
437 #ifdef MZ_SUPPORTED
438 /* (see loader/dos/module.c, function MZ_InitDPMI) */
440 static void StartPM( CONTEXT *context, LPDOSTASK lpDosTask )
442 char *base = DOSMEM_MemoryBase(0);
443 UINT16 cs, ss, ds, es;
444 CONTEXT pm_ctx;
446 RESET_CFLAG(context);
447 lpDosTask->dpmi_flag = AX_reg(context);
448 /* our mode switch wrapper have placed the desired CS into DX */
449 cs = SELECTOR_AllocBlock( base + (DWORD)(DX_reg(context)<<4), 0x10000, SEGMENT_CODE, FALSE, FALSE );
450 ss = SELECTOR_AllocBlock( base + (DWORD)(SS_reg(context)<<4), 0x10000, SEGMENT_DATA, FALSE, FALSE );
451 ds = SELECTOR_AllocBlock( base + (DWORD)(DS_reg(context)<<4), 0x10000, SEGMENT_DATA, FALSE, FALSE );
452 es = SELECTOR_AllocBlock( base + (DWORD)(lpDosTask->psp_seg<<4), 0x100, SEGMENT_DATA, FALSE, FALSE );
454 pm_ctx = *context;
455 CS_reg(&pm_ctx) = lpDosTask->dpmi_sel;
456 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
457 AX_reg(&pm_ctx) = ss;
458 DX_reg(&pm_ctx) = cs;
459 DS_reg(&pm_ctx) = ds;
460 ES_reg(&pm_ctx) = es;
461 FS_reg(&pm_ctx) = 0;
462 GS_reg(&pm_ctx) = 0;
464 TRACE(int31,"DOS program is now entering protected mode\n");
465 Callbacks->CallRegisterShortProc(&pm_ctx, 0);
467 /* in the current state of affairs, we won't ever actually return here... */
469 UnMapLS(PTR_SEG_OFF_TO_SEGPTR(es,0));
470 UnMapLS(PTR_SEG_OFF_TO_SEGPTR(ds,0));
471 UnMapLS(PTR_SEG_OFF_TO_SEGPTR(ss,0));
472 UnMapLS(PTR_SEG_OFF_TO_SEGPTR(cs,0));
474 #endif
476 /**********************************************************************
477 * INT_Int31Handler
479 * Handler for int 31h (DPMI).
482 void WINAPI INT_Int31Handler( CONTEXT *context )
485 * Note: For Win32s processes, the whole linear address space is
486 * shifted by 0x10000 relative to the OS linear address space.
487 * See the comment in msdos/vxd.c.
489 DWORD offset = PROCESS_Current()->flags & PDB32_WIN32S_PROC ? 0x10000 : 0;
490 #define AppToWine(addr) ((addr)? ((DWORD)(addr)) + offset : 0)
491 #define WineToApp(addr) ((addr)? ((DWORD)(addr)) - offset : 0)
493 DWORD dw;
494 BYTE *ptr;
496 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
497 NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
499 GlobalUnlock16( GetCurrentTask() );
501 #ifdef MZ_SUPPORTED
502 if (ISV86(context) && pModule && pModule->lpDosTask) {
503 /* Called from real mode, check if it's our wrapper */
504 TRACE(int31,"called from real mode\n");
505 if (CS_reg(context)==pModule->lpDosTask->dpmi_seg) {
506 /* This is the protected mode switch */
507 StartPM(context,pModule->lpDosTask);
508 return;
509 } else
510 if (CS_reg(context)==pModule->lpDosTask->xms_seg) {
511 /* This is the XMS driver entry point */
512 XMS_Handler(context);
513 return;
516 #endif
518 RESET_CFLAG(context);
519 switch(AX_reg(context))
521 case 0x0000: /* Allocate LDT descriptors */
522 TRACE(int31,"allocate LDT descriptors (%d)\n",CX_reg(context));
523 if (!(AX_reg(context) = AllocSelectorArray( CX_reg(context) )))
525 TRACE(int31,"failed\n");
526 AX_reg(context) = 0x8011; /* descriptor unavailable */
527 SET_CFLAG(context);
529 TRACE(int31,"success, array starts at 0x%04x\n",AX_reg(context));
530 break;
532 case 0x0001: /* Free LDT descriptor */
533 TRACE(int31,"free LDT descriptor (0x%04x)\n",BX_reg(context));
534 if (FreeSelector( BX_reg(context) ))
536 AX_reg(context) = 0x8022; /* invalid selector */
537 SET_CFLAG(context);
539 else
541 /* If a segment register contains the selector being freed, */
542 /* set it to zero. */
543 if (!((DS_reg(context)^BX_reg(context)) & ~3)) DS_reg(context) = 0;
544 if (!((ES_reg(context)^BX_reg(context)) & ~3)) ES_reg(context) = 0;
545 if (!((FS_reg(context)^BX_reg(context)) & ~3)) FS_reg(context) = 0;
546 if (!((GS_reg(context)^BX_reg(context)) & ~3)) GS_reg(context) = 0;
548 break;
550 case 0x0002: /* Real mode segment to descriptor */
551 TRACE(int31,"real mode segment to descriptor (0x%04x)\n",BX_reg(context));
553 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
554 switch(BX_reg(context))
556 case 0x0000: entryPoint = 183; break; /* __0000H */
557 case 0x0040: entryPoint = 193; break; /* __0040H */
558 case 0xa000: entryPoint = 174; break; /* __A000H */
559 case 0xb000: entryPoint = 181; break; /* __B000H */
560 case 0xb800: entryPoint = 182; break; /* __B800H */
561 case 0xc000: entryPoint = 195; break; /* __C000H */
562 case 0xd000: entryPoint = 179; break; /* __D000H */
563 case 0xe000: entryPoint = 190; break; /* __E000H */
564 case 0xf000: entryPoint = 194; break; /* __F000H */
565 default:
566 AX_reg(context) = DOSMEM_AllocSelector(BX_reg(context));
567 break;
569 if (entryPoint)
570 AX_reg(context) = LOWORD(NE_GetEntryPoint(
571 GetModuleHandle16( "KERNEL" ),
572 entryPoint ));
574 break;
576 case 0x0003: /* Get next selector increment */
577 TRACE(int31,"get selector increment (__AHINCR)\n");
578 AX_reg(context) = __AHINCR;
579 break;
581 case 0x0004: /* Lock selector (not supported) */
582 FIXME(int31,"lock selector not supported\n");
583 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
584 break;
586 case 0x0005: /* Unlock selector (not supported) */
587 FIXME(int31,"unlock selector not supported\n");
588 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
589 break;
591 case 0x0006: /* Get selector base address */
592 TRACE(int31,"get selector base address (0x%04x)\n",BX_reg(context));
593 if (!(dw = GetSelectorBase( BX_reg(context) )))
595 AX_reg(context) = 0x8022; /* invalid selector */
596 SET_CFLAG(context);
598 else
600 #ifdef MZ_SUPPORTED
601 if (pModule && pModule->lpDosTask) {
602 DWORD base = (DWORD)DOSMEM_MemoryBase(pModule->self);
603 if ((dw >= base) && (dw < base + 0x110000)) dw -= base;
605 #endif
606 CX_reg(context) = HIWORD(WineToApp(dw));
607 DX_reg(context) = LOWORD(WineToApp(dw));
609 break;
611 case 0x0007: /* Set selector base address */
612 TRACE(int31, "set selector base address (0x%04x,0x%08lx)\n",
613 BX_reg(context),
614 AppToWine(MAKELONG(DX_reg(context),CX_reg(context))));
615 dw = AppToWine(MAKELONG(DX_reg(context), CX_reg(context)));
616 #ifdef MZ_SUPPORTED
617 /* well, what else could we possibly do? */
618 if (pModule && pModule->lpDosTask) {
619 if (dw < 0x110000) dw += (DWORD)DOSMEM_MemoryBase(pModule->self);
621 #endif
622 SetSelectorBase(BX_reg(context), dw);
623 break;
625 case 0x0008: /* Set selector limit */
626 TRACE(int31,"set selector limit (0x%04x,0x%08lx)\n",BX_reg(context),MAKELONG(DX_reg(context),CX_reg(context)));
627 dw = MAKELONG( DX_reg(context), CX_reg(context) );
628 #ifdef MZ_SUPPORTED
629 if (pModule && pModule->lpDosTask) {
630 DWORD base = GetSelectorBase( BX_reg(context) );
631 if ((dw == 0xffffffff) || ((base < 0x110000) && (base + dw > 0x110000))) {
632 AX_reg(context) = 0x8021; /* invalid value */
633 SET_CFLAG(context);
634 break;
637 #endif
638 SetSelectorLimit( BX_reg(context), dw );
639 break;
641 case 0x0009: /* Set selector access rights */
642 TRACE(int31,"set selector access rights(0x%04x,0x%04x)\n",BX_reg(context),CX_reg(context));
643 SelectorAccessRights( BX_reg(context), 1, CX_reg(context) );
644 break;
646 case 0x000a: /* Allocate selector alias */
647 TRACE(int31,"allocate selector alias (0x%04x)\n",BX_reg(context));
648 if (!(AX_reg(context) = AllocCStoDSAlias( BX_reg(context) )))
650 AX_reg(context) = 0x8011; /* descriptor unavailable */
651 SET_CFLAG(context);
653 break;
655 case 0x000b: /* Get descriptor */
656 TRACE(int31,"get descriptor (0x%04x)\n",BX_reg(context));
658 ldt_entry entry;
659 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
660 entry.base = WineToApp(entry.base);
662 /* FIXME: should use ES:EDI for 32-bit clients */
663 LDT_EntryToBytes( PTR_SEG_OFF_TO_LIN( ES_reg(context),
664 DI_reg(context) ), &entry );
666 break;
668 case 0x000c: /* Set descriptor */
669 TRACE(int31,"set descriptor (0x%04x)\n",BX_reg(context));
671 ldt_entry entry;
672 LDT_BytesToEntry( PTR_SEG_OFF_TO_LIN( ES_reg(context),
673 DI_reg(context) ), &entry );
674 entry.base = AppToWine(entry.base);
676 LDT_SetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
678 break;
680 case 0x000d: /* Allocate specific LDT descriptor */
681 FIXME(int31,"allocate descriptor (0x%04x), stub!\n",BX_reg(context));
682 AX_reg(context) = 0x8011; /* descriptor unavailable */
683 SET_CFLAG(context);
684 break;
685 case 0x0100: /* Allocate DOS memory block */
686 TRACE(int31,"allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context));
687 dw = GlobalDOSAlloc((DWORD)BX_reg(context)<<4);
688 if (dw) {
689 AX_reg(context) = HIWORD(dw);
690 DX_reg(context) = LOWORD(dw);
691 } else {
692 AX_reg(context) = 0x0008; /* insufficient memory */
693 BX_reg(context) = DOSMEM_Available(0)>>4;
694 SET_CFLAG(context);
696 break;
697 case 0x0101: /* Free DOS memory block */
698 TRACE(int31,"free DOS memory block (0x%04x)\n",DX_reg(context));
699 dw = GlobalDOSFree(DX_reg(context));
700 if (!dw) {
701 AX_reg(context) = 0x0009; /* memory block address invalid */
702 SET_CFLAG(context);
704 break;
705 case 0x0200: /* get real mode interrupt vector */
706 FIXME(int31,"get realmode interupt vector(0x%02x) unimplemented.\n",
707 BL_reg(context));
708 SET_CFLAG(context);
709 break;
710 case 0x0201: /* set real mode interrupt vector */
711 FIXME(int31, "set realmode interupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context),CX_reg(context),DX_reg(context));
712 SET_CFLAG(context);
713 break;
714 case 0x0204: /* Get protected mode interrupt vector */
715 TRACE(int31,"get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context));
716 dw = (DWORD)INT_GetPMHandler( BL_reg(context) );
717 CX_reg(context) = HIWORD(dw);
718 DX_reg(context) = LOWORD(dw);
719 break;
721 case 0x0205: /* Set protected mode interrupt vector */
722 TRACE(int31,"set protected mode interrupt handler (0x%02x,%p), stub!\n",
723 BL_reg(context),PTR_SEG_OFF_TO_LIN(CX_reg(context),DX_reg(context)));
724 INT_SetPMHandler( BL_reg(context),
725 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( CX_reg(context),
726 DX_reg(context) ));
727 break;
729 case 0x0300: /* Simulate real mode interrupt */
730 INT_DoRealModeInt( context );
731 break;
733 case 0x0301: /* Call real mode procedure with far return */
734 CallRMProcFar( context );
735 break;
737 case 0x0302: /* Call real mode procedure with interrupt return */
739 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
740 FIXME(int31, "RealModeCallIret: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n", p->eax, p->ebx, p->ecx, p->edx);
741 FIXME(int31, " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n", p->esi, p->edi, p->es, p->ds, p->cs, p->ip );
742 SET_CFLAG(context);
744 break;
746 case 0x0303: /* Allocate Real Mode Callback Address */
747 AllocRMCB( context );
748 break;
750 case 0x0304: /* Free Real Mode Callback Address */
751 FreeRMCB( context );
752 break;
754 case 0x0400: /* Get DPMI version */
755 TRACE(int31,"get DPMI version\n");
757 SYSTEM_INFO si;
759 GetSystemInfo(&si);
760 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
761 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
762 CL_reg(context) = si.wProcessorLevel;
763 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
764 break;
766 case 0x0500: /* Get free memory information */
767 TRACE(int31,"get free memory information\n");
769 MEMMANINFO mmi;
771 mmi.dwSize = sizeof(mmi);
772 MemManInfo(&mmi);
773 ptr = (BYTE *)PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context));
774 /* the layout is just the same as MEMMANINFO, but without
775 * the dwSize entry.
777 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
778 break;
780 case 0x0501: /* Allocate memory block */
781 TRACE(int31,"allocate memory block (%ld)\n",MAKELONG(CX_reg(context),BX_reg(context)));
782 if (!(ptr = (BYTE *)DPMI_xalloc(MAKELONG(CX_reg(context), BX_reg(context)))))
784 AX_reg(context) = 0x8012; /* linear memory not available */
785 SET_CFLAG(context);
786 } else {
787 BX_reg(context) = SI_reg(context) = HIWORD(WineToApp(ptr));
788 CX_reg(context) = DI_reg(context) = LOWORD(WineToApp(ptr));
790 break;
792 case 0x0502: /* Free memory block */
793 TRACE(int31, "free memory block (0x%08lx)\n",
794 AppToWine(MAKELONG(DI_reg(context),SI_reg(context))));
795 DPMI_xfree( (void *)AppToWine(MAKELONG(DI_reg(context),
796 SI_reg(context))) );
797 break;
799 case 0x0503: /* Resize memory block */
800 TRACE(int31, "resize memory block (0x%08lx,%ld)\n",
801 AppToWine(MAKELONG(DI_reg(context),SI_reg(context))),
802 MAKELONG(CX_reg(context),BX_reg(context)));
803 if (!(ptr = (BYTE *)DPMI_xrealloc(
804 (void *)AppToWine(MAKELONG(DI_reg(context),SI_reg(context))),
805 MAKELONG(CX_reg(context),BX_reg(context)))))
807 AX_reg(context) = 0x8012; /* linear memory not available */
808 SET_CFLAG(context);
809 } else {
810 BX_reg(context) = SI_reg(context) = HIWORD(WineToApp(ptr));
811 CX_reg(context) = DI_reg(context) = LOWORD(WineToApp(ptr));
813 break;
815 case 0x0507: /* Modify page attributes */
816 FIXME(int31,"modify page attributes unimplemented\n");
817 break; /* Just ignore it */
819 case 0x0600: /* Lock linear region */
820 FIXME(int31,"lock linear region unimplemented\n");
821 break; /* Just ignore it */
823 case 0x0601: /* Unlock linear region */
824 FIXME(int31,"unlock linear region unimplemented\n");
825 break; /* Just ignore it */
827 case 0x0602: /* Unlock real-mode region */
828 FIXME(int31,"unlock realmode region unimplemented\n");
829 break; /* Just ignore it */
831 case 0x0603: /* Lock real-mode region */
832 FIXME(int31,"lock realmode region unimplemented\n");
833 break; /* Just ignore it */
835 case 0x0604: /* Get page size */
836 TRACE(int31,"get pagesize\n");
837 BX_reg(context) = 0;
838 CX_reg(context) = VIRTUAL_GetPageSize();
839 break;
841 case 0x0702: /* Mark page as demand-paging candidate */
842 FIXME(int31,"mark page as demand-paging candidate\n");
843 break; /* Just ignore it */
845 case 0x0703: /* Discard page contents */
846 FIXME(int31,"discard page contents\n");
847 break; /* Just ignore it */
849 case 0x0800: /* Physical address mapping */
850 FIXME(int31,"map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context),BX_reg(context)));
851 if(!(ptr=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context),BX_reg(context)))))
853 AX_reg(context) = 0x8021;
854 SET_CFLAG(context);
856 else
858 BX_reg(context) = HIWORD(WineToApp(ptr));
859 CX_reg(context) = LOWORD(WineToApp(ptr));
860 RESET_CFLAG(context);
862 break;
864 default:
865 INT_BARF( context, 0x31 );
866 AX_reg(context) = 0x8001; /* unsupported function */
867 SET_CFLAG(context);
868 break;
871 #undef AppToWine
872 #undef WineToApp