Allow the implementation of the VxDCall entry points to be moved to
[wine] / dlls / vwin32.vxd / vwin32.c
1 /*
2  * VWIN32 VxD implementation
3  *
4  * Copyright 1998 Marcus Meissner
5  * Copyright 1998 Ulrich Weigand
6  * Copyright 1998 Patrik Stridvall
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <stdarg.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winioctl.h"
28 #include "wine/debug.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(vxd);
31
32 extern void WINAPI CallBuiltinHandler( CONTEXT86 *context, BYTE intnum );  /* from winedos */
33
34 /* Pop a DWORD from the 32-bit stack */
35 static inline DWORD stack32_pop( CONTEXT86 *context )
36 {
37     DWORD ret = *(DWORD *)context->Esp;
38     context->Esp += sizeof(DWORD);
39     return ret;
40 }
41
42 static void DIOCRegs_2_CONTEXT( DIOC_REGISTERS *pIn, CONTEXT86 *pCxt )
43 {
44     memset( pCxt, 0, sizeof(*pCxt) );
45     /* Note: segment registers == 0 means that CTX_SEG_OFF_TO_LIN
46              will interpret 32-bit register contents as linear pointers */
47
48     pCxt->ContextFlags=CONTEXT86_INTEGER|CONTEXT86_CONTROL;
49     pCxt->Eax = pIn->reg_EAX;
50     pCxt->Ebx = pIn->reg_EBX;
51     pCxt->Ecx = pIn->reg_ECX;
52     pCxt->Edx = pIn->reg_EDX;
53     pCxt->Esi = pIn->reg_ESI;
54     pCxt->Edi = pIn->reg_EDI;
55
56     /* FIXME: Only partial CONTEXT86_CONTROL */
57
58     pCxt->EFlags = pIn->reg_Flags & ~0x00020000; /* clear vm86 mode */
59 }
60
61 static void CONTEXT_2_DIOCRegs( CONTEXT86 *pCxt, DIOC_REGISTERS *pOut )
62 {
63     memset( pOut, 0, sizeof(DIOC_REGISTERS) );
64
65     pOut->reg_EAX = pCxt->Eax;
66     pOut->reg_EBX = pCxt->Ebx;
67     pOut->reg_ECX = pCxt->Ecx;
68     pOut->reg_EDX = pCxt->Edx;
69     pOut->reg_ESI = pCxt->Esi;
70     pOut->reg_EDI = pCxt->Edi;
71
72     /* FIXME: Only partial CONTEXT86_CONTROL */
73     pOut->reg_Flags = pCxt->EFlags;
74 }
75
76 #define DIOC_AH(regs) (((unsigned char*)&((regs)->reg_EAX))[1])
77 #define DIOC_AL(regs) (((unsigned char*)&((regs)->reg_EAX))[0])
78 #define DIOC_BH(regs) (((unsigned char*)&((regs)->reg_EBX))[1])
79 #define DIOC_BL(regs) (((unsigned char*)&((regs)->reg_EBX))[0])
80 #define DIOC_DH(regs) (((unsigned char*)&((regs)->reg_EDX))[1])
81 #define DIOC_DL(regs) (((unsigned char*)&((regs)->reg_EDX))[0])
82
83 #define DIOC_AX(regs) (((unsigned short*)&((regs)->reg_EAX))[0])
84 #define DIOC_BX(regs) (((unsigned short*)&((regs)->reg_EBX))[0])
85 #define DIOC_CX(regs) (((unsigned short*)&((regs)->reg_ECX))[0])
86 #define DIOC_DX(regs) (((unsigned short*)&((regs)->reg_EDX))[0])
87
88 #define DIOC_SET_CARRY(regs) (((regs)->reg_Flags)|=0x00000001)
89
90 /***********************************************************************
91  *           DeviceIoControl   (VWIN32.VXD.@)
92  */
93 BOOL WINAPI VWIN32_DeviceIoControl(DWORD dwIoControlCode,
94                                    LPVOID lpvInBuffer, DWORD cbInBuffer,
95                                    LPVOID lpvOutBuffer, DWORD cbOutBuffer,
96                                    LPDWORD lpcbBytesReturned, LPOVERLAPPED lpOverlapped)
97 {
98     switch (dwIoControlCode)
99     {
100     case VWIN32_DIOC_DOS_IOCTL:
101     case 0x10: /* Int 0x21 call, call it VWIN_DIOC_INT21 ? */
102     case VWIN32_DIOC_DOS_INT13:
103     case VWIN32_DIOC_DOS_INT25:
104     case VWIN32_DIOC_DOS_INT26:
105     case 0x29: /* Int 0x31 call, call it VWIN_DIOC_INT31 ? */
106     case VWIN32_DIOC_DOS_DRIVEINFO:
107         {
108             CONTEXT86 cxt;
109             DIOC_REGISTERS *pIn  = (DIOC_REGISTERS *)lpvInBuffer;
110             DIOC_REGISTERS *pOut = (DIOC_REGISTERS *)lpvOutBuffer;
111             BYTE intnum = 0;
112
113             TRACE( "Control '%s': "
114                    "eax=0x%08lx, ebx=0x%08lx, ecx=0x%08lx, "
115                    "edx=0x%08lx, esi=0x%08lx, edi=0x%08lx \n",
116                    (dwIoControlCode == VWIN32_DIOC_DOS_IOCTL)? "VWIN32_DIOC_DOS_IOCTL" :
117                    (dwIoControlCode == VWIN32_DIOC_DOS_INT25)? "VWIN32_DIOC_DOS_INT25" :
118                    (dwIoControlCode == VWIN32_DIOC_DOS_INT26)? "VWIN32_DIOC_DOS_INT26" :
119                    (dwIoControlCode == VWIN32_DIOC_DOS_DRIVEINFO)? "VWIN32_DIOC_DOS_DRIVEINFO" :  "???",
120                    pIn->reg_EAX, pIn->reg_EBX, pIn->reg_ECX,
121                    pIn->reg_EDX, pIn->reg_ESI, pIn->reg_EDI );
122
123             DIOCRegs_2_CONTEXT( pIn, &cxt );
124
125             switch (dwIoControlCode)
126             {
127             case VWIN32_DIOC_DOS_IOCTL: /* Call int 21h */
128             case 0x10: /* Int 0x21 call, call it VWIN_DIOC_INT21 ? */
129             case VWIN32_DIOC_DOS_DRIVEINFO:        /* Call int 21h 730x */
130                 intnum = 0x21;
131                 break;
132             case VWIN32_DIOC_DOS_INT13:
133                 intnum = 0x13;
134                 break;
135             case VWIN32_DIOC_DOS_INT25:
136                 intnum = 0x25;
137                 break;
138             case VWIN32_DIOC_DOS_INT26:
139                 intnum = 0x26;
140                 break;
141             case 0x29: /* Int 0x31 call, call it VWIN_DIOC_INT31 ? */
142                 intnum = 0x31;
143                 break;
144             }
145
146             CallBuiltinHandler( &cxt, intnum );
147             CONTEXT_2_DIOCRegs( &cxt, pOut );
148         }
149         return TRUE;
150
151     case VWIN32_DIOC_SIMCTRLC:
152         FIXME( "Control VWIN32_DIOC_SIMCTRLC not implemented\n");
153         return FALSE;
154
155     default:
156         FIXME( "Unknown Control %ld\n", dwIoControlCode);
157         return FALSE;
158     }
159 }
160
161
162 /***********************************************************************
163  *           VxDCall   (VWIN32.VXD.@)
164  *
165  *  Service numbers taken from page 448 of Pietrek's "Windows 95 System
166  *  Programming Secrets".  Parameters from experimentation on real Win98.
167  *
168  */
169 DWORD WINAPI VWIN32_VxDCall( DWORD service, CONTEXT86 *context )
170 {
171     switch ( LOWORD(service) )
172     {
173     case 0x0000: /* GetVersion */
174         {
175             DWORD vers = GetVersion();
176             return (LOBYTE(vers) << 8) | HIBYTE(vers);
177         }
178     case 0x0020: /* Get VMCPD Version */
179         {
180             DWORD parm = stack32_pop(context);
181
182             FIXME("Get VMCPD Version(%08lx): partial stub!\n", parm);
183
184             /* FIXME: This is what Win98 returns, it may
185              *        not be correct in all situations.
186              *        It makes Bleem! happy though.
187              */
188             return 0x0405;
189         }
190     case 0x0029: /* Int31/DPMI dispatch */
191         {
192             DWORD callnum = stack32_pop(context);
193             DWORD parm    = stack32_pop(context);
194
195             TRACE("Int31/DPMI dispatch(%08lx)\n", callnum);
196
197             context->Eax = callnum;
198             context->Ecx = parm;
199             CallBuiltinHandler( context, 0x31 );
200             return LOWORD(context->Eax);
201         }
202     case 0x002a: /* Int41 dispatch - parm = int41 service number */
203         {
204             DWORD callnum = stack32_pop(context);
205             return callnum; /* FIXME: should really call INT_Int41Handler() */
206         }
207     default:
208         FIXME("Unknown service %08lx\n", service);
209         return 0xffffffff;
210     }
211 }