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