4 * Copyright 1995 Alexandre Julliard
12 #include "wine/winbase16.h"
13 #include "wine/port.h"
14 #include "builtin16.h"
20 #include "selectors.h"
22 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(int31
);
26 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
28 void CreateBPB(int drive
, BYTE
*data
, BOOL16 limited
); /* defined in int21.c */
30 static void* lastvalloced
= NULL
;
32 /* Structure for real-mode callbacks */
56 typedef struct tagRMCB
{
58 DWORD proc_ofs
,proc_sel
;
59 DWORD regs_ofs
,regs_sel
;
63 static RMCB
*FirstRMCB
= NULL
;
66 /**********************************************************************
68 * special virtualalloc, allocates lineary monoton growing memory.
69 * (the usual VirtualAlloc does not satisfy that restriction)
72 DPMI_xalloc(int len
) {
74 LPVOID oldlastv
= lastvalloced
;
80 ret
=VirtualAlloc(lastvalloced
,len
,MEM_COMMIT
|MEM_RESERVE
,PAGE_EXECUTE_READWRITE
);
82 lastvalloced
= (char *) lastvalloced
+ 0x10000;
83 /* we failed to allocate one in the first round.
86 if (!xflag
&& (lastvalloced
<oldlastv
)) { /* wrapped */
87 FIXME("failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len
);
90 /* if we even fail to allocate something in the next
93 if ((xflag
==1) && (lastvalloced
>= oldlastv
))
95 if ((xflag
==2) && (lastvalloced
< oldlastv
)) {
96 FIXME("failed to allocate any memory of %d bytes!\n",len
);
101 ret
=VirtualAlloc(NULL
,len
,MEM_COMMIT
|MEM_RESERVE
,PAGE_EXECUTE_READWRITE
);
102 lastvalloced
= (LPVOID
)(((DWORD
)ret
+len
+0xffff)&~0xffff);
107 DPMI_xfree(LPVOID ptr
) {
108 VirtualFree(ptr
,0,MEM_RELEASE
);
111 /* FIXME: perhaps we could grow this mapped area... */
113 DPMI_xrealloc(LPVOID ptr
,DWORD newsize
) {
114 MEMORY_BASIC_INFORMATION mbi
;
117 newptr
= DPMI_xalloc(newsize
);
119 if (!VirtualQuery(ptr
,&mbi
,sizeof(mbi
))) {
120 FIXME("realloc of DPMI_xallocd region %p?\n",ptr
);
123 if (mbi
.State
== MEM_FREE
) {
124 FIXME("realloc of DPMI_xallocd region %p?\n",ptr
);
127 /* We do not shrink allocated memory. most reallocs
128 * only do grows anyway
130 if (newsize
<=mbi
.RegionSize
)
132 memcpy(newptr
,ptr
,mbi
.RegionSize
);
137 /**********************************************************************
138 * INT_GetRealModeContext
140 static void INT_GetRealModeContext( REALMODECALL
*call
, CONTEXT86
*context
)
142 context
->Eax
= call
->eax
;
143 context
->Ebx
= call
->ebx
;
144 context
->Ecx
= call
->ecx
;
145 context
->Edx
= call
->edx
;
146 context
->Esi
= call
->esi
;
147 context
->Edi
= call
->edi
;
148 context
->Ebp
= call
->ebp
;
149 context
->EFlags
= call
->fl
| V86_FLAG
;
150 context
->Eip
= call
->ip
;
151 context
->Esp
= call
->sp
;
152 context
->SegCs
= call
->cs
;
153 context
->SegDs
= call
->ds
;
154 context
->SegEs
= call
->es
;
155 context
->SegFs
= call
->fs
;
156 context
->SegGs
= call
->gs
;
157 context
->SegSs
= call
->ss
;
161 /**********************************************************************
162 * INT_SetRealModeContext
164 static void INT_SetRealModeContext( REALMODECALL
*call
, CONTEXT86
*context
)
166 call
->eax
= context
->Eax
;
167 call
->ebx
= context
->Ebx
;
168 call
->ecx
= context
->Ecx
;
169 call
->edx
= context
->Edx
;
170 call
->esi
= context
->Esi
;
171 call
->edi
= context
->Edi
;
172 call
->ebp
= context
->Ebp
;
173 call
->fl
= LOWORD(context
->EFlags
);
174 call
->ip
= LOWORD(context
->Eip
);
175 call
->sp
= LOWORD(context
->Esp
);
176 call
->cs
= context
->SegCs
;
177 call
->ds
= context
->SegDs
;
178 call
->es
= context
->SegEs
;
179 call
->fs
= context
->SegFs
;
180 call
->gs
= context
->SegGs
;
181 call
->ss
= context
->SegSs
;
186 void DPMI_CallRMCB32(RMCB
*rmcb
, UINT16 ss
, DWORD esp
, UINT16
*es
, DWORD
*edi
)
187 #if 0 /* original code, which early gccs puke on */
190 __asm__
__volatile__(
198 ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
204 : "=d" (*es
), "=D" (*edi
), "=S" (_clobber
), "=a" (_clobber
), "=c" (_clobber
)
205 : "0" (ss
), "2" (esp
),
206 "4" (rmcb
->regs_sel
), "1" (rmcb
->regs_ofs
),
207 "3" (&rmcb
->proc_ofs
) );
209 #else /* code generated by a gcc new enough */
211 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32
,
216 "movl 0x8(%ebp),%eax\n\t"
217 "movl 0x10(%ebp),%esi\n\t"
218 "movl 0xc(%ebp),%edx\n\t"
219 "movl 0x10(%eax),%ecx\n\t"
220 "movl 0xc(%eax),%edi\n\t"
229 ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
235 "movl 0x14(%ebp),%eax\n\t"
236 "movw %dx,(%eax)\n\t"
237 "movl 0x18(%ebp),%edx\n\t"
238 "movl %edi,(%edx)\n\t"
245 #endif /* __i386__ */
247 /**********************************************************************
250 * This routine does the hard work of calling a callback procedure.
252 static void DPMI_CallRMCBProc( CONTEXT86
*context
, RMCB
*rmcb
, WORD flag
)
254 if (IS_SELECTOR_SYSTEM( rmcb
->proc_sel
)) {
255 /* Wine-internal RMCB, call directly */
256 ((RMCBPROC
)rmcb
->proc_ofs
)(context
);
262 INT_SetRealModeContext(MapSL(MAKESEGPTR( rmcb
->regs_sel
, rmcb
->regs_ofs
)), context
);
263 ss
= SELECTOR_AllocBlock( (void *)(context
->SegSs
<<4), 0x10000, WINE_LDT_FLAGS_DATA
);
266 FIXME("untested!\n");
268 /* The called proc ends with an IRET, and takes these parameters:
269 * DS:ESI = pointer to real-mode SS:SP
270 * ES:EDI = pointer to real-mode call structure
272 * ES:EDI = pointer to real-mode call structure (may be a copy)
273 * It is the proc's responsibility to change the return CS:IP in the
274 * real-mode call structure. */
276 /* 32-bit DPMI client */
277 DPMI_CallRMCB32(rmcb
, ss
, esp
, &es
, &edi
);
279 /* 16-bit DPMI client */
280 CONTEXT86 ctx
= *context
;
281 ctx
.SegCs
= rmcb
->proc_sel
;
282 ctx
.Eip
= rmcb
->proc_ofs
;
285 ctx
.SegEs
= rmcb
->regs_sel
;
286 ctx
.Edi
= rmcb
->regs_ofs
;
287 /* FIXME: I'm pretty sure this isn't right - should push flags first */
288 wine_call_to_16_regs_short(&ctx
, 0);
293 INT_GetRealModeContext( MapSL( MAKESEGPTR( es
, edi
)), context
);
295 ERR("RMCBs only implemented for i386\n");
301 /**********************************************************************
304 * This routine does the hard work of calling a real mode procedure.
306 int DPMI_CallRMProc( CONTEXT86
*context
, LPWORD stack
, int args
, int iret
)
309 LPVOID addr
= NULL
; /* avoid gcc warning */
310 LPDOSTASK lpDosTask
= MZ_Current();
312 int alloc
= 0, already
= 0;
315 GlobalUnlock16( GetCurrentTask() );
317 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
318 context
->Eax
, context
->Ebx
, context
->Ecx
, context
->Edx
);
319 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
320 context
->Esi
, context
->Edi
, context
->SegEs
, context
->SegDs
,
321 context
->SegCs
, LOWORD(context
->Eip
), args
, iret
?"IRET":"FAR" );
325 /* there might be some code that just jumps to RMCBs or the like,
326 in which case following the jumps here might get us to a shortcut */
327 code
= CTX_SEG_OFF_TO_LIN(context
, context
->SegCs
, context
->Eip
);
329 case 0xe9: /* JMP NEAR */
330 context
->Eip
+= 3 + *(WORD
*)(code
+1);
331 /* yeah, I know these gotos don't look good... */
332 goto callrmproc_again
;
333 case 0xea: /* JMP FAR */
334 context
->Eip
= *(WORD
*)(code
+1);
335 context
->SegCs
= *(WORD
*)(code
+3);
336 /* ...but since the label is there anyway... */
337 goto callrmproc_again
;
338 case 0xeb: /* JMP SHORT */
339 context
->Eip
+= 2 + *(signed char *)(code
+1);
340 /* ...because of other gotos below, so... */
341 goto callrmproc_again
;
344 /* shortcut for chaining to internal interrupt handlers */
345 if ((context
->SegCs
== 0xF000) && iret
) {
346 return INT_RealModeInterrupt( LOWORD(context
->Eip
)/4, context
);
349 /* shortcut for RMCBs */
350 CurrRMCB
= FirstRMCB
;
352 while (CurrRMCB
&& (HIWORD(CurrRMCB
->address
) != context
->SegCs
))
353 CurrRMCB
= CurrRMCB
->next
;
355 if (!(CurrRMCB
|| lpDosTask
)) {
356 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
357 TRACE("creating VM86 task\n");
358 if (!(lpDosTask
= MZ_AllocDPMITask() )) {
359 ERR("could not setup VM86 task\n");
364 if (!context
->SegSs
) {
365 alloc
= 1; /* allocate default stack */
366 stack16
= addr
= DOSMEM_GetBlock( 64, (UINT16
*)&(context
->SegSs
) );
370 ERR("could not allocate default stack\n");
374 stack16
= CTX_SEG_OFF_TO_LIN(context
, context
->SegSs
, context
->Esp
);
376 context
->Esp
-= (args
+ (iret
?1:0)) * sizeof(WORD
);
378 if (args
) memcpy(stack16
, stack
, args
*sizeof(WORD
) );
379 /* push flags if iret */
382 *stack16
= LOWORD(context
->EFlags
);
384 /* push return address (return to interrupt wrapper) */
385 *(--stack16
) = DOSMEM_wrap_seg
;
388 context
->Esp
-= 2*sizeof(WORD
);
393 /* RMCB call, invoke protected-mode handler directly */
394 DPMI_CallRMCBProc(context
, CurrRMCB
, lpDosTask
? lpDosTask
->dpmi_flag
: 0);
395 /* check if we returned to where we thought we would */
396 if ((context
->SegCs
!= DOSMEM_wrap_seg
) ||
397 (LOWORD(context
->Eip
) != 0)) {
398 /* we need to continue at different address in real-mode space,
399 so we need to set it all up for real mode again */
400 goto callrmproc_again
;
403 TRACE("entering real mode...\n");
404 DOSVM_Enter( context
);
405 TRACE("returned from real-mode call\n");
407 if (alloc
) DOSMEM_FreeBlock( addr
);
412 /**********************************************************************
415 static void CallRMInt( CONTEXT86
*context
)
417 CONTEXT86 realmode_ctx
;
418 FARPROC16 rm_int
= INT_GetRMHandler( BL_reg(context
) );
419 REALMODECALL
*call
= MapSL( MAKESEGPTR( context
->SegEs
, DI_reg(context
) ));
420 INT_GetRealModeContext( call
, &realmode_ctx
);
422 /* we need to check if a real-mode program has hooked the interrupt */
423 if (HIWORD(rm_int
)!=0xF000) {
424 /* yup, which means we need to switch to real mode... */
425 realmode_ctx
.SegCs
= HIWORD(rm_int
);
426 realmode_ctx
.Eip
= LOWORD(rm_int
);
427 if (DPMI_CallRMProc( &realmode_ctx
, NULL
, 0, TRUE
))
430 RESET_CFLAG(context
);
431 /* use the IP we have instead of BL_reg, in case some apps
432 decide to move interrupts around for whatever reason... */
433 if (INT_RealModeInterrupt( LOWORD(rm_int
)/4, &realmode_ctx
))
435 if (context
->EFlags
& 1) {
436 FIXME("%02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
437 BL_reg(context
), realmode_ctx
.Eax
, realmode_ctx
.Ebx
,
438 realmode_ctx
.Ecx
, realmode_ctx
.Edx
);
439 FIXME(" ESI=%08lx EDI=%08lx DS=%04lx ES=%04lx\n",
440 realmode_ctx
.Esi
, realmode_ctx
.Edi
,
441 realmode_ctx
.SegDs
, realmode_ctx
.SegEs
);
444 INT_SetRealModeContext( call
, &realmode_ctx
);
448 static void CallRMProc( CONTEXT86
*context
, int iret
)
450 REALMODECALL
*p
= MapSL( MAKESEGPTR( context
->SegEs
, DI_reg(context
) ));
453 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
454 p
->eax
, p
->ebx
, p
->ecx
, p
->edx
);
455 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
456 p
->esi
, p
->edi
, p
->es
, p
->ds
, p
->cs
, p
->ip
, CX_reg(context
), iret
?"IRET":"FAR" );
458 if (!(p
->cs
) && !(p
->ip
)) { /* remove this check
459 if Int21/6501 case map function
460 has been implemented */
464 INT_GetRealModeContext(p
, &context16
);
465 DPMI_CallRMProc( &context16
, ((LPWORD
)MapSL(MAKESEGPTR(context
->SegSs
, LOWORD(context
->Esp
))))+3,
466 CX_reg(context
), iret
);
467 INT_SetRealModeContext(p
, &context16
);
471 static RMCB
*DPMI_AllocRMCB( void )
473 RMCB
*NewRMCB
= HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB
));
478 LPVOID RMCBmem
= DOSMEM_GetBlock(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 */
487 NewRMCB
->address
= MAKELONG(0, uParagraph
);
488 NewRMCB
->next
= FirstRMCB
;
495 static void AllocRMCB( CONTEXT86
*context
)
497 RMCB
*NewRMCB
= DPMI_AllocRMCB();
499 TRACE("Function to call: %04x:%04x\n", (WORD
)context
->SegDs
, SI_reg(context
) );
503 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
504 NewRMCB
->proc_ofs
= LOWORD(context
->Esi
);
505 NewRMCB
->proc_sel
= context
->SegDs
;
506 NewRMCB
->regs_ofs
= LOWORD(context
->Edi
);
507 NewRMCB
->regs_sel
= context
->SegEs
;
508 SET_LOWORD( context
->Ecx
, HIWORD(NewRMCB
->address
) );
509 SET_LOWORD( context
->Edx
, LOWORD(NewRMCB
->address
) );
513 SET_LOWORD( context
->Eax
, 0x8015 ); /* callback unavailable */
519 FARPROC16 WINAPI
DPMI_AllocInternalRMCB( RMCBPROC proc
)
521 RMCB
*NewRMCB
= DPMI_AllocRMCB();
524 NewRMCB
->proc_ofs
= (DWORD
)proc
;
525 NewRMCB
->proc_sel
= 0;
526 NewRMCB
->regs_ofs
= 0;
527 NewRMCB
->regs_sel
= 0;
528 return (FARPROC16
)(NewRMCB
->address
);
534 static int DPMI_FreeRMCB( DWORD address
)
536 RMCB
*CurrRMCB
= FirstRMCB
;
537 RMCB
*PrevRMCB
= NULL
;
539 while (CurrRMCB
&& (CurrRMCB
->address
!= address
))
542 CurrRMCB
= CurrRMCB
->next
;
547 PrevRMCB
->next
= CurrRMCB
->next
;
549 FirstRMCB
= CurrRMCB
->next
;
550 DOSMEM_FreeBlock(DOSMEM_MapRealToLinear(CurrRMCB
->address
));
551 HeapFree(GetProcessHeap(), 0, CurrRMCB
);
558 static void FreeRMCB( CONTEXT86
*context
)
560 FIXME("callback address: %04x:%04x\n",
561 CX_reg(context
), DX_reg(context
));
563 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context
), CX_reg(context
)))) {
564 SET_LOWORD( context
->Eax
, 0x8024 ); /* invalid callback address */
570 void WINAPI
DPMI_FreeInternalRMCB( FARPROC16 proc
)
572 DPMI_FreeRMCB( (DWORD
)proc
);
576 /* (see dosmem.c, function DOSMEM_InitDPMI) */
578 static void StartPM( CONTEXT86
*context
, LPDOSTASK lpDosTask
)
580 UINT16 cs
, ss
, ds
, es
;
582 DWORD psp_ofs
= (DWORD
)(lpDosTask
->psp_seg
<<4);
583 PDB16
*psp
= (PDB16
*)psp_ofs
;
584 HANDLE16 env_seg
= psp
->environment
;
585 unsigned char selflags
= WINE_LDT_FLAGS_DATA
;
587 RESET_CFLAG(context
);
588 lpDosTask
->dpmi_flag
= AX_reg(context
);
589 /* our mode switch wrapper have placed the desired CS into DX */
590 cs
= SELECTOR_AllocBlock( (void *)(DX_reg(context
)<<4), 0x10000, WINE_LDT_FLAGS_CODE
);
591 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
592 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
593 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
594 32-bit code using this stack. */
595 if (lpDosTask
->dpmi_flag
& 1) selflags
|= WINE_LDT_FLAGS_32BIT
;
596 ss
= SELECTOR_AllocBlock( (void *)(context
->SegSs
<<4), 0x10000, selflags
);
597 /* do the same for the data segments, just in case */
598 if (context
->SegDs
== context
->SegSs
) ds
= ss
;
599 else ds
= SELECTOR_AllocBlock( (void *)(context
->SegDs
<<4), 0x10000, selflags
);
600 es
= SELECTOR_AllocBlock( psp
, 0x100, selflags
);
601 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
602 psp
->environment
= SELECTOR_AllocBlock( (void *)(env_seg
<<4), 0x10000, WINE_LDT_FLAGS_DATA
);
605 pm_ctx
.SegCs
= DOSMEM_dpmi_sel
;
606 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
614 TRACE("DOS program is now entering protected mode\n");
615 wine_call_to_16_regs_short(&pm_ctx
, 0);
617 /* in the current state of affairs, we won't ever actually return here... */
618 /* we should have int21/ah=4c do it someday, though... */
620 FreeSelector16(psp
->environment
);
621 psp
->environment
= env_seg
;
623 if (ds
!= ss
) FreeSelector16(ds
);
628 /* DPMI Raw Mode Switch handler */
631 void WINAPI
DPMI_RawModeSwitch( SIGCONTEXT
*context
)
633 LPDOSTASK lpDosTask
= MZ_Current();
638 /* we could probably start a DPMI-only dosmod task here, but I doubt
639 anything other than real DOS apps want to call raw mode switch */
640 ERR("attempting raw mode switch without DOS task!\n");
643 /* initialize real-mode context as per spec */
644 memset(&rm_ctx
, 0, sizeof(rm_ctx
));
645 rm_ctx
.SegDs
= AX_sig(context
);
646 rm_ctx
.SegEs
= CX_sig(context
);
647 rm_ctx
.SegSs
= DX_sig(context
);
648 rm_ctx
.Esp
= EBX_sig(context
);
649 rm_ctx
.SegCs
= SI_sig(context
);
650 rm_ctx
.Eip
= EDI_sig(context
);
651 rm_ctx
.Ebp
= EBP_sig(context
);
654 rm_ctx
.EFlags
= EFL_sig(context
); /* 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 */
663 /* if the sync was lost, there's no way to recover */
667 /* alter protected-mode context as per spec */
668 DS_sig(context
) = LOWORD(rm_ctx
.Eax
);
669 ES_sig(context
) = LOWORD(rm_ctx
.Ecx
);
670 SS_sig(context
) = LOWORD(rm_ctx
.Edx
);
671 ESP_sig(context
) = rm_ctx
.Ebx
;
672 CS_sig(context
) = LOWORD(rm_ctx
.Esi
);
673 EIP_sig(context
) = rm_ctx
.Edi
;
674 EBP_sig(context
) = rm_ctx
.Ebp
;
678 /* Return to new address and hope that we didn't mess up */
679 TRACE("re-entering protected mode at %04x:%08lx\n",
680 CS_sig(context
), EIP_sig(context
));
685 /**********************************************************************
688 * Handler for int 31h (DPMI).
691 void WINAPI
INT_Int31Handler( CONTEXT86
*context
)
694 * Note: For Win32s processes, the whole linear address space is
695 * shifted by 0x10000 relative to the OS linear address space.
696 * See the comment in msdos/vxd.c.
701 LPDOSTASK lpDosTask
= MZ_Current();
703 if (ISV86(context
) && lpDosTask
) {
704 /* Called from real mode, check if it's our wrapper */
705 TRACE("called from real mode\n");
706 if (context
->SegCs
==DOSMEM_dpmi_seg
) {
707 /* This is the protected mode switch */
708 StartPM(context
,lpDosTask
);
711 if (context
->SegCs
==DOSMEM_xms_seg
) {
712 /* This is the XMS driver entry point */
713 XMS_Handler(context
);
718 RMCB
*CurrRMCB
= FirstRMCB
;
720 while (CurrRMCB
&& (HIWORD(CurrRMCB
->address
) != context
->SegCs
))
721 CurrRMCB
= CurrRMCB
->next
;
724 /* RMCB call, propagate to protected-mode handler */
725 DPMI_CallRMCBProc(context
, CurrRMCB
, lpDosTask
->dpmi_flag
);
731 RESET_CFLAG(context
);
732 switch(AX_reg(context
))
734 case 0x0000: /* Allocate LDT descriptors */
735 TRACE("allocate LDT descriptors (%d)\n",CX_reg(context
));
736 if (!(context
->Eax
= AllocSelectorArray16( CX_reg(context
) )))
739 context
->Eax
= 0x8011; /* descriptor unavailable */
742 TRACE("success, array starts at 0x%04x\n",AX_reg(context
));
745 case 0x0001: /* Free LDT descriptor */
746 TRACE("free LDT descriptor (0x%04x)\n",BX_reg(context
));
747 if (FreeSelector16( BX_reg(context
) ))
749 context
->Eax
= 0x8022; /* invalid selector */
754 /* If a segment register contains the selector being freed, */
755 /* set it to zero. */
756 if (!((context
->SegDs
^BX_reg(context
)) & ~3)) context
->SegDs
= 0;
757 if (!((context
->SegEs
^BX_reg(context
)) & ~3)) context
->SegEs
= 0;
758 if (!((context
->SegFs
^BX_reg(context
)) & ~3)) context
->SegFs
= 0;
759 if (!((context
->SegGs
^BX_reg(context
)) & ~3)) context
->SegGs
= 0;
763 case 0x0002: /* Real mode segment to descriptor */
764 TRACE("real mode segment to descriptor (0x%04x)\n",BX_reg(context
));
766 WORD entryPoint
= 0; /* KERNEL entry point for descriptor */
767 switch(BX_reg(context
))
769 case 0x0000: entryPoint
= 183; break; /* __0000H */
770 case 0x0040: entryPoint
= 193; break; /* __0040H */
771 case 0xa000: entryPoint
= 174; break; /* __A000H */
772 case 0xb000: entryPoint
= 181; break; /* __B000H */
773 case 0xb800: entryPoint
= 182; break; /* __B800H */
774 case 0xc000: entryPoint
= 195; break; /* __C000H */
775 case 0xd000: entryPoint
= 179; break; /* __D000H */
776 case 0xe000: entryPoint
= 190; break; /* __E000H */
777 case 0xf000: entryPoint
= 194; break; /* __F000H */
779 context
->Eax
= DOSMEM_AllocSelector(BX_reg(context
));
783 context
->Eax
= LOWORD(GetProcAddress16( GetModuleHandle16( "KERNEL" ),
784 (LPCSTR
)(ULONG_PTR
)entryPoint
));
788 case 0x0003: /* Get next selector increment */
789 TRACE("get selector increment (__AHINCR)\n");
790 context
->Eax
= __AHINCR
;
793 case 0x0004: /* Lock selector (not supported) */
794 FIXME("lock selector not supported\n");
795 context
->Eax
= 0; /* FIXME: is this a correct return value? */
798 case 0x0005: /* Unlock selector (not supported) */
799 FIXME("unlock selector not supported\n");
800 context
->Eax
= 0; /* FIXME: is this a correct return value? */
803 case 0x0006: /* Get selector base address */
804 TRACE("get selector base address (0x%04x)\n",BX_reg(context
));
805 if (!(dw
= GetSelectorBase( BX_reg(context
) )))
807 context
->Eax
= 0x8022; /* invalid selector */
812 CX_reg(context
) = HIWORD(W32S_WINE2APP(dw
));
813 DX_reg(context
) = LOWORD(W32S_WINE2APP(dw
));
817 case 0x0007: /* Set selector base address */
818 TRACE("set selector base address (0x%04x,0x%08lx)\n",
820 W32S_APP2WINE(MAKELONG(DX_reg(context
),CX_reg(context
))));
821 dw
= W32S_APP2WINE(MAKELONG(DX_reg(context
), CX_reg(context
)));
823 /* app wants to access lower 64K of DOS memory, map it in now */
825 SetSelectorBase(BX_reg(context
), dw
);
828 case 0x0008: /* Set selector limit */
829 TRACE("set selector limit (0x%04x,0x%08lx)\n",BX_reg(context
),MAKELONG(DX_reg(context
),CX_reg(context
)));
830 dw
= MAKELONG( DX_reg(context
), CX_reg(context
) );
831 SetSelectorLimit16( BX_reg(context
), dw
);
834 case 0x0009: /* Set selector access rights */
835 TRACE("set selector access rights(0x%04x,0x%04x)\n",BX_reg(context
),CX_reg(context
));
836 SelectorAccessRights16( BX_reg(context
), 1, CX_reg(context
) );
839 case 0x000a: /* Allocate selector alias */
840 TRACE("allocate selector alias (0x%04x)\n",BX_reg(context
));
841 if (!(AX_reg(context
) = AllocCStoDSAlias16( BX_reg(context
) )))
843 AX_reg(context
) = 0x8011; /* descriptor unavailable */
848 case 0x000b: /* Get descriptor */
849 TRACE("get descriptor (0x%04x)\n",BX_reg(context
));
852 wine_ldt_set_base( &entry
, (void*)W32S_WINE2APP(wine_ldt_get_base(&entry
)) );
853 /* FIXME: should use ES:EDI for 32-bit clients */
854 *(LDT_ENTRY
*)MapSL( MAKESEGPTR( context
->SegEs
, LOWORD(context
->Edi
) )) = entry
;
858 case 0x000c: /* Set descriptor */
859 TRACE("set descriptor (0x%04x)\n",BX_reg(context
));
861 LDT_ENTRY entry
= *(LDT_ENTRY
*)MapSL(MAKESEGPTR( context
->SegEs
, LOWORD(context
->Edi
)));
862 wine_ldt_set_base( &entry
, (void*)W32S_APP2WINE(wine_ldt_get_base(&entry
)) );
863 wine_ldt_set_entry( LOWORD(context
->Ebx
), &entry
);
867 case 0x000d: /* Allocate specific LDT descriptor */
868 FIXME("allocate descriptor (0x%04x), stub!\n",BX_reg(context
));
869 AX_reg(context
) = 0x8011; /* descriptor unavailable */
872 case 0x0100: /* Allocate DOS memory block */
873 TRACE("allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context
));
874 dw
= GlobalDOSAlloc16((DWORD
)BX_reg(context
)<<4);
876 AX_reg(context
) = HIWORD(dw
);
877 DX_reg(context
) = LOWORD(dw
);
879 AX_reg(context
) = 0x0008; /* insufficient memory */
880 BX_reg(context
) = DOSMEM_Available()>>4;
884 case 0x0101: /* Free DOS memory block */
885 TRACE("free DOS memory block (0x%04x)\n",DX_reg(context
));
886 dw
= GlobalDOSFree16(DX_reg(context
));
888 AX_reg(context
) = 0x0009; /* memory block address invalid */
892 case 0x0200: /* get real mode interrupt vector */
893 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
897 case 0x0201: /* set real mode interrupt vector */
898 FIXME("set realmode interrupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context
),CX_reg(context
),DX_reg(context
));
901 case 0x0204: /* Get protected mode interrupt vector */
902 TRACE("get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context
));
903 dw
= (DWORD
)INT_GetPMHandler( BL_reg(context
) );
904 CX_reg(context
) = HIWORD(dw
);
905 DX_reg(context
) = LOWORD(dw
);
908 case 0x0205: /* Set protected mode interrupt vector */
909 TRACE("set protected mode interrupt handler (0x%02x,%p), stub!\n",
910 BL_reg(context
),MapSL(MAKESEGPTR(CX_reg(context
),DX_reg(context
))));
911 INT_SetPMHandler( BL_reg(context
),
912 (FARPROC16
)MAKESEGPTR( CX_reg(context
), DX_reg(context
) ));
915 case 0x0300: /* Simulate real mode interrupt */
916 CallRMInt( context
);
919 case 0x0301: /* Call real mode procedure with far return */
920 CallRMProc( context
, FALSE
);
923 case 0x0302: /* Call real mode procedure with interrupt return */
924 CallRMProc( context
, TRUE
);
927 case 0x0303: /* Allocate Real Mode Callback Address */
928 AllocRMCB( context
);
931 case 0x0304: /* Free Real Mode Callback Address */
935 case 0x0305: /* Get State Save/Restore Addresses */
936 TRACE("get state save/restore addresses\n");
937 /* we probably won't need this kind of state saving */
939 /* real mode: just point to the lret */
940 BX_reg(context
) = DOSMEM_wrap_seg
;
942 /* protected mode: don't have any handler yet... */
943 FIXME("no protected-mode dummy state save/restore handler yet\n");
948 case 0x0306: /* Get Raw Mode Switch Addresses */
949 TRACE("get raw mode switch addresses\n");
950 /* real mode, point to standard DPMI return wrapper */
951 BX_reg(context
) = DOSMEM_wrap_seg
;
953 /* protected mode, point to DPMI call wrapper */
954 SI_reg(context
) = DOSMEM_dpmi_sel
;
955 context
->Edi
= 8; /* offset of the INT 0x31 call */
957 case 0x0400: /* Get DPMI version */
958 TRACE("get DPMI version\n");
963 AX_reg(context
) = 0x005a; /* DPMI version 0.90 */
964 BX_reg(context
) = 0x0005; /* Flags: 32-bit, virtual memory */
965 CL_reg(context
) = si
.wProcessorLevel
;
966 DX_reg(context
) = 0x0102; /* Master/slave interrupt controller base*/
969 case 0x0500: /* Get free memory information */
970 TRACE("get free memory information\n");
974 mmi
.dwSize
= sizeof(mmi
);
976 ptr
= MapSL(MAKESEGPTR(context
->SegEs
,DI_reg(context
)));
977 /* the layout is just the same as MEMMANINFO, but without
980 memcpy(ptr
,((char*)&mmi
)+4,sizeof(mmi
)-4);
983 case 0x0501: /* Allocate memory block */
984 TRACE("allocate memory block (%ld)\n",MAKELONG(CX_reg(context
),BX_reg(context
)));
985 if (!(ptr
= (BYTE
*)DPMI_xalloc(MAKELONG(CX_reg(context
), BX_reg(context
)))))
987 AX_reg(context
) = 0x8012; /* linear memory not available */
990 BX_reg(context
) = SI_reg(context
) = HIWORD(W32S_WINE2APP(ptr
));
991 CX_reg(context
) = DI_reg(context
) = LOWORD(W32S_WINE2APP(ptr
));
995 case 0x0502: /* Free memory block */
996 TRACE("free memory block (0x%08lx)\n",
997 W32S_APP2WINE(MAKELONG(DI_reg(context
),SI_reg(context
))));
998 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context
), SI_reg(context
))) );
1001 case 0x0503: /* Resize memory block */
1002 TRACE("resize memory block (0x%08lx,%ld)\n",
1003 W32S_APP2WINE(MAKELONG(DI_reg(context
),SI_reg(context
))),
1004 MAKELONG(CX_reg(context
),BX_reg(context
)));
1005 if (!(ptr
= (BYTE
*)DPMI_xrealloc(
1006 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context
),SI_reg(context
))),
1007 MAKELONG(CX_reg(context
),BX_reg(context
)))))
1009 AX_reg(context
) = 0x8012; /* linear memory not available */
1012 BX_reg(context
) = SI_reg(context
) = HIWORD(W32S_WINE2APP(ptr
));
1013 CX_reg(context
) = DI_reg(context
) = LOWORD(W32S_WINE2APP(ptr
));
1017 case 0x0507: /* Modify page attributes */
1018 FIXME("modify page attributes unimplemented\n");
1019 break; /* Just ignore it */
1021 case 0x0600: /* Lock linear region */
1022 FIXME("lock linear region unimplemented\n");
1023 break; /* Just ignore it */
1025 case 0x0601: /* Unlock linear region */
1026 FIXME("unlock linear region unimplemented\n");
1027 break; /* Just ignore it */
1029 case 0x0602: /* Unlock real-mode region */
1030 FIXME("unlock realmode region unimplemented\n");
1031 break; /* Just ignore it */
1033 case 0x0603: /* Lock real-mode region */
1034 FIXME("lock realmode region unimplemented\n");
1035 break; /* Just ignore it */
1037 case 0x0604: /* Get page size */
1038 TRACE("get pagesize\n");
1039 BX_reg(context
) = 0;
1040 CX_reg(context
) = getpagesize();
1043 case 0x0702: /* Mark page as demand-paging candidate */
1044 FIXME("mark page as demand-paging candidate\n");
1045 break; /* Just ignore it */
1047 case 0x0703: /* Discard page contents */
1048 FIXME("discard page contents\n");
1049 break; /* Just ignore it */
1051 case 0x0800: /* Physical address mapping */
1052 FIXME("map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context
),BX_reg(context
)));
1053 if(!(ptr
=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context
),BX_reg(context
)))))
1055 AX_reg(context
) = 0x8021;
1060 BX_reg(context
) = HIWORD(W32S_WINE2APP(ptr
));
1061 CX_reg(context
) = LOWORD(W32S_WINE2APP(ptr
));
1062 RESET_CFLAG(context
);
1067 INT_BARF( context
, 0x31 );
1068 AX_reg(context
) = 0x8001; /* unsupported function */