Move interrupt emulation code from INSTR_EmulateInstruction to winedos
[wine] / dlls / winedos / int31.c
1 /*
2  * DPMI 0.9 emulation
3  *
4  * Copyright 1995 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include "windef.h"
25 #include "wine/winbase16.h"
26 #include "miscemu.h"
27 #include "task.h"
28 #include "msdos.h"
29 #include "dosexe.h"
30
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(int31);
34
35 /* Structure for real-mode callbacks */
36 typedef struct
37 {
38     DWORD edi;
39     DWORD esi;
40     DWORD ebp;
41     DWORD reserved;
42     DWORD ebx;
43     DWORD edx;
44     DWORD ecx;
45     DWORD eax;
46     WORD  fl;
47     WORD  es;
48     WORD  ds;
49     WORD  fs;
50     WORD  gs;
51     WORD  ip;
52     WORD  cs;
53     WORD  sp;
54     WORD  ss;
55 } REALMODECALL;
56
57 typedef struct tagRMCB {
58     DWORD address;
59     DWORD proc_ofs,proc_sel;
60     DWORD regs_ofs,regs_sel;
61     struct tagRMCB *next;
62 } RMCB;
63
64 static RMCB *FirstRMCB = NULL;
65 static WORD dpmi_flag;
66
67 /**********************************************************************
68  *          DOSVM_IsDos32
69  * 
70  * Return TRUE if we are in 32-bit protected mode DOS process.
71  */
72 BOOL DOSVM_IsDos32()
73 {
74   return (dpmi_flag & 1) ? TRUE : FALSE;
75 }
76
77
78 /**********************************************************************
79  *          INT_GetRealModeContext
80  */
81 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
82 {
83     context->Eax    = call->eax;
84     context->Ebx    = call->ebx;
85     context->Ecx    = call->ecx;
86     context->Edx    = call->edx;
87     context->Esi    = call->esi;
88     context->Edi    = call->edi;
89     context->Ebp    = call->ebp;
90     context->EFlags = call->fl | V86_FLAG;
91     context->Eip    = call->ip;
92     context->Esp    = call->sp;
93     context->SegCs  = call->cs;
94     context->SegDs  = call->ds;
95     context->SegEs  = call->es;
96     context->SegFs  = call->fs;
97     context->SegGs  = call->gs;
98     context->SegSs  = call->ss;
99 }
100
101
102 /**********************************************************************
103  *          INT_SetRealModeContext
104  */
105 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
106 {
107     call->eax = context->Eax;
108     call->ebx = context->Ebx;
109     call->ecx = context->Ecx;
110     call->edx = context->Edx;
111     call->esi = context->Esi;
112     call->edi = context->Edi;
113     call->ebp = context->Ebp;
114     call->fl  = LOWORD(context->EFlags);
115     call->ip  = LOWORD(context->Eip);
116     call->sp  = LOWORD(context->Esp);
117     call->cs  = context->SegCs;
118     call->ds  = context->SegDs;
119     call->es  = context->SegEs;
120     call->fs  = context->SegFs;
121     call->gs  = context->SegGs;
122     call->ss  = context->SegSs;
123 }
124
125
126 #ifdef __i386__
127
128 void DPMI_CallRMCB32(RMCB *rmcb, UINT16 ss, DWORD esp, UINT16*es, DWORD*edi)
129 #if 0 /* original code, which early gccs puke on */
130 {
131     int _clobber;
132     __asm__ __volatile__(
133         "pushl %%ebp\n"
134         "pushl %%ebx\n"
135         "pushl %%es\n"
136         "pushl %%ds\n"
137         "pushfl\n"
138         "mov %7,%%es\n"
139         "mov %5,%%ds\n"
140         ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
141         "popl %%ds\n"
142         "mov %%es,%0\n"
143         "popl %%es\n"
144         "popl %%ebx\n"
145         "popl %%ebp\n"
146     : "=d" (*es), "=D" (*edi), "=S" (_clobber), "=a" (_clobber), "=c" (_clobber)
147     : "0" (ss), "2" (esp),
148       "4" (rmcb->regs_sel), "1" (rmcb->regs_ofs),
149       "3" (&rmcb->proc_ofs) );
150 }
151 #else /* code generated by a gcc new enough */
152 ;
153 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32,
154     "pushl %ebp\n\t"
155     "movl %esp,%ebp\n\t"
156     "pushl %edi\n\t"
157     "pushl %esi\n\t"
158     "movl 0x8(%ebp),%eax\n\t"
159     "movl 0x10(%ebp),%esi\n\t"
160     "movl 0xc(%ebp),%edx\n\t"
161     "movl 0x10(%eax),%ecx\n\t"
162     "movl 0xc(%eax),%edi\n\t"
163     "addl $0x4,%eax\n\t"
164     "pushl %ebp\n\t"
165     "pushl %ebx\n\t"
166     "pushl %es\n\t"
167     "pushl %ds\n\t"
168     "pushfl\n\t"
169     "mov %cx,%es\n\t"
170     "mov %dx,%ds\n\t"
171     ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
172     "popl %ds\n\t"
173     "mov %es,%dx\n\t"
174     "popl %es\n\t"
175     "popl %ebx\n\t"
176     "popl %ebp\n\t"
177     "movl 0x14(%ebp),%eax\n\t"
178     "movw %dx,(%eax)\n\t"
179     "movl 0x18(%ebp),%edx\n\t"
180     "movl %edi,(%edx)\n\t"
181     "popl %esi\n\t"
182     "popl %edi\n\t"
183     "leave\n\t"
184     "ret")
185 #endif
186
187 #endif /* __i386__ */
188
189 /**********************************************************************
190  *          DPMI_CallRMCBProc
191  *
192  * This routine does the hard work of calling a callback procedure.
193  */
194 static void DPMI_CallRMCBProc( CONTEXT86 *context, RMCB *rmcb, WORD flag )
195 {
196     if (IS_SELECTOR_SYSTEM( rmcb->proc_sel )) {
197         /* Wine-internal RMCB, call directly */
198         ((RMCBPROC)rmcb->proc_ofs)(context);
199     } else {
200 #ifdef __i386__
201         UINT16 ss,es;
202         DWORD esp,edi;
203
204         INT_SetRealModeContext(MapSL(MAKESEGPTR( rmcb->regs_sel, rmcb->regs_ofs )), context);
205         ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, WINE_LDT_FLAGS_DATA );
206         esp = context->Esp;
207
208         FIXME("untested!\n");
209
210         /* The called proc ends with an IRET, and takes these parameters:
211          * DS:ESI = pointer to real-mode SS:SP
212          * ES:EDI = pointer to real-mode call structure
213          * It returns:
214          * ES:EDI = pointer to real-mode call structure (may be a copy)
215          * It is the proc's responsibility to change the return CS:IP in the
216          * real-mode call structure. */
217         if (flag & 1) {
218             /* 32-bit DPMI client */
219             DPMI_CallRMCB32(rmcb, ss, esp, &es, &edi);
220         } else {
221             /* 16-bit DPMI client */
222             CONTEXT86 ctx = *context;
223             ctx.SegCs = rmcb->proc_sel;
224             ctx.Eip   = rmcb->proc_ofs;
225             ctx.SegDs = ss;
226             ctx.Esi   = esp;
227             ctx.SegEs = rmcb->regs_sel;
228             ctx.Edi   = rmcb->regs_ofs;
229             /* FIXME: I'm pretty sure this isn't right - should push flags first */
230             wine_call_to_16_regs_short(&ctx, 0);
231             es = ctx.SegEs;
232             edi = ctx.Edi;
233         }
234         FreeSelector16(ss);
235         INT_GetRealModeContext( MapSL( MAKESEGPTR( es, edi )), context);
236 #else
237         ERR("RMCBs only implemented for i386\n");
238 #endif
239     }
240 }
241
242
243 /**********************************************************************
244  *          DPMI_CallRMProc
245  *
246  * This routine does the hard work of calling a real mode procedure.
247  */
248 int DPMI_CallRMProc( CONTEXT86 *context, LPWORD stack, int args, int iret )
249 {
250     LPWORD stack16;
251     LPVOID addr = NULL; /* avoid gcc warning */
252     RMCB *CurrRMCB;
253     int alloc = 0, already = 0;
254     BYTE *code;
255
256     TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
257                  context->Eax, context->Ebx, context->Ecx, context->Edx );
258     TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
259                  context->Esi, context->Edi, context->SegEs, context->SegDs,
260                  context->SegCs, LOWORD(context->Eip), args, iret?"IRET":"FAR" );
261
262 callrmproc_again:
263
264 /* there might be some code that just jumps to RMCBs or the like,
265    in which case following the jumps here might get us to a shortcut */
266     code = CTX_SEG_OFF_TO_LIN(context, context->SegCs, context->Eip);
267     switch (*code) {
268     case 0xe9: /* JMP NEAR */
269       context->Eip += 3 + *(WORD *)(code+1);
270       /* yeah, I know these gotos don't look good... */
271       goto callrmproc_again;
272     case 0xea: /* JMP FAR */
273       context->Eip = *(WORD *)(code+1);
274       context->SegCs = *(WORD *)(code+3);
275       /* ...but since the label is there anyway... */
276       goto callrmproc_again;
277     case 0xeb: /* JMP SHORT */
278       context->Eip += 2 + *(signed char *)(code+1);
279       /* ...because of other gotos below, so... */
280       goto callrmproc_again;
281     }
282
283 /* shortcut for chaining to internal interrupt handlers */
284     if ((context->SegCs == 0xF000) && iret)
285     {
286         DOSVM_RealModeInterrupt( LOWORD(context->Eip)/4, context);
287         return 0;
288     }
289
290 /* shortcut for RMCBs */
291     CurrRMCB = FirstRMCB;
292
293     while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
294         CurrRMCB = CurrRMCB->next;
295
296     if (!CurrRMCB && !MZ_Current())
297     {
298         FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
299         TRACE("creating VM86 task\n");
300         MZ_AllocDPMITask();
301     }
302     if (!already) {
303         if (!context->SegSs) {
304             alloc = 1; /* allocate default stack */
305             stack16 = addr = DOSMEM_GetBlock( 64, (UINT16 *)&(context->SegSs) );
306             context->Esp = 64-2;
307             stack16 += 32-1;
308             if (!addr) {
309                 ERR("could not allocate default stack\n");
310                 return 1;
311             }
312         } else {
313             stack16 = CTX_SEG_OFF_TO_LIN(context, context->SegSs, context->Esp);
314         }
315         context->Esp -= (args + (iret?1:0)) * sizeof(WORD);
316         stack16 -= args;
317         if (args) memcpy(stack16, stack, args*sizeof(WORD) );
318         /* push flags if iret */
319         if (iret) {
320             stack16--; args++;
321             *stack16 = LOWORD(context->EFlags);
322         }
323         /* push return address (return to interrupt wrapper) */
324         *(--stack16) = DOSVM_dpmi_segments->wrap_seg;
325         *(--stack16) = 0;
326         /* adjust stack */
327         context->Esp -= 2*sizeof(WORD);
328         already = 1;
329     }
330
331     if (CurrRMCB) {
332         /* RMCB call, invoke protected-mode handler directly */
333         DPMI_CallRMCBProc(context, CurrRMCB, dpmi_flag);
334         /* check if we returned to where we thought we would */
335         if ((context->SegCs != DOSVM_dpmi_segments->wrap_seg) ||
336             (LOWORD(context->Eip) != 0)) {
337             /* we need to continue at different address in real-mode space,
338                so we need to set it all up for real mode again */
339             goto callrmproc_again;
340         }
341     } else {
342         TRACE("entering real mode...\n");
343         DOSVM_Enter( context );
344         TRACE("returned from real-mode call\n");
345     }
346     if (alloc) DOSMEM_FreeBlock( addr );
347     return 0;
348 }
349
350
351 /**********************************************************************
352  *          CallRMInt   (WINEDOS.@)
353  */
354 void WINAPI DOSVM_CallRMInt( CONTEXT86 *context )
355 {
356     CONTEXT86 realmode_ctx;
357     FARPROC16 rm_int = DOSVM_GetRMHandler( BL_reg(context) );
358     REALMODECALL *call = MapSL( MAKESEGPTR( context->SegEs, DI_reg(context) ));
359     INT_GetRealModeContext( call, &realmode_ctx );
360
361     /* we need to check if a real-mode program has hooked the interrupt */
362     if (HIWORD(rm_int)!=0xF000) {
363         /* yup, which means we need to switch to real mode... */
364         realmode_ctx.SegCs = HIWORD(rm_int);
365         realmode_ctx.Eip   = LOWORD(rm_int);
366         if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
367           SET_CFLAG(context);
368     } else {
369         RESET_CFLAG(context);
370         /* use the IP we have instead of BL_reg, in case some apps
371            decide to move interrupts around for whatever reason... */
372         DOSVM_RealModeInterrupt( LOWORD(rm_int)/4, &realmode_ctx );
373     }
374     INT_SetRealModeContext( call, &realmode_ctx );
375 }
376
377
378 /**********************************************************************
379  *          CallRMProc   (WINEDOS.@)
380  */
381 void WINAPI DOSVM_CallRMProc( CONTEXT86 *context, int iret )
382 {
383     REALMODECALL *p = MapSL( MAKESEGPTR( context->SegEs, DI_reg(context) ));
384     CONTEXT86 context16;
385
386     TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
387           p->eax, p->ebx, p->ecx, p->edx);
388     TRACE("              ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
389           p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
390
391     if (!(p->cs) && !(p->ip)) { /* remove this check
392                                    if Int21/6501 case map function
393                                    has been implemented */
394         SET_CFLAG(context);
395         return;
396      }
397     INT_GetRealModeContext(p, &context16);
398     DPMI_CallRMProc( &context16, ((LPWORD)MapSL(MAKESEGPTR(context->SegSs, LOWORD(context->Esp))))+3,
399                      CX_reg(context), iret );
400     INT_SetRealModeContext(p, &context16);
401 }
402
403
404 /* (see dosmem.c, function DOSMEM_InitDPMI) */
405 static void StartPM( CONTEXT86 *context )
406 {
407     UINT16 cs, ss, ds, es;
408     CONTEXT86 pm_ctx;
409     DWORD psp_ofs = (DWORD)(DOSVM_psp<<4);
410     PDB16 *psp = (PDB16 *)psp_ofs;
411     HANDLE16 env_seg = psp->environment;
412     unsigned char selflags = WINE_LDT_FLAGS_DATA;
413
414     RESET_CFLAG(context);
415     dpmi_flag = AX_reg(context);
416 /* our mode switch wrapper have placed the desired CS into DX */
417     cs = SELECTOR_AllocBlock( (void *)(DX_reg(context)<<4), 0x10000, WINE_LDT_FLAGS_CODE );
418 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
419    can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
420    ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
421    32-bit code using this stack. */
422     if (dpmi_flag & 1) selflags |= WINE_LDT_FLAGS_32BIT;
423     ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, selflags );
424 /* do the same for the data segments, just in case */
425     if (context->SegDs == context->SegSs) ds = ss;
426     else ds = SELECTOR_AllocBlock( (void *)(context->SegDs<<4), 0x10000, selflags );
427     es = SELECTOR_AllocBlock( psp, 0x100, selflags );
428 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
429     psp->environment = SELECTOR_AllocBlock( (void *)(env_seg<<4), 0x10000, WINE_LDT_FLAGS_DATA );
430
431     pm_ctx = *context;
432     pm_ctx.SegCs = DOSVM_dpmi_segments->dpmi_sel;
433 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
434     pm_ctx.Eax   = ss;
435     pm_ctx.Edx   = cs;
436     pm_ctx.SegDs = ds;
437     pm_ctx.SegEs = es;
438     pm_ctx.SegFs = 0;
439     pm_ctx.SegGs = 0;
440
441     TRACE("DOS program is now entering protected mode\n");
442     wine_call_to_16_regs_short(&pm_ctx, 0);
443
444     /* in the current state of affairs, we won't ever actually return here... */
445     /* we should have int21/ah=4c do it someday, though... */
446
447     FreeSelector16(psp->environment);
448     psp->environment = env_seg;
449     FreeSelector16(es);
450     if (ds != ss) FreeSelector16(ds);
451     FreeSelector16(ss);
452     FreeSelector16(cs);
453 }
454
455 static RMCB *DPMI_AllocRMCB( void )
456 {
457     RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
458     UINT16 uParagraph;
459
460     if (NewRMCB)
461     {
462         LPVOID RMCBmem = DOSMEM_GetBlock(4, &uParagraph);
463         LPBYTE p = RMCBmem;
464
465         *p++ = 0xcd; /* RMCB: */
466         *p++ = 0x31; /* int $0x31 */
467 /* it is the called procedure's task to change the return CS:EIP
468    the DPMI 0.9 spec states that if it doesn't, it will be called again */
469         *p++ = 0xeb;
470         *p++ = 0xfc; /* jmp RMCB */
471         NewRMCB->address = MAKELONG(0, uParagraph);
472         NewRMCB->next = FirstRMCB;
473         FirstRMCB = NewRMCB;
474     }
475     return NewRMCB;
476 }
477
478
479 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
480 {
481     RMCB *NewRMCB = DPMI_AllocRMCB();
482
483     if (NewRMCB) {
484         NewRMCB->proc_ofs = (DWORD)proc;
485         NewRMCB->proc_sel = 0;
486         NewRMCB->regs_ofs = 0;
487         NewRMCB->regs_sel = 0;
488         return (FARPROC16)(NewRMCB->address);
489     }
490     return NULL;
491 }
492
493
494 static int DPMI_FreeRMCB( DWORD address )
495 {
496     RMCB *CurrRMCB = FirstRMCB;
497     RMCB *PrevRMCB = NULL;
498
499     while (CurrRMCB && (CurrRMCB->address != address))
500     {
501         PrevRMCB = CurrRMCB;
502         CurrRMCB = CurrRMCB->next;
503     }
504     if (CurrRMCB)
505     {
506         if (PrevRMCB)
507         PrevRMCB->next = CurrRMCB->next;
508             else
509         FirstRMCB = CurrRMCB->next;
510         DOSMEM_FreeBlock(PTR_REAL_TO_LIN(SELECTOROF(CurrRMCB->address),OFFSETOF(CurrRMCB->address)));
511         HeapFree(GetProcessHeap(), 0, CurrRMCB);
512         return 0;
513     }
514     return 1;
515 }
516
517
518 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
519 {
520     DPMI_FreeRMCB( (DWORD)proc );
521 }
522
523
524 /**********************************************************************
525  *          RawModeSwitch   (WINEDOS.@)
526  *
527  * DPMI Raw Mode Switch handler
528  */
529 void WINAPI DOSVM_RawModeSwitch( CONTEXT86 *context )
530 {
531   CONTEXT86 rm_ctx;
532   int ret;
533
534   /* initialize real-mode context as per spec */
535   memset(&rm_ctx, 0, sizeof(rm_ctx));
536   rm_ctx.SegDs  = AX_reg(context);
537   rm_ctx.SegEs  = CX_reg(context);
538   rm_ctx.SegSs  = DX_reg(context);
539   rm_ctx.Esp    = context->Ebx;
540   rm_ctx.SegCs  = SI_reg(context);
541   rm_ctx.Eip    = context->Edi;
542   rm_ctx.Ebp    = context->Ebp;
543   rm_ctx.SegFs  = 0;
544   rm_ctx.SegGs  = 0;
545   rm_ctx.EFlags = context->EFlags; /* at least we need the IF flag */
546
547   /* enter real mode again */
548   TRACE("re-entering real mode at %04lx:%04lx\n",rm_ctx.SegCs,rm_ctx.Eip);
549   ret = DOSVM_Enter( &rm_ctx );
550   /* when the real-mode stuff call its mode switch address,
551      DOSVM_Enter will return and we will continue here */
552
553   if (ret<0) {
554     ERR("Sync lost!\n");
555     /* if the sync was lost, there's no way to recover */
556     ExitProcess(1);
557   }
558
559   /* alter protected-mode context as per spec */
560   context->SegDs   = LOWORD(rm_ctx.Eax);
561   context->SegEs   = LOWORD(rm_ctx.Ecx);
562   context->SegSs   = LOWORD(rm_ctx.Edx);
563   context->Esp     = rm_ctx.Ebx;
564   context->SegCs   = LOWORD(rm_ctx.Esi);
565   context->Eip     = rm_ctx.Edi;
566   context->Ebp     = rm_ctx.Ebp;
567   context->SegFs   = 0;
568   context->SegGs   = 0;
569
570   /* Return to new address and hope that we didn't mess up */
571   TRACE("re-entering protected mode at %04lx:%08lx\n",
572       context->SegCs, context->Eip);
573 }
574
575
576 /**********************************************************************
577  *          AllocRMCB   (WINEDOS.@)
578  */
579 void WINAPI DOSVM_AllocRMCB( CONTEXT86 *context )
580 {
581     RMCB *NewRMCB = DPMI_AllocRMCB();
582
583     TRACE("Function to call: %04x:%04x\n", (WORD)context->SegDs, SI_reg(context) );
584
585     if (NewRMCB)
586     {
587         /* FIXME: if 32-bit DPMI client, use ESI and EDI */
588         NewRMCB->proc_ofs = LOWORD(context->Esi);
589         NewRMCB->proc_sel = context->SegDs;
590         NewRMCB->regs_ofs = LOWORD(context->Edi);
591         NewRMCB->regs_sel = context->SegEs;
592         SET_LOWORD( context->Ecx, HIWORD(NewRMCB->address) );
593         SET_LOWORD( context->Edx, LOWORD(NewRMCB->address) );
594     }
595     else
596     {
597         SET_LOWORD( context->Eax, 0x8015 ); /* callback unavailable */
598         SET_CFLAG(context);
599     }
600 }
601
602
603 /**********************************************************************
604  *          FreeRMCB   (WINEDOS.@)
605  */
606 void WINAPI DOSVM_FreeRMCB( CONTEXT86 *context )
607 {
608     FIXME("callback address: %04x:%04x\n",
609           CX_reg(context), DX_reg(context));
610
611     if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
612         SET_LOWORD( context->Eax, 0x8024 ); /* invalid callback address */
613         SET_CFLAG(context);
614     }
615 }
616
617
618 /**********************************************************************
619  *          DOSVM_Int31Handler
620  *
621  * Handler for real-mode int 31h (DPMI).
622  */
623 void WINAPI DOSVM_Int31Handler( CONTEXT86 *context )
624 {
625     /* check if it's our wrapper */
626     TRACE("called from real mode\n");
627     if (context->SegCs==DOSVM_dpmi_segments->dpmi_seg) {
628         /* This is the protected mode switch */
629         StartPM(context);
630         return;
631     }
632     else if (context->SegCs==DOSVM_dpmi_segments->xms_seg)
633     {
634         /* This is the XMS driver entry point */
635         XMS_Handler(context);
636         return;
637     }
638     else
639     {
640         /* Check for RMCB */
641         RMCB *CurrRMCB = FirstRMCB;
642
643         while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
644             CurrRMCB = CurrRMCB->next;
645
646         if (CurrRMCB) {
647             /* RMCB call, propagate to protected-mode handler */
648             DPMI_CallRMCBProc(context, CurrRMCB, dpmi_flag);
649             return;
650         }
651     }
652
653     /* chain to protected mode handler */
654     INT_Int31Handler( context );
655 }