4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
28 #include "wine/winbase16.h"
34 #include "wine/debug.h"
35 #include "wine/exception.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(int31);
40 /* Structure for real-mode callbacks */
62 typedef struct tagRMCB {
64 DWORD proc_ofs,proc_sel;
65 DWORD regs_ofs,regs_sel;
69 static RMCB *FirstRMCB = NULL;
70 static WORD dpmi_flag;
71 static void* lastvalloced = NULL;
72 static BYTE DPMI_retval;
74 /**********************************************************************
77 * Return TRUE if we are in 32-bit protected mode DOS process.
79 BOOL DOSVM_IsDos32(void)
81 return (dpmi_flag & 1) ? TRUE : FALSE;
85 /**********************************************************************
88 * Allocate a 64k sized selector corresponding to a real mode segment.
90 static WORD alloc_pm_selector( WORD seg, unsigned char flags )
92 WORD sel = wine_ldt_alloc_entries( 1 );
97 wine_ldt_set_base( &entry, (void *)(seg << 4) );
98 wine_ldt_set_limit( &entry, 0xffff );
99 wine_ldt_set_flags( &entry, flags );
100 wine_ldt_set_entry( sel, &entry );
106 /**********************************************************************
107 * dpmi_exception_handler
109 * Handle EXCEPTION_VM86_STI exceptions generated
110 * when there are pending asynchronous events.
112 static WINE_EXCEPTION_FILTER(dpmi_exception_handler)
115 EXCEPTION_RECORD *rec = GetExceptionInformation()->ExceptionRecord;
116 CONTEXT *context = GetExceptionInformation()->ContextRecord;
118 if (rec->ExceptionCode == EXCEPTION_VM86_STI)
121 ERR( "Real mode STI caught by protected mode handler!\n" );
122 DOSVM_SendQueuedEvents(context);
123 return EXCEPTION_CONTINUE_EXECUTION;
125 else if (rec->ExceptionCode == EXCEPTION_VM86_INTx)
128 ERR( "Real mode INTx caught by protected mode handler!\n" );
129 DPMI_retval = (BYTE)rec->ExceptionInformation[0];
130 return EXCEPTION_EXECUTE_HANDLER;
134 return EXCEPTION_CONTINUE_SEARCH;
138 /**********************************************************************
139 * INT_GetRealModeContext
141 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
143 context->Eax = call->eax;
144 context->Ebx = call->ebx;
145 context->Ecx = call->ecx;
146 context->Edx = call->edx;
147 context->Esi = call->esi;
148 context->Edi = call->edi;
149 context->Ebp = call->ebp;
150 context->EFlags = call->fl | V86_FLAG;
151 context->Eip = call->ip;
152 context->Esp = call->sp;
153 context->SegCs = call->cs;
154 context->SegDs = call->ds;
155 context->SegEs = call->es;
156 context->SegFs = call->fs;
157 context->SegGs = call->gs;
158 context->SegSs = call->ss;
162 /**********************************************************************
163 * INT_SetRealModeContext
165 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
167 call->eax = context->Eax;
168 call->ebx = context->Ebx;
169 call->ecx = context->Ecx;
170 call->edx = context->Edx;
171 call->esi = context->Esi;
172 call->edi = context->Edi;
173 call->ebp = context->Ebp;
174 call->fl = LOWORD(context->EFlags);
175 call->ip = LOWORD(context->Eip);
176 call->sp = LOWORD(context->Esp);
177 call->cs = context->SegCs;
178 call->ds = context->SegDs;
179 call->es = context->SegEs;
180 call->fs = context->SegFs;
181 call->gs = context->SegGs;
182 call->ss = context->SegSs;
185 /**********************************************************************
187 * special virtualalloc, allocates lineary monoton growing memory.
188 * (the usual VirtualAlloc does not satisfy that restriction)
190 static LPVOID DPMI_xalloc( DWORD len )
193 LPVOID oldlastv = lastvalloced;
202 ret = VirtualAlloc( lastvalloced, len,
203 MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE );
205 lastvalloced = (char *) lastvalloced + 0x10000;
207 /* we failed to allocate one in the first round.
210 if (!xflag && (lastvalloced<oldlastv))
213 FIXME( "failed to allocate linearly growing memory (%ld bytes), "
214 "using non-linear growing...\n", len );
218 /* if we even fail to allocate something in the next
221 if ((xflag==1) && (lastvalloced >= oldlastv))
224 if ((xflag==2) && (lastvalloced < oldlastv)) {
225 FIXME( "failed to allocate any memory of %ld bytes!\n", len );
232 ret = VirtualAlloc( NULL, len,
233 MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE );
236 lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
240 /**********************************************************************
243 static void DPMI_xfree( LPVOID ptr )
245 VirtualFree( ptr, 0, MEM_RELEASE );
248 /**********************************************************************
251 * FIXME: perhaps we could grow this mapped area...
253 static LPVOID DPMI_xrealloc( LPVOID ptr, DWORD newsize )
255 MEMORY_BASIC_INFORMATION mbi;
258 newptr = DPMI_xalloc( newsize );
261 if (!VirtualQuery(ptr,&mbi,sizeof(mbi)))
263 FIXME( "realloc of DPMI_xallocd region %p?\n", ptr );
267 if (mbi.State == MEM_FREE)
269 FIXME( "realloc of DPMI_xallocd region %p?\n", ptr );
273 /* We do not shrink allocated memory. most reallocs
274 * only do grows anyway
276 if (newsize <= mbi.RegionSize)
279 memcpy( newptr, ptr, mbi.RegionSize );
289 void DPMI_CallRMCB32(RMCB *rmcb, UINT16 ss, DWORD esp, UINT16*es, DWORD*edi)
290 #if 0 /* original code, which early gccs puke on */
293 __asm__ __volatile__(
301 ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
307 : "=d" (*es), "=D" (*edi), "=S" (_clobber), "=a" (_clobber), "=c" (_clobber)
308 : "0" (ss), "2" (esp),
309 "4" (rmcb->regs_sel), "1" (rmcb->regs_ofs),
310 "3" (&rmcb->proc_ofs) );
312 #else /* code generated by a gcc new enough */
314 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32,
319 "movl 0x8(%ebp),%eax\n\t"
320 "movl 0x10(%ebp),%esi\n\t"
321 "movl 0xc(%ebp),%edx\n\t"
322 "movl 0x10(%eax),%ecx\n\t"
323 "movl 0xc(%eax),%edi\n\t"
332 ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
338 "movl 0x14(%ebp),%eax\n\t"
339 "movw %dx,(%eax)\n\t"
340 "movl 0x18(%ebp),%edx\n\t"
341 "movl %edi,(%edx)\n\t"
348 #endif /* __i386__ */
350 /**********************************************************************
353 * This routine does the hard work of calling a callback procedure.
355 static void DPMI_CallRMCBProc( CONTEXT86 *context, RMCB *rmcb, WORD flag )
357 DWORD old_vif = NtCurrentTeb()->dpmi_vif;
359 /* Disable virtual interrupts. */
360 NtCurrentTeb()->dpmi_vif = 0;
362 if (wine_ldt_is_system( rmcb->proc_sel )) {
363 /* Wine-internal RMCB, call directly */
364 ((RMCBPROC)rmcb->proc_ofs)(context);
370 INT_SetRealModeContext(MapSL(MAKESEGPTR( rmcb->regs_sel, rmcb->regs_ofs )), context);
371 ss = alloc_pm_selector( context->SegSs, WINE_LDT_FLAGS_DATA );
374 FIXME("untested!\n");
376 /* The called proc ends with an IRET, and takes these parameters:
377 * DS:ESI = pointer to real-mode SS:SP
378 * ES:EDI = pointer to real-mode call structure
380 * ES:EDI = pointer to real-mode call structure (may be a copy)
381 * It is the proc's responsibility to change the return CS:IP in the
382 * real-mode call structure. */
384 /* 32-bit DPMI client */
385 DPMI_CallRMCB32(rmcb, ss, esp, &es, &edi);
387 /* 16-bit DPMI client */
388 CONTEXT86 ctx = *context;
389 ctx.SegCs = rmcb->proc_sel;
390 ctx.Eip = rmcb->proc_ofs;
393 ctx.SegEs = rmcb->regs_sel;
394 ctx.Edi = rmcb->regs_ofs;
395 /* FIXME: I'm pretty sure this isn't right - should push flags first */
396 WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&ctx );
400 wine_ldt_free_entries( ss, 1 );
401 INT_GetRealModeContext( MapSL( MAKESEGPTR( es, edi )), context);
403 ERR("RMCBs only implemented for i386\n");
405 } __EXCEPT(dpmi_exception_handler) { } __ENDTRY
407 /* Restore virtual interrupt flag. */
408 NtCurrentTeb()->dpmi_vif = old_vif;
412 /**********************************************************************
415 * This routine does the hard work of calling a real mode procedure.
417 int DPMI_CallRMProc( CONTEXT86 *context, LPWORD stack, int args, int iret )
420 LPVOID addr = NULL; /* avoid gcc warning */
422 int alloc = 0, already = 0;
425 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
426 context->Eax, context->Ebx, context->Ecx, context->Edx );
427 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
428 context->Esi, context->Edi, context->SegEs, context->SegDs,
429 context->SegCs, LOWORD(context->Eip), args, iret?"IRET":"FAR" );
433 /* there might be some code that just jumps to RMCBs or the like,
434 in which case following the jumps here might get us to a shortcut */
435 code = CTX_SEG_OFF_TO_LIN(context, context->SegCs, context->Eip);
437 case 0xe9: /* JMP NEAR */
438 context->Eip += 3 + *(WORD *)(code+1);
439 /* yeah, I know these gotos don't look good... */
440 goto callrmproc_again;
441 case 0xea: /* JMP FAR */
442 context->Eip = *(WORD *)(code+1);
443 context->SegCs = *(WORD *)(code+3);
444 /* ...but since the label is there anyway... */
445 goto callrmproc_again;
446 case 0xeb: /* JMP SHORT */
447 context->Eip += 2 + *(signed char *)(code+1);
448 /* ...because of other gotos below, so... */
449 goto callrmproc_again;
452 /* shortcut for chaining to internal interrupt handlers */
453 if ((context->SegCs == 0xF000) && iret)
455 DOSVM_CallBuiltinHandler( context, LOWORD(context->Eip)/4 );
459 /* shortcut for RMCBs */
460 CurrRMCB = FirstRMCB;
462 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
463 CurrRMCB = CurrRMCB->next;
465 if (!CurrRMCB && !MZ_Current())
467 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
468 TRACE("creating VM86 task\n");
472 if (!context->SegSs) {
473 alloc = 1; /* allocate default stack */
474 stack16 = addr = DOSMEM_GetBlock( 64, (UINT16 *)&(context->SegSs) );
478 ERR("could not allocate default stack\n");
482 stack16 = CTX_SEG_OFF_TO_LIN(context, context->SegSs, context->Esp);
484 context->Esp -= (args + (iret?1:0)) * sizeof(WORD);
486 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
487 /* push flags if iret */
490 *stack16 = LOWORD(context->EFlags);
492 /* push return address (return to interrupt wrapper) */
493 *(--stack16) = DOSVM_dpmi_segments->wrap_seg;
496 context->Esp -= 2*sizeof(WORD);
501 /* RMCB call, invoke protected-mode handler directly */
502 DPMI_CallRMCBProc(context, CurrRMCB, dpmi_flag);
503 /* check if we returned to where we thought we would */
504 if ((context->SegCs != DOSVM_dpmi_segments->wrap_seg) ||
505 (LOWORD(context->Eip) != 0)) {
506 /* we need to continue at different address in real-mode space,
507 so we need to set it all up for real mode again */
508 goto callrmproc_again;
511 TRACE("entering real mode...\n");
512 DOSVM_Enter( context );
513 TRACE("returned from real-mode call\n");
515 if (alloc) DOSMEM_FreeBlock( addr );
520 /**********************************************************************
521 * CallRMInt (WINEDOS.@)
523 void WINAPI DOSVM_CallRMInt( CONTEXT86 *context )
525 CONTEXT86 realmode_ctx;
526 FARPROC16 rm_int = DOSVM_GetRMHandler( BL_reg(context) );
527 REALMODECALL *call = CTX_SEG_OFF_TO_LIN( context,
530 INT_GetRealModeContext( call, &realmode_ctx );
532 /* we need to check if a real-mode program has hooked the interrupt */
533 if (HIWORD(rm_int)!=0xF000) {
534 /* yup, which means we need to switch to real mode... */
535 realmode_ctx.SegCs = HIWORD(rm_int);
536 realmode_ctx.Eip = LOWORD(rm_int);
537 if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
540 RESET_CFLAG(context);
541 /* use the IP we have instead of BL_reg, in case some apps
542 decide to move interrupts around for whatever reason... */
543 DOSVM_CallBuiltinHandler( &realmode_ctx, LOWORD(rm_int)/4 );
545 INT_SetRealModeContext( call, &realmode_ctx );
549 /**********************************************************************
550 * CallRMProc (WINEDOS.@)
552 void WINAPI DOSVM_CallRMProc( CONTEXT86 *context, int iret )
554 REALMODECALL *p = CTX_SEG_OFF_TO_LIN( context,
559 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
560 p->eax, p->ebx, p->ecx, p->edx);
561 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
562 p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
564 if (!(p->cs) && !(p->ip)) { /* remove this check
565 if Int21/6501 case map function
566 has been implemented */
570 INT_GetRealModeContext(p, &context16);
571 DPMI_CallRMProc( &context16, ((LPWORD)MapSL(MAKESEGPTR(context->SegSs, LOWORD(context->Esp))))+3,
572 CX_reg(context), iret );
573 INT_SetRealModeContext(p, &context16);
577 /* (see dosmem.c, function DOSMEM_InitDPMI) */
578 static void StartPM( CONTEXT86 *context )
580 UINT16 cs, ss, ds, es;
582 DWORD psp_ofs = (DWORD)(DOSVM_psp<<4);
583 PDB16 *psp = (PDB16 *)psp_ofs;
584 HANDLE16 env_seg = psp->environment;
585 unsigned char selflags = WINE_LDT_FLAGS_DATA;
587 RESET_CFLAG(context);
588 dpmi_flag = AX_reg(context);
589 /* our mode switch wrapper have placed the desired CS into DX */
590 cs = alloc_pm_selector( context->Edx, WINE_LDT_FLAGS_CODE );
591 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
592 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
593 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
594 32-bit code using this stack. */
595 if (dpmi_flag & 1) selflags |= WINE_LDT_FLAGS_32BIT;
596 ss = alloc_pm_selector( context->SegSs, selflags );
597 /* do the same for the data segments, just in case */
598 if (context->SegDs == context->SegSs) ds = ss;
599 else ds = alloc_pm_selector( context->SegDs, selflags );
600 es = alloc_pm_selector( DOSVM_psp, selflags );
601 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
602 psp->environment = alloc_pm_selector( env_seg, WINE_LDT_FLAGS_DATA );
605 pm_ctx.SegCs = DOSVM_dpmi_segments->dpmi_sel;
606 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
611 pm_ctx.SegFs = wine_get_fs();
612 pm_ctx.SegGs = wine_get_gs();
613 pm_ctx.EFlags &= ~V86_FLAG;
615 TRACE("DOS program is now entering %d-bit protected mode\n",
616 DOSVM_IsDos32() ? 32 : 16);
620 WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&pm_ctx );
622 __EXCEPT(dpmi_exception_handler)
627 TRACE( "Protected mode DOS program is terminating\n" );
630 * FIXME: Instead of calling ExitThread, we should release all
631 * allocated protected mode resources and call MZ_Exit
632 * using real mode context. See DPMI specification.
634 ExitThread( DPMI_retval );
637 wine_ldt_free_entries( psp->environment, 1 );
638 psp->environment = env_seg;
639 wine_ldt_free_entries(es,1);
640 if (ds != ss) wine_ldt_free_entries(ds,1);
641 wine_ldt_free_entries(ss,1);
642 wine_ldt_free_entries(cs,1);
646 static RMCB *DPMI_AllocRMCB( void )
648 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
653 LPVOID RMCBmem = DOSMEM_GetBlock(4, &uParagraph);
656 *p++ = 0xcd; /* RMCB: */
657 *p++ = 0x31; /* int $0x31 */
658 /* it is the called procedure's task to change the return CS:EIP
659 the DPMI 0.9 spec states that if it doesn't, it will be called again */
661 *p++ = 0xfc; /* jmp RMCB */
662 NewRMCB->address = MAKELONG(0, uParagraph);
663 NewRMCB->next = FirstRMCB;
670 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
672 RMCB *NewRMCB = DPMI_AllocRMCB();
675 NewRMCB->proc_ofs = (DWORD)proc;
676 NewRMCB->proc_sel = 0;
677 NewRMCB->regs_ofs = 0;
678 NewRMCB->regs_sel = 0;
679 return (FARPROC16)(NewRMCB->address);
685 static int DPMI_FreeRMCB( DWORD address )
687 RMCB *CurrRMCB = FirstRMCB;
688 RMCB *PrevRMCB = NULL;
690 while (CurrRMCB && (CurrRMCB->address != address))
693 CurrRMCB = CurrRMCB->next;
698 PrevRMCB->next = CurrRMCB->next;
700 FirstRMCB = CurrRMCB->next;
701 DOSMEM_FreeBlock(PTR_REAL_TO_LIN(SELECTOROF(CurrRMCB->address),OFFSETOF(CurrRMCB->address)));
702 HeapFree(GetProcessHeap(), 0, CurrRMCB);
709 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
711 DPMI_FreeRMCB( (DWORD)proc );
715 /**********************************************************************
716 * DOSVM_RawModeSwitchHandler
718 * DPMI Raw Mode Switch handler
720 void WINAPI DOSVM_RawModeSwitchHandler( CONTEXT86 *context )
725 /* initialize real-mode context as per spec */
726 memset(&rm_ctx, 0, sizeof(rm_ctx));
727 rm_ctx.SegDs = AX_reg(context);
728 rm_ctx.SegEs = CX_reg(context);
729 rm_ctx.SegSs = DX_reg(context);
730 rm_ctx.Esp = context->Ebx;
731 rm_ctx.SegCs = SI_reg(context);
732 rm_ctx.Eip = context->Edi;
733 rm_ctx.Ebp = context->Ebp;
737 /* Copy interrupt state. */
738 if (NtCurrentTeb()->dpmi_vif)
739 rm_ctx.EFlags = V86_FLAG | VIF_MASK;
741 rm_ctx.EFlags = V86_FLAG;
743 /* enter real mode again */
744 TRACE("re-entering real mode at %04lx:%04lx\n",rm_ctx.SegCs,rm_ctx.Eip);
745 ret = DOSVM_Enter( &rm_ctx );
746 /* when the real-mode stuff call its mode switch address,
747 DOSVM_Enter will return and we will continue here */
751 /* if the sync was lost, there's no way to recover */
755 /* alter protected-mode context as per spec */
756 context->SegDs = LOWORD(rm_ctx.Eax);
757 context->SegEs = LOWORD(rm_ctx.Ecx);
758 context->SegSs = LOWORD(rm_ctx.Edx);
759 context->Esp = rm_ctx.Ebx;
760 context->SegCs = LOWORD(rm_ctx.Esi);
761 context->Eip = rm_ctx.Edi;
762 context->Ebp = rm_ctx.Ebp;
766 /* Copy interrupt state. */
767 if (rm_ctx.EFlags & VIF_MASK)
768 NtCurrentTeb()->dpmi_vif = 1;
770 NtCurrentTeb()->dpmi_vif = 0;
772 /* Return to new address and hope that we didn't mess up */
773 TRACE("re-entering protected mode at %04lx:%08lx\n",
774 context->SegCs, context->Eip);
778 /**********************************************************************
779 * AllocRMCB (WINEDOS.@)
781 void WINAPI DOSVM_AllocRMCB( CONTEXT86 *context )
783 RMCB *NewRMCB = DPMI_AllocRMCB();
785 TRACE("Function to call: %04x:%04x\n", (WORD)context->SegDs, SI_reg(context) );
789 NewRMCB->proc_ofs = DOSVM_IsDos32() ? context->Esi : LOWORD(context->Esi);
790 NewRMCB->proc_sel = context->SegDs;
791 NewRMCB->regs_ofs = DOSVM_IsDos32() ? context->Edi : LOWORD(context->Edi);
792 NewRMCB->regs_sel = context->SegEs;
793 SET_CX( context, HIWORD(NewRMCB->address) );
794 SET_DX( context, LOWORD(NewRMCB->address) );
798 SET_AX( context, 0x8015 ); /* callback unavailable */
804 /**********************************************************************
805 * FreeRMCB (WINEDOS.@)
807 void WINAPI DOSVM_FreeRMCB( CONTEXT86 *context )
809 FIXME("callback address: %04x:%04x\n",
810 CX_reg(context), DX_reg(context));
812 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
813 SET_AX( context, 0x8024 ); /* invalid callback address */
819 /**********************************************************************
820 * DOSVM_CheckWrappers
822 * Check if this was really a wrapper call instead of an interrupt.
824 BOOL DOSVM_CheckWrappers( CONTEXT86 *context )
826 if (context->SegCs==DOSVM_dpmi_segments->dpmi_seg) {
827 /* This is the protected mode switch */
831 else if (context->SegCs==DOSVM_dpmi_segments->xms_seg)
833 /* This is the XMS driver entry point */
834 XMS_Handler(context);
840 RMCB *CurrRMCB = FirstRMCB;
842 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
843 CurrRMCB = CurrRMCB->next;
846 /* RMCB call, propagate to protected-mode handler */
847 DPMI_CallRMCBProc(context, CurrRMCB, dpmi_flag);
855 /**********************************************************************
856 * DOSVM_Int31Handler (WINEDOS16.149)
858 * Handler for int 31h (DPMI).
860 void WINAPI DOSVM_Int31Handler( CONTEXT86 *context )
862 RESET_CFLAG(context);
863 switch(AX_reg(context))
865 case 0x0000: /* Allocate LDT descriptors */
866 TRACE( "allocate LDT descriptors (%d)\n", CX_reg(context) );
868 WORD sel = AllocSelectorArray16( CX_reg(context) );
872 SET_AX( context, 0x8011 ); /* descriptor unavailable */
873 SET_CFLAG( context );
877 TRACE( "success, array starts at 0x%04x\n", sel );
878 SET_AX( context, sel );
883 case 0x0001: /* Free LDT descriptor */
884 TRACE( "free LDT descriptor (0x%04x)\n", BX_reg(context) );
885 if (FreeSelector16( BX_reg(context) ))
887 SET_AX( context, 0x8022 ); /* invalid selector */
888 SET_CFLAG( context );
892 /* If a segment register contains the selector being freed, */
893 /* set it to zero. */
894 if (!((context->SegDs^BX_reg(context)) & ~3)) context->SegDs = 0;
895 if (!((context->SegEs^BX_reg(context)) & ~3)) context->SegEs = 0;
896 if (!((context->SegFs^BX_reg(context)) & ~3)) context->SegFs = 0;
897 if (!((context->SegGs^BX_reg(context)) & ~3)) context->SegGs = 0;
901 case 0x0002: /* Real mode segment to descriptor */
902 TRACE( "real mode segment to descriptor (0x%04x)\n", BX_reg(context) );
904 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
905 switch(BX_reg(context))
907 case 0x0000: entryPoint = 183; break; /* __0000H */
908 case 0x0040: entryPoint = 193; break; /* __0040H */
909 case 0xa000: entryPoint = 174; break; /* __A000H */
910 case 0xb000: entryPoint = 181; break; /* __B000H */
911 case 0xb800: entryPoint = 182; break; /* __B800H */
912 case 0xc000: entryPoint = 195; break; /* __C000H */
913 case 0xd000: entryPoint = 179; break; /* __D000H */
914 case 0xe000: entryPoint = 190; break; /* __E000H */
915 case 0xf000: entryPoint = 194; break; /* __F000H */
917 SET_AX( context, DOSMEM_AllocSelector(BX_reg(context)) );
922 FARPROC16 proc = GetProcAddress16( GetModuleHandle16( "KERNEL" ),
923 (LPCSTR)(ULONG_PTR)entryPoint );
924 SET_AX( context, LOWORD(proc) );
929 case 0x0003: /* Get next selector increment */
930 TRACE("get selector increment (__AHINCR)\n");
931 context->Eax = __AHINCR;
934 case 0x0004: /* Lock selector (not supported) */
935 FIXME("lock selector not supported\n");
936 context->Eax = 0; /* FIXME: is this a correct return value? */
939 case 0x0005: /* Unlock selector (not supported) */
940 FIXME("unlock selector not supported\n");
941 context->Eax = 0; /* FIXME: is this a correct return value? */
944 case 0x0006: /* Get selector base address */
945 TRACE( "get selector base address (0x%04x)\n", BX_reg(context) );
948 WORD sel = BX_reg(context);
949 wine_ldt_get_entry( sel, &entry );
950 if (wine_ldt_is_empty(&entry))
952 context->Eax = 0x8022; /* invalid selector */
957 void *base = wine_ldt_get_base(&entry);
958 SET_CX( context, HIWORD(base) );
959 SET_DX( context, LOWORD(base) );
964 case 0x0007: /* Set selector base address */
966 DWORD base = MAKELONG( DX_reg(context), CX_reg(context) );
967 WORD sel = BX_reg(context);
968 TRACE( "set selector base address (0x%04x,0x%08lx)\n", sel, base );
970 /* check if Win16 app wants to access lower 64K of DOS memory */
971 if (base < 0x10000 && DOSVM_IsWin16())
974 SetSelectorBase( sel, base );
978 case 0x0008: /* Set selector limit */
980 DWORD limit = MAKELONG( DX_reg(context), CX_reg(context) );
981 TRACE( "set selector limit (0x%04x,0x%08lx)\n",
982 BX_reg(context), limit );
983 SetSelectorLimit16( BX_reg(context), limit );
987 case 0x0009: /* Set selector access rights */
988 TRACE( "set selector access rights(0x%04x,0x%04x)\n",
989 BX_reg(context), CX_reg(context) );
990 SelectorAccessRights16( BX_reg(context), 1, CX_reg(context) );
993 case 0x000a: /* Allocate selector alias */
994 TRACE( "allocate selector alias (0x%04x)\n", BX_reg(context) );
995 SET_AX( context, AllocCStoDSAlias16( BX_reg(context) ) );
996 if (!AX_reg(context))
998 SET_AX( context, 0x8011 ); /* descriptor unavailable */
1003 case 0x000b: /* Get descriptor */
1004 TRACE( "get descriptor (0x%04x)\n", BX_reg(context) );
1006 LDT_ENTRY *entry = (LDT_ENTRY*)CTX_SEG_OFF_TO_LIN( context,
1009 wine_ldt_get_entry( BX_reg(context), entry );
1013 case 0x000c: /* Set descriptor */
1014 TRACE( "set descriptor (0x%04x)\n", BX_reg(context) );
1016 LDT_ENTRY *entry = (LDT_ENTRY*)CTX_SEG_OFF_TO_LIN( context,
1019 wine_ldt_set_entry( BX_reg(context), entry );
1023 case 0x000d: /* Allocate specific LDT descriptor */
1024 FIXME( "allocate descriptor (0x%04x), stub!\n", BX_reg(context) );
1025 SET_AX( context, 0x8011 ); /* descriptor unavailable */
1026 SET_CFLAG( context );
1029 case 0x000e: /* Get Multiple Descriptors (1.0) */
1030 FIXME( "get multiple descriptors - unimplemented\n" );
1033 case 0x000f: /* Set Multiple Descriptors (1.0) */
1034 FIXME( "set multiple descriptors - unimplemented\n" );
1037 case 0x0100: /* Allocate DOS memory block */
1038 TRACE( "allocate DOS memory block (0x%x paragraphs)\n", BX_reg(context) );
1040 DWORD dw = GlobalDOSAlloc16( (DWORD)BX_reg(context) << 4 );
1042 SET_AX( context, HIWORD(dw) );
1043 SET_DX( context, LOWORD(dw) );
1045 SET_AX( context, 0x0008 ); /* insufficient memory */
1046 SET_BX( context, DOSMEM_Available() >> 4 );
1052 case 0x0101: /* Free DOS memory block */
1053 TRACE( "free DOS memory block (0x%04x)\n", DX_reg(context) );
1055 WORD error = GlobalDOSFree16( DX_reg(context) );
1057 SET_AX( context, 0x0009 ); /* memory block address invalid */
1058 SET_CFLAG( context );
1063 case 0x0102: /* Resize DOS Memory Block */
1064 FIXME( "resize DOS memory block (0x%04x, 0x%x paragraphs) - unimplemented\n",
1065 DX_reg(context), BX_reg(context) );
1068 case 0x0200: /* get real mode interrupt vector */
1069 TRACE( "get realmode interupt vector (0x%02x)\n",
1072 FARPROC16 proc = DOSVM_GetRMHandler( BL_reg(context) );
1073 SET_CX( context, SELECTOROF(proc) );
1074 SET_DX( context, OFFSETOF(proc) );
1078 case 0x0201: /* set real mode interrupt vector */
1079 TRACE( "set realmode interrupt vector (0x%02x, 0x%04x:0x%04x)\n",
1080 BL_reg(context), CX_reg(context), DX_reg(context) );
1081 DOSVM_SetRMHandler( BL_reg(context),
1082 (FARPROC16)MAKESEGPTR(CX_reg(context), DX_reg(context)) );
1085 case 0x0202: /* Get Processor Exception Handler Vector */
1086 FIXME( "Get Processor Exception Handler Vector (0x%02x)\n",
1088 if (DOSVM_IsDos32())
1090 SET_CX( context, 0 );
1095 SET_CX( context, 0 );
1096 SET_DX( context, 0 );
1100 case 0x0203: /* Set Processor Exception Handler Vector */
1101 FIXME( "Set Processor Exception Handler Vector (0x%02x)\n",
1105 case 0x0204: /* Get protected mode interrupt vector */
1106 TRACE("get protected mode interrupt handler (0x%02x)\n",
1108 if (DOSVM_IsDos32())
1110 FARPROC48 handler = DOSVM_GetPMHandler48( BL_reg(context) );
1111 SET_CX( context, handler.selector );
1112 context->Edx = handler.offset;
1116 FARPROC16 handler = DOSVM_GetPMHandler16( BL_reg(context) );
1117 SET_CX( context, SELECTOROF(handler) );
1118 SET_DX( context, OFFSETOF(handler) );
1122 case 0x0205: /* Set protected mode interrupt vector */
1123 TRACE("set protected mode interrupt handler (0x%02x,0x%04x:0x%08lx)\n",
1124 BL_reg(context), CX_reg(context), context->Edx);
1125 if (DOSVM_IsDos32())
1128 handler.selector = CX_reg(context);
1129 handler.offset = context->Edx;
1130 DOSVM_SetPMHandler48( BL_reg(context), handler );
1135 handler = (FARPROC16)MAKESEGPTR( CX_reg(context), DX_reg(context));
1136 DOSVM_SetPMHandler16( BL_reg(context), handler );
1140 case 0x0300: /* Simulate real mode interrupt */
1141 TRACE( "Simulate real mode interrupt %02x.\n", BL_reg(context));
1142 DOSVM_CallRMInt( context );
1145 case 0x0301: /* Call real mode procedure with far return */
1146 TRACE( "Call real mode procedure with far return.\n" );
1147 DOSVM_CallRMProc( context, FALSE );
1150 case 0x0302: /* Call real mode procedure with interrupt return */
1151 TRACE( "Call real mode procedure with interrupt return.\n" );
1152 DOSVM_CallRMProc( context, TRUE );
1155 case 0x0303: /* Allocate Real Mode Callback Address */
1156 TRACE( "Allocate real mode callback address.\n" );
1157 DOSVM_AllocRMCB( context );
1160 case 0x0304: /* Free Real Mode Callback Address */
1161 TRACE( "Free real mode callback address.\n" );
1162 DOSVM_FreeRMCB( context );
1165 case 0x0305: /* Get State Save/Restore Addresses */
1166 TRACE("get state save/restore addresses\n");
1167 /* we probably won't need this kind of state saving */
1168 SET_AX( context, 0 );
1170 /* real mode: just point to the lret */
1171 SET_BX( context, DOSVM_dpmi_segments->wrap_seg );
1172 SET_CX( context, 2 );
1174 /* protected mode: don't have any handler yet... */
1175 /* FIXME: Use DI in 16-bit DPMI and EDI in 32-bit DPMI */
1176 FIXME("no protected-mode dummy state save/restore handler yet\n");
1177 SET_SI( context, 0 );
1181 case 0x0306: /* Get Raw Mode Switch Addresses */
1182 TRACE("get raw mode switch addresses\n");
1184 /* real mode, point to standard DPMI return wrapper */
1185 SET_BX( context, DOSVM_dpmi_segments->wrap_seg );
1186 SET_CX( context, 0 );
1188 /* protected mode, point to DPMI call wrapper */
1189 /* FIXME: Use DI in 16-bit DPMI and EDI in 32-bit DPMI */
1190 /* FIXME: Doesn't work in DPMI32... */
1191 SET_SI( context, DOSVM_dpmi_segments->dpmi_sel );
1192 context->Edi = 8; /* offset of the INT 0x31 call */
1195 case 0x0400: /* Get DPMI version */
1196 TRACE("get DPMI version\n");
1201 SET_AX( context, 0x005a ); /* DPMI version 0.90 */
1202 SET_BX( context, 0x0005 ); /* Flags: 32-bit, virtual memory */
1203 SET_CL( context, si.wProcessorLevel );
1204 SET_DX( context, 0x0870 ); /* Master/slave interrupt controller base */
1208 case 0x0401: /* Get DPMI Capabilities (1.0) */
1209 FIXME( "get dpmi capabilities - unimplemented\n");
1212 case 0x0500: /* Get free memory information */
1213 TRACE("get free memory information\n");
1215 MEMORYSTATUS status;
1217 /* the layout is just the same as MEMMANINFO, but without
1222 DWORD dwLargestFreeBlock;
1223 DWORD dwMaxPagesAvailable;
1224 DWORD dwMaxPagesLockable;
1225 DWORD dwTotalLinearSpace;
1226 DWORD dwTotalUnlockedPages;
1229 DWORD dwFreeLinearSpace;
1230 DWORD dwSwapFilePages;
1232 } *info = CTX_SEG_OFF_TO_LIN( context, context->SegEs, context->Edi );
1234 GlobalMemoryStatus( &status );
1235 info->wPageSize = getpagesize();
1236 info->dwLargestFreeBlock = status.dwAvailVirtual;
1237 info->dwMaxPagesAvailable = info->dwLargestFreeBlock / info->wPageSize;
1238 info->dwMaxPagesLockable = info->dwMaxPagesAvailable;
1239 info->dwTotalLinearSpace = status.dwTotalVirtual / info->wPageSize;
1240 info->dwTotalUnlockedPages = info->dwTotalLinearSpace;
1241 info->dwFreePages = info->dwMaxPagesAvailable;
1242 info->dwTotalPages = info->dwTotalLinearSpace;
1243 info->dwFreeLinearSpace = info->dwMaxPagesAvailable;
1244 info->dwSwapFilePages = status.dwTotalPageFile / info->wPageSize;
1248 case 0x0501: /* Allocate memory block */
1250 DWORD size = MAKELONG( CX_reg(context), BX_reg(context) );
1253 TRACE( "allocate memory block (%ld bytes)\n", size );
1255 ptr = (BYTE *)DPMI_xalloc( size );
1258 SET_AX( context, 0x8012 ); /* linear memory not available */
1263 SET_BX( context, HIWORD(ptr) );
1264 SET_CX( context, LOWORD(ptr) );
1265 SET_SI( context, HIWORD(ptr) );
1266 SET_DI( context, LOWORD(ptr) );
1271 case 0x0502: /* Free memory block */
1273 DWORD handle = MAKELONG( DI_reg(context), SI_reg(context) );
1274 TRACE( "free memory block (0x%08lx)\n", handle );
1275 DPMI_xfree( (void *)handle );
1279 case 0x0503: /* Resize memory block */
1281 DWORD size = MAKELONG( CX_reg(context), BX_reg(context) );
1282 DWORD handle = MAKELONG( DI_reg(context), SI_reg(context) );
1285 TRACE( "resize memory block (0x%08lx, %ld bytes)\n", handle, size );
1287 ptr = (BYTE *)DPMI_xrealloc( (void *)handle, size );
1290 SET_AX( context, 0x8012 ); /* linear memory not available */
1293 SET_BX( context, HIWORD(ptr) );
1294 SET_CX( context, LOWORD(ptr) );
1295 SET_SI( context, HIWORD(ptr) );
1296 SET_DI( context, LOWORD(ptr) );
1301 case 0x0507: /* Set page attributes (1.0) */
1302 FIXME( "set page attributes - unimplemented\n" );
1303 break; /* Just ignore it */
1305 case 0x0600: /* Lock linear region */
1306 TRACE( "lock linear region - ignored (no paging)\n" );
1309 case 0x0601: /* Unlock linear region */
1310 TRACE( "unlock linear region - ignored (no paging)\n" );
1313 case 0x0602: /* Mark real mode region as pageable */
1314 TRACE( "mark real mode region as pageable - ignored (no paging)\n" );
1317 case 0x0603: /* Relock real mode region */
1318 TRACE( "relock real mode region - ignored (no paging)\n" );
1321 case 0x0604: /* Get page size */
1322 TRACE("get pagesize\n");
1323 SET_BX( context, HIWORD(getpagesize()) );
1324 SET_CX( context, LOWORD(getpagesize()) );
1327 case 0x0700: /* Mark pages as paging candidates */
1328 TRACE( "mark pages as paging candidates - ignored (no paging)\n" );
1331 case 0x0701: /* Discard pages */
1332 TRACE( "discard pages - ignored (no paging)\n" );
1335 case 0x0702: /* Mark page as demand-paging candidate */
1336 TRACE( "mark page as demand-paging candidate - ignored (no paging)\n" );
1339 case 0x0703: /* Discard page contents */
1340 TRACE( "discard page contents - ignored (no paging)\n" );
1343 case 0x0800: /* Physical address mapping */
1344 FIXME( "physical address mapping (0x%08lx) - unimplemented\n",
1345 MAKELONG(CX_reg(context),BX_reg(context)) );
1348 case 0x0900: /* Get and Disable Virtual Interrupt State */
1349 TRACE( "Get and Disable Virtual Interrupt State: %ld\n",
1350 NtCurrentTeb()->dpmi_vif );
1351 SET_AL( context, NtCurrentTeb()->dpmi_vif ? 1 : 0 );
1352 NtCurrentTeb()->dpmi_vif = 0;
1355 case 0x0901: /* Get and Enable Virtual Interrupt State */
1356 TRACE( "Get and Enable Virtual Interrupt State: %ld\n",
1357 NtCurrentTeb()->dpmi_vif );
1358 SET_AL( context, NtCurrentTeb()->dpmi_vif ? 1 : 0 );
1359 NtCurrentTeb()->dpmi_vif = 1;
1362 case 0x0902: /* Get Virtual Interrupt State */
1363 TRACE( "Get Virtual Interrupt State: %ld\n",
1364 NtCurrentTeb()->dpmi_vif );
1365 SET_AL( context, NtCurrentTeb()->dpmi_vif ? 1 : 0 );
1368 case 0x0e00: /* Get Coprocessor Status (1.0) */
1370 * Return status in AX bits:
1371 * B0 - MPv (MP bit in the virtual MSW/CR0)
1372 * 0 = numeric coprocessor is disabled for this client
1373 * 1 = numeric coprocessor is enabled for this client
1374 * B1 - EMv (EM bit in the virtual MSW/CR0)
1375 * 0 = client is not emulating coprocessor instructions
1376 * 1 = client is emulating coprocessor instructions
1377 * B2 - MPr (MP bit from the actual MSW/CR0)
1378 * 0 = numeric coprocessor is not present
1379 * 1 = numeric coprocessor is present
1380 * B3 - EMr (EM bit from the actual MSW/CR0)
1381 * 0 = host is not emulating coprocessor instructions
1382 * 1 = host is emulating coprocessor instructions
1383 * B4-B7 - coprocessor type
1384 * 00H = no coprocessor
1387 * 04H = 80486 with numeric coprocessor
1388 * 05H-0FH = reserved for future numeric processors
1390 TRACE( "Get Coprocessor Status\n" );
1391 SET_AX( context, 69 ); /* 486, coprocessor present and enabled */
1394 case 0x0e01: /* Set Coprocessor Emulation (1.0) */
1396 * See function 0x0e00.
1397 * BX bit B0 is new value for MPv.
1398 * BX bit B1 is new value for EMv.
1400 if (BX_reg(context) != 1)
1401 FIXME( "Set Coprocessor Emulation to %d - unimplemented\n",
1404 TRACE( "Set Coprocessor Emulation - ignored\n" );
1408 INT_BARF( context, 0x31 );
1409 SET_AX( context, 0x8001 ); /* unsupported function */