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 | * If you received this file as part of a commercial VirtualBox
|
---|
19 | * distribution, then only the terms of your commercial VirtualBox
|
---|
20 | * license agreement apply instead of the previous paragraph.
|
---|
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 | int vbglLockLinear (void **ppvCtx, void *pv, uint32_t u32Size)
|
---|
31 | {
|
---|
32 | int rc = VINF_SUCCESS;
|
---|
33 |
|
---|
34 | #ifdef __WIN__
|
---|
35 | PMDL pMdl = IoAllocateMdl (pv, u32Size, FALSE, FALSE, NULL);
|
---|
36 |
|
---|
37 | if (pMdl == NULL)
|
---|
38 | {
|
---|
39 | rc = VERR_NOT_SUPPORTED;
|
---|
40 | }
|
---|
41 | else
|
---|
42 | {
|
---|
43 | __try {
|
---|
44 | /* Calls to MmProbeAndLockPages must be enclosed in a try/except block. */
|
---|
45 | MmProbeAndLockPages (pMdl,
|
---|
46 | KernelMode,
|
---|
47 | IoModifyAccess);
|
---|
48 |
|
---|
49 | *ppvCtx = pMdl;
|
---|
50 |
|
---|
51 | } __except(EXCEPTION_EXECUTE_HANDLER) {
|
---|
52 |
|
---|
53 | IoFreeMdl (pMdl);
|
---|
54 | rc = VERR_INVALID_PARAMETER;
|
---|
55 | }
|
---|
56 | }
|
---|
57 | #else
|
---|
58 | NOREF(ppvCtx);
|
---|
59 | NOREF(pv);
|
---|
60 | NOREF(u32Size);
|
---|
61 | #endif /* __WIN__ */
|
---|
62 |
|
---|
63 | return rc;
|
---|
64 | }
|
---|
65 |
|
---|
66 | void vbglUnlockLinear (void *pvCtx, void *pv, uint32_t u32Size)
|
---|
67 | {
|
---|
68 | NOREF(pv);
|
---|
69 | NOREF(u32Size);
|
---|
70 |
|
---|
71 | #ifdef __WIN__
|
---|
72 | PMDL pMdl = (PMDL)pvCtx;
|
---|
73 |
|
---|
74 | if (pMdl != NULL)
|
---|
75 | {
|
---|
76 | MmUnlockPages (pMdl);
|
---|
77 | IoFreeMdl (pMdl);
|
---|
78 | }
|
---|
79 | #else
|
---|
80 | NOREF(pvCtx);
|
---|
81 | #endif /* __WIN__ */
|
---|
82 | }
|
---|
83 |
|
---|
84 | #ifndef VBGL_VBOXGUEST
|
---|
85 |
|
---|
86 | #if defined (__LINUX__) && !defined (__KERNEL__)
|
---|
87 | # include <unistd.h>
|
---|
88 | # include <errno.h>
|
---|
89 | # include <sys/fcntl.h>
|
---|
90 | # include <sys/ioctl.h>
|
---|
91 | #endif
|
---|
92 |
|
---|
93 | #ifndef __WIN__
|
---|
94 | # ifdef __cplusplus
|
---|
95 | extern "C" {
|
---|
96 | # endif
|
---|
97 | extern DECLVBGL(void *) vboxadd_cmc_open (void);
|
---|
98 | extern DECLVBGL(void) vboxadd_cmc_close (void *);
|
---|
99 | extern DECLVBGL(int) vboxadd_cmc_call (void *opaque, uint32_t func, void *data);
|
---|
100 | # ifdef __cplusplus
|
---|
101 | }
|
---|
102 | # endif
|
---|
103 | #endif
|
---|
104 |
|
---|
105 | int vbglDriverOpen (VBGLDRIVER *pDriver)
|
---|
106 | {
|
---|
107 | #ifdef __WIN__
|
---|
108 | UNICODE_STRING uszDeviceName;
|
---|
109 | RtlInitUnicodeString (&uszDeviceName, L"\\Device\\VBoxGuest");
|
---|
110 |
|
---|
111 | PDEVICE_OBJECT pDeviceObject = NULL;
|
---|
112 | PFILE_OBJECT pFileObject = NULL;
|
---|
113 |
|
---|
114 | NTSTATUS rc = IoGetDeviceObjectPointer (&uszDeviceName, FILE_ALL_ACCESS,
|
---|
115 | &pFileObject, &pDeviceObject);
|
---|
116 |
|
---|
117 | if (NT_SUCCESS (rc))
|
---|
118 | {
|
---|
119 | Log(("vbglDriverOpen VBoxGuest successful pDeviceObject=%x\n", pDeviceObject));
|
---|
120 | pDriver->pDeviceObject = pDeviceObject;
|
---|
121 | pDriver->pFileObject = pFileObject;
|
---|
122 | return VINF_SUCCESS;
|
---|
123 | }
|
---|
124 | /** @todo return RTErrConvertFromNtStatus(rc)! */
|
---|
125 | Log(("vbglDriverOpen VBoxGuest failed with ntstatus=%x\n", rc));
|
---|
126 | return rc;
|
---|
127 |
|
---|
128 | #elif defined (__LINUX__)
|
---|
129 | void *opaque;
|
---|
130 |
|
---|
131 | opaque = (void *) vboxadd_cmc_open ();
|
---|
132 | if (!opaque)
|
---|
133 | {
|
---|
134 | return VERR_NOT_IMPLEMENTED;
|
---|
135 | }
|
---|
136 | pDriver->opaque = opaque;
|
---|
137 | return VINF_SUCCESS;
|
---|
138 |
|
---|
139 | #elif defined (__OS2__)
|
---|
140 | return VERR_NOT_IMPLEMENTED;
|
---|
141 |
|
---|
142 | #else
|
---|
143 | # error "Port me"
|
---|
144 | #endif
|
---|
145 | }
|
---|
146 |
|
---|
147 | int vbglDriverIOCtl (VBGLDRIVER *pDriver, uint32_t u32Function, void *pvData, uint32_t cbData)
|
---|
148 | {
|
---|
149 | #ifdef __WIN__
|
---|
150 | IO_STATUS_BLOCK ioStatusBlock;
|
---|
151 |
|
---|
152 | KEVENT Event;
|
---|
153 | KeInitializeEvent (&Event, NotificationEvent, FALSE);
|
---|
154 |
|
---|
155 | PIRP irp = IoBuildDeviceIoControlRequest (u32Function,
|
---|
156 | pDriver->pDeviceObject,
|
---|
157 | pvData,
|
---|
158 | cbData,
|
---|
159 | pvData,
|
---|
160 | cbData,
|
---|
161 | FALSE, /* external */
|
---|
162 | &Event,
|
---|
163 | &ioStatusBlock);
|
---|
164 | if (irp == NULL)
|
---|
165 | {
|
---|
166 | Log(("vbglDriverIOCtl: IoBuildDeviceIoControlRequest failed\n"));
|
---|
167 | return VERR_NO_MEMORY;
|
---|
168 | }
|
---|
169 |
|
---|
170 | NTSTATUS rc = IoCallDriver (pDriver->pDeviceObject, irp);
|
---|
171 |
|
---|
172 | if (rc == STATUS_PENDING)
|
---|
173 | {
|
---|
174 | Log(("vbglDriverIOCtl: STATUS_PENDING\n"));
|
---|
175 | rc = KeWaitForSingleObject(&Event,
|
---|
176 | Executive,
|
---|
177 | KernelMode,
|
---|
178 | FALSE,
|
---|
179 | NULL);
|
---|
180 |
|
---|
181 | rc = ioStatusBlock.Status;
|
---|
182 | }
|
---|
183 |
|
---|
184 | if (!NT_SUCCESS(rc))
|
---|
185 | Log(("vbglDriverIOCtl: IoCallDriver failed with ntstatus=%x\n", rc));
|
---|
186 |
|
---|
187 | return NT_SUCCESS(rc)? VINF_SUCCESS: VERR_VBGL_IOCTL_FAILED;
|
---|
188 |
|
---|
189 | #elif defined (__LINUX__)
|
---|
190 | return vboxadd_cmc_call (pDriver->opaque, u32Function, pvData);
|
---|
191 |
|
---|
192 | #elif defined (__OS2__)
|
---|
193 | return VERR_NOT_IMPLEMENTED;
|
---|
194 |
|
---|
195 | #else
|
---|
196 | # error "Port me"
|
---|
197 | #endif
|
---|
198 | }
|
---|
199 |
|
---|
200 | void vbglDriverClose (VBGLDRIVER *pDriver)
|
---|
201 | {
|
---|
202 | #ifdef __WIN__
|
---|
203 | Log(("vbglDriverClose pDeviceObject=%x\n", pDriver->pDeviceObject));
|
---|
204 | ObDereferenceObject (pDriver->pFileObject);
|
---|
205 |
|
---|
206 | #elif defined (__LINUX__)
|
---|
207 | vboxadd_cmc_close (pDriver->opaque);
|
---|
208 |
|
---|
209 | #elif defined (__OS2__)
|
---|
210 |
|
---|
211 |
|
---|
212 | #else
|
---|
213 | # error "Port me"
|
---|
214 | #endif
|
---|
215 | }
|
---|
216 |
|
---|
217 | #endif /* !VBGL_VBOXGUEST */
|
---|