Fixed a race condition on RPC worker thread creation, and a typo.
[wine] / dlls / winedos / xms.c
1 /*
2  * XMS v2+ emulation
3  *
4  * Copyright 1998 Ove Kåven
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  * Note: This XMS emulation is hooked through the DPMI interrupt.
21  */
22
23 #include "config.h"
24
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28 #include <string.h>
29 #include "winbase.h"
30 #include "wine/winbase16.h"
31 #include "miscemu.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(int31);
35
36 typedef struct {
37  WORD Handle;
38  DWORD Offset;
39 } WINE_PACKED MOVEOFS;
40
41 typedef struct {
42  DWORD Length;
43  MOVEOFS Source;
44  MOVEOFS Dest;
45 } WINE_PACKED MOVESTRUCT;
46
47 static BYTE * XMS_Offset( MOVEOFS *ofs )
48 {
49     if (ofs->Handle) return (BYTE*)GlobalLock16(ofs->Handle)+ofs->Offset;
50     else return (BYTE*)PTR_REAL_TO_LIN(SELECTOROF(ofs->Offset),OFFSETOF(ofs->Offset));
51 }
52
53 /**********************************************************************
54  *          XMS_Handler
55  */
56
57 void WINAPI XMS_Handler( CONTEXT86 *context )
58 {
59     switch(AH_reg(context))
60     {
61     case 0x00:   /* Get XMS version number */
62         TRACE("get XMS version number\n");
63         SET_AX( context, 0x0200 ); /* 2.0 */
64         SET_BX( context, 0x0000 ); /* internal revision */
65         SET_DX( context, 0x0001 ); /* HMA exists */
66         break;
67     case 0x08:   /* Query Free Extended Memory */
68     {
69         MEMORYSTATUS status;
70
71         TRACE("query free extended memory\n");
72         GlobalMemoryStatus( &status );
73         SET_DX( context, status.dwAvailVirtual >> 10 );
74         SET_AX( context, status.dwAvailVirtual >> 10 );
75         TRACE("returning largest %dK, total %dK\n", AX_reg(context), DX_reg(context));
76     }
77     break;
78     case 0x09:   /* Allocate Extended Memory Block */
79         TRACE("allocate extended memory block (%dK)\n",
80             DX_reg(context));
81         SET_DX( context, GlobalAlloc16(GMEM_MOVEABLE, (DWORD)DX_reg(context)<<10) );
82         SET_AX( context, DX_reg(context) ? 1 : 0 );
83         if (!DX_reg(context)) SET_BL( context, 0xA0 ); /* out of memory */
84         break;
85     case 0x0a:   /* Free Extended Memory Block */
86         TRACE("free extended memory block %04x\n",DX_reg(context));
87        if(!DX_reg(context) || GlobalFree16(DX_reg(context))) {
88          SET_AX( context, 0 );    /* failure */
89          SET_BL( context, 0xa2 ); /* invalid handle */
90        } else
91          SET_AX( context, 1 );    /* success */
92         break;
93     case 0x0b:   /* Move Extended Memory Block */
94     {
95         MOVESTRUCT*move=CTX_SEG_OFF_TO_LIN(context,
96             context->SegDs,context->Esi);
97         BYTE*src,*dst;
98         TRACE("move extended memory block\n");
99         src=XMS_Offset(&move->Source);
100         dst=XMS_Offset(&move->Dest);
101         memcpy(dst,src,move->Length);
102         if (move->Source.Handle) GlobalUnlock16(move->Source.Handle);
103         if (move->Dest.Handle) GlobalUnlock16(move->Dest.Handle);
104         break;
105     }
106     case 0x88:   /* Query Any Free Extended Memory */
107     {
108         MEMORYSTATUS status;
109         SYSTEM_INFO  info;
110
111         TRACE("query any free extended memory\n");
112
113         GlobalMemoryStatus( &status );
114         GetSystemInfo( &info );
115         context->Eax = status.dwAvailVirtual >> 10;
116         context->Edx = status.dwAvailVirtual >> 10;
117         context->Ecx = (DWORD)info.lpMaximumApplicationAddress;
118         SET_BL( context, 0 ); /* No errors. */
119
120         TRACE("returning largest %ldK, total %ldK, highest 0x%lx\n", 
121               context->Eax, context->Edx, context->Ecx);
122     }
123     break;
124     default:
125         INT_BARF( context, 0x31 );
126         SET_AX( context, 0x0000 ); /* failure */
127         SET_BL( context, 0x80 );   /* function not implemented */
128         break;
129     }
130 }