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"
25 #include "wine/winbase16.h"
33 #include "wine/debug.h"
34 #include "wine/exception.h"
35 #include "stackframe.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
;
73 /**********************************************************************
76 * Return TRUE if we are in 32-bit protected mode DOS process.
78 BOOL
DOSVM_IsDos32(void)
80 return (dpmi_flag
& 1) ? TRUE
: FALSE
;
84 /**********************************************************************
85 * dpmi_exception_handler
87 * Handle EXCEPTION_VM86_STI exceptions generated
88 * when there are pending asynchronous events.
90 static WINE_EXCEPTION_FILTER(dpmi_exception_handler
)
93 EXCEPTION_RECORD
*rec
= GetExceptionInformation()->ExceptionRecord
;
94 CONTEXT
*context
= GetExceptionInformation()->ContextRecord
;
96 if (rec
->ExceptionCode
== EXCEPTION_VM86_STI
)
99 ERR( "Real mode STI caught by protected mode handler!\n" );
100 DOSVM_SendQueuedEvents(context
);
101 return EXCEPTION_CONTINUE_EXECUTION
;
104 return EXCEPTION_CONTINUE_SEARCH
;
108 /**********************************************************************
109 * INT_GetRealModeContext
111 static void INT_GetRealModeContext( REALMODECALL
*call
, CONTEXT86
*context
)
113 context
->Eax
= call
->eax
;
114 context
->Ebx
= call
->ebx
;
115 context
->Ecx
= call
->ecx
;
116 context
->Edx
= call
->edx
;
117 context
->Esi
= call
->esi
;
118 context
->Edi
= call
->edi
;
119 context
->Ebp
= call
->ebp
;
120 context
->EFlags
= call
->fl
| V86_FLAG
;
121 context
->Eip
= call
->ip
;
122 context
->Esp
= call
->sp
;
123 context
->SegCs
= call
->cs
;
124 context
->SegDs
= call
->ds
;
125 context
->SegEs
= call
->es
;
126 context
->SegFs
= call
->fs
;
127 context
->SegGs
= call
->gs
;
128 context
->SegSs
= call
->ss
;
132 /**********************************************************************
133 * INT_SetRealModeContext
135 static void INT_SetRealModeContext( REALMODECALL
*call
, CONTEXT86
*context
)
137 call
->eax
= context
->Eax
;
138 call
->ebx
= context
->Ebx
;
139 call
->ecx
= context
->Ecx
;
140 call
->edx
= context
->Edx
;
141 call
->esi
= context
->Esi
;
142 call
->edi
= context
->Edi
;
143 call
->ebp
= context
->Ebp
;
144 call
->fl
= LOWORD(context
->EFlags
);
145 call
->ip
= LOWORD(context
->Eip
);
146 call
->sp
= LOWORD(context
->Esp
);
147 call
->cs
= context
->SegCs
;
148 call
->ds
= context
->SegDs
;
149 call
->es
= context
->SegEs
;
150 call
->fs
= context
->SegFs
;
151 call
->gs
= context
->SegGs
;
152 call
->ss
= context
->SegSs
;
155 /**********************************************************************
157 * special virtualalloc, allocates lineary monoton growing memory.
158 * (the usual VirtualAlloc does not satisfy that restriction)
160 static LPVOID
DPMI_xalloc( DWORD len
)
163 LPVOID oldlastv
= lastvalloced
;
172 ret
= VirtualAlloc( lastvalloced
, len
,
173 MEM_COMMIT
|MEM_RESERVE
, PAGE_EXECUTE_READWRITE
);
175 lastvalloced
= (char *) lastvalloced
+ 0x10000;
177 /* we failed to allocate one in the first round.
180 if (!xflag
&& (lastvalloced
<oldlastv
))
183 FIXME( "failed to allocate linearly growing memory (%ld bytes), "
184 "using non-linear growing...\n", len
);
188 /* if we even fail to allocate something in the next
191 if ((xflag
==1) && (lastvalloced
>= oldlastv
))
194 if ((xflag
==2) && (lastvalloced
< oldlastv
)) {
195 FIXME( "failed to allocate any memory of %ld bytes!\n", len
);
202 ret
= VirtualAlloc( NULL
, len
,
203 MEM_COMMIT
|MEM_RESERVE
, PAGE_EXECUTE_READWRITE
);
206 lastvalloced
= (LPVOID
)(((DWORD
)ret
+len
+0xffff)&~0xffff);
210 /**********************************************************************
213 static void DPMI_xfree( LPVOID ptr
)
215 VirtualFree( ptr
, 0, MEM_RELEASE
);
218 /**********************************************************************
221 * FIXME: perhaps we could grow this mapped area...
223 static LPVOID
DPMI_xrealloc( LPVOID ptr
, DWORD newsize
)
225 MEMORY_BASIC_INFORMATION mbi
;
228 newptr
= DPMI_xalloc( newsize
);
231 if (!VirtualQuery(ptr
,&mbi
,sizeof(mbi
)))
233 FIXME( "realloc of DPMI_xallocd region %p?\n", ptr
);
237 if (mbi
.State
== MEM_FREE
)
239 FIXME( "realloc of DPMI_xallocd region %p?\n", ptr
);
243 /* We do not shrink allocated memory. most reallocs
244 * only do grows anyway
246 if (newsize
<= mbi
.RegionSize
)
249 memcpy( newptr
, ptr
, mbi
.RegionSize
);
259 void DPMI_CallRMCB32(RMCB
*rmcb
, UINT16 ss
, DWORD esp
, UINT16
*es
, DWORD
*edi
)
260 #if 0 /* original code, which early gccs puke on */
263 __asm__
__volatile__(
271 ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
277 : "=d" (*es
), "=D" (*edi
), "=S" (_clobber
), "=a" (_clobber
), "=c" (_clobber
)
278 : "0" (ss
), "2" (esp
),
279 "4" (rmcb
->regs_sel
), "1" (rmcb
->regs_ofs
),
280 "3" (&rmcb
->proc_ofs
) );
282 #else /* code generated by a gcc new enough */
284 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32
,
289 "movl 0x8(%ebp),%eax\n\t"
290 "movl 0x10(%ebp),%esi\n\t"
291 "movl 0xc(%ebp),%edx\n\t"
292 "movl 0x10(%eax),%ecx\n\t"
293 "movl 0xc(%eax),%edi\n\t"
302 ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
308 "movl 0x14(%ebp),%eax\n\t"
309 "movw %dx,(%eax)\n\t"
310 "movl 0x18(%ebp),%edx\n\t"
311 "movl %edi,(%edx)\n\t"
318 #endif /* __i386__ */
320 /**********************************************************************
323 * This routine does the hard work of calling a callback procedure.
325 static void DPMI_CallRMCBProc( CONTEXT86
*context
, RMCB
*rmcb
, WORD flag
)
327 DWORD old_vif
= NtCurrentTeb()->dpmi_vif
;
329 /* Disable virtual interrupts. */
330 NtCurrentTeb()->dpmi_vif
= 0;
332 if (IS_SELECTOR_SYSTEM( rmcb
->proc_sel
)) {
333 /* Wine-internal RMCB, call directly */
334 ((RMCBPROC
)rmcb
->proc_ofs
)(context
);
340 INT_SetRealModeContext(MapSL(MAKESEGPTR( rmcb
->regs_sel
, rmcb
->regs_ofs
)), context
);
341 ss
= SELECTOR_AllocBlock( (void *)(context
->SegSs
<<4), 0x10000, WINE_LDT_FLAGS_DATA
);
344 FIXME("untested!\n");
346 /* The called proc ends with an IRET, and takes these parameters:
347 * DS:ESI = pointer to real-mode SS:SP
348 * ES:EDI = pointer to real-mode call structure
350 * ES:EDI = pointer to real-mode call structure (may be a copy)
351 * It is the proc's responsibility to change the return CS:IP in the
352 * real-mode call structure. */
354 /* 32-bit DPMI client */
355 DPMI_CallRMCB32(rmcb
, ss
, esp
, &es
, &edi
);
357 /* 16-bit DPMI client */
358 CONTEXT86 ctx
= *context
;
359 ctx
.SegCs
= rmcb
->proc_sel
;
360 ctx
.Eip
= rmcb
->proc_ofs
;
363 ctx
.SegEs
= rmcb
->regs_sel
;
364 ctx
.Edi
= rmcb
->regs_ofs
;
365 /* FIXME: I'm pretty sure this isn't right - should push flags first */
366 WOWCallback16Ex( 0, WCB16_REGS
, 0, NULL
, (DWORD
*)&context
);
371 INT_GetRealModeContext( MapSL( MAKESEGPTR( es
, edi
)), context
);
373 ERR("RMCBs only implemented for i386\n");
375 } __EXCEPT(dpmi_exception_handler
) { } __ENDTRY
377 /* Restore virtual interrupt flag. */
378 NtCurrentTeb()->dpmi_vif
= old_vif
;
382 /**********************************************************************
385 * This routine does the hard work of calling a real mode procedure.
387 int DPMI_CallRMProc( CONTEXT86
*context
, LPWORD stack
, int args
, int iret
)
390 LPVOID addr
= NULL
; /* avoid gcc warning */
392 int alloc
= 0, already
= 0;
395 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
396 context
->Eax
, context
->Ebx
, context
->Ecx
, context
->Edx
);
397 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
398 context
->Esi
, context
->Edi
, context
->SegEs
, context
->SegDs
,
399 context
->SegCs
, LOWORD(context
->Eip
), args
, iret
?"IRET":"FAR" );
403 /* there might be some code that just jumps to RMCBs or the like,
404 in which case following the jumps here might get us to a shortcut */
405 code
= CTX_SEG_OFF_TO_LIN(context
, context
->SegCs
, context
->Eip
);
407 case 0xe9: /* JMP NEAR */
408 context
->Eip
+= 3 + *(WORD
*)(code
+1);
409 /* yeah, I know these gotos don't look good... */
410 goto callrmproc_again
;
411 case 0xea: /* JMP FAR */
412 context
->Eip
= *(WORD
*)(code
+1);
413 context
->SegCs
= *(WORD
*)(code
+3);
414 /* ...but since the label is there anyway... */
415 goto callrmproc_again
;
416 case 0xeb: /* JMP SHORT */
417 context
->Eip
+= 2 + *(signed char *)(code
+1);
418 /* ...because of other gotos below, so... */
419 goto callrmproc_again
;
422 /* shortcut for chaining to internal interrupt handlers */
423 if ((context
->SegCs
== 0xF000) && iret
)
425 DOSVM_CallBuiltinHandler( context
, LOWORD(context
->Eip
)/4 );
429 /* shortcut for RMCBs */
430 CurrRMCB
= FirstRMCB
;
432 while (CurrRMCB
&& (HIWORD(CurrRMCB
->address
) != context
->SegCs
))
433 CurrRMCB
= CurrRMCB
->next
;
435 if (!CurrRMCB
&& !MZ_Current())
437 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
438 TRACE("creating VM86 task\n");
442 if (!context
->SegSs
) {
443 alloc
= 1; /* allocate default stack */
444 stack16
= addr
= DOSMEM_GetBlock( 64, (UINT16
*)&(context
->SegSs
) );
448 ERR("could not allocate default stack\n");
452 stack16
= CTX_SEG_OFF_TO_LIN(context
, context
->SegSs
, context
->Esp
);
454 context
->Esp
-= (args
+ (iret
?1:0)) * sizeof(WORD
);
456 if (args
) memcpy(stack16
, stack
, args
*sizeof(WORD
) );
457 /* push flags if iret */
460 *stack16
= LOWORD(context
->EFlags
);
462 /* push return address (return to interrupt wrapper) */
463 *(--stack16
) = DOSVM_dpmi_segments
->wrap_seg
;
466 context
->Esp
-= 2*sizeof(WORD
);
471 /* RMCB call, invoke protected-mode handler directly */
472 DPMI_CallRMCBProc(context
, CurrRMCB
, dpmi_flag
);
473 /* check if we returned to where we thought we would */
474 if ((context
->SegCs
!= DOSVM_dpmi_segments
->wrap_seg
) ||
475 (LOWORD(context
->Eip
) != 0)) {
476 /* we need to continue at different address in real-mode space,
477 so we need to set it all up for real mode again */
478 goto callrmproc_again
;
481 TRACE("entering real mode...\n");
482 DOSVM_Enter( context
);
483 TRACE("returned from real-mode call\n");
485 if (alloc
) DOSMEM_FreeBlock( addr
);
490 /**********************************************************************
491 * CallRMInt (WINEDOS.@)
493 void WINAPI
DOSVM_CallRMInt( CONTEXT86
*context
)
495 CONTEXT86 realmode_ctx
;
496 FARPROC16 rm_int
= DOSVM_GetRMHandler( BL_reg(context
) );
497 REALMODECALL
*call
= CTX_SEG_OFF_TO_LIN( context
,
500 INT_GetRealModeContext( call
, &realmode_ctx
);
502 /* we need to check if a real-mode program has hooked the interrupt */
503 if (HIWORD(rm_int
)!=0xF000) {
504 /* yup, which means we need to switch to real mode... */
505 realmode_ctx
.SegCs
= HIWORD(rm_int
);
506 realmode_ctx
.Eip
= LOWORD(rm_int
);
507 if (DPMI_CallRMProc( &realmode_ctx
, NULL
, 0, TRUE
))
510 RESET_CFLAG(context
);
511 /* use the IP we have instead of BL_reg, in case some apps
512 decide to move interrupts around for whatever reason... */
513 DOSVM_CallBuiltinHandler( &realmode_ctx
, LOWORD(rm_int
)/4 );
515 INT_SetRealModeContext( call
, &realmode_ctx
);
519 /**********************************************************************
520 * CallRMProc (WINEDOS.@)
522 void WINAPI
DOSVM_CallRMProc( CONTEXT86
*context
, int iret
)
524 REALMODECALL
*p
= CTX_SEG_OFF_TO_LIN( context
,
529 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
530 p
->eax
, p
->ebx
, p
->ecx
, p
->edx
);
531 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
532 p
->esi
, p
->edi
, p
->es
, p
->ds
, p
->cs
, p
->ip
, CX_reg(context
), iret
?"IRET":"FAR" );
534 if (!(p
->cs
) && !(p
->ip
)) { /* remove this check
535 if Int21/6501 case map function
536 has been implemented */
540 INT_GetRealModeContext(p
, &context16
);
541 DPMI_CallRMProc( &context16
, ((LPWORD
)MapSL(MAKESEGPTR(context
->SegSs
, LOWORD(context
->Esp
))))+3,
542 CX_reg(context
), iret
);
543 INT_SetRealModeContext(p
, &context16
);
547 /* (see dosmem.c, function DOSMEM_InitDPMI) */
548 static void StartPM( CONTEXT86
*context
)
550 UINT16 cs
, ss
, ds
, es
;
552 DWORD psp_ofs
= (DWORD
)(DOSVM_psp
<<4);
553 PDB16
*psp
= (PDB16
*)psp_ofs
;
554 HANDLE16 env_seg
= psp
->environment
;
555 unsigned char selflags
= WINE_LDT_FLAGS_DATA
;
557 RESET_CFLAG(context
);
558 dpmi_flag
= AX_reg(context
);
559 /* our mode switch wrapper have placed the desired CS into DX */
560 cs
= SELECTOR_AllocBlock( (void *)(DX_reg(context
)<<4), 0x10000, WINE_LDT_FLAGS_CODE
);
561 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
562 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
563 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
564 32-bit code using this stack. */
565 if (dpmi_flag
& 1) selflags
|= WINE_LDT_FLAGS_32BIT
;
566 ss
= SELECTOR_AllocBlock( (void *)(context
->SegSs
<<4), 0x10000, selflags
);
567 /* do the same for the data segments, just in case */
568 if (context
->SegDs
== context
->SegSs
) ds
= ss
;
569 else ds
= SELECTOR_AllocBlock( (void *)(context
->SegDs
<<4), 0x10000, selflags
);
570 es
= SELECTOR_AllocBlock( psp
, 0x100, selflags
);
571 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
572 psp
->environment
= SELECTOR_AllocBlock( (void *)(env_seg
<<4), 0x10000, WINE_LDT_FLAGS_DATA
);
575 pm_ctx
.SegCs
= DOSVM_dpmi_segments
->dpmi_sel
;
576 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
584 TRACE("DOS program is now entering %d-bit protected mode\n",
585 DOSVM_IsDos32() ? 32 : 16);
589 WOWCallback16Ex( 0, WCB16_REGS
, 0, NULL
, (DWORD
*)&pm_ctx
);
591 __EXCEPT(dpmi_exception_handler
)
596 /* in the current state of affairs, we won't ever actually return here... */
597 /* we should have int21/ah=4c do it someday, though... */
600 FreeSelector16(psp
->environment
);
601 psp
->environment
= env_seg
;
603 if (ds
!= ss
) FreeSelector16(ds
);
609 static RMCB
*DPMI_AllocRMCB( void )
611 RMCB
*NewRMCB
= HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB
));
616 LPVOID RMCBmem
= DOSMEM_GetBlock(4, &uParagraph
);
619 *p
++ = 0xcd; /* RMCB: */
620 *p
++ = 0x31; /* int $0x31 */
621 /* it is the called procedure's task to change the return CS:EIP
622 the DPMI 0.9 spec states that if it doesn't, it will be called again */
624 *p
++ = 0xfc; /* jmp RMCB */
625 NewRMCB
->address
= MAKELONG(0, uParagraph
);
626 NewRMCB
->next
= FirstRMCB
;
633 FARPROC16 WINAPI
DPMI_AllocInternalRMCB( RMCBPROC proc
)
635 RMCB
*NewRMCB
= DPMI_AllocRMCB();
638 NewRMCB
->proc_ofs
= (DWORD
)proc
;
639 NewRMCB
->proc_sel
= 0;
640 NewRMCB
->regs_ofs
= 0;
641 NewRMCB
->regs_sel
= 0;
642 return (FARPROC16
)(NewRMCB
->address
);
648 static int DPMI_FreeRMCB( DWORD address
)
650 RMCB
*CurrRMCB
= FirstRMCB
;
651 RMCB
*PrevRMCB
= NULL
;
653 while (CurrRMCB
&& (CurrRMCB
->address
!= address
))
656 CurrRMCB
= CurrRMCB
->next
;
661 PrevRMCB
->next
= CurrRMCB
->next
;
663 FirstRMCB
= CurrRMCB
->next
;
664 DOSMEM_FreeBlock(PTR_REAL_TO_LIN(SELECTOROF(CurrRMCB
->address
),OFFSETOF(CurrRMCB
->address
)));
665 HeapFree(GetProcessHeap(), 0, CurrRMCB
);
672 void WINAPI
DPMI_FreeInternalRMCB( FARPROC16 proc
)
674 DPMI_FreeRMCB( (DWORD
)proc
);
678 /**********************************************************************
679 * DOSVM_RawModeSwitchHandler
681 * DPMI Raw Mode Switch handler
683 void WINAPI
DOSVM_RawModeSwitchHandler( CONTEXT86
*context
)
688 /* initialize real-mode context as per spec */
689 memset(&rm_ctx
, 0, sizeof(rm_ctx
));
690 rm_ctx
.SegDs
= AX_reg(context
);
691 rm_ctx
.SegEs
= CX_reg(context
);
692 rm_ctx
.SegSs
= DX_reg(context
);
693 rm_ctx
.Esp
= context
->Ebx
;
694 rm_ctx
.SegCs
= SI_reg(context
);
695 rm_ctx
.Eip
= context
->Edi
;
696 rm_ctx
.Ebp
= context
->Ebp
;
699 rm_ctx
.EFlags
= context
->EFlags
; /* at least we need the IF flag */
701 /* enter real mode again */
702 TRACE("re-entering real mode at %04lx:%04lx\n",rm_ctx
.SegCs
,rm_ctx
.Eip
);
703 ret
= DOSVM_Enter( &rm_ctx
);
704 /* when the real-mode stuff call its mode switch address,
705 DOSVM_Enter will return and we will continue here */
709 /* if the sync was lost, there's no way to recover */
713 /* alter protected-mode context as per spec */
714 context
->SegDs
= LOWORD(rm_ctx
.Eax
);
715 context
->SegEs
= LOWORD(rm_ctx
.Ecx
);
716 context
->SegSs
= LOWORD(rm_ctx
.Edx
);
717 context
->Esp
= rm_ctx
.Ebx
;
718 context
->SegCs
= LOWORD(rm_ctx
.Esi
);
719 context
->Eip
= rm_ctx
.Edi
;
720 context
->Ebp
= rm_ctx
.Ebp
;
724 /* Return to new address and hope that we didn't mess up */
725 TRACE("re-entering protected mode at %04lx:%08lx\n",
726 context
->SegCs
, context
->Eip
);
730 /**********************************************************************
731 * AllocRMCB (WINEDOS.@)
733 void WINAPI
DOSVM_AllocRMCB( CONTEXT86
*context
)
735 RMCB
*NewRMCB
= DPMI_AllocRMCB();
737 TRACE("Function to call: %04x:%04x\n", (WORD
)context
->SegDs
, SI_reg(context
) );
741 NewRMCB
->proc_ofs
= DOSVM_IsDos32() ? context
->Esi
: LOWORD(context
->Esi
);
742 NewRMCB
->proc_sel
= context
->SegDs
;
743 NewRMCB
->regs_ofs
= DOSVM_IsDos32() ? context
->Edi
: LOWORD(context
->Edi
);
744 NewRMCB
->regs_sel
= context
->SegEs
;
745 SET_CX( context
, HIWORD(NewRMCB
->address
) );
746 SET_DX( context
, LOWORD(NewRMCB
->address
) );
750 SET_AX( context
, 0x8015 ); /* callback unavailable */
756 /**********************************************************************
757 * FreeRMCB (WINEDOS.@)
759 void WINAPI
DOSVM_FreeRMCB( CONTEXT86
*context
)
761 FIXME("callback address: %04x:%04x\n",
762 CX_reg(context
), DX_reg(context
));
764 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context
), CX_reg(context
)))) {
765 SET_AX( context
, 0x8024 ); /* invalid callback address */
771 /**********************************************************************
772 * DOSVM_CheckWrappers
774 * Check if this was really a wrapper call instead of an interrupt.
776 BOOL
DOSVM_CheckWrappers( CONTEXT86
*context
)
778 if (context
->SegCs
==DOSVM_dpmi_segments
->dpmi_seg
) {
779 /* This is the protected mode switch */
783 else if (context
->SegCs
==DOSVM_dpmi_segments
->xms_seg
)
785 /* This is the XMS driver entry point */
786 XMS_Handler(context
);
792 RMCB
*CurrRMCB
= FirstRMCB
;
794 while (CurrRMCB
&& (HIWORD(CurrRMCB
->address
) != context
->SegCs
))
795 CurrRMCB
= CurrRMCB
->next
;
798 /* RMCB call, propagate to protected-mode handler */
799 DPMI_CallRMCBProc(context
, CurrRMCB
, dpmi_flag
);
807 /**********************************************************************
808 * DOSVM_Int31Handler (WINEDOS16.149)
810 * Handler for int 31h (DPMI).
812 void WINAPI
DOSVM_Int31Handler( CONTEXT86
*context
)
814 RESET_CFLAG(context
);
815 switch(AX_reg(context
))
817 case 0x0000: /* Allocate LDT descriptors */
818 TRACE( "allocate LDT descriptors (%d)\n", CX_reg(context
) );
820 WORD sel
= AllocSelectorArray16( CX_reg(context
) );
824 SET_AX( context
, 0x8011 ); /* descriptor unavailable */
825 SET_CFLAG( context
);
829 TRACE( "success, array starts at 0x%04x\n", sel
);
830 SET_AX( context
, sel
);
835 case 0x0001: /* Free LDT descriptor */
836 TRACE( "free LDT descriptor (0x%04x)\n", BX_reg(context
) );
837 if (FreeSelector16( BX_reg(context
) ))
839 SET_AX( context
, 0x8022 ); /* invalid selector */
840 SET_CFLAG( context
);
844 /* If a segment register contains the selector being freed, */
845 /* set it to zero. */
846 if (!((context
->SegDs
^BX_reg(context
)) & ~3)) context
->SegDs
= 0;
847 if (!((context
->SegEs
^BX_reg(context
)) & ~3)) context
->SegEs
= 0;
848 if (!((context
->SegFs
^BX_reg(context
)) & ~3)) context
->SegFs
= 0;
849 if (!((context
->SegGs
^BX_reg(context
)) & ~3)) context
->SegGs
= 0;
853 case 0x0002: /* Real mode segment to descriptor */
854 TRACE( "real mode segment to descriptor (0x%04x)\n", BX_reg(context
) );
856 WORD entryPoint
= 0; /* KERNEL entry point for descriptor */
857 switch(BX_reg(context
))
859 case 0x0000: entryPoint
= 183; break; /* __0000H */
860 case 0x0040: entryPoint
= 193; break; /* __0040H */
861 case 0xa000: entryPoint
= 174; break; /* __A000H */
862 case 0xb000: entryPoint
= 181; break; /* __B000H */
863 case 0xb800: entryPoint
= 182; break; /* __B800H */
864 case 0xc000: entryPoint
= 195; break; /* __C000H */
865 case 0xd000: entryPoint
= 179; break; /* __D000H */
866 case 0xe000: entryPoint
= 190; break; /* __E000H */
867 case 0xf000: entryPoint
= 194; break; /* __F000H */
869 SET_AX( context
, DOSMEM_AllocSelector(BX_reg(context
)) );
874 FARPROC16 proc
= GetProcAddress16( GetModuleHandle16( "KERNEL" ),
875 (LPCSTR
)(ULONG_PTR
)entryPoint
);
876 SET_AX( context
, LOWORD(proc
) );
881 case 0x0003: /* Get next selector increment */
882 TRACE("get selector increment (__AHINCR)\n");
883 context
->Eax
= __AHINCR
;
886 case 0x0004: /* Lock selector (not supported) */
887 FIXME("lock selector not supported\n");
888 context
->Eax
= 0; /* FIXME: is this a correct return value? */
891 case 0x0005: /* Unlock selector (not supported) */
892 FIXME("unlock selector not supported\n");
893 context
->Eax
= 0; /* FIXME: is this a correct return value? */
896 case 0x0006: /* Get selector base address */
897 TRACE( "get selector base address (0x%04x)\n", BX_reg(context
) );
900 WORD sel
= BX_reg(context
);
901 wine_ldt_get_entry( sel
, &entry
);
902 if (wine_ldt_is_empty(&entry
))
904 context
->Eax
= 0x8022; /* invalid selector */
909 void *base
= wine_ldt_get_base(&entry
);
910 SET_CX( context
, HIWORD(base
) );
911 SET_DX( context
, LOWORD(base
) );
916 case 0x0007: /* Set selector base address */
918 DWORD base
= MAKELONG( DX_reg(context
), CX_reg(context
) );
919 WORD sel
= BX_reg(context
);
920 TRACE( "set selector base address (0x%04x,0x%08lx)\n", sel
, base
);
922 /* check if Win16 app wants to access lower 64K of DOS memory */
923 if (base
< 0x10000 && DOSVM_IsWin16())
926 SetSelectorBase( sel
, base
);
930 case 0x0008: /* Set selector limit */
932 DWORD limit
= MAKELONG( DX_reg(context
), CX_reg(context
) );
933 TRACE( "set selector limit (0x%04x,0x%08lx)\n",
934 BX_reg(context
), limit
);
935 SetSelectorLimit16( BX_reg(context
), limit
);
939 case 0x0009: /* Set selector access rights */
940 TRACE( "set selector access rights(0x%04x,0x%04x)\n",
941 BX_reg(context
), CX_reg(context
) );
942 SelectorAccessRights16( BX_reg(context
), 1, CX_reg(context
) );
945 case 0x000a: /* Allocate selector alias */
946 TRACE( "allocate selector alias (0x%04x)\n", BX_reg(context
) );
947 SET_AX( context
, AllocCStoDSAlias16( BX_reg(context
) ) );
948 if (!AX_reg(context
))
950 SET_AX( context
, 0x8011 ); /* descriptor unavailable */
955 case 0x000b: /* Get descriptor */
956 TRACE( "get descriptor (0x%04x)\n", BX_reg(context
) );
958 LDT_ENTRY
*entry
= (LDT_ENTRY
*)CTX_SEG_OFF_TO_LIN( context
,
961 wine_ldt_get_entry( BX_reg(context
), entry
);
965 case 0x000c: /* Set descriptor */
966 TRACE( "set descriptor (0x%04x)\n", BX_reg(context
) );
968 LDT_ENTRY
*entry
= (LDT_ENTRY
*)CTX_SEG_OFF_TO_LIN( context
,
971 wine_ldt_set_entry( BX_reg(context
), entry
);
975 case 0x000d: /* Allocate specific LDT descriptor */
976 FIXME( "allocate descriptor (0x%04x), stub!\n", BX_reg(context
) );
977 SET_AX( context
, 0x8011 ); /* descriptor unavailable */
978 SET_CFLAG( context
);
981 case 0x000e: /* Get Multiple Descriptors (1.0) */
982 FIXME( "get multiple descriptors - unimplemented\n" );
985 case 0x000f: /* Set Multiple Descriptors (1.0) */
986 FIXME( "set multiple descriptors - unimplemented\n" );
989 case 0x0100: /* Allocate DOS memory block */
990 TRACE( "allocate DOS memory block (0x%x paragraphs)\n", BX_reg(context
) );
992 DWORD dw
= GlobalDOSAlloc16( (DWORD
)BX_reg(context
) << 4 );
994 SET_AX( context
, HIWORD(dw
) );
995 SET_DX( context
, LOWORD(dw
) );
997 SET_AX( context
, 0x0008 ); /* insufficient memory */
998 SET_BX( context
, DOSMEM_Available() >> 4 );
1004 case 0x0101: /* Free DOS memory block */
1005 TRACE( "free DOS memory block (0x%04x)\n", DX_reg(context
) );
1007 WORD error
= GlobalDOSFree16( DX_reg(context
) );
1009 SET_AX( context
, 0x0009 ); /* memory block address invalid */
1010 SET_CFLAG( context
);
1015 case 0x0102: /* Resize DOS Memory Block */
1016 FIXME( "resize DOS memory block (0x%04x, 0x%x paragraphs) - unimplemented\n",
1017 DX_reg(context
), BX_reg(context
) );
1020 case 0x0200: /* get real mode interrupt vector */
1021 TRACE( "get realmode interupt vector (0x%02x)\n",
1024 FARPROC16 proc
= DOSVM_GetRMHandler( BL_reg(context
) );
1025 SET_CX( context
, SELECTOROF(proc
) );
1026 SET_DX( context
, OFFSETOF(proc
) );
1030 case 0x0201: /* set real mode interrupt vector */
1031 TRACE( "set realmode interrupt vector (0x%02x, 0x%04x:0x%04x)\n",
1032 BL_reg(context
), CX_reg(context
), DX_reg(context
) );
1033 DOSVM_SetRMHandler( BL_reg(context
),
1034 (FARPROC16
)MAKESEGPTR(CX_reg(context
), DX_reg(context
)) );
1037 case 0x0202: /* Get Processor Exception Handler Vector */
1038 FIXME( "Get Processor Exception Handler Vector (0x%02x)\n",
1040 if (DOSVM_IsDos32())
1042 SET_CX( context
, 0 );
1047 SET_CX( context
, 0 );
1048 SET_DX( context
, 0 );
1052 case 0x0203: /* Set Processor Exception Handler Vector */
1053 FIXME( "Set Processor Exception Handler Vector (0x%02x)\n",
1057 case 0x0204: /* Get protected mode interrupt vector */
1058 TRACE("get protected mode interrupt handler (0x%02x)\n",
1060 if (DOSVM_IsDos32())
1062 FARPROC48 handler
= DOSVM_GetPMHandler48( BL_reg(context
) );
1063 SET_CX( context
, handler
.selector
);
1064 context
->Edx
= handler
.offset
;
1068 FARPROC16 handler
= DOSVM_GetPMHandler16( BL_reg(context
) );
1069 SET_CX( context
, SELECTOROF(handler
) );
1070 SET_DX( context
, OFFSETOF(handler
) );
1074 case 0x0205: /* Set protected mode interrupt vector */
1075 TRACE("set protected mode interrupt handler (0x%02x,0x%04x:0x%08lx)\n",
1076 BL_reg(context
), CX_reg(context
), context
->Edx
);
1077 if (DOSVM_IsDos32())
1080 handler
.selector
= CX_reg(context
);
1081 handler
.offset
= context
->Edx
;
1082 DOSVM_SetPMHandler48( BL_reg(context
), handler
);
1087 handler
= (FARPROC16
)MAKESEGPTR( CX_reg(context
), DX_reg(context
));
1088 DOSVM_SetPMHandler16( BL_reg(context
), handler
);
1092 case 0x0300: /* Simulate real mode interrupt */
1093 TRACE( "Simulate real mode interrupt %02x.\n", BL_reg(context
));
1094 DOSVM_CallRMInt( context
);
1097 case 0x0301: /* Call real mode procedure with far return */
1098 TRACE( "Call real mode procedure with far return.\n" );
1099 DOSVM_CallRMProc( context
, FALSE
);
1102 case 0x0302: /* Call real mode procedure with interrupt return */
1103 TRACE( "Call real mode procedure with interrupt return.\n" );
1104 DOSVM_CallRMProc( context
, TRUE
);
1107 case 0x0303: /* Allocate Real Mode Callback Address */
1108 TRACE( "Allocate real mode callback address.\n" );
1109 DOSVM_AllocRMCB( context
);
1112 case 0x0304: /* Free Real Mode Callback Address */
1113 TRACE( "Free real mode callback address.\n" );
1114 DOSVM_FreeRMCB( context
);
1117 case 0x0305: /* Get State Save/Restore Addresses */
1118 TRACE("get state save/restore addresses\n");
1119 /* we probably won't need this kind of state saving */
1120 SET_AX( context
, 0 );
1122 /* real mode: just point to the lret */
1123 SET_BX( context
, DOSVM_dpmi_segments
->wrap_seg
);
1124 SET_CX( context
, 2 );
1126 /* protected mode: don't have any handler yet... */
1127 /* FIXME: Use DI in 16-bit DPMI and EDI in 32-bit DPMI */
1128 FIXME("no protected-mode dummy state save/restore handler yet\n");
1129 SET_SI( context
, 0 );
1133 case 0x0306: /* Get Raw Mode Switch Addresses */
1134 TRACE("get raw mode switch addresses\n");
1136 /* real mode, point to standard DPMI return wrapper */
1137 SET_BX( context
, DOSVM_dpmi_segments
->wrap_seg
);
1138 SET_CX( context
, 0 );
1140 /* protected mode, point to DPMI call wrapper */
1141 /* FIXME: Use DI in 16-bit DPMI and EDI in 32-bit DPMI */
1142 /* FIXME: Doesn't work in DPMI32... */
1143 SET_SI( context
, DOSVM_dpmi_segments
->dpmi_sel
);
1144 context
->Edi
= 8; /* offset of the INT 0x31 call */
1147 case 0x0400: /* Get DPMI version */
1148 TRACE("get DPMI version\n");
1153 SET_AX( context
, 0x005a ); /* DPMI version 0.90 */
1154 SET_BX( context
, 0x0005 ); /* Flags: 32-bit, virtual memory */
1155 SET_CL( context
, si
.wProcessorLevel
);
1156 SET_DX( context
, 0x0870 ); /* Master/slave interrupt controller base */
1160 case 0x0401: /* Get DPMI Capabilities (1.0) */
1161 FIXME( "get dpmi capabilities - unimplemented\n");
1164 case 0x0500: /* Get free memory information */
1165 TRACE("get free memory information\n");
1168 void *ptr
= CTX_SEG_OFF_TO_LIN( context
,
1172 mmi
.dwSize
= sizeof( mmi
);
1173 MemManInfo16( &mmi
);
1175 /* the layout is just the same as MEMMANINFO, but without
1178 memcpy( ptr
, ((char*)&mmi
)+4, sizeof(mmi
)-4 );
1182 case 0x0501: /* Allocate memory block */
1184 DWORD size
= MAKELONG( CX_reg(context
), BX_reg(context
) );
1187 TRACE( "allocate memory block (%ld bytes)\n", size
);
1189 ptr
= (BYTE
*)DPMI_xalloc( size
);
1192 SET_AX( context
, 0x8012 ); /* linear memory not available */
1197 SET_BX( context
, HIWORD(ptr
) );
1198 SET_CX( context
, LOWORD(ptr
) );
1199 SET_SI( context
, HIWORD(ptr
) );
1200 SET_DI( context
, LOWORD(ptr
) );
1205 case 0x0502: /* Free memory block */
1207 DWORD handle
= MAKELONG( DI_reg(context
), SI_reg(context
) );
1208 TRACE( "free memory block (0x%08lx)\n", handle
);
1209 DPMI_xfree( (void *)handle
);
1213 case 0x0503: /* Resize memory block */
1215 DWORD size
= MAKELONG( CX_reg(context
), BX_reg(context
) );
1216 DWORD handle
= MAKELONG( DI_reg(context
), SI_reg(context
) );
1219 TRACE( "resize memory block (0x%08lx, %ld bytes)\n", handle
, size
);
1221 ptr
= (BYTE
*)DPMI_xrealloc( (void *)handle
, size
);
1224 SET_AX( context
, 0x8012 ); /* linear memory not available */
1227 SET_BX( context
, HIWORD(ptr
) );
1228 SET_CX( context
, LOWORD(ptr
) );
1229 SET_SI( context
, HIWORD(ptr
) );
1230 SET_DI( context
, LOWORD(ptr
) );
1235 case 0x0507: /* Set page attributes (1.0) */
1236 FIXME( "set page attributes - unimplemented\n" );
1237 break; /* Just ignore it */
1239 case 0x0600: /* Lock linear region */
1240 TRACE( "lock linear region - ignored (no paging)\n" );
1243 case 0x0601: /* Unlock linear region */
1244 TRACE( "unlock linear region - ignored (no paging)\n" );
1247 case 0x0602: /* Mark real mode region as pageable */
1248 TRACE( "mark real mode region as pageable - ignored (no paging)\n" );
1251 case 0x0603: /* Relock real mode region */
1252 TRACE( "relock real mode region - ignored (no paging)\n" );
1255 case 0x0604: /* Get page size */
1256 TRACE("get pagesize\n");
1257 SET_BX( context
, HIWORD(getpagesize()) );
1258 SET_CX( context
, LOWORD(getpagesize()) );
1261 case 0x0700: /* Mark pages as paging candidates */
1262 TRACE( "mark pages as paging candidates - ignored (no paging)\n" );
1265 case 0x0701: /* Discard pages */
1266 TRACE( "discard pages - ignored (no paging)\n" );
1269 case 0x0702: /* Mark page as demand-paging candidate */
1270 TRACE( "mark page as demand-paging candidate - ignored (no paging)\n" );
1273 case 0x0703: /* Discard page contents */
1274 TRACE( "discard page contents - ignored (no paging)\n" );
1277 case 0x0800: /* Physical address mapping */
1278 FIXME( "physical address mapping (0x%08lx) - unimplemented\n",
1279 MAKELONG(CX_reg(context
),BX_reg(context
)) );
1282 case 0x0900: /* Get and Disable Virtual Interrupt State */
1283 TRACE( "Get and Disable Virtual Interrupt State: %ld\n",
1284 NtCurrentTeb()->dpmi_vif
);
1285 SET_AL( context
, NtCurrentTeb()->dpmi_vif
? 1 : 0 );
1286 NtCurrentTeb()->dpmi_vif
= 0;
1289 case 0x0901: /* Get and Enable Virtual Interrupt State */
1290 TRACE( "Get and Enable Virtual Interrupt State: %ld\n",
1291 NtCurrentTeb()->dpmi_vif
);
1292 SET_AL( context
, NtCurrentTeb()->dpmi_vif
? 1 : 0 );
1293 NtCurrentTeb()->dpmi_vif
= 1;
1296 case 0x0902: /* Get Virtual Interrupt State */
1297 TRACE( "Get Virtual Interrupt State: %ld\n",
1298 NtCurrentTeb()->dpmi_vif
);
1299 SET_AL( context
, NtCurrentTeb()->dpmi_vif
? 1 : 0 );
1302 case 0x0e00: /* Get Coprocessor Status (1.0) */
1304 * Return status in AX bits:
1305 * B0 - MPv (MP bit in the virtual MSW/CR0)
1306 * 0 = numeric coprocessor is disabled for this client
1307 * 1 = numeric coprocessor is enabled for this client
1308 * B1 - EMv (EM bit in the virtual MSW/CR0)
1309 * 0 = client is not emulating coprocessor instructions
1310 * 1 = client is emulating coprocessor instructions
1311 * B2 - MPr (MP bit from the actual MSW/CR0)
1312 * 0 = numeric coprocessor is not present
1313 * 1 = numeric coprocessor is present
1314 * B3 - EMr (EM bit from the actual MSW/CR0)
1315 * 0 = host is not emulating coprocessor instructions
1316 * 1 = host is emulating coprocessor instructions
1317 * B4-B7 - coprocessor type
1318 * 00H = no coprocessor
1321 * 04H = 80486 with numeric coprocessor
1322 * 05H-0FH = reserved for future numeric processors
1324 TRACE( "Get Coprocessor Status\n" );
1325 SET_AX( context
, 69 ); /* 486, coprocessor present and enabled */
1328 case 0x0e01: /* Set Coprocessor Emulation (1.0) */
1330 * See function 0x0e00.
1331 * BX bit B0 is new value for MPv.
1332 * BX bit B1 is new value for EMv.
1334 if (BX_reg(context
) != 1)
1335 FIXME( "Set Coprocessor Emulation to %d - unimplemented\n",
1338 TRACE( "Set Coprocessor Emulation - ignored\n" );
1342 INT_BARF( context
, 0x31 );
1343 SET_AX( context
, 0x8001 ); /* unsupported function */