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