4 * Copyright 1995 Alexandre Julliard
12 #include "wine/winbase16.h"
14 #include "builtin16.h"
21 #include "selectors.h"
24 #include "debugtools.h"
26 DEFAULT_DEBUG_CHANNEL(int31);
28 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
30 void CreateBPB(int drive, BYTE *data, BOOL16 limited); /* defined in int21.c */
32 static void* lastvalloced = NULL;
34 /* Structure for real-mode callbacks */
58 typedef struct tagRMCB {
60 DWORD proc_ofs,proc_sel;
61 DWORD regs_ofs,regs_sel;
65 static RMCB *FirstRMCB = NULL;
68 /**********************************************************************
70 * special virtualalloc, allocates lineary monoton growing memory.
71 * (the usual VirtualAlloc does not satisfy that restriction)
74 DPMI_xalloc(int len) {
76 LPVOID oldlastv = lastvalloced;
82 ret=VirtualAlloc(lastvalloced,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
84 lastvalloced = (char *) lastvalloced + 0x10000;
85 /* we failed to allocate one in the first round.
88 if (!xflag && (lastvalloced<oldlastv)) { /* wrapped */
89 FIXME("failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len);
92 /* if we even fail to allocate something in the next
95 if ((xflag==1) && (lastvalloced >= oldlastv))
97 if ((xflag==2) && (lastvalloced < oldlastv)) {
98 FIXME("failed to allocate any memory of %d bytes!\n",len);
103 ret=VirtualAlloc(NULL,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
104 lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
109 DPMI_xfree(LPVOID ptr) {
110 VirtualFree(ptr,0,MEM_RELEASE);
113 /* FIXME: perhaps we could grow this mapped area... */
115 DPMI_xrealloc(LPVOID ptr,DWORD newsize) {
116 MEMORY_BASIC_INFORMATION mbi;
119 newptr = DPMI_xalloc(newsize);
121 if (!VirtualQuery(ptr,&mbi,sizeof(mbi))) {
122 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
125 if (mbi.State == MEM_FREE) {
126 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
129 /* We do not shrink allocated memory. most reallocs
130 * only do grows anyway
132 if (newsize<=mbi.RegionSize)
134 memcpy(newptr,ptr,mbi.RegionSize);
139 /**********************************************************************
140 * INT_GetRealModeContext
142 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
144 context->Eax = call->eax;
145 context->Ebx = call->ebx;
146 context->Ecx = call->ecx;
147 context->Edx = call->edx;
148 context->Esi = call->esi;
149 context->Edi = call->edi;
150 context->Ebp = call->ebp;
151 context->EFlags = call->fl | V86_FLAG;
152 context->Eip = call->ip;
153 context->Esp = call->sp;
154 context->SegCs = call->cs;
155 context->SegDs = call->ds;
156 context->SegEs = call->es;
157 context->SegFs = call->fs;
158 context->SegGs = call->gs;
159 context->SegSs = call->ss;
163 /**********************************************************************
164 * INT_SetRealModeContext
166 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
168 call->eax = context->Eax;
169 call->ebx = context->Ebx;
170 call->ecx = context->Ecx;
171 call->edx = context->Edx;
172 call->esi = context->Esi;
173 call->edi = context->Edi;
174 call->ebp = context->Ebp;
175 call->fl = LOWORD(context->EFlags);
176 call->ip = LOWORD(context->Eip);
177 call->sp = LOWORD(context->Esp);
178 call->cs = context->SegCs;
179 call->ds = context->SegDs;
180 call->es = context->SegEs;
181 call->fs = context->SegFs;
182 call->gs = context->SegGs;
183 call->ss = context->SegSs;
188 void DPMI_CallRMCB32(RMCB *rmcb, UINT16 ss, DWORD esp, UINT16*es, DWORD*edi)
189 #if 0 /* original code, which early gccs puke on */
192 __asm__ __volatile__(
200 ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
206 : "=d" (*es), "=D" (*edi), "=S" (_clobber), "=a" (_clobber), "=c" (_clobber)
207 : "0" (ss), "2" (esp),
208 "4" (rmcb->regs_sel), "1" (rmcb->regs_ofs),
209 "3" (&rmcb->proc_ofs) );
211 #else /* code generated by a gcc new enough */
213 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32,
218 "movl 0x8(%ebp),%eax\n\t"
219 "movl 0x10(%ebp),%esi\n\t"
220 "movl 0xc(%ebp),%edx\n\t"
221 "movl 0x10(%eax),%ecx\n\t"
222 "movl 0xc(%eax),%edi\n\t"
231 ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
237 "movl 0x14(%ebp),%eax\n\t"
238 "movw %dx,(%eax)\n\t"
239 "movl 0x18(%ebp),%edx\n\t"
240 "movl %edi,(%edx)\n\t"
247 #endif /* __i386__ */
249 /**********************************************************************
252 * This routine does the hard work of calling a callback procedure.
254 static void DPMI_CallRMCBProc( CONTEXT86 *context, RMCB *rmcb, WORD flag )
256 if (IS_SELECTOR_SYSTEM( rmcb->proc_sel )) {
257 /* Wine-internal RMCB, call directly */
258 ((RMCBPROC)rmcb->proc_ofs)(context);
264 INT_SetRealModeContext((REALMODECALL *)PTR_SEG_OFF_TO_LIN( rmcb->regs_sel, rmcb->regs_ofs ), context);
265 ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, WINE_LDT_FLAGS_DATA );
268 FIXME("untested!\n");
270 /* The called proc ends with an IRET, and takes these parameters:
271 * DS:ESI = pointer to real-mode SS:SP
272 * ES:EDI = pointer to real-mode call structure
274 * ES:EDI = pointer to real-mode call structure (may be a copy)
275 * It is the proc's responsibility to change the return CS:IP in the
276 * real-mode call structure. */
278 /* 32-bit DPMI client */
279 DPMI_CallRMCB32(rmcb, ss, esp, &es, &edi);
281 /* 16-bit DPMI client */
282 CONTEXT86 ctx = *context;
283 ctx.SegCs = rmcb->proc_sel;
284 ctx.Eip = rmcb->proc_ofs;
287 ctx.SegEs = rmcb->regs_sel;
288 ctx.Edi = rmcb->regs_ofs;
289 /* FIXME: I'm pretty sure this isn't right - should push flags first */
290 CallTo16RegisterShort(&ctx, 0);
295 INT_GetRealModeContext((REALMODECALL*)PTR_SEG_OFF_TO_LIN( es, edi ), context);
297 ERR("RMCBs only implemented for i386\n");
303 /**********************************************************************
306 * This routine does the hard work of calling a real mode procedure.
308 int DPMI_CallRMProc( CONTEXT86 *context, LPWORD stack, int args, int iret )
311 LPVOID addr = NULL; /* avoid gcc warning */
312 LPDOSTASK lpDosTask = MZ_Current();
314 int alloc = 0, already = 0;
317 GlobalUnlock16( GetCurrentTask() );
319 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
320 context->Eax, context->Ebx, context->Ecx, context->Edx );
321 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
322 context->Esi, context->Edi, context->SegEs, context->SegDs,
323 context->SegCs, LOWORD(context->Eip), args, iret?"IRET":"FAR" );
327 /* there might be some code that just jumps to RMCBs or the like,
328 in which case following the jumps here might get us to a shortcut */
329 code = CTX_SEG_OFF_TO_LIN(context, context->SegCs, context->Eip);
331 case 0xe9: /* JMP NEAR */
332 context->Eip += 3 + *(WORD *)(code+1);
333 /* yeah, I know these gotos don't look good... */
334 goto callrmproc_again;
335 case 0xea: /* JMP FAR */
336 context->Eip = *(WORD *)(code+1);
337 context->SegCs = *(WORD *)(code+3);
338 /* ...but since the label is there anyway... */
339 goto callrmproc_again;
340 case 0xeb: /* JMP SHORT */
341 context->Eip += 2 + *(signed char *)(code+1);
342 /* ...because of other gotos below, so... */
343 goto callrmproc_again;
346 /* shortcut for chaining to internal interrupt handlers */
347 if ((context->SegCs == 0xF000) && iret) {
348 return INT_RealModeInterrupt( LOWORD(context->Eip)/4, context);
351 /* shortcut for RMCBs */
352 CurrRMCB = FirstRMCB;
354 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
355 CurrRMCB = CurrRMCB->next;
357 if (!(CurrRMCB || lpDosTask)) {
358 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
359 TRACE("creating VM86 task\n");
360 if (!(lpDosTask = MZ_AllocDPMITask() )) {
361 ERR("could not setup VM86 task\n");
366 if (!context->SegSs) {
367 alloc = 1; /* allocate default stack */
368 stack16 = addr = DOSMEM_GetBlock( 64, (UINT16 *)&(context->SegSs) );
372 ERR("could not allocate default stack\n");
376 stack16 = CTX_SEG_OFF_TO_LIN(context, context->SegSs, context->Esp);
378 context->Esp -= (args + (iret?1:0)) * sizeof(WORD);
380 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
381 /* push flags if iret */
384 *stack16 = LOWORD(context->EFlags);
386 /* push return address (return to interrupt wrapper) */
387 *(--stack16) = DOSMEM_wrap_seg;
390 context->Esp -= 2*sizeof(WORD);
395 /* RMCB call, invoke protected-mode handler directly */
396 DPMI_CallRMCBProc(context, CurrRMCB, lpDosTask ? lpDosTask->dpmi_flag : 0);
397 /* check if we returned to where we thought we would */
398 if ((context->SegCs != DOSMEM_wrap_seg) ||
399 (LOWORD(context->Eip) != 0)) {
400 /* we need to continue at different address in real-mode space,
401 so we need to set it all up for real mode again */
402 goto callrmproc_again;
405 TRACE("entering real mode...\n");
406 DOSVM_Enter( context );
407 TRACE("returned from real-mode call\n");
409 if (alloc) DOSMEM_FreeBlock( addr );
414 /**********************************************************************
417 static void CallRMInt( CONTEXT86 *context )
419 CONTEXT86 realmode_ctx;
420 FARPROC16 rm_int = INT_GetRMHandler( BL_reg(context) );
421 REALMODECALL *call = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( context->SegEs,
423 INT_GetRealModeContext( call, &realmode_ctx );
425 /* we need to check if a real-mode program has hooked the interrupt */
426 if (HIWORD(rm_int)!=0xF000) {
427 /* yup, which means we need to switch to real mode... */
428 realmode_ctx.SegCs = HIWORD(rm_int);
429 realmode_ctx.Eip = LOWORD(rm_int);
430 if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
433 RESET_CFLAG(context);
434 /* use the IP we have instead of BL_reg, in case some apps
435 decide to move interrupts around for whatever reason... */
436 if (INT_RealModeInterrupt( LOWORD(rm_int)/4, &realmode_ctx ))
438 if (context->EFlags & 1) {
439 FIXME("%02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
440 BL_reg(context), realmode_ctx.Eax, realmode_ctx.Ebx,
441 realmode_ctx.Ecx, realmode_ctx.Edx);
442 FIXME(" ESI=%08lx EDI=%08lx DS=%04lx ES=%04lx\n",
443 realmode_ctx.Esi, realmode_ctx.Edi,
444 realmode_ctx.SegDs, realmode_ctx.SegEs );
447 INT_SetRealModeContext( call, &realmode_ctx );
451 static void CallRMProc( CONTEXT86 *context, int iret )
453 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( context->SegEs, DI_reg(context) );
456 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
457 p->eax, p->ebx, p->ecx, p->edx);
458 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
459 p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
461 if (!(p->cs) && !(p->ip)) { /* remove this check
462 if Int21/6501 case map function
463 has been implemented */
467 INT_GetRealModeContext(p, &context16);
468 DPMI_CallRMProc( &context16, ((LPWORD)PTR_SEG_OFF_TO_LIN(context->SegSs, LOWORD(context->Esp)))+3,
469 CX_reg(context), iret );
470 INT_SetRealModeContext(p, &context16);
474 static RMCB *DPMI_AllocRMCB( void )
476 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
481 LPVOID RMCBmem = DOSMEM_GetBlock(4, &uParagraph);
484 *p++ = 0xcd; /* RMCB: */
485 *p++ = 0x31; /* int $0x31 */
486 /* it is the called procedure's task to change the return CS:EIP
487 the DPMI 0.9 spec states that if it doesn't, it will be called again */
489 *p++ = 0xfc; /* jmp RMCB */
490 NewRMCB->address = MAKELONG(0, uParagraph);
491 NewRMCB->next = FirstRMCB;
498 static void AllocRMCB( CONTEXT86 *context )
500 RMCB *NewRMCB = DPMI_AllocRMCB();
502 TRACE("Function to call: %04x:%04x\n", (WORD)context->SegDs, SI_reg(context) );
506 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
507 NewRMCB->proc_ofs = LOWORD(context->Esi);
508 NewRMCB->proc_sel = context->SegDs;
509 NewRMCB->regs_ofs = LOWORD(context->Edi);
510 NewRMCB->regs_sel = context->SegEs;
511 SET_LOWORD( context->Ecx, HIWORD(NewRMCB->address) );
512 SET_LOWORD( context->Edx, LOWORD(NewRMCB->address) );
516 SET_LOWORD( context->Eax, 0x8015 ); /* callback unavailable */
522 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
524 RMCB *NewRMCB = DPMI_AllocRMCB();
527 NewRMCB->proc_ofs = (DWORD)proc;
528 NewRMCB->proc_sel = 0;
529 NewRMCB->regs_ofs = 0;
530 NewRMCB->regs_sel = 0;
531 return (FARPROC16)(NewRMCB->address);
537 static int DPMI_FreeRMCB( DWORD address )
539 RMCB *CurrRMCB = FirstRMCB;
540 RMCB *PrevRMCB = NULL;
542 while (CurrRMCB && (CurrRMCB->address != address))
545 CurrRMCB = CurrRMCB->next;
550 PrevRMCB->next = CurrRMCB->next;
552 FirstRMCB = CurrRMCB->next;
553 DOSMEM_FreeBlock(DOSMEM_MapRealToLinear(CurrRMCB->address));
554 HeapFree(GetProcessHeap(), 0, CurrRMCB);
561 static void FreeRMCB( CONTEXT86 *context )
563 FIXME("callback address: %04x:%04x\n",
564 CX_reg(context), DX_reg(context));
566 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
567 SET_LOWORD( context->Eax, 0x8024 ); /* invalid callback address */
573 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
575 DPMI_FreeRMCB( (DWORD)proc );
579 /* (see dosmem.c, function DOSMEM_InitDPMI) */
581 static void StartPM( CONTEXT86 *context, LPDOSTASK lpDosTask )
583 UINT16 cs, ss, ds, es;
585 DWORD psp_ofs = (DWORD)(lpDosTask->psp_seg<<4);
586 PDB16 *psp = (PDB16 *)psp_ofs;
587 HANDLE16 env_seg = psp->environment;
588 unsigned char selflags = WINE_LDT_FLAGS_DATA;
590 RESET_CFLAG(context);
591 lpDosTask->dpmi_flag = AX_reg(context);
592 /* our mode switch wrapper have placed the desired CS into DX */
593 cs = SELECTOR_AllocBlock( (void *)(DX_reg(context)<<4), 0x10000, WINE_LDT_FLAGS_CODE );
594 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
595 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
596 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
597 32-bit code using this stack. */
598 if (lpDosTask->dpmi_flag & 1) selflags |= WINE_LDT_FLAGS_32BIT;
599 ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, selflags );
600 /* do the same for the data segments, just in case */
601 if (context->SegDs == context->SegSs) ds = ss;
602 else ds = SELECTOR_AllocBlock( (void *)(context->SegDs<<4), 0x10000, selflags );
603 es = SELECTOR_AllocBlock( psp, 0x100, selflags );
604 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
605 psp->environment = SELECTOR_AllocBlock( (void *)(env_seg<<4), 0x10000, WINE_LDT_FLAGS_DATA );
608 pm_ctx.SegCs = DOSMEM_dpmi_sel;
609 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
617 TRACE("DOS program is now entering protected mode\n");
618 CallTo16RegisterShort(&pm_ctx, 0);
620 /* in the current state of affairs, we won't ever actually return here... */
621 /* we should have int21/ah=4c do it someday, though... */
623 FreeSelector16(psp->environment);
624 psp->environment = env_seg;
626 if (ds != ss) FreeSelector16(ds);
631 /* DPMI Raw Mode Switch handler */
634 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
636 LPDOSTASK lpDosTask = MZ_Current();
641 /* we could probably start a DPMI-only dosmod task here, but I doubt
642 anything other than real DOS apps want to call raw mode switch */
643 ERR("attempting raw mode switch without DOS task!\n");
646 /* initialize real-mode context as per spec */
647 memset(&rm_ctx, 0, sizeof(rm_ctx));
648 rm_ctx.SegDs = AX_sig(context);
649 rm_ctx.SegEs = CX_sig(context);
650 rm_ctx.SegSs = DX_sig(context);
651 rm_ctx.Esp = EBX_sig(context);
652 rm_ctx.SegCs = SI_sig(context);
653 rm_ctx.Eip = EDI_sig(context);
654 rm_ctx.Ebp = EBP_sig(context);
657 rm_ctx.EFlags = EFL_sig(context); /* at least we need the IF flag */
659 /* enter real mode again */
660 TRACE("re-entering real mode at %04lx:%04lx\n",rm_ctx.SegCs,rm_ctx.Eip);
661 ret = DOSVM_Enter( &rm_ctx );
662 /* when the real-mode stuff call its mode switch address,
663 DOSVM_Enter will return and we will continue here */
666 /* if the sync was lost, there's no way to recover */
670 /* alter protected-mode context as per spec */
671 DS_sig(context) = LOWORD(rm_ctx.Eax);
672 ES_sig(context) = LOWORD(rm_ctx.Ecx);
673 SS_sig(context) = LOWORD(rm_ctx.Edx);
674 ESP_sig(context) = rm_ctx.Ebx;
675 CS_sig(context) = LOWORD(rm_ctx.Esi);
676 EIP_sig(context) = rm_ctx.Edi;
677 EBP_sig(context) = rm_ctx.Ebp;
681 /* Return to new address and hope that we didn't mess up */
682 TRACE("re-entering protected mode at %04x:%08lx\n",
683 CS_sig(context), EIP_sig(context));
688 /**********************************************************************
691 * Handler for int 31h (DPMI).
694 void WINAPI INT_Int31Handler( CONTEXT86 *context )
697 * Note: For Win32s processes, the whole linear address space is
698 * shifted by 0x10000 relative to the OS linear address space.
699 * See the comment in msdos/vxd.c.
701 DWORD offset = W32S_APPLICATION() ? W32S_OFFSET : 0;
706 LPDOSTASK lpDosTask = MZ_Current();
708 if (ISV86(context) && lpDosTask) {
709 /* Called from real mode, check if it's our wrapper */
710 TRACE("called from real mode\n");
711 if (context->SegCs==DOSMEM_dpmi_seg) {
712 /* This is the protected mode switch */
713 StartPM(context,lpDosTask);
716 if (context->SegCs==DOSMEM_xms_seg) {
717 /* This is the XMS driver entry point */
718 XMS_Handler(context);
723 RMCB *CurrRMCB = FirstRMCB;
725 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
726 CurrRMCB = CurrRMCB->next;
729 /* RMCB call, propagate to protected-mode handler */
730 DPMI_CallRMCBProc(context, CurrRMCB, lpDosTask->dpmi_flag);
736 RESET_CFLAG(context);
737 switch(AX_reg(context))
739 case 0x0000: /* Allocate LDT descriptors */
740 TRACE("allocate LDT descriptors (%d)\n",CX_reg(context));
741 if (!(context->Eax = AllocSelectorArray16( CX_reg(context) )))
744 context->Eax = 0x8011; /* descriptor unavailable */
747 TRACE("success, array starts at 0x%04x\n",AX_reg(context));
750 case 0x0001: /* Free LDT descriptor */
751 TRACE("free LDT descriptor (0x%04x)\n",BX_reg(context));
752 if (FreeSelector16( BX_reg(context) ))
754 context->Eax = 0x8022; /* invalid selector */
759 /* If a segment register contains the selector being freed, */
760 /* set it to zero. */
761 if (!((context->SegDs^BX_reg(context)) & ~3)) context->SegDs = 0;
762 if (!((context->SegEs^BX_reg(context)) & ~3)) context->SegEs = 0;
763 if (!((context->SegFs^BX_reg(context)) & ~3)) context->SegFs = 0;
764 if (!((context->SegGs^BX_reg(context)) & ~3)) context->SegGs = 0;
768 case 0x0002: /* Real mode segment to descriptor */
769 TRACE("real mode segment to descriptor (0x%04x)\n",BX_reg(context));
771 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
772 switch(BX_reg(context))
774 case 0x0000: entryPoint = 183; break; /* __0000H */
775 case 0x0040: entryPoint = 193; break; /* __0040H */
776 case 0xa000: entryPoint = 174; break; /* __A000H */
777 case 0xb000: entryPoint = 181; break; /* __B000H */
778 case 0xb800: entryPoint = 182; break; /* __B800H */
779 case 0xc000: entryPoint = 195; break; /* __C000H */
780 case 0xd000: entryPoint = 179; break; /* __D000H */
781 case 0xe000: entryPoint = 190; break; /* __E000H */
782 case 0xf000: entryPoint = 194; break; /* __F000H */
784 context->Eax = DOSMEM_AllocSelector(BX_reg(context));
788 context->Eax = LOWORD(NE_GetEntryPoint( GetModuleHandle16( "KERNEL" ),
793 case 0x0003: /* Get next selector increment */
794 TRACE("get selector increment (__AHINCR)\n");
795 context->Eax = __AHINCR;
798 case 0x0004: /* Lock selector (not supported) */
799 FIXME("lock selector not supported\n");
800 context->Eax = 0; /* FIXME: is this a correct return value? */
803 case 0x0005: /* Unlock selector (not supported) */
804 FIXME("unlock selector not supported\n");
805 context->Eax = 0; /* FIXME: is this a correct return value? */
808 case 0x0006: /* Get selector base address */
809 TRACE("get selector base address (0x%04x)\n",BX_reg(context));
810 if (!(dw = GetSelectorBase( BX_reg(context) )))
812 context->Eax = 0x8022; /* invalid selector */
817 CX_reg(context) = HIWORD(W32S_WINE2APP(dw, offset));
818 DX_reg(context) = LOWORD(W32S_WINE2APP(dw, offset));
822 case 0x0007: /* Set selector base address */
823 TRACE("set selector base address (0x%04x,0x%08lx)\n",
825 W32S_APP2WINE(MAKELONG(DX_reg(context),CX_reg(context)), offset));
826 dw = W32S_APP2WINE(MAKELONG(DX_reg(context), CX_reg(context)), offset);
828 /* app wants to access lower 64K of DOS memory, map it in now */
830 SetSelectorBase(BX_reg(context), dw);
833 case 0x0008: /* Set selector limit */
834 TRACE("set selector limit (0x%04x,0x%08lx)\n",BX_reg(context),MAKELONG(DX_reg(context),CX_reg(context)));
835 dw = MAKELONG( DX_reg(context), CX_reg(context) );
836 SetSelectorLimit16( BX_reg(context), dw );
839 case 0x0009: /* Set selector access rights */
840 TRACE("set selector access rights(0x%04x,0x%04x)\n",BX_reg(context),CX_reg(context));
841 SelectorAccessRights16( BX_reg(context), 1, CX_reg(context) );
844 case 0x000a: /* Allocate selector alias */
845 TRACE("allocate selector alias (0x%04x)\n",BX_reg(context));
846 if (!(AX_reg(context) = AllocCStoDSAlias16( BX_reg(context) )))
848 AX_reg(context) = 0x8011; /* descriptor unavailable */
853 case 0x000b: /* Get descriptor */
854 TRACE("get descriptor (0x%04x)\n",BX_reg(context));
857 wine_ldt_set_base( &entry, (void*)W32S_WINE2APP(wine_ldt_get_base(&entry), offset) );
858 /* FIXME: should use ES:EDI for 32-bit clients */
859 *(LDT_ENTRY *)PTR_SEG_OFF_TO_LIN( context->SegEs, LOWORD(context->Edi) ) = entry;
863 case 0x000c: /* Set descriptor */
864 TRACE("set descriptor (0x%04x)\n",BX_reg(context));
866 LDT_ENTRY entry = *(LDT_ENTRY *)PTR_SEG_OFF_TO_LIN( context->SegEs,
867 LOWORD(context->Edi) );
868 wine_ldt_set_base( &entry, (void*)W32S_APP2WINE(wine_ldt_get_base(&entry), offset) );
869 wine_ldt_set_entry( LOWORD(context->Ebx), &entry );
873 case 0x000d: /* Allocate specific LDT descriptor */
874 FIXME("allocate descriptor (0x%04x), stub!\n",BX_reg(context));
875 AX_reg(context) = 0x8011; /* descriptor unavailable */
878 case 0x0100: /* Allocate DOS memory block */
879 TRACE("allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context));
880 dw = GlobalDOSAlloc16((DWORD)BX_reg(context)<<4);
882 AX_reg(context) = HIWORD(dw);
883 DX_reg(context) = LOWORD(dw);
885 AX_reg(context) = 0x0008; /* insufficient memory */
886 BX_reg(context) = DOSMEM_Available()>>4;
890 case 0x0101: /* Free DOS memory block */
891 TRACE("free DOS memory block (0x%04x)\n",DX_reg(context));
892 dw = GlobalDOSFree16(DX_reg(context));
894 AX_reg(context) = 0x0009; /* memory block address invalid */
898 case 0x0200: /* get real mode interrupt vector */
899 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
903 case 0x0201: /* set real mode interrupt vector */
904 FIXME("set realmode interrupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context),CX_reg(context),DX_reg(context));
907 case 0x0204: /* Get protected mode interrupt vector */
908 TRACE("get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context));
909 dw = (DWORD)INT_GetPMHandler( BL_reg(context) );
910 CX_reg(context) = HIWORD(dw);
911 DX_reg(context) = LOWORD(dw);
914 case 0x0205: /* Set protected mode interrupt vector */
915 TRACE("set protected mode interrupt handler (0x%02x,%p), stub!\n",
916 BL_reg(context),PTR_SEG_OFF_TO_LIN(CX_reg(context),DX_reg(context)));
917 INT_SetPMHandler( BL_reg(context),
918 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( CX_reg(context),
922 case 0x0300: /* Simulate real mode interrupt */
923 CallRMInt( context );
926 case 0x0301: /* Call real mode procedure with far return */
927 CallRMProc( context, FALSE );
930 case 0x0302: /* Call real mode procedure with interrupt return */
931 CallRMProc( context, TRUE );
934 case 0x0303: /* Allocate Real Mode Callback Address */
935 AllocRMCB( context );
938 case 0x0304: /* Free Real Mode Callback Address */
942 case 0x0305: /* Get State Save/Restore Addresses */
943 TRACE("get state save/restore addresses\n");
944 /* we probably won't need this kind of state saving */
946 /* real mode: just point to the lret */
947 BX_reg(context) = DOSMEM_wrap_seg;
949 /* protected mode: don't have any handler yet... */
950 FIXME("no protected-mode dummy state save/restore handler yet\n");
955 case 0x0306: /* Get Raw Mode Switch Addresses */
956 TRACE("get raw mode switch addresses\n");
957 /* real mode, point to standard DPMI return wrapper */
958 BX_reg(context) = DOSMEM_wrap_seg;
960 /* protected mode, point to DPMI call wrapper */
961 SI_reg(context) = DOSMEM_dpmi_sel;
962 context->Edi = 8; /* offset of the INT 0x31 call */
964 case 0x0400: /* Get DPMI version */
965 TRACE("get DPMI version\n");
970 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
971 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
972 CL_reg(context) = si.wProcessorLevel;
973 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
976 case 0x0500: /* Get free memory information */
977 TRACE("get free memory information\n");
981 mmi.dwSize = sizeof(mmi);
983 ptr = (BYTE *)PTR_SEG_OFF_TO_LIN(context->SegEs,DI_reg(context));
984 /* the layout is just the same as MEMMANINFO, but without
987 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
990 case 0x0501: /* Allocate memory block */
991 TRACE("allocate memory block (%ld)\n",MAKELONG(CX_reg(context),BX_reg(context)));
992 if (!(ptr = (BYTE *)DPMI_xalloc(MAKELONG(CX_reg(context), BX_reg(context)))))
994 AX_reg(context) = 0x8012; /* linear memory not available */
997 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
998 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1002 case 0x0502: /* Free memory block */
1003 TRACE("free memory block (0x%08lx)\n",
1004 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset));
1005 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),
1006 SI_reg(context)), offset) );
1009 case 0x0503: /* Resize memory block */
1010 TRACE("resize memory block (0x%08lx,%ld)\n",
1011 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
1012 MAKELONG(CX_reg(context),BX_reg(context)));
1013 if (!(ptr = (BYTE *)DPMI_xrealloc(
1014 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
1015 MAKELONG(CX_reg(context),BX_reg(context)))))
1017 AX_reg(context) = 0x8012; /* linear memory not available */
1020 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1021 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1025 case 0x0507: /* Modify page attributes */
1026 FIXME("modify page attributes unimplemented\n");
1027 break; /* Just ignore it */
1029 case 0x0600: /* Lock linear region */
1030 FIXME("lock linear region unimplemented\n");
1031 break; /* Just ignore it */
1033 case 0x0601: /* Unlock linear region */
1034 FIXME("unlock linear region unimplemented\n");
1035 break; /* Just ignore it */
1037 case 0x0602: /* Unlock real-mode region */
1038 FIXME("unlock realmode region unimplemented\n");
1039 break; /* Just ignore it */
1041 case 0x0603: /* Lock real-mode region */
1042 FIXME("lock realmode region unimplemented\n");
1043 break; /* Just ignore it */
1045 case 0x0604: /* Get page size */
1046 TRACE("get pagesize\n");
1047 BX_reg(context) = 0;
1048 CX_reg(context) = VIRTUAL_GetPageSize();
1051 case 0x0702: /* Mark page as demand-paging candidate */
1052 FIXME("mark page as demand-paging candidate\n");
1053 break; /* Just ignore it */
1055 case 0x0703: /* Discard page contents */
1056 FIXME("discard page contents\n");
1057 break; /* Just ignore it */
1059 case 0x0800: /* Physical address mapping */
1060 FIXME("map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context),BX_reg(context)));
1061 if(!(ptr=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context),BX_reg(context)))))
1063 AX_reg(context) = 0x8021;
1068 BX_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1069 CX_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1070 RESET_CFLAG(context);
1075 INT_BARF( context, 0x31 );
1076 AX_reg(context) = 0x8001; /* unsupported function */