4 * Copyright 1995 Alexandre Julliard
10 #include "wine/winbase16.h"
12 #include "builtin16.h"
19 #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
,int 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((REALMODECALL
*)PTR_SEG_OFF_TO_LIN( rmcb
->regs_sel
, rmcb
->regs_ofs
), context
);
263 ss
= SELECTOR_AllocBlock( (void *)(context
->SegSs
<<4), 0x10000, SEGMENT_DATA
, FALSE
, FALSE
);
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 CallTo16RegisterShort(&ctx
, 0);
293 INT_GetRealModeContext((REALMODECALL
*)PTR_SEG_OFF_TO_LIN( 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
= (REALMODECALL
*)PTR_SEG_OFF_TO_LIN( context
->SegEs
,
421 INT_GetRealModeContext( call
, &realmode_ctx
);
423 /* we need to check if a real-mode program has hooked the interrupt */
424 if (HIWORD(rm_int
)!=0xF000) {
425 /* yup, which means we need to switch to real mode... */
426 realmode_ctx
.SegCs
= HIWORD(rm_int
);
427 realmode_ctx
.Eip
= LOWORD(rm_int
);
428 if (DPMI_CallRMProc( &realmode_ctx
, NULL
, 0, TRUE
))
431 RESET_CFLAG(context
);
432 /* use the IP we have instead of BL_reg, in case some apps
433 decide to move interrupts around for whatever reason... */
434 if (INT_RealModeInterrupt( LOWORD(rm_int
)/4, &realmode_ctx
))
436 if (context
->EFlags
& 1) {
437 FIXME("%02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
438 BL_reg(context
), realmode_ctx
.Eax
, realmode_ctx
.Ebx
,
439 realmode_ctx
.Ecx
, realmode_ctx
.Edx
);
440 FIXME(" ESI=%08lx EDI=%08lx DS=%04lx ES=%04lx\n",
441 realmode_ctx
.Esi
, realmode_ctx
.Edi
,
442 realmode_ctx
.SegDs
, realmode_ctx
.SegEs
);
445 INT_SetRealModeContext( call
, &realmode_ctx
);
449 static void CallRMProc( CONTEXT86
*context
, int iret
)
451 REALMODECALL
*p
= (REALMODECALL
*)PTR_SEG_OFF_TO_LIN( context
->SegEs
, DI_reg(context
) );
454 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
455 p
->eax
, p
->ebx
, p
->ecx
, p
->edx
);
456 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
457 p
->esi
, p
->edi
, p
->es
, p
->ds
, p
->cs
, p
->ip
, CX_reg(context
), iret
?"IRET":"FAR" );
459 if (!(p
->cs
) && !(p
->ip
)) { /* remove this check
460 if Int21/6501 case map function
461 has been implemented */
465 INT_GetRealModeContext(p
, &context16
);
466 DPMI_CallRMProc( &context16
, ((LPWORD
)PTR_SEG_OFF_TO_LIN(context
->SegSs
, LOWORD(context
->Esp
)))+3,
467 CX_reg(context
), iret
);
468 INT_SetRealModeContext(p
, &context16
);
472 static RMCB
*DPMI_AllocRMCB( void )
474 RMCB
*NewRMCB
= HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB
));
479 LPVOID RMCBmem
= DOSMEM_GetBlock(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 */
488 NewRMCB
->address
= MAKELONG(0, uParagraph
);
489 NewRMCB
->next
= FirstRMCB
;
496 static void AllocRMCB( CONTEXT86
*context
)
498 RMCB
*NewRMCB
= DPMI_AllocRMCB();
500 TRACE("Function to call: %04x:%04x\n", (WORD
)context
->SegDs
, SI_reg(context
) );
504 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
505 NewRMCB
->proc_ofs
= LOWORD(context
->Esi
);
506 NewRMCB
->proc_sel
= context
->SegDs
;
507 NewRMCB
->regs_ofs
= LOWORD(context
->Edi
);
508 NewRMCB
->regs_sel
= context
->SegEs
;
509 SET_LOWORD( context
->Ecx
, HIWORD(NewRMCB
->address
) );
510 SET_LOWORD( context
->Edx
, LOWORD(NewRMCB
->address
) );
514 SET_LOWORD( context
->Eax
, 0x8015 ); /* callback unavailable */
520 FARPROC16 WINAPI
DPMI_AllocInternalRMCB( RMCBPROC proc
)
522 RMCB
*NewRMCB
= DPMI_AllocRMCB();
525 NewRMCB
->proc_ofs
= (DWORD
)proc
;
526 NewRMCB
->proc_sel
= 0;
527 NewRMCB
->regs_ofs
= 0;
528 NewRMCB
->regs_sel
= 0;
529 return (FARPROC16
)(NewRMCB
->address
);
535 static int DPMI_FreeRMCB( DWORD address
)
537 RMCB
*CurrRMCB
= FirstRMCB
;
538 RMCB
*PrevRMCB
= NULL
;
540 while (CurrRMCB
&& (CurrRMCB
->address
!= address
))
543 CurrRMCB
= CurrRMCB
->next
;
548 PrevRMCB
->next
= CurrRMCB
->next
;
550 FirstRMCB
= CurrRMCB
->next
;
551 DOSMEM_FreeBlock(DOSMEM_MapRealToLinear(CurrRMCB
->address
));
552 HeapFree(GetProcessHeap(), 0, CurrRMCB
);
559 static void FreeRMCB( CONTEXT86
*context
)
561 FIXME("callback address: %04x:%04x\n",
562 CX_reg(context
), DX_reg(context
));
564 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context
), CX_reg(context
)))) {
565 SET_LOWORD( context
->Eax
, 0x8024 ); /* invalid callback address */
571 void WINAPI
DPMI_FreeInternalRMCB( FARPROC16 proc
)
573 DPMI_FreeRMCB( (DWORD
)proc
);
577 /* (see dosmem.c, function DOSMEM_InitDPMI) */
579 static void StartPM( CONTEXT86
*context
, LPDOSTASK lpDosTask
)
581 UINT16 cs
, ss
, ds
, es
;
583 DWORD psp_ofs
= (DWORD
)(lpDosTask
->psp_seg
<<4);
584 PDB16
*psp
= (PDB16
*)psp_ofs
;
585 HANDLE16 env_seg
= psp
->environment
;
588 RESET_CFLAG(context
);
589 lpDosTask
->dpmi_flag
= AX_reg(context
);
590 is32
= lpDosTask
->dpmi_flag
& 1;
591 /* our mode switch wrapper have placed the desired CS into DX */
592 cs
= SELECTOR_AllocBlock( (void *)(DX_reg(context
)<<4), 0x10000, SEGMENT_CODE
, FALSE
, FALSE
);
593 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
594 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
595 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
596 32-bit code using this stack. */
597 ss
= SELECTOR_AllocBlock( (void *)(context
->SegSs
<<4), 0x10000, SEGMENT_DATA
, is32
, FALSE
);
598 /* do the same for the data segments, just in case */
599 if (context
->SegDs
== context
->SegSs
) ds
= ss
;
600 else ds
= SELECTOR_AllocBlock( (void *)(context
->SegDs
<<4), 0x10000, SEGMENT_DATA
, is32
, FALSE
);
601 es
= SELECTOR_AllocBlock( psp
, 0x100, SEGMENT_DATA
, is32
, FALSE
);
602 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
603 psp
->environment
= SELECTOR_AllocBlock( (void *)(env_seg
<<4),
604 0x10000, SEGMENT_DATA
, FALSE
, FALSE
);
607 pm_ctx
.SegCs
= DOSMEM_dpmi_sel
;
608 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
616 TRACE("DOS program is now entering protected mode\n");
617 CallTo16RegisterShort(&pm_ctx
, 0);
619 /* in the current state of affairs, we won't ever actually return here... */
620 /* we should have int21/ah=4c do it someday, though... */
622 FreeSelector16(psp
->environment
);
623 psp
->environment
= env_seg
;
625 if (ds
!= ss
) FreeSelector16(ds
);
630 /* DPMI Raw Mode Switch handler */
633 void WINAPI
DPMI_RawModeSwitch( SIGCONTEXT
*context
)
635 LPDOSTASK lpDosTask
= MZ_Current();
640 /* we could probably start a DPMI-only dosmod task here, but I doubt
641 anything other than real DOS apps want to call raw mode switch */
642 ERR("attempting raw mode switch without DOS task!\n");
645 /* initialize real-mode context as per spec */
646 memset(&rm_ctx
, 0, sizeof(rm_ctx
));
647 rm_ctx
.SegDs
= AX_sig(context
);
648 rm_ctx
.SegEs
= CX_sig(context
);
649 rm_ctx
.SegSs
= DX_sig(context
);
650 rm_ctx
.Esp
= EBX_sig(context
);
651 rm_ctx
.SegCs
= SI_sig(context
);
652 rm_ctx
.Eip
= EDI_sig(context
);
653 rm_ctx
.Ebp
= EBP_sig(context
);
656 rm_ctx
.EFlags
= EFL_sig(context
); /* at least we need the IF flag */
658 /* enter real mode again */
659 TRACE("re-entering real mode at %04lx:%04lx\n",rm_ctx
.SegCs
,rm_ctx
.Eip
);
660 ret
= DOSVM_Enter( &rm_ctx
);
661 /* when the real-mode stuff call its mode switch address,
662 DOSVM_Enter will return and we will continue here */
665 /* if the sync was lost, there's no way to recover */
669 /* alter protected-mode context as per spec */
670 DS_sig(context
) = LOWORD(rm_ctx
.Eax
);
671 ES_sig(context
) = LOWORD(rm_ctx
.Ecx
);
672 SS_sig(context
) = LOWORD(rm_ctx
.Edx
);
673 ESP_sig(context
) = rm_ctx
.Ebx
;
674 CS_sig(context
) = LOWORD(rm_ctx
.Esi
);
675 EIP_sig(context
) = rm_ctx
.Edi
;
676 EBP_sig(context
) = rm_ctx
.Ebp
;
680 /* Return to new address and hope that we didn't mess up */
681 TRACE("re-entering protected mode at %04x:%08lx\n",
682 CS_sig(context
), EIP_sig(context
));
687 /**********************************************************************
690 * Handler for int 31h (DPMI).
693 void WINAPI
INT_Int31Handler( CONTEXT86
*context
)
696 * Note: For Win32s processes, the whole linear address space is
697 * shifted by 0x10000 relative to the OS linear address space.
698 * See the comment in msdos/vxd.c.
700 DWORD offset
= W32S_APPLICATION() ? W32S_OFFSET
: 0;
705 LPDOSTASK lpDosTask
= MZ_Current();
707 if (ISV86(context
) && lpDosTask
) {
708 /* Called from real mode, check if it's our wrapper */
709 TRACE("called from real mode\n");
710 if (context
->SegCs
==DOSMEM_dpmi_seg
) {
711 /* This is the protected mode switch */
712 StartPM(context
,lpDosTask
);
715 if (context
->SegCs
==DOSMEM_xms_seg
) {
716 /* This is the XMS driver entry point */
717 XMS_Handler(context
);
722 RMCB
*CurrRMCB
= FirstRMCB
;
724 while (CurrRMCB
&& (HIWORD(CurrRMCB
->address
) != context
->SegCs
))
725 CurrRMCB
= CurrRMCB
->next
;
728 /* RMCB call, propagate to protected-mode handler */
729 DPMI_CallRMCBProc(context
, CurrRMCB
, lpDosTask
->dpmi_flag
);
735 RESET_CFLAG(context
);
736 switch(AX_reg(context
))
738 case 0x0000: /* Allocate LDT descriptors */
739 TRACE("allocate LDT descriptors (%d)\n",CX_reg(context
));
740 if (!(context
->Eax
= AllocSelectorArray16( CX_reg(context
) )))
743 context
->Eax
= 0x8011; /* descriptor unavailable */
746 TRACE("success, array starts at 0x%04x\n",AX_reg(context
));
749 case 0x0001: /* Free LDT descriptor */
750 TRACE("free LDT descriptor (0x%04x)\n",BX_reg(context
));
751 if (FreeSelector16( BX_reg(context
) ))
753 context
->Eax
= 0x8022; /* invalid selector */
758 /* If a segment register contains the selector being freed, */
759 /* set it to zero. */
760 if (!((context
->SegDs
^BX_reg(context
)) & ~3)) context
->SegDs
= 0;
761 if (!((context
->SegEs
^BX_reg(context
)) & ~3)) context
->SegEs
= 0;
762 if (!((context
->SegFs
^BX_reg(context
)) & ~3)) context
->SegFs
= 0;
763 if (!((context
->SegGs
^BX_reg(context
)) & ~3)) context
->SegGs
= 0;
767 case 0x0002: /* Real mode segment to descriptor */
768 TRACE("real mode segment to descriptor (0x%04x)\n",BX_reg(context
));
770 WORD entryPoint
= 0; /* KERNEL entry point for descriptor */
771 switch(BX_reg(context
))
773 case 0x0000: entryPoint
= 183; break; /* __0000H */
774 case 0x0040: entryPoint
= 193; break; /* __0040H */
775 case 0xa000: entryPoint
= 174; break; /* __A000H */
776 case 0xb000: entryPoint
= 181; break; /* __B000H */
777 case 0xb800: entryPoint
= 182; break; /* __B800H */
778 case 0xc000: entryPoint
= 195; break; /* __C000H */
779 case 0xd000: entryPoint
= 179; break; /* __D000H */
780 case 0xe000: entryPoint
= 190; break; /* __E000H */
781 case 0xf000: entryPoint
= 194; break; /* __F000H */
783 context
->Eax
= DOSMEM_AllocSelector(BX_reg(context
));
787 context
->Eax
= LOWORD(NE_GetEntryPoint( GetModuleHandle16( "KERNEL" ),
792 case 0x0003: /* Get next selector increment */
793 TRACE("get selector increment (__AHINCR)\n");
794 context
->Eax
= __AHINCR
;
797 case 0x0004: /* Lock selector (not supported) */
798 FIXME("lock selector not supported\n");
799 context
->Eax
= 0; /* FIXME: is this a correct return value? */
802 case 0x0005: /* Unlock selector (not supported) */
803 FIXME("unlock selector not supported\n");
804 context
->Eax
= 0; /* FIXME: is this a correct return value? */
807 case 0x0006: /* Get selector base address */
808 TRACE("get selector base address (0x%04x)\n",BX_reg(context
));
809 if (!(dw
= GetSelectorBase( BX_reg(context
) )))
811 context
->Eax
= 0x8022; /* invalid selector */
816 CX_reg(context
) = HIWORD(W32S_WINE2APP(dw
, offset
));
817 DX_reg(context
) = LOWORD(W32S_WINE2APP(dw
, offset
));
821 case 0x0007: /* Set selector base address */
822 TRACE("set selector base address (0x%04x,0x%08lx)\n",
824 W32S_APP2WINE(MAKELONG(DX_reg(context
),CX_reg(context
)), offset
));
825 dw
= W32S_APP2WINE(MAKELONG(DX_reg(context
), CX_reg(context
)), offset
);
827 /* app wants to access lower 64K of DOS memory, map it in now */
829 SetSelectorBase(BX_reg(context
), dw
);
832 case 0x0008: /* Set selector limit */
833 TRACE("set selector limit (0x%04x,0x%08lx)\n",BX_reg(context
),MAKELONG(DX_reg(context
),CX_reg(context
)));
834 dw
= MAKELONG( DX_reg(context
), CX_reg(context
) );
835 SetSelectorLimit16( BX_reg(context
), dw
);
838 case 0x0009: /* Set selector access rights */
839 TRACE("set selector access rights(0x%04x,0x%04x)\n",BX_reg(context
),CX_reg(context
));
840 SelectorAccessRights16( BX_reg(context
), 1, CX_reg(context
) );
843 case 0x000a: /* Allocate selector alias */
844 TRACE("allocate selector alias (0x%04x)\n",BX_reg(context
));
845 if (!(AX_reg(context
) = AllocCStoDSAlias16( BX_reg(context
) )))
847 AX_reg(context
) = 0x8011; /* descriptor unavailable */
852 case 0x000b: /* Get descriptor */
853 TRACE("get descriptor (0x%04x)\n",BX_reg(context
));
856 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context
) ), &entry
);
857 entry
.base
= W32S_WINE2APP(entry
.base
, offset
);
859 /* FIXME: should use ES:EDI for 32-bit clients */
860 LDT_EntryToBytes( PTR_SEG_OFF_TO_LIN( context
->SegEs
,
861 DI_reg(context
) ), &entry
);
865 case 0x000c: /* Set descriptor */
866 TRACE("set descriptor (0x%04x)\n",BX_reg(context
));
869 LDT_BytesToEntry( PTR_SEG_OFF_TO_LIN( context
->SegEs
,
870 DI_reg(context
) ), &entry
);
871 entry
.base
= W32S_APP2WINE(entry
.base
, offset
);
872 LDT_SetEntry( SELECTOR_TO_ENTRY( BX_reg(context
) ), &entry
);
876 case 0x000d: /* Allocate specific LDT descriptor */
877 FIXME("allocate descriptor (0x%04x), stub!\n",BX_reg(context
));
878 AX_reg(context
) = 0x8011; /* descriptor unavailable */
881 case 0x0100: /* Allocate DOS memory block */
882 TRACE("allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context
));
883 dw
= GlobalDOSAlloc16((DWORD
)BX_reg(context
)<<4);
885 AX_reg(context
) = HIWORD(dw
);
886 DX_reg(context
) = LOWORD(dw
);
888 AX_reg(context
) = 0x0008; /* insufficient memory */
889 BX_reg(context
) = DOSMEM_Available()>>4;
893 case 0x0101: /* Free DOS memory block */
894 TRACE("free DOS memory block (0x%04x)\n",DX_reg(context
));
895 dw
= GlobalDOSFree16(DX_reg(context
));
897 AX_reg(context
) = 0x0009; /* memory block address invalid */
901 case 0x0200: /* get real mode interrupt vector */
902 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
906 case 0x0201: /* set real mode interrupt vector */
907 FIXME("set realmode interupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context
),CX_reg(context
),DX_reg(context
));
910 case 0x0204: /* Get protected mode interrupt vector */
911 TRACE("get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context
));
912 dw
= (DWORD
)INT_GetPMHandler( BL_reg(context
) );
913 CX_reg(context
) = HIWORD(dw
);
914 DX_reg(context
) = LOWORD(dw
);
917 case 0x0205: /* Set protected mode interrupt vector */
918 TRACE("set protected mode interrupt handler (0x%02x,%p), stub!\n",
919 BL_reg(context
),PTR_SEG_OFF_TO_LIN(CX_reg(context
),DX_reg(context
)));
920 INT_SetPMHandler( BL_reg(context
),
921 (FARPROC16
)PTR_SEG_OFF_TO_SEGPTR( CX_reg(context
),
925 case 0x0300: /* Simulate real mode interrupt */
926 CallRMInt( context
);
929 case 0x0301: /* Call real mode procedure with far return */
930 CallRMProc( context
, FALSE
);
933 case 0x0302: /* Call real mode procedure with interrupt return */
934 CallRMProc( context
, TRUE
);
937 case 0x0303: /* Allocate Real Mode Callback Address */
938 AllocRMCB( context
);
941 case 0x0304: /* Free Real Mode Callback Address */
945 case 0x0305: /* Get State Save/Restore Addresses */
946 TRACE("get state save/restore addresses\n");
947 /* we probably won't need this kind of state saving */
949 /* real mode: just point to the lret */
950 BX_reg(context
) = DOSMEM_wrap_seg
;
952 /* protected mode: don't have any handler yet... */
953 FIXME("no protected-mode dummy state save/restore handler yet\n");
958 case 0x0306: /* Get Raw Mode Switch Addresses */
959 TRACE("get raw mode switch addresses\n");
960 /* real mode, point to standard DPMI return wrapper */
961 BX_reg(context
) = DOSMEM_wrap_seg
;
963 /* protected mode, point to DPMI call wrapper */
964 SI_reg(context
) = DOSMEM_dpmi_sel
;
965 context
->Edi
= 8; /* offset of the INT 0x31 call */
967 case 0x0400: /* Get DPMI version */
968 TRACE("get DPMI version\n");
973 AX_reg(context
) = 0x005a; /* DPMI version 0.90 */
974 BX_reg(context
) = 0x0005; /* Flags: 32-bit, virtual memory */
975 CL_reg(context
) = si
.wProcessorLevel
;
976 DX_reg(context
) = 0x0102; /* Master/slave interrupt controller base*/
979 case 0x0500: /* Get free memory information */
980 TRACE("get free memory information\n");
984 mmi
.dwSize
= sizeof(mmi
);
986 ptr
= (BYTE
*)PTR_SEG_OFF_TO_LIN(context
->SegEs
,DI_reg(context
));
987 /* the layout is just the same as MEMMANINFO, but without
990 memcpy(ptr
,((char*)&mmi
)+4,sizeof(mmi
)-4);
993 case 0x0501: /* Allocate memory block */
994 TRACE("allocate memory block (%ld)\n",MAKELONG(CX_reg(context
),BX_reg(context
)));
995 if (!(ptr
= (BYTE
*)DPMI_xalloc(MAKELONG(CX_reg(context
), BX_reg(context
)))))
997 AX_reg(context
) = 0x8012; /* linear memory not available */
1000 BX_reg(context
) = SI_reg(context
) = HIWORD(W32S_WINE2APP(ptr
, offset
));
1001 CX_reg(context
) = DI_reg(context
) = LOWORD(W32S_WINE2APP(ptr
, offset
));
1005 case 0x0502: /* Free memory block */
1006 TRACE("free memory block (0x%08lx)\n",
1007 W32S_APP2WINE(MAKELONG(DI_reg(context
),SI_reg(context
)), offset
));
1008 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context
),
1009 SI_reg(context
)), offset
) );
1012 case 0x0503: /* Resize memory block */
1013 TRACE("resize memory block (0x%08lx,%ld)\n",
1014 W32S_APP2WINE(MAKELONG(DI_reg(context
),SI_reg(context
)), offset
),
1015 MAKELONG(CX_reg(context
),BX_reg(context
)));
1016 if (!(ptr
= (BYTE
*)DPMI_xrealloc(
1017 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context
),SI_reg(context
)), offset
),
1018 MAKELONG(CX_reg(context
),BX_reg(context
)))))
1020 AX_reg(context
) = 0x8012; /* linear memory not available */
1023 BX_reg(context
) = SI_reg(context
) = HIWORD(W32S_WINE2APP(ptr
, offset
));
1024 CX_reg(context
) = DI_reg(context
) = LOWORD(W32S_WINE2APP(ptr
, offset
));
1028 case 0x0507: /* Modify page attributes */
1029 FIXME("modify page attributes unimplemented\n");
1030 break; /* Just ignore it */
1032 case 0x0600: /* Lock linear region */
1033 FIXME("lock linear region unimplemented\n");
1034 break; /* Just ignore it */
1036 case 0x0601: /* Unlock linear region */
1037 FIXME("unlock linear region unimplemented\n");
1038 break; /* Just ignore it */
1040 case 0x0602: /* Unlock real-mode region */
1041 FIXME("unlock realmode region unimplemented\n");
1042 break; /* Just ignore it */
1044 case 0x0603: /* Lock real-mode region */
1045 FIXME("lock realmode region unimplemented\n");
1046 break; /* Just ignore it */
1048 case 0x0604: /* Get page size */
1049 TRACE("get pagesize\n");
1050 BX_reg(context
) = 0;
1051 CX_reg(context
) = VIRTUAL_GetPageSize();
1054 case 0x0702: /* Mark page as demand-paging candidate */
1055 FIXME("mark page as demand-paging candidate\n");
1056 break; /* Just ignore it */
1058 case 0x0703: /* Discard page contents */
1059 FIXME("discard page contents\n");
1060 break; /* Just ignore it */
1062 case 0x0800: /* Physical address mapping */
1063 FIXME("map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context
),BX_reg(context
)));
1064 if(!(ptr
=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context
),BX_reg(context
)))))
1066 AX_reg(context
) = 0x8021;
1071 BX_reg(context
) = HIWORD(W32S_WINE2APP(ptr
, offset
));
1072 CX_reg(context
) = LOWORD(W32S_WINE2APP(ptr
, offset
));
1073 RESET_CFLAG(context
);
1078 INT_BARF( context
, 0x31 );
1079 AX_reg(context
) = 0x8001; /* unsupported function */