Use MapLS/UnMapLS instead of SEGPTR_* macros.
[wine] / dlls / winedos / xms.c
1 /*
2  * XMS v2+ emulation
3  *
4  * Copyright 1998 Ove Kåven
5  *
6  * This XMS emulation is hooked through the DPMI interrupt.
7  */
8
9 #include <unistd.h>
10 #include <string.h>
11 #include "winbase.h"
12 #include "wine/winbase16.h"
13 #include "module.h"
14 #include "miscemu.h"
15 #include "toolhelp.h"
16 #include "debugtools.h"
17
18 DEFAULT_DEBUG_CHANNEL(int31);
19
20 typedef struct {
21  WORD Handle;
22  DWORD Offset;
23 } WINE_PACKED MOVEOFS;
24
25 typedef struct {
26  DWORD Length;
27  MOVEOFS Source;
28  MOVEOFS Dest;
29 } WINE_PACKED MOVESTRUCT;
30
31 static BYTE * XMS_Offset( MOVEOFS *ofs )
32 {
33     if (ofs->Handle) return (BYTE*)GlobalLock16(ofs->Handle)+ofs->Offset;
34         else return (BYTE*)DOSMEM_MapRealToLinear(ofs->Offset);
35 }
36
37 /**********************************************************************
38  *          XMS_Handler
39  */
40
41 void WINAPI XMS_Handler( CONTEXT86 *context )
42 {
43     switch(AH_reg(context))
44     {
45     case 0x00:   /* Get XMS version number */
46         TRACE("get XMS version number\n");
47         AX_reg(context) = 0x0200; /* 2.0 */
48         BX_reg(context) = 0x0000; /* internal revision */
49         DX_reg(context) = 0x0001; /* HMA exists */
50         break;
51     case 0x08:   /* Query Free Extended Memory */
52     {
53         MEMMANINFO mmi;
54
55         TRACE("query free extended memory\n");
56         mmi.dwSize = sizeof(mmi);
57         MemManInfo16(&mmi);
58         AX_reg(context) = mmi.dwLargestFreeBlock >> 10;
59         DX_reg(context) = (mmi.dwFreePages * mmi.wPageSize) >> 10;
60         TRACE("returning largest %dK, total %dK\n", AX_reg(context), DX_reg(context));
61     }
62     break;
63     case 0x09:   /* Allocate Extended Memory Block */
64         TRACE("allocate extended memory block (%dK)\n",
65             DX_reg(context));
66         DX_reg(context) = GlobalAlloc16(GMEM_MOVEABLE,
67             (DWORD)DX_reg(context)<<10);
68         AX_reg(context) = DX_reg(context) ? 1 : 0;
69         if (!DX_reg(context)) BL_reg(context) = 0xA0; /* out of memory */
70         break;
71     case 0x0a:   /* Free Extended Memory Block */
72         TRACE("free extended memory block %04x\n",DX_reg(context));
73         GlobalFree16(DX_reg(context));
74         break;
75     case 0x0b:   /* Move Extended Memory Block */
76     {
77         MOVESTRUCT*move=CTX_SEG_OFF_TO_LIN(context,
78             context->SegDs,context->Esi);
79         BYTE*src,*dst;
80         TRACE("move extended memory block\n");
81         src=XMS_Offset(&move->Source);
82         dst=XMS_Offset(&move->Dest);
83         memcpy(dst,src,move->Length);
84         if (move->Source.Handle) GlobalUnlock16(move->Source.Handle);
85         if (move->Dest.Handle) GlobalUnlock16(move->Dest.Handle);
86         break;
87     }
88     default:
89         INT_BARF( context, 0x31 );
90         AX_reg(context) = 0x0000; /* failure */
91         BL_reg(context) = 0x80;   /* function not implemented */
92         break;
93     }
94 }