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"
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(int31,"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(int31,"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(int31,"realloc of DPMI_xallocd region %p?\n",ptr);
125 if (mbi.State == MEM_FREE) {
126 FIXME(int31,"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, CONTEXT *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, CONTEXT *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( CONTEXT *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(int31,"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 CONTEXT 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(int31,"RMCBs only implemented for i386\n");
255 /**********************************************************************
258 * This routine does the hard work of calling a real mode procedure.
260 int DPMI_CallRMProc( CONTEXT *context, LPWORD stack, int args, int iret )
264 THDB *thdb = THREAD_Current();
267 #endif /* !MZ_SUPPORTED */
268 LPVOID addr = NULL; /* avoid gcc warning */
269 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
270 NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
272 int alloc = 0, already = 0;
275 GlobalUnlock16( GetCurrentTask() );
277 TRACE(int31, "EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
278 EAX_reg(context), EBX_reg(context), ECX_reg(context), EDX_reg(context) );
279 TRACE(int31, "ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
280 ESI_reg(context), EDI_reg(context), ES_reg(context), DS_reg(context),
281 CS_reg(context), IP_reg(context), args, iret?"IRET":"FAR" );
285 /* there might be some code that just jumps to RMCBs or the like,
286 in which case following the jumps here might get us to a shortcut */
287 code = CTX_SEG_OFF_TO_LIN(context, CS_reg(context), EIP_reg(context));
289 case 0xe9: /* JMP NEAR */
290 IP_reg(context) += 3 + *(WORD *)(code+1);
291 /* yeah, I know these gotos don't look good... */
292 goto callrmproc_again;
293 case 0xea: /* JMP FAR */
294 IP_reg(context) = *(WORD *)(code+1);
295 CS_reg(context) = *(WORD *)(code+3);
296 /* ...but since the label is there anyway... */
297 goto callrmproc_again;
298 case 0xeb: /* JMP SHORT */
299 IP_reg(context) += 2 + *(signed char *)(code+1);
300 /* ...because of other gotos below, so... */
301 goto callrmproc_again;
304 /* shortcut for chaining to internal interrupt handlers */
305 if ((CS_reg(context) == 0xF000) && iret) {
306 return INT_RealModeInterrupt( IP_reg(context)/4, context);
309 /* shortcut for RMCBs */
310 CurrRMCB = FirstRMCB;
312 while (CurrRMCB && (HIWORD(CurrRMCB->address) != CS_reg(context)))
313 CurrRMCB = CurrRMCB->next;
316 if (!(CurrRMCB || pModule->lpDosTask)) {
317 FIXME(int31,"DPMI real-mode call using DOS VM task system, not fully tested!\n");
318 TRACE(int31,"creating VM86 task\n");
319 if (!MZ_InitTask( MZ_AllocDPMITask( pModule->self ) )) {
320 ERR(int31,"could not setup VM86 task\n");
325 if (!SS_reg(context)) {
326 alloc = 1; /* allocate default stack */
327 stack16 = addr = DOSMEM_GetBlock( pModule->self, 64, (UINT16 *)&(SS_reg(context)) );
328 SP_reg(context) = 64-2;
331 ERR(int31,"could not allocate default stack\n");
335 stack16 = CTX_SEG_OFF_TO_LIN(context, SS_reg(context), ESP_reg(context));
337 SP_reg(context) -= (args + (iret?1:0)) * sizeof(WORD);
340 stack16 = THREAD_STACK16(thdb);
343 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
344 /* push flags if iret */
347 *stack16 = FL_reg(context);
350 /* push return address (return to interrupt wrapper) */
351 *(--stack16) = DPMI_wrap_seg;
354 SP_reg(context) -= 2*sizeof(WORD);
360 /* RMCB call, invoke protected-mode handler directly */
361 DPMI_CallRMCBProc(context, CurrRMCB, pModule->lpDosTask?pModule->lpDosTask->dpmi_flag:0);
362 /* check if we returned to where we thought we would */
363 if ((CS_reg(context) != DPMI_wrap_seg) ||
364 (IP_reg(context) != 0)) {
365 /* we need to continue at different address in real-mode space,
366 so we need to set it all up for real mode again */
367 goto callrmproc_again;
371 #if 0 /* this was probably unnecessary */
372 /* push call address */
373 *(--stack16) = CS_reg(context);
374 *(--stack16) = IP_reg(context);
376 SP_reg(context) -= 2*sizeof(WORD);
377 /* set initial CS:IP to the wrapper's "lret" */
378 CS_reg(context) = DPMI_wrap_seg;
381 TRACE(int31,"entering real mode...\n");
382 DOSVM_Enter( context );
383 TRACE(int31,"returned from real-mode call\n");
385 addr = CTX_SEG_OFF_TO_LIN(context, CS_reg(context), EIP_reg(context));
386 sel = SELECTOR_AllocBlock( addr, 0x10000, SEGMENT_CODE, FALSE, FALSE );
387 seg_addr = PTR_SEG_OFF_TO_SEGPTR( sel, 0 );
389 CS_reg(context) = HIWORD(seg_addr);
390 IP_reg(context) = LOWORD(seg_addr);
391 EBP_reg(context) = OFFSETOF( thdb->cur_stack )
392 + (WORD)&((STACK16FRAME*)0)->bp;
393 Callbacks->CallRegisterShortProc(context, args*sizeof(WORD));
394 SELECTOR_FreeBlock(sel, 1);
397 if (alloc) DOSMEM_FreeBlock( pModule->self, addr );
402 /**********************************************************************
405 static void CallRMInt( CONTEXT *context )
407 CONTEXT realmode_ctx;
408 FARPROC16 rm_int = INT_GetRMHandler( BL_reg(context) );
409 REALMODECALL *call = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context),
411 INT_GetRealModeContext( call, &realmode_ctx );
413 /* we need to check if a real-mode program has hooked the interrupt */
414 if (HIWORD(rm_int)!=0xF000) {
415 /* yup, which means we need to switch to real mode... */
416 CS_reg(&realmode_ctx) = HIWORD(rm_int);
417 EIP_reg(&realmode_ctx) = LOWORD(rm_int);
418 if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
421 RESET_CFLAG(context);
422 /* use the IP we have instead of BL_reg, in case some apps
423 decide to move interrupts around for whatever reason... */
424 if (INT_RealModeInterrupt( LOWORD(rm_int)/4, &realmode_ctx ))
426 if (EFL_reg(context)&1) {
427 FIXME(int31,"%02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
428 BL_reg(context), EAX_reg(&realmode_ctx), EBX_reg(&realmode_ctx),
429 ECX_reg(&realmode_ctx), EDX_reg(&realmode_ctx));
430 FIXME(int31," ESI=%08lx EDI=%08lx DS=%04lx ES=%04lx\n",
431 ESI_reg(&realmode_ctx), EDI_reg(&realmode_ctx),
432 DS_reg(&realmode_ctx), ES_reg(&realmode_ctx) );
435 INT_SetRealModeContext( call, &realmode_ctx );
439 static void CallRMProc( CONTEXT *context, int iret )
441 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
444 TRACE(int31, "RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
445 p->eax, p->ebx, p->ecx, p->edx);
446 TRACE(int31, " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
447 p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
449 if (!(p->cs) && !(p->ip)) { /* remove this check
450 if Int21/6501 case map function
451 has been implemented */
455 INT_GetRealModeContext(p, &context16);
456 DPMI_CallRMProc( &context16, ((LPWORD)PTR_SEG_OFF_TO_LIN(SS_reg(context), SP_reg(context)))+3,
457 CX_reg(context), iret );
458 INT_SetRealModeContext(p, &context16);
462 static void WINAPI WINE_UNUSED RMCallbackProc( RMCB *rmcb )
464 /* This routine should call DPMI_CallRMCBProc, but we don't have the
465 register structure available - this is easily fixed by going through
466 a Win16 register relay instead of calling RMCallbackProc "directly",
467 but I won't bother at this time. */
468 FIXME(int31,"not properly supported on your architecture!\n");
471 static RMCB *DPMI_AllocRMCB( void )
473 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
479 LPVOID RMCBmem = DOSMEM_GetBlock(0, 4, &uParagraph);
482 *p++ = 0xcd; /* RMCB: */
483 *p++ = 0x31; /* int $0x31 */
484 /* it is the called procedure's task to change the return CS:EIP
485 the DPMI 0.9 spec states that if it doesn't, it will be called again */
487 *p++ = 0xfc; /* jmp RMCB */
489 LPVOID RMCBmem = DOSMEM_GetBlock(0, 15, &uParagraph);
492 *p++ = 0x68; /* pushl */
493 *(LPVOID *)p = NewRMCB;
495 *p++ = 0x9a; /* lcall */
496 *(FARPROC16 *)p = (FARPROC16)RMCallbackProc; /* FIXME: register relay */
500 *p++=0xc3; /* lret (FIXME?) */
502 NewRMCB->address = MAKELONG(0, uParagraph);
503 NewRMCB->next = FirstRMCB;
510 static void AllocRMCB( CONTEXT *context )
512 RMCB *NewRMCB = DPMI_AllocRMCB();
514 TRACE(int31, "Function to call: %04x:%04x\n", (WORD)DS_reg(context), SI_reg(context) );
518 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
519 NewRMCB->proc_ofs = SI_reg(context);
520 NewRMCB->proc_sel = DS_reg(context);
521 NewRMCB->regs_ofs = DI_reg(context);
522 NewRMCB->regs_sel = ES_reg(context);
523 CX_reg(context) = HIWORD(NewRMCB->address);
524 DX_reg(context) = LOWORD(NewRMCB->address);
528 AX_reg(context) = 0x8015; /* callback unavailable */
534 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
536 RMCB *NewRMCB = DPMI_AllocRMCB();
539 NewRMCB->proc_ofs = (DWORD)proc;
540 NewRMCB->proc_sel = 0;
541 NewRMCB->regs_ofs = 0;
542 NewRMCB->regs_sel = 0;
543 return (FARPROC16)(NewRMCB->address);
549 static int DPMI_FreeRMCB( DWORD address )
551 RMCB *CurrRMCB = FirstRMCB;
552 RMCB *PrevRMCB = NULL;
554 while (CurrRMCB && (CurrRMCB->address != address))
557 CurrRMCB = CurrRMCB->next;
562 PrevRMCB->next = CurrRMCB->next;
564 FirstRMCB = CurrRMCB->next;
565 DOSMEM_FreeBlock(0, DOSMEM_MapRealToLinear(CurrRMCB->address));
566 HeapFree(GetProcessHeap(), 0, CurrRMCB);
573 static void FreeRMCB( CONTEXT *context )
575 FIXME(int31, "callback address: %04x:%04x\n",
576 CX_reg(context), DX_reg(context));
578 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
579 AX_reg(context) = 0x8024; /* invalid callback address */
585 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
587 DPMI_FreeRMCB( (DWORD)proc );
592 /* (see loader/dos/module.c, function MZ_InitDPMI) */
594 static void StartPM( CONTEXT *context, LPDOSTASK lpDosTask )
596 char *base = DOSMEM_MemoryBase(0);
597 UINT16 cs, ss, ds, es;
599 DWORD psp_ofs = (DWORD)(lpDosTask->psp_seg<<4);
600 PDB16 *psp = (PDB16 *)(base + psp_ofs);
601 HANDLE16 env_seg = psp->environment;
604 RESET_CFLAG(context);
605 lpDosTask->dpmi_flag = AX_reg(context);
606 is32 = lpDosTask->dpmi_flag & 1;
607 /* our mode switch wrapper have placed the desired CS into DX */
608 cs = SELECTOR_AllocBlock( base + (DWORD)(DX_reg(context)<<4), 0x10000, SEGMENT_CODE, FALSE, FALSE );
609 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
610 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
611 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
612 32-bit code using this stack. */
613 ss = SELECTOR_AllocBlock( base + (DWORD)(SS_reg(context)<<4), 0x10000, SEGMENT_DATA, is32, FALSE );
614 /* do the same for the data segments, just in case */
615 if (DS_reg(context) == SS_reg(context)) ds = ss;
616 else ds = SELECTOR_AllocBlock( base + (DWORD)(DS_reg(context)<<4), 0x10000, SEGMENT_DATA, is32, FALSE );
617 es = SELECTOR_AllocBlock( base + psp_ofs, 0x100, SEGMENT_DATA, is32, FALSE );
618 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
619 psp->environment = SELECTOR_AllocBlock( base + (DWORD)(env_seg<<4),
620 0x10000, SEGMENT_DATA, FALSE, FALSE );
623 CS_reg(&pm_ctx) = lpDosTask->dpmi_sel;
624 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
625 AX_reg(&pm_ctx) = ss;
626 DX_reg(&pm_ctx) = cs;
627 DS_reg(&pm_ctx) = ds;
628 ES_reg(&pm_ctx) = es;
632 TRACE(int31,"DOS program is now entering protected mode\n");
633 Callbacks->CallRegisterShortProc(&pm_ctx, 0);
635 /* in the current state of affairs, we won't ever actually return here... */
636 /* we should have int21/ah=4c do it someday, though... */
638 SELECTOR_FreeBlock(psp->environment, 1);
639 psp->environment = env_seg;
640 SELECTOR_FreeBlock(es, 1);
641 if (ds != ss) SELECTOR_FreeBlock(ds, 1);
642 SELECTOR_FreeBlock(ss, 1);
643 SELECTOR_FreeBlock(cs, 1);
646 /* DPMI Raw Mode Switch handler */
648 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
650 LPDOSTASK lpDosTask = MZ_Current();
655 /* we could probably start a DPMI-only dosmod task here, but I doubt
656 anything other than real DOS apps want to call raw mode switch */
657 ERR(int31,"attempting raw mode switch without DOS task!\n");
660 /* initialize real-mode context as per spec */
661 memset(&rm_ctx, 0, sizeof(rm_ctx));
662 DS_reg(&rm_ctx) = AX_sig(context);
663 ES_reg(&rm_ctx) = CX_sig(context);
664 SS_reg(&rm_ctx) = DX_sig(context);
665 ESP_reg(&rm_ctx) = EBX_sig(context);
666 CS_reg(&rm_ctx) = SI_sig(context);
667 EIP_reg(&rm_ctx) = EDI_sig(context);
668 EBP_reg(&rm_ctx) = EBP_sig(context);
671 EFL_reg(&rm_ctx) = EFL_sig(context); /* at least we need the IF flag */
673 /* enter real mode again */
674 TRACE(int31,"re-entering real mode at %04lx:%04lx\n",
675 CS_reg(&rm_ctx),EIP_reg(&rm_ctx));
676 ret = DOSVM_Enter( &rm_ctx );
677 /* when the real-mode stuff call its mode switch address,
678 DOSVM_Enter will return and we will continue here */
681 /* if the sync was lost, there's no way to recover */
685 /* alter protected-mode context as per spec */
686 DS_sig(context) = AX_reg(&rm_ctx);
687 ES_sig(context) = CX_reg(&rm_ctx);
688 SS_sig(context) = DX_reg(&rm_ctx);
689 ESP_sig(context) = EBX_reg(&rm_ctx);
690 CS_sig(context) = SI_reg(&rm_ctx);
691 EIP_sig(context) = EDI_reg(&rm_ctx);
692 EBP_sig(context) = EBP_reg(&rm_ctx);
696 /* Return to new address and hope that we didn't mess up */
697 TRACE(int31,"re-entering protected mode at %04x:%08lx\n",
698 CS_sig(context), EIP_sig(context));
702 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
704 ERR(int31,"don't even think about DPMI raw mode switch without DOS support!\n");
709 #define DOS_APP_ISDOS(addr,base) ((addr) < 0x110000)
710 #define DOS_WINE_ISDOS(addr,base) (((addr) >= (base)) && ((addr) < (base) + 0x110000))
711 #define DOS_UC_APPTOWINE(addr,base) ((addr) + (base))
712 #define DOS_UC_WINETOAPP(addr,base) ((addr) - (base))
713 #define DOS_APPTOWINE(addr,base) (DOS_APP_ISDOS(addr,base) ? DOS_UC_APPTOWINE(addr,base) : (addr))
714 #define DOS_WINETOAPP(addr,base) (DOS_WINE_ISDOS(addr,base) ? DOS_UC_WINETOAPP(addr,base) : (addr))
715 #define DOS_BADLIMIT(addr,base,limit) \
716 ((limit == 0xffffffff) || /* disallow "fat DS" for now */ \
717 (DOS_WINE_ISDOS(addr,base) && \
718 ((addr) + (limit) > (base) + 0x110000)))
720 /**********************************************************************
723 * Handler for int 31h (DPMI).
726 void WINAPI INT_Int31Handler( CONTEXT *context )
729 * Note: For Win32s processes, the whole linear address space is
730 * shifted by 0x10000 relative to the OS linear address space.
731 * See the comment in msdos/vxd.c.
733 DWORD offset = W32S_APPLICATION() ? W32S_OFFSET : 0;
738 LPDOSTASK lpDosTask = MZ_Current();
741 if (ISV86(context) && lpDosTask) {
742 /* Called from real mode, check if it's our wrapper */
743 TRACE(int31,"called from real mode\n");
744 if (CS_reg(context)==lpDosTask->dpmi_seg) {
745 /* This is the protected mode switch */
746 StartPM(context,lpDosTask);
749 if (CS_reg(context)==lpDosTask->xms_seg) {
750 /* This is the XMS driver entry point */
751 XMS_Handler(context);
756 RMCB *CurrRMCB = FirstRMCB;
758 while (CurrRMCB && (HIWORD(CurrRMCB->address) != CS_reg(context)))
759 CurrRMCB = CurrRMCB->next;
762 /* RMCB call, propagate to protected-mode handler */
763 DPMI_CallRMCBProc(context, CurrRMCB, lpDosTask->dpmi_flag);
770 RESET_CFLAG(context);
771 switch(AX_reg(context))
773 case 0x0000: /* Allocate LDT descriptors */
774 TRACE(int31,"allocate LDT descriptors (%d)\n",CX_reg(context));
775 if (!(AX_reg(context) = AllocSelectorArray16( CX_reg(context) )))
777 TRACE(int31,"failed\n");
778 AX_reg(context) = 0x8011; /* descriptor unavailable */
781 TRACE(int31,"success, array starts at 0x%04x\n",AX_reg(context));
784 case 0x0001: /* Free LDT descriptor */
785 TRACE(int31,"free LDT descriptor (0x%04x)\n",BX_reg(context));
786 if (FreeSelector16( BX_reg(context) ))
788 AX_reg(context) = 0x8022; /* invalid selector */
793 /* If a segment register contains the selector being freed, */
794 /* set it to zero. */
795 if (!((DS_reg(context)^BX_reg(context)) & ~3)) DS_reg(context) = 0;
796 if (!((ES_reg(context)^BX_reg(context)) & ~3)) ES_reg(context) = 0;
797 if (!((FS_reg(context)^BX_reg(context)) & ~3)) FS_reg(context) = 0;
798 if (!((GS_reg(context)^BX_reg(context)) & ~3)) GS_reg(context) = 0;
802 case 0x0002: /* Real mode segment to descriptor */
803 TRACE(int31,"real mode segment to descriptor (0x%04x)\n",BX_reg(context));
805 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
806 switch(BX_reg(context))
808 case 0x0000: entryPoint = 183; break; /* __0000H */
809 case 0x0040: entryPoint = 193; break; /* __0040H */
810 case 0xa000: entryPoint = 174; break; /* __A000H */
811 case 0xb000: entryPoint = 181; break; /* __B000H */
812 case 0xb800: entryPoint = 182; break; /* __B800H */
813 case 0xc000: entryPoint = 195; break; /* __C000H */
814 case 0xd000: entryPoint = 179; break; /* __D000H */
815 case 0xe000: entryPoint = 190; break; /* __E000H */
816 case 0xf000: entryPoint = 194; break; /* __F000H */
818 AX_reg(context) = DOSMEM_AllocSelector(BX_reg(context));
822 AX_reg(context) = LOWORD(NE_GetEntryPoint(
823 GetModuleHandle16( "KERNEL" ),
828 case 0x0003: /* Get next selector increment */
829 TRACE(int31,"get selector increment (__AHINCR)\n");
830 AX_reg(context) = __AHINCR;
833 case 0x0004: /* Lock selector (not supported) */
834 FIXME(int31,"lock selector not supported\n");
835 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
838 case 0x0005: /* Unlock selector (not supported) */
839 FIXME(int31,"unlock selector not supported\n");
840 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
843 case 0x0006: /* Get selector base address */
844 TRACE(int31,"get selector base address (0x%04x)\n",BX_reg(context));
845 if (!(dw = GetSelectorBase( BX_reg(context) )))
847 AX_reg(context) = 0x8022; /* invalid selector */
854 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
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(int31, "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(lpDosTask->hModule);
871 dw = DOS_APPTOWINE(dw, base);
874 SetSelectorBase(BX_reg(context), dw);
877 case 0x0008: /* Set selector limit */
878 TRACE(int31,"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(lpDosTask->hModule);
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(int31,"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(int31,"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(int31,"get descriptor (0x%04x)\n",BX_reg(context));
923 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
926 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
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(int31,"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(lpDosTask->hModule);
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(int31,"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(int31,"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(0)>>4;
978 case 0x0101: /* Free DOS memory block */
979 TRACE(int31,"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(int31,"get realmode interupt vector(0x%02x) unimplemented.\n",
991 case 0x0201: /* set real mode interrupt vector */
992 FIXME(int31, "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(int31,"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(int31,"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(int31,"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(int31,"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(int31,"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(int31,"win app attempting to get raw mode switch!\n");
1054 AX_reg(context) = 0x8001; /* unsupported function */
1058 case 0x0400: /* Get DPMI version */
1059 TRACE(int31,"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(int31,"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(int31,"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(int31, "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(int31, "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(int31,"modify page attributes unimplemented\n");
1121 break; /* Just ignore it */
1123 case 0x0600: /* Lock linear region */
1124 FIXME(int31,"lock linear region unimplemented\n");
1125 break; /* Just ignore it */
1127 case 0x0601: /* Unlock linear region */
1128 FIXME(int31,"unlock linear region unimplemented\n");
1129 break; /* Just ignore it */
1131 case 0x0602: /* Unlock real-mode region */
1132 FIXME(int31,"unlock realmode region unimplemented\n");
1133 break; /* Just ignore it */
1135 case 0x0603: /* Lock real-mode region */
1136 FIXME(int31,"lock realmode region unimplemented\n");
1137 break; /* Just ignore it */
1139 case 0x0604: /* Get page size */
1140 TRACE(int31,"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(int31,"mark page as demand-paging candidate\n");
1147 break; /* Just ignore it */
1149 case 0x0703: /* Discard page contents */
1150 FIXME(int31,"discard page contents\n");
1151 break; /* Just ignore it */
1153 case 0x0800: /* Physical address mapping */
1154 FIXME(int31,"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 */