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
24 #include "wine/winbase16.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(int31);
34 /* 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;
64 static WORD dpmi_flag;
66 /**********************************************************************
67 * INT_GetRealModeContext
69 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
71 context->Eax = call->eax;
72 context->Ebx = call->ebx;
73 context->Ecx = call->ecx;
74 context->Edx = call->edx;
75 context->Esi = call->esi;
76 context->Edi = call->edi;
77 context->Ebp = call->ebp;
78 context->EFlags = call->fl | V86_FLAG;
79 context->Eip = call->ip;
80 context->Esp = call->sp;
81 context->SegCs = call->cs;
82 context->SegDs = call->ds;
83 context->SegEs = call->es;
84 context->SegFs = call->fs;
85 context->SegGs = call->gs;
86 context->SegSs = call->ss;
90 /**********************************************************************
91 * INT_SetRealModeContext
93 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
95 call->eax = context->Eax;
96 call->ebx = context->Ebx;
97 call->ecx = context->Ecx;
98 call->edx = context->Edx;
99 call->esi = context->Esi;
100 call->edi = context->Edi;
101 call->ebp = context->Ebp;
102 call->fl = LOWORD(context->EFlags);
103 call->ip = LOWORD(context->Eip);
104 call->sp = LOWORD(context->Esp);
105 call->cs = context->SegCs;
106 call->ds = context->SegDs;
107 call->es = context->SegEs;
108 call->fs = context->SegFs;
109 call->gs = context->SegGs;
110 call->ss = context->SegSs;
116 void DPMI_CallRMCB32(RMCB *rmcb, UINT16 ss, DWORD esp, UINT16*es, DWORD*edi)
117 #if 0 /* original code, which early gccs puke on */
120 __asm__ __volatile__(
128 ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
134 : "=d" (*es), "=D" (*edi), "=S" (_clobber), "=a" (_clobber), "=c" (_clobber)
135 : "0" (ss), "2" (esp),
136 "4" (rmcb->regs_sel), "1" (rmcb->regs_ofs),
137 "3" (&rmcb->proc_ofs) );
139 #else /* code generated by a gcc new enough */
141 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32,
146 "movl 0x8(%ebp),%eax\n\t"
147 "movl 0x10(%ebp),%esi\n\t"
148 "movl 0xc(%ebp),%edx\n\t"
149 "movl 0x10(%eax),%ecx\n\t"
150 "movl 0xc(%eax),%edi\n\t"
159 ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
165 "movl 0x14(%ebp),%eax\n\t"
166 "movw %dx,(%eax)\n\t"
167 "movl 0x18(%ebp),%edx\n\t"
168 "movl %edi,(%edx)\n\t"
175 #endif /* __i386__ */
177 /**********************************************************************
180 * This routine does the hard work of calling a callback procedure.
182 static void DPMI_CallRMCBProc( CONTEXT86 *context, RMCB *rmcb, WORD flag )
184 if (IS_SELECTOR_SYSTEM( rmcb->proc_sel )) {
185 /* Wine-internal RMCB, call directly */
186 ((RMCBPROC)rmcb->proc_ofs)(context);
192 INT_SetRealModeContext(MapSL(MAKESEGPTR( rmcb->regs_sel, rmcb->regs_ofs )), context);
193 ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, WINE_LDT_FLAGS_DATA );
196 FIXME("untested!\n");
198 /* The called proc ends with an IRET, and takes these parameters:
199 * DS:ESI = pointer to real-mode SS:SP
200 * ES:EDI = pointer to real-mode call structure
202 * ES:EDI = pointer to real-mode call structure (may be a copy)
203 * It is the proc's responsibility to change the return CS:IP in the
204 * real-mode call structure. */
206 /* 32-bit DPMI client */
207 DPMI_CallRMCB32(rmcb, ss, esp, &es, &edi);
209 /* 16-bit DPMI client */
210 CONTEXT86 ctx = *context;
211 ctx.SegCs = rmcb->proc_sel;
212 ctx.Eip = rmcb->proc_ofs;
215 ctx.SegEs = rmcb->regs_sel;
216 ctx.Edi = rmcb->regs_ofs;
217 /* FIXME: I'm pretty sure this isn't right - should push flags first */
218 wine_call_to_16_regs_short(&ctx, 0);
223 INT_GetRealModeContext( MapSL( MAKESEGPTR( es, edi )), context);
225 ERR("RMCBs only implemented for i386\n");
231 /**********************************************************************
234 * This routine does the hard work of calling a real mode procedure.
236 int DPMI_CallRMProc( CONTEXT86 *context, LPWORD stack, int args, int iret )
239 LPVOID addr = NULL; /* avoid gcc warning */
241 int alloc = 0, already = 0;
244 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
245 context->Eax, context->Ebx, context->Ecx, context->Edx );
246 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
247 context->Esi, context->Edi, context->SegEs, context->SegDs,
248 context->SegCs, LOWORD(context->Eip), args, iret?"IRET":"FAR" );
252 /* there might be some code that just jumps to RMCBs or the like,
253 in which case following the jumps here might get us to a shortcut */
254 code = CTX_SEG_OFF_TO_LIN(context, context->SegCs, context->Eip);
256 case 0xe9: /* JMP NEAR */
257 context->Eip += 3 + *(WORD *)(code+1);
258 /* yeah, I know these gotos don't look good... */
259 goto callrmproc_again;
260 case 0xea: /* JMP FAR */
261 context->Eip = *(WORD *)(code+1);
262 context->SegCs = *(WORD *)(code+3);
263 /* ...but since the label is there anyway... */
264 goto callrmproc_again;
265 case 0xeb: /* JMP SHORT */
266 context->Eip += 2 + *(signed char *)(code+1);
267 /* ...because of other gotos below, so... */
268 goto callrmproc_again;
271 /* shortcut for chaining to internal interrupt handlers */
272 if ((context->SegCs == 0xF000) && iret)
274 DOSVM_RealModeInterrupt( LOWORD(context->Eip)/4, context);
278 /* shortcut for RMCBs */
279 CurrRMCB = FirstRMCB;
281 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
282 CurrRMCB = CurrRMCB->next;
284 if (!CurrRMCB && !MZ_Current())
286 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
287 TRACE("creating VM86 task\n");
291 if (!context->SegSs) {
292 alloc = 1; /* allocate default stack */
293 stack16 = addr = DOSMEM_GetBlock( 64, (UINT16 *)&(context->SegSs) );
297 ERR("could not allocate default stack\n");
301 stack16 = CTX_SEG_OFF_TO_LIN(context, context->SegSs, context->Esp);
303 context->Esp -= (args + (iret?1:0)) * sizeof(WORD);
305 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
306 /* push flags if iret */
309 *stack16 = LOWORD(context->EFlags);
311 /* push return address (return to interrupt wrapper) */
312 *(--stack16) = DOSMEM_wrap_seg;
315 context->Esp -= 2*sizeof(WORD);
320 /* RMCB call, invoke protected-mode handler directly */
321 DPMI_CallRMCBProc(context, CurrRMCB, dpmi_flag);
322 /* check if we returned to where we thought we would */
323 if ((context->SegCs != DOSMEM_wrap_seg) ||
324 (LOWORD(context->Eip) != 0)) {
325 /* we need to continue at different address in real-mode space,
326 so we need to set it all up for real mode again */
327 goto callrmproc_again;
330 TRACE("entering real mode...\n");
331 DOSVM_Enter( context );
332 TRACE("returned from real-mode call\n");
334 if (alloc) DOSMEM_FreeBlock( addr );
339 /**********************************************************************
340 * CallRMInt (WINEDOS.@)
342 void WINAPI DOSVM_CallRMInt( CONTEXT86 *context )
344 CONTEXT86 realmode_ctx;
345 FARPROC16 rm_int = DOSVM_GetRMHandler( BL_reg(context) );
346 REALMODECALL *call = MapSL( MAKESEGPTR( context->SegEs, DI_reg(context) ));
347 INT_GetRealModeContext( call, &realmode_ctx );
349 /* we need to check if a real-mode program has hooked the interrupt */
350 if (HIWORD(rm_int)!=0xF000) {
351 /* yup, which means we need to switch to real mode... */
352 realmode_ctx.SegCs = HIWORD(rm_int);
353 realmode_ctx.Eip = LOWORD(rm_int);
354 if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
357 RESET_CFLAG(context);
358 /* use the IP we have instead of BL_reg, in case some apps
359 decide to move interrupts around for whatever reason... */
360 DOSVM_RealModeInterrupt( LOWORD(rm_int)/4, &realmode_ctx );
362 INT_SetRealModeContext( call, &realmode_ctx );
366 /**********************************************************************
367 * CallRMProc (WINEDOS.@)
369 void WINAPI DOSVM_CallRMProc( CONTEXT86 *context, int iret )
371 REALMODECALL *p = MapSL( MAKESEGPTR( context->SegEs, DI_reg(context) ));
374 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
375 p->eax, p->ebx, p->ecx, p->edx);
376 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
377 p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
379 if (!(p->cs) && !(p->ip)) { /* remove this check
380 if Int21/6501 case map function
381 has been implemented */
385 INT_GetRealModeContext(p, &context16);
386 DPMI_CallRMProc( &context16, ((LPWORD)MapSL(MAKESEGPTR(context->SegSs, LOWORD(context->Esp))))+3,
387 CX_reg(context), iret );
388 INT_SetRealModeContext(p, &context16);
392 /* (see dosmem.c, function DOSMEM_InitDPMI) */
393 static void StartPM( CONTEXT86 *context )
395 UINT16 cs, ss, ds, es;
397 DWORD psp_ofs = (DWORD)(DOSVM_psp<<4);
398 PDB16 *psp = (PDB16 *)psp_ofs;
399 HANDLE16 env_seg = psp->environment;
400 unsigned char selflags = WINE_LDT_FLAGS_DATA;
402 RESET_CFLAG(context);
403 dpmi_flag = AX_reg(context);
404 /* our mode switch wrapper have placed the desired CS into DX */
405 cs = SELECTOR_AllocBlock( (void *)(DX_reg(context)<<4), 0x10000, WINE_LDT_FLAGS_CODE );
406 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
407 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
408 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
409 32-bit code using this stack. */
410 if (dpmi_flag & 1) selflags |= WINE_LDT_FLAGS_32BIT;
411 ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, selflags );
412 /* do the same for the data segments, just in case */
413 if (context->SegDs == context->SegSs) ds = ss;
414 else ds = SELECTOR_AllocBlock( (void *)(context->SegDs<<4), 0x10000, selflags );
415 es = SELECTOR_AllocBlock( psp, 0x100, selflags );
416 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
417 psp->environment = SELECTOR_AllocBlock( (void *)(env_seg<<4), 0x10000, WINE_LDT_FLAGS_DATA );
420 pm_ctx.SegCs = DOSMEM_dpmi_sel;
421 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
429 TRACE("DOS program is now entering protected mode\n");
430 wine_call_to_16_regs_short(&pm_ctx, 0);
432 /* in the current state of affairs, we won't ever actually return here... */
433 /* we should have int21/ah=4c do it someday, though... */
435 FreeSelector16(psp->environment);
436 psp->environment = env_seg;
438 if (ds != ss) FreeSelector16(ds);
443 static RMCB *DPMI_AllocRMCB( void )
445 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
450 LPVOID RMCBmem = DOSMEM_GetBlock(4, &uParagraph);
453 *p++ = 0xcd; /* RMCB: */
454 *p++ = 0x31; /* int $0x31 */
455 /* it is the called procedure's task to change the return CS:EIP
456 the DPMI 0.9 spec states that if it doesn't, it will be called again */
458 *p++ = 0xfc; /* jmp RMCB */
459 NewRMCB->address = MAKELONG(0, uParagraph);
460 NewRMCB->next = FirstRMCB;
467 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
469 RMCB *NewRMCB = DPMI_AllocRMCB();
472 NewRMCB->proc_ofs = (DWORD)proc;
473 NewRMCB->proc_sel = 0;
474 NewRMCB->regs_ofs = 0;
475 NewRMCB->regs_sel = 0;
476 return (FARPROC16)(NewRMCB->address);
482 static int DPMI_FreeRMCB( DWORD address )
484 RMCB *CurrRMCB = FirstRMCB;
485 RMCB *PrevRMCB = NULL;
487 while (CurrRMCB && (CurrRMCB->address != address))
490 CurrRMCB = CurrRMCB->next;
495 PrevRMCB->next = CurrRMCB->next;
497 FirstRMCB = CurrRMCB->next;
498 DOSMEM_FreeBlock(DOSMEM_MapRealToLinear(CurrRMCB->address));
499 HeapFree(GetProcessHeap(), 0, CurrRMCB);
506 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
508 DPMI_FreeRMCB( (DWORD)proc );
512 /* DPMI Raw Mode Switch handler */
515 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
517 LPDOSTASK lpDosTask = MZ_Current();
522 /* we could probably start a DPMI-only dosmod task here, but I doubt
523 anything other than real DOS apps want to call raw mode switch */
524 ERR("attempting raw mode switch without DOS task!\n");
527 /* initialize real-mode context as per spec */
528 memset(&rm_ctx, 0, sizeof(rm_ctx));
529 rm_ctx.SegDs = AX_sig(context);
530 rm_ctx.SegEs = CX_sig(context);
531 rm_ctx.SegSs = DX_sig(context);
532 rm_ctx.Esp = EBX_sig(context);
533 rm_ctx.SegCs = SI_sig(context);
534 rm_ctx.Eip = EDI_sig(context);
535 rm_ctx.Ebp = EBP_sig(context);
538 rm_ctx.EFlags = EFL_sig(context); /* at least we need the IF flag */
540 /* enter real mode again */
541 TRACE("re-entering real mode at %04lx:%04lx\n",rm_ctx.SegCs,rm_ctx.Eip);
542 ret = DOSVM_Enter( &rm_ctx );
543 /* when the real-mode stuff call its mode switch address,
544 DOSVM_Enter will return and we will continue here */
547 /* if the sync was lost, there's no way to recover */
551 /* alter protected-mode context as per spec */
552 DS_sig(context) = LOWORD(rm_ctx.Eax);
553 ES_sig(context) = LOWORD(rm_ctx.Ecx);
554 SS_sig(context) = LOWORD(rm_ctx.Edx);
555 ESP_sig(context) = rm_ctx.Ebx;
556 CS_sig(context) = LOWORD(rm_ctx.Esi);
557 EIP_sig(context) = rm_ctx.Edi;
558 EBP_sig(context) = rm_ctx.Ebp;
562 /* Return to new address and hope that we didn't mess up */
563 TRACE("re-entering protected mode at %04x:%08lx\n",
564 CS_sig(context), EIP_sig(context));
569 /**********************************************************************
570 * AllocRMCB (WINEDOS.@)
572 void WINAPI DOSVM_AllocRMCB( CONTEXT86 *context )
574 RMCB *NewRMCB = DPMI_AllocRMCB();
576 TRACE("Function to call: %04x:%04x\n", (WORD)context->SegDs, SI_reg(context) );
580 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
581 NewRMCB->proc_ofs = LOWORD(context->Esi);
582 NewRMCB->proc_sel = context->SegDs;
583 NewRMCB->regs_ofs = LOWORD(context->Edi);
584 NewRMCB->regs_sel = context->SegEs;
585 SET_LOWORD( context->Ecx, HIWORD(NewRMCB->address) );
586 SET_LOWORD( context->Edx, LOWORD(NewRMCB->address) );
590 SET_LOWORD( context->Eax, 0x8015 ); /* callback unavailable */
596 /**********************************************************************
597 * FreeRMCB (WINEDOS.@)
599 void WINAPI DOSVM_FreeRMCB( CONTEXT86 *context )
601 FIXME("callback address: %04x:%04x\n",
602 CX_reg(context), DX_reg(context));
604 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
605 SET_LOWORD( context->Eax, 0x8024 ); /* invalid callback address */
611 /**********************************************************************
614 * Handler for real-mode int 31h (DPMI).
616 void WINAPI DOSVM_Int31Handler( CONTEXT86 *context )
618 /* check if it's our wrapper */
619 TRACE("called from real mode\n");
620 if (context->SegCs==DOSMEM_dpmi_seg) {
621 /* This is the protected mode switch */
625 else if (context->SegCs==DOSMEM_xms_seg)
627 /* This is the XMS driver entry point */
628 XMS_Handler(context);
634 RMCB *CurrRMCB = FirstRMCB;
636 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
637 CurrRMCB = CurrRMCB->next;
640 /* RMCB call, propagate to protected-mode handler */
641 DPMI_CallRMCBProc(context, CurrRMCB, dpmi_flag);
646 /* chain to protected mode handler */
647 INT_Int31Handler( context );