4 * Copyright 1995 Alexandre Julliard
10 #include "wine/winbase16.h"
17 #include "thread.h" /* for !MZ_SUPPORTED */
18 #include "stackframe.h" /* for !MZ_SUPPORTED */
20 #include "selectors.h"
23 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(int31);
27 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
29 void CreateBPB(int drive, BYTE *data, BOOL16 limited); /* defined in int21.c */
31 static void* lastvalloced = NULL;
33 /* Structure for real-mode callbacks */
57 typedef struct tagRMCB {
59 DWORD proc_ofs,proc_sel;
60 DWORD regs_ofs,regs_sel;
64 static RMCB *FirstRMCB = NULL;
68 /**********************************************************************
70 * special virtualalloc, allocates lineary monoton growing memory.
71 * (the usual VirtualAlloc does not satisfy that restriction)
74 DPMI_xalloc(int len) {
76 LPVOID oldlastv = lastvalloced;
82 ret=VirtualAlloc(lastvalloced,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
84 lastvalloced = (char *) lastvalloced + 0x10000;
85 /* we failed to allocate one in the first round.
88 if (!xflag && (lastvalloced<oldlastv)) { /* wrapped */
89 FIXME("failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len);
92 /* if we even fail to allocate something in the next
95 if ((xflag==1) && (lastvalloced >= oldlastv))
97 if ((xflag==2) && (lastvalloced < oldlastv)) {
98 FIXME("failed to allocate any memory of %d bytes!\n",len);
103 ret=VirtualAlloc(NULL,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
104 lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
109 DPMI_xfree(LPVOID ptr) {
110 VirtualFree(ptr,0,MEM_RELEASE);
113 /* FIXME: perhaps we could grow this mapped area... */
115 DPMI_xrealloc(LPVOID ptr,int newsize) {
116 MEMORY_BASIC_INFORMATION mbi;
119 newptr = DPMI_xalloc(newsize);
121 if (!VirtualQuery(ptr,&mbi,sizeof(mbi))) {
122 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
125 if (mbi.State == MEM_FREE) {
126 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
129 /* We do not shrink allocated memory. most reallocs
130 * only do grows anyway
132 if (newsize<=mbi.RegionSize)
134 memcpy(newptr,ptr,mbi.RegionSize);
139 /**********************************************************************
140 * INT_GetRealModeContext
142 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
144 EAX_reg(context) = call->eax;
145 EBX_reg(context) = call->ebx;
146 ECX_reg(context) = call->ecx;
147 EDX_reg(context) = call->edx;
148 ESI_reg(context) = call->esi;
149 EDI_reg(context) = call->edi;
150 EBP_reg(context) = call->ebp;
151 EFL_reg(context) = call->fl | V86_FLAG;
152 EIP_reg(context) = call->ip;
153 ESP_reg(context) = call->sp;
154 CS_reg(context) = call->cs;
155 DS_reg(context) = call->ds;
156 ES_reg(context) = call->es;
157 FS_reg(context) = call->fs;
158 GS_reg(context) = call->gs;
159 SS_reg(context) = call->ss;
163 /**********************************************************************
164 * INT_SetRealModeContext
166 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
168 call->eax = EAX_reg(context);
169 call->ebx = EBX_reg(context);
170 call->ecx = ECX_reg(context);
171 call->edx = EDX_reg(context);
172 call->esi = ESI_reg(context);
173 call->edi = EDI_reg(context);
174 call->ebp = EBP_reg(context);
175 call->fl = LOWORD(EFL_reg(context));
176 call->ip = LOWORD(EIP_reg(context));
177 call->sp = LOWORD(ESP_reg(context));
178 call->cs = CS_reg(context);
179 call->ds = DS_reg(context);
180 call->es = ES_reg(context);
181 call->fs = FS_reg(context);
182 call->gs = GS_reg(context);
183 call->ss = SS_reg(context);
187 /**********************************************************************
190 * This routine does the hard work of calling a callback procedure.
192 static void DPMI_CallRMCBProc( CONTEXT86 *context, RMCB *rmcb, WORD flag )
194 if (IS_SELECTOR_SYSTEM( rmcb->proc_sel )) {
195 /* Wine-internal RMCB, call directly */
196 ((RMCBPROC)rmcb->proc_ofs)(context);
202 INT_SetRealModeContext((REALMODECALL *)PTR_SEG_OFF_TO_LIN( rmcb->regs_sel, rmcb->regs_ofs ), context);
203 ss = SELECTOR_AllocBlock( DOSMEM_MemoryBase(0) + (DWORD)(SS_reg(context)<<4), 0x10000, SEGMENT_DATA, FALSE, FALSE );
205 FIXME("untested!\n");
207 /* The called proc ends with an IRET, and takes these parameters:
208 * DS:ESI = pointer to real-mode SS:SP
209 * ES:EDI = pointer to real-mode call structure
211 * ES:EDI = pointer to real-mode call structure (may be a copy)
212 * It is the proc's responsibility to change the return CS:IP in the
213 * real-mode call structure. */
215 /* 32-bit DPMI client */
217 __asm__ __volatile__(
224 ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
229 : "=d" (es), "=D" (edi), "=S" (_clobber), "=a" (_clobber), "=c" (_clobber)
230 : "0" (ss), "2" (ESP_reg(context)),
231 "4" (rmcb->regs_sel), "1" (rmcb->regs_ofs),
232 "3" (&rmcb->proc_ofs)
235 /* 16-bit DPMI client */
236 CONTEXT86 ctx = *context;
237 CS_reg(&ctx) = rmcb->proc_sel;
238 EIP_reg(&ctx) = rmcb->proc_ofs;
240 ESI_reg(&ctx) = ESP_reg(context);
241 ES_reg(&ctx) = rmcb->regs_sel;
242 EDI_reg(&ctx) = rmcb->regs_ofs;
243 /* FIXME: I'm pretty sure this isn't right */
244 Callbacks->CallRegisterShortProc(&ctx, 2);
248 SELECTOR_FreeBlock(ss, 1);
249 INT_GetRealModeContext((REALMODECALL*)PTR_SEG_OFF_TO_LIN( es, edi ), context);
251 ERR("RMCBs only implemented for i386\n");
257 /**********************************************************************
260 * This routine does the hard work of calling a real mode procedure.
262 int DPMI_CallRMProc( CONTEXT86 *context, LPWORD stack, int args, int iret )
265 LPVOID addr = NULL; /* avoid gcc warning */
266 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
267 NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
269 int alloc = 0, already = 0;
272 GlobalUnlock16( GetCurrentTask() );
274 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
275 EAX_reg(context), EBX_reg(context), ECX_reg(context), EDX_reg(context) );
276 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
277 ESI_reg(context), EDI_reg(context), ES_reg(context), DS_reg(context),
278 CS_reg(context), LOWORD(EIP_reg(context)), args, iret?"IRET":"FAR" );
282 /* there might be some code that just jumps to RMCBs or the like,
283 in which case following the jumps here might get us to a shortcut */
284 code = CTX_SEG_OFF_TO_LIN(context, CS_reg(context), EIP_reg(context));
286 case 0xe9: /* JMP NEAR */
287 EIP_reg(context) += 3 + *(WORD *)(code+1);
288 /* yeah, I know these gotos don't look good... */
289 goto callrmproc_again;
290 case 0xea: /* JMP FAR */
291 EIP_reg(context) = *(WORD *)(code+1);
292 CS_reg(context) = *(WORD *)(code+3);
293 /* ...but since the label is there anyway... */
294 goto callrmproc_again;
295 case 0xeb: /* JMP SHORT */
296 EIP_reg(context) += 2 + *(signed char *)(code+1);
297 /* ...because of other gotos below, so... */
298 goto callrmproc_again;
301 /* shortcut for chaining to internal interrupt handlers */
302 if ((CS_reg(context) == 0xF000) && iret) {
303 return INT_RealModeInterrupt( LOWORD(EIP_reg(context))/4, context);
306 /* shortcut for RMCBs */
307 CurrRMCB = FirstRMCB;
309 while (CurrRMCB && (HIWORD(CurrRMCB->address) != CS_reg(context)))
310 CurrRMCB = CurrRMCB->next;
312 if (!(CurrRMCB || pModule->lpDosTask)) {
314 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
315 TRACE("creating VM86 task\n");
316 if (!MZ_InitTask( MZ_AllocDPMITask( pModule->self ) )) {
317 ERR("could not setup VM86 task\n");
321 ERR("Actual real-mode calls not supported on this architecture!\n");
326 if (!SS_reg(context)) {
327 alloc = 1; /* allocate default stack */
328 stack16 = addr = DOSMEM_GetBlock( pModule->self, 64, (UINT16 *)&(SS_reg(context)) );
329 ESP_reg(context) = 64-2;
332 ERR("could not allocate default stack\n");
336 stack16 = CTX_SEG_OFF_TO_LIN(context, SS_reg(context), ESP_reg(context));
338 ESP_reg(context) -= (args + (iret?1:0)) * sizeof(WORD);
340 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
341 /* push flags if iret */
344 *stack16 = LOWORD(EFL_reg(context));
346 /* push return address (return to interrupt wrapper) */
347 *(--stack16) = DPMI_wrap_seg;
350 ESP_reg(context) -= 2*sizeof(WORD);
355 /* RMCB call, invoke protected-mode handler directly */
356 DPMI_CallRMCBProc(context, CurrRMCB, pModule->lpDosTask?pModule->lpDosTask->dpmi_flag:0);
357 /* check if we returned to where we thought we would */
358 if ((CS_reg(context) != DPMI_wrap_seg) ||
359 (LOWORD(EIP_reg(context)) != 0)) {
360 /* we need to continue at different address in real-mode space,
361 so we need to set it all up for real mode again */
362 goto callrmproc_again;
366 TRACE("entering real mode...\n");
367 DOSVM_Enter( context );
368 TRACE("returned from real-mode call\n");
370 /* we should never get here, but... */
371 ERR("cannot perform real-mode call\n");
374 if (alloc) DOSMEM_FreeBlock( pModule->self, addr );
379 /**********************************************************************
382 static void CallRMInt( CONTEXT86 *context )
384 CONTEXT86 realmode_ctx;
385 FARPROC16 rm_int = INT_GetRMHandler( BL_reg(context) );
386 REALMODECALL *call = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context),
388 INT_GetRealModeContext( call, &realmode_ctx );
390 /* we need to check if a real-mode program has hooked the interrupt */
391 if (HIWORD(rm_int)!=0xF000) {
392 /* yup, which means we need to switch to real mode... */
393 CS_reg(&realmode_ctx) = HIWORD(rm_int);
394 EIP_reg(&realmode_ctx) = LOWORD(rm_int);
395 if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
398 RESET_CFLAG(context);
399 /* use the IP we have instead of BL_reg, in case some apps
400 decide to move interrupts around for whatever reason... */
401 if (INT_RealModeInterrupt( LOWORD(rm_int)/4, &realmode_ctx ))
403 if (EFL_reg(context)&1) {
404 FIXME("%02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
405 BL_reg(context), EAX_reg(&realmode_ctx), EBX_reg(&realmode_ctx),
406 ECX_reg(&realmode_ctx), EDX_reg(&realmode_ctx));
407 FIXME(" ESI=%08lx EDI=%08lx DS=%04lx ES=%04lx\n",
408 ESI_reg(&realmode_ctx), EDI_reg(&realmode_ctx),
409 DS_reg(&realmode_ctx), ES_reg(&realmode_ctx) );
412 INT_SetRealModeContext( call, &realmode_ctx );
416 static void CallRMProc( CONTEXT86 *context, int iret )
418 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
421 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
422 p->eax, p->ebx, p->ecx, p->edx);
423 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
424 p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
426 if (!(p->cs) && !(p->ip)) { /* remove this check
427 if Int21/6501 case map function
428 has been implemented */
432 INT_GetRealModeContext(p, &context16);
433 DPMI_CallRMProc( &context16, ((LPWORD)PTR_SEG_OFF_TO_LIN(SS_reg(context), LOWORD(ESP_reg(context))))+3,
434 CX_reg(context), iret );
435 INT_SetRealModeContext(p, &context16);
439 static RMCB *DPMI_AllocRMCB( void )
441 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
446 LPVOID RMCBmem = DOSMEM_GetBlock(0, 4, &uParagraph);
449 *p++ = 0xcd; /* RMCB: */
450 *p++ = 0x31; /* int $0x31 */
451 /* it is the called procedure's task to change the return CS:EIP
452 the DPMI 0.9 spec states that if it doesn't, it will be called again */
454 *p++ = 0xfc; /* jmp RMCB */
455 NewRMCB->address = MAKELONG(0, uParagraph);
456 NewRMCB->next = FirstRMCB;
463 static void AllocRMCB( CONTEXT86 *context )
465 RMCB *NewRMCB = DPMI_AllocRMCB();
467 TRACE("Function to call: %04x:%04x\n", (WORD)DS_reg(context), SI_reg(context) );
471 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
472 NewRMCB->proc_ofs = SI_reg(context);
473 NewRMCB->proc_sel = DS_reg(context);
474 NewRMCB->regs_ofs = DI_reg(context);
475 NewRMCB->regs_sel = ES_reg(context);
476 SET_LOWORD( ECX_reg(context), HIWORD(NewRMCB->address) );
477 SET_LOWORD( EDX_reg(context), LOWORD(NewRMCB->address) );
481 SET_LOWORD( EAX_reg(context), 0x8015 ); /* callback unavailable */
487 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
489 RMCB *NewRMCB = DPMI_AllocRMCB();
492 NewRMCB->proc_ofs = (DWORD)proc;
493 NewRMCB->proc_sel = 0;
494 NewRMCB->regs_ofs = 0;
495 NewRMCB->regs_sel = 0;
496 return (FARPROC16)(NewRMCB->address);
502 static int DPMI_FreeRMCB( DWORD address )
504 RMCB *CurrRMCB = FirstRMCB;
505 RMCB *PrevRMCB = NULL;
507 while (CurrRMCB && (CurrRMCB->address != address))
510 CurrRMCB = CurrRMCB->next;
515 PrevRMCB->next = CurrRMCB->next;
517 FirstRMCB = CurrRMCB->next;
518 DOSMEM_FreeBlock(0, DOSMEM_MapRealToLinear(CurrRMCB->address));
519 HeapFree(GetProcessHeap(), 0, CurrRMCB);
526 static void FreeRMCB( CONTEXT86 *context )
528 FIXME("callback address: %04x:%04x\n",
529 CX_reg(context), DX_reg(context));
531 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
532 SET_LOWORD( EAX_reg(context), 0x8024 ); /* invalid callback address */
538 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
540 DPMI_FreeRMCB( (DWORD)proc );
545 /* (see loader/dos/module.c, function MZ_InitDPMI) */
547 static void StartPM( CONTEXT86 *context, LPDOSTASK lpDosTask )
549 char *base = DOSMEM_MemoryBase(0);
550 UINT16 cs, ss, ds, es;
552 DWORD psp_ofs = (DWORD)(lpDosTask->psp_seg<<4);
553 PDB16 *psp = (PDB16 *)(base + psp_ofs);
554 HANDLE16 env_seg = psp->environment;
557 RESET_CFLAG(context);
558 lpDosTask->dpmi_flag = AX_reg(context);
559 is32 = lpDosTask->dpmi_flag & 1;
560 /* our mode switch wrapper have placed the desired CS into DX */
561 cs = SELECTOR_AllocBlock( base + (DWORD)(DX_reg(context)<<4), 0x10000, SEGMENT_CODE, FALSE, FALSE );
562 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
563 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
564 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
565 32-bit code using this stack. */
566 ss = SELECTOR_AllocBlock( base + (DWORD)(SS_reg(context)<<4), 0x10000, SEGMENT_DATA, is32, FALSE );
567 /* do the same for the data segments, just in case */
568 if (DS_reg(context) == SS_reg(context)) ds = ss;
569 else ds = SELECTOR_AllocBlock( base + (DWORD)(DS_reg(context)<<4), 0x10000, SEGMENT_DATA, is32, FALSE );
570 es = SELECTOR_AllocBlock( base + psp_ofs, 0x100, SEGMENT_DATA, is32, FALSE );
571 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
572 psp->environment = SELECTOR_AllocBlock( base + (DWORD)(env_seg<<4),
573 0x10000, SEGMENT_DATA, FALSE, FALSE );
576 CS_reg(&pm_ctx) = lpDosTask->dpmi_sel;
577 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
578 EAX_reg(&pm_ctx) = ss;
579 EDX_reg(&pm_ctx) = cs;
580 DS_reg(&pm_ctx) = ds;
581 ES_reg(&pm_ctx) = es;
585 TRACE("DOS program is now entering protected mode\n");
586 Callbacks->CallRegisterShortProc(&pm_ctx, 0);
588 /* in the current state of affairs, we won't ever actually return here... */
589 /* we should have int21/ah=4c do it someday, though... */
591 SELECTOR_FreeBlock(psp->environment, 1);
592 psp->environment = env_seg;
593 SELECTOR_FreeBlock(es, 1);
594 if (ds != ss) SELECTOR_FreeBlock(ds, 1);
595 SELECTOR_FreeBlock(ss, 1);
596 SELECTOR_FreeBlock(cs, 1);
599 /* DPMI Raw Mode Switch handler */
602 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
604 LPDOSTASK lpDosTask = MZ_Current();
609 /* we could probably start a DPMI-only dosmod task here, but I doubt
610 anything other than real DOS apps want to call raw mode switch */
611 ERR("attempting raw mode switch without DOS task!\n");
614 /* initialize real-mode context as per spec */
615 memset(&rm_ctx, 0, sizeof(rm_ctx));
616 DS_reg(&rm_ctx) = AX_sig(context);
617 ES_reg(&rm_ctx) = CX_sig(context);
618 SS_reg(&rm_ctx) = DX_sig(context);
619 ESP_reg(&rm_ctx) = EBX_sig(context);
620 CS_reg(&rm_ctx) = SI_sig(context);
621 EIP_reg(&rm_ctx) = EDI_sig(context);
622 EBP_reg(&rm_ctx) = EBP_sig(context);
625 EFL_reg(&rm_ctx) = EFL_sig(context); /* at least we need the IF flag */
627 /* enter real mode again */
628 TRACE("re-entering real mode at %04lx:%04lx\n",
629 CS_reg(&rm_ctx),EIP_reg(&rm_ctx));
630 ret = DOSVM_Enter( &rm_ctx );
631 /* when the real-mode stuff call its mode switch address,
632 DOSVM_Enter will return and we will continue here */
635 /* if the sync was lost, there's no way to recover */
639 /* alter protected-mode context as per spec */
640 DS_sig(context) = AX_reg(&rm_ctx);
641 ES_sig(context) = CX_reg(&rm_ctx);
642 SS_sig(context) = DX_reg(&rm_ctx);
643 ESP_sig(context) = EBX_reg(&rm_ctx);
644 CS_sig(context) = SI_reg(&rm_ctx);
645 EIP_sig(context) = EDI_reg(&rm_ctx);
646 EBP_sig(context) = EBP_reg(&rm_ctx);
650 /* Return to new address and hope that we didn't mess up */
651 TRACE("re-entering protected mode at %04x:%08lx\n",
652 CS_sig(context), EIP_sig(context));
658 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
660 ERR("don't even think about DPMI raw mode switch without DOS support!\n");
666 #define DOS_APP_ISDOS(addr,base) ((addr) < 0x110000)
667 #define DOS_WINE_ISDOS(addr,base) (((addr) >= (base)) && ((addr) < (base) + 0x110000))
668 #define DOS_UC_APPTOWINE(addr,base) ((addr) + (base))
669 #define DOS_UC_WINETOAPP(addr,base) ((addr) - (base))
670 #define DOS_APPTOWINE(addr,base) (DOS_APP_ISDOS(addr,base) ? DOS_UC_APPTOWINE(addr,base) : (addr))
671 #define DOS_WINETOAPP(addr,base) (DOS_WINE_ISDOS(addr,base) ? DOS_UC_WINETOAPP(addr,base) : (addr))
672 #define DOS_BADLIMIT(addr,base,limit) \
673 ((limit == 0xffffffff) || /* disallow "fat DS" for now */ \
674 (DOS_WINE_ISDOS(addr,base) && \
675 ((addr) + (limit) > (base) + 0x110000)))
677 /**********************************************************************
680 * Handler for int 31h (DPMI).
683 void WINAPI INT_Int31Handler( CONTEXT86 *context )
686 * Note: For Win32s processes, the whole linear address space is
687 * shifted by 0x10000 relative to the OS linear address space.
688 * See the comment in msdos/vxd.c.
690 DWORD offset = W32S_APPLICATION() ? W32S_OFFSET : 0;
695 LPDOSTASK lpDosTask = MZ_Current();
698 if (ISV86(context) && lpDosTask) {
699 /* Called from real mode, check if it's our wrapper */
700 TRACE("called from real mode\n");
701 if (CS_reg(context)==lpDosTask->dpmi_seg) {
702 /* This is the protected mode switch */
703 StartPM(context,lpDosTask);
706 if (CS_reg(context)==lpDosTask->xms_seg) {
707 /* This is the XMS driver entry point */
708 XMS_Handler(context);
713 RMCB *CurrRMCB = FirstRMCB;
715 while (CurrRMCB && (HIWORD(CurrRMCB->address) != CS_reg(context)))
716 CurrRMCB = CurrRMCB->next;
719 /* RMCB call, propagate to protected-mode handler */
720 DPMI_CallRMCBProc(context, CurrRMCB, lpDosTask->dpmi_flag);
727 RESET_CFLAG(context);
728 switch(AX_reg(context))
730 case 0x0000: /* Allocate LDT descriptors */
731 TRACE("allocate LDT descriptors (%d)\n",CX_reg(context));
732 if (!(EAX_reg(context) = AllocSelectorArray16( CX_reg(context) )))
735 EAX_reg(context) = 0x8011; /* descriptor unavailable */
738 TRACE("success, array starts at 0x%04x\n",AX_reg(context));
741 case 0x0001: /* Free LDT descriptor */
742 TRACE("free LDT descriptor (0x%04x)\n",BX_reg(context));
743 if (FreeSelector16( BX_reg(context) ))
745 EAX_reg(context) = 0x8022; /* invalid selector */
750 /* If a segment register contains the selector being freed, */
751 /* set it to zero. */
752 if (!((DS_reg(context)^BX_reg(context)) & ~3)) DS_reg(context) = 0;
753 if (!((ES_reg(context)^BX_reg(context)) & ~3)) ES_reg(context) = 0;
754 if (!((FS_reg(context)^BX_reg(context)) & ~3)) FS_reg(context) = 0;
755 if (!((GS_reg(context)^BX_reg(context)) & ~3)) GS_reg(context) = 0;
759 case 0x0002: /* Real mode segment to descriptor */
760 TRACE("real mode segment to descriptor (0x%04x)\n",BX_reg(context));
762 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
763 switch(BX_reg(context))
765 case 0x0000: entryPoint = 183; break; /* __0000H */
766 case 0x0040: entryPoint = 193; break; /* __0040H */
767 case 0xa000: entryPoint = 174; break; /* __A000H */
768 case 0xb000: entryPoint = 181; break; /* __B000H */
769 case 0xb800: entryPoint = 182; break; /* __B800H */
770 case 0xc000: entryPoint = 195; break; /* __C000H */
771 case 0xd000: entryPoint = 179; break; /* __D000H */
772 case 0xe000: entryPoint = 190; break; /* __E000H */
773 case 0xf000: entryPoint = 194; break; /* __F000H */
775 EAX_reg(context) = DOSMEM_AllocSelector(BX_reg(context));
779 EAX_reg(context) = LOWORD(NE_GetEntryPoint( GetModuleHandle16( "KERNEL" ),
784 case 0x0003: /* Get next selector increment */
785 TRACE("get selector increment (__AHINCR)\n");
786 EAX_reg(context) = __AHINCR;
789 case 0x0004: /* Lock selector (not supported) */
790 FIXME("lock selector not supported\n");
791 EAX_reg(context) = 0; /* FIXME: is this a correct return value? */
794 case 0x0005: /* Unlock selector (not supported) */
795 FIXME("unlock selector not supported\n");
796 EAX_reg(context) = 0; /* FIXME: is this a correct return value? */
799 case 0x0006: /* Get selector base address */
800 TRACE("get selector base address (0x%04x)\n",BX_reg(context));
801 if (!(dw = GetSelectorBase( BX_reg(context) )))
803 EAX_reg(context) = 0x8022; /* invalid selector */
810 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
811 dw = DOS_WINETOAPP(dw, base);
814 CX_reg(context) = HIWORD(W32S_WINE2APP(dw, offset));
815 DX_reg(context) = LOWORD(W32S_WINE2APP(dw, offset));
819 case 0x0007: /* Set selector base address */
820 TRACE("set selector base address (0x%04x,0x%08lx)\n",
822 W32S_APP2WINE(MAKELONG(DX_reg(context),CX_reg(context)), offset));
823 dw = W32S_APP2WINE(MAKELONG(DX_reg(context), CX_reg(context)), offset);
826 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
827 dw = DOS_APPTOWINE(dw, base);
830 SetSelectorBase(BX_reg(context), dw);
833 case 0x0008: /* Set selector limit */
834 TRACE("set selector limit (0x%04x,0x%08lx)\n",BX_reg(context),MAKELONG(DX_reg(context),CX_reg(context)));
835 dw = MAKELONG( DX_reg(context), CX_reg(context) );
838 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
839 DWORD sbase = GetSelectorBase( BX_reg(context) );
841 /* the app has set the limit without setting the base,
842 * it must be relying on that the default should be DOS space;
843 * so set the base address now */
844 SetSelectorBase( BX_reg(context), sbase = base );
845 if (dw == 0xffffffff) {
846 /* djgpp does this without checking (in _dos_ds setup, crt1.c),
847 * so we have to override the limit here */
851 if (DOS_BADLIMIT(sbase, base, dw)) {
852 AX_reg(context) = 0x8021; /* invalid value */
858 SetSelectorLimit16( BX_reg(context), dw );
861 case 0x0009: /* Set selector access rights */
862 TRACE("set selector access rights(0x%04x,0x%04x)\n",BX_reg(context),CX_reg(context));
863 SelectorAccessRights16( BX_reg(context), 1, CX_reg(context) );
866 case 0x000a: /* Allocate selector alias */
867 TRACE("allocate selector alias (0x%04x)\n",BX_reg(context));
868 if (!(AX_reg(context) = AllocCStoDSAlias16( BX_reg(context) )))
870 AX_reg(context) = 0x8011; /* descriptor unavailable */
875 case 0x000b: /* Get descriptor */
876 TRACE("get descriptor (0x%04x)\n",BX_reg(context));
879 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
882 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
883 entry.base = DOS_WINETOAPP(entry.base, base);
886 entry.base = W32S_WINE2APP(entry.base, offset);
888 /* FIXME: should use ES:EDI for 32-bit clients */
889 LDT_EntryToBytes( PTR_SEG_OFF_TO_LIN( ES_reg(context),
890 DI_reg(context) ), &entry );
894 case 0x000c: /* Set descriptor */
895 TRACE("set descriptor (0x%04x)\n",BX_reg(context));
898 LDT_BytesToEntry( PTR_SEG_OFF_TO_LIN( ES_reg(context),
899 DI_reg(context) ), &entry );
900 entry.base = W32S_APP2WINE(entry.base, offset);
903 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
904 entry.base = DOS_APPTOWINE(entry.base, base);
905 if (DOS_BADLIMIT(entry.base, base, entry.limit)) {
906 AX_reg(context) = 0x8021; /* invalid value */
913 LDT_SetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
917 case 0x000d: /* Allocate specific LDT descriptor */
918 FIXME("allocate descriptor (0x%04x), stub!\n",BX_reg(context));
919 AX_reg(context) = 0x8011; /* descriptor unavailable */
922 case 0x0100: /* Allocate DOS memory block */
923 TRACE("allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context));
924 dw = GlobalDOSAlloc16((DWORD)BX_reg(context)<<4);
926 AX_reg(context) = HIWORD(dw);
927 DX_reg(context) = LOWORD(dw);
929 AX_reg(context) = 0x0008; /* insufficient memory */
930 BX_reg(context) = DOSMEM_Available(0)>>4;
934 case 0x0101: /* Free DOS memory block */
935 TRACE("free DOS memory block (0x%04x)\n",DX_reg(context));
936 dw = GlobalDOSFree16(DX_reg(context));
938 AX_reg(context) = 0x0009; /* memory block address invalid */
942 case 0x0200: /* get real mode interrupt vector */
943 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
947 case 0x0201: /* set real mode interrupt vector */
948 FIXME("set realmode interupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context),CX_reg(context),DX_reg(context));
951 case 0x0204: /* Get protected mode interrupt vector */
952 TRACE("get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context));
953 dw = (DWORD)INT_GetPMHandler( BL_reg(context) );
954 CX_reg(context) = HIWORD(dw);
955 DX_reg(context) = LOWORD(dw);
958 case 0x0205: /* Set protected mode interrupt vector */
959 TRACE("set protected mode interrupt handler (0x%02x,%p), stub!\n",
960 BL_reg(context),PTR_SEG_OFF_TO_LIN(CX_reg(context),DX_reg(context)));
961 INT_SetPMHandler( BL_reg(context),
962 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( CX_reg(context),
966 case 0x0300: /* Simulate real mode interrupt */
967 CallRMInt( context );
970 case 0x0301: /* Call real mode procedure with far return */
971 CallRMProc( context, FALSE );
974 case 0x0302: /* Call real mode procedure with interrupt return */
975 CallRMProc( context, TRUE );
978 case 0x0303: /* Allocate Real Mode Callback Address */
979 AllocRMCB( context );
982 case 0x0304: /* Free Real Mode Callback Address */
986 case 0x0305: /* Get State Save/Restore Addresses */
987 TRACE("get state save/restore addresses\n");
988 /* we probably won't need this kind of state saving */
990 /* real mode: just point to the lret */
991 BX_reg(context) = DPMI_wrap_seg;
992 ECX_reg(context) = 2;
993 /* protected mode: don't have any handler yet... */
994 FIXME("no protected-mode dummy state save/restore handler yet\n");
996 EDI_reg(context) = 0;
999 case 0x0306: /* Get Raw Mode Switch Addresses */
1000 TRACE("get raw mode switch addresses\n");
1002 /* real mode, point to standard DPMI return wrapper */
1003 BX_reg(context) = DPMI_wrap_seg;
1004 ECX_reg(context) = 0;
1005 /* protected mode, point to DPMI call wrapper */
1006 SI_reg(context) = lpDosTask->dpmi_sel;
1007 EDI_reg(context) = 8; /* offset of the INT 0x31 call */
1009 ERR("win app attempting to get raw mode switch!\n");
1010 AX_reg(context) = 0x8001; /* unsupported function */
1014 case 0x0400: /* Get DPMI version */
1015 TRACE("get DPMI version\n");
1020 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
1021 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
1022 CL_reg(context) = si.wProcessorLevel;
1023 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
1026 case 0x0500: /* Get free memory information */
1027 TRACE("get free memory information\n");
1031 mmi.dwSize = sizeof(mmi);
1033 ptr = (BYTE *)PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context));
1034 /* the layout is just the same as MEMMANINFO, but without
1037 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
1040 case 0x0501: /* Allocate memory block */
1041 TRACE("allocate memory block (%ld)\n",MAKELONG(CX_reg(context),BX_reg(context)));
1042 if (!(ptr = (BYTE *)DPMI_xalloc(MAKELONG(CX_reg(context), BX_reg(context)))))
1044 AX_reg(context) = 0x8012; /* linear memory not available */
1047 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1048 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1052 case 0x0502: /* Free memory block */
1053 TRACE("free memory block (0x%08lx)\n",
1054 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset));
1055 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),
1056 SI_reg(context)), offset) );
1059 case 0x0503: /* Resize memory block */
1060 TRACE("resize memory block (0x%08lx,%ld)\n",
1061 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
1062 MAKELONG(CX_reg(context),BX_reg(context)));
1063 if (!(ptr = (BYTE *)DPMI_xrealloc(
1064 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
1065 MAKELONG(CX_reg(context),BX_reg(context)))))
1067 AX_reg(context) = 0x8012; /* linear memory not available */
1070 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1071 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1075 case 0x0507: /* Modify page attributes */
1076 FIXME("modify page attributes unimplemented\n");
1077 break; /* Just ignore it */
1079 case 0x0600: /* Lock linear region */
1080 FIXME("lock linear region unimplemented\n");
1081 break; /* Just ignore it */
1083 case 0x0601: /* Unlock linear region */
1084 FIXME("unlock linear region unimplemented\n");
1085 break; /* Just ignore it */
1087 case 0x0602: /* Unlock real-mode region */
1088 FIXME("unlock realmode region unimplemented\n");
1089 break; /* Just ignore it */
1091 case 0x0603: /* Lock real-mode region */
1092 FIXME("lock realmode region unimplemented\n");
1093 break; /* Just ignore it */
1095 case 0x0604: /* Get page size */
1096 TRACE("get pagesize\n");
1097 BX_reg(context) = 0;
1098 CX_reg(context) = VIRTUAL_GetPageSize();
1101 case 0x0702: /* Mark page as demand-paging candidate */
1102 FIXME("mark page as demand-paging candidate\n");
1103 break; /* Just ignore it */
1105 case 0x0703: /* Discard page contents */
1106 FIXME("discard page contents\n");
1107 break; /* Just ignore it */
1109 case 0x0800: /* Physical address mapping */
1110 FIXME("map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context),BX_reg(context)));
1111 if(!(ptr=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context),BX_reg(context)))))
1113 AX_reg(context) = 0x8021;
1118 BX_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1119 CX_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1120 RESET_CFLAG(context);
1125 INT_BARF( context, 0x31 );
1126 AX_reg(context) = 0x8001; /* unsupported function */