Implemented recursive RegDeleteKey in Win95 emulation.
[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  *          INT_GetRealModeContext
69  */
70 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
71 {
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;
88 }
89
90
91 /**********************************************************************
92  *          INT_SetRealModeContext
93  */
94 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
95 {
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;
112 }
113
114
115 #ifdef __i386__
116
117 void DPMI_CallRMCB32(RMCB *rmcb, UINT16 ss, DWORD esp, UINT16*es, DWORD*edi)
118 #if 0 /* original code, which early gccs puke on */
119 {
120     int _clobber;
121     __asm__ __volatile__(
122         "pushl %%ebp\n"
123         "pushl %%ebx\n"
124         "pushl %%es\n"
125         "pushl %%ds\n"
126         "pushfl\n"
127         "mov %7,%%es\n"
128         "mov %5,%%ds\n"
129         ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
130         "popl %%ds\n"
131         "mov %%es,%0\n"
132         "popl %%es\n"
133         "popl %%ebx\n"
134         "popl %%ebp\n"
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) );
139 }
140 #else /* code generated by a gcc new enough */
141 ;
142 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32,
143     "pushl %ebp\n\t"
144     "movl %esp,%ebp\n\t"
145     "pushl %edi\n\t"
146     "pushl %esi\n\t"
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"
152     "addl $0x4,%eax\n\t"
153     "pushl %ebp\n\t"
154     "pushl %ebx\n\t"
155     "pushl %es\n\t"
156     "pushl %ds\n\t"
157     "pushfl\n\t"
158     "mov %cx,%es\n\t"
159     "mov %dx,%ds\n\t"
160     ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
161     "popl %ds\n\t"
162     "mov %es,%dx\n\t"
163     "popl %es\n\t"
164     "popl %ebx\n\t"
165     "popl %ebp\n\t"
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"
170     "popl %esi\n\t"
171     "popl %edi\n\t"
172     "leave\n\t"
173     "ret")
174 #endif
175
176 #endif /* __i386__ */
177
178 /**********************************************************************
179  *          DPMI_CallRMCBProc
180  *
181  * This routine does the hard work of calling a callback procedure.
182  */
183 static void DPMI_CallRMCBProc( CONTEXT86 *context, RMCB *rmcb, WORD flag )
184 {
185     if (IS_SELECTOR_SYSTEM( rmcb->proc_sel )) {
186         /* Wine-internal RMCB, call directly */
187         ((RMCBPROC)rmcb->proc_ofs)(context);
188     } else {
189 #ifdef __i386__
190         UINT16 ss,es;
191         DWORD esp,edi;
192
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 );
195         esp = context->Esp;
196
197         FIXME("untested!\n");
198
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
202          * It returns:
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. */
206         if (flag & 1) {
207             /* 32-bit DPMI client */
208             DPMI_CallRMCB32(rmcb, ss, esp, &es, &edi);
209         } else {
210             /* 16-bit DPMI client */
211             CONTEXT86 ctx = *context;
212             ctx.SegCs = rmcb->proc_sel;
213             ctx.Eip   = rmcb->proc_ofs;
214             ctx.SegDs = ss;
215             ctx.Esi   = esp;
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);
220             es = ctx.SegEs;
221             edi = ctx.Edi;
222         }
223         FreeSelector16(ss);
224         INT_GetRealModeContext( MapSL( MAKESEGPTR( es, edi )), context);
225 #else
226         ERR("RMCBs only implemented for i386\n");
227 #endif
228     }
229 }
230
231
232 /**********************************************************************
233  *          DPMI_CallRMProc
234  *
235  * This routine does the hard work of calling a real mode procedure.
236  */
237 int DPMI_CallRMProc( CONTEXT86 *context, LPWORD stack, int args, int iret )
238 {
239     LPWORD stack16;
240     LPVOID addr = NULL; /* avoid gcc warning */
241     RMCB *CurrRMCB;
242     int alloc = 0, already = 0;
243     BYTE *code;
244
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" );
250
251 callrmproc_again:
252
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);
256     switch (*code) {
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;
270     }
271
272 /* shortcut for chaining to internal interrupt handlers */
273     if ((context->SegCs == 0xF000) && iret)
274     {
275         DOSVM_RealModeInterrupt( LOWORD(context->Eip)/4, context);
276         return 0;
277     }
278
279 /* shortcut for RMCBs */
280     CurrRMCB = FirstRMCB;
281
282     while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
283         CurrRMCB = CurrRMCB->next;
284
285     if (!CurrRMCB && !MZ_Current())
286     {
287         FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
288         TRACE("creating VM86 task\n");
289         MZ_AllocDPMITask();
290     }
291     if (!already) {
292         if (!context->SegSs) {
293             alloc = 1; /* allocate default stack */
294             stack16 = addr = DOSMEM_GetBlock( 64, (UINT16 *)&(context->SegSs) );
295             context->Esp = 64-2;
296             stack16 += 32-1;
297             if (!addr) {
298                 ERR("could not allocate default stack\n");
299                 return 1;
300             }
301         } else {
302             stack16 = CTX_SEG_OFF_TO_LIN(context, context->SegSs, context->Esp);
303         }
304         context->Esp -= (args + (iret?1:0)) * sizeof(WORD);
305         stack16 -= args;
306         if (args) memcpy(stack16, stack, args*sizeof(WORD) );
307         /* push flags if iret */
308         if (iret) {
309             stack16--; args++;
310             *stack16 = LOWORD(context->EFlags);
311         }
312         /* push return address (return to interrupt wrapper) */
313         *(--stack16) = DOSMEM_wrap_seg;
314         *(--stack16) = 0;
315         /* adjust stack */
316         context->Esp -= 2*sizeof(WORD);
317         already = 1;
318     }
319
320     if (CurrRMCB) {
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;
329         }
330     } else {
331         TRACE("entering real mode...\n");
332         DOSVM_Enter( context );
333         TRACE("returned from real-mode call\n");
334     }
335     if (alloc) DOSMEM_FreeBlock( addr );
336     return 0;
337 }
338
339
340 /**********************************************************************
341  *          CallRMInt   (WINEDOS.@)
342  */
343 void WINAPI DOSVM_CallRMInt( CONTEXT86 *context )
344 {
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 );
349
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))
356           SET_CFLAG(context);
357     } else {
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 );
362     }
363     INT_SetRealModeContext( call, &realmode_ctx );
364 }
365
366
367 /**********************************************************************
368  *          CallRMProc   (WINEDOS.@)
369  */
370 void WINAPI DOSVM_CallRMProc( CONTEXT86 *context, int iret )
371 {
372     REALMODECALL *p = MapSL( MAKESEGPTR( context->SegEs, DI_reg(context) ));
373     CONTEXT86 context16;
374
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" );
379
380     if (!(p->cs) && !(p->ip)) { /* remove this check
381                                    if Int21/6501 case map function
382                                    has been implemented */
383         SET_CFLAG(context);
384         return;
385      }
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);
390 }
391
392
393 /* (see dosmem.c, function DOSMEM_InitDPMI) */
394 static void StartPM( CONTEXT86 *context )
395 {
396     UINT16 cs, ss, ds, es;
397     CONTEXT86 pm_ctx;
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;
402
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 );
419
420     pm_ctx = *context;
421     pm_ctx.SegCs = DOSMEM_dpmi_sel;
422 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
423     pm_ctx.Eax   = ss;
424     pm_ctx.Edx   = cs;
425     pm_ctx.SegDs = ds;
426     pm_ctx.SegEs = es;
427     pm_ctx.SegFs = 0;
428     pm_ctx.SegGs = 0;
429
430     TRACE("DOS program is now entering protected mode\n");
431     wine_call_to_16_regs_short(&pm_ctx, 0);
432
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... */
435
436     FreeSelector16(psp->environment);
437     psp->environment = env_seg;
438     FreeSelector16(es);
439     if (ds != ss) FreeSelector16(ds);
440     FreeSelector16(ss);
441     FreeSelector16(cs);
442 }
443
444 static RMCB *DPMI_AllocRMCB( void )
445 {
446     RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
447     UINT16 uParagraph;
448
449     if (NewRMCB)
450     {
451         LPVOID RMCBmem = DOSMEM_GetBlock(4, &uParagraph);
452         LPBYTE p = RMCBmem;
453
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 */
458         *p++ = 0xeb;
459         *p++ = 0xfc; /* jmp RMCB */
460         NewRMCB->address = MAKELONG(0, uParagraph);
461         NewRMCB->next = FirstRMCB;
462         FirstRMCB = NewRMCB;
463     }
464     return NewRMCB;
465 }
466
467
468 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
469 {
470     RMCB *NewRMCB = DPMI_AllocRMCB();
471
472     if (NewRMCB) {
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);
478     }
479     return NULL;
480 }
481
482
483 static int DPMI_FreeRMCB( DWORD address )
484 {
485     RMCB *CurrRMCB = FirstRMCB;
486     RMCB *PrevRMCB = NULL;
487
488     while (CurrRMCB && (CurrRMCB->address != address))
489     {
490         PrevRMCB = CurrRMCB;
491         CurrRMCB = CurrRMCB->next;
492     }
493     if (CurrRMCB)
494     {
495         if (PrevRMCB)
496         PrevRMCB->next = CurrRMCB->next;
497             else
498         FirstRMCB = CurrRMCB->next;
499         DOSMEM_FreeBlock(DOSMEM_MapRealToLinear(CurrRMCB->address));
500         HeapFree(GetProcessHeap(), 0, CurrRMCB);
501         return 0;
502     }
503     return 1;
504 }
505
506
507 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
508 {
509     DPMI_FreeRMCB( (DWORD)proc );
510 }
511
512
513 /**********************************************************************
514  *          RawModeSwitch   (WINEDOS.@)
515  *
516  * DPMI Raw Mode Switch handler
517  */
518 void WINAPI DOSVM_RawModeSwitch( CONTEXT86 *context )
519 {
520   CONTEXT86 rm_ctx;
521   int ret;
522
523   /* initialize real-mode context as per spec */
524   memset(&rm_ctx, 0, sizeof(rm_ctx));
525   rm_ctx.SegDs  = AX_reg(context);
526   rm_ctx.SegEs  = CX_reg(context);
527   rm_ctx.SegSs  = DX_reg(context);
528   rm_ctx.Esp    = context->Ebx;
529   rm_ctx.SegCs  = SI_reg(context);
530   rm_ctx.Eip    = context->Edi;
531   rm_ctx.Ebp    = context->Ebp;
532   rm_ctx.SegFs  = 0;
533   rm_ctx.SegGs  = 0;
534   rm_ctx.EFlags = context->EFlags; /* at least we need the IF flag */
535
536   /* enter real mode again */
537   TRACE("re-entering real mode at %04lx:%04lx\n",rm_ctx.SegCs,rm_ctx.Eip);
538   ret = DOSVM_Enter( &rm_ctx );
539   /* when the real-mode stuff call its mode switch address,
540      DOSVM_Enter will return and we will continue here */
541
542   if (ret<0) {
543     ERR("Sync lost!\n");
544     /* if the sync was lost, there's no way to recover */
545     ExitProcess(1);
546   }
547
548   /* alter protected-mode context as per spec */
549   context->SegDs   = LOWORD(rm_ctx.Eax);
550   context->SegEs   = LOWORD(rm_ctx.Ecx);
551   context->SegSs   = LOWORD(rm_ctx.Edx);
552   context->Esp     = rm_ctx.Ebx;
553   context->SegCs   = LOWORD(rm_ctx.Esi);
554   context->Eip     = rm_ctx.Edi;
555   context->Ebp     = rm_ctx.Ebp;
556   context->SegFs   = 0;
557   context->SegGs   = 0;
558
559   /* Return to new address and hope that we didn't mess up */
560   TRACE("re-entering protected mode at %04lx:%08lx\n",
561       context->SegCs, context->Eip);
562 }
563
564
565 /**********************************************************************
566  *          AllocRMCB   (WINEDOS.@)
567  */
568 void WINAPI DOSVM_AllocRMCB( CONTEXT86 *context )
569 {
570     RMCB *NewRMCB = DPMI_AllocRMCB();
571
572     TRACE("Function to call: %04x:%04x\n", (WORD)context->SegDs, SI_reg(context) );
573
574     if (NewRMCB)
575     {
576         /* FIXME: if 32-bit DPMI client, use ESI and EDI */
577         NewRMCB->proc_ofs = LOWORD(context->Esi);
578         NewRMCB->proc_sel = context->SegDs;
579         NewRMCB->regs_ofs = LOWORD(context->Edi);
580         NewRMCB->regs_sel = context->SegEs;
581         SET_LOWORD( context->Ecx, HIWORD(NewRMCB->address) );
582         SET_LOWORD( context->Edx, LOWORD(NewRMCB->address) );
583     }
584     else
585     {
586         SET_LOWORD( context->Eax, 0x8015 ); /* callback unavailable */
587         SET_CFLAG(context);
588     }
589 }
590
591
592 /**********************************************************************
593  *          FreeRMCB   (WINEDOS.@)
594  */
595 void WINAPI DOSVM_FreeRMCB( CONTEXT86 *context )
596 {
597     FIXME("callback address: %04x:%04x\n",
598           CX_reg(context), DX_reg(context));
599
600     if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
601         SET_LOWORD( context->Eax, 0x8024 ); /* invalid callback address */
602         SET_CFLAG(context);
603     }
604 }
605
606
607 /**********************************************************************
608  *          DOSVM_Int31Handler
609  *
610  * Handler for real-mode int 31h (DPMI).
611  */
612 void WINAPI DOSVM_Int31Handler( CONTEXT86 *context )
613 {
614     /* check if it's our wrapper */
615     TRACE("called from real mode\n");
616     if (context->SegCs==DOSMEM_dpmi_seg) {
617         /* This is the protected mode switch */
618         StartPM(context);
619         return;
620     }
621     else if (context->SegCs==DOSMEM_xms_seg)
622     {
623         /* This is the XMS driver entry point */
624         XMS_Handler(context);
625         return;
626     }
627     else
628     {
629         /* Check for RMCB */
630         RMCB *CurrRMCB = FirstRMCB;
631
632         while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
633             CurrRMCB = CurrRMCB->next;
634
635         if (CurrRMCB) {
636             /* RMCB call, propagate to protected-mode handler */
637             DPMI_CallRMCBProc(context, CurrRMCB, dpmi_flag);
638             return;
639         }
640     }
641
642     /* chain to protected mode handler */
643     INT_Int31Handler( context );
644 }