VirtualBox

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

Last change on this file since 4113 was 4071, checked in by vboxsync, 18 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 KB
Line 
1/** @file
2 *
3 * VBoxGuestLib - A support library for VirtualBox guest additions:
4 * Physical memory heap
5 */
6
7/*
8 * Copyright (C) 2006-2007 innotek GmbH
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License as published by the Free Software Foundation,
14 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15 * distribution. VirtualBox OSE is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18#define LOG_GROUP LOG_GROUP_HGCM
19#include <VBox/log.h>
20
21#include <VBox/VBoxGuestLib.h>
22#include "SysHlp.h"
23
24#include <iprt/assert.h>
25#if !defined(RT_OS_WINDOWS) && !defined(RT_OS_LINUX)
26#include <iprt/memobj.h>
27#endif
28
29
30int vbglLockLinear (void **ppvCtx, void *pv, uint32_t u32Size, bool fWriteAccess)
31{
32 int rc = VINF_SUCCESS;
33
34#ifdef RT_OS_WINDOWS
35 PMDL pMdl = IoAllocateMdl (pv, u32Size, FALSE, FALSE, NULL);
36
37 if (pMdl == NULL)
38 {
39 rc = VERR_NOT_SUPPORTED;
40 AssertMsgFailed(("IoAllocateMdl %VGv %x failed!!\n", pv, u32Size));
41 }
42 else
43 {
44 __try {
45 /* Calls to MmProbeAndLockPages must be enclosed in a try/except block. */
46 MmProbeAndLockPages (pMdl,
47 KernelMode,
48 (fWriteAccess) ? IoModifyAccess : IoReadAccess);
49
50 *ppvCtx = pMdl;
51
52 } __except(EXCEPTION_EXECUTE_HANDLER) {
53
54 IoFreeMdl (pMdl);
55 rc = VERR_INVALID_PARAMETER;
56 AssertMsgFailed(("MmProbeAndLockPages %VGv %x failed!!\n", pv, u32Size));
57 }
58 }
59
60#elif defined(RT_OS_LINUX)
61 NOREF(ppvCtx);
62 NOREF(pv);
63 NOREF(u32Size);
64
65#else
66 /* Default to IPRT - this ASSUMES that it is USER addresses we're locking. */
67 RTR0MEMOBJ MemObj;
68 rc = RTR0MemObjLockUser(&MemObj, pv, u32Size, NIL_RTR0PROCESS);
69 if (RT_SUCCESS(rc))
70 *ppvCtx = MemObj;
71 else
72 *ppvCtx = NIL_RTR0MEMOBJ;
73
74#endif
75
76 return rc;
77}
78
79void vbglUnlockLinear (void *pvCtx, void *pv, uint32_t u32Size)
80{
81 NOREF(pv);
82 NOREF(u32Size);
83
84#ifdef RT_OS_WINDOWS
85 PMDL pMdl = (PMDL)pvCtx;
86
87 if (pMdl != NULL)
88 {
89 MmUnlockPages (pMdl);
90 IoFreeMdl (pMdl);
91 }
92
93#elif defined(RT_OS_LINUX)
94 NOREF(pvCtx);
95
96#else
97 /* default to IPRT */
98 RTR0MEMOBJ MemObj = (RTR0MEMOBJ)pvCtx;
99 int rc = RTR0MemObjFree(MemObj, false);
100 AssertRC(rc);
101
102#endif
103}
104
105#ifndef VBGL_VBOXGUEST
106
107#if defined (RT_OS_LINUX) && !defined (__KERNEL__)
108# include <unistd.h>
109# include <errno.h>
110# include <sys/fcntl.h>
111# include <sys/ioctl.h>
112#endif
113
114#ifdef RT_OS_LINUX
115__BEGIN_DECLS
116extern DECLVBGL(void *) vboxadd_cmc_open (void);
117extern DECLVBGL(void) vboxadd_cmc_close (void *);
118extern DECLVBGL(int) vboxadd_cmc_call (void *opaque, uint32_t func, void *data);
119__END_DECLS
120#endif /* RT_OS_LINUX */
121
122#ifdef RT_OS_OS2
123__BEGIN_DECLS
124/*
125 * On OS/2 we'll do the connecting in the assembly code of the
126 * client driver, exporting a g_VBoxGuestIDC symbol containing
127 * the connection information obtained from the 16-bit IDC.
128 */
129extern VBOXGUESTOS2IDCCONNECT g_VBoxGuestIDC;
130__END_DECLS
131#endif
132
133
134int vbglDriverOpen (VBGLDRIVER *pDriver)
135{
136#ifdef RT_OS_WINDOWS
137 UNICODE_STRING uszDeviceName;
138 RtlInitUnicodeString (&uszDeviceName, L"\\Device\\VBoxGuest");
139
140 PDEVICE_OBJECT pDeviceObject = NULL;
141 PFILE_OBJECT pFileObject = NULL;
142
143 NTSTATUS rc = IoGetDeviceObjectPointer (&uszDeviceName, FILE_ALL_ACCESS,
144 &pFileObject, &pDeviceObject);
145
146 if (NT_SUCCESS (rc))
147 {
148 Log(("vbglDriverOpen VBoxGuest successful pDeviceObject=%x\n", pDeviceObject));
149 pDriver->pDeviceObject = pDeviceObject;
150 pDriver->pFileObject = pFileObject;
151 return VINF_SUCCESS;
152 }
153 /** @todo return RTErrConvertFromNtStatus(rc)! */
154 Log(("vbglDriverOpen VBoxGuest failed with ntstatus=%x\n", rc));
155 return rc;
156
157#elif defined (RT_OS_LINUX)
158 void *opaque;
159
160 opaque = (void *) vboxadd_cmc_open ();
161 if (!opaque)
162 {
163 return VERR_NOT_IMPLEMENTED;
164 }
165 pDriver->opaque = opaque;
166 return VINF_SUCCESS;
167
168#elif defined (RT_OS_OS2)
169 /*
170 * Just check whether the connection was made or not.
171 */
172 if ( g_VBoxGuestIDC.u32Version == VMMDEV_VERSION
173 && VALID_PTR(g_VBoxGuestIDC.u32Session)
174 && VALID_PTR(g_VBoxGuestIDC.pfnServiceEP))
175 {
176 pDriver->u32Session = g_VBoxGuestIDC.u32Session;
177 return VINF_SUCCESS;
178 }
179 pDriver->u32Session = UINT32_MAX;
180 Log(("vbglDriverOpen: failed\n"));
181 return VERR_FILE_NOT_FOUND;
182
183#else
184# error "Port me"
185#endif
186}
187
188int vbglDriverIOCtl (VBGLDRIVER *pDriver, uint32_t u32Function, void *pvData, uint32_t cbData)
189{
190#ifdef RT_OS_WINDOWS
191 IO_STATUS_BLOCK ioStatusBlock;
192
193 KEVENT Event;
194 KeInitializeEvent (&Event, NotificationEvent, FALSE);
195
196 PIRP irp = IoBuildDeviceIoControlRequest (u32Function,
197 pDriver->pDeviceObject,
198 pvData,
199 cbData,
200 pvData,
201 cbData,
202 FALSE, /* external */
203 &Event,
204 &ioStatusBlock);
205 if (irp == NULL)
206 {
207 Log(("vbglDriverIOCtl: IoBuildDeviceIoControlRequest failed\n"));
208 return VERR_NO_MEMORY;
209 }
210
211 NTSTATUS rc = IoCallDriver (pDriver->pDeviceObject, irp);
212
213 if (rc == STATUS_PENDING)
214 {
215 Log(("vbglDriverIOCtl: STATUS_PENDING\n"));
216 rc = KeWaitForSingleObject(&Event,
217 Executive,
218 KernelMode,
219 FALSE,
220 NULL);
221
222 rc = ioStatusBlock.Status;
223 }
224
225 if (!NT_SUCCESS(rc))
226 Log(("vbglDriverIOCtl: IoCallDriver failed with ntstatus=%x\n", rc));
227
228 return NT_SUCCESS(rc)? VINF_SUCCESS: VERR_VBGL_IOCTL_FAILED;
229
230#elif defined (RT_OS_LINUX)
231 return vboxadd_cmc_call (pDriver->opaque, u32Function, pvData);
232
233#elif defined (RT_OS_OS2)
234 if ( pDriver->u32Session
235 && pDriver->u32Session == g_VBoxGuestIDC.u32Session)
236 return g_VBoxGuestIDC.pfnServiceEP(pDriver->u32Session, u32Function, pvData, cbData, NULL);
237
238 Log(("vbglDriverIOCtl: No connection\n"));
239 return VERR_WRONG_ORDER;
240
241#else
242# error "Port me"
243#endif
244}
245
246void vbglDriverClose (VBGLDRIVER *pDriver)
247{
248#ifdef RT_OS_WINDOWS
249 Log(("vbglDriverClose pDeviceObject=%x\n", pDriver->pDeviceObject));
250 ObDereferenceObject (pDriver->pFileObject);
251
252#elif defined (RT_OS_LINUX)
253 vboxadd_cmc_close (pDriver->opaque);
254
255#elif defined (RT_OS_OS2)
256 pDriver->u32Session = 0;
257
258#else
259# error "Port me"
260#endif
261}
262
263#endif /* !VBGL_VBOXGUEST */
Note: See TracBrowser for help on using the repository browser.

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