VirtualBox

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

Last change on this file since 24349 was 24287, checked in by vboxsync, 15 years ago

Made the common parts of the OS/2 additions build again.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 KB
Line 
1/* $Revision: 24287 $ */
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
145# include <VBox/VBoxGuest.h> /* for VBOXGUESTOS2IDCCONNECT */
146RT_C_DECLS_BEGIN
147/*
148 * On OS/2 we'll do the connecting in the assembly code of the
149 * client driver, exporting a g_VBoxGuestIDC symbol containing
150 * the connection information obtained from the 16-bit IDC.
151 */
152extern VBOXGUESTOS2IDCCONNECT g_VBoxGuestIDC;
153RT_C_DECLS_END
154# endif
155
156# if !defined(RT_OS_OS2) \
157 && !defined(RT_OS_WINDOWS)
158RT_C_DECLS_BEGIN
159extern DECLVBGL(void *) VBoxGuestIDCOpen (uint32_t *pu32Version);
160extern DECLVBGL(void) VBoxGuestIDCClose (void *pvOpaque);
161extern DECLVBGL(int) VBoxGuestIDCCall (void *pvOpaque, unsigned int iCmd, void *pvData, size_t cbSize, size_t *pcbReturn);
162RT_C_DECLS_END
163# endif
164
165int vbglDriverOpen (VBGLDRIVER *pDriver)
166{
167# ifdef RT_OS_WINDOWS
168 UNICODE_STRING uszDeviceName;
169 RtlInitUnicodeString (&uszDeviceName, L"\\Device\\VBoxGuest");
170
171 PDEVICE_OBJECT pDeviceObject = NULL;
172 PFILE_OBJECT pFileObject = NULL;
173
174 NTSTATUS rc = IoGetDeviceObjectPointer (&uszDeviceName, FILE_ALL_ACCESS,
175 &pFileObject, &pDeviceObject);
176
177 if (NT_SUCCESS (rc))
178 {
179 Log(("vbglDriverOpen VBoxGuest successful pDeviceObject=%x\n", pDeviceObject));
180 pDriver->pDeviceObject = pDeviceObject;
181 pDriver->pFileObject = pFileObject;
182 return VINF_SUCCESS;
183 }
184 /** @todo return RTErrConvertFromNtStatus(rc)! */
185 Log(("vbglDriverOpen VBoxGuest failed with ntstatus=%x\n", rc));
186 return rc;
187
188# elif defined (RT_OS_OS2)
189 /*
190 * Just check whether the connection was made or not.
191 */
192 if ( g_VBoxGuestIDC.u32Version == VMMDEV_VERSION
193 && VALID_PTR(g_VBoxGuestIDC.u32Session)
194 && VALID_PTR(g_VBoxGuestIDC.pfnServiceEP))
195 {
196 pDriver->u32Session = g_VBoxGuestIDC.u32Session;
197 return VINF_SUCCESS;
198 }
199 pDriver->u32Session = UINT32_MAX;
200 Log(("vbglDriverOpen: failed\n"));
201 return VERR_FILE_NOT_FOUND;
202
203# else
204 uint32_t u32VMMDevVersion;
205 pDriver->pvOpaque = VBoxGuestIDCOpen (&u32VMMDevVersion);
206 if ( pDriver->pvOpaque
207 && u32VMMDevVersion == VMMDEV_VERSION)
208 return VINF_SUCCESS;
209
210 Log(("vbglDriverOpen: failed\n"));
211 return VERR_FILE_NOT_FOUND;
212# endif
213}
214
215# ifdef RT_OS_WINDOWS
216static NTSTATUS vbglDriverIOCtlCompletion (IN PDEVICE_OBJECT DeviceObject,
217 IN PIRP Irp,
218 IN PVOID Context)
219{
220 Log(("VBGL completion %x\n", Irp));
221
222 KEVENT *pEvent = (KEVENT *)Context;
223 KeSetEvent (pEvent, IO_NO_INCREMENT, FALSE);
224
225 return STATUS_MORE_PROCESSING_REQUIRED;
226}
227# endif
228
229int vbglDriverIOCtl (VBGLDRIVER *pDriver, uint32_t u32Function, void *pvData, uint32_t cbData)
230{
231 Log(("vbglDriverIOCtl: pDriver: %p, Func: %x, pvData: %p, cbData: %d\n", pDriver, u32Function, pvData, cbData));
232
233# ifdef RT_OS_WINDOWS
234 KEVENT Event;
235
236 KeInitializeEvent (&Event, NotificationEvent, FALSE);
237
238 /* Have to use the IoAllocateIRP method because this code is generic and
239 * must work in any thread context.
240 * The IoBuildDeviceIoControlRequest, which was used here, does not work
241 * when APCs are disabled, for example.
242 */
243 PIRP irp = IoAllocateIrp (pDriver->pDeviceObject->StackSize, FALSE);
244
245 Log(("vbglDriverIOCtl: irp %p, IRQL = %d\n", irp, KeGetCurrentIrql()));
246
247 if (irp == NULL)
248 {
249 Log(("vbglDriverIOCtl: IRP allocation failed!\n"));
250 return VERR_NO_MEMORY;
251 }
252
253 /*
254 * Setup the IRP_MJ_DEVICE_CONTROL IRP.
255 */
256
257 PIO_STACK_LOCATION nextStack = IoGetNextIrpStackLocation (irp);
258
259 nextStack->MajorFunction = IRP_MJ_DEVICE_CONTROL;
260 nextStack->MinorFunction = 0;
261 nextStack->DeviceObject = pDriver->pDeviceObject;
262 nextStack->Parameters.DeviceIoControl.OutputBufferLength = cbData;
263 nextStack->Parameters.DeviceIoControl.InputBufferLength = cbData;
264 nextStack->Parameters.DeviceIoControl.IoControlCode = u32Function;
265 nextStack->Parameters.DeviceIoControl.Type3InputBuffer = pvData;
266
267 irp->AssociatedIrp.SystemBuffer = pvData; /* Output buffer. */
268 irp->MdlAddress = NULL;
269
270 /* A completion routine is required to signal the Event. */
271 IoSetCompletionRoutine (irp, vbglDriverIOCtlCompletion, &Event, TRUE, TRUE, TRUE);
272
273 NTSTATUS rc = IoCallDriver (pDriver->pDeviceObject, irp);
274
275 if (NT_SUCCESS (rc))
276 {
277 /* Wait the event to be signalled by the completion routine. */
278 KeWaitForSingleObject (&Event,
279 Executive,
280 KernelMode,
281 FALSE,
282 NULL);
283
284 rc = irp->IoStatus.Status;
285
286 Log(("vbglDriverIOCtl: wait completed IRQL = %d\n", KeGetCurrentIrql()));
287 }
288
289 IoFreeIrp (irp);
290
291 if (rc != STATUS_SUCCESS)
292 Log(("vbglDriverIOCtl: ntstatus=%x\n", rc));
293
294 return NT_SUCCESS(rc)? VINF_SUCCESS: VERR_VBGL_IOCTL_FAILED;
295
296# elif defined (RT_OS_OS2)
297 if ( pDriver->u32Session
298 && pDriver->u32Session == g_VBoxGuestIDC.u32Session)
299 return g_VBoxGuestIDC.pfnServiceEP(pDriver->u32Session, u32Function, pvData, cbData, NULL);
300
301 Log(("vbglDriverIOCtl: No connection\n"));
302 return VERR_WRONG_ORDER;
303
304# else
305 return VBoxGuestIDCCall(pDriver->pvOpaque, u32Function, pvData, cbData, NULL);
306# endif
307}
308
309void vbglDriverClose (VBGLDRIVER *pDriver)
310{
311# ifdef RT_OS_WINDOWS
312 Log(("vbglDriverClose pDeviceObject=%x\n", pDriver->pDeviceObject));
313 ObDereferenceObject (pDriver->pFileObject);
314
315# elif defined (RT_OS_OS2)
316 pDriver->u32Session = 0;
317
318# else
319 VBoxGuestIDCClose (pDriver->pvOpaque);
320# endif
321}
322
323#endif /* !VBGL_VBOXGUEST */
324
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