4 * Copyright 1995 Alexandre Julliard
10 #include "wine/winbase16.h"
12 #include "builtin16.h"
19 #include "selectors.h"
22 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(int31
);
26 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
28 void CreateBPB(int drive
, BYTE
*data
, BOOL16 limited
); /* defined in int21.c */
30 static void* lastvalloced
= NULL
;
32 /* Structure for real-mode callbacks */
56 typedef struct tagRMCB
{
58 DWORD proc_ofs
,proc_sel
;
59 DWORD regs_ofs
,regs_sel
;
63 static RMCB
*FirstRMCB
= NULL
;
67 /**********************************************************************
69 * special virtualalloc, allocates lineary monoton growing memory.
70 * (the usual VirtualAlloc does not satisfy that restriction)
73 DPMI_xalloc(int len
) {
75 LPVOID oldlastv
= lastvalloced
;
81 ret
=VirtualAlloc(lastvalloced
,len
,MEM_COMMIT
|MEM_RESERVE
,PAGE_EXECUTE_READWRITE
);
83 lastvalloced
= (char *) lastvalloced
+ 0x10000;
84 /* we failed to allocate one in the first round.
87 if (!xflag
&& (lastvalloced
<oldlastv
)) { /* wrapped */
88 FIXME("failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len
);
91 /* if we even fail to allocate something in the next
94 if ((xflag
==1) && (lastvalloced
>= oldlastv
))
96 if ((xflag
==2) && (lastvalloced
< oldlastv
)) {
97 FIXME("failed to allocate any memory of %d bytes!\n",len
);
102 ret
=VirtualAlloc(NULL
,len
,MEM_COMMIT
|MEM_RESERVE
,PAGE_EXECUTE_READWRITE
);
103 lastvalloced
= (LPVOID
)(((DWORD
)ret
+len
+0xffff)&~0xffff);
108 DPMI_xfree(LPVOID ptr
) {
109 VirtualFree(ptr
,0,MEM_RELEASE
);
112 /* FIXME: perhaps we could grow this mapped area... */
114 DPMI_xrealloc(LPVOID ptr
,int newsize
) {
115 MEMORY_BASIC_INFORMATION mbi
;
118 newptr
= DPMI_xalloc(newsize
);
120 if (!VirtualQuery(ptr
,&mbi
,sizeof(mbi
))) {
121 FIXME("realloc of DPMI_xallocd region %p?\n",ptr
);
124 if (mbi
.State
== MEM_FREE
) {
125 FIXME("realloc of DPMI_xallocd region %p?\n",ptr
);
128 /* We do not shrink allocated memory. most reallocs
129 * only do grows anyway
131 if (newsize
<=mbi
.RegionSize
)
133 memcpy(newptr
,ptr
,mbi
.RegionSize
);
138 /**********************************************************************
139 * INT_GetRealModeContext
141 static void INT_GetRealModeContext( REALMODECALL
*call
, CONTEXT86
*context
)
143 EAX_reg(context
) = call
->eax
;
144 EBX_reg(context
) = call
->ebx
;
145 ECX_reg(context
) = call
->ecx
;
146 EDX_reg(context
) = call
->edx
;
147 ESI_reg(context
) = call
->esi
;
148 EDI_reg(context
) = call
->edi
;
149 EBP_reg(context
) = call
->ebp
;
150 EFL_reg(context
) = call
->fl
| V86_FLAG
;
151 EIP_reg(context
) = call
->ip
;
152 ESP_reg(context
) = call
->sp
;
153 CS_reg(context
) = call
->cs
;
154 DS_reg(context
) = call
->ds
;
155 ES_reg(context
) = call
->es
;
156 FS_reg(context
) = call
->fs
;
157 GS_reg(context
) = call
->gs
;
158 SS_reg(context
) = call
->ss
;
162 /**********************************************************************
163 * INT_SetRealModeContext
165 static void INT_SetRealModeContext( REALMODECALL
*call
, CONTEXT86
*context
)
167 call
->eax
= EAX_reg(context
);
168 call
->ebx
= EBX_reg(context
);
169 call
->ecx
= ECX_reg(context
);
170 call
->edx
= EDX_reg(context
);
171 call
->esi
= ESI_reg(context
);
172 call
->edi
= EDI_reg(context
);
173 call
->ebp
= EBP_reg(context
);
174 call
->fl
= LOWORD(EFL_reg(context
));
175 call
->ip
= LOWORD(EIP_reg(context
));
176 call
->sp
= LOWORD(ESP_reg(context
));
177 call
->cs
= CS_reg(context
);
178 call
->ds
= DS_reg(context
);
179 call
->es
= ES_reg(context
);
180 call
->fs
= FS_reg(context
);
181 call
->gs
= GS_reg(context
);
182 call
->ss
= SS_reg(context
);
187 void DPMI_CallRMCB32(RMCB
*rmcb
, UINT16 ss
, DWORD esp
, UINT16
*es
, DWORD
*edi
)
188 #if 0 /* original code, which early gccs puke on */
191 __asm__
__volatile__(
199 ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
205 : "=d" (*es
), "=D" (*edi
), "=S" (_clobber
), "=a" (_clobber
), "=c" (_clobber
)
206 : "0" (ss
), "2" (esp
),
207 "4" (rmcb
->regs_sel
), "1" (rmcb
->regs_ofs
),
208 "3" (&rmcb
->proc_ofs
) );
210 #else /* code generated by a gcc new enough */
212 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32
,
217 "movl 0x8(%ebp),%eax\n\t"
218 "movl 0x10(%ebp),%esi\n\t"
219 "movl 0xc(%ebp),%edx\n\t"
220 "movl 0x10(%eax),%ecx\n\t"
221 "movl 0xc(%eax),%edi\n\t"
230 ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
236 "movl 0x14(%ebp),%eax\n\t"
237 "movw %dx,(%eax)\n\t"
238 "movl 0x18(%ebp),%edx\n\t"
239 "movl %edi,(%edx)\n\t"
246 #endif /* __i386__ */
248 /**********************************************************************
251 * This routine does the hard work of calling a callback procedure.
253 static void DPMI_CallRMCBProc( CONTEXT86
*context
, RMCB
*rmcb
, WORD flag
)
255 if (IS_SELECTOR_SYSTEM( rmcb
->proc_sel
)) {
256 /* Wine-internal RMCB, call directly */
257 ((RMCBPROC
)rmcb
->proc_ofs
)(context
);
263 INT_SetRealModeContext((REALMODECALL
*)PTR_SEG_OFF_TO_LIN( rmcb
->regs_sel
, rmcb
->regs_ofs
), context
);
264 ss
= SELECTOR_AllocBlock( DOSMEM_MemoryBase() + (DWORD
)(SS_reg(context
)<<4), 0x10000, SEGMENT_DATA
, FALSE
, FALSE
);
265 esp
= ESP_reg(context
);
267 FIXME("untested!\n");
269 /* The called proc ends with an IRET, and takes these parameters:
270 * DS:ESI = pointer to real-mode SS:SP
271 * ES:EDI = pointer to real-mode call structure
273 * ES:EDI = pointer to real-mode call structure (may be a copy)
274 * It is the proc's responsibility to change the return CS:IP in the
275 * real-mode call structure. */
277 /* 32-bit DPMI client */
278 DPMI_CallRMCB32(rmcb
, ss
, esp
, &es
, &edi
);
280 /* 16-bit DPMI client */
281 CONTEXT86 ctx
= *context
;
282 CS_reg(&ctx
) = rmcb
->proc_sel
;
283 EIP_reg(&ctx
) = rmcb
->proc_ofs
;
286 ES_reg(&ctx
) = rmcb
->regs_sel
;
287 EDI_reg(&ctx
) = rmcb
->regs_ofs
;
288 /* FIXME: I'm pretty sure this isn't right - should push flags first */
289 CallTo16RegisterShort(&ctx
, 0);
293 SELECTOR_FreeBlock(ss
, 1);
294 INT_GetRealModeContext((REALMODECALL
*)PTR_SEG_OFF_TO_LIN( es
, edi
), context
);
296 ERR("RMCBs only implemented for i386\n");
302 /**********************************************************************
305 * This routine does the hard work of calling a real mode procedure.
307 int DPMI_CallRMProc( CONTEXT86
*context
, LPWORD stack
, int args
, int iret
)
310 LPVOID addr
= NULL
; /* avoid gcc warning */
311 LPDOSTASK lpDosTask
= MZ_Current();
313 int alloc
= 0, already
= 0;
316 GlobalUnlock16( GetCurrentTask() );
318 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
319 EAX_reg(context
), EBX_reg(context
), ECX_reg(context
), EDX_reg(context
) );
320 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
321 ESI_reg(context
), EDI_reg(context
), ES_reg(context
), DS_reg(context
),
322 CS_reg(context
), LOWORD(EIP_reg(context
)), args
, iret
?"IRET":"FAR" );
326 /* there might be some code that just jumps to RMCBs or the like,
327 in which case following the jumps here might get us to a shortcut */
328 code
= CTX_SEG_OFF_TO_LIN(context
, CS_reg(context
), EIP_reg(context
));
330 case 0xe9: /* JMP NEAR */
331 EIP_reg(context
) += 3 + *(WORD
*)(code
+1);
332 /* yeah, I know these gotos don't look good... */
333 goto callrmproc_again
;
334 case 0xea: /* JMP FAR */
335 EIP_reg(context
) = *(WORD
*)(code
+1);
336 CS_reg(context
) = *(WORD
*)(code
+3);
337 /* ...but since the label is there anyway... */
338 goto callrmproc_again
;
339 case 0xeb: /* JMP SHORT */
340 EIP_reg(context
) += 2 + *(signed char *)(code
+1);
341 /* ...because of other gotos below, so... */
342 goto callrmproc_again
;
345 /* shortcut for chaining to internal interrupt handlers */
346 if ((CS_reg(context
) == 0xF000) && iret
) {
347 return INT_RealModeInterrupt( LOWORD(EIP_reg(context
))/4, context
);
350 /* shortcut for RMCBs */
351 CurrRMCB
= FirstRMCB
;
353 while (CurrRMCB
&& (HIWORD(CurrRMCB
->address
) != CS_reg(context
)))
354 CurrRMCB
= CurrRMCB
->next
;
356 if (!(CurrRMCB
|| lpDosTask
)) {
358 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
359 TRACE("creating VM86 task\n");
360 if (!MZ_InitTask( lpDosTask
= MZ_AllocDPMITask() )) {
361 ERR("could not setup VM86 task\n");
365 ERR("Actual real-mode calls not supported on this architecture!\n");
370 if (!SS_reg(context
)) {
371 alloc
= 1; /* allocate default stack */
372 stack16
= addr
= DOSMEM_GetBlock( 64, (UINT16
*)&(SS_reg(context
)) );
373 ESP_reg(context
) = 64-2;
376 ERR("could not allocate default stack\n");
380 stack16
= CTX_SEG_OFF_TO_LIN(context
, SS_reg(context
), ESP_reg(context
));
382 ESP_reg(context
) -= (args
+ (iret
?1:0)) * sizeof(WORD
);
384 if (args
) memcpy(stack16
, stack
, args
*sizeof(WORD
) );
385 /* push flags if iret */
388 *stack16
= LOWORD(EFL_reg(context
));
390 /* push return address (return to interrupt wrapper) */
391 *(--stack16
) = DPMI_wrap_seg
;
394 ESP_reg(context
) -= 2*sizeof(WORD
);
399 /* RMCB call, invoke protected-mode handler directly */
400 DPMI_CallRMCBProc(context
, CurrRMCB
, lpDosTask
? lpDosTask
->dpmi_flag
: 0);
401 /* check if we returned to where we thought we would */
402 if ((CS_reg(context
) != DPMI_wrap_seg
) ||
403 (LOWORD(EIP_reg(context
)) != 0)) {
404 /* we need to continue at different address in real-mode space,
405 so we need to set it all up for real mode again */
406 goto callrmproc_again
;
410 TRACE("entering real mode...\n");
411 DOSVM_Enter( context
);
412 TRACE("returned from real-mode call\n");
414 /* we should never get here, but... */
415 ERR("cannot perform real-mode call\n");
418 if (alloc
) DOSMEM_FreeBlock( addr
);
423 /**********************************************************************
426 static void CallRMInt( CONTEXT86
*context
)
428 CONTEXT86 realmode_ctx
;
429 FARPROC16 rm_int
= INT_GetRMHandler( BL_reg(context
) );
430 REALMODECALL
*call
= (REALMODECALL
*)PTR_SEG_OFF_TO_LIN( ES_reg(context
),
432 INT_GetRealModeContext( call
, &realmode_ctx
);
434 /* we need to check if a real-mode program has hooked the interrupt */
435 if (HIWORD(rm_int
)!=0xF000) {
436 /* yup, which means we need to switch to real mode... */
437 CS_reg(&realmode_ctx
) = HIWORD(rm_int
);
438 EIP_reg(&realmode_ctx
) = LOWORD(rm_int
);
439 if (DPMI_CallRMProc( &realmode_ctx
, NULL
, 0, TRUE
))
442 RESET_CFLAG(context
);
443 /* use the IP we have instead of BL_reg, in case some apps
444 decide to move interrupts around for whatever reason... */
445 if (INT_RealModeInterrupt( LOWORD(rm_int
)/4, &realmode_ctx
))
447 if (EFL_reg(context
)&1) {
448 FIXME("%02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
449 BL_reg(context
), EAX_reg(&realmode_ctx
), EBX_reg(&realmode_ctx
),
450 ECX_reg(&realmode_ctx
), EDX_reg(&realmode_ctx
));
451 FIXME(" ESI=%08lx EDI=%08lx DS=%04lx ES=%04lx\n",
452 ESI_reg(&realmode_ctx
), EDI_reg(&realmode_ctx
),
453 DS_reg(&realmode_ctx
), ES_reg(&realmode_ctx
) );
456 INT_SetRealModeContext( call
, &realmode_ctx
);
460 static void CallRMProc( CONTEXT86
*context
, int iret
)
462 REALMODECALL
*p
= (REALMODECALL
*)PTR_SEG_OFF_TO_LIN( ES_reg(context
), DI_reg(context
) );
465 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
466 p
->eax
, p
->ebx
, p
->ecx
, p
->edx
);
467 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
468 p
->esi
, p
->edi
, p
->es
, p
->ds
, p
->cs
, p
->ip
, CX_reg(context
), iret
?"IRET":"FAR" );
470 if (!(p
->cs
) && !(p
->ip
)) { /* remove this check
471 if Int21/6501 case map function
472 has been implemented */
476 INT_GetRealModeContext(p
, &context16
);
477 DPMI_CallRMProc( &context16
, ((LPWORD
)PTR_SEG_OFF_TO_LIN(SS_reg(context
), LOWORD(ESP_reg(context
))))+3,
478 CX_reg(context
), iret
);
479 INT_SetRealModeContext(p
, &context16
);
483 static RMCB
*DPMI_AllocRMCB( void )
485 RMCB
*NewRMCB
= HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB
));
490 LPVOID RMCBmem
= DOSMEM_GetBlock(4, &uParagraph
);
493 *p
++ = 0xcd; /* RMCB: */
494 *p
++ = 0x31; /* int $0x31 */
495 /* it is the called procedure's task to change the return CS:EIP
496 the DPMI 0.9 spec states that if it doesn't, it will be called again */
498 *p
++ = 0xfc; /* jmp RMCB */
499 NewRMCB
->address
= MAKELONG(0, uParagraph
);
500 NewRMCB
->next
= FirstRMCB
;
507 static void AllocRMCB( CONTEXT86
*context
)
509 RMCB
*NewRMCB
= DPMI_AllocRMCB();
511 TRACE("Function to call: %04x:%04x\n", (WORD
)DS_reg(context
), SI_reg(context
) );
515 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
516 NewRMCB
->proc_ofs
= SI_reg(context
);
517 NewRMCB
->proc_sel
= DS_reg(context
);
518 NewRMCB
->regs_ofs
= DI_reg(context
);
519 NewRMCB
->regs_sel
= ES_reg(context
);
520 SET_LOWORD( ECX_reg(context
), HIWORD(NewRMCB
->address
) );
521 SET_LOWORD( EDX_reg(context
), LOWORD(NewRMCB
->address
) );
525 SET_LOWORD( EAX_reg(context
), 0x8015 ); /* callback unavailable */
531 FARPROC16 WINAPI
DPMI_AllocInternalRMCB( RMCBPROC proc
)
533 RMCB
*NewRMCB
= DPMI_AllocRMCB();
536 NewRMCB
->proc_ofs
= (DWORD
)proc
;
537 NewRMCB
->proc_sel
= 0;
538 NewRMCB
->regs_ofs
= 0;
539 NewRMCB
->regs_sel
= 0;
540 return (FARPROC16
)(NewRMCB
->address
);
546 static int DPMI_FreeRMCB( DWORD address
)
548 RMCB
*CurrRMCB
= FirstRMCB
;
549 RMCB
*PrevRMCB
= NULL
;
551 while (CurrRMCB
&& (CurrRMCB
->address
!= address
))
554 CurrRMCB
= CurrRMCB
->next
;
559 PrevRMCB
->next
= CurrRMCB
->next
;
561 FirstRMCB
= CurrRMCB
->next
;
562 DOSMEM_FreeBlock(DOSMEM_MapRealToLinear(CurrRMCB
->address
));
563 HeapFree(GetProcessHeap(), 0, CurrRMCB
);
570 static void FreeRMCB( CONTEXT86
*context
)
572 FIXME("callback address: %04x:%04x\n",
573 CX_reg(context
), DX_reg(context
));
575 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context
), CX_reg(context
)))) {
576 SET_LOWORD( EAX_reg(context
), 0x8024 ); /* invalid callback address */
582 void WINAPI
DPMI_FreeInternalRMCB( FARPROC16 proc
)
584 DPMI_FreeRMCB( (DWORD
)proc
);
589 /* (see loader/dos/module.c, function MZ_InitDPMI) */
591 static void StartPM( CONTEXT86
*context
, LPDOSTASK lpDosTask
)
593 char *base
= DOSMEM_MemoryBase();
594 UINT16 cs
, ss
, ds
, es
;
596 DWORD psp_ofs
= (DWORD
)(lpDosTask
->psp_seg
<<4);
597 PDB16
*psp
= (PDB16
*)(base
+ psp_ofs
);
598 HANDLE16 env_seg
= psp
->environment
;
601 RESET_CFLAG(context
);
602 lpDosTask
->dpmi_flag
= AX_reg(context
);
603 is32
= lpDosTask
->dpmi_flag
& 1;
604 /* our mode switch wrapper have placed the desired CS into DX */
605 cs
= SELECTOR_AllocBlock( base
+ (DWORD
)(DX_reg(context
)<<4), 0x10000, SEGMENT_CODE
, FALSE
, FALSE
);
606 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
607 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
608 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
609 32-bit code using this stack. */
610 ss
= SELECTOR_AllocBlock( base
+ (DWORD
)(SS_reg(context
)<<4), 0x10000, SEGMENT_DATA
, is32
, FALSE
);
611 /* do the same for the data segments, just in case */
612 if (DS_reg(context
) == SS_reg(context
)) ds
= ss
;
613 else ds
= SELECTOR_AllocBlock( base
+ (DWORD
)(DS_reg(context
)<<4), 0x10000, SEGMENT_DATA
, is32
, FALSE
);
614 es
= SELECTOR_AllocBlock( base
+ psp_ofs
, 0x100, SEGMENT_DATA
, is32
, FALSE
);
615 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
616 psp
->environment
= SELECTOR_AllocBlock( base
+ (DWORD
)(env_seg
<<4),
617 0x10000, SEGMENT_DATA
, FALSE
, FALSE
);
620 CS_reg(&pm_ctx
) = lpDosTask
->dpmi_sel
;
621 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
622 EAX_reg(&pm_ctx
) = ss
;
623 EDX_reg(&pm_ctx
) = cs
;
624 DS_reg(&pm_ctx
) = ds
;
625 ES_reg(&pm_ctx
) = es
;
629 TRACE("DOS program is now entering protected mode\n");
630 CallTo16RegisterShort(&pm_ctx
, 0);
632 /* in the current state of affairs, we won't ever actually return here... */
633 /* we should have int21/ah=4c do it someday, though... */
635 SELECTOR_FreeBlock(psp
->environment
, 1);
636 psp
->environment
= env_seg
;
637 SELECTOR_FreeBlock(es
, 1);
638 if (ds
!= ss
) SELECTOR_FreeBlock(ds
, 1);
639 SELECTOR_FreeBlock(ss
, 1);
640 SELECTOR_FreeBlock(cs
, 1);
643 /* DPMI Raw Mode Switch handler */
646 void WINAPI
DPMI_RawModeSwitch( SIGCONTEXT
*context
)
648 LPDOSTASK lpDosTask
= MZ_Current();
653 /* we could probably start a DPMI-only dosmod task here, but I doubt
654 anything other than real DOS apps want to call raw mode switch */
655 ERR("attempting raw mode switch without DOS task!\n");
658 /* initialize real-mode context as per spec */
659 memset(&rm_ctx
, 0, sizeof(rm_ctx
));
660 DS_reg(&rm_ctx
) = AX_sig(context
);
661 ES_reg(&rm_ctx
) = CX_sig(context
);
662 SS_reg(&rm_ctx
) = DX_sig(context
);
663 ESP_reg(&rm_ctx
) = EBX_sig(context
);
664 CS_reg(&rm_ctx
) = SI_sig(context
);
665 EIP_reg(&rm_ctx
) = EDI_sig(context
);
666 EBP_reg(&rm_ctx
) = EBP_sig(context
);
669 EFL_reg(&rm_ctx
) = EFL_sig(context
); /* at least we need the IF flag */
671 /* enter real mode again */
672 TRACE("re-entering real mode at %04lx:%04lx\n",
673 CS_reg(&rm_ctx
),EIP_reg(&rm_ctx
));
674 ret
= DOSVM_Enter( &rm_ctx
);
675 /* when the real-mode stuff call its mode switch address,
676 DOSVM_Enter will return and we will continue here */
679 /* if the sync was lost, there's no way to recover */
683 /* alter protected-mode context as per spec */
684 DS_sig(context
) = AX_reg(&rm_ctx
);
685 ES_sig(context
) = CX_reg(&rm_ctx
);
686 SS_sig(context
) = DX_reg(&rm_ctx
);
687 ESP_sig(context
) = EBX_reg(&rm_ctx
);
688 CS_sig(context
) = SI_reg(&rm_ctx
);
689 EIP_sig(context
) = EDI_reg(&rm_ctx
);
690 EBP_sig(context
) = EBP_reg(&rm_ctx
);
694 /* Return to new address and hope that we didn't mess up */
695 TRACE("re-entering protected mode at %04x:%08lx\n",
696 CS_sig(context
), EIP_sig(context
));
702 void WINAPI
DPMI_RawModeSwitch( SIGCONTEXT
*context
)
704 ERR("don't even think about DPMI raw mode switch without DOS support!\n");
710 #define DOS_APP_ISDOS(addr,base) ((addr) < 0x110000)
711 #define DOS_WINE_ISDOS(addr,base) (((addr) >= (base)) && ((addr) < (base) + 0x110000))
712 #define DOS_UC_APPTOWINE(addr,base) ((addr) + (base))
713 #define DOS_UC_WINETOAPP(addr,base) ((addr) - (base))
714 #define DOS_APPTOWINE(addr,base) (DOS_APP_ISDOS(addr,base) ? DOS_UC_APPTOWINE(addr,base) : (addr))
715 #define DOS_WINETOAPP(addr,base) (DOS_WINE_ISDOS(addr,base) ? DOS_UC_WINETOAPP(addr,base) : (addr))
716 #define DOS_BADLIMIT(addr,base,limit) \
717 ((limit == 0xffffffff) || /* disallow "fat DS" for now */ \
718 (DOS_WINE_ISDOS(addr,base) && \
719 ((addr) + (limit) > (base) + 0x110000)))
721 /**********************************************************************
724 * Handler for int 31h (DPMI).
727 void WINAPI
INT_Int31Handler( CONTEXT86
*context
)
730 * Note: For Win32s processes, the whole linear address space is
731 * shifted by 0x10000 relative to the OS linear address space.
732 * See the comment in msdos/vxd.c.
734 DWORD offset
= W32S_APPLICATION() ? W32S_OFFSET
: 0;
739 LPDOSTASK lpDosTask
= MZ_Current();
742 if (ISV86(context
) && lpDosTask
) {
743 /* Called from real mode, check if it's our wrapper */
744 TRACE("called from real mode\n");
745 if (CS_reg(context
)==lpDosTask
->dpmi_seg
) {
746 /* This is the protected mode switch */
747 StartPM(context
,lpDosTask
);
750 if (CS_reg(context
)==lpDosTask
->xms_seg
) {
751 /* This is the XMS driver entry point */
752 XMS_Handler(context
);
757 RMCB
*CurrRMCB
= FirstRMCB
;
759 while (CurrRMCB
&& (HIWORD(CurrRMCB
->address
) != CS_reg(context
)))
760 CurrRMCB
= CurrRMCB
->next
;
763 /* RMCB call, propagate to protected-mode handler */
764 DPMI_CallRMCBProc(context
, CurrRMCB
, lpDosTask
->dpmi_flag
);
771 RESET_CFLAG(context
);
772 switch(AX_reg(context
))
774 case 0x0000: /* Allocate LDT descriptors */
775 TRACE("allocate LDT descriptors (%d)\n",CX_reg(context
));
776 if (!(EAX_reg(context
) = AllocSelectorArray16( CX_reg(context
) )))
779 EAX_reg(context
) = 0x8011; /* descriptor unavailable */
782 TRACE("success, array starts at 0x%04x\n",AX_reg(context
));
785 case 0x0001: /* Free LDT descriptor */
786 TRACE("free LDT descriptor (0x%04x)\n",BX_reg(context
));
787 if (FreeSelector16( BX_reg(context
) ))
789 EAX_reg(context
) = 0x8022; /* invalid selector */
794 /* If a segment register contains the selector being freed, */
795 /* set it to zero. */
796 if (!((DS_reg(context
)^BX_reg(context
)) & ~3)) DS_reg(context
) = 0;
797 if (!((ES_reg(context
)^BX_reg(context
)) & ~3)) ES_reg(context
) = 0;
798 if (!((FS_reg(context
)^BX_reg(context
)) & ~3)) FS_reg(context
) = 0;
799 if (!((GS_reg(context
)^BX_reg(context
)) & ~3)) GS_reg(context
) = 0;
803 case 0x0002: /* Real mode segment to descriptor */
804 TRACE("real mode segment to descriptor (0x%04x)\n",BX_reg(context
));
806 WORD entryPoint
= 0; /* KERNEL entry point for descriptor */
807 switch(BX_reg(context
))
809 case 0x0000: entryPoint
= 183; break; /* __0000H */
810 case 0x0040: entryPoint
= 193; break; /* __0040H */
811 case 0xa000: entryPoint
= 174; break; /* __A000H */
812 case 0xb000: entryPoint
= 181; break; /* __B000H */
813 case 0xb800: entryPoint
= 182; break; /* __B800H */
814 case 0xc000: entryPoint
= 195; break; /* __C000H */
815 case 0xd000: entryPoint
= 179; break; /* __D000H */
816 case 0xe000: entryPoint
= 190; break; /* __E000H */
817 case 0xf000: entryPoint
= 194; break; /* __F000H */
819 EAX_reg(context
) = DOSMEM_AllocSelector(BX_reg(context
));
823 EAX_reg(context
) = LOWORD(NE_GetEntryPoint( GetModuleHandle16( "KERNEL" ),
828 case 0x0003: /* Get next selector increment */
829 TRACE("get selector increment (__AHINCR)\n");
830 EAX_reg(context
) = __AHINCR
;
833 case 0x0004: /* Lock selector (not supported) */
834 FIXME("lock selector not supported\n");
835 EAX_reg(context
) = 0; /* FIXME: is this a correct return value? */
838 case 0x0005: /* Unlock selector (not supported) */
839 FIXME("unlock selector not supported\n");
840 EAX_reg(context
) = 0; /* FIXME: is this a correct return value? */
843 case 0x0006: /* Get selector base address */
844 TRACE("get selector base address (0x%04x)\n",BX_reg(context
));
845 if (!(dw
= GetSelectorBase( BX_reg(context
) )))
847 EAX_reg(context
) = 0x8022; /* invalid selector */
854 DWORD base
= (DWORD
)DOSMEM_MemoryBase();
855 dw
= DOS_WINETOAPP(dw
, base
);
858 CX_reg(context
) = HIWORD(W32S_WINE2APP(dw
, offset
));
859 DX_reg(context
) = LOWORD(W32S_WINE2APP(dw
, offset
));
863 case 0x0007: /* Set selector base address */
864 TRACE("set selector base address (0x%04x,0x%08lx)\n",
866 W32S_APP2WINE(MAKELONG(DX_reg(context
),CX_reg(context
)), offset
));
867 dw
= W32S_APP2WINE(MAKELONG(DX_reg(context
), CX_reg(context
)), offset
);
870 DWORD base
= (DWORD
)DOSMEM_MemoryBase();
871 dw
= DOS_APPTOWINE(dw
, base
);
874 SetSelectorBase(BX_reg(context
), dw
);
877 case 0x0008: /* Set selector limit */
878 TRACE("set selector limit (0x%04x,0x%08lx)\n",BX_reg(context
),MAKELONG(DX_reg(context
),CX_reg(context
)));
879 dw
= MAKELONG( DX_reg(context
), CX_reg(context
) );
882 DWORD base
= (DWORD
)DOSMEM_MemoryBase();
883 DWORD sbase
= GetSelectorBase( BX_reg(context
) );
885 /* the app has set the limit without setting the base,
886 * it must be relying on that the default should be DOS space;
887 * so set the base address now */
888 SetSelectorBase( BX_reg(context
), sbase
= base
);
889 if (dw
== 0xffffffff) {
890 /* djgpp does this without checking (in _dos_ds setup, crt1.c),
891 * so we have to override the limit here */
895 if (DOS_BADLIMIT(sbase
, base
, dw
)) {
896 AX_reg(context
) = 0x8021; /* invalid value */
902 SetSelectorLimit16( BX_reg(context
), dw
);
905 case 0x0009: /* Set selector access rights */
906 TRACE("set selector access rights(0x%04x,0x%04x)\n",BX_reg(context
),CX_reg(context
));
907 SelectorAccessRights16( BX_reg(context
), 1, CX_reg(context
) );
910 case 0x000a: /* Allocate selector alias */
911 TRACE("allocate selector alias (0x%04x)\n",BX_reg(context
));
912 if (!(AX_reg(context
) = AllocCStoDSAlias16( BX_reg(context
) )))
914 AX_reg(context
) = 0x8011; /* descriptor unavailable */
919 case 0x000b: /* Get descriptor */
920 TRACE("get descriptor (0x%04x)\n",BX_reg(context
));
923 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context
) ), &entry
);
926 DWORD base
= (DWORD
)DOSMEM_MemoryBase();
927 entry
.base
= DOS_WINETOAPP(entry
.base
, base
);
930 entry
.base
= W32S_WINE2APP(entry
.base
, offset
);
932 /* FIXME: should use ES:EDI for 32-bit clients */
933 LDT_EntryToBytes( PTR_SEG_OFF_TO_LIN( ES_reg(context
),
934 DI_reg(context
) ), &entry
);
938 case 0x000c: /* Set descriptor */
939 TRACE("set descriptor (0x%04x)\n",BX_reg(context
));
942 LDT_BytesToEntry( PTR_SEG_OFF_TO_LIN( ES_reg(context
),
943 DI_reg(context
) ), &entry
);
944 entry
.base
= W32S_APP2WINE(entry
.base
, offset
);
947 DWORD base
= (DWORD
)DOSMEM_MemoryBase();
948 entry
.base
= DOS_APPTOWINE(entry
.base
, base
);
949 if (DOS_BADLIMIT(entry
.base
, base
, entry
.limit
)) {
950 AX_reg(context
) = 0x8021; /* invalid value */
957 LDT_SetEntry( SELECTOR_TO_ENTRY( BX_reg(context
) ), &entry
);
961 case 0x000d: /* Allocate specific LDT descriptor */
962 FIXME("allocate descriptor (0x%04x), stub!\n",BX_reg(context
));
963 AX_reg(context
) = 0x8011; /* descriptor unavailable */
966 case 0x0100: /* Allocate DOS memory block */
967 TRACE("allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context
));
968 dw
= GlobalDOSAlloc16((DWORD
)BX_reg(context
)<<4);
970 AX_reg(context
) = HIWORD(dw
);
971 DX_reg(context
) = LOWORD(dw
);
973 AX_reg(context
) = 0x0008; /* insufficient memory */
974 BX_reg(context
) = DOSMEM_Available()>>4;
978 case 0x0101: /* Free DOS memory block */
979 TRACE("free DOS memory block (0x%04x)\n",DX_reg(context
));
980 dw
= GlobalDOSFree16(DX_reg(context
));
982 AX_reg(context
) = 0x0009; /* memory block address invalid */
986 case 0x0200: /* get real mode interrupt vector */
987 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
991 case 0x0201: /* set real mode interrupt vector */
992 FIXME("set realmode interupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context
),CX_reg(context
),DX_reg(context
));
995 case 0x0204: /* Get protected mode interrupt vector */
996 TRACE("get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context
));
997 dw
= (DWORD
)INT_GetPMHandler( BL_reg(context
) );
998 CX_reg(context
) = HIWORD(dw
);
999 DX_reg(context
) = LOWORD(dw
);
1002 case 0x0205: /* Set protected mode interrupt vector */
1003 TRACE("set protected mode interrupt handler (0x%02x,%p), stub!\n",
1004 BL_reg(context
),PTR_SEG_OFF_TO_LIN(CX_reg(context
),DX_reg(context
)));
1005 INT_SetPMHandler( BL_reg(context
),
1006 (FARPROC16
)PTR_SEG_OFF_TO_SEGPTR( CX_reg(context
),
1010 case 0x0300: /* Simulate real mode interrupt */
1011 CallRMInt( context
);
1014 case 0x0301: /* Call real mode procedure with far return */
1015 CallRMProc( context
, FALSE
);
1018 case 0x0302: /* Call real mode procedure with interrupt return */
1019 CallRMProc( context
, TRUE
);
1022 case 0x0303: /* Allocate Real Mode Callback Address */
1023 AllocRMCB( context
);
1026 case 0x0304: /* Free Real Mode Callback Address */
1027 FreeRMCB( context
);
1030 case 0x0305: /* Get State Save/Restore Addresses */
1031 TRACE("get state save/restore addresses\n");
1032 /* we probably won't need this kind of state saving */
1033 AX_reg(context
) = 0;
1034 /* real mode: just point to the lret */
1035 BX_reg(context
) = DPMI_wrap_seg
;
1036 ECX_reg(context
) = 2;
1037 /* protected mode: don't have any handler yet... */
1038 FIXME("no protected-mode dummy state save/restore handler yet\n");
1039 SI_reg(context
) = 0;
1040 EDI_reg(context
) = 0;
1043 case 0x0306: /* Get Raw Mode Switch Addresses */
1044 TRACE("get raw mode switch addresses\n");
1046 /* real mode, point to standard DPMI return wrapper */
1047 BX_reg(context
) = DPMI_wrap_seg
;
1048 ECX_reg(context
) = 0;
1049 /* protected mode, point to DPMI call wrapper */
1050 SI_reg(context
) = lpDosTask
->dpmi_sel
;
1051 EDI_reg(context
) = 8; /* offset of the INT 0x31 call */
1053 ERR("win app attempting to get raw mode switch!\n");
1054 AX_reg(context
) = 0x8001; /* unsupported function */
1058 case 0x0400: /* Get DPMI version */
1059 TRACE("get DPMI version\n");
1064 AX_reg(context
) = 0x005a; /* DPMI version 0.90 */
1065 BX_reg(context
) = 0x0005; /* Flags: 32-bit, virtual memory */
1066 CL_reg(context
) = si
.wProcessorLevel
;
1067 DX_reg(context
) = 0x0102; /* Master/slave interrupt controller base*/
1070 case 0x0500: /* Get free memory information */
1071 TRACE("get free memory information\n");
1075 mmi
.dwSize
= sizeof(mmi
);
1077 ptr
= (BYTE
*)PTR_SEG_OFF_TO_LIN(ES_reg(context
),DI_reg(context
));
1078 /* the layout is just the same as MEMMANINFO, but without
1081 memcpy(ptr
,((char*)&mmi
)+4,sizeof(mmi
)-4);
1084 case 0x0501: /* Allocate memory block */
1085 TRACE("allocate memory block (%ld)\n",MAKELONG(CX_reg(context
),BX_reg(context
)));
1086 if (!(ptr
= (BYTE
*)DPMI_xalloc(MAKELONG(CX_reg(context
), BX_reg(context
)))))
1088 AX_reg(context
) = 0x8012; /* linear memory not available */
1091 BX_reg(context
) = SI_reg(context
) = HIWORD(W32S_WINE2APP(ptr
, offset
));
1092 CX_reg(context
) = DI_reg(context
) = LOWORD(W32S_WINE2APP(ptr
, offset
));
1096 case 0x0502: /* Free memory block */
1097 TRACE("free memory block (0x%08lx)\n",
1098 W32S_APP2WINE(MAKELONG(DI_reg(context
),SI_reg(context
)), offset
));
1099 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context
),
1100 SI_reg(context
)), offset
) );
1103 case 0x0503: /* Resize memory block */
1104 TRACE("resize memory block (0x%08lx,%ld)\n",
1105 W32S_APP2WINE(MAKELONG(DI_reg(context
),SI_reg(context
)), offset
),
1106 MAKELONG(CX_reg(context
),BX_reg(context
)));
1107 if (!(ptr
= (BYTE
*)DPMI_xrealloc(
1108 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context
),SI_reg(context
)), offset
),
1109 MAKELONG(CX_reg(context
),BX_reg(context
)))))
1111 AX_reg(context
) = 0x8012; /* linear memory not available */
1114 BX_reg(context
) = SI_reg(context
) = HIWORD(W32S_WINE2APP(ptr
, offset
));
1115 CX_reg(context
) = DI_reg(context
) = LOWORD(W32S_WINE2APP(ptr
, offset
));
1119 case 0x0507: /* Modify page attributes */
1120 FIXME("modify page attributes unimplemented\n");
1121 break; /* Just ignore it */
1123 case 0x0600: /* Lock linear region */
1124 FIXME("lock linear region unimplemented\n");
1125 break; /* Just ignore it */
1127 case 0x0601: /* Unlock linear region */
1128 FIXME("unlock linear region unimplemented\n");
1129 break; /* Just ignore it */
1131 case 0x0602: /* Unlock real-mode region */
1132 FIXME("unlock realmode region unimplemented\n");
1133 break; /* Just ignore it */
1135 case 0x0603: /* Lock real-mode region */
1136 FIXME("lock realmode region unimplemented\n");
1137 break; /* Just ignore it */
1139 case 0x0604: /* Get page size */
1140 TRACE("get pagesize\n");
1141 BX_reg(context
) = 0;
1142 CX_reg(context
) = VIRTUAL_GetPageSize();
1145 case 0x0702: /* Mark page as demand-paging candidate */
1146 FIXME("mark page as demand-paging candidate\n");
1147 break; /* Just ignore it */
1149 case 0x0703: /* Discard page contents */
1150 FIXME("discard page contents\n");
1151 break; /* Just ignore it */
1153 case 0x0800: /* Physical address mapping */
1154 FIXME("map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context
),BX_reg(context
)));
1155 if(!(ptr
=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context
),BX_reg(context
)))))
1157 AX_reg(context
) = 0x8021;
1162 BX_reg(context
) = HIWORD(W32S_WINE2APP(ptr
, offset
));
1163 CX_reg(context
) = LOWORD(W32S_WINE2APP(ptr
, offset
));
1164 RESET_CFLAG(context
);
1169 INT_BARF( context
, 0x31 );
1170 AX_reg(context
) = 0x8001; /* unsupported function */