VirtualBox

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

Last change on this file since 58029 was 56294, checked in by vboxsync, 10 years ago

Additions/common,Additions/os2: Updated (C) year.

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