4 * Copyright 1995 Alexandre Julliard
10 #include "wine/winbase16.h"
19 #include "selectors.h"
24 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
26 void CreateBPB(int drive, BYTE *data, BOOL16 limited); /* defined in int21.c */
28 static void* lastvalloced = NULL;
30 /* Structure for real-mode callbacks */
54 typedef struct tagRMCB {
56 DWORD proc_ofs,proc_sel;
57 DWORD regs_ofs,regs_sel;
61 static RMCB *FirstRMCB = NULL;
65 /**********************************************************************
67 * special virtualalloc, allocates lineary monoton growing memory.
68 * (the usual VirtualAlloc does not satisfy that restriction)
71 DPMI_xalloc(int len) {
73 LPVOID oldlastv = lastvalloced;
79 ret=VirtualAlloc(lastvalloced,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
81 lastvalloced+=0x10000;
82 /* we failed to allocate one in the first round.
85 if (!xflag && (lastvalloced<oldlastv)) { /* wrapped */
86 FIXME(int31,"failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len);
89 /* if we even fail to allocate something in the next
92 if ((xflag==1) && (lastvalloced >= oldlastv))
94 if ((xflag==2) && (lastvalloced < oldlastv)) {
95 FIXME(int31,"failed to allocate any memory of %d bytes!\n",len);
100 ret=VirtualAlloc(NULL,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
101 lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
106 DPMI_xfree(LPVOID ptr) {
107 VirtualFree(ptr,0,MEM_RELEASE);
110 /* FIXME: perhaps we could grow this mapped area... */
112 DPMI_xrealloc(LPVOID ptr,int newsize) {
113 MEMORY_BASIC_INFORMATION mbi;
116 newptr = DPMI_xalloc(newsize);
118 if (!VirtualQuery(ptr,&mbi,sizeof(mbi))) {
119 FIXME(int31,"realloc of DPMI_xallocd region %p?\n",ptr);
122 if (mbi.State == MEM_FREE) {
123 FIXME(int31,"realloc of DPMI_xallocd region %p?\n",ptr);
126 /* We do not shrink allocated memory. most reallocs
127 * only do grows anyway
129 if (newsize<=mbi.RegionSize)
131 memcpy(newptr,ptr,mbi.RegionSize);
136 /**********************************************************************
137 * INT_GetRealModeContext
139 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT *context )
141 EAX_reg(context) = call->eax;
142 EBX_reg(context) = call->ebx;
143 ECX_reg(context) = call->ecx;
144 EDX_reg(context) = call->edx;
145 ESI_reg(context) = call->esi;
146 EDI_reg(context) = call->edi;
147 EBP_reg(context) = call->ebp;
148 EFL_reg(context) = call->fl | V86_FLAG;
149 EIP_reg(context) = call->ip;
150 ESP_reg(context) = call->sp;
151 CS_reg(context) = call->cs;
152 DS_reg(context) = call->ds;
153 ES_reg(context) = call->es;
154 FS_reg(context) = call->fs;
155 GS_reg(context) = call->gs;
156 SS_reg(context) = call->ss;
157 (char*)V86BASE(context) = DOSMEM_MemoryBase(0);
161 /**********************************************************************
162 * INT_SetRealModeContext
164 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT *context )
166 call->eax = EAX_reg(context);
167 call->ebx = EBX_reg(context);
168 call->ecx = ECX_reg(context);
169 call->edx = EDX_reg(context);
170 call->esi = ESI_reg(context);
171 call->edi = EDI_reg(context);
172 call->ebp = EBP_reg(context);
173 call->fl = FL_reg(context);
174 call->ip = IP_reg(context);
175 call->sp = SP_reg(context);
176 call->cs = CS_reg(context);
177 call->ds = DS_reg(context);
178 call->es = ES_reg(context);
179 call->fs = FS_reg(context);
180 call->gs = GS_reg(context);
181 call->ss = SS_reg(context);
185 /**********************************************************************
188 * This routine does the hard work of calling a callback procedure.
190 static void DPMI_CallRMCBProc( CONTEXT *context, RMCB *rmcb, WORD flag )
192 if (IS_SELECTOR_SYSTEM( rmcb->proc_sel )) {
193 /* Wine-internal RMCB, call directly */
194 ((RMCBPROC)rmcb->proc_ofs)(context);
200 INT_SetRealModeContext((REALMODECALL *)PTR_SEG_OFF_TO_LIN( rmcb->regs_sel, rmcb->regs_ofs ), context);
201 ss = SELECTOR_AllocBlock( DOSMEM_MemoryBase(0) + (DWORD)(SS_reg(context)<<4), 0x10000, SEGMENT_DATA, FALSE, FALSE );
203 FIXME(int31,"untested!\n");
205 /* The called proc ends with an IRET, and takes these parameters:
206 * DS:ESI = pointer to real-mode SS:SP
207 * ES:EDI = pointer to real-mode call structure
209 * ES:EDI = pointer to real-mode call structure (may be a copy)
210 * It is the proc's responsibility to change the return CS:IP in the
211 * real-mode call structure. */
213 /* 32-bit DPMI client */
214 __asm__ __volatile__("
225 : "=g" (es), "=D" (edi)
226 : "m" (rmcb->proc_ofs),
227 "g" (ss), "g" (rmcb->regs_sel),
228 "S" (ESP_reg(context)), "D" (rmcb->regs_ofs)
229 : "eax", "ecx", "edx", "esi", "ebp" );
231 /* 16-bit DPMI client */
232 CONTEXT ctx = *context;
233 CS_reg(&ctx) = rmcb->proc_sel;
234 EIP_reg(&ctx) = rmcb->proc_ofs;
236 ESI_reg(&ctx) = ESP_reg(context);
237 ES_reg(&ctx) = rmcb->regs_sel;
238 EDI_reg(&ctx) = rmcb->regs_ofs;
239 Callbacks->CallRegisterShortProc(&ctx, 2);
243 UnMapLS(PTR_SEG_OFF_TO_SEGPTR(ss,0));
244 INT_GetRealModeContext((REALMODECALL*)PTR_SEG_OFF_TO_LIN( es, edi ), context);
246 ERR(int31,"RMCBs only implemented for i386\n");
252 /**********************************************************************
255 * This routine does the hard work of calling a real mode procedure.
257 int DPMI_CallRMProc( CONTEXT *context, LPWORD stack, int args, int iret )
261 THDB *thdb = THREAD_Current();
264 #endif /* !MZ_SUPPORTED */
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;
271 GlobalUnlock16( GetCurrentTask() );
273 TRACE(int31, "EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
274 EAX_reg(context), EBX_reg(context), ECX_reg(context), EDX_reg(context) );
275 TRACE(int31, "ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
276 ESI_reg(context), EDI_reg(context), ES_reg(context), DS_reg(context),
277 CS_reg(context), IP_reg(context), args, iret?"IRET":"FAR" );
281 /* shortcut for chaining to internal interrupt handlers */
282 if ((CS_reg(context) == 0xF000) && iret) {
283 return INT_RealModeInterrupt( IP_reg(context)/4, context);
286 /* shortcut for RMCBs */
287 CurrRMCB = FirstRMCB;
289 while (CurrRMCB && (HIWORD(CurrRMCB->address) != CS_reg(context)))
290 CurrRMCB = CurrRMCB->next;
293 if (!(CurrRMCB || pModule->lpDosTask)) {
294 FIXME(int31,"DPMI real-mode call using DOS VM task system, not fully tested!\n");
295 TRACE(int31,"creating VM86 task\n");
296 if (MZ_InitTask( MZ_AllocDPMITask( pModule->self ) ) < 32) {
297 ERR(int31,"could not setup VM86 task\n");
302 if (!SS_reg(context)) {
303 alloc = 1; /* allocate default stack */
304 stack16 = addr = DOSMEM_GetBlock( pModule->self, 64, (UINT16 *)&(SS_reg(context)) );
305 SP_reg(context) = 64-2;
308 ERR(int31,"could not allocate default stack\n");
312 stack16 = CTX_SEG_OFF_TO_LIN(context, SS_reg(context), ESP_reg(context));
314 SP_reg(context) -= (args + (iret?1:0)) * sizeof(WORD);
317 stack16 = THREAD_STACK16(thdb);
320 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
321 /* push flags if iret */
324 *stack16 = FL_reg(context);
327 /* push return address (return to interrupt wrapper) */
328 *(--stack16) = DPMI_wrap_seg;
331 SP_reg(context) -= 2*sizeof(WORD);
337 /* RMCB call, invoke protected-mode handler directly */
338 DPMI_CallRMCBProc(context, CurrRMCB, pModule->lpDosTask?pModule->lpDosTask->dpmi_flag:0);
339 /* check if we returned to where we thought we would */
340 if ((CS_reg(context) != DPMI_wrap_seg) ||
341 (IP_reg(context) != 0)) {
342 /* we need to continue at different address in real-mode space,
343 so we need to set it all up for real mode again */
344 goto callrmproc_again;
348 #if 0 /* this was probably unnecessary */
349 /* push call address */
350 *(--stack16) = CS_reg(context);
351 *(--stack16) = IP_reg(context);
353 SP_reg(context) -= 2*sizeof(WORD);
354 /* set initial CS:IP to the wrapper's "lret" */
355 CS_reg(context) = DPMI_wrap_seg;
358 TRACE(int31,"entering real mode...\n");
359 DOSVM_Enter( context );
360 TRACE(int31,"returned from real-mode call\n");
362 addr = CTX_SEG_OFF_TO_LIN(context, CS_reg(context), EIP_reg(context));
363 sel = SELECTOR_AllocBlock( addr, 0x10000, SEGMENT_CODE, FALSE, FALSE );
364 seg_addr = PTR_SEG_OFF_TO_SEGPTR( sel, 0 );
366 CS_reg(context) = HIWORD(seg_addr);
367 IP_reg(context) = LOWORD(seg_addr);
368 EBP_reg(context) = OFFSETOF( thdb->cur_stack )
369 + (WORD)&((STACK16FRAME*)0)->bp;
370 Callbacks->CallRegisterShortProc(context, args*sizeof(WORD));
374 if (alloc) DOSMEM_FreeBlock( pModule->self, addr );
379 /**********************************************************************
382 static void CallRMInt( CONTEXT *context )
384 CONTEXT 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(int31,"%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(int31," 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( CONTEXT *context, int iret )
418 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
421 TRACE(int31, "RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
422 p->eax, p->ebx, p->ecx, p->edx);
423 TRACE(int31, " 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), SP_reg(context)))+3,
434 CX_reg(context), iret );
435 INT_SetRealModeContext(p, &context16);
439 static void WINAPI RMCallbackProc( RMCB *rmcb )
441 /* This routine should call DPMI_CallRMCBProc, but we don't have the
442 register structure available - this is easily fixed by going through
443 a Win16 register relay instead of calling RMCallbackProc "directly",
444 but I won't bother at this time. */
445 FIXME(int31,"not properly supported on your architecture!\n");
448 static RMCB *DPMI_AllocRMCB( void )
450 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
456 LPVOID RMCBmem = DOSMEM_GetBlock(0, 4, &uParagraph);
459 *p++ = 0xcd; /* RMCB: */
460 *p++ = 0x31; /* int $0x31 */
461 /* it is the called procedure's task to change the return CS:EIP
462 the DPMI 0.9 spec states that if it doesn't, it will be called again */
464 *p++ = 0xfc; /* jmp RMCB */
466 LPVOID RMCBmem = DOSMEM_GetBlock(0, 15, &uParagraph);
469 *p++ = 0x68; /* pushl */
470 *(LPVOID *)p = NewRMCB;
472 *p++ = 0x9a; /* lcall */
473 *(FARPROC16 *)p = (FARPROC16)RMCallbackProc; /* FIXME: register relay */
477 *p++=0xc3; /* lret (FIXME?) */
479 NewRMCB->address = MAKELONG(0, uParagraph);
480 NewRMCB->next = FirstRMCB;
487 static void AllocRMCB( CONTEXT *context )
489 RMCB *NewRMCB = DPMI_AllocRMCB();
491 TRACE(int31, "Function to call: %04x:%04x\n", (WORD)DS_reg(context), SI_reg(context) );
495 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
496 NewRMCB->proc_ofs = SI_reg(context);
497 NewRMCB->proc_sel = DS_reg(context);
498 NewRMCB->regs_ofs = DI_reg(context);
499 NewRMCB->regs_sel = ES_reg(context);
500 CX_reg(context) = HIWORD(NewRMCB->address);
501 DX_reg(context) = LOWORD(NewRMCB->address);
505 AX_reg(context) = 0x8015; /* callback unavailable */
511 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
513 RMCB *NewRMCB = DPMI_AllocRMCB();
516 NewRMCB->proc_ofs = (DWORD)proc;
517 NewRMCB->proc_sel = 0;
518 NewRMCB->regs_ofs = 0;
519 NewRMCB->regs_sel = 0;
520 return (FARPROC16)(NewRMCB->address);
526 static int DPMI_FreeRMCB( DWORD address )
528 RMCB *CurrRMCB = FirstRMCB;
529 RMCB *PrevRMCB = NULL;
531 while (CurrRMCB && (CurrRMCB->address != address))
534 CurrRMCB = CurrRMCB->next;
539 PrevRMCB->next = CurrRMCB->next;
541 FirstRMCB = CurrRMCB->next;
542 DOSMEM_FreeBlock(0, DOSMEM_MapRealToLinear(CurrRMCB->address));
543 HeapFree(GetProcessHeap(), 0, CurrRMCB);
550 static void FreeRMCB( CONTEXT *context )
552 FIXME(int31, "callback address: %04x:%04x\n",
553 CX_reg(context), DX_reg(context));
555 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
556 AX_reg(context) = 0x8024; /* invalid callback address */
562 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
564 DPMI_FreeRMCB( (DWORD)proc );
569 /* (see loader/dos/module.c, function MZ_InitDPMI) */
571 static void StartPM( CONTEXT *context, LPDOSTASK lpDosTask )
573 char *base = DOSMEM_MemoryBase(0);
574 UINT16 cs, ss, ds, es;
577 RESET_CFLAG(context);
578 lpDosTask->dpmi_flag = AX_reg(context);
579 /* our mode switch wrapper have placed the desired CS into DX */
580 cs = SELECTOR_AllocBlock( base + (DWORD)(DX_reg(context)<<4), 0x10000, SEGMENT_CODE, FALSE, FALSE );
581 ss = SELECTOR_AllocBlock( base + (DWORD)(SS_reg(context)<<4), 0x10000, SEGMENT_DATA, FALSE, FALSE );
582 ds = SELECTOR_AllocBlock( base + (DWORD)(DS_reg(context)<<4), 0x10000, SEGMENT_DATA, FALSE, FALSE );
583 es = SELECTOR_AllocBlock( base + (DWORD)(lpDosTask->psp_seg<<4), 0x100, SEGMENT_DATA, FALSE, FALSE );
586 CS_reg(&pm_ctx) = lpDosTask->dpmi_sel;
587 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
588 AX_reg(&pm_ctx) = ss;
589 DX_reg(&pm_ctx) = cs;
590 DS_reg(&pm_ctx) = ds;
591 ES_reg(&pm_ctx) = es;
595 TRACE(int31,"DOS program is now entering protected mode\n");
596 Callbacks->CallRegisterShortProc(&pm_ctx, 0);
598 /* in the current state of affairs, we won't ever actually return here... */
600 UnMapLS(PTR_SEG_OFF_TO_SEGPTR(es,0));
601 UnMapLS(PTR_SEG_OFF_TO_SEGPTR(ds,0));
602 UnMapLS(PTR_SEG_OFF_TO_SEGPTR(ss,0));
603 UnMapLS(PTR_SEG_OFF_TO_SEGPTR(cs,0));
607 /**********************************************************************
610 * Handler for int 31h (DPMI).
613 void WINAPI INT_Int31Handler( CONTEXT *context )
616 * Note: For Win32s processes, the whole linear address space is
617 * shifted by 0x10000 relative to the OS linear address space.
618 * See the comment in msdos/vxd.c.
620 DWORD offset = W32S_APPLICATION() ? W32S_OFFSET : 0;
625 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
626 NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
628 GlobalUnlock16( GetCurrentTask() );
631 if (ISV86(context) && pModule && pModule->lpDosTask) {
632 /* Called from real mode, check if it's our wrapper */
633 TRACE(int31,"called from real mode\n");
634 if (CS_reg(context)==pModule->lpDosTask->dpmi_seg) {
635 /* This is the protected mode switch */
636 StartPM(context,pModule->lpDosTask);
639 if (CS_reg(context)==pModule->lpDosTask->xms_seg) {
640 /* This is the XMS driver entry point */
641 XMS_Handler(context);
646 RMCB *CurrRMCB = FirstRMCB;
648 while (CurrRMCB && (HIWORD(CurrRMCB->address) != CS_reg(context)))
649 CurrRMCB = CurrRMCB->next;
652 /* RMCB call, propagate to protected-mode handler */
653 DPMI_CallRMCBProc(context, CurrRMCB, pModule->lpDosTask->dpmi_flag);
660 RESET_CFLAG(context);
661 switch(AX_reg(context))
663 case 0x0000: /* Allocate LDT descriptors */
664 TRACE(int31,"allocate LDT descriptors (%d)\n",CX_reg(context));
665 if (!(AX_reg(context) = AllocSelectorArray16( CX_reg(context) )))
667 TRACE(int31,"failed\n");
668 AX_reg(context) = 0x8011; /* descriptor unavailable */
671 TRACE(int31,"success, array starts at 0x%04x\n",AX_reg(context));
674 case 0x0001: /* Free LDT descriptor */
675 TRACE(int31,"free LDT descriptor (0x%04x)\n",BX_reg(context));
676 if (FreeSelector16( BX_reg(context) ))
678 AX_reg(context) = 0x8022; /* invalid selector */
683 /* If a segment register contains the selector being freed, */
684 /* set it to zero. */
685 if (!((DS_reg(context)^BX_reg(context)) & ~3)) DS_reg(context) = 0;
686 if (!((ES_reg(context)^BX_reg(context)) & ~3)) ES_reg(context) = 0;
687 if (!((FS_reg(context)^BX_reg(context)) & ~3)) FS_reg(context) = 0;
688 if (!((GS_reg(context)^BX_reg(context)) & ~3)) GS_reg(context) = 0;
692 case 0x0002: /* Real mode segment to descriptor */
693 TRACE(int31,"real mode segment to descriptor (0x%04x)\n",BX_reg(context));
695 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
696 switch(BX_reg(context))
698 case 0x0000: entryPoint = 183; break; /* __0000H */
699 case 0x0040: entryPoint = 193; break; /* __0040H */
700 case 0xa000: entryPoint = 174; break; /* __A000H */
701 case 0xb000: entryPoint = 181; break; /* __B000H */
702 case 0xb800: entryPoint = 182; break; /* __B800H */
703 case 0xc000: entryPoint = 195; break; /* __C000H */
704 case 0xd000: entryPoint = 179; break; /* __D000H */
705 case 0xe000: entryPoint = 190; break; /* __E000H */
706 case 0xf000: entryPoint = 194; break; /* __F000H */
708 AX_reg(context) = DOSMEM_AllocSelector(BX_reg(context));
712 AX_reg(context) = LOWORD(NE_GetEntryPoint(
713 GetModuleHandle16( "KERNEL" ),
718 case 0x0003: /* Get next selector increment */
719 TRACE(int31,"get selector increment (__AHINCR)\n");
720 AX_reg(context) = __AHINCR;
723 case 0x0004: /* Lock selector (not supported) */
724 FIXME(int31,"lock selector not supported\n");
725 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
728 case 0x0005: /* Unlock selector (not supported) */
729 FIXME(int31,"unlock selector not supported\n");
730 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
733 case 0x0006: /* Get selector base address */
734 TRACE(int31,"get selector base address (0x%04x)\n",BX_reg(context));
735 if (!(dw = GetSelectorBase( BX_reg(context) )))
737 AX_reg(context) = 0x8022; /* invalid selector */
743 if (pModule && pModule->lpDosTask) {
744 DWORD base = (DWORD)DOSMEM_MemoryBase(pModule->self);
745 if ((dw >= base) && (dw < base + 0x110000)) dw -= base;
748 CX_reg(context) = HIWORD(W32S_WINE2APP(dw, offset));
749 DX_reg(context) = LOWORD(W32S_WINE2APP(dw, offset));
753 case 0x0007: /* Set selector base address */
754 TRACE(int31, "set selector base address (0x%04x,0x%08lx)\n",
756 W32S_APP2WINE(MAKELONG(DX_reg(context),CX_reg(context)), offset));
757 dw = W32S_APP2WINE(MAKELONG(DX_reg(context), CX_reg(context)), offset);
759 /* well, what else could we possibly do? */
760 if (pModule && pModule->lpDosTask) {
761 if (dw < 0x110000) dw += (DWORD)DOSMEM_MemoryBase(pModule->self);
764 SetSelectorBase(BX_reg(context), dw);
767 case 0x0008: /* Set selector limit */
768 TRACE(int31,"set selector limit (0x%04x,0x%08lx)\n",BX_reg(context),MAKELONG(DX_reg(context),CX_reg(context)));
769 dw = MAKELONG( DX_reg(context), CX_reg(context) );
771 if (pModule && pModule->lpDosTask) {
772 DWORD base = GetSelectorBase( BX_reg(context) );
773 if ((dw == 0xffffffff) || ((base < 0x110000) && (base + dw > 0x110000))) {
774 AX_reg(context) = 0x8021; /* invalid value */
780 SetSelectorLimit16( BX_reg(context), dw );
783 case 0x0009: /* Set selector access rights */
784 TRACE(int31,"set selector access rights(0x%04x,0x%04x)\n",BX_reg(context),CX_reg(context));
785 SelectorAccessRights16( BX_reg(context), 1, CX_reg(context) );
788 case 0x000a: /* Allocate selector alias */
789 TRACE(int31,"allocate selector alias (0x%04x)\n",BX_reg(context));
790 if (!(AX_reg(context) = AllocCStoDSAlias16( BX_reg(context) )))
792 AX_reg(context) = 0x8011; /* descriptor unavailable */
797 case 0x000b: /* Get descriptor */
798 TRACE(int31,"get descriptor (0x%04x)\n",BX_reg(context));
801 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
802 entry.base = W32S_WINE2APP(entry.base, offset);
804 /* FIXME: should use ES:EDI for 32-bit clients */
805 LDT_EntryToBytes( PTR_SEG_OFF_TO_LIN( ES_reg(context),
806 DI_reg(context) ), &entry );
810 case 0x000c: /* Set descriptor */
811 TRACE(int31,"set descriptor (0x%04x)\n",BX_reg(context));
814 LDT_BytesToEntry( PTR_SEG_OFF_TO_LIN( ES_reg(context),
815 DI_reg(context) ), &entry );
816 entry.base = W32S_APP2WINE(entry.base, offset);
818 LDT_SetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
822 case 0x000d: /* Allocate specific LDT descriptor */
823 FIXME(int31,"allocate descriptor (0x%04x), stub!\n",BX_reg(context));
824 AX_reg(context) = 0x8011; /* descriptor unavailable */
827 case 0x0100: /* Allocate DOS memory block */
828 TRACE(int31,"allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context));
829 dw = GlobalDOSAlloc16((DWORD)BX_reg(context)<<4);
831 AX_reg(context) = HIWORD(dw);
832 DX_reg(context) = LOWORD(dw);
834 AX_reg(context) = 0x0008; /* insufficient memory */
835 BX_reg(context) = DOSMEM_Available(0)>>4;
839 case 0x0101: /* Free DOS memory block */
840 TRACE(int31,"free DOS memory block (0x%04x)\n",DX_reg(context));
841 dw = GlobalDOSFree16(DX_reg(context));
843 AX_reg(context) = 0x0009; /* memory block address invalid */
847 case 0x0200: /* get real mode interrupt vector */
848 FIXME(int31,"get realmode interupt vector(0x%02x) unimplemented.\n",
852 case 0x0201: /* set real mode interrupt vector */
853 FIXME(int31, "set realmode interupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context),CX_reg(context),DX_reg(context));
856 case 0x0204: /* Get protected mode interrupt vector */
857 TRACE(int31,"get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context));
858 dw = (DWORD)INT_GetPMHandler( BL_reg(context) );
859 CX_reg(context) = HIWORD(dw);
860 DX_reg(context) = LOWORD(dw);
863 case 0x0205: /* Set protected mode interrupt vector */
864 TRACE(int31,"set protected mode interrupt handler (0x%02x,%p), stub!\n",
865 BL_reg(context),PTR_SEG_OFF_TO_LIN(CX_reg(context),DX_reg(context)));
866 INT_SetPMHandler( BL_reg(context),
867 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( CX_reg(context),
871 case 0x0300: /* Simulate real mode interrupt */
872 CallRMInt( context );
875 case 0x0301: /* Call real mode procedure with far return */
876 CallRMProc( context, FALSE );
879 case 0x0302: /* Call real mode procedure with interrupt return */
880 CallRMProc( context, TRUE );
883 case 0x0303: /* Allocate Real Mode Callback Address */
884 AllocRMCB( context );
887 case 0x0304: /* Free Real Mode Callback Address */
891 case 0x0400: /* Get DPMI version */
892 TRACE(int31,"get DPMI version\n");
897 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
898 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
899 CL_reg(context) = si.wProcessorLevel;
900 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
903 case 0x0500: /* Get free memory information */
904 TRACE(int31,"get free memory information\n");
908 mmi.dwSize = sizeof(mmi);
910 ptr = (BYTE *)PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context));
911 /* the layout is just the same as MEMMANINFO, but without
914 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
917 case 0x0501: /* Allocate memory block */
918 TRACE(int31,"allocate memory block (%ld)\n",MAKELONG(CX_reg(context),BX_reg(context)));
919 if (!(ptr = (BYTE *)DPMI_xalloc(MAKELONG(CX_reg(context), BX_reg(context)))))
921 AX_reg(context) = 0x8012; /* linear memory not available */
924 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
925 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
929 case 0x0502: /* Free memory block */
930 TRACE(int31, "free memory block (0x%08lx)\n",
931 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset));
932 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),
933 SI_reg(context)), offset) );
936 case 0x0503: /* Resize memory block */
937 TRACE(int31, "resize memory block (0x%08lx,%ld)\n",
938 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
939 MAKELONG(CX_reg(context),BX_reg(context)));
940 if (!(ptr = (BYTE *)DPMI_xrealloc(
941 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
942 MAKELONG(CX_reg(context),BX_reg(context)))))
944 AX_reg(context) = 0x8012; /* linear memory not available */
947 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
948 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
952 case 0x0507: /* Modify page attributes */
953 FIXME(int31,"modify page attributes unimplemented\n");
954 break; /* Just ignore it */
956 case 0x0600: /* Lock linear region */
957 FIXME(int31,"lock linear region unimplemented\n");
958 break; /* Just ignore it */
960 case 0x0601: /* Unlock linear region */
961 FIXME(int31,"unlock linear region unimplemented\n");
962 break; /* Just ignore it */
964 case 0x0602: /* Unlock real-mode region */
965 FIXME(int31,"unlock realmode region unimplemented\n");
966 break; /* Just ignore it */
968 case 0x0603: /* Lock real-mode region */
969 FIXME(int31,"lock realmode region unimplemented\n");
970 break; /* Just ignore it */
972 case 0x0604: /* Get page size */
973 TRACE(int31,"get pagesize\n");
975 CX_reg(context) = VIRTUAL_GetPageSize();
978 case 0x0702: /* Mark page as demand-paging candidate */
979 FIXME(int31,"mark page as demand-paging candidate\n");
980 break; /* Just ignore it */
982 case 0x0703: /* Discard page contents */
983 FIXME(int31,"discard page contents\n");
984 break; /* Just ignore it */
986 case 0x0800: /* Physical address mapping */
987 FIXME(int31,"map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context),BX_reg(context)));
988 if(!(ptr=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context),BX_reg(context)))))
990 AX_reg(context) = 0x8021;
995 BX_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
996 CX_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
997 RESET_CFLAG(context);
1002 INT_BARF( context, 0x31 );
1003 AX_reg(context) = 0x8001; /* unsupported function */