xms.c 4.16 KB
Newer Older
1 2 3
/*
 * XMS v2+ emulation
 *
4
 * Copyright 1998 Ove Kåven
5
 *
6 7 8 9 10 11 12 13 14 15 16 17
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
 *
 * Note: This XMS emulation is hooked through the DPMI interrupt.
21 22
 */

23 24 25 26 27
#include "config.h"

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
28
#include <stdarg.h>
29
#include <string.h>
30
#include "windef.h"
31
#include "winbase.h"
32
#include "wine/winbase16.h"
33
#include "dosexe.h"
34
#include "wine/debug.h"
35

36
WINE_DEFAULT_DEBUG_CHANNEL(int31);
37

38 39
#include "pshpack1.h"

40 41 42
typedef struct {
 WORD Handle;
 DWORD Offset;
43
} MOVEOFS;
44 45 46 47 48

typedef struct {
 DWORD Length;
 MOVEOFS Source;
 MOVEOFS Dest;
49 50 51
} MOVESTRUCT;

#include "poppack.h"
52 53 54 55

static BYTE * XMS_Offset( MOVEOFS *ofs )
{
    if (ofs->Handle) return (BYTE*)GlobalLock16(ofs->Handle)+ofs->Offset;
56
    else return (BYTE*)PTR_REAL_TO_LIN(SELECTOROF(ofs->Offset),OFFSETOF(ofs->Offset));
57 58 59 60 61 62
}

/**********************************************************************
 *	    XMS_Handler
 */

63
void WINAPI XMS_Handler( CONTEXT86 *context )
64 65 66 67
{
    switch(AH_reg(context))
    {
    case 0x00:   /* Get XMS version number */
68
        TRACE("get XMS version number\n");
69 70 71
        SET_AX( context, 0x0200 ); /* 2.0 */
        SET_BX( context, 0x0000 ); /* internal revision */
        SET_DX( context, 0x0001 ); /* HMA exists */
72 73 74
        break;
    case 0x08:   /* Query Free Extended Memory */
    {
75
        MEMORYSTATUS status;
76

77
        TRACE("query free extended memory\n");
78
        GlobalMemoryStatus( &status );
79 80
        SET_DX( context, status.dwAvailVirtual >> 10 );
        SET_AX( context, status.dwAvailVirtual >> 10 );
81
        TRACE("returning largest %dK, total %dK\n", AX_reg(context), DX_reg(context));
82
    }
Andreas Mohr's avatar
Andreas Mohr committed
83
    break;
84
    case 0x09:   /* Allocate Extended Memory Block */
85
        TRACE("allocate extended memory block (%dK)\n",
86
            DX_reg(context));
87 88 89
	SET_DX( context, GlobalAlloc16(GMEM_MOVEABLE, (DWORD)DX_reg(context)<<10) );
	SET_AX( context, DX_reg(context) ? 1 : 0 );
	if (!DX_reg(context)) SET_BL( context, 0xA0 ); /* out of memory */
90 91
	break;
    case 0x0a:   /* Free Extended Memory Block */
92
	TRACE("free extended memory block %04x\n",DX_reg(context));
93
       if(!DX_reg(context) || GlobalFree16(DX_reg(context))) {
94 95
         SET_AX( context, 0 );    /* failure */
         SET_BL( context, 0xa2 ); /* invalid handle */
96
       } else
97
         SET_AX( context, 1 );    /* success */
98 99 100 101
	break;
    case 0x0b:   /* Move Extended Memory Block */
    {
	MOVESTRUCT*move=CTX_SEG_OFF_TO_LIN(context,
102
	    context->SegDs,context->Esi);
103
        BYTE*src,*dst;
104
        TRACE("move extended memory block\n");
105 106 107 108 109 110 111
        src=XMS_Offset(&move->Source);
        dst=XMS_Offset(&move->Dest);
	memcpy(dst,src,move->Length);
	if (move->Source.Handle) GlobalUnlock16(move->Source.Handle);
	if (move->Dest.Handle) GlobalUnlock16(move->Dest.Handle);
	break;
    }
112 113 114 115 116 117 118 119 120 121 122 123 124 125
    case 0x88:   /* Query Any Free Extended Memory */
    {
        MEMORYSTATUS status;
        SYSTEM_INFO  info;

        TRACE("query any free extended memory\n");

        GlobalMemoryStatus( &status );
        GetSystemInfo( &info );
        context->Eax = status.dwAvailVirtual >> 10;
        context->Edx = status.dwAvailVirtual >> 10;
        context->Ecx = (DWORD)info.lpMaximumApplicationAddress;
        SET_BL( context, 0 ); /* No errors. */

126
        TRACE("returning largest %dK, total %dK, highest 0x%x\n",
127 128 129
              context->Eax, context->Edx, context->Ecx);
    }
    break;
130 131
    default:
        INT_BARF( context, 0x31 );
132 133
        SET_AX( context, 0x0000 ); /* failure */
        SET_BL( context, 0x80 );   /* function not implemented */
134 135 136
        break;
    }
}