4 * Copyright 1995 Alexandre Julliard
14 #include "wine/winbase16.h"
15 #include "builtin16.h"
21 #include "selectors.h"
23 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(int31
);
27 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
29 void CreateBPB(int drive
, BYTE
*data
, BOOL16 limited
); /* defined in int21.c */
31 static void* lastvalloced
= NULL
;
33 /* Structure for real-mode callbacks */
57 typedef struct tagRMCB
{
59 DWORD proc_ofs
,proc_sel
;
60 DWORD regs_ofs
,regs_sel
;
64 static RMCB
*FirstRMCB
= NULL
;
67 static LPDOSTASK WINAPI
DPMI_NoCurrent(void) { return NULL
; }
68 DOSVM_TABLE Dosvm
= { DPMI_NoCurrent
};
70 static HMODULE DosModule
;
72 /**********************************************************************
75 BOOL
DPMI_LoadDosSystem(void)
77 if (DosModule
) return TRUE
;
78 DosModule
= LoadLibraryA( "winedos.dll" );
80 ERR("could not load winedos.dll, DOS subsystem unavailable\n");
83 Dosvm
.Current
= (void *)GetProcAddress(DosModule
, "GetCurrent");
84 Dosvm
.LoadDPMI
= (void *)GetProcAddress(DosModule
, "LoadDPMI");
85 Dosvm
.LoadDosExe
= (void *)GetProcAddress(DosModule
, "LoadDosExe");
86 Dosvm
.Exec
= (void *)GetProcAddress(DosModule
, "Exec");
87 Dosvm
.Exit
= (void *)GetProcAddress(DosModule
, "Exit");
88 Dosvm
.Enter
= (void *)GetProcAddress(DosModule
, "Enter");
89 Dosvm
.RunInThread
= (void *)GetProcAddress(DosModule
, "RunInThread");
90 Dosvm
.Wait
= (void *)GetProcAddress(DosModule
, "Wait");
91 Dosvm
.QueueEvent
= (void *)GetProcAddress(DosModule
, "QueueEvent");
92 Dosvm
.OutPIC
= (void *)GetProcAddress(DosModule
, "OutPIC");
93 Dosvm
.SetTimer
= (void *)GetProcAddress(DosModule
, "SetTimer");
94 Dosvm
.GetTimer
= (void *)GetProcAddress(DosModule
, "GetTimer");
95 Dosvm
.KbdReadScan
= (void *)GetProcAddress(DosModule
, "KbdReadScan");
99 /**********************************************************************
101 * special virtualalloc, allocates lineary monoton growing memory.
102 * (the usual VirtualAlloc does not satisfy that restriction)
105 DPMI_xalloc(int len
) {
107 LPVOID oldlastv
= lastvalloced
;
113 ret
=VirtualAlloc(lastvalloced
,len
,MEM_COMMIT
|MEM_RESERVE
,PAGE_EXECUTE_READWRITE
);
115 lastvalloced
= (char *) lastvalloced
+ 0x10000;
116 /* we failed to allocate one in the first round.
119 if (!xflag
&& (lastvalloced
<oldlastv
)) { /* wrapped */
120 FIXME("failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len
);
123 /* if we even fail to allocate something in the next
126 if ((xflag
==1) && (lastvalloced
>= oldlastv
))
128 if ((xflag
==2) && (lastvalloced
< oldlastv
)) {
129 FIXME("failed to allocate any memory of %d bytes!\n",len
);
134 ret
=VirtualAlloc(NULL
,len
,MEM_COMMIT
|MEM_RESERVE
,PAGE_EXECUTE_READWRITE
);
135 lastvalloced
= (LPVOID
)(((DWORD
)ret
+len
+0xffff)&~0xffff);
140 DPMI_xfree(LPVOID ptr
) {
141 VirtualFree(ptr
,0,MEM_RELEASE
);
144 /* FIXME: perhaps we could grow this mapped area... */
146 DPMI_xrealloc(LPVOID ptr
,DWORD newsize
) {
147 MEMORY_BASIC_INFORMATION mbi
;
150 newptr
= DPMI_xalloc(newsize
);
152 if (!VirtualQuery(ptr
,&mbi
,sizeof(mbi
))) {
153 FIXME("realloc of DPMI_xallocd region %p?\n",ptr
);
156 if (mbi
.State
== MEM_FREE
) {
157 FIXME("realloc of DPMI_xallocd region %p?\n",ptr
);
160 /* We do not shrink allocated memory. most reallocs
161 * only do grows anyway
163 if (newsize
<=mbi
.RegionSize
)
165 memcpy(newptr
,ptr
,mbi
.RegionSize
);
170 /**********************************************************************
171 * INT_GetRealModeContext
173 static void INT_GetRealModeContext( REALMODECALL
*call
, CONTEXT86
*context
)
175 context
->Eax
= call
->eax
;
176 context
->Ebx
= call
->ebx
;
177 context
->Ecx
= call
->ecx
;
178 context
->Edx
= call
->edx
;
179 context
->Esi
= call
->esi
;
180 context
->Edi
= call
->edi
;
181 context
->Ebp
= call
->ebp
;
182 context
->EFlags
= call
->fl
| V86_FLAG
;
183 context
->Eip
= call
->ip
;
184 context
->Esp
= call
->sp
;
185 context
->SegCs
= call
->cs
;
186 context
->SegDs
= call
->ds
;
187 context
->SegEs
= call
->es
;
188 context
->SegFs
= call
->fs
;
189 context
->SegGs
= call
->gs
;
190 context
->SegSs
= call
->ss
;
194 /**********************************************************************
195 * INT_SetRealModeContext
197 static void INT_SetRealModeContext( REALMODECALL
*call
, CONTEXT86
*context
)
199 call
->eax
= context
->Eax
;
200 call
->ebx
= context
->Ebx
;
201 call
->ecx
= context
->Ecx
;
202 call
->edx
= context
->Edx
;
203 call
->esi
= context
->Esi
;
204 call
->edi
= context
->Edi
;
205 call
->ebp
= context
->Ebp
;
206 call
->fl
= LOWORD(context
->EFlags
);
207 call
->ip
= LOWORD(context
->Eip
);
208 call
->sp
= LOWORD(context
->Esp
);
209 call
->cs
= context
->SegCs
;
210 call
->ds
= context
->SegDs
;
211 call
->es
= context
->SegEs
;
212 call
->fs
= context
->SegFs
;
213 call
->gs
= context
->SegGs
;
214 call
->ss
= context
->SegSs
;
219 void DPMI_CallRMCB32(RMCB
*rmcb
, UINT16 ss
, DWORD esp
, UINT16
*es
, DWORD
*edi
)
220 #if 0 /* original code, which early gccs puke on */
223 __asm__
__volatile__(
231 ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
237 : "=d" (*es
), "=D" (*edi
), "=S" (_clobber
), "=a" (_clobber
), "=c" (_clobber
)
238 : "0" (ss
), "2" (esp
),
239 "4" (rmcb
->regs_sel
), "1" (rmcb
->regs_ofs
),
240 "3" (&rmcb
->proc_ofs
) );
242 #else /* code generated by a gcc new enough */
244 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32
,
249 "movl 0x8(%ebp),%eax\n\t"
250 "movl 0x10(%ebp),%esi\n\t"
251 "movl 0xc(%ebp),%edx\n\t"
252 "movl 0x10(%eax),%ecx\n\t"
253 "movl 0xc(%eax),%edi\n\t"
262 ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
268 "movl 0x14(%ebp),%eax\n\t"
269 "movw %dx,(%eax)\n\t"
270 "movl 0x18(%ebp),%edx\n\t"
271 "movl %edi,(%edx)\n\t"
278 #endif /* __i386__ */
280 /**********************************************************************
283 * This routine does the hard work of calling a callback procedure.
285 static void DPMI_CallRMCBProc( CONTEXT86
*context
, RMCB
*rmcb
, WORD flag
)
287 if (IS_SELECTOR_SYSTEM( rmcb
->proc_sel
)) {
288 /* Wine-internal RMCB, call directly */
289 ((RMCBPROC
)rmcb
->proc_ofs
)(context
);
295 INT_SetRealModeContext(MapSL(MAKESEGPTR( rmcb
->regs_sel
, rmcb
->regs_ofs
)), context
);
296 ss
= SELECTOR_AllocBlock( (void *)(context
->SegSs
<<4), 0x10000, WINE_LDT_FLAGS_DATA
);
299 FIXME("untested!\n");
301 /* The called proc ends with an IRET, and takes these parameters:
302 * DS:ESI = pointer to real-mode SS:SP
303 * ES:EDI = pointer to real-mode call structure
305 * ES:EDI = pointer to real-mode call structure (may be a copy)
306 * It is the proc's responsibility to change the return CS:IP in the
307 * real-mode call structure. */
309 /* 32-bit DPMI client */
310 DPMI_CallRMCB32(rmcb
, ss
, esp
, &es
, &edi
);
312 /* 16-bit DPMI client */
313 CONTEXT86 ctx
= *context
;
314 ctx
.SegCs
= rmcb
->proc_sel
;
315 ctx
.Eip
= rmcb
->proc_ofs
;
318 ctx
.SegEs
= rmcb
->regs_sel
;
319 ctx
.Edi
= rmcb
->regs_ofs
;
320 /* FIXME: I'm pretty sure this isn't right - should push flags first */
321 wine_call_to_16_regs_short(&ctx
, 0);
326 INT_GetRealModeContext( MapSL( MAKESEGPTR( es
, edi
)), context
);
328 ERR("RMCBs only implemented for i386\n");
334 /**********************************************************************
337 * This routine does the hard work of calling a real mode procedure.
339 int DPMI_CallRMProc( CONTEXT86
*context
, LPWORD stack
, int args
, int iret
)
342 LPVOID addr
= NULL
; /* avoid gcc warning */
343 LPDOSTASK lpDosTask
= Dosvm
.Current();
345 int alloc
= 0, already
= 0;
348 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
349 context
->Eax
, context
->Ebx
, context
->Ecx
, context
->Edx
);
350 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
351 context
->Esi
, context
->Edi
, context
->SegEs
, context
->SegDs
,
352 context
->SegCs
, LOWORD(context
->Eip
), args
, iret
?"IRET":"FAR" );
356 /* there might be some code that just jumps to RMCBs or the like,
357 in which case following the jumps here might get us to a shortcut */
358 code
= CTX_SEG_OFF_TO_LIN(context
, context
->SegCs
, context
->Eip
);
360 case 0xe9: /* JMP NEAR */
361 context
->Eip
+= 3 + *(WORD
*)(code
+1);
362 /* yeah, I know these gotos don't look good... */
363 goto callrmproc_again
;
364 case 0xea: /* JMP FAR */
365 context
->Eip
= *(WORD
*)(code
+1);
366 context
->SegCs
= *(WORD
*)(code
+3);
367 /* ...but since the label is there anyway... */
368 goto callrmproc_again
;
369 case 0xeb: /* JMP SHORT */
370 context
->Eip
+= 2 + *(signed char *)(code
+1);
371 /* ...because of other gotos below, so... */
372 goto callrmproc_again
;
375 /* shortcut for chaining to internal interrupt handlers */
376 if ((context
->SegCs
== 0xF000) && iret
) {
377 return INT_RealModeInterrupt( LOWORD(context
->Eip
)/4, context
);
380 /* shortcut for RMCBs */
381 CurrRMCB
= FirstRMCB
;
383 while (CurrRMCB
&& (HIWORD(CurrRMCB
->address
) != context
->SegCs
))
384 CurrRMCB
= CurrRMCB
->next
;
386 if (!(CurrRMCB
|| lpDosTask
)) {
387 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
388 TRACE("creating VM86 task\n");
389 if ((!DPMI_LoadDosSystem()) || !(lpDosTask
= Dosvm
.LoadDPMI() )) {
390 ERR("could not setup VM86 task\n");
395 if (!context
->SegSs
) {
396 alloc
= 1; /* allocate default stack */
397 stack16
= addr
= DOSMEM_GetBlock( 64, (UINT16
*)&(context
->SegSs
) );
401 ERR("could not allocate default stack\n");
405 stack16
= CTX_SEG_OFF_TO_LIN(context
, context
->SegSs
, context
->Esp
);
407 context
->Esp
-= (args
+ (iret
?1:0)) * sizeof(WORD
);
409 if (args
) memcpy(stack16
, stack
, args
*sizeof(WORD
) );
410 /* push flags if iret */
413 *stack16
= LOWORD(context
->EFlags
);
415 /* push return address (return to interrupt wrapper) */
416 *(--stack16
) = DOSMEM_wrap_seg
;
419 context
->Esp
-= 2*sizeof(WORD
);
424 /* RMCB call, invoke protected-mode handler directly */
425 DPMI_CallRMCBProc(context
, CurrRMCB
, lpDosTask
? lpDosTask
->dpmi_flag
: 0);
426 /* check if we returned to where we thought we would */
427 if ((context
->SegCs
!= DOSMEM_wrap_seg
) ||
428 (LOWORD(context
->Eip
) != 0)) {
429 /* we need to continue at different address in real-mode space,
430 so we need to set it all up for real mode again */
431 goto callrmproc_again
;
434 TRACE("entering real mode...\n");
435 Dosvm
.Enter( context
);
436 TRACE("returned from real-mode call\n");
438 if (alloc
) DOSMEM_FreeBlock( addr
);
443 /**********************************************************************
446 static void CallRMInt( CONTEXT86
*context
)
448 CONTEXT86 realmode_ctx
;
449 FARPROC16 rm_int
= INT_GetRMHandler( BL_reg(context
) );
450 REALMODECALL
*call
= MapSL( MAKESEGPTR( context
->SegEs
, DI_reg(context
) ));
451 INT_GetRealModeContext( call
, &realmode_ctx
);
453 /* we need to check if a real-mode program has hooked the interrupt */
454 if (HIWORD(rm_int
)!=0xF000) {
455 /* yup, which means we need to switch to real mode... */
456 realmode_ctx
.SegCs
= HIWORD(rm_int
);
457 realmode_ctx
.Eip
= LOWORD(rm_int
);
458 if (DPMI_CallRMProc( &realmode_ctx
, NULL
, 0, TRUE
))
461 RESET_CFLAG(context
);
462 /* use the IP we have instead of BL_reg, in case some apps
463 decide to move interrupts around for whatever reason... */
464 if (INT_RealModeInterrupt( LOWORD(rm_int
)/4, &realmode_ctx
))
466 if (context
->EFlags
& 1) {
467 FIXME("%02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
468 BL_reg(context
), realmode_ctx
.Eax
, realmode_ctx
.Ebx
,
469 realmode_ctx
.Ecx
, realmode_ctx
.Edx
);
470 FIXME(" ESI=%08lx EDI=%08lx DS=%04lx ES=%04lx\n",
471 realmode_ctx
.Esi
, realmode_ctx
.Edi
,
472 realmode_ctx
.SegDs
, realmode_ctx
.SegEs
);
475 INT_SetRealModeContext( call
, &realmode_ctx
);
479 static void CallRMProc( CONTEXT86
*context
, int iret
)
481 REALMODECALL
*p
= MapSL( MAKESEGPTR( context
->SegEs
, DI_reg(context
) ));
484 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
485 p
->eax
, p
->ebx
, p
->ecx
, p
->edx
);
486 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
487 p
->esi
, p
->edi
, p
->es
, p
->ds
, p
->cs
, p
->ip
, CX_reg(context
), iret
?"IRET":"FAR" );
489 if (!(p
->cs
) && !(p
->ip
)) { /* remove this check
490 if Int21/6501 case map function
491 has been implemented */
495 INT_GetRealModeContext(p
, &context16
);
496 DPMI_CallRMProc( &context16
, ((LPWORD
)MapSL(MAKESEGPTR(context
->SegSs
, LOWORD(context
->Esp
))))+3,
497 CX_reg(context
), iret
);
498 INT_SetRealModeContext(p
, &context16
);
502 static RMCB
*DPMI_AllocRMCB( void )
504 RMCB
*NewRMCB
= HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB
));
509 LPVOID RMCBmem
= DOSMEM_GetBlock(4, &uParagraph
);
512 *p
++ = 0xcd; /* RMCB: */
513 *p
++ = 0x31; /* int $0x31 */
514 /* it is the called procedure's task to change the return CS:EIP
515 the DPMI 0.9 spec states that if it doesn't, it will be called again */
517 *p
++ = 0xfc; /* jmp RMCB */
518 NewRMCB
->address
= MAKELONG(0, uParagraph
);
519 NewRMCB
->next
= FirstRMCB
;
526 static void AllocRMCB( CONTEXT86
*context
)
528 RMCB
*NewRMCB
= DPMI_AllocRMCB();
530 TRACE("Function to call: %04x:%04x\n", (WORD
)context
->SegDs
, SI_reg(context
) );
534 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
535 NewRMCB
->proc_ofs
= LOWORD(context
->Esi
);
536 NewRMCB
->proc_sel
= context
->SegDs
;
537 NewRMCB
->regs_ofs
= LOWORD(context
->Edi
);
538 NewRMCB
->regs_sel
= context
->SegEs
;
539 SET_LOWORD( context
->Ecx
, HIWORD(NewRMCB
->address
) );
540 SET_LOWORD( context
->Edx
, LOWORD(NewRMCB
->address
) );
544 SET_LOWORD( context
->Eax
, 0x8015 ); /* callback unavailable */
550 FARPROC16 WINAPI
DPMI_AllocInternalRMCB( RMCBPROC proc
)
552 RMCB
*NewRMCB
= DPMI_AllocRMCB();
555 NewRMCB
->proc_ofs
= (DWORD
)proc
;
556 NewRMCB
->proc_sel
= 0;
557 NewRMCB
->regs_ofs
= 0;
558 NewRMCB
->regs_sel
= 0;
559 return (FARPROC16
)(NewRMCB
->address
);
565 static int DPMI_FreeRMCB( DWORD address
)
567 RMCB
*CurrRMCB
= FirstRMCB
;
568 RMCB
*PrevRMCB
= NULL
;
570 while (CurrRMCB
&& (CurrRMCB
->address
!= address
))
573 CurrRMCB
= CurrRMCB
->next
;
578 PrevRMCB
->next
= CurrRMCB
->next
;
580 FirstRMCB
= CurrRMCB
->next
;
581 DOSMEM_FreeBlock(DOSMEM_MapRealToLinear(CurrRMCB
->address
));
582 HeapFree(GetProcessHeap(), 0, CurrRMCB
);
589 static void FreeRMCB( CONTEXT86
*context
)
591 FIXME("callback address: %04x:%04x\n",
592 CX_reg(context
), DX_reg(context
));
594 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context
), CX_reg(context
)))) {
595 SET_LOWORD( context
->Eax
, 0x8024 ); /* invalid callback address */
601 void WINAPI
DPMI_FreeInternalRMCB( FARPROC16 proc
)
603 DPMI_FreeRMCB( (DWORD
)proc
);
607 /* (see dosmem.c, function DOSMEM_InitDPMI) */
609 static void StartPM( CONTEXT86
*context
, LPDOSTASK lpDosTask
)
611 UINT16 cs
, ss
, ds
, es
;
613 DWORD psp_ofs
= (DWORD
)(lpDosTask
->psp_seg
<<4);
614 PDB16
*psp
= (PDB16
*)psp_ofs
;
615 HANDLE16 env_seg
= psp
->environment
;
616 unsigned char selflags
= WINE_LDT_FLAGS_DATA
;
618 RESET_CFLAG(context
);
619 lpDosTask
->dpmi_flag
= AX_reg(context
);
620 /* our mode switch wrapper have placed the desired CS into DX */
621 cs
= SELECTOR_AllocBlock( (void *)(DX_reg(context
)<<4), 0x10000, WINE_LDT_FLAGS_CODE
);
622 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
623 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
624 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
625 32-bit code using this stack. */
626 if (lpDosTask
->dpmi_flag
& 1) selflags
|= WINE_LDT_FLAGS_32BIT
;
627 ss
= SELECTOR_AllocBlock( (void *)(context
->SegSs
<<4), 0x10000, selflags
);
628 /* do the same for the data segments, just in case */
629 if (context
->SegDs
== context
->SegSs
) ds
= ss
;
630 else ds
= SELECTOR_AllocBlock( (void *)(context
->SegDs
<<4), 0x10000, selflags
);
631 es
= SELECTOR_AllocBlock( psp
, 0x100, selflags
);
632 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
633 psp
->environment
= SELECTOR_AllocBlock( (void *)(env_seg
<<4), 0x10000, WINE_LDT_FLAGS_DATA
);
636 pm_ctx
.SegCs
= DOSMEM_dpmi_sel
;
637 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
645 TRACE("DOS program is now entering protected mode\n");
646 wine_call_to_16_regs_short(&pm_ctx
, 0);
648 /* in the current state of affairs, we won't ever actually return here... */
649 /* we should have int21/ah=4c do it someday, though... */
651 FreeSelector16(psp
->environment
);
652 psp
->environment
= env_seg
;
654 if (ds
!= ss
) FreeSelector16(ds
);
659 /* DPMI Raw Mode Switch handler */
662 void WINAPI
DPMI_RawModeSwitch( SIGCONTEXT
*context
)
664 LPDOSTASK lpDosTask
= Dosvm
.Current();
669 /* we could probably start a DPMI-only dosmod task here, but I doubt
670 anything other than real DOS apps want to call raw mode switch */
671 ERR("attempting raw mode switch without DOS task!\n");
674 /* initialize real-mode context as per spec */
675 memset(&rm_ctx
, 0, sizeof(rm_ctx
));
676 rm_ctx
.SegDs
= AX_sig(context
);
677 rm_ctx
.SegEs
= CX_sig(context
);
678 rm_ctx
.SegSs
= DX_sig(context
);
679 rm_ctx
.Esp
= EBX_sig(context
);
680 rm_ctx
.SegCs
= SI_sig(context
);
681 rm_ctx
.Eip
= EDI_sig(context
);
682 rm_ctx
.Ebp
= EBP_sig(context
);
685 rm_ctx
.EFlags
= EFL_sig(context
); /* at least we need the IF flag */
687 /* enter real mode again */
688 TRACE("re-entering real mode at %04lx:%04lx\n",rm_ctx
.SegCs
,rm_ctx
.Eip
);
689 ret
= Dosvm
.Enter( &rm_ctx
);
690 /* when the real-mode stuff call its mode switch address,
691 Dosvm.Enter will return and we will continue here */
694 /* if the sync was lost, there's no way to recover */
698 /* alter protected-mode context as per spec */
699 DS_sig(context
) = LOWORD(rm_ctx
.Eax
);
700 ES_sig(context
) = LOWORD(rm_ctx
.Ecx
);
701 SS_sig(context
) = LOWORD(rm_ctx
.Edx
);
702 ESP_sig(context
) = rm_ctx
.Ebx
;
703 CS_sig(context
) = LOWORD(rm_ctx
.Esi
);
704 EIP_sig(context
) = rm_ctx
.Edi
;
705 EBP_sig(context
) = rm_ctx
.Ebp
;
709 /* Return to new address and hope that we didn't mess up */
710 TRACE("re-entering protected mode at %04x:%08lx\n",
711 CS_sig(context
), EIP_sig(context
));
716 /**********************************************************************
717 * INT_Int31Handler (WPROCS.149)
719 * Handler for int 31h (DPMI).
722 void WINAPI
INT_Int31Handler( CONTEXT86
*context
)
725 * Note: For Win32s processes, the whole linear address space is
726 * shifted by 0x10000 relative to the OS linear address space.
727 * See the comment in msdos/vxd.c.
732 LPDOSTASK lpDosTask
= Dosvm
.Current();
734 if (ISV86(context
) && lpDosTask
) {
735 /* Called from real mode, check if it's our wrapper */
736 TRACE("called from real mode\n");
737 if (context
->SegCs
==DOSMEM_dpmi_seg
) {
738 /* This is the protected mode switch */
739 StartPM(context
,lpDosTask
);
742 if (context
->SegCs
==DOSMEM_xms_seg
) {
743 /* This is the XMS driver entry point */
744 XMS_Handler(context
);
749 RMCB
*CurrRMCB
= FirstRMCB
;
751 while (CurrRMCB
&& (HIWORD(CurrRMCB
->address
) != context
->SegCs
))
752 CurrRMCB
= CurrRMCB
->next
;
755 /* RMCB call, propagate to protected-mode handler */
756 DPMI_CallRMCBProc(context
, CurrRMCB
, lpDosTask
->dpmi_flag
);
762 RESET_CFLAG(context
);
763 switch(AX_reg(context
))
765 case 0x0000: /* Allocate LDT descriptors */
766 TRACE("allocate LDT descriptors (%d)\n",CX_reg(context
));
767 if (!(context
->Eax
= AllocSelectorArray16( CX_reg(context
) )))
770 context
->Eax
= 0x8011; /* descriptor unavailable */
773 TRACE("success, array starts at 0x%04x\n",AX_reg(context
));
776 case 0x0001: /* Free LDT descriptor */
777 TRACE("free LDT descriptor (0x%04x)\n",BX_reg(context
));
778 if (FreeSelector16( BX_reg(context
) ))
780 context
->Eax
= 0x8022; /* invalid selector */
785 /* If a segment register contains the selector being freed, */
786 /* set it to zero. */
787 if (!((context
->SegDs
^BX_reg(context
)) & ~3)) context
->SegDs
= 0;
788 if (!((context
->SegEs
^BX_reg(context
)) & ~3)) context
->SegEs
= 0;
789 if (!((context
->SegFs
^BX_reg(context
)) & ~3)) context
->SegFs
= 0;
790 if (!((context
->SegGs
^BX_reg(context
)) & ~3)) context
->SegGs
= 0;
794 case 0x0002: /* Real mode segment to descriptor */
795 TRACE("real mode segment to descriptor (0x%04x)\n",BX_reg(context
));
797 WORD entryPoint
= 0; /* KERNEL entry point for descriptor */
798 switch(BX_reg(context
))
800 case 0x0000: entryPoint
= 183; break; /* __0000H */
801 case 0x0040: entryPoint
= 193; break; /* __0040H */
802 case 0xa000: entryPoint
= 174; break; /* __A000H */
803 case 0xb000: entryPoint
= 181; break; /* __B000H */
804 case 0xb800: entryPoint
= 182; break; /* __B800H */
805 case 0xc000: entryPoint
= 195; break; /* __C000H */
806 case 0xd000: entryPoint
= 179; break; /* __D000H */
807 case 0xe000: entryPoint
= 190; break; /* __E000H */
808 case 0xf000: entryPoint
= 194; break; /* __F000H */
810 context
->Eax
= DOSMEM_AllocSelector(BX_reg(context
));
814 context
->Eax
= LOWORD(GetProcAddress16( GetModuleHandle16( "KERNEL" ),
815 (LPCSTR
)(ULONG_PTR
)entryPoint
));
819 case 0x0003: /* Get next selector increment */
820 TRACE("get selector increment (__AHINCR)\n");
821 context
->Eax
= __AHINCR
;
824 case 0x0004: /* Lock selector (not supported) */
825 FIXME("lock selector not supported\n");
826 context
->Eax
= 0; /* FIXME: is this a correct return value? */
829 case 0x0005: /* Unlock selector (not supported) */
830 FIXME("unlock selector not supported\n");
831 context
->Eax
= 0; /* FIXME: is this a correct return value? */
834 case 0x0006: /* Get selector base address */
835 TRACE("get selector base address (0x%04x)\n",BX_reg(context
));
836 if (!(dw
= GetSelectorBase( BX_reg(context
) )))
838 context
->Eax
= 0x8022; /* invalid selector */
843 CX_reg(context
) = HIWORD(W32S_WINE2APP(dw
));
844 DX_reg(context
) = LOWORD(W32S_WINE2APP(dw
));
848 case 0x0007: /* Set selector base address */
849 TRACE("set selector base address (0x%04x,0x%08lx)\n",
851 W32S_APP2WINE(MAKELONG(DX_reg(context
),CX_reg(context
))));
852 dw
= W32S_APP2WINE(MAKELONG(DX_reg(context
), CX_reg(context
)));
854 /* app wants to access lower 64K of DOS memory, map it in now */
856 SetSelectorBase(BX_reg(context
), dw
);
859 case 0x0008: /* Set selector limit */
860 TRACE("set selector limit (0x%04x,0x%08lx)\n",BX_reg(context
),MAKELONG(DX_reg(context
),CX_reg(context
)));
861 dw
= MAKELONG( DX_reg(context
), CX_reg(context
) );
862 SetSelectorLimit16( BX_reg(context
), dw
);
865 case 0x0009: /* Set selector access rights */
866 TRACE("set selector access rights(0x%04x,0x%04x)\n",BX_reg(context
),CX_reg(context
));
867 SelectorAccessRights16( BX_reg(context
), 1, CX_reg(context
) );
870 case 0x000a: /* Allocate selector alias */
871 TRACE("allocate selector alias (0x%04x)\n",BX_reg(context
));
872 if (!(AX_reg(context
) = AllocCStoDSAlias16( BX_reg(context
) )))
874 AX_reg(context
) = 0x8011; /* descriptor unavailable */
879 case 0x000b: /* Get descriptor */
880 TRACE("get descriptor (0x%04x)\n",BX_reg(context
));
883 wine_ldt_set_base( &entry
, (void*)W32S_WINE2APP(wine_ldt_get_base(&entry
)) );
884 /* FIXME: should use ES:EDI for 32-bit clients */
885 *(LDT_ENTRY
*)MapSL( MAKESEGPTR( context
->SegEs
, LOWORD(context
->Edi
) )) = entry
;
889 case 0x000c: /* Set descriptor */
890 TRACE("set descriptor (0x%04x)\n",BX_reg(context
));
892 LDT_ENTRY entry
= *(LDT_ENTRY
*)MapSL(MAKESEGPTR( context
->SegEs
, LOWORD(context
->Edi
)));
893 wine_ldt_set_base( &entry
, (void*)W32S_APP2WINE(wine_ldt_get_base(&entry
)) );
894 wine_ldt_set_entry( LOWORD(context
->Ebx
), &entry
);
898 case 0x000d: /* Allocate specific LDT descriptor */
899 FIXME("allocate descriptor (0x%04x), stub!\n",BX_reg(context
));
900 AX_reg(context
) = 0x8011; /* descriptor unavailable */
903 case 0x0100: /* Allocate DOS memory block */
904 TRACE("allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context
));
905 dw
= GlobalDOSAlloc16((DWORD
)BX_reg(context
)<<4);
907 AX_reg(context
) = HIWORD(dw
);
908 DX_reg(context
) = LOWORD(dw
);
910 AX_reg(context
) = 0x0008; /* insufficient memory */
911 BX_reg(context
) = DOSMEM_Available()>>4;
915 case 0x0101: /* Free DOS memory block */
916 TRACE("free DOS memory block (0x%04x)\n",DX_reg(context
));
917 dw
= GlobalDOSFree16(DX_reg(context
));
919 AX_reg(context
) = 0x0009; /* memory block address invalid */
923 case 0x0200: /* get real mode interrupt vector */
924 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
928 case 0x0201: /* set real mode interrupt vector */
929 FIXME("set realmode interrupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context
),CX_reg(context
),DX_reg(context
));
932 case 0x0204: /* Get protected mode interrupt vector */
933 TRACE("get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context
));
934 dw
= (DWORD
)INT_GetPMHandler( BL_reg(context
) );
935 CX_reg(context
) = HIWORD(dw
);
936 DX_reg(context
) = LOWORD(dw
);
939 case 0x0205: /* Set protected mode interrupt vector */
940 TRACE("set protected mode interrupt handler (0x%02x,%p), stub!\n",
941 BL_reg(context
),MapSL(MAKESEGPTR(CX_reg(context
),DX_reg(context
))));
942 INT_SetPMHandler( BL_reg(context
),
943 (FARPROC16
)MAKESEGPTR( CX_reg(context
), DX_reg(context
) ));
946 case 0x0300: /* Simulate real mode interrupt */
947 CallRMInt( context
);
950 case 0x0301: /* Call real mode procedure with far return */
951 CallRMProc( context
, FALSE
);
954 case 0x0302: /* Call real mode procedure with interrupt return */
955 CallRMProc( context
, TRUE
);
958 case 0x0303: /* Allocate Real Mode Callback Address */
959 AllocRMCB( context
);
962 case 0x0304: /* Free Real Mode Callback Address */
966 case 0x0305: /* Get State Save/Restore Addresses */
967 TRACE("get state save/restore addresses\n");
968 /* we probably won't need this kind of state saving */
970 /* real mode: just point to the lret */
971 BX_reg(context
) = DOSMEM_wrap_seg
;
973 /* protected mode: don't have any handler yet... */
974 FIXME("no protected-mode dummy state save/restore handler yet\n");
979 case 0x0306: /* Get Raw Mode Switch Addresses */
980 TRACE("get raw mode switch addresses\n");
981 /* real mode, point to standard DPMI return wrapper */
982 BX_reg(context
) = DOSMEM_wrap_seg
;
984 /* protected mode, point to DPMI call wrapper */
985 SI_reg(context
) = DOSMEM_dpmi_sel
;
986 context
->Edi
= 8; /* offset of the INT 0x31 call */
988 case 0x0400: /* Get DPMI version */
989 TRACE("get DPMI version\n");
994 AX_reg(context
) = 0x005a; /* DPMI version 0.90 */
995 BX_reg(context
) = 0x0005; /* Flags: 32-bit, virtual memory */
996 CL_reg(context
) = si
.wProcessorLevel
;
997 DX_reg(context
) = 0x0102; /* Master/slave interrupt controller base*/
1000 case 0x0500: /* Get free memory information */
1001 TRACE("get free memory information\n");
1005 mmi
.dwSize
= sizeof(mmi
);
1007 ptr
= MapSL(MAKESEGPTR(context
->SegEs
,DI_reg(context
)));
1008 /* the layout is just the same as MEMMANINFO, but without
1011 memcpy(ptr
,((char*)&mmi
)+4,sizeof(mmi
)-4);
1014 case 0x0501: /* Allocate memory block */
1015 TRACE("allocate memory block (%ld)\n",MAKELONG(CX_reg(context
),BX_reg(context
)));
1016 if (!(ptr
= (BYTE
*)DPMI_xalloc(MAKELONG(CX_reg(context
), BX_reg(context
)))))
1018 AX_reg(context
) = 0x8012; /* linear memory not available */
1021 BX_reg(context
) = SI_reg(context
) = HIWORD(W32S_WINE2APP(ptr
));
1022 CX_reg(context
) = DI_reg(context
) = LOWORD(W32S_WINE2APP(ptr
));
1026 case 0x0502: /* Free memory block */
1027 TRACE("free memory block (0x%08lx)\n",
1028 W32S_APP2WINE(MAKELONG(DI_reg(context
),SI_reg(context
))));
1029 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context
), SI_reg(context
))) );
1032 case 0x0503: /* Resize memory block */
1033 TRACE("resize memory block (0x%08lx,%ld)\n",
1034 W32S_APP2WINE(MAKELONG(DI_reg(context
),SI_reg(context
))),
1035 MAKELONG(CX_reg(context
),BX_reg(context
)));
1036 if (!(ptr
= (BYTE
*)DPMI_xrealloc(
1037 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context
),SI_reg(context
))),
1038 MAKELONG(CX_reg(context
),BX_reg(context
)))))
1040 AX_reg(context
) = 0x8012; /* linear memory not available */
1043 BX_reg(context
) = SI_reg(context
) = HIWORD(W32S_WINE2APP(ptr
));
1044 CX_reg(context
) = DI_reg(context
) = LOWORD(W32S_WINE2APP(ptr
));
1048 case 0x0507: /* Modify page attributes */
1049 FIXME("modify page attributes unimplemented\n");
1050 break; /* Just ignore it */
1052 case 0x0600: /* Lock linear region */
1053 FIXME("lock linear region unimplemented\n");
1054 break; /* Just ignore it */
1056 case 0x0601: /* Unlock linear region */
1057 FIXME("unlock linear region unimplemented\n");
1058 break; /* Just ignore it */
1060 case 0x0602: /* Unlock real-mode region */
1061 FIXME("unlock realmode region unimplemented\n");
1062 break; /* Just ignore it */
1064 case 0x0603: /* Lock real-mode region */
1065 FIXME("lock realmode region unimplemented\n");
1066 break; /* Just ignore it */
1068 case 0x0604: /* Get page size */
1069 TRACE("get pagesize\n");
1070 BX_reg(context
) = 0;
1071 CX_reg(context
) = getpagesize();
1074 case 0x0702: /* Mark page as demand-paging candidate */
1075 FIXME("mark page as demand-paging candidate\n");
1076 break; /* Just ignore it */
1078 case 0x0703: /* Discard page contents */
1079 FIXME("discard page contents\n");
1080 break; /* Just ignore it */
1082 case 0x0800: /* Physical address mapping */
1083 FIXME("map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context
),BX_reg(context
)));
1084 if(!(ptr
=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context
),BX_reg(context
)))))
1086 AX_reg(context
) = 0x8021;
1091 BX_reg(context
) = HIWORD(W32S_WINE2APP(ptr
));
1092 CX_reg(context
) = LOWORD(W32S_WINE2APP(ptr
));
1093 RESET_CFLAG(context
);
1098 INT_BARF( context
, 0x31 );
1099 AX_reg(context
) = 0x8001; /* unsupported function */