Added a framework for testing CreateProcess and a few tests.
[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
23 #include "windef.h"
24 #include "wine/winbase16.h"
25 #include "miscemu.h"
26 #include "task.h"
27 #include "msdos.h"
28 #include "dosexe.h"
29
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(int31);
33
34 /* Structure for real-mode callbacks */
35 typedef struct
36 {
37     DWORD edi;
38     DWORD esi;
39     DWORD ebp;
40     DWORD reserved;
41     DWORD ebx;
42     DWORD edx;
43     DWORD ecx;
44     DWORD eax;
45     WORD  fl;
46     WORD  es;
47     WORD  ds;
48     WORD  fs;
49     WORD  gs;
50     WORD  ip;
51     WORD  cs;
52     WORD  sp;
53     WORD  ss;
54 } REALMODECALL;
55
56 typedef struct tagRMCB {
57     DWORD address;
58     DWORD proc_ofs,proc_sel;
59     DWORD regs_ofs,regs_sel;
60     struct tagRMCB *next;
61 } RMCB;
62
63 static RMCB *FirstRMCB = NULL;
64 static WORD dpmi_flag;
65
66 /**********************************************************************
67  *          INT_GetRealModeContext
68  */
69 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
70 {
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;
87 }
88
89
90 /**********************************************************************
91  *          INT_SetRealModeContext
92  */
93 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
94 {
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;
111 }
112
113
114 #ifdef __i386__
115
116 void DPMI_CallRMCB32(RMCB *rmcb, UINT16 ss, DWORD esp, UINT16*es, DWORD*edi)
117 #if 0 /* original code, which early gccs puke on */
118 {
119     int _clobber;
120     __asm__ __volatile__(
121         "pushl %%ebp\n"
122         "pushl %%ebx\n"
123         "pushl %%es\n"
124         "pushl %%ds\n"
125         "pushfl\n"
126         "mov %7,%%es\n"
127         "mov %5,%%ds\n"
128         ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
129         "popl %%ds\n"
130         "mov %%es,%0\n"
131         "popl %%es\n"
132         "popl %%ebx\n"
133         "popl %%ebp\n"
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) );
138 }
139 #else /* code generated by a gcc new enough */
140 ;
141 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32,
142     "pushl %ebp\n\t"
143     "movl %esp,%ebp\n\t"
144     "pushl %edi\n\t"
145     "pushl %esi\n\t"
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"
151     "addl $0x4,%eax\n\t"
152     "pushl %ebp\n\t"
153     "pushl %ebx\n\t"
154     "pushl %es\n\t"
155     "pushl %ds\n\t"
156     "pushfl\n\t"
157     "mov %cx,%es\n\t"
158     "mov %dx,%ds\n\t"
159     ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
160     "popl %ds\n\t"
161     "mov %es,%dx\n\t"
162     "popl %es\n\t"
163     "popl %ebx\n\t"
164     "popl %ebp\n\t"
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"
169     "popl %esi\n\t"
170     "popl %edi\n\t"
171     "leave\n\t"
172     "ret")
173 #endif
174
175 #endif /* __i386__ */
176
177 /**********************************************************************
178  *          DPMI_CallRMCBProc
179  *
180  * This routine does the hard work of calling a callback procedure.
181  */
182 static void DPMI_CallRMCBProc( CONTEXT86 *context, RMCB *rmcb, WORD flag )
183 {
184     if (IS_SELECTOR_SYSTEM( rmcb->proc_sel )) {
185         /* Wine-internal RMCB, call directly */
186         ((RMCBPROC)rmcb->proc_ofs)(context);
187     } else {
188 #ifdef __i386__
189         UINT16 ss,es;
190         DWORD esp,edi;
191
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 );
194         esp = context->Esp;
195
196         FIXME("untested!\n");
197
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
201          * It returns:
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. */
205         if (flag & 1) {
206             /* 32-bit DPMI client */
207             DPMI_CallRMCB32(rmcb, ss, esp, &es, &edi);
208         } else {
209             /* 16-bit DPMI client */
210             CONTEXT86 ctx = *context;
211             ctx.SegCs = rmcb->proc_sel;
212             ctx.Eip   = rmcb->proc_ofs;
213             ctx.SegDs = ss;
214             ctx.Esi   = esp;
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);
219             es = ctx.SegEs;
220             edi = ctx.Edi;
221         }
222         FreeSelector16(ss);
223         INT_GetRealModeContext( MapSL( MAKESEGPTR( es, edi )), context);
224 #else
225         ERR("RMCBs only implemented for i386\n");
226 #endif
227     }
228 }
229
230
231 /**********************************************************************
232  *          DPMI_CallRMProc
233  *
234  * This routine does the hard work of calling a real mode procedure.
235  */
236 int DPMI_CallRMProc( CONTEXT86 *context, LPWORD stack, int args, int iret )
237 {
238     LPWORD stack16;
239     LPVOID addr = NULL; /* avoid gcc warning */
240     RMCB *CurrRMCB;
241     int alloc = 0, already = 0;
242     BYTE *code;
243
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" );
249
250 callrmproc_again:
251
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);
255     switch (*code) {
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;
269     }
270
271 /* shortcut for chaining to internal interrupt handlers */
272     if ((context->SegCs == 0xF000) && iret)
273     {
274         DOSVM_RealModeInterrupt( LOWORD(context->Eip)/4, context);
275         return 0;
276     }
277
278 /* shortcut for RMCBs */
279     CurrRMCB = FirstRMCB;
280
281     while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
282         CurrRMCB = CurrRMCB->next;
283
284     if (!CurrRMCB && !MZ_Current())
285     {
286         FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
287         TRACE("creating VM86 task\n");
288         MZ_AllocDPMITask();
289     }
290     if (!already) {
291         if (!context->SegSs) {
292             alloc = 1; /* allocate default stack */
293             stack16 = addr = DOSMEM_GetBlock( 64, (UINT16 *)&(context->SegSs) );
294             context->Esp = 64-2;
295             stack16 += 32-1;
296             if (!addr) {
297                 ERR("could not allocate default stack\n");
298                 return 1;
299             }
300         } else {
301             stack16 = CTX_SEG_OFF_TO_LIN(context, context->SegSs, context->Esp);
302         }
303         context->Esp -= (args + (iret?1:0)) * sizeof(WORD);
304         stack16 -= args;
305         if (args) memcpy(stack16, stack, args*sizeof(WORD) );
306         /* push flags if iret */
307         if (iret) {
308             stack16--; args++;
309             *stack16 = LOWORD(context->EFlags);
310         }
311         /* push return address (return to interrupt wrapper) */
312         *(--stack16) = DOSMEM_wrap_seg;
313         *(--stack16) = 0;
314         /* adjust stack */
315         context->Esp -= 2*sizeof(WORD);
316         already = 1;
317     }
318
319     if (CurrRMCB) {
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;
328         }
329     } else {
330         TRACE("entering real mode...\n");
331         DOSVM_Enter( context );
332         TRACE("returned from real-mode call\n");
333     }
334     if (alloc) DOSMEM_FreeBlock( addr );
335     return 0;
336 }
337
338
339 /**********************************************************************
340  *          CallRMInt   (WINEDOS.@)
341  */
342 void WINAPI DOSVM_CallRMInt( CONTEXT86 *context )
343 {
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 );
348
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))
355           SET_CFLAG(context);
356     } else {
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 );
361     }
362     INT_SetRealModeContext( call, &realmode_ctx );
363 }
364
365
366 /**********************************************************************
367  *          CallRMProc   (WINEDOS.@)
368  */
369 void WINAPI DOSVM_CallRMProc( CONTEXT86 *context, int iret )
370 {
371     REALMODECALL *p = MapSL( MAKESEGPTR( context->SegEs, DI_reg(context) ));
372     CONTEXT86 context16;
373
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" );
378
379     if (!(p->cs) && !(p->ip)) { /* remove this check
380                                    if Int21/6501 case map function
381                                    has been implemented */
382         SET_CFLAG(context);
383         return;
384      }
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);
389 }
390
391
392 /* (see dosmem.c, function DOSMEM_InitDPMI) */
393 static void StartPM( CONTEXT86 *context )
394 {
395     UINT16 cs, ss, ds, es;
396     CONTEXT86 pm_ctx;
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;
401
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 );
418
419     pm_ctx = *context;
420     pm_ctx.SegCs = DOSMEM_dpmi_sel;
421 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
422     pm_ctx.Eax   = ss;
423     pm_ctx.Edx   = cs;
424     pm_ctx.SegDs = ds;
425     pm_ctx.SegEs = es;
426     pm_ctx.SegFs = 0;
427     pm_ctx.SegGs = 0;
428
429     TRACE("DOS program is now entering protected mode\n");
430     wine_call_to_16_regs_short(&pm_ctx, 0);
431
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... */
434
435     FreeSelector16(psp->environment);
436     psp->environment = env_seg;
437     FreeSelector16(es);
438     if (ds != ss) FreeSelector16(ds);
439     FreeSelector16(ss);
440     FreeSelector16(cs);
441 }
442
443 static RMCB *DPMI_AllocRMCB( void )
444 {
445     RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
446     UINT16 uParagraph;
447
448     if (NewRMCB)
449     {
450         LPVOID RMCBmem = DOSMEM_GetBlock(4, &uParagraph);
451         LPBYTE p = RMCBmem;
452
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 */
457         *p++ = 0xeb;
458         *p++ = 0xfc; /* jmp RMCB */
459         NewRMCB->address = MAKELONG(0, uParagraph);
460         NewRMCB->next = FirstRMCB;
461         FirstRMCB = NewRMCB;
462     }
463     return NewRMCB;
464 }
465
466
467 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
468 {
469     RMCB *NewRMCB = DPMI_AllocRMCB();
470
471     if (NewRMCB) {
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);
477     }
478     return NULL;
479 }
480
481
482 static int DPMI_FreeRMCB( DWORD address )
483 {
484     RMCB *CurrRMCB = FirstRMCB;
485     RMCB *PrevRMCB = NULL;
486
487     while (CurrRMCB && (CurrRMCB->address != address))
488     {
489         PrevRMCB = CurrRMCB;
490         CurrRMCB = CurrRMCB->next;
491     }
492     if (CurrRMCB)
493     {
494         if (PrevRMCB)
495         PrevRMCB->next = CurrRMCB->next;
496             else
497         FirstRMCB = CurrRMCB->next;
498         DOSMEM_FreeBlock(DOSMEM_MapRealToLinear(CurrRMCB->address));
499         HeapFree(GetProcessHeap(), 0, CurrRMCB);
500         return 0;
501     }
502     return 1;
503 }
504
505
506 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
507 {
508     DPMI_FreeRMCB( (DWORD)proc );
509 }
510
511
512 /* DPMI Raw Mode Switch handler */
513
514 #if 0
515 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
516 {
517   LPDOSTASK lpDosTask = MZ_Current();
518   CONTEXT86 rm_ctx;
519   int ret;
520
521   if (!lpDosTask) {
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");
525     ExitProcess(1);
526   }
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);
536   rm_ctx.SegFs  = 0;
537   rm_ctx.SegGs  = 0;
538   rm_ctx.EFlags = EFL_sig(context); /* at least we need the IF flag */
539
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 */
545
546   if (ret<0) {
547     /* if the sync was lost, there's no way to recover */
548     ExitProcess(1);
549   }
550
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;
559   FS_sig(context) = 0;
560   GS_sig(context) = 0;
561
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));
565 }
566 #endif
567
568
569 /**********************************************************************
570  *          AllocRMCB   (WINEDOS.@)
571  */
572 void WINAPI DOSVM_AllocRMCB( CONTEXT86 *context )
573 {
574     RMCB *NewRMCB = DPMI_AllocRMCB();
575
576     TRACE("Function to call: %04x:%04x\n", (WORD)context->SegDs, SI_reg(context) );
577
578     if (NewRMCB)
579     {
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) );
587     }
588     else
589     {
590         SET_LOWORD( context->Eax, 0x8015 ); /* callback unavailable */
591         SET_CFLAG(context);
592     }
593 }
594
595
596 /**********************************************************************
597  *          FreeRMCB   (WINEDOS.@)
598  */
599 void WINAPI DOSVM_FreeRMCB( CONTEXT86 *context )
600 {
601     FIXME("callback address: %04x:%04x\n",
602           CX_reg(context), DX_reg(context));
603
604     if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
605         SET_LOWORD( context->Eax, 0x8024 ); /* invalid callback address */
606         SET_CFLAG(context);
607     }
608 }
609
610
611 /**********************************************************************
612  *          DOSVM_Int31Handler
613  *
614  * Handler for real-mode int 31h (DPMI).
615  */
616 void WINAPI DOSVM_Int31Handler( CONTEXT86 *context )
617 {
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 */
622         StartPM(context);
623         return;
624     }
625     else if (context->SegCs==DOSMEM_xms_seg)
626     {
627         /* This is the XMS driver entry point */
628         XMS_Handler(context);
629         return;
630     }
631     else
632     {
633         /* Check for RMCB */
634         RMCB *CurrRMCB = FirstRMCB;
635
636         while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
637             CurrRMCB = CurrRMCB->next;
638
639         if (CurrRMCB) {
640             /* RMCB call, propagate to protected-mode handler */
641             DPMI_CallRMCBProc(context, CurrRMCB, dpmi_flag);
642             return;
643         }
644     }
645
646     /* chain to protected mode handler */
647     INT_Int31Handler( context );
648 }