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