4 * Copyright 1995 Alexandre Julliard
12 #include "wine/winbase16.h"
13 #include "wine/port.h"
14 #include "builtin16.h"
20 #include "selectors.h"
22 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(int31);
26 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
28 void CreateBPB(int drive, BYTE *data, BOOL16 limited); /* defined in int21.c */
30 static void* lastvalloced = NULL;
32 /* Structure for real-mode callbacks */
56 typedef struct tagRMCB {
58 DWORD proc_ofs,proc_sel;
59 DWORD regs_ofs,regs_sel;
63 static RMCB *FirstRMCB = NULL;
66 static LPDOSTASK WINAPI DPMI_NoCurrent(void) { return NULL; }
67 DOSVM_TABLE Dosvm = { DPMI_NoCurrent };
69 static HMODULE DosModule;
71 /**********************************************************************
74 BOOL DPMI_LoadDosSystem(void)
76 if (DosModule) return TRUE;
77 DosModule = LoadLibraryA( "winedos.dll" );
79 ERR("could not load winedos.dll, DOS subsystem unavailable\n");
82 Dosvm.Current = (void *)GetProcAddress(DosModule, "GetCurrent");
83 Dosvm.LoadDPMI = (void *)GetProcAddress(DosModule, "LoadDPMI");
84 Dosvm.LoadDosExe = (void *)GetProcAddress(DosModule, "LoadDosExe");
85 Dosvm.Exec = (void *)GetProcAddress(DosModule, "Exec");
86 Dosvm.Exit = (void *)GetProcAddress(DosModule, "Exit");
87 Dosvm.Enter = (void *)GetProcAddress(DosModule, "Enter");
88 Dosvm.Wait = (void *)GetProcAddress(DosModule, "Wait");
89 Dosvm.QueueEvent = (void *)GetProcAddress(DosModule, "QueueEvent");
90 Dosvm.OutPIC = (void *)GetProcAddress(DosModule, "OutPIC");
91 Dosvm.SetTimer = (void *)GetProcAddress(DosModule, "SetTimer");
92 Dosvm.GetTimer = (void *)GetProcAddress(DosModule, "GetTimer");
96 /**********************************************************************
98 * special virtualalloc, allocates lineary monoton growing memory.
99 * (the usual VirtualAlloc does not satisfy that restriction)
102 DPMI_xalloc(int len) {
104 LPVOID oldlastv = lastvalloced;
110 ret=VirtualAlloc(lastvalloced,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
112 lastvalloced = (char *) lastvalloced + 0x10000;
113 /* we failed to allocate one in the first round.
116 if (!xflag && (lastvalloced<oldlastv)) { /* wrapped */
117 FIXME("failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len);
120 /* if we even fail to allocate something in the next
123 if ((xflag==1) && (lastvalloced >= oldlastv))
125 if ((xflag==2) && (lastvalloced < oldlastv)) {
126 FIXME("failed to allocate any memory of %d bytes!\n",len);
131 ret=VirtualAlloc(NULL,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
132 lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
137 DPMI_xfree(LPVOID ptr) {
138 VirtualFree(ptr,0,MEM_RELEASE);
141 /* FIXME: perhaps we could grow this mapped area... */
143 DPMI_xrealloc(LPVOID ptr,DWORD newsize) {
144 MEMORY_BASIC_INFORMATION mbi;
147 newptr = DPMI_xalloc(newsize);
149 if (!VirtualQuery(ptr,&mbi,sizeof(mbi))) {
150 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
153 if (mbi.State == MEM_FREE) {
154 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
157 /* We do not shrink allocated memory. most reallocs
158 * only do grows anyway
160 if (newsize<=mbi.RegionSize)
162 memcpy(newptr,ptr,mbi.RegionSize);
167 /**********************************************************************
168 * INT_GetRealModeContext
170 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
172 context->Eax = call->eax;
173 context->Ebx = call->ebx;
174 context->Ecx = call->ecx;
175 context->Edx = call->edx;
176 context->Esi = call->esi;
177 context->Edi = call->edi;
178 context->Ebp = call->ebp;
179 context->EFlags = call->fl | V86_FLAG;
180 context->Eip = call->ip;
181 context->Esp = call->sp;
182 context->SegCs = call->cs;
183 context->SegDs = call->ds;
184 context->SegEs = call->es;
185 context->SegFs = call->fs;
186 context->SegGs = call->gs;
187 context->SegSs = call->ss;
191 /**********************************************************************
192 * INT_SetRealModeContext
194 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
196 call->eax = context->Eax;
197 call->ebx = context->Ebx;
198 call->ecx = context->Ecx;
199 call->edx = context->Edx;
200 call->esi = context->Esi;
201 call->edi = context->Edi;
202 call->ebp = context->Ebp;
203 call->fl = LOWORD(context->EFlags);
204 call->ip = LOWORD(context->Eip);
205 call->sp = LOWORD(context->Esp);
206 call->cs = context->SegCs;
207 call->ds = context->SegDs;
208 call->es = context->SegEs;
209 call->fs = context->SegFs;
210 call->gs = context->SegGs;
211 call->ss = context->SegSs;
216 void DPMI_CallRMCB32(RMCB *rmcb, UINT16 ss, DWORD esp, UINT16*es, DWORD*edi)
217 #if 0 /* original code, which early gccs puke on */
220 __asm__ __volatile__(
228 ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
234 : "=d" (*es), "=D" (*edi), "=S" (_clobber), "=a" (_clobber), "=c" (_clobber)
235 : "0" (ss), "2" (esp),
236 "4" (rmcb->regs_sel), "1" (rmcb->regs_ofs),
237 "3" (&rmcb->proc_ofs) );
239 #else /* code generated by a gcc new enough */
241 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32,
246 "movl 0x8(%ebp),%eax\n\t"
247 "movl 0x10(%ebp),%esi\n\t"
248 "movl 0xc(%ebp),%edx\n\t"
249 "movl 0x10(%eax),%ecx\n\t"
250 "movl 0xc(%eax),%edi\n\t"
259 ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
265 "movl 0x14(%ebp),%eax\n\t"
266 "movw %dx,(%eax)\n\t"
267 "movl 0x18(%ebp),%edx\n\t"
268 "movl %edi,(%edx)\n\t"
275 #endif /* __i386__ */
277 /**********************************************************************
280 * This routine does the hard work of calling a callback procedure.
282 static void DPMI_CallRMCBProc( CONTEXT86 *context, RMCB *rmcb, WORD flag )
284 if (IS_SELECTOR_SYSTEM( rmcb->proc_sel )) {
285 /* Wine-internal RMCB, call directly */
286 ((RMCBPROC)rmcb->proc_ofs)(context);
292 INT_SetRealModeContext(MapSL(MAKESEGPTR( rmcb->regs_sel, rmcb->regs_ofs )), context);
293 ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, WINE_LDT_FLAGS_DATA );
296 FIXME("untested!\n");
298 /* The called proc ends with an IRET, and takes these parameters:
299 * DS:ESI = pointer to real-mode SS:SP
300 * ES:EDI = pointer to real-mode call structure
302 * ES:EDI = pointer to real-mode call structure (may be a copy)
303 * It is the proc's responsibility to change the return CS:IP in the
304 * real-mode call structure. */
306 /* 32-bit DPMI client */
307 DPMI_CallRMCB32(rmcb, ss, esp, &es, &edi);
309 /* 16-bit DPMI client */
310 CONTEXT86 ctx = *context;
311 ctx.SegCs = rmcb->proc_sel;
312 ctx.Eip = rmcb->proc_ofs;
315 ctx.SegEs = rmcb->regs_sel;
316 ctx.Edi = rmcb->regs_ofs;
317 /* FIXME: I'm pretty sure this isn't right - should push flags first */
318 wine_call_to_16_regs_short(&ctx, 0);
323 INT_GetRealModeContext( MapSL( MAKESEGPTR( es, edi )), context);
325 ERR("RMCBs only implemented for i386\n");
331 /**********************************************************************
334 * This routine does the hard work of calling a real mode procedure.
336 int DPMI_CallRMProc( CONTEXT86 *context, LPWORD stack, int args, int iret )
339 LPVOID addr = NULL; /* avoid gcc warning */
340 LPDOSTASK lpDosTask = Dosvm.Current();
342 int alloc = 0, already = 0;
345 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
346 context->Eax, context->Ebx, context->Ecx, context->Edx );
347 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
348 context->Esi, context->Edi, context->SegEs, context->SegDs,
349 context->SegCs, LOWORD(context->Eip), args, iret?"IRET":"FAR" );
353 /* there might be some code that just jumps to RMCBs or the like,
354 in which case following the jumps here might get us to a shortcut */
355 code = CTX_SEG_OFF_TO_LIN(context, context->SegCs, context->Eip);
357 case 0xe9: /* JMP NEAR */
358 context->Eip += 3 + *(WORD *)(code+1);
359 /* yeah, I know these gotos don't look good... */
360 goto callrmproc_again;
361 case 0xea: /* JMP FAR */
362 context->Eip = *(WORD *)(code+1);
363 context->SegCs = *(WORD *)(code+3);
364 /* ...but since the label is there anyway... */
365 goto callrmproc_again;
366 case 0xeb: /* JMP SHORT */
367 context->Eip += 2 + *(signed char *)(code+1);
368 /* ...because of other gotos below, so... */
369 goto callrmproc_again;
372 /* shortcut for chaining to internal interrupt handlers */
373 if ((context->SegCs == 0xF000) && iret) {
374 return INT_RealModeInterrupt( LOWORD(context->Eip)/4, context);
377 /* shortcut for RMCBs */
378 CurrRMCB = FirstRMCB;
380 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
381 CurrRMCB = CurrRMCB->next;
383 if (!(CurrRMCB || lpDosTask)) {
384 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
385 TRACE("creating VM86 task\n");
386 if ((!DPMI_LoadDosSystem()) || !(lpDosTask = Dosvm.LoadDPMI() )) {
387 ERR("could not setup VM86 task\n");
392 if (!context->SegSs) {
393 alloc = 1; /* allocate default stack */
394 stack16 = addr = DOSMEM_GetBlock( 64, (UINT16 *)&(context->SegSs) );
398 ERR("could not allocate default stack\n");
402 stack16 = CTX_SEG_OFF_TO_LIN(context, context->SegSs, context->Esp);
404 context->Esp -= (args + (iret?1:0)) * sizeof(WORD);
406 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
407 /* push flags if iret */
410 *stack16 = LOWORD(context->EFlags);
412 /* push return address (return to interrupt wrapper) */
413 *(--stack16) = DOSMEM_wrap_seg;
416 context->Esp -= 2*sizeof(WORD);
421 /* RMCB call, invoke protected-mode handler directly */
422 DPMI_CallRMCBProc(context, CurrRMCB, lpDosTask ? lpDosTask->dpmi_flag : 0);
423 /* check if we returned to where we thought we would */
424 if ((context->SegCs != DOSMEM_wrap_seg) ||
425 (LOWORD(context->Eip) != 0)) {
426 /* we need to continue at different address in real-mode space,
427 so we need to set it all up for real mode again */
428 goto callrmproc_again;
431 TRACE("entering real mode...\n");
432 Dosvm.Enter( context );
433 TRACE("returned from real-mode call\n");
435 if (alloc) DOSMEM_FreeBlock( addr );
440 /**********************************************************************
443 static void CallRMInt( CONTEXT86 *context )
445 CONTEXT86 realmode_ctx;
446 FARPROC16 rm_int = INT_GetRMHandler( BL_reg(context) );
447 REALMODECALL *call = MapSL( MAKESEGPTR( context->SegEs, DI_reg(context) ));
448 INT_GetRealModeContext( call, &realmode_ctx );
450 /* we need to check if a real-mode program has hooked the interrupt */
451 if (HIWORD(rm_int)!=0xF000) {
452 /* yup, which means we need to switch to real mode... */
453 realmode_ctx.SegCs = HIWORD(rm_int);
454 realmode_ctx.Eip = LOWORD(rm_int);
455 if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
458 RESET_CFLAG(context);
459 /* use the IP we have instead of BL_reg, in case some apps
460 decide to move interrupts around for whatever reason... */
461 if (INT_RealModeInterrupt( LOWORD(rm_int)/4, &realmode_ctx ))
463 if (context->EFlags & 1) {
464 FIXME("%02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
465 BL_reg(context), realmode_ctx.Eax, realmode_ctx.Ebx,
466 realmode_ctx.Ecx, realmode_ctx.Edx);
467 FIXME(" ESI=%08lx EDI=%08lx DS=%04lx ES=%04lx\n",
468 realmode_ctx.Esi, realmode_ctx.Edi,
469 realmode_ctx.SegDs, realmode_ctx.SegEs );
472 INT_SetRealModeContext( call, &realmode_ctx );
476 static void CallRMProc( CONTEXT86 *context, int iret )
478 REALMODECALL *p = MapSL( MAKESEGPTR( context->SegEs, DI_reg(context) ));
481 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
482 p->eax, p->ebx, p->ecx, p->edx);
483 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
484 p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
486 if (!(p->cs) && !(p->ip)) { /* remove this check
487 if Int21/6501 case map function
488 has been implemented */
492 INT_GetRealModeContext(p, &context16);
493 DPMI_CallRMProc( &context16, ((LPWORD)MapSL(MAKESEGPTR(context->SegSs, LOWORD(context->Esp))))+3,
494 CX_reg(context), iret );
495 INT_SetRealModeContext(p, &context16);
499 static RMCB *DPMI_AllocRMCB( void )
501 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
506 LPVOID RMCBmem = DOSMEM_GetBlock(4, &uParagraph);
509 *p++ = 0xcd; /* RMCB: */
510 *p++ = 0x31; /* int $0x31 */
511 /* it is the called procedure's task to change the return CS:EIP
512 the DPMI 0.9 spec states that if it doesn't, it will be called again */
514 *p++ = 0xfc; /* jmp RMCB */
515 NewRMCB->address = MAKELONG(0, uParagraph);
516 NewRMCB->next = FirstRMCB;
523 static void AllocRMCB( CONTEXT86 *context )
525 RMCB *NewRMCB = DPMI_AllocRMCB();
527 TRACE("Function to call: %04x:%04x\n", (WORD)context->SegDs, SI_reg(context) );
531 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
532 NewRMCB->proc_ofs = LOWORD(context->Esi);
533 NewRMCB->proc_sel = context->SegDs;
534 NewRMCB->regs_ofs = LOWORD(context->Edi);
535 NewRMCB->regs_sel = context->SegEs;
536 SET_LOWORD( context->Ecx, HIWORD(NewRMCB->address) );
537 SET_LOWORD( context->Edx, LOWORD(NewRMCB->address) );
541 SET_LOWORD( context->Eax, 0x8015 ); /* callback unavailable */
547 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
549 RMCB *NewRMCB = DPMI_AllocRMCB();
552 NewRMCB->proc_ofs = (DWORD)proc;
553 NewRMCB->proc_sel = 0;
554 NewRMCB->regs_ofs = 0;
555 NewRMCB->regs_sel = 0;
556 return (FARPROC16)(NewRMCB->address);
562 static int DPMI_FreeRMCB( DWORD address )
564 RMCB *CurrRMCB = FirstRMCB;
565 RMCB *PrevRMCB = NULL;
567 while (CurrRMCB && (CurrRMCB->address != address))
570 CurrRMCB = CurrRMCB->next;
575 PrevRMCB->next = CurrRMCB->next;
577 FirstRMCB = CurrRMCB->next;
578 DOSMEM_FreeBlock(DOSMEM_MapRealToLinear(CurrRMCB->address));
579 HeapFree(GetProcessHeap(), 0, CurrRMCB);
586 static void FreeRMCB( CONTEXT86 *context )
588 FIXME("callback address: %04x:%04x\n",
589 CX_reg(context), DX_reg(context));
591 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
592 SET_LOWORD( context->Eax, 0x8024 ); /* invalid callback address */
598 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
600 DPMI_FreeRMCB( (DWORD)proc );
604 /* (see dosmem.c, function DOSMEM_InitDPMI) */
606 static void StartPM( CONTEXT86 *context, LPDOSTASK lpDosTask )
608 UINT16 cs, ss, ds, es;
610 DWORD psp_ofs = (DWORD)(lpDosTask->psp_seg<<4);
611 PDB16 *psp = (PDB16 *)psp_ofs;
612 HANDLE16 env_seg = psp->environment;
613 unsigned char selflags = WINE_LDT_FLAGS_DATA;
615 RESET_CFLAG(context);
616 lpDosTask->dpmi_flag = AX_reg(context);
617 /* our mode switch wrapper have placed the desired CS into DX */
618 cs = SELECTOR_AllocBlock( (void *)(DX_reg(context)<<4), 0x10000, WINE_LDT_FLAGS_CODE );
619 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
620 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
621 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
622 32-bit code using this stack. */
623 if (lpDosTask->dpmi_flag & 1) selflags |= WINE_LDT_FLAGS_32BIT;
624 ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, selflags );
625 /* do the same for the data segments, just in case */
626 if (context->SegDs == context->SegSs) ds = ss;
627 else ds = SELECTOR_AllocBlock( (void *)(context->SegDs<<4), 0x10000, selflags );
628 es = SELECTOR_AllocBlock( psp, 0x100, selflags );
629 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
630 psp->environment = SELECTOR_AllocBlock( (void *)(env_seg<<4), 0x10000, WINE_LDT_FLAGS_DATA );
633 pm_ctx.SegCs = DOSMEM_dpmi_sel;
634 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
642 TRACE("DOS program is now entering protected mode\n");
643 wine_call_to_16_regs_short(&pm_ctx, 0);
645 /* in the current state of affairs, we won't ever actually return here... */
646 /* we should have int21/ah=4c do it someday, though... */
648 FreeSelector16(psp->environment);
649 psp->environment = env_seg;
651 if (ds != ss) FreeSelector16(ds);
656 /* DPMI Raw Mode Switch handler */
659 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
661 LPDOSTASK lpDosTask = Dosvm.Current();
666 /* we could probably start a DPMI-only dosmod task here, but I doubt
667 anything other than real DOS apps want to call raw mode switch */
668 ERR("attempting raw mode switch without DOS task!\n");
671 /* initialize real-mode context as per spec */
672 memset(&rm_ctx, 0, sizeof(rm_ctx));
673 rm_ctx.SegDs = AX_sig(context);
674 rm_ctx.SegEs = CX_sig(context);
675 rm_ctx.SegSs = DX_sig(context);
676 rm_ctx.Esp = EBX_sig(context);
677 rm_ctx.SegCs = SI_sig(context);
678 rm_ctx.Eip = EDI_sig(context);
679 rm_ctx.Ebp = EBP_sig(context);
682 rm_ctx.EFlags = EFL_sig(context); /* at least we need the IF flag */
684 /* enter real mode again */
685 TRACE("re-entering real mode at %04lx:%04lx\n",rm_ctx.SegCs,rm_ctx.Eip);
686 ret = Dosvm.Enter( &rm_ctx );
687 /* when the real-mode stuff call its mode switch address,
688 Dosvm.Enter will return and we will continue here */
691 /* if the sync was lost, there's no way to recover */
695 /* alter protected-mode context as per spec */
696 DS_sig(context) = LOWORD(rm_ctx.Eax);
697 ES_sig(context) = LOWORD(rm_ctx.Ecx);
698 SS_sig(context) = LOWORD(rm_ctx.Edx);
699 ESP_sig(context) = rm_ctx.Ebx;
700 CS_sig(context) = LOWORD(rm_ctx.Esi);
701 EIP_sig(context) = rm_ctx.Edi;
702 EBP_sig(context) = rm_ctx.Ebp;
706 /* Return to new address and hope that we didn't mess up */
707 TRACE("re-entering protected mode at %04x:%08lx\n",
708 CS_sig(context), EIP_sig(context));
713 /**********************************************************************
714 * INT_Int31Handler (WPROCS.149)
716 * Handler for int 31h (DPMI).
719 void WINAPI INT_Int31Handler( CONTEXT86 *context )
722 * Note: For Win32s processes, the whole linear address space is
723 * shifted by 0x10000 relative to the OS linear address space.
724 * See the comment in msdos/vxd.c.
729 LPDOSTASK lpDosTask = Dosvm.Current();
731 if (ISV86(context) && lpDosTask) {
732 /* Called from real mode, check if it's our wrapper */
733 TRACE("called from real mode\n");
734 if (context->SegCs==DOSMEM_dpmi_seg) {
735 /* This is the protected mode switch */
736 StartPM(context,lpDosTask);
739 if (context->SegCs==DOSMEM_xms_seg) {
740 /* This is the XMS driver entry point */
741 XMS_Handler(context);
746 RMCB *CurrRMCB = FirstRMCB;
748 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
749 CurrRMCB = CurrRMCB->next;
752 /* RMCB call, propagate to protected-mode handler */
753 DPMI_CallRMCBProc(context, CurrRMCB, lpDosTask->dpmi_flag);
759 RESET_CFLAG(context);
760 switch(AX_reg(context))
762 case 0x0000: /* Allocate LDT descriptors */
763 TRACE("allocate LDT descriptors (%d)\n",CX_reg(context));
764 if (!(context->Eax = AllocSelectorArray16( CX_reg(context) )))
767 context->Eax = 0x8011; /* descriptor unavailable */
770 TRACE("success, array starts at 0x%04x\n",AX_reg(context));
773 case 0x0001: /* Free LDT descriptor */
774 TRACE("free LDT descriptor (0x%04x)\n",BX_reg(context));
775 if (FreeSelector16( BX_reg(context) ))
777 context->Eax = 0x8022; /* invalid selector */
782 /* If a segment register contains the selector being freed, */
783 /* set it to zero. */
784 if (!((context->SegDs^BX_reg(context)) & ~3)) context->SegDs = 0;
785 if (!((context->SegEs^BX_reg(context)) & ~3)) context->SegEs = 0;
786 if (!((context->SegFs^BX_reg(context)) & ~3)) context->SegFs = 0;
787 if (!((context->SegGs^BX_reg(context)) & ~3)) context->SegGs = 0;
791 case 0x0002: /* Real mode segment to descriptor */
792 TRACE("real mode segment to descriptor (0x%04x)\n",BX_reg(context));
794 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
795 switch(BX_reg(context))
797 case 0x0000: entryPoint = 183; break; /* __0000H */
798 case 0x0040: entryPoint = 193; break; /* __0040H */
799 case 0xa000: entryPoint = 174; break; /* __A000H */
800 case 0xb000: entryPoint = 181; break; /* __B000H */
801 case 0xb800: entryPoint = 182; break; /* __B800H */
802 case 0xc000: entryPoint = 195; break; /* __C000H */
803 case 0xd000: entryPoint = 179; break; /* __D000H */
804 case 0xe000: entryPoint = 190; break; /* __E000H */
805 case 0xf000: entryPoint = 194; break; /* __F000H */
807 context->Eax = DOSMEM_AllocSelector(BX_reg(context));
811 context->Eax = LOWORD(GetProcAddress16( GetModuleHandle16( "KERNEL" ),
812 (LPCSTR)(ULONG_PTR)entryPoint ));
816 case 0x0003: /* Get next selector increment */
817 TRACE("get selector increment (__AHINCR)\n");
818 context->Eax = __AHINCR;
821 case 0x0004: /* Lock selector (not supported) */
822 FIXME("lock selector not supported\n");
823 context->Eax = 0; /* FIXME: is this a correct return value? */
826 case 0x0005: /* Unlock selector (not supported) */
827 FIXME("unlock selector not supported\n");
828 context->Eax = 0; /* FIXME: is this a correct return value? */
831 case 0x0006: /* Get selector base address */
832 TRACE("get selector base address (0x%04x)\n",BX_reg(context));
833 if (!(dw = GetSelectorBase( BX_reg(context) )))
835 context->Eax = 0x8022; /* invalid selector */
840 CX_reg(context) = HIWORD(W32S_WINE2APP(dw));
841 DX_reg(context) = LOWORD(W32S_WINE2APP(dw));
845 case 0x0007: /* Set selector base address */
846 TRACE("set selector base address (0x%04x,0x%08lx)\n",
848 W32S_APP2WINE(MAKELONG(DX_reg(context),CX_reg(context))));
849 dw = W32S_APP2WINE(MAKELONG(DX_reg(context), CX_reg(context)));
851 /* app wants to access lower 64K of DOS memory, map it in now */
853 SetSelectorBase(BX_reg(context), dw);
856 case 0x0008: /* Set selector limit */
857 TRACE("set selector limit (0x%04x,0x%08lx)\n",BX_reg(context),MAKELONG(DX_reg(context),CX_reg(context)));
858 dw = MAKELONG( DX_reg(context), CX_reg(context) );
859 SetSelectorLimit16( BX_reg(context), dw );
862 case 0x0009: /* Set selector access rights */
863 TRACE("set selector access rights(0x%04x,0x%04x)\n",BX_reg(context),CX_reg(context));
864 SelectorAccessRights16( BX_reg(context), 1, CX_reg(context) );
867 case 0x000a: /* Allocate selector alias */
868 TRACE("allocate selector alias (0x%04x)\n",BX_reg(context));
869 if (!(AX_reg(context) = AllocCStoDSAlias16( BX_reg(context) )))
871 AX_reg(context) = 0x8011; /* descriptor unavailable */
876 case 0x000b: /* Get descriptor */
877 TRACE("get descriptor (0x%04x)\n",BX_reg(context));
880 wine_ldt_set_base( &entry, (void*)W32S_WINE2APP(wine_ldt_get_base(&entry)) );
881 /* FIXME: should use ES:EDI for 32-bit clients */
882 *(LDT_ENTRY *)MapSL( MAKESEGPTR( context->SegEs, LOWORD(context->Edi) )) = entry;
886 case 0x000c: /* Set descriptor */
887 TRACE("set descriptor (0x%04x)\n",BX_reg(context));
889 LDT_ENTRY entry = *(LDT_ENTRY *)MapSL(MAKESEGPTR( context->SegEs, LOWORD(context->Edi)));
890 wine_ldt_set_base( &entry, (void*)W32S_APP2WINE(wine_ldt_get_base(&entry)) );
891 wine_ldt_set_entry( LOWORD(context->Ebx), &entry );
895 case 0x000d: /* Allocate specific LDT descriptor */
896 FIXME("allocate descriptor (0x%04x), stub!\n",BX_reg(context));
897 AX_reg(context) = 0x8011; /* descriptor unavailable */
900 case 0x0100: /* Allocate DOS memory block */
901 TRACE("allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context));
902 dw = GlobalDOSAlloc16((DWORD)BX_reg(context)<<4);
904 AX_reg(context) = HIWORD(dw);
905 DX_reg(context) = LOWORD(dw);
907 AX_reg(context) = 0x0008; /* insufficient memory */
908 BX_reg(context) = DOSMEM_Available()>>4;
912 case 0x0101: /* Free DOS memory block */
913 TRACE("free DOS memory block (0x%04x)\n",DX_reg(context));
914 dw = GlobalDOSFree16(DX_reg(context));
916 AX_reg(context) = 0x0009; /* memory block address invalid */
920 case 0x0200: /* get real mode interrupt vector */
921 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
925 case 0x0201: /* set real mode interrupt vector */
926 FIXME("set realmode interrupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context),CX_reg(context),DX_reg(context));
929 case 0x0204: /* Get protected mode interrupt vector */
930 TRACE("get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context));
931 dw = (DWORD)INT_GetPMHandler( BL_reg(context) );
932 CX_reg(context) = HIWORD(dw);
933 DX_reg(context) = LOWORD(dw);
936 case 0x0205: /* Set protected mode interrupt vector */
937 TRACE("set protected mode interrupt handler (0x%02x,%p), stub!\n",
938 BL_reg(context),MapSL(MAKESEGPTR(CX_reg(context),DX_reg(context))));
939 INT_SetPMHandler( BL_reg(context),
940 (FARPROC16)MAKESEGPTR( CX_reg(context), DX_reg(context) ));
943 case 0x0300: /* Simulate real mode interrupt */
944 CallRMInt( context );
947 case 0x0301: /* Call real mode procedure with far return */
948 CallRMProc( context, FALSE );
951 case 0x0302: /* Call real mode procedure with interrupt return */
952 CallRMProc( context, TRUE );
955 case 0x0303: /* Allocate Real Mode Callback Address */
956 AllocRMCB( context );
959 case 0x0304: /* Free Real Mode Callback Address */
963 case 0x0305: /* Get State Save/Restore Addresses */
964 TRACE("get state save/restore addresses\n");
965 /* we probably won't need this kind of state saving */
967 /* real mode: just point to the lret */
968 BX_reg(context) = DOSMEM_wrap_seg;
970 /* protected mode: don't have any handler yet... */
971 FIXME("no protected-mode dummy state save/restore handler yet\n");
976 case 0x0306: /* Get Raw Mode Switch Addresses */
977 TRACE("get raw mode switch addresses\n");
978 /* real mode, point to standard DPMI return wrapper */
979 BX_reg(context) = DOSMEM_wrap_seg;
981 /* protected mode, point to DPMI call wrapper */
982 SI_reg(context) = DOSMEM_dpmi_sel;
983 context->Edi = 8; /* offset of the INT 0x31 call */
985 case 0x0400: /* Get DPMI version */
986 TRACE("get DPMI version\n");
991 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
992 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
993 CL_reg(context) = si.wProcessorLevel;
994 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
997 case 0x0500: /* Get free memory information */
998 TRACE("get free memory information\n");
1002 mmi.dwSize = sizeof(mmi);
1004 ptr = MapSL(MAKESEGPTR(context->SegEs,DI_reg(context)));
1005 /* the layout is just the same as MEMMANINFO, but without
1008 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
1011 case 0x0501: /* Allocate memory block */
1012 TRACE("allocate memory block (%ld)\n",MAKELONG(CX_reg(context),BX_reg(context)));
1013 if (!(ptr = (BYTE *)DPMI_xalloc(MAKELONG(CX_reg(context), BX_reg(context)))))
1015 AX_reg(context) = 0x8012; /* linear memory not available */
1018 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr));
1019 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr));
1023 case 0x0502: /* Free memory block */
1024 TRACE("free memory block (0x%08lx)\n",
1025 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context))));
1026 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context), SI_reg(context))) );
1029 case 0x0503: /* Resize memory block */
1030 TRACE("resize memory block (0x%08lx,%ld)\n",
1031 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context))),
1032 MAKELONG(CX_reg(context),BX_reg(context)));
1033 if (!(ptr = (BYTE *)DPMI_xrealloc(
1034 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context))),
1035 MAKELONG(CX_reg(context),BX_reg(context)))))
1037 AX_reg(context) = 0x8012; /* linear memory not available */
1040 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr));
1041 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr));
1045 case 0x0507: /* Modify page attributes */
1046 FIXME("modify page attributes unimplemented\n");
1047 break; /* Just ignore it */
1049 case 0x0600: /* Lock linear region */
1050 FIXME("lock linear region unimplemented\n");
1051 break; /* Just ignore it */
1053 case 0x0601: /* Unlock linear region */
1054 FIXME("unlock linear region unimplemented\n");
1055 break; /* Just ignore it */
1057 case 0x0602: /* Unlock real-mode region */
1058 FIXME("unlock realmode region unimplemented\n");
1059 break; /* Just ignore it */
1061 case 0x0603: /* Lock real-mode region */
1062 FIXME("lock realmode region unimplemented\n");
1063 break; /* Just ignore it */
1065 case 0x0604: /* Get page size */
1066 TRACE("get pagesize\n");
1067 BX_reg(context) = 0;
1068 CX_reg(context) = getpagesize();
1071 case 0x0702: /* Mark page as demand-paging candidate */
1072 FIXME("mark page as demand-paging candidate\n");
1073 break; /* Just ignore it */
1075 case 0x0703: /* Discard page contents */
1076 FIXME("discard page contents\n");
1077 break; /* Just ignore it */
1079 case 0x0800: /* Physical address mapping */
1080 FIXME("map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context),BX_reg(context)));
1081 if(!(ptr=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context),BX_reg(context)))))
1083 AX_reg(context) = 0x8021;
1088 BX_reg(context) = HIWORD(W32S_WINE2APP(ptr));
1089 CX_reg(context) = LOWORD(W32S_WINE2APP(ptr));
1090 RESET_CFLAG(context);
1095 INT_BARF( context, 0x31 );
1096 AX_reg(context) = 0x8001; /* unsupported function */