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"
33 #include "wine/debug.h"
34 #include "wine/exception.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(int31
);
39 /* Structure for real-mode callbacks */
61 typedef struct tagRMCB
{
63 DWORD proc_ofs
,proc_sel
;
64 DWORD regs_ofs
,regs_sel
;
68 static RMCB
*FirstRMCB
= NULL
;
69 static WORD dpmi_flag
;
70 static void* lastvalloced
= NULL
;
71 static BYTE DPMI_retval
;
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 /**********************************************************************
87 * Allocate a 64k sized selector corresponding to a real mode segment.
89 static WORD
alloc_pm_selector( WORD seg
, unsigned char flags
)
91 WORD sel
= wine_ldt_alloc_entries( 1 );
96 wine_ldt_set_base( &entry
, (void *)(seg
<< 4) );
97 wine_ldt_set_limit( &entry
, 0xffff );
98 wine_ldt_set_flags( &entry
, flags
);
99 wine_ldt_set_entry( sel
, &entry
);
105 /**********************************************************************
106 * dpmi_exception_handler
108 * Handle EXCEPTION_VM86_STI exceptions generated
109 * when there are pending asynchronous events.
111 static WINE_EXCEPTION_FILTER(dpmi_exception_handler
)
114 EXCEPTION_RECORD
*rec
= GetExceptionInformation()->ExceptionRecord
;
115 CONTEXT
*context
= GetExceptionInformation()->ContextRecord
;
117 if (rec
->ExceptionCode
== EXCEPTION_VM86_STI
)
120 ERR( "Real mode STI caught by protected mode handler!\n" );
121 DOSVM_SendQueuedEvents(context
);
122 return EXCEPTION_CONTINUE_EXECUTION
;
124 else if (rec
->ExceptionCode
== EXCEPTION_VM86_INTx
)
127 ERR( "Real mode INTx caught by protected mode handler!\n" );
128 DPMI_retval
= (BYTE
)rec
->ExceptionInformation
[0];
129 return EXCEPTION_EXECUTE_HANDLER
;
133 return EXCEPTION_CONTINUE_SEARCH
;
137 /**********************************************************************
138 * INT_GetRealModeContext
140 static void INT_GetRealModeContext( REALMODECALL
*call
, CONTEXT86
*context
)
142 context
->Eax
= call
->eax
;
143 context
->Ebx
= call
->ebx
;
144 context
->Ecx
= call
->ecx
;
145 context
->Edx
= call
->edx
;
146 context
->Esi
= call
->esi
;
147 context
->Edi
= call
->edi
;
148 context
->Ebp
= call
->ebp
;
149 context
->EFlags
= call
->fl
| V86_FLAG
;
150 context
->Eip
= call
->ip
;
151 context
->Esp
= call
->sp
;
152 context
->SegCs
= call
->cs
;
153 context
->SegDs
= call
->ds
;
154 context
->SegEs
= call
->es
;
155 context
->SegFs
= call
->fs
;
156 context
->SegGs
= call
->gs
;
157 context
->SegSs
= call
->ss
;
161 /**********************************************************************
162 * INT_SetRealModeContext
164 static void INT_SetRealModeContext( REALMODECALL
*call
, CONTEXT86
*context
)
166 call
->eax
= context
->Eax
;
167 call
->ebx
= context
->Ebx
;
168 call
->ecx
= context
->Ecx
;
169 call
->edx
= context
->Edx
;
170 call
->esi
= context
->Esi
;
171 call
->edi
= context
->Edi
;
172 call
->ebp
= context
->Ebp
;
173 call
->fl
= LOWORD(context
->EFlags
);
174 call
->ip
= LOWORD(context
->Eip
);
175 call
->sp
= LOWORD(context
->Esp
);
176 call
->cs
= context
->SegCs
;
177 call
->ds
= context
->SegDs
;
178 call
->es
= context
->SegEs
;
179 call
->fs
= context
->SegFs
;
180 call
->gs
= context
->SegGs
;
181 call
->ss
= context
->SegSs
;
184 /**********************************************************************
186 * special virtualalloc, allocates lineary monoton growing memory.
187 * (the usual VirtualAlloc does not satisfy that restriction)
189 static LPVOID
DPMI_xalloc( DWORD len
)
192 LPVOID oldlastv
= lastvalloced
;
201 ret
= VirtualAlloc( lastvalloced
, len
,
202 MEM_COMMIT
|MEM_RESERVE
, PAGE_EXECUTE_READWRITE
);
204 lastvalloced
= (char *) lastvalloced
+ 0x10000;
206 /* we failed to allocate one in the first round.
209 if (!xflag
&& (lastvalloced
<oldlastv
))
212 FIXME( "failed to allocate linearly growing memory (%ld bytes), "
213 "using non-linear growing...\n", len
);
217 /* if we even fail to allocate something in the next
220 if ((xflag
==1) && (lastvalloced
>= oldlastv
))
223 if ((xflag
==2) && (lastvalloced
< oldlastv
)) {
224 FIXME( "failed to allocate any memory of %ld bytes!\n", len
);
231 ret
= VirtualAlloc( NULL
, len
,
232 MEM_COMMIT
|MEM_RESERVE
, PAGE_EXECUTE_READWRITE
);
235 lastvalloced
= (LPVOID
)(((DWORD
)ret
+len
+0xffff)&~0xffff);
239 /**********************************************************************
242 static void DPMI_xfree( LPVOID ptr
)
244 VirtualFree( ptr
, 0, MEM_RELEASE
);
247 /**********************************************************************
250 * FIXME: perhaps we could grow this mapped area...
252 static LPVOID
DPMI_xrealloc( LPVOID ptr
, DWORD newsize
)
254 MEMORY_BASIC_INFORMATION mbi
;
257 newptr
= DPMI_xalloc( newsize
);
260 if (!VirtualQuery(ptr
,&mbi
,sizeof(mbi
)))
262 FIXME( "realloc of DPMI_xallocd region %p?\n", ptr
);
266 if (mbi
.State
== MEM_FREE
)
268 FIXME( "realloc of DPMI_xallocd region %p?\n", ptr
);
272 /* We do not shrink allocated memory. most reallocs
273 * only do grows anyway
275 if (newsize
<= mbi
.RegionSize
)
278 memcpy( newptr
, ptr
, mbi
.RegionSize
);
288 void DPMI_CallRMCB32(RMCB
*rmcb
, UINT16 ss
, DWORD esp
, UINT16
*es
, DWORD
*edi
)
289 #if 0 /* original code, which early gccs puke on */
292 __asm__
__volatile__(
300 ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
306 : "=d" (*es
), "=D" (*edi
), "=S" (_clobber
), "=a" (_clobber
), "=c" (_clobber
)
307 : "0" (ss
), "2" (esp
),
308 "4" (rmcb
->regs_sel
), "1" (rmcb
->regs_ofs
),
309 "3" (&rmcb
->proc_ofs
) );
311 #else /* code generated by a gcc new enough */
313 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32
,
318 "movl 0x8(%ebp),%eax\n\t"
319 "movl 0x10(%ebp),%esi\n\t"
320 "movl 0xc(%ebp),%edx\n\t"
321 "movl 0x10(%eax),%ecx\n\t"
322 "movl 0xc(%eax),%edi\n\t"
331 ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
337 "movl 0x14(%ebp),%eax\n\t"
338 "movw %dx,(%eax)\n\t"
339 "movl 0x18(%ebp),%edx\n\t"
340 "movl %edi,(%edx)\n\t"
347 #endif /* __i386__ */
349 /**********************************************************************
352 * This routine does the hard work of calling a callback procedure.
354 static void DPMI_CallRMCBProc( CONTEXT86
*context
, RMCB
*rmcb
, WORD flag
)
356 DWORD old_vif
= NtCurrentTeb()->dpmi_vif
;
358 /* Disable virtual interrupts. */
359 NtCurrentTeb()->dpmi_vif
= 0;
361 if (wine_ldt_is_system( rmcb
->proc_sel
)) {
362 /* Wine-internal RMCB, call directly */
363 ((RMCBPROC
)rmcb
->proc_ofs
)(context
);
369 INT_SetRealModeContext(MapSL(MAKESEGPTR( rmcb
->regs_sel
, rmcb
->regs_ofs
)), context
);
370 ss
= alloc_pm_selector( context
->SegSs
, WINE_LDT_FLAGS_DATA
);
373 FIXME("untested!\n");
375 /* The called proc ends with an IRET, and takes these parameters:
376 * DS:ESI = pointer to real-mode SS:SP
377 * ES:EDI = pointer to real-mode call structure
379 * ES:EDI = pointer to real-mode call structure (may be a copy)
380 * It is the proc's responsibility to change the return CS:IP in the
381 * real-mode call structure. */
383 /* 32-bit DPMI client */
384 DPMI_CallRMCB32(rmcb
, ss
, esp
, &es
, &edi
);
386 /* 16-bit DPMI client */
387 CONTEXT86 ctx
= *context
;
388 ctx
.SegCs
= rmcb
->proc_sel
;
389 ctx
.Eip
= rmcb
->proc_ofs
;
392 ctx
.SegEs
= rmcb
->regs_sel
;
393 ctx
.Edi
= rmcb
->regs_ofs
;
394 /* FIXME: I'm pretty sure this isn't right - should push flags first */
395 WOWCallback16Ex( 0, WCB16_REGS
, 0, NULL
, (DWORD
*)&ctx
);
399 wine_ldt_free_entries( ss
, 1 );
400 INT_GetRealModeContext( MapSL( MAKESEGPTR( es
, edi
)), context
);
402 ERR("RMCBs only implemented for i386\n");
404 } __EXCEPT(dpmi_exception_handler
) { } __ENDTRY
406 /* Restore virtual interrupt flag. */
407 NtCurrentTeb()->dpmi_vif
= old_vif
;
411 /**********************************************************************
414 * This routine does the hard work of calling a real mode procedure.
416 int DPMI_CallRMProc( CONTEXT86
*context
, LPWORD stack
, int args
, int iret
)
419 LPVOID addr
= NULL
; /* avoid gcc warning */
421 int alloc
= 0, already
= 0;
424 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
425 context
->Eax
, context
->Ebx
, context
->Ecx
, context
->Edx
);
426 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
427 context
->Esi
, context
->Edi
, context
->SegEs
, context
->SegDs
,
428 context
->SegCs
, LOWORD(context
->Eip
), args
, iret
?"IRET":"FAR" );
432 /* there might be some code that just jumps to RMCBs or the like,
433 in which case following the jumps here might get us to a shortcut */
434 code
= CTX_SEG_OFF_TO_LIN(context
, context
->SegCs
, context
->Eip
);
436 case 0xe9: /* JMP NEAR */
437 context
->Eip
+= 3 + *(WORD
*)(code
+1);
438 /* yeah, I know these gotos don't look good... */
439 goto callrmproc_again
;
440 case 0xea: /* JMP FAR */
441 context
->Eip
= *(WORD
*)(code
+1);
442 context
->SegCs
= *(WORD
*)(code
+3);
443 /* ...but since the label is there anyway... */
444 goto callrmproc_again
;
445 case 0xeb: /* JMP SHORT */
446 context
->Eip
+= 2 + *(signed char *)(code
+1);
447 /* ...because of other gotos below, so... */
448 goto callrmproc_again
;
451 /* shortcut for chaining to internal interrupt handlers */
452 if ((context
->SegCs
== 0xF000) && iret
)
454 DOSVM_CallBuiltinHandler( context
, LOWORD(context
->Eip
)/4 );
458 /* shortcut for RMCBs */
459 CurrRMCB
= FirstRMCB
;
461 while (CurrRMCB
&& (HIWORD(CurrRMCB
->address
) != context
->SegCs
))
462 CurrRMCB
= CurrRMCB
->next
;
464 if (!CurrRMCB
&& !MZ_Current())
466 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
467 TRACE("creating VM86 task\n");
471 if (!context
->SegSs
) {
472 alloc
= 1; /* allocate default stack */
473 stack16
= addr
= DOSMEM_AllocBlock( 64, (UINT16
*)&(context
->SegSs
) );
477 ERR("could not allocate default stack\n");
481 stack16
= CTX_SEG_OFF_TO_LIN(context
, context
->SegSs
, context
->Esp
);
483 context
->Esp
-= (args
+ (iret
?1:0)) * sizeof(WORD
);
485 if (args
) memcpy(stack16
, stack
, args
*sizeof(WORD
) );
486 /* push flags if iret */
489 *stack16
= LOWORD(context
->EFlags
);
491 /* push return address (return to interrupt wrapper) */
492 *(--stack16
) = DOSVM_dpmi_segments
->wrap_seg
;
495 context
->Esp
-= 2*sizeof(WORD
);
500 /* RMCB call, invoke protected-mode handler directly */
501 DPMI_CallRMCBProc(context
, CurrRMCB
, dpmi_flag
);
502 /* check if we returned to where we thought we would */
503 if ((context
->SegCs
!= DOSVM_dpmi_segments
->wrap_seg
) ||
504 (LOWORD(context
->Eip
) != 0)) {
505 /* we need to continue at different address in real-mode space,
506 so we need to set it all up for real mode again */
507 goto callrmproc_again
;
510 TRACE("entering real mode...\n");
511 DOSVM_Enter( context
);
512 TRACE("returned from real-mode call\n");
514 if (alloc
) DOSMEM_FreeBlock( addr
);
519 /**********************************************************************
520 * CallRMInt (WINEDOS.@)
522 void WINAPI
DOSVM_CallRMInt( CONTEXT86
*context
)
524 CONTEXT86 realmode_ctx
;
525 FARPROC16 rm_int
= DOSVM_GetRMHandler( BL_reg(context
) );
526 REALMODECALL
*call
= CTX_SEG_OFF_TO_LIN( context
,
529 INT_GetRealModeContext( call
, &realmode_ctx
);
531 /* we need to check if a real-mode program has hooked the interrupt */
532 if (HIWORD(rm_int
)!=0xF000) {
533 /* yup, which means we need to switch to real mode... */
534 realmode_ctx
.SegCs
= HIWORD(rm_int
);
535 realmode_ctx
.Eip
= LOWORD(rm_int
);
536 if (DPMI_CallRMProc( &realmode_ctx
, NULL
, 0, TRUE
))
539 RESET_CFLAG(context
);
540 /* use the IP we have instead of BL_reg, in case some apps
541 decide to move interrupts around for whatever reason... */
542 DOSVM_CallBuiltinHandler( &realmode_ctx
, LOWORD(rm_int
)/4 );
544 INT_SetRealModeContext( call
, &realmode_ctx
);
548 /**********************************************************************
549 * CallRMProc (WINEDOS.@)
551 void WINAPI
DOSVM_CallRMProc( CONTEXT86
*context
, int iret
)
553 REALMODECALL
*p
= CTX_SEG_OFF_TO_LIN( context
,
558 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
559 p
->eax
, p
->ebx
, p
->ecx
, p
->edx
);
560 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
561 p
->esi
, p
->edi
, p
->es
, p
->ds
, p
->cs
, p
->ip
, CX_reg(context
), iret
?"IRET":"FAR" );
563 if (!(p
->cs
) && !(p
->ip
)) { /* remove this check
564 if Int21/6501 case map function
565 has been implemented */
569 INT_GetRealModeContext(p
, &context16
);
570 DPMI_CallRMProc( &context16
, ((LPWORD
)MapSL(MAKESEGPTR(context
->SegSs
, LOWORD(context
->Esp
))))+3,
571 CX_reg(context
), iret
);
572 INT_SetRealModeContext(p
, &context16
);
576 /* (see dosmem.c, function DOSMEM_InitDPMI) */
577 static void StartPM( CONTEXT86
*context
)
579 UINT16 cs
, ss
, ds
, es
;
581 DWORD psp_ofs
= (DWORD
)(DOSVM_psp
<<4);
582 PDB16
*psp
= (PDB16
*)psp_ofs
;
583 HANDLE16 env_seg
= psp
->environment
;
584 unsigned char selflags
= WINE_LDT_FLAGS_DATA
;
586 RESET_CFLAG(context
);
587 dpmi_flag
= AX_reg(context
);
588 /* our mode switch wrapper have placed the desired CS into DX */
589 cs
= alloc_pm_selector( context
->Edx
, WINE_LDT_FLAGS_CODE
);
590 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
591 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
592 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
593 32-bit code using this stack. */
594 if (dpmi_flag
& 1) selflags
|= WINE_LDT_FLAGS_32BIT
;
595 ss
= alloc_pm_selector( context
->SegSs
, selflags
);
596 /* do the same for the data segments, just in case */
597 if (context
->SegDs
== context
->SegSs
) ds
= ss
;
598 else ds
= alloc_pm_selector( context
->SegDs
, selflags
);
599 es
= alloc_pm_selector( DOSVM_psp
, selflags
);
600 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
601 psp
->environment
= alloc_pm_selector( env_seg
, WINE_LDT_FLAGS_DATA
);
604 pm_ctx
.SegCs
= DOSVM_dpmi_segments
->dpmi_sel
;
605 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
610 pm_ctx
.SegFs
= wine_get_fs();
611 pm_ctx
.SegGs
= wine_get_gs();
612 pm_ctx
.EFlags
&= ~V86_FLAG
;
614 TRACE("DOS program is now entering %d-bit protected mode\n",
615 DOSVM_IsDos32() ? 32 : 16);
619 WOWCallback16Ex( 0, WCB16_REGS
, 0, NULL
, (DWORD
*)&pm_ctx
);
621 __EXCEPT(dpmi_exception_handler
)
626 TRACE( "Protected mode DOS program is terminating\n" );
629 * FIXME: Instead of calling ExitThread, we should release all
630 * allocated protected mode resources and call MZ_Exit
631 * using real mode context. See DPMI specification.
633 ExitThread( DPMI_retval
);
636 wine_ldt_free_entries( psp
->environment
, 1 );
637 psp
->environment
= env_seg
;
638 wine_ldt_free_entries(es
,1);
639 if (ds
!= ss
) wine_ldt_free_entries(ds
,1);
640 wine_ldt_free_entries(ss
,1);
641 wine_ldt_free_entries(cs
,1);
645 static RMCB
*DPMI_AllocRMCB( void )
647 RMCB
*NewRMCB
= HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB
));
652 LPVOID RMCBmem
= DOSMEM_AllocBlock(4, &uParagraph
);
655 *p
++ = 0xcd; /* RMCB: */
656 *p
++ = 0x31; /* int $0x31 */
657 /* it is the called procedure's task to change the return CS:EIP
658 the DPMI 0.9 spec states that if it doesn't, it will be called again */
660 *p
++ = 0xfc; /* jmp RMCB */
661 NewRMCB
->address
= MAKELONG(0, uParagraph
);
662 NewRMCB
->next
= FirstRMCB
;
669 FARPROC16 WINAPI
DPMI_AllocInternalRMCB( RMCBPROC proc
)
671 RMCB
*NewRMCB
= DPMI_AllocRMCB();
674 NewRMCB
->proc_ofs
= (DWORD
)proc
;
675 NewRMCB
->proc_sel
= 0;
676 NewRMCB
->regs_ofs
= 0;
677 NewRMCB
->regs_sel
= 0;
678 return (FARPROC16
)(NewRMCB
->address
);
684 static int DPMI_FreeRMCB( DWORD address
)
686 RMCB
*CurrRMCB
= FirstRMCB
;
687 RMCB
*PrevRMCB
= NULL
;
689 while (CurrRMCB
&& (CurrRMCB
->address
!= address
))
692 CurrRMCB
= CurrRMCB
->next
;
697 PrevRMCB
->next
= CurrRMCB
->next
;
699 FirstRMCB
= CurrRMCB
->next
;
700 DOSMEM_FreeBlock(PTR_REAL_TO_LIN(SELECTOROF(CurrRMCB
->address
),OFFSETOF(CurrRMCB
->address
)));
701 HeapFree(GetProcessHeap(), 0, CurrRMCB
);
708 void WINAPI
DPMI_FreeInternalRMCB( FARPROC16 proc
)
710 DPMI_FreeRMCB( (DWORD
)proc
);
714 /**********************************************************************
715 * DOSVM_RawModeSwitchHandler
717 * DPMI Raw Mode Switch handler
719 void WINAPI
DOSVM_RawModeSwitchHandler( CONTEXT86
*context
)
724 /* initialize real-mode context as per spec */
725 memset(&rm_ctx
, 0, sizeof(rm_ctx
));
726 rm_ctx
.SegDs
= AX_reg(context
);
727 rm_ctx
.SegEs
= CX_reg(context
);
728 rm_ctx
.SegSs
= DX_reg(context
);
729 rm_ctx
.Esp
= context
->Ebx
;
730 rm_ctx
.SegCs
= SI_reg(context
);
731 rm_ctx
.Eip
= context
->Edi
;
732 rm_ctx
.Ebp
= context
->Ebp
;
736 /* Copy interrupt state. */
737 if (NtCurrentTeb()->dpmi_vif
)
738 rm_ctx
.EFlags
= V86_FLAG
| VIF_MASK
;
740 rm_ctx
.EFlags
= V86_FLAG
;
742 /* enter real mode again */
743 TRACE("re-entering real mode at %04lx:%04lx\n",rm_ctx
.SegCs
,rm_ctx
.Eip
);
744 ret
= DOSVM_Enter( &rm_ctx
);
745 /* when the real-mode stuff call its mode switch address,
746 DOSVM_Enter will return and we will continue here */
750 /* if the sync was lost, there's no way to recover */
754 /* alter protected-mode context as per spec */
755 context
->SegDs
= LOWORD(rm_ctx
.Eax
);
756 context
->SegEs
= LOWORD(rm_ctx
.Ecx
);
757 context
->SegSs
= LOWORD(rm_ctx
.Edx
);
758 context
->Esp
= rm_ctx
.Ebx
;
759 context
->SegCs
= LOWORD(rm_ctx
.Esi
);
760 context
->Eip
= rm_ctx
.Edi
;
761 context
->Ebp
= rm_ctx
.Ebp
;
765 /* Copy interrupt state. */
766 if (rm_ctx
.EFlags
& VIF_MASK
)
767 NtCurrentTeb()->dpmi_vif
= 1;
769 NtCurrentTeb()->dpmi_vif
= 0;
771 /* Return to new address and hope that we didn't mess up */
772 TRACE("re-entering protected mode at %04lx:%08lx\n",
773 context
->SegCs
, context
->Eip
);
777 /**********************************************************************
778 * AllocRMCB (WINEDOS.@)
780 void WINAPI
DOSVM_AllocRMCB( CONTEXT86
*context
)
782 RMCB
*NewRMCB
= DPMI_AllocRMCB();
784 TRACE("Function to call: %04x:%04x\n", (WORD
)context
->SegDs
, SI_reg(context
) );
788 NewRMCB
->proc_ofs
= DOSVM_IsDos32() ? context
->Esi
: LOWORD(context
->Esi
);
789 NewRMCB
->proc_sel
= context
->SegDs
;
790 NewRMCB
->regs_ofs
= DOSVM_IsDos32() ? context
->Edi
: LOWORD(context
->Edi
);
791 NewRMCB
->regs_sel
= context
->SegEs
;
792 SET_CX( context
, HIWORD(NewRMCB
->address
) );
793 SET_DX( context
, LOWORD(NewRMCB
->address
) );
797 SET_AX( context
, 0x8015 ); /* callback unavailable */
803 /**********************************************************************
804 * FreeRMCB (WINEDOS.@)
806 void WINAPI
DOSVM_FreeRMCB( CONTEXT86
*context
)
808 FIXME("callback address: %04x:%04x\n",
809 CX_reg(context
), DX_reg(context
));
811 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context
), CX_reg(context
)))) {
812 SET_AX( context
, 0x8024 ); /* invalid callback address */
818 /**********************************************************************
819 * DOSVM_CheckWrappers
821 * Check if this was really a wrapper call instead of an interrupt.
823 BOOL
DOSVM_CheckWrappers( CONTEXT86
*context
)
825 if (context
->SegCs
==DOSVM_dpmi_segments
->dpmi_seg
) {
826 /* This is the protected mode switch */
830 else if (context
->SegCs
==DOSVM_dpmi_segments
->xms_seg
)
832 /* This is the XMS driver entry point */
833 XMS_Handler(context
);
839 RMCB
*CurrRMCB
= FirstRMCB
;
841 while (CurrRMCB
&& (HIWORD(CurrRMCB
->address
) != context
->SegCs
))
842 CurrRMCB
= CurrRMCB
->next
;
845 /* RMCB call, propagate to protected-mode handler */
846 DPMI_CallRMCBProc(context
, CurrRMCB
, dpmi_flag
);
854 /**********************************************************************
855 * DOSVM_Int31Handler (WINEDOS16.149)
857 * Handler for int 31h (DPMI).
859 void WINAPI
DOSVM_Int31Handler( CONTEXT86
*context
)
861 RESET_CFLAG(context
);
862 switch(AX_reg(context
))
864 case 0x0000: /* Allocate LDT descriptors */
865 TRACE( "allocate LDT descriptors (%d)\n", CX_reg(context
) );
867 WORD sel
= AllocSelectorArray16( CX_reg(context
) );
871 SET_AX( context
, 0x8011 ); /* descriptor unavailable */
872 SET_CFLAG( context
);
876 TRACE( "success, array starts at 0x%04x\n", sel
);
877 SET_AX( context
, sel
);
882 case 0x0001: /* Free LDT descriptor */
883 TRACE( "free LDT descriptor (0x%04x)\n", BX_reg(context
) );
884 if (FreeSelector16( BX_reg(context
) ))
886 SET_AX( context
, 0x8022 ); /* invalid selector */
887 SET_CFLAG( context
);
891 /* If a segment register contains the selector being freed, */
892 /* set it to zero. */
893 if (!((context
->SegDs
^BX_reg(context
)) & ~3)) context
->SegDs
= 0;
894 if (!((context
->SegEs
^BX_reg(context
)) & ~3)) context
->SegEs
= 0;
895 if (!((context
->SegFs
^BX_reg(context
)) & ~3)) context
->SegFs
= 0;
896 if (!((context
->SegGs
^BX_reg(context
)) & ~3)) context
->SegGs
= 0;
900 case 0x0002: /* Real mode segment to descriptor */
901 TRACE( "real mode segment to descriptor (0x%04x)\n", BX_reg(context
) );
903 WORD entryPoint
= 0; /* KERNEL entry point for descriptor */
904 switch(BX_reg(context
))
906 case 0x0000: entryPoint
= 183; break; /* __0000H */
907 case 0x0040: entryPoint
= 193; break; /* __0040H */
908 case 0xa000: entryPoint
= 174; break; /* __A000H */
909 case 0xb000: entryPoint
= 181; break; /* __B000H */
910 case 0xb800: entryPoint
= 182; break; /* __B800H */
911 case 0xc000: entryPoint
= 195; break; /* __C000H */
912 case 0xd000: entryPoint
= 179; break; /* __D000H */
913 case 0xe000: entryPoint
= 190; break; /* __E000H */
914 case 0xf000: entryPoint
= 194; break; /* __F000H */
916 SET_AX( context
, DOSMEM_AllocSelector(BX_reg(context
)) );
921 FARPROC16 proc
= GetProcAddress16( GetModuleHandle16( "KERNEL" ),
922 (LPCSTR
)(ULONG_PTR
)entryPoint
);
923 SET_AX( context
, LOWORD(proc
) );
928 case 0x0003: /* Get next selector increment */
929 TRACE("get selector increment (__AHINCR)\n");
930 context
->Eax
= __AHINCR
;
933 case 0x0004: /* Lock selector (not supported) */
934 FIXME("lock selector not supported\n");
935 context
->Eax
= 0; /* FIXME: is this a correct return value? */
938 case 0x0005: /* Unlock selector (not supported) */
939 FIXME("unlock selector not supported\n");
940 context
->Eax
= 0; /* FIXME: is this a correct return value? */
943 case 0x0006: /* Get selector base address */
944 TRACE( "get selector base address (0x%04x)\n", BX_reg(context
) );
947 WORD sel
= BX_reg(context
);
948 wine_ldt_get_entry( sel
, &entry
);
949 if (wine_ldt_is_empty(&entry
))
951 context
->Eax
= 0x8022; /* invalid selector */
956 void *base
= wine_ldt_get_base(&entry
);
957 SET_CX( context
, HIWORD(base
) );
958 SET_DX( context
, LOWORD(base
) );
963 case 0x0007: /* Set selector base address */
965 DWORD base
= MAKELONG( DX_reg(context
), CX_reg(context
) );
966 WORD sel
= BX_reg(context
);
967 TRACE( "set selector base address (0x%04x,0x%08lx)\n", sel
, base
);
969 /* check if Win16 app wants to access lower 64K of DOS memory */
970 if (base
< 0x10000 && DOSVM_IsWin16())
971 DOSMEM_MapDosLayout();
973 SetSelectorBase( sel
, base
);
977 case 0x0008: /* Set selector limit */
979 DWORD limit
= MAKELONG( DX_reg(context
), CX_reg(context
) );
980 TRACE( "set selector limit (0x%04x,0x%08lx)\n",
981 BX_reg(context
), limit
);
982 SetSelectorLimit16( BX_reg(context
), limit
);
986 case 0x0009: /* Set selector access rights */
987 TRACE( "set selector access rights(0x%04x,0x%04x)\n",
988 BX_reg(context
), CX_reg(context
) );
989 SelectorAccessRights16( BX_reg(context
), 1, CX_reg(context
) );
992 case 0x000a: /* Allocate selector alias */
993 TRACE( "allocate selector alias (0x%04x)\n", BX_reg(context
) );
994 SET_AX( context
, AllocCStoDSAlias16( BX_reg(context
) ) );
995 if (!AX_reg(context
))
997 SET_AX( context
, 0x8011 ); /* descriptor unavailable */
1002 case 0x000b: /* Get descriptor */
1003 TRACE( "get descriptor (0x%04x)\n", BX_reg(context
) );
1005 LDT_ENTRY
*entry
= (LDT_ENTRY
*)CTX_SEG_OFF_TO_LIN( context
,
1008 wine_ldt_get_entry( BX_reg(context
), entry
);
1012 case 0x000c: /* Set descriptor */
1013 TRACE( "set descriptor (0x%04x)\n", BX_reg(context
) );
1015 LDT_ENTRY
*entry
= (LDT_ENTRY
*)CTX_SEG_OFF_TO_LIN( context
,
1018 wine_ldt_set_entry( BX_reg(context
), entry
);
1022 case 0x000d: /* Allocate specific LDT descriptor */
1023 FIXME( "allocate descriptor (0x%04x), stub!\n", BX_reg(context
) );
1024 SET_AX( context
, 0x8011 ); /* descriptor unavailable */
1025 SET_CFLAG( context
);
1028 case 0x000e: /* Get Multiple Descriptors (1.0) */
1029 FIXME( "get multiple descriptors - unimplemented\n" );
1032 case 0x000f: /* Set Multiple Descriptors (1.0) */
1033 FIXME( "set multiple descriptors - unimplemented\n" );
1036 case 0x0100: /* Allocate DOS memory block */
1037 TRACE( "allocate DOS memory block (0x%x paragraphs)\n", BX_reg(context
) );
1039 DWORD dw
= GlobalDOSAlloc16( (DWORD
)BX_reg(context
) << 4 );
1041 SET_AX( context
, HIWORD(dw
) );
1042 SET_DX( context
, LOWORD(dw
) );
1044 SET_AX( context
, 0x0008 ); /* insufficient memory */
1045 SET_BX( context
, DOSMEM_Available() >> 4 );
1051 case 0x0101: /* Free DOS memory block */
1052 TRACE( "free DOS memory block (0x%04x)\n", DX_reg(context
) );
1054 WORD error
= GlobalDOSFree16( DX_reg(context
) );
1056 SET_AX( context
, 0x0009 ); /* memory block address invalid */
1057 SET_CFLAG( context
);
1062 case 0x0102: /* Resize DOS Memory Block */
1063 FIXME( "resize DOS memory block (0x%04x, 0x%x paragraphs) - unimplemented\n",
1064 DX_reg(context
), BX_reg(context
) );
1067 case 0x0200: /* get real mode interrupt vector */
1068 TRACE( "get realmode interupt vector (0x%02x)\n",
1071 FARPROC16 proc
= DOSVM_GetRMHandler( BL_reg(context
) );
1072 SET_CX( context
, SELECTOROF(proc
) );
1073 SET_DX( context
, OFFSETOF(proc
) );
1077 case 0x0201: /* set real mode interrupt vector */
1078 TRACE( "set realmode interrupt vector (0x%02x, 0x%04x:0x%04x)\n",
1079 BL_reg(context
), CX_reg(context
), DX_reg(context
) );
1080 DOSVM_SetRMHandler( BL_reg(context
),
1081 (FARPROC16
)MAKESEGPTR(CX_reg(context
), DX_reg(context
)) );
1084 case 0x0202: /* Get Processor Exception Handler Vector */
1085 FIXME( "Get Processor Exception Handler Vector (0x%02x)\n",
1087 if (DOSVM_IsDos32())
1089 SET_CX( context
, 0 );
1094 SET_CX( context
, 0 );
1095 SET_DX( context
, 0 );
1099 case 0x0203: /* Set Processor Exception Handler Vector */
1100 FIXME( "Set Processor Exception Handler Vector (0x%02x)\n",
1104 case 0x0204: /* Get protected mode interrupt vector */
1105 TRACE("get protected mode interrupt handler (0x%02x)\n",
1107 if (DOSVM_IsDos32())
1109 FARPROC48 handler
= DOSVM_GetPMHandler48( BL_reg(context
) );
1110 SET_CX( context
, handler
.selector
);
1111 context
->Edx
= handler
.offset
;
1115 FARPROC16 handler
= DOSVM_GetPMHandler16( BL_reg(context
) );
1116 SET_CX( context
, SELECTOROF(handler
) );
1117 SET_DX( context
, OFFSETOF(handler
) );
1121 case 0x0205: /* Set protected mode interrupt vector */
1122 TRACE("set protected mode interrupt handler (0x%02x,0x%04x:0x%08lx)\n",
1123 BL_reg(context
), CX_reg(context
), context
->Edx
);
1124 if (DOSVM_IsDos32())
1127 handler
.selector
= CX_reg(context
);
1128 handler
.offset
= context
->Edx
;
1129 DOSVM_SetPMHandler48( BL_reg(context
), handler
);
1134 handler
= (FARPROC16
)MAKESEGPTR( CX_reg(context
), DX_reg(context
));
1135 DOSVM_SetPMHandler16( BL_reg(context
), handler
);
1139 case 0x0300: /* Simulate real mode interrupt */
1140 TRACE( "Simulate real mode interrupt %02x.\n", BL_reg(context
));
1141 DOSVM_CallRMInt( context
);
1144 case 0x0301: /* Call real mode procedure with far return */
1145 TRACE( "Call real mode procedure with far return.\n" );
1146 DOSVM_CallRMProc( context
, FALSE
);
1149 case 0x0302: /* Call real mode procedure with interrupt return */
1150 TRACE( "Call real mode procedure with interrupt return.\n" );
1151 DOSVM_CallRMProc( context
, TRUE
);
1154 case 0x0303: /* Allocate Real Mode Callback Address */
1155 TRACE( "Allocate real mode callback address.\n" );
1156 DOSVM_AllocRMCB( context
);
1159 case 0x0304: /* Free Real Mode Callback Address */
1160 TRACE( "Free real mode callback address.\n" );
1161 DOSVM_FreeRMCB( context
);
1164 case 0x0305: /* Get State Save/Restore Addresses */
1165 TRACE("get state save/restore addresses\n");
1166 /* we probably won't need this kind of state saving */
1167 SET_AX( context
, 0 );
1169 /* real mode: just point to the lret */
1170 SET_BX( context
, DOSVM_dpmi_segments
->wrap_seg
);
1171 SET_CX( context
, 2 );
1173 /* protected mode: don't have any handler yet... */
1174 /* FIXME: Use DI in 16-bit DPMI and EDI in 32-bit DPMI */
1175 FIXME("no protected-mode dummy state save/restore handler yet\n");
1176 SET_SI( context
, 0 );
1180 case 0x0306: /* Get Raw Mode Switch Addresses */
1181 TRACE("get raw mode switch addresses\n");
1183 /* real mode, point to standard DPMI return wrapper */
1184 SET_BX( context
, DOSVM_dpmi_segments
->wrap_seg
);
1185 SET_CX( context
, 0 );
1187 /* protected mode, point to DPMI call wrapper */
1188 /* FIXME: Use DI in 16-bit DPMI and EDI in 32-bit DPMI */
1189 /* FIXME: Doesn't work in DPMI32... */
1190 SET_SI( context
, DOSVM_dpmi_segments
->dpmi_sel
);
1191 context
->Edi
= 8; /* offset of the INT 0x31 call */
1194 case 0x0400: /* Get DPMI version */
1195 TRACE("get DPMI version\n");
1200 SET_AX( context
, 0x005a ); /* DPMI version 0.90 */
1201 SET_BX( context
, 0x0005 ); /* Flags: 32-bit, virtual memory */
1202 SET_CL( context
, si
.wProcessorLevel
);
1203 SET_DX( context
, 0x0870 ); /* Master/slave interrupt controller base */
1207 case 0x0401: /* Get DPMI Capabilities (1.0) */
1208 FIXME( "get dpmi capabilities - unimplemented\n");
1211 case 0x0500: /* Get free memory information */
1212 TRACE("get free memory information\n");
1214 MEMORYSTATUS status
;
1216 /* the layout is just the same as MEMMANINFO, but without
1221 DWORD dwLargestFreeBlock
;
1222 DWORD dwMaxPagesAvailable
;
1223 DWORD dwMaxPagesLockable
;
1224 DWORD dwTotalLinearSpace
;
1225 DWORD dwTotalUnlockedPages
;
1228 DWORD dwFreeLinearSpace
;
1229 DWORD dwSwapFilePages
;
1231 } *info
= CTX_SEG_OFF_TO_LIN( context
, context
->SegEs
, context
->Edi
);
1233 GlobalMemoryStatus( &status
);
1234 info
->wPageSize
= getpagesize();
1235 info
->dwLargestFreeBlock
= status
.dwAvailVirtual
;
1236 info
->dwMaxPagesAvailable
= info
->dwLargestFreeBlock
/ info
->wPageSize
;
1237 info
->dwMaxPagesLockable
= info
->dwMaxPagesAvailable
;
1238 info
->dwTotalLinearSpace
= status
.dwTotalVirtual
/ info
->wPageSize
;
1239 info
->dwTotalUnlockedPages
= info
->dwTotalLinearSpace
;
1240 info
->dwFreePages
= info
->dwMaxPagesAvailable
;
1241 info
->dwTotalPages
= info
->dwTotalLinearSpace
;
1242 info
->dwFreeLinearSpace
= info
->dwMaxPagesAvailable
;
1243 info
->dwSwapFilePages
= status
.dwTotalPageFile
/ info
->wPageSize
;
1247 case 0x0501: /* Allocate memory block */
1249 DWORD size
= MAKELONG( CX_reg(context
), BX_reg(context
) );
1252 TRACE( "allocate memory block (%ld bytes)\n", size
);
1254 ptr
= (BYTE
*)DPMI_xalloc( size
);
1257 SET_AX( context
, 0x8012 ); /* linear memory not available */
1262 SET_BX( context
, HIWORD(ptr
) );
1263 SET_CX( context
, LOWORD(ptr
) );
1264 SET_SI( context
, HIWORD(ptr
) );
1265 SET_DI( context
, LOWORD(ptr
) );
1270 case 0x0502: /* Free memory block */
1272 DWORD handle
= MAKELONG( DI_reg(context
), SI_reg(context
) );
1273 TRACE( "free memory block (0x%08lx)\n", handle
);
1274 DPMI_xfree( (void *)handle
);
1278 case 0x0503: /* Resize memory block */
1280 DWORD size
= MAKELONG( CX_reg(context
), BX_reg(context
) );
1281 DWORD handle
= MAKELONG( DI_reg(context
), SI_reg(context
) );
1284 TRACE( "resize memory block (0x%08lx, %ld bytes)\n", handle
, size
);
1286 ptr
= (BYTE
*)DPMI_xrealloc( (void *)handle
, size
);
1289 SET_AX( context
, 0x8012 ); /* linear memory not available */
1292 SET_BX( context
, HIWORD(ptr
) );
1293 SET_CX( context
, LOWORD(ptr
) );
1294 SET_SI( context
, HIWORD(ptr
) );
1295 SET_DI( context
, LOWORD(ptr
) );
1300 case 0x0507: /* Set page attributes (1.0) */
1301 FIXME( "set page attributes - unimplemented\n" );
1302 break; /* Just ignore it */
1304 case 0x0600: /* Lock linear region */
1305 TRACE( "lock linear region - ignored (no paging)\n" );
1308 case 0x0601: /* Unlock linear region */
1309 TRACE( "unlock linear region - ignored (no paging)\n" );
1312 case 0x0602: /* Mark real mode region as pageable */
1313 TRACE( "mark real mode region as pageable - ignored (no paging)\n" );
1316 case 0x0603: /* Relock real mode region */
1317 TRACE( "relock real mode region - ignored (no paging)\n" );
1320 case 0x0604: /* Get page size */
1321 TRACE("get pagesize\n");
1322 SET_BX( context
, HIWORD(getpagesize()) );
1323 SET_CX( context
, LOWORD(getpagesize()) );
1326 case 0x0700: /* Mark pages as paging candidates */
1327 TRACE( "mark pages as paging candidates - ignored (no paging)\n" );
1330 case 0x0701: /* Discard pages */
1331 TRACE( "discard pages - ignored (no paging)\n" );
1334 case 0x0702: /* Mark page as demand-paging candidate */
1335 TRACE( "mark page as demand-paging candidate - ignored (no paging)\n" );
1338 case 0x0703: /* Discard page contents */
1339 TRACE( "discard page contents - ignored (no paging)\n" );
1342 case 0x0800: /* Physical address mapping */
1343 FIXME( "physical address mapping (0x%08lx) - unimplemented\n",
1344 MAKELONG(CX_reg(context
),BX_reg(context
)) );
1347 case 0x0900: /* Get and Disable Virtual Interrupt State */
1348 TRACE( "Get and Disable Virtual Interrupt State: %ld\n",
1349 NtCurrentTeb()->dpmi_vif
);
1350 SET_AL( context
, NtCurrentTeb()->dpmi_vif
? 1 : 0 );
1351 NtCurrentTeb()->dpmi_vif
= 0;
1354 case 0x0901: /* Get and Enable Virtual Interrupt State */
1355 TRACE( "Get and Enable Virtual Interrupt State: %ld\n",
1356 NtCurrentTeb()->dpmi_vif
);
1357 SET_AL( context
, NtCurrentTeb()->dpmi_vif
? 1 : 0 );
1358 NtCurrentTeb()->dpmi_vif
= 1;
1361 case 0x0902: /* Get Virtual Interrupt State */
1362 TRACE( "Get Virtual Interrupt State: %ld\n",
1363 NtCurrentTeb()->dpmi_vif
);
1364 SET_AL( context
, NtCurrentTeb()->dpmi_vif
? 1 : 0 );
1367 case 0x0e00: /* Get Coprocessor Status (1.0) */
1369 * Return status in AX bits:
1370 * B0 - MPv (MP bit in the virtual MSW/CR0)
1371 * 0 = numeric coprocessor is disabled for this client
1372 * 1 = numeric coprocessor is enabled for this client
1373 * B1 - EMv (EM bit in the virtual MSW/CR0)
1374 * 0 = client is not emulating coprocessor instructions
1375 * 1 = client is emulating coprocessor instructions
1376 * B2 - MPr (MP bit from the actual MSW/CR0)
1377 * 0 = numeric coprocessor is not present
1378 * 1 = numeric coprocessor is present
1379 * B3 - EMr (EM bit from the actual MSW/CR0)
1380 * 0 = host is not emulating coprocessor instructions
1381 * 1 = host is emulating coprocessor instructions
1382 * B4-B7 - coprocessor type
1383 * 00H = no coprocessor
1386 * 04H = 80486 with numeric coprocessor
1387 * 05H-0FH = reserved for future numeric processors
1389 TRACE( "Get Coprocessor Status\n" );
1390 SET_AX( context
, 69 ); /* 486, coprocessor present and enabled */
1393 case 0x0e01: /* Set Coprocessor Emulation (1.0) */
1395 * See function 0x0e00.
1396 * BX bit B0 is new value for MPv.
1397 * BX bit B1 is new value for EMv.
1399 if (BX_reg(context
) != 1)
1400 FIXME( "Set Coprocessor Emulation to %d - unimplemented\n",
1403 TRACE( "Set Coprocessor Emulation - ignored\n" );
1407 INT_BARF( context
, 0x31 );
1408 SET_AX( context
, 0x8001 ); /* unsupported function */