d3d10core: Allow a NULL vertex buffer to be set in IASetVertexBuffers().
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winternl.h"
29 #include "wine/winbase16.h"
30 #include "wownt32.h"
31 #include "dosexe.h"
32
33 #include "excpt.h"
34 #include "wine/debug.h"
35 #include "wine/exception.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(int31);
38
39 /* Structure for real-mode callbacks */
40 typedef struct
41 {
42     DWORD edi;
43     DWORD esi;
44     DWORD ebp;
45     DWORD reserved;
46     DWORD ebx;
47     DWORD edx;
48     DWORD ecx;
49     DWORD eax;
50     WORD  fl;
51     WORD  es;
52     WORD  ds;
53     WORD  fs;
54     WORD  gs;
55     WORD  ip;
56     WORD  cs;
57     WORD  sp;
58     WORD  ss;
59 } REALMODECALL;
60
61 typedef struct tagRMCB {
62     DWORD address;
63     DWORD proc_ofs,proc_sel;
64     DWORD regs_ofs,regs_sel;
65     struct tagRMCB *next;
66 } RMCB;
67
68 static RMCB *FirstRMCB = NULL;
69 static WORD dpmi_flag;
70 static void* lastvalloced = NULL;
71 static BYTE DPMI_retval;
72
73 /**********************************************************************
74  *          DOSVM_IsDos32
75  * 
76  * Return TRUE if we are in 32-bit protected mode DOS process.
77  */
78 BOOL DOSVM_IsDos32(void)
79 {
80   return (dpmi_flag & 1) ? TRUE : FALSE;
81 }
82
83
84 /**********************************************************************
85  *          alloc_pm_selector
86  *
87  * Allocate a 64k sized selector corresponding to a real mode segment.
88  */
89 static WORD alloc_pm_selector( WORD seg, unsigned char flags )
90 {
91     WORD sel = wine_ldt_alloc_entries( 1 );
92
93     if (sel)
94     {
95         LDT_ENTRY entry;
96         wine_ldt_set_base( &entry, (void *)(seg << 4) );
97         wine_ldt_set_limit( &entry, 0xffff );
98         wine_ldt_set_flags( &entry, flags );
99         wine_ldt_set_entry( sel, &entry );
100     }
101     return sel;
102 }
103
104
105 /**********************************************************************
106  *          dpmi_exception_handler
107  *
108  * Handle EXCEPTION_VM86_STI exceptions generated
109  * when there are pending asynchronous events.
110  */
111 static LONG WINAPI dpmi_exception_handler(EXCEPTION_POINTERS *eptr)
112 {
113 #ifdef __i386__
114     EXCEPTION_RECORD *rec = eptr->ExceptionRecord;
115     CONTEXT *context = eptr->ContextRecord;
116
117     if (rec->ExceptionCode == EXCEPTION_VM86_STI)
118     {
119         if (ISV86(context))
120             ERR( "Real mode STI caught by protected mode handler!\n" );
121         DOSVM_SendQueuedEvents(context);
122         return EXCEPTION_CONTINUE_EXECUTION;
123     }
124     else if (rec->ExceptionCode == EXCEPTION_VM86_INTx)
125     {
126         if (ISV86(context))
127             ERR( "Real mode INTx caught by protected mode handler!\n" );
128         DPMI_retval = (BYTE)rec->ExceptionInformation[0];
129         return EXCEPTION_EXECUTE_HANDLER;
130     }
131
132 #endif
133     return EXCEPTION_CONTINUE_SEARCH;
134 }
135
136
137 /**********************************************************************
138  *          INT_GetRealModeContext
139  */
140 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
141 {
142     context->Eax    = call->eax;
143     context->Ebx    = call->ebx;
144     context->Ecx    = call->ecx;
145     context->Edx    = call->edx;
146     context->Esi    = call->esi;
147     context->Edi    = call->edi;
148     context->Ebp    = call->ebp;
149     context->EFlags = call->fl | V86_FLAG;
150     context->Eip    = call->ip;
151     context->Esp    = call->sp;
152     context->SegCs  = call->cs;
153     context->SegDs  = call->ds;
154     context->SegEs  = call->es;
155     context->SegFs  = call->fs;
156     context->SegGs  = call->gs;
157     context->SegSs  = call->ss;
158 }
159
160
161 /**********************************************************************
162  *          INT_SetRealModeContext
163  */
164 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
165 {
166     call->eax = context->Eax;
167     call->ebx = context->Ebx;
168     call->ecx = context->Ecx;
169     call->edx = context->Edx;
170     call->esi = context->Esi;
171     call->edi = context->Edi;
172     call->ebp = context->Ebp;
173     call->fl  = LOWORD(context->EFlags);
174     call->ip  = LOWORD(context->Eip);
175     call->sp  = LOWORD(context->Esp);
176     call->cs  = context->SegCs;
177     call->ds  = context->SegDs;
178     call->es  = context->SegEs;
179     call->fs  = context->SegFs;
180     call->gs  = context->SegGs;
181     call->ss  = context->SegSs;
182 }
183
184 /**********************************************************************
185  *          DPMI_xalloc
186  * special virtualalloc, allocates linearly monoton growing memory.
187  * (the usual VirtualAlloc does not satisfy that restriction)
188  */
189 static LPVOID DPMI_xalloc( DWORD len ) 
190 {
191     LPVOID  ret;
192     LPVOID  oldlastv = lastvalloced;
193
194     if (lastvalloced) 
195     {
196         int xflag = 0;
197
198         ret = NULL;
199         while (!ret) 
200         {
201             ret = VirtualAlloc( lastvalloced, len,
202                                 MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE );
203             if (!ret)
204                 lastvalloced = (char *) lastvalloced + 0x10000;
205
206             /* we failed to allocate one in the first round.
207              * try non-linear
208              */
209             if (!xflag && (lastvalloced<oldlastv)) 
210             { 
211                 /* wrapped */
212                 FIXME( "failed to allocate linearly growing memory (%d bytes), "
213                        "using non-linear growing...\n", len );
214                 xflag++;
215             }
216
217             /* if we even fail to allocate something in the next
218              * round, return NULL
219              */
220             if ((xflag==1) && (lastvalloced >= oldlastv))
221                 xflag++;
222
223             if ((xflag==2) && (lastvalloced < oldlastv)) {
224                 FIXME( "failed to allocate any memory of %d bytes!\n", len );
225                 return NULL;
226             }
227         }
228     } 
229     else
230     {
231         ret = VirtualAlloc( NULL, len, 
232                             MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE );
233     }
234
235     lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
236     return ret;
237 }
238
239 /**********************************************************************
240  *          DPMI_xfree
241  */
242 static void DPMI_xfree( LPVOID ptr ) 
243 {
244     VirtualFree( ptr, 0, MEM_RELEASE );
245 }
246
247 /**********************************************************************
248  *          DPMI_xrealloc
249  *
250  * FIXME: perhaps we could grow this mapped area... 
251  */
252 static LPVOID DPMI_xrealloc( LPVOID ptr, DWORD newsize ) 
253 {
254     MEMORY_BASIC_INFORMATION        mbi;
255     LPVOID                          newptr;
256
257     newptr = DPMI_xalloc( newsize );
258     if (ptr) 
259     {
260         if (!VirtualQuery(ptr,&mbi,sizeof(mbi))) 
261         {
262             FIXME( "realloc of DPMI_xallocd region %p?\n", ptr );
263             return NULL;
264         }
265
266         if (mbi.State == MEM_FREE) 
267         {
268             FIXME( "realloc of DPMI_xallocd region %p?\n", ptr );
269             return NULL;
270         }
271
272         /* We do not shrink allocated memory. most reallocs
273          * only do grows anyway
274          */
275         if (newsize <= mbi.RegionSize)
276             return ptr;
277
278         memcpy( newptr, ptr, mbi.RegionSize );
279         DPMI_xfree( ptr );
280     }
281
282     return newptr;
283 }
284
285
286 #ifdef __i386__
287
288 void DPMI_CallRMCB32(RMCB *rmcb, UINT16 ss, DWORD esp, UINT16*es, DWORD*edi)
289 #if 0 /* original code, which early gccs puke on */
290 {
291     int _clobber;
292     __asm__ __volatile__(
293         "pushl %%ebp\n"
294         "pushl %%ebx\n"
295         "pushl %%es\n"
296         "pushl %%ds\n"
297         "pushfl\n"
298         "mov %7,%%es\n"
299         "mov %5,%%ds\n"
300         ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
301         "popl %%ds\n"
302         "mov %%es,%0\n"
303         "popl %%es\n"
304         "popl %%ebx\n"
305         "popl %%ebp\n"
306     : "=d" (*es), "=D" (*edi), "=S" (_clobber), "=a" (_clobber), "=c" (_clobber)
307     : "0" (ss), "2" (esp),
308       "4" (rmcb->regs_sel), "1" (rmcb->regs_ofs),
309       "3" (&rmcb->proc_ofs) );
310 }
311 #else /* code generated by a gcc new enough */
312 ;
313 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32,
314     "pushl %ebp\n\t"
315     __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
316     __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
317     "movl %esp,%ebp\n\t"
318     __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
319     "pushl %edi\n\t"
320     __ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
321     "pushl %esi\n\t"
322     __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
323     "movl 0x8(%ebp),%eax\n\t"
324     "movl 0x10(%ebp),%esi\n\t"
325     "movl 0xc(%ebp),%edx\n\t"
326     "movl 0x10(%eax),%ecx\n\t"
327     "movl 0xc(%eax),%edi\n\t"
328     "addl $0x4,%eax\n\t"
329     "pushl %ebp\n\t"
330     "pushl %ebx\n\t"
331     "pushl %es\n\t"
332     "pushl %ds\n\t"
333     "pushfl\n\t"
334     "mov %cx,%es\n\t"
335     "mov %dx,%ds\n\t"
336     ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
337     "popl %ds\n\t"
338     "mov %es,%dx\n\t"
339     "popl %es\n\t"
340     "popl %ebx\n\t"
341     "popl %ebp\n\t"
342     "movl 0x14(%ebp),%eax\n\t"
343     "movw %dx,(%eax)\n\t"
344     "movl 0x18(%ebp),%edx\n\t"
345     "movl %edi,(%edx)\n\t"
346     "popl %esi\n\t"
347     __ASM_CFI(".cfi_same_value %esi\n\t")
348     "popl %edi\n\t"
349     __ASM_CFI(".cfi_same_value %edi\n\t")
350     "leave\n\t"
351     __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
352     __ASM_CFI(".cfi_same_value %ebp\n\t")
353     "ret")
354 #endif
355
356 #endif /* __i386__ */
357
358 /**********************************************************************
359  *          DPMI_CallRMCBProc
360  *
361  * This routine does the hard work of calling a callback procedure.
362  */
363 static void DPMI_CallRMCBProc( CONTEXT86 *context, RMCB *rmcb, WORD flag )
364 {
365     DWORD old_vif = get_vm86_teb_info()->dpmi_vif;
366
367     /* Disable virtual interrupts. */
368     get_vm86_teb_info()->dpmi_vif = 0;
369
370     if (wine_ldt_is_system( rmcb->proc_sel )) {
371         /* Wine-internal RMCB, call directly */
372         ((RMCBPROC)rmcb->proc_ofs)(context);
373     } else __TRY {
374 #ifdef __i386__
375         UINT16 ss,es;
376         DWORD esp,edi;
377
378         INT_SetRealModeContext(MapSL(MAKESEGPTR( rmcb->regs_sel, rmcb->regs_ofs )), context);
379         ss = alloc_pm_selector( context->SegSs, WINE_LDT_FLAGS_DATA );
380         esp = context->Esp;
381
382         FIXME("untested!\n");
383
384         /* The called proc ends with an IRET, and takes these parameters:
385          * DS:ESI = pointer to real-mode SS:SP
386          * ES:EDI = pointer to real-mode call structure
387          * It returns:
388          * ES:EDI = pointer to real-mode call structure (may be a copy)
389          * It is the proc's responsibility to change the return CS:IP in the
390          * real-mode call structure. */
391         if (flag & 1) {
392             /* 32-bit DPMI client */
393             DPMI_CallRMCB32(rmcb, ss, esp, &es, &edi);
394         } else {
395             /* 16-bit DPMI client */
396             CONTEXT86 ctx = *context;
397             ctx.SegCs = rmcb->proc_sel;
398             ctx.Eip   = rmcb->proc_ofs;
399             ctx.SegDs = ss;
400             ctx.Esi   = esp;
401             ctx.SegEs = rmcb->regs_sel;
402             ctx.Edi   = rmcb->regs_ofs;
403             /* FIXME: I'm pretty sure this isn't right - should push flags first */
404             WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&ctx );
405             es = ctx.SegEs;
406             edi = ctx.Edi;
407         }
408         wine_ldt_free_entries( ss, 1 );
409         INT_GetRealModeContext( MapSL( MAKESEGPTR( es, edi )), context);
410 #else
411         ERR("RMCBs only implemented for i386\n");
412 #endif
413     } __EXCEPT(dpmi_exception_handler) { } __ENDTRY
414         
415     /* Restore virtual interrupt flag. */
416     get_vm86_teb_info()->dpmi_vif = old_vif;
417 }
418
419
420 /**********************************************************************
421  *          DPMI_CallRMProc
422  *
423  * This routine does the hard work of calling a real mode procedure.
424  */
425 int DPMI_CallRMProc( CONTEXT86 *context, LPWORD stack, int args, int iret )
426 {
427     LPWORD stack16;
428     LPVOID addr = NULL; /* avoid gcc warning */
429     RMCB *CurrRMCB;
430     int alloc = 0, already = 0;
431     BYTE *code;
432
433     TRACE("EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n",
434                  context->Eax, context->Ebx, context->Ecx, context->Edx );
435     TRACE("ESI=%08x EDI=%08x ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
436                  context->Esi, context->Edi, context->SegEs, context->SegDs,
437                  context->SegCs, LOWORD(context->Eip), args, iret?"IRET":"FAR" );
438
439 callrmproc_again:
440
441 /* there might be some code that just jumps to RMCBs or the like,
442    in which case following the jumps here might get us to a shortcut */
443     code = CTX_SEG_OFF_TO_LIN(context, context->SegCs, context->Eip);
444     switch (*code) {
445     case 0xe9: /* JMP NEAR */
446       context->Eip += 3 + *(WORD *)(code+1);
447       /* yeah, I know these gotos don't look good... */
448       goto callrmproc_again;
449     case 0xea: /* JMP FAR */
450       context->Eip = *(WORD *)(code+1);
451       context->SegCs = *(WORD *)(code+3);
452       /* ...but since the label is there anyway... */
453       goto callrmproc_again;
454     case 0xeb: /* JMP SHORT */
455       context->Eip += 2 + *(signed char *)(code+1);
456       /* ...because of other gotos below, so... */
457       goto callrmproc_again;
458     }
459
460 /* shortcut for chaining to internal interrupt handlers */
461     if ((context->SegCs == 0xF000) && iret)
462     {
463         DOSVM_CallBuiltinHandler( context, LOWORD(context->Eip)/4 );
464         return 0;
465     }
466
467 /* shortcut for RMCBs */
468     CurrRMCB = FirstRMCB;
469
470     while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
471         CurrRMCB = CurrRMCB->next;
472
473     if (!CurrRMCB && !MZ_Current())
474     {
475         FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
476         TRACE("creating VM86 task\n");
477         MZ_AllocDPMITask();
478     }
479     if (!already) {
480         if (!context->SegSs) {
481             alloc = 1; /* allocate default stack */
482             stack16 = addr = DOSMEM_AllocBlock( 64, (UINT16 *)&(context->SegSs) );
483             context->Esp = 64-2;
484             stack16 += 32-1;
485             if (!addr) {
486                 ERR("could not allocate default stack\n");
487                 return 1;
488             }
489         } else {
490             stack16 = CTX_SEG_OFF_TO_LIN(context, context->SegSs, context->Esp);
491         }
492         context->Esp -= (args + (iret?1:0)) * sizeof(WORD);
493         stack16 -= args;
494         if (args) memcpy(stack16, stack, args*sizeof(WORD) );
495         /* push flags if iret */
496         if (iret) {
497             stack16--; args++;
498             *stack16 = LOWORD(context->EFlags);
499         }
500         /* push return address (return to interrupt wrapper) */
501         *(--stack16) = DOSVM_dpmi_segments->wrap_seg;
502         *(--stack16) = 0;
503         /* adjust stack */
504         context->Esp -= 2*sizeof(WORD);
505         already = 1;
506     }
507
508     if (CurrRMCB) {
509         /* RMCB call, invoke protected-mode handler directly */
510         DPMI_CallRMCBProc(context, CurrRMCB, dpmi_flag);
511         /* check if we returned to where we thought we would */
512         if ((context->SegCs != DOSVM_dpmi_segments->wrap_seg) ||
513             (LOWORD(context->Eip) != 0)) {
514             /* we need to continue at different address in real-mode space,
515                so we need to set it all up for real mode again */
516             goto callrmproc_again;
517         }
518     } else {
519         TRACE("entering real mode...\n");
520         DOSVM_Enter( context );
521         TRACE("returned from real-mode call\n");
522     }
523     if (alloc) DOSMEM_FreeBlock( addr );
524     return 0;
525 }
526
527
528 /**********************************************************************
529  *          CallRMInt
530  */
531 static void DOSVM_CallRMInt( CONTEXT86 *context )
532 {
533     CONTEXT86 realmode_ctx;
534     FARPROC16 rm_int = DOSVM_GetRMHandler( BL_reg(context) );
535     REALMODECALL *call = CTX_SEG_OFF_TO_LIN( context, 
536                                              context->SegEs, 
537                                              context->Edi );
538     INT_GetRealModeContext( call, &realmode_ctx );
539
540     /* we need to check if a real-mode program has hooked the interrupt */
541     if (HIWORD(rm_int)!=0xF000) {
542         /* yup, which means we need to switch to real mode... */
543         realmode_ctx.SegCs = HIWORD(rm_int);
544         realmode_ctx.Eip   = LOWORD(rm_int);
545         if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
546           SET_CFLAG(context);
547     } else {
548         RESET_CFLAG(context);
549         /* use the IP we have instead of BL_reg, in case some apps
550            decide to move interrupts around for whatever reason... */
551         DOSVM_CallBuiltinHandler( &realmode_ctx, LOWORD(rm_int)/4 );
552     }
553     INT_SetRealModeContext( call, &realmode_ctx );
554 }
555
556
557 /**********************************************************************
558  *          CallRMProc
559  */
560 static void DOSVM_CallRMProc( CONTEXT86 *context, int iret )
561 {
562     REALMODECALL *p = CTX_SEG_OFF_TO_LIN( context, 
563                                           context->SegEs, 
564                                           context->Edi );
565     CONTEXT86 context16;
566
567     TRACE("RealModeCall: EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n",
568           p->eax, p->ebx, p->ecx, p->edx);
569     TRACE("              ESI=%08x EDI=%08x ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
570           p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
571
572     if (!(p->cs) && !(p->ip)) { /* remove this check
573                                    if Int21/6501 case map function
574                                    has been implemented */
575         SET_CFLAG(context);
576         return;
577      }
578     INT_GetRealModeContext(p, &context16);
579     DPMI_CallRMProc( &context16, ((LPWORD)MapSL(MAKESEGPTR(context->SegSs, LOWORD(context->Esp))))+3,
580                      CX_reg(context), iret );
581     INT_SetRealModeContext(p, &context16);
582 }
583
584
585 /* (see dosmem.c, function DOSMEM_InitDPMI) */
586 static void StartPM( CONTEXT86 *context )
587 {
588     UINT16 cs, ss, ds, es;
589     CONTEXT86 pm_ctx;
590     DWORD psp_ofs = (DWORD)(DOSVM_psp<<4);
591     PDB16 *psp = (PDB16 *)psp_ofs;
592     HANDLE16 env_seg = psp->environment;
593     unsigned char selflags = WINE_LDT_FLAGS_DATA;
594
595     RESET_CFLAG(context);
596     dpmi_flag = AX_reg(context);
597 /* our mode switch wrapper have placed the desired CS into DX */
598     cs = alloc_pm_selector( context->Edx, WINE_LDT_FLAGS_CODE );
599 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
600    can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
601    ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
602    32-bit code using this stack. */
603     if (dpmi_flag & 1) selflags |= WINE_LDT_FLAGS_32BIT;
604     ss = alloc_pm_selector( context->SegSs, selflags );
605 /* do the same for the data segments, just in case */
606     if (context->SegDs == context->SegSs) ds = ss;
607     else ds = alloc_pm_selector( context->SegDs, selflags );
608     es = alloc_pm_selector( DOSVM_psp, selflags );
609 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
610     psp->environment = alloc_pm_selector( env_seg, WINE_LDT_FLAGS_DATA );
611
612     pm_ctx = *context;
613     pm_ctx.SegCs = DOSVM_dpmi_segments->dpmi_sel;
614 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
615     pm_ctx.Eax   = ss;
616     pm_ctx.Edx   = cs;
617     pm_ctx.SegDs = ds;
618     pm_ctx.SegEs = es;
619     pm_ctx.SegFs = wine_get_fs();
620     pm_ctx.SegGs = wine_get_gs();
621     pm_ctx.EFlags &= ~V86_FLAG;
622
623     TRACE("DOS program is now entering %d-bit protected mode\n", 
624           DOSVM_IsDos32() ? 32 : 16);
625
626     __TRY 
627     {
628         WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&pm_ctx );
629     } 
630     __EXCEPT(dpmi_exception_handler) 
631     { 
632     } 
633     __ENDTRY
634
635     TRACE( "Protected mode DOS program is terminating\n" );
636
637     /*
638      * FIXME: Instead of calling DOSVM_Exit, we should release all
639      *        allocated protected mode resources and call MZ_Exit
640      *        using real mode context. See DPMI specification.
641      */
642     DOSVM_Exit( DPMI_retval );
643
644 #if 0
645     wine_ldt_free_entries( psp->environment, 1 );
646     psp->environment = env_seg;
647     wine_ldt_free_entries(es,1);
648     if (ds != ss) wine_ldt_free_entries(ds,1);
649     wine_ldt_free_entries(ss,1);
650     wine_ldt_free_entries(cs,1);
651 #endif
652 }
653
654 static RMCB *DPMI_AllocRMCB( void )
655 {
656     RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
657     UINT16 uParagraph;
658
659     if (NewRMCB)
660     {
661         LPVOID RMCBmem = DOSMEM_AllocBlock(4, &uParagraph);
662         LPBYTE p = RMCBmem;
663
664         *p++ = 0xcd; /* RMCB: */
665         *p++ = 0x31; /* int $0x31 */
666 /* it is the called procedure's task to change the return CS:EIP
667    the DPMI 0.9 spec states that if it doesn't, it will be called again */
668         *p++ = 0xeb;
669         *p++ = 0xfc; /* jmp RMCB */
670         NewRMCB->address = MAKELONG(0, uParagraph);
671         NewRMCB->next = FirstRMCB;
672         FirstRMCB = NewRMCB;
673     }
674     return NewRMCB;
675 }
676
677
678 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
679 {
680     RMCB *NewRMCB = DPMI_AllocRMCB();
681
682     if (NewRMCB) {
683         NewRMCB->proc_ofs = (DWORD)proc;
684         NewRMCB->proc_sel = 0;
685         NewRMCB->regs_ofs = 0;
686         NewRMCB->regs_sel = 0;
687         return (FARPROC16)(NewRMCB->address);
688     }
689     return NULL;
690 }
691
692
693 static int DPMI_FreeRMCB( DWORD address )
694 {
695     RMCB *CurrRMCB = FirstRMCB;
696     RMCB *PrevRMCB = NULL;
697
698     while (CurrRMCB && (CurrRMCB->address != address))
699     {
700         PrevRMCB = CurrRMCB;
701         CurrRMCB = CurrRMCB->next;
702     }
703     if (CurrRMCB)
704     {
705         if (PrevRMCB)
706         PrevRMCB->next = CurrRMCB->next;
707             else
708         FirstRMCB = CurrRMCB->next;
709         DOSMEM_FreeBlock(PTR_REAL_TO_LIN(SELECTOROF(CurrRMCB->address),OFFSETOF(CurrRMCB->address)));
710         HeapFree(GetProcessHeap(), 0, CurrRMCB);
711         return 0;
712     }
713     return 1;
714 }
715
716
717 /**********************************************************************
718  *          DOSVM_RawModeSwitchHandler
719  *
720  * DPMI Raw Mode Switch handler
721  */
722 void WINAPI DOSVM_RawModeSwitchHandler( CONTEXT86 *context )
723 {
724   CONTEXT86 rm_ctx;
725   int ret;
726
727   /* initialize real-mode context as per spec */
728   memset(&rm_ctx, 0, sizeof(rm_ctx));
729   rm_ctx.SegDs  = AX_reg(context);
730   rm_ctx.SegEs  = CX_reg(context);
731   rm_ctx.SegSs  = DX_reg(context);
732   rm_ctx.Esp    = context->Ebx;
733   rm_ctx.SegCs  = SI_reg(context);
734   rm_ctx.Eip    = context->Edi;
735   rm_ctx.Ebp    = context->Ebp;
736   rm_ctx.SegFs  = 0;
737   rm_ctx.SegGs  = 0;
738
739   /* Copy interrupt state. */
740   if (get_vm86_teb_info()->dpmi_vif)
741       rm_ctx.EFlags = V86_FLAG | VIF_MASK;
742   else
743       rm_ctx.EFlags = V86_FLAG;
744
745   /* enter real mode again */
746   TRACE("re-entering real mode at %04x:%04x\n",rm_ctx.SegCs,rm_ctx.Eip);
747   ret = DOSVM_Enter( &rm_ctx );
748   /* when the real-mode stuff call its mode switch address,
749      DOSVM_Enter will return and we will continue here */
750
751   if (ret<0) {
752     ERR("Sync lost!\n");
753     /* if the sync was lost, there's no way to recover */
754     ExitProcess(1);
755   }
756
757   /* alter protected-mode context as per spec */
758   context->SegDs   = LOWORD(rm_ctx.Eax);
759   context->SegEs   = LOWORD(rm_ctx.Ecx);
760   context->SegSs   = LOWORD(rm_ctx.Edx);
761   context->Esp     = rm_ctx.Ebx;
762   context->SegCs   = LOWORD(rm_ctx.Esi);
763   context->Eip     = rm_ctx.Edi;
764   context->Ebp     = rm_ctx.Ebp;
765   context->SegFs   = 0;
766   context->SegGs   = 0;
767
768   /* Copy interrupt state. */
769   if (rm_ctx.EFlags & VIF_MASK)
770       get_vm86_teb_info()->dpmi_vif = 1;
771   else
772       get_vm86_teb_info()->dpmi_vif = 0;
773
774   /* Return to new address and hope that we didn't mess up */
775   TRACE("re-entering protected mode at %04x:%08x\n",
776       context->SegCs, context->Eip);
777 }
778
779
780 /**********************************************************************
781  *          AllocRMCB
782  */
783 static void DOSVM_AllocRMCB( CONTEXT86 *context )
784 {
785     RMCB *NewRMCB = DPMI_AllocRMCB();
786
787     TRACE("Function to call: %04x:%04x\n", (WORD)context->SegDs, SI_reg(context) );
788
789     if (NewRMCB)
790     {
791        NewRMCB->proc_ofs = DOSVM_IsDos32() ? context->Esi : LOWORD(context->Esi);
792         NewRMCB->proc_sel = context->SegDs;
793        NewRMCB->regs_ofs = DOSVM_IsDos32() ? context->Edi : LOWORD(context->Edi);
794         NewRMCB->regs_sel = context->SegEs;
795         SET_CX( context, HIWORD(NewRMCB->address) );
796         SET_DX( context, LOWORD(NewRMCB->address) );
797     }
798     else
799     {
800         SET_AX( context, 0x8015 ); /* callback unavailable */
801         SET_CFLAG(context);
802     }
803 }
804
805
806 /**********************************************************************
807  *          FreeRMCB
808  */
809 static void DOSVM_FreeRMCB( CONTEXT86 *context )
810 {
811     FIXME("callback address: %04x:%04x\n",
812           CX_reg(context), DX_reg(context));
813
814     if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
815         SET_AX( context, 0x8024 ); /* invalid callback address */
816         SET_CFLAG(context);
817     }
818 }
819
820
821 /**********************************************************************
822  *         DOSVM_CheckWrappers
823  *
824  * Check if this was really a wrapper call instead of an interrupt.
825  */
826 BOOL DOSVM_CheckWrappers( CONTEXT86 *context )
827 {
828     if (context->SegCs==DOSVM_dpmi_segments->dpmi_seg) {
829         /* This is the protected mode switch */
830         StartPM(context);
831         return TRUE;
832     }
833     else if (context->SegCs==DOSVM_dpmi_segments->xms_seg)
834     {
835         /* This is the XMS driver entry point */
836         XMS_Handler(context);
837         return TRUE;
838     }
839     else
840     {
841         /* Check for RMCB */
842         RMCB *CurrRMCB = FirstRMCB;
843
844         while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
845             CurrRMCB = CurrRMCB->next;
846
847         if (CurrRMCB) {
848             /* RMCB call, propagate to protected-mode handler */
849             DPMI_CallRMCBProc(context, CurrRMCB, dpmi_flag);
850             return TRUE;
851         }
852     }
853
854     return FALSE;
855 }
856
857 /**********************************************************************
858  *         DOSVM_Int31Handler (WINEDOS16.149)
859  *
860  * Handler for int 31h (DPMI).
861  */
862 void WINAPI DOSVM_Int31Handler( CONTEXT86 *context )
863 {
864     RESET_CFLAG(context);
865     switch(AX_reg(context))
866     {
867     case 0x0000:  /* Allocate LDT descriptors */
868         TRACE( "allocate LDT descriptors (%d)\n", CX_reg(context) );
869         {
870             WORD sel =  AllocSelectorArray16( CX_reg(context) );
871             if(!sel) 
872             {
873                TRACE( "failed\n" );
874                SET_AX( context, 0x8011 ); /* descriptor unavailable */
875                SET_CFLAG( context );
876             } 
877             else 
878             { 
879                 TRACE( "success, array starts at 0x%04x\n", sel );
880                 SET_AX( context, sel );      
881             }
882         }
883         break;
884
885     case 0x0001:  /* Free LDT descriptor */
886         TRACE( "free LDT descriptor (0x%04x)\n", BX_reg(context) );
887         if (FreeSelector16( BX_reg(context) ))
888         {
889             SET_AX( context, 0x8022 );  /* invalid selector */
890             SET_CFLAG( context );
891         }
892         else
893         {
894             /* If a segment register contains the selector being freed, */
895             /* set it to zero. */
896             if (!((context->SegDs^BX_reg(context)) & ~3)) context->SegDs = 0;
897             if (!((context->SegEs^BX_reg(context)) & ~3)) context->SegEs = 0;
898             if (!((context->SegFs^BX_reg(context)) & ~3)) context->SegFs = 0;
899             if (!((context->SegGs^BX_reg(context)) & ~3)) context->SegGs = 0;
900         }
901         break;
902
903     case 0x0002:  /* Real mode segment to descriptor */
904         TRACE( "real mode segment to descriptor (0x%04x)\n", BX_reg(context) );
905         {
906             WORD entryPoint = 0;  /* KERNEL entry point for descriptor */
907             switch(BX_reg(context))
908             {
909             case 0x0000: entryPoint = 183; break;  /* __0000H */
910             case 0x0040: entryPoint = 193; break;  /* __0040H */
911             case 0xa000: entryPoint = 174; break;  /* __A000H */
912             case 0xb000: entryPoint = 181; break;  /* __B000H */
913             case 0xb800: entryPoint = 182; break;  /* __B800H */
914             case 0xc000: entryPoint = 195; break;  /* __C000H */
915             case 0xd000: entryPoint = 179; break;  /* __D000H */
916             case 0xe000: entryPoint = 190; break;  /* __E000H */
917             case 0xf000: entryPoint = 194; break;  /* __F000H */
918             default:
919                 FIXME("Real mode segment (%x) to descriptor: no longer supported\n",
920                       BX_reg(context));
921                 SET_CFLAG( context );
922                 break;
923             }
924             if (entryPoint)
925             {
926                 FARPROC16 proc = GetProcAddress16( GetModuleHandle16( "KERNEL" ),
927                                                    (LPCSTR)(ULONG_PTR)entryPoint );
928                 SET_AX( context, LOWORD(proc) );
929             }
930         }
931         break;
932
933     case 0x0003:  /* Get next selector increment */
934         TRACE("get selector increment (__AHINCR)\n");
935         context->Eax = __AHINCR;
936         break;
937
938     case 0x0004:  /* Lock selector (not supported) */
939         FIXME("lock selector not supported\n");
940         context->Eax = 0;  /* FIXME: is this a correct return value? */
941         break;
942
943     case 0x0005:  /* Unlock selector (not supported) */
944         FIXME("unlock selector not supported\n");
945         context->Eax = 0;  /* FIXME: is this a correct return value? */
946         break;
947
948     case 0x0006:  /* Get selector base address */
949         TRACE( "get selector base address (0x%04x)\n", BX_reg(context) );
950         {
951             LDT_ENTRY entry;
952             WORD sel = BX_reg(context);
953             wine_ldt_get_entry( sel, &entry );
954             if (wine_ldt_is_empty(&entry))
955             {
956                 context->Eax = 0x8022;  /* invalid selector */
957                 SET_CFLAG(context);
958             }
959             else
960             {
961                 void *base = wine_ldt_get_base(&entry);
962                 SET_CX( context, HIWORD(base) );
963                 SET_DX( context, LOWORD(base) );
964             }
965         }
966         break;
967
968     case 0x0007:  /* Set selector base address */
969         {
970             DWORD base = MAKELONG( DX_reg(context), CX_reg(context) );
971             WORD  sel = BX_reg(context);
972             TRACE( "set selector base address (0x%04x,0x%08x)\n", sel, base );
973
974             /* check if Win16 app wants to access lower 64K of DOS memory */
975             if (base < 0x10000 && DOSVM_IsWin16())
976                 DOSMEM_MapDosLayout();
977
978             SetSelectorBase( sel, base );
979         }
980         break;
981
982     case 0x0008:  /* Set selector limit */
983         {
984             DWORD limit = MAKELONG( DX_reg(context), CX_reg(context) );
985             TRACE( "set selector limit (0x%04x,0x%08x)\n",
986                    BX_reg(context), limit );
987             SetSelectorLimit16( BX_reg(context), limit );
988         }
989         break;
990
991     case 0x0009:  /* Set selector access rights */
992         TRACE( "set selector access rights(0x%04x,0x%04x)\n",
993                BX_reg(context), CX_reg(context) );
994         SelectorAccessRights16( BX_reg(context), 1, CX_reg(context) );
995         break;
996
997     case 0x000a:  /* Allocate selector alias */
998         TRACE( "allocate selector alias (0x%04x)\n", BX_reg(context) );
999         SET_AX( context, AllocCStoDSAlias16( BX_reg(context) ) );
1000         if (!AX_reg(context))
1001         {
1002             SET_AX( context, 0x8011 );  /* descriptor unavailable */
1003             SET_CFLAG(context);
1004         }
1005         break;
1006
1007     case 0x000b:  /* Get descriptor */
1008         TRACE( "get descriptor (0x%04x)\n", BX_reg(context) );
1009         {
1010             LDT_ENTRY *entry = CTX_SEG_OFF_TO_LIN( context, context->SegEs,
1011                                                    context->Edi );
1012             wine_ldt_get_entry( BX_reg(context), entry );
1013         }
1014         break;
1015
1016     case 0x000c:  /* Set descriptor */
1017         TRACE( "set descriptor (0x%04x)\n", BX_reg(context) );
1018         {
1019             LDT_ENTRY *entry = CTX_SEG_OFF_TO_LIN( context, context->SegEs,
1020                                                    context->Edi );
1021             wine_ldt_set_entry( BX_reg(context), entry );
1022         }
1023         break;
1024
1025     case 0x000d:  /* Allocate specific LDT descriptor */
1026         FIXME( "allocate descriptor (0x%04x), stub!\n", BX_reg(context) );
1027         SET_AX( context, 0x8011 ); /* descriptor unavailable */
1028         SET_CFLAG( context );
1029         break;
1030
1031     case 0x000e:  /* Get Multiple Descriptors (1.0) */
1032         FIXME( "get multiple descriptors - unimplemented\n" );
1033         break;
1034
1035     case 0x000f:  /* Set Multiple Descriptors (1.0) */
1036         FIXME( "set multiple descriptors - unimplemented\n" );
1037         break;
1038
1039     case 0x0100:  /* Allocate DOS memory block */
1040         TRACE( "allocate DOS memory block (0x%x paragraphs)\n", BX_reg(context) );
1041         {
1042             DWORD dw = GlobalDOSAlloc16( (DWORD)BX_reg(context) << 4 );
1043             if (dw) {
1044                 SET_AX( context, HIWORD(dw) );
1045                 SET_DX( context, LOWORD(dw) );
1046             } else {
1047                 SET_AX( context, 0x0008 ); /* insufficient memory */
1048                 SET_BX( context, DOSMEM_Available() >> 4 );
1049                 SET_CFLAG(context);
1050             }
1051             break;
1052         }
1053
1054     case 0x0101:  /* Free DOS memory block */
1055         TRACE( "free DOS memory block (0x%04x)\n", DX_reg(context) );
1056         {
1057             WORD error = GlobalDOSFree16( DX_reg(context) );
1058             if (error) {
1059                 SET_AX( context, 0x0009 ); /* memory block address invalid */
1060                 SET_CFLAG( context );
1061             }
1062         }
1063         break;
1064
1065     case 0x0102: /* Resize DOS Memory Block */
1066         FIXME( "resize DOS memory block (0x%04x, 0x%x paragraphs) - unimplemented\n", 
1067                DX_reg(context), BX_reg(context) );
1068         break;
1069
1070     case 0x0200: /* get real mode interrupt vector */
1071         TRACE( "get realmode interrupt vector (0x%02x)\n",
1072                BL_reg(context) );
1073         {
1074             FARPROC16 proc = DOSVM_GetRMHandler( BL_reg(context) );
1075             SET_CX( context, SELECTOROF(proc) );
1076             SET_DX( context, OFFSETOF(proc) );
1077         }
1078         break;
1079
1080     case 0x0201: /* set real mode interrupt vector */
1081         TRACE( "set realmode interrupt vector (0x%02x, 0x%04x:0x%04x)\n", 
1082                BL_reg(context), CX_reg(context), DX_reg(context) );
1083         DOSVM_SetRMHandler( BL_reg(context), 
1084                             (FARPROC16)MAKESEGPTR(CX_reg(context), DX_reg(context)) );
1085         break;
1086
1087     case 0x0202:  /* Get Processor Exception Handler Vector */
1088         FIXME( "Get Processor Exception Handler Vector (0x%02x)\n",
1089                BL_reg(context) );
1090         if (DOSVM_IsDos32()) 
1091         {
1092             SET_CX( context, 0 );
1093             context->Edx = 0;
1094         } 
1095         else 
1096         {
1097             SET_CX( context, 0 );
1098             SET_DX( context, 0 );
1099         }
1100         break;
1101
1102     case 0x0203:  /* Set Processor Exception Handler Vector */
1103          FIXME( "Set Processor Exception Handler Vector (0x%02x)\n",
1104                 BL_reg(context) );
1105          break;
1106
1107     case 0x0204:  /* Get protected mode interrupt vector */
1108         TRACE("get protected mode interrupt handler (0x%02x)\n",
1109               BL_reg(context));
1110         if (DOSVM_IsDos32()) 
1111         {
1112             FARPROC48 handler = DOSVM_GetPMHandler48( BL_reg(context) );
1113             SET_CX( context, handler.selector );
1114             context->Edx = handler.offset;
1115         } 
1116         else 
1117         {
1118             FARPROC16 handler = DOSVM_GetPMHandler16( BL_reg(context) );
1119             SET_CX( context, SELECTOROF(handler) );
1120             SET_DX( context, OFFSETOF(handler) );
1121         }
1122         break;
1123
1124     case 0x0205:  /* Set protected mode interrupt vector */
1125         TRACE("set protected mode interrupt handler (0x%02x,0x%04x:0x%08x)\n",
1126               BL_reg(context), CX_reg(context), context->Edx);
1127         if (DOSVM_IsDos32()) 
1128         {
1129             FARPROC48 handler;
1130             handler.selector = CX_reg(context);
1131             handler.offset = context->Edx;
1132             DOSVM_SetPMHandler48( BL_reg(context), handler );
1133         } 
1134         else 
1135         {
1136             FARPROC16 handler;
1137             handler = (FARPROC16)MAKESEGPTR( CX_reg(context), DX_reg(context)); 
1138             DOSVM_SetPMHandler16( BL_reg(context), handler );
1139         }
1140         break;
1141
1142     case 0x0300:  /* Simulate real mode interrupt */
1143         TRACE( "Simulate real mode interrupt %02x.\n", BL_reg(context));
1144         DOSVM_CallRMInt( context );
1145         break;
1146
1147     case 0x0301:  /* Call real mode procedure with far return */
1148         TRACE( "Call real mode procedure with far return.\n" );
1149         DOSVM_CallRMProc( context, FALSE );
1150         break;
1151
1152     case 0x0302:  /* Call real mode procedure with interrupt return */
1153         TRACE( "Call real mode procedure with interrupt return.\n" );
1154         DOSVM_CallRMProc( context, TRUE );
1155         break;
1156
1157     case 0x0303:  /* Allocate Real Mode Callback Address */
1158         TRACE( "Allocate real mode callback address.\n" );
1159         DOSVM_AllocRMCB( context );
1160         break;
1161
1162     case 0x0304:  /* Free Real Mode Callback Address */
1163         TRACE( "Free real mode callback address.\n" );
1164         DOSVM_FreeRMCB( context );
1165         break;
1166
1167     case 0x0305:  /* Get State Save/Restore Addresses */
1168         TRACE("get state save/restore addresses\n");
1169         /* we probably won't need this kind of state saving */
1170         SET_AX( context, 0 );
1171
1172         /* real mode: just point to the lret */
1173         SET_BX( context, DOSVM_dpmi_segments->wrap_seg );
1174         SET_CX( context, 2 );
1175
1176         /* protected mode: don't have any handler yet... */
1177         /* FIXME: Use DI in 16-bit DPMI and EDI in 32-bit DPMI */
1178         FIXME("no protected-mode dummy state save/restore handler yet\n");
1179         SET_SI( context, 0 );
1180         context->Edi = 0;
1181         break;
1182
1183     case 0x0306:  /* Get Raw Mode Switch Addresses */
1184         TRACE("get raw mode switch addresses\n");
1185
1186         /* real mode, point to standard DPMI return wrapper */
1187         SET_BX( context, DOSVM_dpmi_segments->wrap_seg );
1188         SET_CX( context, 0 );
1189
1190         /* protected mode, point to DPMI call wrapper */
1191         /* FIXME: Use DI in 16-bit DPMI and EDI in 32-bit DPMI */
1192         /* FIXME: Doesn't work in DPMI32... */
1193         SET_SI( context, DOSVM_dpmi_segments->dpmi_sel );
1194         context->Edi = 8; /* offset of the INT 0x31 call */
1195         break;
1196
1197     case 0x0400:  /* Get DPMI version */
1198         TRACE("get DPMI version\n");
1199         {
1200             SYSTEM_INFO si;
1201
1202             GetSystemInfo(&si);
1203             SET_AX( context, 0x005a );  /* DPMI version 0.90 */
1204             SET_BX( context, 0x0005 );  /* Flags: 32-bit, virtual memory */
1205             SET_CL( context, si.wProcessorLevel );
1206             SET_DX( context, 0x0870 );  /* Master/slave interrupt controller base */
1207         }
1208         break;
1209
1210     case 0x0401:  /* Get DPMI Capabilities (1.0) */
1211         FIXME( "get dpmi capabilities - unimplemented\n");
1212         break;
1213
1214     case 0x0500:  /* Get free memory information */
1215         TRACE("get free memory information\n");
1216         {
1217             MEMORYSTATUS status;
1218
1219             /* the layout is just the same as MEMMANINFO, but without
1220              * the dwSize entry.
1221              */
1222             struct
1223             {
1224                 DWORD dwLargestFreeBlock;
1225                 DWORD dwMaxPagesAvailable;
1226                 DWORD dwMaxPagesLockable;
1227                 DWORD dwTotalLinearSpace;
1228                 DWORD dwTotalUnlockedPages;
1229                 DWORD dwFreePages;
1230                 DWORD dwTotalPages;
1231                 DWORD dwFreeLinearSpace;
1232                 DWORD dwSwapFilePages;
1233                 WORD  wPageSize;
1234             } *info = CTX_SEG_OFF_TO_LIN( context, context->SegEs, context->Edi );
1235
1236             GlobalMemoryStatus( &status );
1237             info->wPageSize            = getpagesize();
1238             info->dwLargestFreeBlock   = status.dwAvailVirtual;
1239             info->dwMaxPagesAvailable  = info->dwLargestFreeBlock / info->wPageSize;
1240             info->dwMaxPagesLockable   = info->dwMaxPagesAvailable;
1241             info->dwTotalLinearSpace   = status.dwTotalVirtual / info->wPageSize;
1242             info->dwTotalUnlockedPages = info->dwTotalLinearSpace;
1243             info->dwFreePages          = info->dwMaxPagesAvailable;
1244             info->dwTotalPages         = info->dwTotalLinearSpace;
1245             info->dwFreeLinearSpace    = info->dwMaxPagesAvailable;
1246             info->dwSwapFilePages      = status.dwTotalPageFile / info->wPageSize;
1247             break;
1248         }
1249
1250     case 0x0501:  /* Allocate memory block */
1251         {
1252             DWORD size = MAKELONG( CX_reg(context), BX_reg(context) );
1253             BYTE *ptr;
1254
1255             TRACE( "allocate memory block (%d bytes)\n", size );
1256
1257             ptr = DPMI_xalloc( size );
1258             if (!ptr)
1259             {
1260                 SET_AX( context, 0x8012 );  /* linear memory not available */
1261                 SET_CFLAG(context);
1262             } 
1263             else 
1264             {
1265                 SET_BX( context, HIWORD(ptr) );
1266                 SET_CX( context, LOWORD(ptr) );
1267                 SET_SI( context, HIWORD(ptr) );
1268                 SET_DI( context, LOWORD(ptr) );
1269             }
1270             break;
1271         }
1272
1273     case 0x0502:  /* Free memory block */
1274         {
1275             DWORD handle = MAKELONG( DI_reg(context), SI_reg(context) );
1276             TRACE( "free memory block (0x%08x)\n", handle );
1277             DPMI_xfree( (void *)handle );
1278         }
1279         break;
1280
1281     case 0x0503:  /* Resize memory block */
1282         {
1283             DWORD size = MAKELONG( CX_reg(context), BX_reg(context) );
1284             DWORD handle = MAKELONG( DI_reg(context), SI_reg(context) );
1285             BYTE *ptr;
1286
1287             TRACE( "resize memory block (0x%08x, %d bytes)\n", handle, size );
1288
1289             ptr = DPMI_xrealloc( (void *)handle, size );
1290             if (!ptr)
1291             {
1292                 SET_AX( context, 0x8012 );  /* linear memory not available */
1293                 SET_CFLAG(context);
1294             } else {
1295                 SET_BX( context, HIWORD(ptr) );
1296                 SET_CX( context, LOWORD(ptr) );
1297                 SET_SI( context, HIWORD(ptr) );
1298                 SET_DI( context, LOWORD(ptr) );
1299             }
1300         }
1301         break;
1302
1303     case 0x0507:  /* Set page attributes (1.0) */
1304         FIXME( "set page attributes - unimplemented\n" );
1305         break;  /* Just ignore it */
1306
1307     case 0x0600:  /* Lock linear region */
1308         TRACE( "lock linear region - ignored (no paging)\n" );
1309         break;
1310
1311     case 0x0601:  /* Unlock linear region */
1312         TRACE( "unlock linear region - ignored (no paging)\n" );
1313         break;
1314
1315     case 0x0602:  /* Mark real mode region as pageable */
1316         TRACE( "mark real mode region as pageable - ignored (no paging)\n" );
1317         break;
1318
1319     case 0x0603:  /* Relock real mode region */
1320         TRACE( "relock real mode region - ignored (no paging)\n" );
1321         break;
1322
1323     case 0x0604:  /* Get page size */
1324         TRACE("get pagesize\n");
1325         SET_BX( context, HIWORD(getpagesize()) );
1326         SET_CX( context, LOWORD(getpagesize()) );
1327         break;
1328
1329     case 0x0700: /* Mark pages as paging candidates */
1330         TRACE( "mark pages as paging candidates - ignored (no paging)\n" );
1331         break;
1332
1333     case 0x0701: /* Discard pages */
1334         TRACE( "discard pages - ignored (no paging)\n" );
1335         break;
1336
1337     case 0x0702:  /* Mark page as demand-paging candidate */
1338         TRACE( "mark page as demand-paging candidate - ignored (no paging)\n" );
1339         break;
1340
1341     case 0x0703:  /* Discard page contents */
1342         TRACE( "discard page contents - ignored (no paging)\n" );
1343         break;
1344
1345     case 0x0800:  /* Physical address mapping */
1346         FIXME( "physical address mapping (0x%08x) - unimplemented\n",
1347                MAKELONG(CX_reg(context),BX_reg(context)) );
1348         break;
1349
1350     case 0x0900:  /* Get and Disable Virtual Interrupt State */
1351         TRACE( "Get and Disable Virtual Interrupt State: %d\n",
1352                get_vm86_teb_info()->dpmi_vif );
1353         SET_AL( context, get_vm86_teb_info()->dpmi_vif ? 1 : 0 );
1354         get_vm86_teb_info()->dpmi_vif = 0;
1355         break;
1356
1357     case 0x0901:  /* Get and Enable Virtual Interrupt State */
1358         TRACE( "Get and Enable Virtual Interrupt State: %d\n",
1359                get_vm86_teb_info()->dpmi_vif );
1360         SET_AL( context, get_vm86_teb_info()->dpmi_vif ? 1 : 0 );
1361         get_vm86_teb_info()->dpmi_vif = 1;
1362         break;
1363
1364     case 0x0902:  /* Get Virtual Interrupt State */
1365         TRACE( "Get Virtual Interrupt State: %d\n",
1366                get_vm86_teb_info()->dpmi_vif );
1367         SET_AL( context, get_vm86_teb_info()->dpmi_vif ? 1 : 0 );
1368         break;
1369
1370     case 0x0e00:  /* Get Coprocessor Status (1.0) */
1371         /*
1372          * Return status in AX bits:
1373          * B0    - MPv (MP bit in the virtual MSW/CR0)
1374          *         0 = numeric coprocessor is disabled for this client
1375          *         1 = numeric coprocessor is enabled for this client
1376          * B1    - EMv (EM bit in the virtual MSW/CR0)
1377          *         0 = client is not emulating coprocessor instructions
1378          *         1 = client is emulating coprocessor instructions
1379          * B2    - MPr (MP bit from the actual MSW/CR0)
1380          *         0 = numeric coprocessor is not present
1381          *         1 = numeric coprocessor is present
1382          * B3    - EMr (EM bit from the actual MSW/CR0)
1383          *         0 = host is not emulating coprocessor instructions
1384          *         1 = host is emulating coprocessor instructions
1385          * B4-B7 - coprocessor type
1386          *         00H = no coprocessor
1387          *         02H = 80287
1388          *         03H = 80387
1389          *         04H = 80486 with numeric coprocessor
1390          *         05H-0FH = reserved for future numeric processors
1391          */
1392         TRACE( "Get Coprocessor Status\n" );
1393         SET_AX( context, 69 ); /* 486, coprocessor present and enabled */ 
1394         break;
1395
1396     case 0x0e01: /* Set Coprocessor Emulation (1.0) */
1397         /*
1398          * See function 0x0e00.
1399          * BX bit B0 is new value for MPv.
1400          * BX bit B1 is new value for EMv.
1401          */
1402         if (BX_reg(context) != 1)
1403             FIXME( "Set Coprocessor Emulation to %d - unimplemented\n", 
1404                    BX_reg(context) );
1405         else
1406             TRACE( "Set Coprocessor Emulation - ignored\n" );
1407         break;
1408
1409     default:
1410         INT_BARF( context, 0x31 );
1411         SET_AX( context, 0x8001 );  /* unsupported function */
1412         SET_CFLAG(context);
1413         break;
1414     }  
1415 }