4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
25 #include "wine/winbase16.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(int31);
35 /* 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;
65 static WORD dpmi_flag;
67 /**********************************************************************
68 * INT_GetRealModeContext
70 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
72 context->Eax = call->eax;
73 context->Ebx = call->ebx;
74 context->Ecx = call->ecx;
75 context->Edx = call->edx;
76 context->Esi = call->esi;
77 context->Edi = call->edi;
78 context->Ebp = call->ebp;
79 context->EFlags = call->fl | V86_FLAG;
80 context->Eip = call->ip;
81 context->Esp = call->sp;
82 context->SegCs = call->cs;
83 context->SegDs = call->ds;
84 context->SegEs = call->es;
85 context->SegFs = call->fs;
86 context->SegGs = call->gs;
87 context->SegSs = call->ss;
91 /**********************************************************************
92 * INT_SetRealModeContext
94 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
96 call->eax = context->Eax;
97 call->ebx = context->Ebx;
98 call->ecx = context->Ecx;
99 call->edx = context->Edx;
100 call->esi = context->Esi;
101 call->edi = context->Edi;
102 call->ebp = context->Ebp;
103 call->fl = LOWORD(context->EFlags);
104 call->ip = LOWORD(context->Eip);
105 call->sp = LOWORD(context->Esp);
106 call->cs = context->SegCs;
107 call->ds = context->SegDs;
108 call->es = context->SegEs;
109 call->fs = context->SegFs;
110 call->gs = context->SegGs;
111 call->ss = context->SegSs;
117 void DPMI_CallRMCB32(RMCB *rmcb, UINT16 ss, DWORD esp, UINT16*es, DWORD*edi)
118 #if 0 /* original code, which early gccs puke on */
121 __asm__ __volatile__(
129 ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
135 : "=d" (*es), "=D" (*edi), "=S" (_clobber), "=a" (_clobber), "=c" (_clobber)
136 : "0" (ss), "2" (esp),
137 "4" (rmcb->regs_sel), "1" (rmcb->regs_ofs),
138 "3" (&rmcb->proc_ofs) );
140 #else /* code generated by a gcc new enough */
142 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32,
147 "movl 0x8(%ebp),%eax\n\t"
148 "movl 0x10(%ebp),%esi\n\t"
149 "movl 0xc(%ebp),%edx\n\t"
150 "movl 0x10(%eax),%ecx\n\t"
151 "movl 0xc(%eax),%edi\n\t"
160 ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
166 "movl 0x14(%ebp),%eax\n\t"
167 "movw %dx,(%eax)\n\t"
168 "movl 0x18(%ebp),%edx\n\t"
169 "movl %edi,(%edx)\n\t"
176 #endif /* __i386__ */
178 /**********************************************************************
181 * This routine does the hard work of calling a callback procedure.
183 static void DPMI_CallRMCBProc( CONTEXT86 *context, RMCB *rmcb, WORD flag )
185 if (IS_SELECTOR_SYSTEM( rmcb->proc_sel )) {
186 /* Wine-internal RMCB, call directly */
187 ((RMCBPROC)rmcb->proc_ofs)(context);
193 INT_SetRealModeContext(MapSL(MAKESEGPTR( rmcb->regs_sel, rmcb->regs_ofs )), context);
194 ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, WINE_LDT_FLAGS_DATA );
197 FIXME("untested!\n");
199 /* The called proc ends with an IRET, and takes these parameters:
200 * DS:ESI = pointer to real-mode SS:SP
201 * ES:EDI = pointer to real-mode call structure
203 * ES:EDI = pointer to real-mode call structure (may be a copy)
204 * It is the proc's responsibility to change the return CS:IP in the
205 * real-mode call structure. */
207 /* 32-bit DPMI client */
208 DPMI_CallRMCB32(rmcb, ss, esp, &es, &edi);
210 /* 16-bit DPMI client */
211 CONTEXT86 ctx = *context;
212 ctx.SegCs = rmcb->proc_sel;
213 ctx.Eip = rmcb->proc_ofs;
216 ctx.SegEs = rmcb->regs_sel;
217 ctx.Edi = rmcb->regs_ofs;
218 /* FIXME: I'm pretty sure this isn't right - should push flags first */
219 wine_call_to_16_regs_short(&ctx, 0);
224 INT_GetRealModeContext( MapSL( MAKESEGPTR( es, edi )), context);
226 ERR("RMCBs only implemented for i386\n");
232 /**********************************************************************
235 * This routine does the hard work of calling a real mode procedure.
237 int DPMI_CallRMProc( CONTEXT86 *context, LPWORD stack, int args, int iret )
240 LPVOID addr = NULL; /* avoid gcc warning */
242 int alloc = 0, already = 0;
245 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
246 context->Eax, context->Ebx, context->Ecx, context->Edx );
247 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
248 context->Esi, context->Edi, context->SegEs, context->SegDs,
249 context->SegCs, LOWORD(context->Eip), args, iret?"IRET":"FAR" );
253 /* there might be some code that just jumps to RMCBs or the like,
254 in which case following the jumps here might get us to a shortcut */
255 code = CTX_SEG_OFF_TO_LIN(context, context->SegCs, context->Eip);
257 case 0xe9: /* JMP NEAR */
258 context->Eip += 3 + *(WORD *)(code+1);
259 /* yeah, I know these gotos don't look good... */
260 goto callrmproc_again;
261 case 0xea: /* JMP FAR */
262 context->Eip = *(WORD *)(code+1);
263 context->SegCs = *(WORD *)(code+3);
264 /* ...but since the label is there anyway... */
265 goto callrmproc_again;
266 case 0xeb: /* JMP SHORT */
267 context->Eip += 2 + *(signed char *)(code+1);
268 /* ...because of other gotos below, so... */
269 goto callrmproc_again;
272 /* shortcut for chaining to internal interrupt handlers */
273 if ((context->SegCs == 0xF000) && iret)
275 DOSVM_RealModeInterrupt( LOWORD(context->Eip)/4, context);
279 /* shortcut for RMCBs */
280 CurrRMCB = FirstRMCB;
282 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
283 CurrRMCB = CurrRMCB->next;
285 if (!CurrRMCB && !MZ_Current())
287 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
288 TRACE("creating VM86 task\n");
292 if (!context->SegSs) {
293 alloc = 1; /* allocate default stack */
294 stack16 = addr = DOSMEM_GetBlock( 64, (UINT16 *)&(context->SegSs) );
298 ERR("could not allocate default stack\n");
302 stack16 = CTX_SEG_OFF_TO_LIN(context, context->SegSs, context->Esp);
304 context->Esp -= (args + (iret?1:0)) * sizeof(WORD);
306 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
307 /* push flags if iret */
310 *stack16 = LOWORD(context->EFlags);
312 /* push return address (return to interrupt wrapper) */
313 *(--stack16) = DOSMEM_wrap_seg;
316 context->Esp -= 2*sizeof(WORD);
321 /* RMCB call, invoke protected-mode handler directly */
322 DPMI_CallRMCBProc(context, CurrRMCB, dpmi_flag);
323 /* check if we returned to where we thought we would */
324 if ((context->SegCs != DOSMEM_wrap_seg) ||
325 (LOWORD(context->Eip) != 0)) {
326 /* we need to continue at different address in real-mode space,
327 so we need to set it all up for real mode again */
328 goto callrmproc_again;
331 TRACE("entering real mode...\n");
332 DOSVM_Enter( context );
333 TRACE("returned from real-mode call\n");
335 if (alloc) DOSMEM_FreeBlock( addr );
340 /**********************************************************************
341 * CallRMInt (WINEDOS.@)
343 void WINAPI DOSVM_CallRMInt( CONTEXT86 *context )
345 CONTEXT86 realmode_ctx;
346 FARPROC16 rm_int = DOSVM_GetRMHandler( BL_reg(context) );
347 REALMODECALL *call = MapSL( MAKESEGPTR( context->SegEs, DI_reg(context) ));
348 INT_GetRealModeContext( call, &realmode_ctx );
350 /* we need to check if a real-mode program has hooked the interrupt */
351 if (HIWORD(rm_int)!=0xF000) {
352 /* yup, which means we need to switch to real mode... */
353 realmode_ctx.SegCs = HIWORD(rm_int);
354 realmode_ctx.Eip = LOWORD(rm_int);
355 if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
358 RESET_CFLAG(context);
359 /* use the IP we have instead of BL_reg, in case some apps
360 decide to move interrupts around for whatever reason... */
361 DOSVM_RealModeInterrupt( LOWORD(rm_int)/4, &realmode_ctx );
363 INT_SetRealModeContext( call, &realmode_ctx );
367 /**********************************************************************
368 * CallRMProc (WINEDOS.@)
370 void WINAPI DOSVM_CallRMProc( CONTEXT86 *context, int iret )
372 REALMODECALL *p = MapSL( MAKESEGPTR( context->SegEs, DI_reg(context) ));
375 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
376 p->eax, p->ebx, p->ecx, p->edx);
377 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
378 p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
380 if (!(p->cs) && !(p->ip)) { /* remove this check
381 if Int21/6501 case map function
382 has been implemented */
386 INT_GetRealModeContext(p, &context16);
387 DPMI_CallRMProc( &context16, ((LPWORD)MapSL(MAKESEGPTR(context->SegSs, LOWORD(context->Esp))))+3,
388 CX_reg(context), iret );
389 INT_SetRealModeContext(p, &context16);
393 /* (see dosmem.c, function DOSMEM_InitDPMI) */
394 static void StartPM( CONTEXT86 *context )
396 UINT16 cs, ss, ds, es;
398 DWORD psp_ofs = (DWORD)(DOSVM_psp<<4);
399 PDB16 *psp = (PDB16 *)psp_ofs;
400 HANDLE16 env_seg = psp->environment;
401 unsigned char selflags = WINE_LDT_FLAGS_DATA;
403 RESET_CFLAG(context);
404 dpmi_flag = AX_reg(context);
405 /* our mode switch wrapper have placed the desired CS into DX */
406 cs = SELECTOR_AllocBlock( (void *)(DX_reg(context)<<4), 0x10000, WINE_LDT_FLAGS_CODE );
407 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
408 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
409 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
410 32-bit code using this stack. */
411 if (dpmi_flag & 1) selflags |= WINE_LDT_FLAGS_32BIT;
412 ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, selflags );
413 /* do the same for the data segments, just in case */
414 if (context->SegDs == context->SegSs) ds = ss;
415 else ds = SELECTOR_AllocBlock( (void *)(context->SegDs<<4), 0x10000, selflags );
416 es = SELECTOR_AllocBlock( psp, 0x100, selflags );
417 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
418 psp->environment = SELECTOR_AllocBlock( (void *)(env_seg<<4), 0x10000, WINE_LDT_FLAGS_DATA );
421 pm_ctx.SegCs = DOSMEM_dpmi_sel;
422 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
430 TRACE("DOS program is now entering protected mode\n");
431 wine_call_to_16_regs_short(&pm_ctx, 0);
433 /* in the current state of affairs, we won't ever actually return here... */
434 /* we should have int21/ah=4c do it someday, though... */
436 FreeSelector16(psp->environment);
437 psp->environment = env_seg;
439 if (ds != ss) FreeSelector16(ds);
444 static RMCB *DPMI_AllocRMCB( void )
446 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
451 LPVOID RMCBmem = DOSMEM_GetBlock(4, &uParagraph);
454 *p++ = 0xcd; /* RMCB: */
455 *p++ = 0x31; /* int $0x31 */
456 /* it is the called procedure's task to change the return CS:EIP
457 the DPMI 0.9 spec states that if it doesn't, it will be called again */
459 *p++ = 0xfc; /* jmp RMCB */
460 NewRMCB->address = MAKELONG(0, uParagraph);
461 NewRMCB->next = FirstRMCB;
468 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
470 RMCB *NewRMCB = DPMI_AllocRMCB();
473 NewRMCB->proc_ofs = (DWORD)proc;
474 NewRMCB->proc_sel = 0;
475 NewRMCB->regs_ofs = 0;
476 NewRMCB->regs_sel = 0;
477 return (FARPROC16)(NewRMCB->address);
483 static int DPMI_FreeRMCB( DWORD address )
485 RMCB *CurrRMCB = FirstRMCB;
486 RMCB *PrevRMCB = NULL;
488 while (CurrRMCB && (CurrRMCB->address != address))
491 CurrRMCB = CurrRMCB->next;
496 PrevRMCB->next = CurrRMCB->next;
498 FirstRMCB = CurrRMCB->next;
499 DOSMEM_FreeBlock(DOSMEM_MapRealToLinear(CurrRMCB->address));
500 HeapFree(GetProcessHeap(), 0, CurrRMCB);
507 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
509 DPMI_FreeRMCB( (DWORD)proc );
513 /* DPMI Raw Mode Switch handler */
515 void WINAPI DOSVM_RawModeSwitch( CONTEXT86 *context )
520 /* initialize real-mode context as per spec */
521 memset(&rm_ctx, 0, sizeof(rm_ctx));
522 rm_ctx.SegDs = AX_reg(context);
523 rm_ctx.SegEs = CX_reg(context);
524 rm_ctx.SegSs = DX_reg(context);
525 rm_ctx.Esp = context->Ebx;
526 rm_ctx.SegCs = SI_reg(context);
527 rm_ctx.Eip = context->Edi;
528 rm_ctx.Ebp = context->Ebp;
531 rm_ctx.EFlags = context->EFlags; /* at least we need the IF flag */
533 /* enter real mode again */
534 TRACE("re-entering real mode at %04lx:%04lx\n",rm_ctx.SegCs,rm_ctx.Eip);
535 ret = DOSVM_Enter( &rm_ctx );
536 /* when the real-mode stuff call its mode switch address,
537 DOSVM_Enter will return and we will continue here */
541 /* if the sync was lost, there's no way to recover */
545 /* alter protected-mode context as per spec */
546 context->SegDs = LOWORD(rm_ctx.Eax);
547 context->SegEs = LOWORD(rm_ctx.Ecx);
548 context->SegSs = LOWORD(rm_ctx.Edx);
549 context->Esp = rm_ctx.Ebx;
550 context->SegCs = LOWORD(rm_ctx.Esi);
551 context->Eip = rm_ctx.Edi;
552 context->Ebp = rm_ctx.Ebp;
556 /* Return to new address and hope that we didn't mess up */
557 TRACE("re-entering protected mode at %04lx:%08lx\n",
558 context->SegCs, context->Eip);
562 /**********************************************************************
563 * AllocRMCB (WINEDOS.@)
565 void WINAPI DOSVM_AllocRMCB( CONTEXT86 *context )
567 RMCB *NewRMCB = DPMI_AllocRMCB();
569 TRACE("Function to call: %04x:%04x\n", (WORD)context->SegDs, SI_reg(context) );
573 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
574 NewRMCB->proc_ofs = LOWORD(context->Esi);
575 NewRMCB->proc_sel = context->SegDs;
576 NewRMCB->regs_ofs = LOWORD(context->Edi);
577 NewRMCB->regs_sel = context->SegEs;
578 SET_LOWORD( context->Ecx, HIWORD(NewRMCB->address) );
579 SET_LOWORD( context->Edx, LOWORD(NewRMCB->address) );
583 SET_LOWORD( context->Eax, 0x8015 ); /* callback unavailable */
589 /**********************************************************************
590 * FreeRMCB (WINEDOS.@)
592 void WINAPI DOSVM_FreeRMCB( CONTEXT86 *context )
594 FIXME("callback address: %04x:%04x\n",
595 CX_reg(context), DX_reg(context));
597 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
598 SET_LOWORD( context->Eax, 0x8024 ); /* invalid callback address */
604 /**********************************************************************
607 * Handler for real-mode int 31h (DPMI).
609 void WINAPI DOSVM_Int31Handler( CONTEXT86 *context )
611 /* check if it's our wrapper */
612 TRACE("called from real mode\n");
613 if (context->SegCs==DOSMEM_dpmi_seg) {
614 /* This is the protected mode switch */
618 else if (context->SegCs==DOSMEM_xms_seg)
620 /* This is the XMS driver entry point */
621 XMS_Handler(context);
627 RMCB *CurrRMCB = FirstRMCB;
629 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
630 CurrRMCB = CurrRMCB->next;
633 /* RMCB call, propagate to protected-mode handler */
634 DPMI_CallRMCBProc(context, CurrRMCB, dpmi_flag);
639 /* chain to protected mode handler */
640 INT_Int31Handler( context );