VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuestLib/SysHlp.cpp@ 24259

Last change on this file since 24259 was 23612, checked in by vboxsync, 15 years ago

build fix.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.5 KB
Line 
1/* $Revision: 23612 $ */
2/** @file
3 * VBoxGuestLibR0 - IDC with VBoxGuest and HGCM helpers.
4 */
5
6/*
7 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22#define LOG_GROUP LOG_GROUP_HGCM
23#include <VBox/log.h>
24
25#include <VBox/VBoxGuestLib.h>
26#include "SysHlp.h"
27
28#include <iprt/assert.h>
29
30#ifdef VBGL_VBOXGUEST
31
32#if !defined (RT_OS_WINDOWS)
33# include <iprt/memobj.h>
34# include <iprt/mem.h>
35#endif
36
37
38/**
39 * Internal worker for locking a range of linear addresses.
40 *
41 * @returns VBox status code.
42 * @param ppvCtx Where to store context data.
43 * @param pv The start of the range.
44 * @param u32Size The size of the range.
45 * @param fWriteAccess Lock for read-write (true) or readonly (false).
46 * @param fFlags HGCM call flags, VBGLR0_HGCM_F_XXX.
47 */
48int vbglLockLinear (void **ppvCtx, void *pv, uint32_t u32Size, bool fWriteAccess, uint32_t fFlags)
49{
50 int rc = VINF_SUCCESS;
51
52 /* Zero size buffers shouldn't be locked. */
53 if (u32Size == 0)
54 {
55 Assert(pv == NULL);
56#ifdef RT_OS_WINDOWS
57 *ppvCtx = NULL;
58#else
59 *ppvCtx = NIL_RTR0MEMOBJ;
60#endif
61 return VINF_SUCCESS;
62 }
63
64 /** @todo just use IPRT here. the extra allocation shouldn't matter much...
65 * Then we can most all this up one level even. */
66#ifdef RT_OS_WINDOWS
67 PMDL pMdl = IoAllocateMdl (pv, u32Size, FALSE, FALSE, NULL);
68
69 if (pMdl == NULL)
70 {
71 rc = VERR_NOT_SUPPORTED;
72 AssertMsgFailed(("IoAllocateMdl %p %x failed!!\n", pv, u32Size));
73 }
74 else
75 {
76 __try {
77 /* Calls to MmProbeAndLockPages must be enclosed in a try/except block. */
78 MmProbeAndLockPages (pMdl,
79 /** @todo (fFlags & VBGLR0_HGCMCALL_F_MODE_MASK) == VBGLR0_HGCMCALL_F_USER? UserMode: KernelMode */
80 KernelMode,
81 (fWriteAccess) ? IoModifyAccess : IoReadAccess);
82
83 *ppvCtx = pMdl;
84
85 } __except(EXCEPTION_EXECUTE_HANDLER) {
86
87 IoFreeMdl (pMdl);
88 /** @todo */
89 rc = VERR_INVALID_PARAMETER;
90 AssertMsgFailed(("MmProbeAndLockPages %p %x failed!!\n", pv, u32Size));
91 }
92 }
93
94#else
95 /*
96 * Lock depending on context.
97 *
98 * Note: We will later use the memory object here to convert the HGCM
99 * linear buffer paramter into a physical page list. This is why
100 * we lock both kernel pages on all systems, even those where we
101 * know they aren't pagable.
102 */
103 RTR0MEMOBJ MemObj = NIL_RTR0MEMOBJ;
104 uint32_t fAccess = RTMEM_PROT_READ | (fWriteAccess ? RTMEM_PROT_WRITE : 0);
105 if ((fFlags & VBGLR0_HGCMCALL_F_MODE_MASK) == VBGLR0_HGCMCALL_F_USER)
106 rc = RTR0MemObjLockUser(&MemObj, (RTR3PTR)pv, u32Size, fAccess, NIL_RTR0PROCESS);
107 else
108 rc = RTR0MemObjLockKernel(&MemObj, pv, u32Size, fAccess);
109 if (RT_SUCCESS(rc))
110 *ppvCtx = MemObj;
111 else
112 *ppvCtx = NIL_RTR0MEMOBJ;
113
114#endif
115
116 return rc;
117}
118
119void vbglUnlockLinear (void *pvCtx, void *pv, uint32_t u32Size)
120{
121 NOREF(pv);
122 NOREF(u32Size);
123
124#ifdef RT_OS_WINDOWS
125 PMDL pMdl = (PMDL)pvCtx;
126
127 Assert(pMdl);
128 if (pMdl != NULL)
129 {
130 MmUnlockPages (pMdl);
131 IoFreeMdl (pMdl);
132 }
133
134#else
135 RTR0MEMOBJ MemObj = (RTR0MEMOBJ)pvCtx;
136 int rc = RTR0MemObjFree(MemObj, false);
137 AssertRC(rc);
138
139#endif
140}
141
142#else /* !VBGL_VBOXGUEST */
143
144# ifdef RT_OS_OS2
145RT_C_DECLS_BEGIN
146/*
147 * On OS/2 we'll do the connecting in the assembly code of the
148 * client driver, exporting a g_VBoxGuestIDC symbol containing
149 * the connection information obtained from the 16-bit IDC.
150 */
151extern VBOXGUESTOS2IDCCONNECT g_VBoxGuestIDC;
152RT_C_DECLS_END
153# endif
154
155# if !defined(RT_OS_OS2) \
156 && !defined(RT_OS_WINDOWS)
157RT_C_DECLS_BEGIN
158extern DECLVBGL(void *) VBoxGuestIDCOpen (uint32_t *pu32Version);
159extern DECLVBGL(void) VBoxGuestIDCClose (void *pvOpaque);
160extern DECLVBGL(int) VBoxGuestIDCCall (void *pvOpaque, unsigned int iCmd, void *pvData, size_t cbSize, size_t *pcbReturn);
161RT_C_DECLS_END
162# endif
163
164int vbglDriverOpen (VBGLDRIVER *pDriver)
165{
166# ifdef RT_OS_WINDOWS
167 UNICODE_STRING uszDeviceName;
168 RtlInitUnicodeString (&uszDeviceName, L"\\Device\\VBoxGuest");
169
170 PDEVICE_OBJECT pDeviceObject = NULL;
171 PFILE_OBJECT pFileObject = NULL;
172
173 NTSTATUS rc = IoGetDeviceObjectPointer (&uszDeviceName, FILE_ALL_ACCESS,
174 &pFileObject, &pDeviceObject);
175
176 if (NT_SUCCESS (rc))
177 {
178 Log(("vbglDriverOpen VBoxGuest successful pDeviceObject=%x\n", pDeviceObject));
179 pDriver->pDeviceObject = pDeviceObject;
180 pDriver->pFileObject = pFileObject;
181 return VINF_SUCCESS;
182 }
183 /** @todo return RTErrConvertFromNtStatus(rc)! */
184 Log(("vbglDriverOpen VBoxGuest failed with ntstatus=%x\n", rc));
185 return rc;
186
187# elif defined (RT_OS_OS2)
188 /*
189 * Just check whether the connection was made or not.
190 */
191 if ( g_VBoxGuestIDC.u32Version == VMMDEV_VERSION
192 && VALID_PTR(g_VBoxGuestIDC.u32Session)
193 && VALID_PTR(g_VBoxGuestIDC.pfnServiceEP))
194 {
195 pDriver->u32Session = g_VBoxGuestIDC.u32Session;
196 return VINF_SUCCESS;
197 }
198 pDriver->u32Session = UINT32_MAX;
199 Log(("vbglDriverOpen: failed\n"));
200 return VERR_FILE_NOT_FOUND;
201
202# else
203 uint32_t u32VMMDevVersion;
204 pDriver->pvOpaque = VBoxGuestIDCOpen (&u32VMMDevVersion);
205 if ( pDriver->pvOpaque
206 && u32VMMDevVersion == VMMDEV_VERSION)
207 return VINF_SUCCESS;
208
209 Log(("vbglDriverOpen: failed\n"));
210 return VERR_FILE_NOT_FOUND;
211# endif
212}
213
214# ifdef RT_OS_WINDOWS
215static NTSTATUS vbglDriverIOCtlCompletion (IN PDEVICE_OBJECT DeviceObject,
216 IN PIRP Irp,
217 IN PVOID Context)
218{
219 Log(("VBGL completion %x\n", Irp));
220
221 KEVENT *pEvent = (KEVENT *)Context;
222 KeSetEvent (pEvent, IO_NO_INCREMENT, FALSE);
223
224 return STATUS_MORE_PROCESSING_REQUIRED;
225}
226# endif
227
228int vbglDriverIOCtl (VBGLDRIVER *pDriver, uint32_t u32Function, void *pvData, uint32_t cbData)
229{
230 Log(("vbglDriverIOCtl: pDriver: %p, Func: %x, pvData: %p, cbData: %d\n", pDriver, u32Function, pvData, cbData));
231
232# ifdef RT_OS_WINDOWS
233 KEVENT Event;
234
235 KeInitializeEvent (&Event, NotificationEvent, FALSE);
236
237 /* Have to use the IoAllocateIRP method because this code is generic and
238 * must work in any thread context.
239 * The IoBuildDeviceIoControlRequest, which was used here, does not work
240 * when APCs are disabled, for example.
241 */
242 PIRP irp = IoAllocateIrp (pDriver->pDeviceObject->StackSize, FALSE);
243
244 Log(("vbglDriverIOCtl: irp %p, IRQL = %d\n", irp, KeGetCurrentIrql()));
245
246 if (irp == NULL)
247 {
248 Log(("vbglDriverIOCtl: IRP allocation failed!\n"));
249 return VERR_NO_MEMORY;
250 }
251
252 /*
253 * Setup the IRP_MJ_DEVICE_CONTROL IRP.
254 */
255
256 PIO_STACK_LOCATION nextStack = IoGetNextIrpStackLocation (irp);
257
258 nextStack->MajorFunction = IRP_MJ_DEVICE_CONTROL;
259 nextStack->MinorFunction = 0;
260 nextStack->DeviceObject = pDriver->pDeviceObject;
261 nextStack->Parameters.DeviceIoControl.OutputBufferLength = cbData;
262 nextStack->Parameters.DeviceIoControl.InputBufferLength = cbData;
263 nextStack->Parameters.DeviceIoControl.IoControlCode = u32Function;
264 nextStack->Parameters.DeviceIoControl.Type3InputBuffer = pvData;
265
266 irp->AssociatedIrp.SystemBuffer = pvData; /* Output buffer. */
267 irp->MdlAddress = NULL;
268
269 /* A completion routine is required to signal the Event. */
270 IoSetCompletionRoutine (irp, vbglDriverIOCtlCompletion, &Event, TRUE, TRUE, TRUE);
271
272 NTSTATUS rc = IoCallDriver (pDriver->pDeviceObject, irp);
273
274 if (NT_SUCCESS (rc))
275 {
276 /* Wait the event to be signalled by the completion routine. */
277 KeWaitForSingleObject (&Event,
278 Executive,
279 KernelMode,
280 FALSE,
281 NULL);
282
283 rc = irp->IoStatus.Status;
284
285 Log(("vbglDriverIOCtl: wait completed IRQL = %d\n", KeGetCurrentIrql()));
286 }
287
288 IoFreeIrp (irp);
289
290 if (rc != STATUS_SUCCESS)
291 Log(("vbglDriverIOCtl: ntstatus=%x\n", rc));
292
293 return NT_SUCCESS(rc)? VINF_SUCCESS: VERR_VBGL_IOCTL_FAILED;
294
295# elif defined (RT_OS_OS2)
296 if ( pDriver->u32Session
297 && pDriver->u32Session == g_VBoxGuestIDC.u32Session)
298 return g_VBoxGuestIDC.pfnServiceEP(pDriver->u32Session, u32Function, pvData, cbData, NULL);
299
300 Log(("vbglDriverIOCtl: No connection\n"));
301 return VERR_WRONG_ORDER;
302
303# else
304 return VBoxGuestIDCCall(pDriver->pvOpaque, u32Function, pvData, cbData, NULL);
305# endif
306}
307
308void vbglDriverClose (VBGLDRIVER *pDriver)
309{
310# ifdef RT_OS_WINDOWS
311 Log(("vbglDriverClose pDeviceObject=%x\n", pDriver->pDeviceObject));
312 ObDereferenceObject (pDriver->pFileObject);
313
314# elif defined (RT_OS_OS2)
315 pDriver->u32Session = 0;
316
317# else
318 VBoxGuestIDCClose (pDriver->pvOpaque);
319# endif
320}
321
322#endif /* !VBGL_VBOXGUEST */
323
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette