4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
25 #include "wine/winbase16.h"
31 #include "wine/debug.h"
32 #include "stackframe.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(int31);
37 /* Structure for real-mode callbacks */
59 typedef struct tagRMCB {
61 DWORD proc_ofs,proc_sel;
62 DWORD regs_ofs,regs_sel;
66 static RMCB *FirstRMCB = NULL;
67 static WORD dpmi_flag;
68 static void* lastvalloced = NULL;
70 /**********************************************************************
73 * Return TRUE if we are in 32-bit protected mode DOS process.
75 BOOL DOSVM_IsDos32(void)
77 return (dpmi_flag & 1) ? TRUE : FALSE;
81 /**********************************************************************
82 * INT_GetRealModeContext
84 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
86 context->Eax = call->eax;
87 context->Ebx = call->ebx;
88 context->Ecx = call->ecx;
89 context->Edx = call->edx;
90 context->Esi = call->esi;
91 context->Edi = call->edi;
92 context->Ebp = call->ebp;
93 context->EFlags = call->fl | V86_FLAG;
94 context->Eip = call->ip;
95 context->Esp = call->sp;
96 context->SegCs = call->cs;
97 context->SegDs = call->ds;
98 context->SegEs = call->es;
99 context->SegFs = call->fs;
100 context->SegGs = call->gs;
101 context->SegSs = call->ss;
105 /**********************************************************************
106 * INT_SetRealModeContext
108 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
110 call->eax = context->Eax;
111 call->ebx = context->Ebx;
112 call->ecx = context->Ecx;
113 call->edx = context->Edx;
114 call->esi = context->Esi;
115 call->edi = context->Edi;
116 call->ebp = context->Ebp;
117 call->fl = LOWORD(context->EFlags);
118 call->ip = LOWORD(context->Eip);
119 call->sp = LOWORD(context->Esp);
120 call->cs = context->SegCs;
121 call->ds = context->SegDs;
122 call->es = context->SegEs;
123 call->fs = context->SegFs;
124 call->gs = context->SegGs;
125 call->ss = context->SegSs;
128 /**********************************************************************
130 * special virtualalloc, allocates lineary monoton growing memory.
131 * (the usual VirtualAlloc does not satisfy that restriction)
133 static LPVOID DPMI_xalloc( DWORD len )
136 LPVOID oldlastv = lastvalloced;
145 ret = VirtualAlloc( lastvalloced, len,
146 MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE );
148 lastvalloced = (char *) lastvalloced + 0x10000;
150 /* we failed to allocate one in the first round.
153 if (!xflag && (lastvalloced<oldlastv))
156 FIXME( "failed to allocate linearly growing memory (%ld bytes), "
157 "using non-linear growing...\n", len );
161 /* if we even fail to allocate something in the next
164 if ((xflag==1) && (lastvalloced >= oldlastv))
167 if ((xflag==2) && (lastvalloced < oldlastv)) {
168 FIXME( "failed to allocate any memory of %ld bytes!\n", len );
175 ret = VirtualAlloc( NULL, len,
176 MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE );
179 lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
183 /**********************************************************************
186 static void DPMI_xfree( LPVOID ptr )
188 VirtualFree( ptr, 0, MEM_RELEASE );
191 /**********************************************************************
194 * FIXME: perhaps we could grow this mapped area...
196 static LPVOID DPMI_xrealloc( LPVOID ptr, DWORD newsize )
198 MEMORY_BASIC_INFORMATION mbi;
201 newptr = DPMI_xalloc( newsize );
204 if (!VirtualQuery(ptr,&mbi,sizeof(mbi)))
206 FIXME( "realloc of DPMI_xallocd region %p?\n", ptr );
210 if (mbi.State == MEM_FREE)
212 FIXME( "realloc of DPMI_xallocd region %p?\n", ptr );
216 /* We do not shrink allocated memory. most reallocs
217 * only do grows anyway
219 if (newsize <= mbi.RegionSize)
222 memcpy( newptr, ptr, mbi.RegionSize );
232 void DPMI_CallRMCB32(RMCB *rmcb, UINT16 ss, DWORD esp, UINT16*es, DWORD*edi)
233 #if 0 /* original code, which early gccs puke on */
236 __asm__ __volatile__(
244 ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
250 : "=d" (*es), "=D" (*edi), "=S" (_clobber), "=a" (_clobber), "=c" (_clobber)
251 : "0" (ss), "2" (esp),
252 "4" (rmcb->regs_sel), "1" (rmcb->regs_ofs),
253 "3" (&rmcb->proc_ofs) );
255 #else /* code generated by a gcc new enough */
257 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32,
262 "movl 0x8(%ebp),%eax\n\t"
263 "movl 0x10(%ebp),%esi\n\t"
264 "movl 0xc(%ebp),%edx\n\t"
265 "movl 0x10(%eax),%ecx\n\t"
266 "movl 0xc(%eax),%edi\n\t"
275 ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
281 "movl 0x14(%ebp),%eax\n\t"
282 "movw %dx,(%eax)\n\t"
283 "movl 0x18(%ebp),%edx\n\t"
284 "movl %edi,(%edx)\n\t"
291 #endif /* __i386__ */
293 /**********************************************************************
296 * This routine does the hard work of calling a callback procedure.
298 static void DPMI_CallRMCBProc( CONTEXT86 *context, RMCB *rmcb, WORD flag )
300 if (IS_SELECTOR_SYSTEM( rmcb->proc_sel )) {
301 /* Wine-internal RMCB, call directly */
302 ((RMCBPROC)rmcb->proc_ofs)(context);
308 INT_SetRealModeContext(MapSL(MAKESEGPTR( rmcb->regs_sel, rmcb->regs_ofs )), context);
309 ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, WINE_LDT_FLAGS_DATA );
312 FIXME("untested!\n");
314 /* The called proc ends with an IRET, and takes these parameters:
315 * DS:ESI = pointer to real-mode SS:SP
316 * ES:EDI = pointer to real-mode call structure
318 * ES:EDI = pointer to real-mode call structure (may be a copy)
319 * It is the proc's responsibility to change the return CS:IP in the
320 * real-mode call structure. */
322 /* 32-bit DPMI client */
323 DPMI_CallRMCB32(rmcb, ss, esp, &es, &edi);
325 /* 16-bit DPMI client */
326 CONTEXT86 ctx = *context;
327 ctx.SegCs = rmcb->proc_sel;
328 ctx.Eip = rmcb->proc_ofs;
331 ctx.SegEs = rmcb->regs_sel;
332 ctx.Edi = rmcb->regs_ofs;
333 /* FIXME: I'm pretty sure this isn't right - should push flags first */
334 wine_call_to_16_regs_short(&ctx, 0);
339 INT_GetRealModeContext( MapSL( MAKESEGPTR( es, edi )), context);
341 ERR("RMCBs only implemented for i386\n");
347 /**********************************************************************
350 * This routine does the hard work of calling a real mode procedure.
352 int DPMI_CallRMProc( CONTEXT86 *context, LPWORD stack, int args, int iret )
355 LPVOID addr = NULL; /* avoid gcc warning */
357 int alloc = 0, already = 0;
360 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
361 context->Eax, context->Ebx, context->Ecx, context->Edx );
362 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
363 context->Esi, context->Edi, context->SegEs, context->SegDs,
364 context->SegCs, LOWORD(context->Eip), args, iret?"IRET":"FAR" );
368 /* there might be some code that just jumps to RMCBs or the like,
369 in which case following the jumps here might get us to a shortcut */
370 code = CTX_SEG_OFF_TO_LIN(context, context->SegCs, context->Eip);
372 case 0xe9: /* JMP NEAR */
373 context->Eip += 3 + *(WORD *)(code+1);
374 /* yeah, I know these gotos don't look good... */
375 goto callrmproc_again;
376 case 0xea: /* JMP FAR */
377 context->Eip = *(WORD *)(code+1);
378 context->SegCs = *(WORD *)(code+3);
379 /* ...but since the label is there anyway... */
380 goto callrmproc_again;
381 case 0xeb: /* JMP SHORT */
382 context->Eip += 2 + *(signed char *)(code+1);
383 /* ...because of other gotos below, so... */
384 goto callrmproc_again;
387 /* shortcut for chaining to internal interrupt handlers */
388 if ((context->SegCs == 0xF000) && iret)
390 DOSVM_CallBuiltinHandler( context, LOWORD(context->Eip)/4 );
394 /* shortcut for RMCBs */
395 CurrRMCB = FirstRMCB;
397 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
398 CurrRMCB = CurrRMCB->next;
400 if (!CurrRMCB && !MZ_Current())
402 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
403 TRACE("creating VM86 task\n");
407 if (!context->SegSs) {
408 alloc = 1; /* allocate default stack */
409 stack16 = addr = DOSMEM_GetBlock( 64, (UINT16 *)&(context->SegSs) );
413 ERR("could not allocate default stack\n");
417 stack16 = CTX_SEG_OFF_TO_LIN(context, context->SegSs, context->Esp);
419 context->Esp -= (args + (iret?1:0)) * sizeof(WORD);
421 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
422 /* push flags if iret */
425 *stack16 = LOWORD(context->EFlags);
427 /* push return address (return to interrupt wrapper) */
428 *(--stack16) = DOSVM_dpmi_segments->wrap_seg;
431 context->Esp -= 2*sizeof(WORD);
436 /* RMCB call, invoke protected-mode handler directly */
437 DPMI_CallRMCBProc(context, CurrRMCB, dpmi_flag);
438 /* check if we returned to where we thought we would */
439 if ((context->SegCs != DOSVM_dpmi_segments->wrap_seg) ||
440 (LOWORD(context->Eip) != 0)) {
441 /* we need to continue at different address in real-mode space,
442 so we need to set it all up for real mode again */
443 goto callrmproc_again;
446 TRACE("entering real mode...\n");
447 DOSVM_Enter( context );
448 TRACE("returned from real-mode call\n");
450 if (alloc) DOSMEM_FreeBlock( addr );
455 /**********************************************************************
456 * CallRMInt (WINEDOS.@)
458 void WINAPI DOSVM_CallRMInt( CONTEXT86 *context )
460 CONTEXT86 realmode_ctx;
461 FARPROC16 rm_int = DOSVM_GetRMHandler( BL_reg(context) );
462 REALMODECALL *call = CTX_SEG_OFF_TO_LIN( context,
465 INT_GetRealModeContext( call, &realmode_ctx );
467 /* we need to check if a real-mode program has hooked the interrupt */
468 if (HIWORD(rm_int)!=0xF000) {
469 /* yup, which means we need to switch to real mode... */
470 realmode_ctx.SegCs = HIWORD(rm_int);
471 realmode_ctx.Eip = LOWORD(rm_int);
472 if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
475 RESET_CFLAG(context);
476 /* use the IP we have instead of BL_reg, in case some apps
477 decide to move interrupts around for whatever reason... */
478 DOSVM_CallBuiltinHandler( &realmode_ctx, LOWORD(rm_int)/4 );
480 INT_SetRealModeContext( call, &realmode_ctx );
484 /**********************************************************************
485 * CallRMProc (WINEDOS.@)
487 void WINAPI DOSVM_CallRMProc( CONTEXT86 *context, int iret )
489 REALMODECALL *p = CTX_SEG_OFF_TO_LIN( context,
494 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
495 p->eax, p->ebx, p->ecx, p->edx);
496 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
497 p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
499 if (!(p->cs) && !(p->ip)) { /* remove this check
500 if Int21/6501 case map function
501 has been implemented */
505 INT_GetRealModeContext(p, &context16);
506 DPMI_CallRMProc( &context16, ((LPWORD)MapSL(MAKESEGPTR(context->SegSs, LOWORD(context->Esp))))+3,
507 CX_reg(context), iret );
508 INT_SetRealModeContext(p, &context16);
512 /* (see dosmem.c, function DOSMEM_InitDPMI) */
513 static void StartPM( CONTEXT86 *context )
515 UINT16 cs, ss, ds, es;
517 DWORD psp_ofs = (DWORD)(DOSVM_psp<<4);
518 PDB16 *psp = (PDB16 *)psp_ofs;
519 HANDLE16 env_seg = psp->environment;
520 unsigned char selflags = WINE_LDT_FLAGS_DATA;
522 RESET_CFLAG(context);
523 dpmi_flag = AX_reg(context);
524 /* our mode switch wrapper have placed the desired CS into DX */
525 cs = SELECTOR_AllocBlock( (void *)(DX_reg(context)<<4), 0x10000, WINE_LDT_FLAGS_CODE );
526 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
527 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
528 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
529 32-bit code using this stack. */
530 if (dpmi_flag & 1) selflags |= WINE_LDT_FLAGS_32BIT;
531 ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, selflags );
532 /* do the same for the data segments, just in case */
533 if (context->SegDs == context->SegSs) ds = ss;
534 else ds = SELECTOR_AllocBlock( (void *)(context->SegDs<<4), 0x10000, selflags );
535 es = SELECTOR_AllocBlock( psp, 0x100, selflags );
536 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
537 psp->environment = SELECTOR_AllocBlock( (void *)(env_seg<<4), 0x10000, WINE_LDT_FLAGS_DATA );
540 pm_ctx.SegCs = DOSVM_dpmi_segments->dpmi_sel;
541 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
549 TRACE("DOS program is now entering %d-bit protected mode\n",
550 DOSVM_IsDos32() ? 32 : 16);
551 wine_call_to_16_regs_short(&pm_ctx, 0);
553 /* in the current state of affairs, we won't ever actually return here... */
554 /* we should have int21/ah=4c do it someday, though... */
556 FreeSelector16(psp->environment);
557 psp->environment = env_seg;
559 if (ds != ss) FreeSelector16(ds);
564 static RMCB *DPMI_AllocRMCB( void )
566 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
571 LPVOID RMCBmem = DOSMEM_GetBlock(4, &uParagraph);
574 *p++ = 0xcd; /* RMCB: */
575 *p++ = 0x31; /* int $0x31 */
576 /* it is the called procedure's task to change the return CS:EIP
577 the DPMI 0.9 spec states that if it doesn't, it will be called again */
579 *p++ = 0xfc; /* jmp RMCB */
580 NewRMCB->address = MAKELONG(0, uParagraph);
581 NewRMCB->next = FirstRMCB;
588 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
590 RMCB *NewRMCB = DPMI_AllocRMCB();
593 NewRMCB->proc_ofs = (DWORD)proc;
594 NewRMCB->proc_sel = 0;
595 NewRMCB->regs_ofs = 0;
596 NewRMCB->regs_sel = 0;
597 return (FARPROC16)(NewRMCB->address);
603 static int DPMI_FreeRMCB( DWORD address )
605 RMCB *CurrRMCB = FirstRMCB;
606 RMCB *PrevRMCB = NULL;
608 while (CurrRMCB && (CurrRMCB->address != address))
611 CurrRMCB = CurrRMCB->next;
616 PrevRMCB->next = CurrRMCB->next;
618 FirstRMCB = CurrRMCB->next;
619 DOSMEM_FreeBlock(PTR_REAL_TO_LIN(SELECTOROF(CurrRMCB->address),OFFSETOF(CurrRMCB->address)));
620 HeapFree(GetProcessHeap(), 0, CurrRMCB);
627 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
629 DPMI_FreeRMCB( (DWORD)proc );
633 /**********************************************************************
634 * RawModeSwitch (WINEDOS.@)
636 * DPMI Raw Mode Switch handler
638 void WINAPI DOSVM_RawModeSwitch( CONTEXT86 *context )
643 /* initialize real-mode context as per spec */
644 memset(&rm_ctx, 0, sizeof(rm_ctx));
645 rm_ctx.SegDs = AX_reg(context);
646 rm_ctx.SegEs = CX_reg(context);
647 rm_ctx.SegSs = DX_reg(context);
648 rm_ctx.Esp = context->Ebx;
649 rm_ctx.SegCs = SI_reg(context);
650 rm_ctx.Eip = context->Edi;
651 rm_ctx.Ebp = context->Ebp;
654 rm_ctx.EFlags = context->EFlags; /* at least we need the IF flag */
656 /* enter real mode again */
657 TRACE("re-entering real mode at %04lx:%04lx\n",rm_ctx.SegCs,rm_ctx.Eip);
658 ret = DOSVM_Enter( &rm_ctx );
659 /* when the real-mode stuff call its mode switch address,
660 DOSVM_Enter will return and we will continue here */
664 /* if the sync was lost, there's no way to recover */
668 /* alter protected-mode context as per spec */
669 context->SegDs = LOWORD(rm_ctx.Eax);
670 context->SegEs = LOWORD(rm_ctx.Ecx);
671 context->SegSs = LOWORD(rm_ctx.Edx);
672 context->Esp = rm_ctx.Ebx;
673 context->SegCs = LOWORD(rm_ctx.Esi);
674 context->Eip = rm_ctx.Edi;
675 context->Ebp = rm_ctx.Ebp;
679 /* Return to new address and hope that we didn't mess up */
680 TRACE("re-entering protected mode at %04lx:%08lx\n",
681 context->SegCs, context->Eip);
685 /**********************************************************************
686 * AllocRMCB (WINEDOS.@)
688 void WINAPI DOSVM_AllocRMCB( CONTEXT86 *context )
690 RMCB *NewRMCB = DPMI_AllocRMCB();
692 TRACE("Function to call: %04x:%04x\n", (WORD)context->SegDs, SI_reg(context) );
696 NewRMCB->proc_ofs = DOSVM_IsDos32() ? context->Esi : LOWORD(context->Esi);
697 NewRMCB->proc_sel = context->SegDs;
698 NewRMCB->regs_ofs = DOSVM_IsDos32() ? context->Edi : LOWORD(context->Edi);
699 NewRMCB->regs_sel = context->SegEs;
700 SET_CX( context, HIWORD(NewRMCB->address) );
701 SET_DX( context, LOWORD(NewRMCB->address) );
705 SET_AX( context, 0x8015 ); /* callback unavailable */
711 /**********************************************************************
712 * FreeRMCB (WINEDOS.@)
714 void WINAPI DOSVM_FreeRMCB( CONTEXT86 *context )
716 FIXME("callback address: %04x:%04x\n",
717 CX_reg(context), DX_reg(context));
719 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
720 SET_AX( context, 0x8024 ); /* invalid callback address */
726 /**********************************************************************
727 * DOSVM_RawModeSwitchHandler
729 * DPMI Raw Mode Switch handler.
730 * This routine does all the stack manipulation tricks needed
731 * to return from protected mode interrupt using modified
732 * code and stack pointers.
734 void WINAPI DOSVM_RawModeSwitchHandler( CONTEXT86 *context )
737 DOSVM_SaveCallFrame( context, &frame );
738 DOSVM_RawModeSwitch( context );
739 DOSVM_RestoreCallFrame( context, &frame );
743 /**********************************************************************
744 * DOSVM_CheckWrappers
746 * Check if this was really a wrapper call instead of an interrupt.
748 BOOL DOSVM_CheckWrappers( CONTEXT86 *context )
750 if (context->SegCs==DOSVM_dpmi_segments->dpmi_seg) {
751 /* This is the protected mode switch */
755 else if (context->SegCs==DOSVM_dpmi_segments->xms_seg)
757 /* This is the XMS driver entry point */
758 XMS_Handler(context);
764 RMCB *CurrRMCB = FirstRMCB;
766 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
767 CurrRMCB = CurrRMCB->next;
770 /* RMCB call, propagate to protected-mode handler */
771 DPMI_CallRMCBProc(context, CurrRMCB, dpmi_flag);
779 /**********************************************************************
780 * DOSVM_Int31Handler (WINEDOS16.149)
782 * Handler for int 31h (DPMI).
784 void WINAPI DOSVM_Int31Handler( CONTEXT86 *context )
786 RESET_CFLAG(context);
787 switch(AX_reg(context))
789 case 0x0000: /* Allocate LDT descriptors */
790 TRACE( "allocate LDT descriptors (%d)\n", CX_reg(context) );
792 WORD sel = AllocSelectorArray16( CX_reg(context) );
796 SET_AX( context, 0x8011 ); /* descriptor unavailable */
797 SET_CFLAG( context );
801 TRACE( "success, array starts at 0x%04x\n", sel );
802 SET_AX( context, sel );
807 case 0x0001: /* Free LDT descriptor */
808 TRACE( "free LDT descriptor (0x%04x)\n", BX_reg(context) );
809 if (FreeSelector16( BX_reg(context) ))
811 SET_AX( context, 0x8022 ); /* invalid selector */
812 SET_CFLAG( context );
816 /* If a segment register contains the selector being freed, */
817 /* set it to zero. */
818 if (!((context->SegDs^BX_reg(context)) & ~3)) context->SegDs = 0;
819 if (!((context->SegEs^BX_reg(context)) & ~3)) context->SegEs = 0;
820 if (!((context->SegFs^BX_reg(context)) & ~3)) context->SegFs = 0;
821 if (!((context->SegGs^BX_reg(context)) & ~3)) context->SegGs = 0;
825 case 0x0002: /* Real mode segment to descriptor */
826 TRACE( "real mode segment to descriptor (0x%04x)\n", BX_reg(context) );
828 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
829 switch(BX_reg(context))
831 case 0x0000: entryPoint = 183; break; /* __0000H */
832 case 0x0040: entryPoint = 193; break; /* __0040H */
833 case 0xa000: entryPoint = 174; break; /* __A000H */
834 case 0xb000: entryPoint = 181; break; /* __B000H */
835 case 0xb800: entryPoint = 182; break; /* __B800H */
836 case 0xc000: entryPoint = 195; break; /* __C000H */
837 case 0xd000: entryPoint = 179; break; /* __D000H */
838 case 0xe000: entryPoint = 190; break; /* __E000H */
839 case 0xf000: entryPoint = 194; break; /* __F000H */
841 SET_AX( context, DOSMEM_AllocSelector(BX_reg(context)) );
846 FARPROC16 proc = GetProcAddress16( GetModuleHandle16( "KERNEL" ),
847 (LPCSTR)(ULONG_PTR)entryPoint );
848 SET_AX( context, LOWORD(proc) );
853 case 0x0003: /* Get next selector increment */
854 TRACE("get selector increment (__AHINCR)\n");
855 context->Eax = __AHINCR;
858 case 0x0004: /* Lock selector (not supported) */
859 FIXME("lock selector not supported\n");
860 context->Eax = 0; /* FIXME: is this a correct return value? */
863 case 0x0005: /* Unlock selector (not supported) */
864 FIXME("unlock selector not supported\n");
865 context->Eax = 0; /* FIXME: is this a correct return value? */
868 case 0x0006: /* Get selector base address */
869 TRACE( "get selector base address (0x%04x)\n", BX_reg(context) );
872 WORD sel = BX_reg(context);
873 wine_ldt_get_entry( sel, &entry );
874 if (wine_ldt_is_empty(&entry))
876 context->Eax = 0x8022; /* invalid selector */
881 void *base = wine_ldt_get_base(&entry);
882 SET_CX( context, HIWORD(base) );
883 SET_DX( context, LOWORD(base) );
888 case 0x0007: /* Set selector base address */
890 DWORD base = MAKELONG( DX_reg(context), CX_reg(context) );
891 WORD sel = BX_reg(context);
892 TRACE( "set selector base address (0x%04x,0x%08lx)\n", sel, base );
894 /* check if Win16 app wants to access lower 64K of DOS memory */
895 if (base < 0x10000 && DOSVM_IsWin16())
898 SetSelectorBase( sel, base );
902 case 0x0008: /* Set selector limit */
904 DWORD limit = MAKELONG( DX_reg(context), CX_reg(context) );
905 TRACE( "set selector limit (0x%04x,0x%08lx)\n",
906 BX_reg(context), limit );
907 SetSelectorLimit16( BX_reg(context), limit );
911 case 0x0009: /* Set selector access rights */
912 TRACE( "set selector access rights(0x%04x,0x%04x)\n",
913 BX_reg(context), CX_reg(context) );
914 SelectorAccessRights16( BX_reg(context), 1, CX_reg(context) );
917 case 0x000a: /* Allocate selector alias */
918 TRACE( "allocate selector alias (0x%04x)\n", BX_reg(context) );
919 SET_AX( context, AllocCStoDSAlias16( BX_reg(context) ) );
920 if (!AX_reg(context))
922 SET_AX( context, 0x8011 ); /* descriptor unavailable */
927 case 0x000b: /* Get descriptor */
928 TRACE( "get descriptor (0x%04x)\n", BX_reg(context) );
930 LDT_ENTRY *entry = (LDT_ENTRY*)CTX_SEG_OFF_TO_LIN( context,
933 wine_ldt_get_entry( BX_reg(context), entry );
937 case 0x000c: /* Set descriptor */
938 TRACE( "set descriptor (0x%04x)\n", BX_reg(context) );
940 LDT_ENTRY *entry = (LDT_ENTRY*)CTX_SEG_OFF_TO_LIN( context,
943 wine_ldt_set_entry( BX_reg(context), entry );
947 case 0x000d: /* Allocate specific LDT descriptor */
948 FIXME( "allocate descriptor (0x%04x), stub!\n", BX_reg(context) );
949 SET_AX( context, 0x8011 ); /* descriptor unavailable */
950 SET_CFLAG( context );
953 case 0x000e: /* Get Multiple Descriptors (1.0) */
954 FIXME( "get multiple descriptors - unimplemented\n" );
957 case 0x000f: /* Set Multiple Descriptors (1.0) */
958 FIXME( "set multiple descriptors - unimplemented\n" );
961 case 0x0100: /* Allocate DOS memory block */
962 TRACE( "allocate DOS memory block (0x%x paragraphs)\n", BX_reg(context) );
964 DWORD dw = GlobalDOSAlloc16( (DWORD)BX_reg(context) << 4 );
966 SET_AX( context, HIWORD(dw) );
967 SET_DX( context, LOWORD(dw) );
969 SET_AX( context, 0x0008 ); /* insufficient memory */
970 SET_BX( context, DOSMEM_Available() >> 4 );
976 case 0x0101: /* Free DOS memory block */
977 TRACE( "free DOS memory block (0x%04x)\n", DX_reg(context) );
979 WORD error = GlobalDOSFree16( DX_reg(context) );
981 SET_AX( context, 0x0009 ); /* memory block address invalid */
982 SET_CFLAG( context );
987 case 0x0102: /* Resize DOS Memory Block */
988 FIXME( "resize DOS memory block (0x%04x, 0x%x paragraphs) - unimplemented\n",
989 DX_reg(context), BX_reg(context) );
992 case 0x0200: /* get real mode interrupt vector */
993 TRACE( "get realmode interupt vector (0x%02x)\n",
996 FARPROC16 proc = DOSVM_GetRMHandler( BL_reg(context) );
997 SET_CX( context, SELECTOROF(proc) );
998 SET_DX( context, OFFSETOF(proc) );
1002 case 0x0201: /* set real mode interrupt vector */
1003 TRACE( "set realmode interrupt vector (0x%02x, 0x%04x:0x%04x)\n",
1004 BL_reg(context), CX_reg(context), DX_reg(context) );
1005 DOSVM_SetRMHandler( BL_reg(context),
1006 (FARPROC16)MAKESEGPTR(CX_reg(context), DX_reg(context)) );
1009 case 0x0202: /* Get Processor Exception Handler Vector */
1010 FIXME( "Get Processor Exception Handler Vector (0x%02x)\n",
1012 if (DOSVM_IsDos32())
1014 SET_CX( context, 0 );
1019 SET_CX( context, 0 );
1020 SET_DX( context, 0 );
1024 case 0x0203: /* Set Processor Exception Handler Vector */
1025 FIXME( "Set Processor Exception Handler Vector (0x%02x)\n",
1029 case 0x0204: /* Get protected mode interrupt vector */
1030 TRACE("get protected mode interrupt handler (0x%02x)\n",
1032 if (DOSVM_IsDos32())
1034 FARPROC48 handler = DOSVM_GetPMHandler48( BL_reg(context) );
1035 SET_CX( context, handler.selector );
1036 context->Edx = handler.offset;
1040 FARPROC16 handler = DOSVM_GetPMHandler16( BL_reg(context) );
1041 SET_CX( context, SELECTOROF(handler) );
1042 SET_DX( context, OFFSETOF(handler) );
1046 case 0x0205: /* Set protected mode interrupt vector */
1047 TRACE("set protected mode interrupt handler (0x%02x,0x%04x:0x%08lx)\n",
1048 BL_reg(context), CX_reg(context), context->Edx);
1049 if (DOSVM_IsDos32())
1052 handler.selector = CX_reg(context);
1053 handler.offset = context->Edx;
1054 DOSVM_SetPMHandler48( BL_reg(context), handler );
1059 handler = (FARPROC16)MAKESEGPTR( CX_reg(context), DX_reg(context));
1060 DOSVM_SetPMHandler16( BL_reg(context), handler );
1064 case 0x0300: /* Simulate real mode interrupt */
1065 TRACE( "Simulate real mode interrupt.\n" );
1066 DOSVM_CallRMInt( context );
1069 case 0x0301: /* Call real mode procedure with far return */
1070 TRACE( "Call real mode procedure with far return.\n" );
1071 DOSVM_CallRMProc( context, FALSE );
1074 case 0x0302: /* Call real mode procedure with interrupt return */
1075 TRACE( "Call real mode procedure with interrupt return.\n" );
1076 DOSVM_CallRMProc( context, TRUE );
1079 case 0x0303: /* Allocate Real Mode Callback Address */
1080 TRACE( "Allocate real mode callback address.\n" );
1081 DOSVM_AllocRMCB( context );
1084 case 0x0304: /* Free Real Mode Callback Address */
1085 TRACE( "Free real mode callback address.\n" );
1086 DOSVM_FreeRMCB( context );
1089 case 0x0305: /* Get State Save/Restore Addresses */
1090 TRACE("get state save/restore addresses\n");
1091 /* we probably won't need this kind of state saving */
1092 SET_AX( context, 0 );
1094 /* real mode: just point to the lret */
1095 SET_BX( context, DOSVM_dpmi_segments->wrap_seg );
1096 SET_CX( context, 2 );
1098 /* protected mode: don't have any handler yet... */
1099 /* FIXME: Use DI in 16-bit DPMI and EDI in 32-bit DPMI */
1100 FIXME("no protected-mode dummy state save/restore handler yet\n");
1101 SET_SI( context, 0 );
1105 case 0x0306: /* Get Raw Mode Switch Addresses */
1106 TRACE("get raw mode switch addresses\n");
1108 /* real mode, point to standard DPMI return wrapper */
1109 SET_BX( context, DOSVM_dpmi_segments->wrap_seg );
1110 SET_CX( context, 0 );
1112 /* protected mode, point to DPMI call wrapper */
1113 /* FIXME: Use DI in 16-bit DPMI and EDI in 32-bit DPMI */
1114 /* FIXME: Doesn't work in DPMI32... */
1115 SET_SI( context, DOSVM_dpmi_segments->dpmi_sel );
1116 context->Edi = 8; /* offset of the INT 0x31 call */
1119 case 0x0400: /* Get DPMI version */
1120 TRACE("get DPMI version\n");
1125 SET_AX( context, 0x005a ); /* DPMI version 0.90 */
1126 SET_BX( context, 0x0005 ); /* Flags: 32-bit, virtual memory */
1127 SET_CL( context, si.wProcessorLevel );
1128 SET_DX( context, 0x0870 ); /* Master/slave interrupt controller base */
1132 case 0x0401: /* Get DPMI Capabilities (1.0) */
1133 FIXME( "get dpmi capabilities - unimplemented\n");
1136 case 0x0500: /* Get free memory information */
1137 TRACE("get free memory information\n");
1140 void *ptr = CTX_SEG_OFF_TO_LIN( context,
1144 mmi.dwSize = sizeof( mmi );
1145 MemManInfo16( &mmi );
1147 /* the layout is just the same as MEMMANINFO, but without
1150 memcpy( ptr, ((char*)&mmi)+4, sizeof(mmi)-4 );
1154 case 0x0501: /* Allocate memory block */
1156 DWORD size = MAKELONG( CX_reg(context), BX_reg(context) );
1159 TRACE( "allocate memory block (%ld bytes)\n", size );
1161 ptr = (BYTE *)DPMI_xalloc( size );
1164 SET_AX( context, 0x8012 ); /* linear memory not available */
1169 SET_BX( context, HIWORD(ptr) );
1170 SET_CX( context, LOWORD(ptr) );
1171 SET_SI( context, HIWORD(ptr) );
1172 SET_DI( context, LOWORD(ptr) );
1177 case 0x0502: /* Free memory block */
1179 DWORD handle = MAKELONG( DI_reg(context), SI_reg(context) );
1180 TRACE( "free memory block (0x%08lx)\n", handle );
1181 DPMI_xfree( (void *)handle );
1185 case 0x0503: /* Resize memory block */
1187 DWORD size = MAKELONG( CX_reg(context), BX_reg(context) );
1188 DWORD handle = MAKELONG( DI_reg(context), SI_reg(context) );
1191 TRACE( "resize memory block (0x%08lx, %ld bytes)\n", handle, size );
1193 ptr = (BYTE *)DPMI_xrealloc( (void *)handle, size );
1196 SET_AX( context, 0x8012 ); /* linear memory not available */
1199 SET_BX( context, HIWORD(ptr) );
1200 SET_CX( context, LOWORD(ptr) );
1201 SET_SI( context, HIWORD(ptr) );
1202 SET_DI( context, LOWORD(ptr) );
1207 case 0x0507: /* Set page attributes (1.0) */
1208 FIXME( "set page attributes - unimplemented\n" );
1209 break; /* Just ignore it */
1211 case 0x0600: /* Lock linear region */
1212 TRACE( "lock linear region - ignored (no paging)\n" );
1215 case 0x0601: /* Unlock linear region */
1216 TRACE( "unlock linear region - ignored (no paging)\n" );
1219 case 0x0602: /* Mark real mode region as pageable */
1220 TRACE( "mark real mode region as pageable - ignored (no paging)\n" );
1223 case 0x0603: /* Relock real mode region */
1224 TRACE( "relock real mode region - ignored (no paging)\n" );
1227 case 0x0604: /* Get page size */
1228 TRACE("get pagesize\n");
1229 SET_BX( context, HIWORD(getpagesize()) );
1230 SET_CX( context, LOWORD(getpagesize()) );
1233 case 0x0700: /* Mark pages as paging candidates */
1234 TRACE( "mark pages as paging candidates - ignored (no paging)\n" );
1237 case 0x0701: /* Discard pages */
1238 TRACE( "discard pages - ignored (no paging)\n" );
1241 case 0x0702: /* Mark page as demand-paging candidate */
1242 TRACE( "mark page as demand-paging candidate - ignored (no paging)\n" );
1245 case 0x0703: /* Discard page contents */
1246 TRACE( "discard page contents - ignored (no paging)\n" );
1249 case 0x0800: /* Physical address mapping */
1250 FIXME( "physical address mapping (0x%08lx) - unimplemented\n",
1251 MAKELONG(CX_reg(context),BX_reg(context)) );
1254 case 0x0e00: /* Get Coprocessor Status (1.0) */
1256 * Return status in AX bits:
1257 * B0 - MPv (MP bit in the virtual MSW/CR0)
1258 * 0 = numeric coprocessor is disabled for this client
1259 * 1 = numeric coprocessor is enabled for this client
1260 * B1 - EMv (EM bit in the virtual MSW/CR0)
1261 * 0 = client is not emulating coprocessor instructions
1262 * 1 = client is emulating coprocessor instructions
1263 * B2 - MPr (MP bit from the actual MSW/CR0)
1264 * 0 = numeric coprocessor is not present
1265 * 1 = numeric coprocessor is present
1266 * B3 - EMr (EM bit from the actual MSW/CR0)
1267 * 0 = host is not emulating coprocessor instructions
1268 * 1 = host is emulating coprocessor instructions
1269 * B4-B7 - coprocessor type
1270 * 00H = no coprocessor
1273 * 04H = 80486 with numeric coprocessor
1274 * 05H-0FH = reserved for future numeric processors
1276 TRACE( "Get Coprocessor Status\n" );
1277 SET_AX( context, 69 ); /* 486, coprocessor present and enabled */
1280 case 0x0e01: /* Set Coprocessor Emulation (1.0) */
1282 * See function 0x0e00.
1283 * BX bit B0 is new value for MPv.
1284 * BX bit B1 is new value for EMv.
1286 if (BX_reg(context) != 1)
1287 FIXME( "Set Coprocessor Emulation to %d - unimplemented\n",
1290 TRACE( "Set Coprocessor Emulation - ignored\n" );
1294 INT_BARF( context, 0x31 );
1295 SET_AX( context, 0x8001 ); /* unsupported function */