4 * Copyright 1995 Alexandre Julliard
10 #include "wine/winbase16.h"
17 #include "thread.h" /* for !MZ_SUPPORTED */
18 #include "stackframe.h" /* for !MZ_SUPPORTED */
20 #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;
68 /**********************************************************************
70 * special virtualalloc, allocates lineary monoton growing memory.
71 * (the usual VirtualAlloc does not satisfy that restriction)
74 DPMI_xalloc(int len) {
76 LPVOID oldlastv = lastvalloced;
82 ret=VirtualAlloc(lastvalloced,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
84 lastvalloced = (char *) lastvalloced + 0x10000;
85 /* we failed to allocate one in the first round.
88 if (!xflag && (lastvalloced<oldlastv)) { /* wrapped */
89 FIXME("failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len);
92 /* if we even fail to allocate something in the next
95 if ((xflag==1) && (lastvalloced >= oldlastv))
97 if ((xflag==2) && (lastvalloced < oldlastv)) {
98 FIXME("failed to allocate any memory of %d bytes!\n",len);
103 ret=VirtualAlloc(NULL,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
104 lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
109 DPMI_xfree(LPVOID ptr) {
110 VirtualFree(ptr,0,MEM_RELEASE);
113 /* FIXME: perhaps we could grow this mapped area... */
115 DPMI_xrealloc(LPVOID ptr,int newsize) {
116 MEMORY_BASIC_INFORMATION mbi;
119 newptr = DPMI_xalloc(newsize);
121 if (!VirtualQuery(ptr,&mbi,sizeof(mbi))) {
122 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
125 if (mbi.State == MEM_FREE) {
126 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
129 /* We do not shrink allocated memory. most reallocs
130 * only do grows anyway
132 if (newsize<=mbi.RegionSize)
134 memcpy(newptr,ptr,mbi.RegionSize);
139 /**********************************************************************
140 * INT_GetRealModeContext
142 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
144 EAX_reg(context) = call->eax;
145 EBX_reg(context) = call->ebx;
146 ECX_reg(context) = call->ecx;
147 EDX_reg(context) = call->edx;
148 ESI_reg(context) = call->esi;
149 EDI_reg(context) = call->edi;
150 EBP_reg(context) = call->ebp;
151 EFL_reg(context) = call->fl | V86_FLAG;
152 EIP_reg(context) = call->ip;
153 ESP_reg(context) = call->sp;
154 CS_reg(context) = call->cs;
155 DS_reg(context) = call->ds;
156 ES_reg(context) = call->es;
157 FS_reg(context) = call->fs;
158 GS_reg(context) = call->gs;
159 SS_reg(context) = call->ss;
160 V86BASE(context) = (DWORD) DOSMEM_MemoryBase(0);
164 /**********************************************************************
165 * INT_SetRealModeContext
167 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
169 call->eax = EAX_reg(context);
170 call->ebx = EBX_reg(context);
171 call->ecx = ECX_reg(context);
172 call->edx = EDX_reg(context);
173 call->esi = ESI_reg(context);
174 call->edi = EDI_reg(context);
175 call->ebp = EBP_reg(context);
176 call->fl = FL_reg(context);
177 call->ip = IP_reg(context);
178 call->sp = SP_reg(context);
179 call->cs = CS_reg(context);
180 call->ds = DS_reg(context);
181 call->es = ES_reg(context);
182 call->fs = FS_reg(context);
183 call->gs = GS_reg(context);
184 call->ss = SS_reg(context);
188 /**********************************************************************
191 * This routine does the hard work of calling a callback procedure.
193 static void DPMI_CallRMCBProc( CONTEXT86 *context, RMCB *rmcb, WORD flag )
195 if (IS_SELECTOR_SYSTEM( rmcb->proc_sel )) {
196 /* Wine-internal RMCB, call directly */
197 ((RMCBPROC)rmcb->proc_ofs)(context);
203 INT_SetRealModeContext((REALMODECALL *)PTR_SEG_OFF_TO_LIN( rmcb->regs_sel, rmcb->regs_ofs ), context);
204 ss = SELECTOR_AllocBlock( DOSMEM_MemoryBase(0) + (DWORD)(SS_reg(context)<<4), 0x10000, SEGMENT_DATA, FALSE, FALSE );
206 FIXME("untested!\n");
208 /* The called proc ends with an IRET, and takes these parameters:
209 * DS:ESI = pointer to real-mode SS:SP
210 * ES:EDI = pointer to real-mode call structure
212 * ES:EDI = pointer to real-mode call structure (may be a copy)
213 * It is the proc's responsibility to change the return CS:IP in the
214 * real-mode call structure. */
217 /* 32-bit DPMI client */
218 __asm__ __volatile__(
228 : "=g" (es), "=D" (edi), "=S" (_clobber)
229 : "m" (rmcb->proc_ofs),
230 "g" (ss), "g" (rmcb->regs_sel),
231 "S" (ESP_reg(context)), "1" (rmcb->regs_ofs)
232 : "ecx", "edx", "ebp" );
234 /* 16-bit DPMI client */
235 CONTEXT86 ctx = *context;
236 CS_reg(&ctx) = rmcb->proc_sel;
237 EIP_reg(&ctx) = rmcb->proc_ofs;
239 ESI_reg(&ctx) = ESP_reg(context);
240 ES_reg(&ctx) = rmcb->regs_sel;
241 EDI_reg(&ctx) = rmcb->regs_ofs;
242 Callbacks->CallRegisterShortProc(&ctx, 2);
246 SELECTOR_FreeBlock(ss, 1);
247 INT_GetRealModeContext((REALMODECALL*)PTR_SEG_OFF_TO_LIN( es, edi ), context);
249 ERR("RMCBs only implemented for i386\n");
255 /**********************************************************************
258 * This routine does the hard work of calling a real mode procedure.
260 int DPMI_CallRMProc( CONTEXT86 *context, LPWORD stack, int args, int iret )
266 #endif /* !MZ_SUPPORTED */
267 LPVOID addr = NULL; /* avoid gcc warning */
268 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
269 NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
271 int alloc = 0, already = 0;
274 GlobalUnlock16( GetCurrentTask() );
276 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
277 EAX_reg(context), EBX_reg(context), ECX_reg(context), EDX_reg(context) );
278 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
279 ESI_reg(context), EDI_reg(context), ES_reg(context), DS_reg(context),
280 CS_reg(context), IP_reg(context), args, iret?"IRET":"FAR" );
284 /* there might be some code that just jumps to RMCBs or the like,
285 in which case following the jumps here might get us to a shortcut */
286 code = CTX_SEG_OFF_TO_LIN(context, CS_reg(context), EIP_reg(context));
288 case 0xe9: /* JMP NEAR */
289 IP_reg(context) += 3 + *(WORD *)(code+1);
290 /* yeah, I know these gotos don't look good... */
291 goto callrmproc_again;
292 case 0xea: /* JMP FAR */
293 IP_reg(context) = *(WORD *)(code+1);
294 CS_reg(context) = *(WORD *)(code+3);
295 /* ...but since the label is there anyway... */
296 goto callrmproc_again;
297 case 0xeb: /* JMP SHORT */
298 IP_reg(context) += 2 + *(signed char *)(code+1);
299 /* ...because of other gotos below, so... */
300 goto callrmproc_again;
303 /* shortcut for chaining to internal interrupt handlers */
304 if ((CS_reg(context) == 0xF000) && iret) {
305 return INT_RealModeInterrupt( IP_reg(context)/4, context);
308 /* shortcut for RMCBs */
309 CurrRMCB = FirstRMCB;
311 while (CurrRMCB && (HIWORD(CurrRMCB->address) != CS_reg(context)))
312 CurrRMCB = CurrRMCB->next;
315 if (!(CurrRMCB || pModule->lpDosTask)) {
316 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
317 TRACE("creating VM86 task\n");
318 if (!MZ_InitTask( MZ_AllocDPMITask( pModule->self ) )) {
319 ERR("could not setup VM86 task\n");
324 if (!SS_reg(context)) {
325 alloc = 1; /* allocate default stack */
326 stack16 = addr = DOSMEM_GetBlock( pModule->self, 64, (UINT16 *)&(SS_reg(context)) );
327 SP_reg(context) = 64-2;
330 ERR("could not allocate default stack\n");
334 stack16 = CTX_SEG_OFF_TO_LIN(context, SS_reg(context), ESP_reg(context));
336 SP_reg(context) -= (args + (iret?1:0)) * sizeof(WORD);
339 stack16 = (LPWORD) CURRENT_STACK16;
342 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
343 /* push flags if iret */
346 *stack16 = FL_reg(context);
349 /* push return address (return to interrupt wrapper) */
350 *(--stack16) = DPMI_wrap_seg;
353 SP_reg(context) -= 2*sizeof(WORD);
359 /* RMCB call, invoke protected-mode handler directly */
360 DPMI_CallRMCBProc(context, CurrRMCB, pModule->lpDosTask?pModule->lpDosTask->dpmi_flag:0);
361 /* check if we returned to where we thought we would */
362 if ((CS_reg(context) != DPMI_wrap_seg) ||
363 (IP_reg(context) != 0)) {
364 /* we need to continue at different address in real-mode space,
365 so we need to set it all up for real mode again */
366 goto callrmproc_again;
370 #if 0 /* this was probably unnecessary */
371 /* push call address */
372 *(--stack16) = CS_reg(context);
373 *(--stack16) = IP_reg(context);
375 SP_reg(context) -= 2*sizeof(WORD);
376 /* set initial CS:IP to the wrapper's "lret" */
377 CS_reg(context) = DPMI_wrap_seg;
380 TRACE("entering real mode...\n");
381 DOSVM_Enter( context );
382 TRACE("returned from real-mode call\n");
384 addr = CTX_SEG_OFF_TO_LIN(context, CS_reg(context), EIP_reg(context));
385 sel = SELECTOR_AllocBlock( addr, 0x10000, SEGMENT_CODE, FALSE, FALSE );
386 seg_addr = PTR_SEG_OFF_TO_SEGPTR( sel, 0 );
388 CS_reg(context) = HIWORD(seg_addr);
389 IP_reg(context) = LOWORD(seg_addr);
390 EBP_reg(context) = OFFSETOF( NtCurrentTeb()->cur_stack )
391 + (WORD)&((STACK16FRAME*)0)->bp;
392 Callbacks->CallRegisterShortProc(context, args*sizeof(WORD));
393 SELECTOR_FreeBlock(sel, 1);
396 if (alloc) DOSMEM_FreeBlock( pModule->self, addr );
401 /**********************************************************************
404 static void CallRMInt( CONTEXT86 *context )
406 CONTEXT86 realmode_ctx;
407 FARPROC16 rm_int = INT_GetRMHandler( BL_reg(context) );
408 REALMODECALL *call = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context),
410 INT_GetRealModeContext( call, &realmode_ctx );
412 /* we need to check if a real-mode program has hooked the interrupt */
413 if (HIWORD(rm_int)!=0xF000) {
414 /* yup, which means we need to switch to real mode... */
415 CS_reg(&realmode_ctx) = HIWORD(rm_int);
416 EIP_reg(&realmode_ctx) = LOWORD(rm_int);
417 if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
420 RESET_CFLAG(context);
421 /* use the IP we have instead of BL_reg, in case some apps
422 decide to move interrupts around for whatever reason... */
423 if (INT_RealModeInterrupt( LOWORD(rm_int)/4, &realmode_ctx ))
425 if (EFL_reg(context)&1) {
426 FIXME("%02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
427 BL_reg(context), EAX_reg(&realmode_ctx), EBX_reg(&realmode_ctx),
428 ECX_reg(&realmode_ctx), EDX_reg(&realmode_ctx));
429 FIXME(" ESI=%08lx EDI=%08lx DS=%04lx ES=%04lx\n",
430 ESI_reg(&realmode_ctx), EDI_reg(&realmode_ctx),
431 DS_reg(&realmode_ctx), ES_reg(&realmode_ctx) );
434 INT_SetRealModeContext( call, &realmode_ctx );
438 static void CallRMProc( CONTEXT86 *context, int iret )
440 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
443 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
444 p->eax, p->ebx, p->ecx, p->edx);
445 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
446 p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
448 if (!(p->cs) && !(p->ip)) { /* remove this check
449 if Int21/6501 case map function
450 has been implemented */
454 INT_GetRealModeContext(p, &context16);
455 DPMI_CallRMProc( &context16, ((LPWORD)PTR_SEG_OFF_TO_LIN(SS_reg(context), SP_reg(context)))+3,
456 CX_reg(context), iret );
457 INT_SetRealModeContext(p, &context16);
461 static void WINAPI WINE_UNUSED RMCallbackProc( RMCB *rmcb )
463 /* This routine should call DPMI_CallRMCBProc, but we don't have the
464 register structure available - this is easily fixed by going through
465 a Win16 register relay instead of calling RMCallbackProc "directly",
466 but I won't bother at this time. */
467 FIXME("not properly supported on your architecture!\n");
470 static RMCB *DPMI_AllocRMCB( void )
472 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
478 LPVOID RMCBmem = DOSMEM_GetBlock(0, 4, &uParagraph);
481 *p++ = 0xcd; /* RMCB: */
482 *p++ = 0x31; /* int $0x31 */
483 /* it is the called procedure's task to change the return CS:EIP
484 the DPMI 0.9 spec states that if it doesn't, it will be called again */
486 *p++ = 0xfc; /* jmp RMCB */
488 LPVOID RMCBmem = DOSMEM_GetBlock(0, 15, &uParagraph);
491 *p++ = 0x68; /* pushl */
492 *(LPVOID *)p = NewRMCB;
494 *p++ = 0x9a; /* lcall */
495 *(FARPROC16 *)p = (FARPROC16)RMCallbackProc; /* FIXME: register relay */
499 *p++=0xc3; /* lret (FIXME?) */
501 NewRMCB->address = MAKELONG(0, uParagraph);
502 NewRMCB->next = FirstRMCB;
509 static void AllocRMCB( CONTEXT86 *context )
511 RMCB *NewRMCB = DPMI_AllocRMCB();
513 TRACE("Function to call: %04x:%04x\n", (WORD)DS_reg(context), SI_reg(context) );
517 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
518 NewRMCB->proc_ofs = SI_reg(context);
519 NewRMCB->proc_sel = DS_reg(context);
520 NewRMCB->regs_ofs = DI_reg(context);
521 NewRMCB->regs_sel = ES_reg(context);
522 CX_reg(context) = HIWORD(NewRMCB->address);
523 DX_reg(context) = LOWORD(NewRMCB->address);
527 AX_reg(context) = 0x8015; /* callback unavailable */
533 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
535 RMCB *NewRMCB = DPMI_AllocRMCB();
538 NewRMCB->proc_ofs = (DWORD)proc;
539 NewRMCB->proc_sel = 0;
540 NewRMCB->regs_ofs = 0;
541 NewRMCB->regs_sel = 0;
542 return (FARPROC16)(NewRMCB->address);
548 static int DPMI_FreeRMCB( DWORD address )
550 RMCB *CurrRMCB = FirstRMCB;
551 RMCB *PrevRMCB = NULL;
553 while (CurrRMCB && (CurrRMCB->address != address))
556 CurrRMCB = CurrRMCB->next;
561 PrevRMCB->next = CurrRMCB->next;
563 FirstRMCB = CurrRMCB->next;
564 DOSMEM_FreeBlock(0, DOSMEM_MapRealToLinear(CurrRMCB->address));
565 HeapFree(GetProcessHeap(), 0, CurrRMCB);
572 static void FreeRMCB( CONTEXT86 *context )
574 FIXME("callback address: %04x:%04x\n",
575 CX_reg(context), DX_reg(context));
577 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
578 AX_reg(context) = 0x8024; /* invalid callback address */
584 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
586 DPMI_FreeRMCB( (DWORD)proc );
591 /* (see loader/dos/module.c, function MZ_InitDPMI) */
593 static void StartPM( CONTEXT86 *context, LPDOSTASK lpDosTask )
595 char *base = DOSMEM_MemoryBase(0);
596 UINT16 cs, ss, ds, es;
598 DWORD psp_ofs = (DWORD)(lpDosTask->psp_seg<<4);
599 PDB16 *psp = (PDB16 *)(base + psp_ofs);
600 HANDLE16 env_seg = psp->environment;
603 RESET_CFLAG(context);
604 lpDosTask->dpmi_flag = AX_reg(context);
605 is32 = lpDosTask->dpmi_flag & 1;
606 /* our mode switch wrapper have placed the desired CS into DX */
607 cs = SELECTOR_AllocBlock( base + (DWORD)(DX_reg(context)<<4), 0x10000, SEGMENT_CODE, FALSE, FALSE );
608 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
609 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
610 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
611 32-bit code using this stack. */
612 ss = SELECTOR_AllocBlock( base + (DWORD)(SS_reg(context)<<4), 0x10000, SEGMENT_DATA, is32, FALSE );
613 /* do the same for the data segments, just in case */
614 if (DS_reg(context) == SS_reg(context)) ds = ss;
615 else ds = SELECTOR_AllocBlock( base + (DWORD)(DS_reg(context)<<4), 0x10000, SEGMENT_DATA, is32, FALSE );
616 es = SELECTOR_AllocBlock( base + psp_ofs, 0x100, SEGMENT_DATA, is32, FALSE );
617 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
618 psp->environment = SELECTOR_AllocBlock( base + (DWORD)(env_seg<<4),
619 0x10000, SEGMENT_DATA, FALSE, FALSE );
622 CS_reg(&pm_ctx) = lpDosTask->dpmi_sel;
623 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
624 AX_reg(&pm_ctx) = ss;
625 DX_reg(&pm_ctx) = cs;
626 DS_reg(&pm_ctx) = ds;
627 ES_reg(&pm_ctx) = es;
631 TRACE("DOS program is now entering protected mode\n");
632 Callbacks->CallRegisterShortProc(&pm_ctx, 0);
634 /* in the current state of affairs, we won't ever actually return here... */
635 /* we should have int21/ah=4c do it someday, though... */
637 SELECTOR_FreeBlock(psp->environment, 1);
638 psp->environment = env_seg;
639 SELECTOR_FreeBlock(es, 1);
640 if (ds != ss) SELECTOR_FreeBlock(ds, 1);
641 SELECTOR_FreeBlock(ss, 1);
642 SELECTOR_FreeBlock(cs, 1);
645 /* DPMI Raw Mode Switch handler */
647 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
649 LPDOSTASK lpDosTask = MZ_Current();
654 /* we could probably start a DPMI-only dosmod task here, but I doubt
655 anything other than real DOS apps want to call raw mode switch */
656 ERR("attempting raw mode switch without DOS task!\n");
659 /* initialize real-mode context as per spec */
660 memset(&rm_ctx, 0, sizeof(rm_ctx));
661 DS_reg(&rm_ctx) = AX_sig(context);
662 ES_reg(&rm_ctx) = CX_sig(context);
663 SS_reg(&rm_ctx) = DX_sig(context);
664 ESP_reg(&rm_ctx) = EBX_sig(context);
665 CS_reg(&rm_ctx) = SI_sig(context);
666 EIP_reg(&rm_ctx) = EDI_sig(context);
667 EBP_reg(&rm_ctx) = EBP_sig(context);
670 EFL_reg(&rm_ctx) = EFL_sig(context); /* at least we need the IF flag */
672 /* enter real mode again */
673 TRACE("re-entering real mode at %04lx:%04lx\n",
674 CS_reg(&rm_ctx),EIP_reg(&rm_ctx));
675 ret = DOSVM_Enter( &rm_ctx );
676 /* when the real-mode stuff call its mode switch address,
677 DOSVM_Enter will return and we will continue here */
680 /* if the sync was lost, there's no way to recover */
684 /* alter protected-mode context as per spec */
685 DS_sig(context) = AX_reg(&rm_ctx);
686 ES_sig(context) = CX_reg(&rm_ctx);
687 SS_sig(context) = DX_reg(&rm_ctx);
688 ESP_sig(context) = EBX_reg(&rm_ctx);
689 CS_sig(context) = SI_reg(&rm_ctx);
690 EIP_sig(context) = EDI_reg(&rm_ctx);
691 EBP_sig(context) = EBP_reg(&rm_ctx);
695 /* Return to new address and hope that we didn't mess up */
696 TRACE("re-entering protected mode at %04x:%08lx\n",
697 CS_sig(context), EIP_sig(context));
701 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
703 ERR("don't even think about DPMI raw mode switch without DOS support!\n");
708 #define DOS_APP_ISDOS(addr,base) ((addr) < 0x110000)
709 #define DOS_WINE_ISDOS(addr,base) (((addr) >= (base)) && ((addr) < (base) + 0x110000))
710 #define DOS_UC_APPTOWINE(addr,base) ((addr) + (base))
711 #define DOS_UC_WINETOAPP(addr,base) ((addr) - (base))
712 #define DOS_APPTOWINE(addr,base) (DOS_APP_ISDOS(addr,base) ? DOS_UC_APPTOWINE(addr,base) : (addr))
713 #define DOS_WINETOAPP(addr,base) (DOS_WINE_ISDOS(addr,base) ? DOS_UC_WINETOAPP(addr,base) : (addr))
714 #define DOS_BADLIMIT(addr,base,limit) \
715 ((limit == 0xffffffff) || /* disallow "fat DS" for now */ \
716 (DOS_WINE_ISDOS(addr,base) && \
717 ((addr) + (limit) > (base) + 0x110000)))
719 /**********************************************************************
722 * Handler for int 31h (DPMI).
725 void WINAPI INT_Int31Handler( CONTEXT86 *context )
728 * Note: For Win32s processes, the whole linear address space is
729 * shifted by 0x10000 relative to the OS linear address space.
730 * See the comment in msdos/vxd.c.
732 DWORD offset = W32S_APPLICATION() ? W32S_OFFSET : 0;
737 LPDOSTASK lpDosTask = MZ_Current();
740 if (ISV86(context) && lpDosTask) {
741 /* Called from real mode, check if it's our wrapper */
742 TRACE("called from real mode\n");
743 if (CS_reg(context)==lpDosTask->dpmi_seg) {
744 /* This is the protected mode switch */
745 StartPM(context,lpDosTask);
748 if (CS_reg(context)==lpDosTask->xms_seg) {
749 /* This is the XMS driver entry point */
750 XMS_Handler(context);
755 RMCB *CurrRMCB = FirstRMCB;
757 while (CurrRMCB && (HIWORD(CurrRMCB->address) != CS_reg(context)))
758 CurrRMCB = CurrRMCB->next;
761 /* RMCB call, propagate to protected-mode handler */
762 DPMI_CallRMCBProc(context, CurrRMCB, lpDosTask->dpmi_flag);
769 RESET_CFLAG(context);
770 switch(AX_reg(context))
772 case 0x0000: /* Allocate LDT descriptors */
773 TRACE("allocate LDT descriptors (%d)\n",CX_reg(context));
774 if (!(AX_reg(context) = AllocSelectorArray16( CX_reg(context) )))
777 AX_reg(context) = 0x8011; /* descriptor unavailable */
780 TRACE("success, array starts at 0x%04x\n",AX_reg(context));
783 case 0x0001: /* Free LDT descriptor */
784 TRACE("free LDT descriptor (0x%04x)\n",BX_reg(context));
785 if (FreeSelector16( BX_reg(context) ))
787 AX_reg(context) = 0x8022; /* invalid selector */
792 /* If a segment register contains the selector being freed, */
793 /* set it to zero. */
794 if (!((DS_reg(context)^BX_reg(context)) & ~3)) DS_reg(context) = 0;
795 if (!((ES_reg(context)^BX_reg(context)) & ~3)) ES_reg(context) = 0;
796 if (!((FS_reg(context)^BX_reg(context)) & ~3)) FS_reg(context) = 0;
797 if (!((GS_reg(context)^BX_reg(context)) & ~3)) GS_reg(context) = 0;
801 case 0x0002: /* Real mode segment to descriptor */
802 TRACE("real mode segment to descriptor (0x%04x)\n",BX_reg(context));
804 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
805 switch(BX_reg(context))
807 case 0x0000: entryPoint = 183; break; /* __0000H */
808 case 0x0040: entryPoint = 193; break; /* __0040H */
809 case 0xa000: entryPoint = 174; break; /* __A000H */
810 case 0xb000: entryPoint = 181; break; /* __B000H */
811 case 0xb800: entryPoint = 182; break; /* __B800H */
812 case 0xc000: entryPoint = 195; break; /* __C000H */
813 case 0xd000: entryPoint = 179; break; /* __D000H */
814 case 0xe000: entryPoint = 190; break; /* __E000H */
815 case 0xf000: entryPoint = 194; break; /* __F000H */
817 AX_reg(context) = DOSMEM_AllocSelector(BX_reg(context));
821 AX_reg(context) = LOWORD(NE_GetEntryPoint(
822 GetModuleHandle16( "KERNEL" ),
827 case 0x0003: /* Get next selector increment */
828 TRACE("get selector increment (__AHINCR)\n");
829 AX_reg(context) = __AHINCR;
832 case 0x0004: /* Lock selector (not supported) */
833 FIXME("lock selector not supported\n");
834 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
837 case 0x0005: /* Unlock selector (not supported) */
838 FIXME("unlock selector not supported\n");
839 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
842 case 0x0006: /* Get selector base address */
843 TRACE("get selector base address (0x%04x)\n",BX_reg(context));
844 if (!(dw = GetSelectorBase( BX_reg(context) )))
846 AX_reg(context) = 0x8022; /* invalid selector */
853 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
854 dw = DOS_WINETOAPP(dw, base);
857 CX_reg(context) = HIWORD(W32S_WINE2APP(dw, offset));
858 DX_reg(context) = LOWORD(W32S_WINE2APP(dw, offset));
862 case 0x0007: /* Set selector base address */
863 TRACE("set selector base address (0x%04x,0x%08lx)\n",
865 W32S_APP2WINE(MAKELONG(DX_reg(context),CX_reg(context)), offset));
866 dw = W32S_APP2WINE(MAKELONG(DX_reg(context), CX_reg(context)), offset);
869 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
870 dw = DOS_APPTOWINE(dw, base);
873 SetSelectorBase(BX_reg(context), dw);
876 case 0x0008: /* Set selector limit */
877 TRACE("set selector limit (0x%04x,0x%08lx)\n",BX_reg(context),MAKELONG(DX_reg(context),CX_reg(context)));
878 dw = MAKELONG( DX_reg(context), CX_reg(context) );
881 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
882 DWORD sbase = GetSelectorBase( BX_reg(context) );
884 /* the app has set the limit without setting the base,
885 * it must be relying on that the default should be DOS space;
886 * so set the base address now */
887 SetSelectorBase( BX_reg(context), sbase = base );
888 if (dw == 0xffffffff) {
889 /* djgpp does this without checking (in _dos_ds setup, crt1.c),
890 * so we have to override the limit here */
894 if (DOS_BADLIMIT(sbase, base, dw)) {
895 AX_reg(context) = 0x8021; /* invalid value */
901 SetSelectorLimit16( BX_reg(context), dw );
904 case 0x0009: /* Set selector access rights */
905 TRACE("set selector access rights(0x%04x,0x%04x)\n",BX_reg(context),CX_reg(context));
906 SelectorAccessRights16( BX_reg(context), 1, CX_reg(context) );
909 case 0x000a: /* Allocate selector alias */
910 TRACE("allocate selector alias (0x%04x)\n",BX_reg(context));
911 if (!(AX_reg(context) = AllocCStoDSAlias16( BX_reg(context) )))
913 AX_reg(context) = 0x8011; /* descriptor unavailable */
918 case 0x000b: /* Get descriptor */
919 TRACE("get descriptor (0x%04x)\n",BX_reg(context));
922 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
925 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
926 entry.base = DOS_WINETOAPP(entry.base, base);
929 entry.base = W32S_WINE2APP(entry.base, offset);
931 /* FIXME: should use ES:EDI for 32-bit clients */
932 LDT_EntryToBytes( PTR_SEG_OFF_TO_LIN( ES_reg(context),
933 DI_reg(context) ), &entry );
937 case 0x000c: /* Set descriptor */
938 TRACE("set descriptor (0x%04x)\n",BX_reg(context));
941 LDT_BytesToEntry( PTR_SEG_OFF_TO_LIN( ES_reg(context),
942 DI_reg(context) ), &entry );
943 entry.base = W32S_APP2WINE(entry.base, offset);
946 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
947 entry.base = DOS_APPTOWINE(entry.base, base);
948 if (DOS_BADLIMIT(entry.base, base, entry.limit)) {
949 AX_reg(context) = 0x8021; /* invalid value */
956 LDT_SetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
960 case 0x000d: /* Allocate specific LDT descriptor */
961 FIXME("allocate descriptor (0x%04x), stub!\n",BX_reg(context));
962 AX_reg(context) = 0x8011; /* descriptor unavailable */
965 case 0x0100: /* Allocate DOS memory block */
966 TRACE("allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context));
967 dw = GlobalDOSAlloc16((DWORD)BX_reg(context)<<4);
969 AX_reg(context) = HIWORD(dw);
970 DX_reg(context) = LOWORD(dw);
972 AX_reg(context) = 0x0008; /* insufficient memory */
973 BX_reg(context) = DOSMEM_Available(0)>>4;
977 case 0x0101: /* Free DOS memory block */
978 TRACE("free DOS memory block (0x%04x)\n",DX_reg(context));
979 dw = GlobalDOSFree16(DX_reg(context));
981 AX_reg(context) = 0x0009; /* memory block address invalid */
985 case 0x0200: /* get real mode interrupt vector */
986 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
990 case 0x0201: /* set real mode interrupt vector */
991 FIXME("set realmode interupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context),CX_reg(context),DX_reg(context));
994 case 0x0204: /* Get protected mode interrupt vector */
995 TRACE("get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context));
996 dw = (DWORD)INT_GetPMHandler( BL_reg(context) );
997 CX_reg(context) = HIWORD(dw);
998 DX_reg(context) = LOWORD(dw);
1001 case 0x0205: /* Set protected mode interrupt vector */
1002 TRACE("set protected mode interrupt handler (0x%02x,%p), stub!\n",
1003 BL_reg(context),PTR_SEG_OFF_TO_LIN(CX_reg(context),DX_reg(context)));
1004 INT_SetPMHandler( BL_reg(context),
1005 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( CX_reg(context),
1009 case 0x0300: /* Simulate real mode interrupt */
1010 CallRMInt( context );
1013 case 0x0301: /* Call real mode procedure with far return */
1014 CallRMProc( context, FALSE );
1017 case 0x0302: /* Call real mode procedure with interrupt return */
1018 CallRMProc( context, TRUE );
1021 case 0x0303: /* Allocate Real Mode Callback Address */
1022 AllocRMCB( context );
1025 case 0x0304: /* Free Real Mode Callback Address */
1026 FreeRMCB( context );
1029 case 0x0305: /* Get State Save/Restore Addresses */
1030 TRACE("get state save/restore addresses\n");
1031 /* we probably won't need this kind of state saving */
1032 AX_reg(context) = 0;
1033 /* real mode: just point to the lret */
1034 BX_reg(context) = DPMI_wrap_seg;
1035 ECX_reg(context) = 2;
1036 /* protected mode: don't have any handler yet... */
1037 FIXME("no protected-mode dummy state save/restore handler yet\n");
1038 SI_reg(context) = 0;
1039 EDI_reg(context) = 0;
1042 case 0x0306: /* Get Raw Mode Switch Addresses */
1043 TRACE("get raw mode switch addresses\n");
1045 /* real mode, point to standard DPMI return wrapper */
1046 BX_reg(context) = DPMI_wrap_seg;
1047 ECX_reg(context) = 0;
1048 /* protected mode, point to DPMI call wrapper */
1049 SI_reg(context) = lpDosTask->dpmi_sel;
1050 EDI_reg(context) = 8; /* offset of the INT 0x31 call */
1052 ERR("win app attempting to get raw mode switch!\n");
1053 AX_reg(context) = 0x8001; /* unsupported function */
1057 case 0x0400: /* Get DPMI version */
1058 TRACE("get DPMI version\n");
1063 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
1064 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
1065 CL_reg(context) = si.wProcessorLevel;
1066 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
1069 case 0x0500: /* Get free memory information */
1070 TRACE("get free memory information\n");
1074 mmi.dwSize = sizeof(mmi);
1076 ptr = (BYTE *)PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context));
1077 /* the layout is just the same as MEMMANINFO, but without
1080 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
1083 case 0x0501: /* Allocate memory block */
1084 TRACE("allocate memory block (%ld)\n",MAKELONG(CX_reg(context),BX_reg(context)));
1085 if (!(ptr = (BYTE *)DPMI_xalloc(MAKELONG(CX_reg(context), BX_reg(context)))))
1087 AX_reg(context) = 0x8012; /* linear memory not available */
1090 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1091 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1095 case 0x0502: /* Free memory block */
1096 TRACE("free memory block (0x%08lx)\n",
1097 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset));
1098 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),
1099 SI_reg(context)), offset) );
1102 case 0x0503: /* Resize memory block */
1103 TRACE("resize memory block (0x%08lx,%ld)\n",
1104 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
1105 MAKELONG(CX_reg(context),BX_reg(context)));
1106 if (!(ptr = (BYTE *)DPMI_xrealloc(
1107 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
1108 MAKELONG(CX_reg(context),BX_reg(context)))))
1110 AX_reg(context) = 0x8012; /* linear memory not available */
1113 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1114 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1118 case 0x0507: /* Modify page attributes */
1119 FIXME("modify page attributes unimplemented\n");
1120 break; /* Just ignore it */
1122 case 0x0600: /* Lock linear region */
1123 FIXME("lock linear region unimplemented\n");
1124 break; /* Just ignore it */
1126 case 0x0601: /* Unlock linear region */
1127 FIXME("unlock linear region unimplemented\n");
1128 break; /* Just ignore it */
1130 case 0x0602: /* Unlock real-mode region */
1131 FIXME("unlock realmode region unimplemented\n");
1132 break; /* Just ignore it */
1134 case 0x0603: /* Lock real-mode region */
1135 FIXME("lock realmode region unimplemented\n");
1136 break; /* Just ignore it */
1138 case 0x0604: /* Get page size */
1139 TRACE("get pagesize\n");
1140 BX_reg(context) = 0;
1141 CX_reg(context) = VIRTUAL_GetPageSize();
1144 case 0x0702: /* Mark page as demand-paging candidate */
1145 FIXME("mark page as demand-paging candidate\n");
1146 break; /* Just ignore it */
1148 case 0x0703: /* Discard page contents */
1149 FIXME("discard page contents\n");
1150 break; /* Just ignore it */
1152 case 0x0800: /* Physical address mapping */
1153 FIXME("map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context),BX_reg(context)));
1154 if(!(ptr=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context),BX_reg(context)))))
1156 AX_reg(context) = 0x8021;
1161 BX_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1162 CX_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1163 RESET_CFLAG(context);
1168 INT_BARF( context, 0x31 );
1169 AX_reg(context) = 0x8001; /* unsupported function */