1 | /* $Id: HGCMInternal.cpp 55401 2015-04-23 10:03:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuestLib - Host-Guest Communication Manager internal functions, implemented by VBoxGuest
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2014 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 | /* Entire file is ifdef'ed with VBGL_VBOXGUEST */
|
---|
28 | #ifdef VBGL_VBOXGUEST
|
---|
29 |
|
---|
30 | /*******************************************************************************
|
---|
31 | * Header Files *
|
---|
32 | *******************************************************************************/
|
---|
33 | #define LOG_GROUP LOG_GROUP_HGCM
|
---|
34 |
|
---|
35 | #include "VBGLInternal.h"
|
---|
36 | #include <iprt/alloca.h>
|
---|
37 | #include <iprt/asm.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include <iprt/mem.h>
|
---|
40 | #include <iprt/memobj.h>
|
---|
41 | #include <iprt/string.h>
|
---|
42 | #include <iprt/thread.h>
|
---|
43 | #include <iprt/time.h>
|
---|
44 |
|
---|
45 | /*******************************************************************************
|
---|
46 | * Defined Constants And Macros *
|
---|
47 | *******************************************************************************/
|
---|
48 | /** The max parameter buffer size for a user request. */
|
---|
49 | #define VBGLR0_MAX_HGCM_USER_PARM (24*_1M)
|
---|
50 | /** The max parameter buffer size for a kernel request. */
|
---|
51 | #define VBGLR0_MAX_HGCM_KERNEL_PARM (16*_1M)
|
---|
52 | #if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN)
|
---|
53 | /** Linux needs to use bounce buffers since RTR0MemObjLockUser has unwanted
|
---|
54 | * side effects.
|
---|
55 | * Darwin 32bit & 64bit also needs this because of 4GB/4GB user/kernel space. */
|
---|
56 | # define USE_BOUNCE_BUFFERS
|
---|
57 | #endif
|
---|
58 |
|
---|
59 |
|
---|
60 | /*******************************************************************************
|
---|
61 | * Structures and Typedefs *
|
---|
62 | *******************************************************************************/
|
---|
63 | /**
|
---|
64 | * Lock info structure used by VbglR0HGCMInternalCall and its helpers.
|
---|
65 | */
|
---|
66 | struct VbglR0ParmInfo
|
---|
67 | {
|
---|
68 | uint32_t cLockBufs;
|
---|
69 | struct
|
---|
70 | {
|
---|
71 | uint32_t iParm;
|
---|
72 | RTR0MEMOBJ hObj;
|
---|
73 | #ifdef USE_BOUNCE_BUFFERS
|
---|
74 | void *pvSmallBuf;
|
---|
75 | #endif
|
---|
76 | } aLockBufs[10];
|
---|
77 | };
|
---|
78 |
|
---|
79 |
|
---|
80 |
|
---|
81 | /* These functions can be only used by VBoxGuest. */
|
---|
82 |
|
---|
83 | DECLVBGL(int) VbglR0HGCMInternalConnect (VBoxGuestHGCMConnectInfo *pConnectInfo,
|
---|
84 | PFNVBGLHGCMCALLBACK pfnAsyncCallback, void *pvAsyncData, uint32_t u32AsyncData)
|
---|
85 | {
|
---|
86 | VMMDevHGCMConnect *pHGCMConnect;
|
---|
87 | int rc;
|
---|
88 |
|
---|
89 | if (!pConnectInfo || !pfnAsyncCallback)
|
---|
90 | return VERR_INVALID_PARAMETER;
|
---|
91 |
|
---|
92 | pHGCMConnect = NULL;
|
---|
93 |
|
---|
94 | /* Allocate request */
|
---|
95 | rc = VbglGRAlloc ((VMMDevRequestHeader **)&pHGCMConnect, sizeof (VMMDevHGCMConnect), VMMDevReq_HGCMConnect);
|
---|
96 |
|
---|
97 | if (RT_SUCCESS(rc))
|
---|
98 | {
|
---|
99 | /* Initialize request memory */
|
---|
100 | pHGCMConnect->header.fu32Flags = 0;
|
---|
101 |
|
---|
102 | memcpy (&pHGCMConnect->loc, &pConnectInfo->Loc, sizeof (HGCMServiceLocation));
|
---|
103 | pHGCMConnect->u32ClientID = 0;
|
---|
104 |
|
---|
105 | /* Issue request */
|
---|
106 | rc = VbglGRPerform (&pHGCMConnect->header.header);
|
---|
107 |
|
---|
108 | if (RT_SUCCESS(rc))
|
---|
109 | {
|
---|
110 | /* Check if host decides to process the request asynchronously. */
|
---|
111 | if (rc == VINF_HGCM_ASYNC_EXECUTE)
|
---|
112 | {
|
---|
113 | /* Wait for request completion interrupt notification from host */
|
---|
114 | pfnAsyncCallback (&pHGCMConnect->header, pvAsyncData, u32AsyncData);
|
---|
115 | }
|
---|
116 |
|
---|
117 | pConnectInfo->result = pHGCMConnect->header.result;
|
---|
118 |
|
---|
119 | if (RT_SUCCESS (pConnectInfo->result))
|
---|
120 | pConnectInfo->u32ClientID = pHGCMConnect->u32ClientID;
|
---|
121 | }
|
---|
122 |
|
---|
123 | VbglGRFree (&pHGCMConnect->header.header);
|
---|
124 | }
|
---|
125 |
|
---|
126 | return rc;
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | DECLR0VBGL(int) VbglR0HGCMInternalDisconnect (VBoxGuestHGCMDisconnectInfo *pDisconnectInfo,
|
---|
131 | PFNVBGLHGCMCALLBACK pfnAsyncCallback, void *pvAsyncData, uint32_t u32AsyncData)
|
---|
132 | {
|
---|
133 | VMMDevHGCMDisconnect *pHGCMDisconnect;
|
---|
134 | int rc;
|
---|
135 |
|
---|
136 | if (!pDisconnectInfo || !pfnAsyncCallback)
|
---|
137 | return VERR_INVALID_PARAMETER;
|
---|
138 |
|
---|
139 | pHGCMDisconnect = NULL;
|
---|
140 |
|
---|
141 | /* Allocate request */
|
---|
142 | rc = VbglGRAlloc ((VMMDevRequestHeader **)&pHGCMDisconnect, sizeof (VMMDevHGCMDisconnect), VMMDevReq_HGCMDisconnect);
|
---|
143 |
|
---|
144 | if (RT_SUCCESS(rc))
|
---|
145 | {
|
---|
146 | /* Initialize request memory */
|
---|
147 | pHGCMDisconnect->header.fu32Flags = 0;
|
---|
148 |
|
---|
149 | pHGCMDisconnect->u32ClientID = pDisconnectInfo->u32ClientID;
|
---|
150 |
|
---|
151 | /* Issue request */
|
---|
152 | rc = VbglGRPerform (&pHGCMDisconnect->header.header);
|
---|
153 |
|
---|
154 | if (RT_SUCCESS(rc))
|
---|
155 | {
|
---|
156 | /* Check if host decides to process the request asynchronously. */
|
---|
157 | if (rc == VINF_HGCM_ASYNC_EXECUTE)
|
---|
158 | {
|
---|
159 | /* Wait for request completion interrupt notification from host */
|
---|
160 | pfnAsyncCallback (&pHGCMDisconnect->header, pvAsyncData, u32AsyncData);
|
---|
161 | }
|
---|
162 |
|
---|
163 | pDisconnectInfo->result = pHGCMDisconnect->header.result;
|
---|
164 | }
|
---|
165 |
|
---|
166 | VbglGRFree (&pHGCMDisconnect->header.header);
|
---|
167 | }
|
---|
168 |
|
---|
169 | return rc;
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Preprocesses the HGCM call, validating and locking/buffering parameters.
|
---|
175 | *
|
---|
176 | * @returns VBox status code.
|
---|
177 | *
|
---|
178 | * @param pCallInfo The call info.
|
---|
179 | * @param cbCallInfo The size of the call info structure.
|
---|
180 | * @param fIsUser Is it a user request or kernel request.
|
---|
181 | * @param pcbExtra Where to return the extra request space needed for
|
---|
182 | * physical page lists.
|
---|
183 | */
|
---|
184 | static int vbglR0HGCMInternalPreprocessCall(VBoxGuestHGCMCallInfo const *pCallInfo, uint32_t cbCallInfo,
|
---|
185 | bool fIsUser, struct VbglR0ParmInfo *pParmInfo, size_t *pcbExtra)
|
---|
186 | {
|
---|
187 | HGCMFunctionParameter const *pSrcParm = VBOXGUEST_HGCM_CALL_PARMS(pCallInfo);
|
---|
188 | uint32_t cParms = pCallInfo->cParms;
|
---|
189 | uint32_t iParm;
|
---|
190 | uint32_t cb;
|
---|
191 |
|
---|
192 | /*
|
---|
193 | * Lock down the any linear buffers so we can get their addresses
|
---|
194 | * and figure out how much extra storage we need for page lists.
|
---|
195 | *
|
---|
196 | * Note! With kernel mode users we can be assertive. For user mode users
|
---|
197 | * we should just (debug) log it and fail without any fanfare.
|
---|
198 | */
|
---|
199 | *pcbExtra = 0;
|
---|
200 | pParmInfo->cLockBufs = 0;
|
---|
201 | for (iParm = 0; iParm < cParms; iParm++, pSrcParm++)
|
---|
202 | {
|
---|
203 | switch (pSrcParm->type)
|
---|
204 | {
|
---|
205 | case VMMDevHGCMParmType_32bit:
|
---|
206 | Log4(("GstHGCMCall: parm=%u type=32bit: %#010x\n", iParm, pSrcParm->u.value32));
|
---|
207 | break;
|
---|
208 |
|
---|
209 | case VMMDevHGCMParmType_64bit:
|
---|
210 | Log4(("GstHGCMCall: parm=%u type=64bit: %#018RX64\n", iParm, pSrcParm->u.value64));
|
---|
211 | break;
|
---|
212 |
|
---|
213 | case VMMDevHGCMParmType_PageList:
|
---|
214 | if (fIsUser)
|
---|
215 | return VERR_INVALID_PARAMETER;
|
---|
216 | cb = pSrcParm->u.PageList.size;
|
---|
217 | if (cb)
|
---|
218 | {
|
---|
219 | uint32_t off = pSrcParm->u.PageList.offset;
|
---|
220 | HGCMPageListInfo *pPgLst;
|
---|
221 | uint32_t cPages;
|
---|
222 | uint32_t u32;
|
---|
223 |
|
---|
224 | AssertMsgReturn(cb <= VBGLR0_MAX_HGCM_KERNEL_PARM, ("%#x > %#x\n", cb, VBGLR0_MAX_HGCM_KERNEL_PARM),
|
---|
225 | VERR_OUT_OF_RANGE);
|
---|
226 | AssertMsgReturn( off >= pCallInfo->cParms * sizeof(HGCMFunctionParameter)
|
---|
227 | && off <= cbCallInfo - sizeof(HGCMPageListInfo),
|
---|
228 | ("offset=%#x cParms=%#x cbCallInfo=%#x\n", off, pCallInfo->cParms, cbCallInfo),
|
---|
229 | VERR_INVALID_PARAMETER);
|
---|
230 |
|
---|
231 | pPgLst = (HGCMPageListInfo *)((uint8_t *)pCallInfo + off);
|
---|
232 | cPages = pPgLst->cPages;
|
---|
233 | u32 = RT_OFFSETOF(HGCMPageListInfo, aPages[cPages]) + off;
|
---|
234 | AssertMsgReturn(u32 <= cbCallInfo,
|
---|
235 | ("u32=%#x (cPages=%#x offset=%#x) cbCallInfo=%#x\n", u32, cPages, off, cbCallInfo),
|
---|
236 | VERR_INVALID_PARAMETER);
|
---|
237 | AssertMsgReturn(pPgLst->offFirstPage < PAGE_SIZE, ("#x\n", pPgLst->offFirstPage), VERR_INVALID_PARAMETER);
|
---|
238 | u32 = RT_ALIGN_32(pPgLst->offFirstPage + cb, PAGE_SIZE) >> PAGE_SHIFT;
|
---|
239 | AssertMsgReturn(cPages == u32, ("cPages=%#x u32=%#x\n", cPages, u32), VERR_INVALID_PARAMETER);
|
---|
240 | AssertMsgReturn(VBOX_HGCM_F_PARM_ARE_VALID(pPgLst->flags), ("%#x\n", pPgLst->flags), VERR_INVALID_PARAMETER);
|
---|
241 | Log4(("GstHGCMCall: parm=%u type=pglst: cb=%#010x cPgs=%u offPg0=%#x flags=%#x\n",
|
---|
242 | iParm, cb, cPages, pPgLst->offFirstPage, pPgLst->flags));
|
---|
243 | u32 = cPages;
|
---|
244 | while (u32-- > 0)
|
---|
245 | {
|
---|
246 | Log4(("GstHGCMCall: pg#%u=%RHp\n", u32, pPgLst->aPages[u32]));
|
---|
247 | AssertMsgReturn(!(pPgLst->aPages[u32] & (PAGE_OFFSET_MASK | UINT64_C(0xfff0000000000000))),
|
---|
248 | ("pg#%u=%RHp\n", u32, pPgLst->aPages[u32]),
|
---|
249 | VERR_INVALID_PARAMETER);
|
---|
250 | }
|
---|
251 |
|
---|
252 | *pcbExtra += RT_OFFSETOF(HGCMPageListInfo, aPages[pPgLst->cPages]);
|
---|
253 | }
|
---|
254 | else
|
---|
255 | Log4(("GstHGCMCall: parm=%u type=pglst: cb=0\n", iParm));
|
---|
256 | break;
|
---|
257 |
|
---|
258 | case VMMDevHGCMParmType_LinAddr_Locked_In:
|
---|
259 | case VMMDevHGCMParmType_LinAddr_Locked_Out:
|
---|
260 | case VMMDevHGCMParmType_LinAddr_Locked:
|
---|
261 | if (fIsUser)
|
---|
262 | return VERR_INVALID_PARAMETER;
|
---|
263 | if (!VBGLR0_CAN_USE_PHYS_PAGE_LIST(/*a_fLocked =*/ true))
|
---|
264 | {
|
---|
265 | cb = pSrcParm->u.Pointer.size;
|
---|
266 | AssertMsgReturn(cb <= VBGLR0_MAX_HGCM_KERNEL_PARM, ("%#x > %#x\n", cb, VBGLR0_MAX_HGCM_KERNEL_PARM),
|
---|
267 | VERR_OUT_OF_RANGE);
|
---|
268 | if (cb != 0)
|
---|
269 | Log4(("GstHGCMCall: parm=%u type=%#x: cb=%#010x pv=%p\n",
|
---|
270 | iParm, pSrcParm->type, cb, pSrcParm->u.Pointer.u.linearAddr));
|
---|
271 | else
|
---|
272 | Log4(("GstHGCMCall: parm=%u type=%#x: cb=0\n", iParm, pSrcParm->type));
|
---|
273 | break;
|
---|
274 | }
|
---|
275 | /* fall thru */
|
---|
276 |
|
---|
277 | case VMMDevHGCMParmType_LinAddr_In:
|
---|
278 | case VMMDevHGCMParmType_LinAddr_Out:
|
---|
279 | case VMMDevHGCMParmType_LinAddr:
|
---|
280 | cb = pSrcParm->u.Pointer.size;
|
---|
281 | if (cb != 0)
|
---|
282 | {
|
---|
283 | #ifdef USE_BOUNCE_BUFFERS
|
---|
284 | void *pvSmallBuf = NULL;
|
---|
285 | #endif
|
---|
286 | uint32_t iLockBuf = pParmInfo->cLockBufs;
|
---|
287 | RTR0MEMOBJ hObj;
|
---|
288 | int rc;
|
---|
289 | uint32_t fAccess = pSrcParm->type == VMMDevHGCMParmType_LinAddr_In
|
---|
290 | || pSrcParm->type == VMMDevHGCMParmType_LinAddr_Locked_In
|
---|
291 | ? RTMEM_PROT_READ
|
---|
292 | : RTMEM_PROT_READ | RTMEM_PROT_WRITE;
|
---|
293 |
|
---|
294 | AssertReturn(iLockBuf < RT_ELEMENTS(pParmInfo->aLockBufs), VERR_INVALID_PARAMETER);
|
---|
295 | if (!fIsUser)
|
---|
296 | {
|
---|
297 | AssertMsgReturn(cb <= VBGLR0_MAX_HGCM_KERNEL_PARM, ("%#x > %#x\n", cb, VBGLR0_MAX_HGCM_KERNEL_PARM),
|
---|
298 | VERR_OUT_OF_RANGE);
|
---|
299 | rc = RTR0MemObjLockKernel(&hObj, (void *)pSrcParm->u.Pointer.u.linearAddr, cb, fAccess);
|
---|
300 | if (RT_FAILURE(rc))
|
---|
301 | {
|
---|
302 | Log(("GstHGCMCall: id=%#x fn=%u parm=%u RTR0MemObjLockKernel(,%p,%#x) -> %Rrc\n",
|
---|
303 | pCallInfo->u32ClientID, pCallInfo->u32Function, iParm, pSrcParm->u.Pointer.u.linearAddr, cb, rc));
|
---|
304 | return rc;
|
---|
305 | }
|
---|
306 | Log3(("GstHGCMCall: parm=%u type=%#x: cb=%#010x pv=%p locked kernel -> %p\n",
|
---|
307 | iParm, pSrcParm->type, cb, pSrcParm->u.Pointer.u.linearAddr, hObj));
|
---|
308 | }
|
---|
309 | else if (cb > VBGLR0_MAX_HGCM_USER_PARM)
|
---|
310 | {
|
---|
311 | Log(("GstHGCMCall: id=%#x fn=%u parm=%u pv=%p cb=%#x > %#x -> out of range\n",
|
---|
312 | pCallInfo->u32ClientID, pCallInfo->u32Function, iParm, pSrcParm->u.Pointer.u.linearAddr,
|
---|
313 | cb, VBGLR0_MAX_HGCM_USER_PARM));
|
---|
314 | return VERR_OUT_OF_RANGE;
|
---|
315 | }
|
---|
316 | else
|
---|
317 | {
|
---|
318 | #ifndef USE_BOUNCE_BUFFERS
|
---|
319 | rc = RTR0MemObjLockUser(&hObj, (RTR3PTR)pSrcParm->u.Pointer.u.linearAddr, cb, fAccess, NIL_RTR0PROCESS);
|
---|
320 | if (RT_FAILURE(rc))
|
---|
321 | {
|
---|
322 | Log(("GstHGCMCall: id=%#x fn=%u parm=%u RTR0MemObjLockUser(,%p,%#x,nil) -> %Rrc\n",
|
---|
323 | pCallInfo->u32ClientID, pCallInfo->u32Function, iParm, pSrcParm->u.Pointer.u.linearAddr, cb, rc));
|
---|
324 | return rc;
|
---|
325 | }
|
---|
326 | Log3(("GstHGCMCall: parm=%u type=%#x: cb=%#010x pv=%p locked user -> %p\n",
|
---|
327 | iParm, pSrcParm->type, cb, pSrcParm->u.Pointer.u.linearAddr, hObj));
|
---|
328 |
|
---|
329 | #else /* USE_BOUNCE_BUFFERS */
|
---|
330 | /*
|
---|
331 | * This is a bit massive, but we don't want to waste a
|
---|
332 | * whole page for a 3 byte string buffer (guest props).
|
---|
333 | *
|
---|
334 | * The threshold is ASSUMING sizeof(RTMEMHDR) == 16 and
|
---|
335 | * the system is using some power of two allocator.
|
---|
336 | */
|
---|
337 | /** @todo A more efficient strategy would be to combine buffers. However it
|
---|
338 | * is probably going to be more massive than the current code, so
|
---|
339 | * it can wait till later. */
|
---|
340 | bool fCopyIn = pSrcParm->type != VMMDevHGCMParmType_LinAddr_Out
|
---|
341 | && pSrcParm->type != VMMDevHGCMParmType_LinAddr_Locked_Out;
|
---|
342 | if (cb <= PAGE_SIZE / 2 - 16)
|
---|
343 | {
|
---|
344 | pvSmallBuf = fCopyIn ? RTMemTmpAlloc(cb) : RTMemTmpAllocZ(cb);
|
---|
345 | if (RT_UNLIKELY(!pvSmallBuf))
|
---|
346 | return VERR_NO_MEMORY;
|
---|
347 | if (fCopyIn)
|
---|
348 | {
|
---|
349 | rc = RTR0MemUserCopyFrom(pvSmallBuf, pSrcParm->u.Pointer.u.linearAddr, cb);
|
---|
350 | if (RT_FAILURE(rc))
|
---|
351 | {
|
---|
352 | RTMemTmpFree(pvSmallBuf);
|
---|
353 | Log(("GstHGCMCall: id=%#x fn=%u parm=%u RTR0MemUserCopyFrom(,%p,%#x) -> %Rrc\n",
|
---|
354 | pCallInfo->u32ClientID, pCallInfo->u32Function, iParm,
|
---|
355 | pSrcParm->u.Pointer.u.linearAddr, cb, rc));
|
---|
356 | return rc;
|
---|
357 | }
|
---|
358 | }
|
---|
359 | rc = RTR0MemObjLockKernel(&hObj, pvSmallBuf, cb, fAccess);
|
---|
360 | if (RT_FAILURE(rc))
|
---|
361 | {
|
---|
362 | RTMemTmpFree(pvSmallBuf);
|
---|
363 | Log(("GstHGCMCall: RTR0MemObjLockKernel failed for small buffer: rc=%Rrc pvSmallBuf=%p cb=%#x\n",
|
---|
364 | rc, pvSmallBuf, cb));
|
---|
365 | return rc;
|
---|
366 | }
|
---|
367 | Log3(("GstHGCMCall: parm=%u type=%#x: cb=%#010x pv=%p small buffer %p -> %p\n",
|
---|
368 | iParm, pSrcParm->type, cb, pSrcParm->u.Pointer.u.linearAddr, pvSmallBuf, hObj));
|
---|
369 | }
|
---|
370 | else
|
---|
371 | {
|
---|
372 | rc = RTR0MemObjAllocPage(&hObj, cb, false /*fExecutable*/);
|
---|
373 | if (RT_FAILURE(rc))
|
---|
374 | return rc;
|
---|
375 | if (!fCopyIn)
|
---|
376 | memset(RTR0MemObjAddress(hObj), '\0', cb);
|
---|
377 | else
|
---|
378 | {
|
---|
379 | rc = RTR0MemUserCopyFrom(RTR0MemObjAddress(hObj), pSrcParm->u.Pointer.u.linearAddr, cb);
|
---|
380 | if (RT_FAILURE(rc))
|
---|
381 | {
|
---|
382 | RTR0MemObjFree(hObj, false /*fFreeMappings*/);
|
---|
383 | Log(("GstHGCMCall: id=%#x fn=%u parm=%u RTR0MemUserCopyFrom(,%p,%#x) -> %Rrc\n",
|
---|
384 | pCallInfo->u32ClientID, pCallInfo->u32Function, iParm,
|
---|
385 | pSrcParm->u.Pointer.u.linearAddr, cb, rc));
|
---|
386 | return rc;
|
---|
387 | }
|
---|
388 | }
|
---|
389 | Log3(("GstHGCMCall: parm=%u type=%#x: cb=%#010x pv=%p big buffer -> %p\n",
|
---|
390 | iParm, pSrcParm->type, cb, pSrcParm->u.Pointer.u.linearAddr, hObj));
|
---|
391 | }
|
---|
392 | #endif /* USE_BOUNCE_BUFFERS */
|
---|
393 | }
|
---|
394 |
|
---|
395 | pParmInfo->aLockBufs[iLockBuf].iParm = iParm;
|
---|
396 | pParmInfo->aLockBufs[iLockBuf].hObj = hObj;
|
---|
397 | #ifdef USE_BOUNCE_BUFFERS
|
---|
398 | pParmInfo->aLockBufs[iLockBuf].pvSmallBuf = pvSmallBuf;
|
---|
399 | #endif
|
---|
400 | pParmInfo->cLockBufs = iLockBuf + 1;
|
---|
401 |
|
---|
402 | if (VBGLR0_CAN_USE_PHYS_PAGE_LIST(/*a_fLocked =*/ false))
|
---|
403 | {
|
---|
404 | size_t const cPages = RTR0MemObjSize(hObj) >> PAGE_SHIFT;
|
---|
405 | *pcbExtra += RT_OFFSETOF(HGCMPageListInfo, aPages[cPages]);
|
---|
406 | }
|
---|
407 | }
|
---|
408 | else
|
---|
409 | Log4(("GstHGCMCall: parm=%u type=%#x: cb=0\n", iParm, pSrcParm->type));
|
---|
410 | break;
|
---|
411 |
|
---|
412 | default:
|
---|
413 | return VERR_INVALID_PARAMETER;
|
---|
414 | }
|
---|
415 | }
|
---|
416 |
|
---|
417 | return VINF_SUCCESS;
|
---|
418 | }
|
---|
419 |
|
---|
420 |
|
---|
421 | /**
|
---|
422 | * Translates locked linear address to the normal type.
|
---|
423 | * The locked types are only for the guest side and not handled by the host.
|
---|
424 | *
|
---|
425 | * @returns normal linear address type.
|
---|
426 | * @param enmType The type.
|
---|
427 | */
|
---|
428 | static HGCMFunctionParameterType vbglR0HGCMInternalConvertLinAddrType(HGCMFunctionParameterType enmType)
|
---|
429 | {
|
---|
430 | switch (enmType)
|
---|
431 | {
|
---|
432 | case VMMDevHGCMParmType_LinAddr_Locked_In:
|
---|
433 | return VMMDevHGCMParmType_LinAddr_In;
|
---|
434 | case VMMDevHGCMParmType_LinAddr_Locked_Out:
|
---|
435 | return VMMDevHGCMParmType_LinAddr_Out;
|
---|
436 | case VMMDevHGCMParmType_LinAddr_Locked:
|
---|
437 | return VMMDevHGCMParmType_LinAddr;
|
---|
438 | default:
|
---|
439 | return enmType;
|
---|
440 | }
|
---|
441 | }
|
---|
442 |
|
---|
443 |
|
---|
444 | /**
|
---|
445 | * Translates linear address types to page list direction flags.
|
---|
446 | *
|
---|
447 | * @returns page list flags.
|
---|
448 | * @param enmType The type.
|
---|
449 | */
|
---|
450 | static uint32_t vbglR0HGCMInternalLinAddrTypeToPageListFlags(HGCMFunctionParameterType enmType)
|
---|
451 | {
|
---|
452 | switch (enmType)
|
---|
453 | {
|
---|
454 | case VMMDevHGCMParmType_LinAddr_In:
|
---|
455 | case VMMDevHGCMParmType_LinAddr_Locked_In:
|
---|
456 | return VBOX_HGCM_F_PARM_DIRECTION_TO_HOST;
|
---|
457 |
|
---|
458 | case VMMDevHGCMParmType_LinAddr_Out:
|
---|
459 | case VMMDevHGCMParmType_LinAddr_Locked_Out:
|
---|
460 | return VBOX_HGCM_F_PARM_DIRECTION_FROM_HOST;
|
---|
461 |
|
---|
462 | default: AssertFailed();
|
---|
463 | case VMMDevHGCMParmType_LinAddr:
|
---|
464 | case VMMDevHGCMParmType_LinAddr_Locked:
|
---|
465 | return VBOX_HGCM_F_PARM_DIRECTION_BOTH;
|
---|
466 | }
|
---|
467 | }
|
---|
468 |
|
---|
469 |
|
---|
470 | /**
|
---|
471 | * Initializes the call request that we're sending to the host.
|
---|
472 | *
|
---|
473 | * @returns VBox status code.
|
---|
474 | *
|
---|
475 | * @param pCallInfo The call info.
|
---|
476 | * @param cbCallInfo The size of the call info structure.
|
---|
477 | * @param fIsUser Is it a user request or kernel request.
|
---|
478 | * @param pcbExtra Where to return the extra request space needed for
|
---|
479 | * physical page lists.
|
---|
480 | */
|
---|
481 | static void vbglR0HGCMInternalInitCall(VMMDevHGCMCall *pHGCMCall, VBoxGuestHGCMCallInfo const *pCallInfo,
|
---|
482 | uint32_t cbCallInfo, bool fIsUser, struct VbglR0ParmInfo *pParmInfo)
|
---|
483 | {
|
---|
484 | HGCMFunctionParameter const *pSrcParm = VBOXGUEST_HGCM_CALL_PARMS(pCallInfo);
|
---|
485 | HGCMFunctionParameter *pDstParm = VMMDEV_HGCM_CALL_PARMS(pHGCMCall);
|
---|
486 | uint32_t cParms = pCallInfo->cParms;
|
---|
487 | uint32_t offExtra = (uintptr_t)(pDstParm + cParms) - (uintptr_t)pHGCMCall;
|
---|
488 | uint32_t iLockBuf = 0;
|
---|
489 | uint32_t iParm;
|
---|
490 |
|
---|
491 |
|
---|
492 | /*
|
---|
493 | * The call request headers.
|
---|
494 | */
|
---|
495 | pHGCMCall->header.fu32Flags = 0;
|
---|
496 | pHGCMCall->header.result = VINF_SUCCESS;
|
---|
497 |
|
---|
498 | pHGCMCall->u32ClientID = pCallInfo->u32ClientID;
|
---|
499 | pHGCMCall->u32Function = pCallInfo->u32Function;
|
---|
500 | pHGCMCall->cParms = cParms;
|
---|
501 |
|
---|
502 | /*
|
---|
503 | * The parameters.
|
---|
504 | */
|
---|
505 | for (iParm = 0; iParm < pCallInfo->cParms; iParm++, pSrcParm++, pDstParm++)
|
---|
506 | {
|
---|
507 | switch (pSrcParm->type)
|
---|
508 | {
|
---|
509 | case VMMDevHGCMParmType_32bit:
|
---|
510 | case VMMDevHGCMParmType_64bit:
|
---|
511 | *pDstParm = *pSrcParm;
|
---|
512 | break;
|
---|
513 |
|
---|
514 | case VMMDevHGCMParmType_PageList:
|
---|
515 | pDstParm->type = VMMDevHGCMParmType_PageList;
|
---|
516 | pDstParm->u.PageList.size = pSrcParm->u.PageList.size;
|
---|
517 | if (pSrcParm->u.PageList.size)
|
---|
518 | {
|
---|
519 | HGCMPageListInfo const *pSrcPgLst = (HGCMPageListInfo *)((uint8_t *)pCallInfo + pSrcParm->u.PageList.offset);
|
---|
520 | HGCMPageListInfo *pDstPgLst = (HGCMPageListInfo *)((uint8_t *)pHGCMCall + offExtra);
|
---|
521 | uint32_t const cPages = pSrcPgLst->cPages;
|
---|
522 | uint32_t iPage;
|
---|
523 |
|
---|
524 | pDstParm->u.PageList.offset = offExtra;
|
---|
525 | pDstPgLst->flags = pSrcPgLst->flags;
|
---|
526 | pDstPgLst->offFirstPage = pSrcPgLst->offFirstPage;
|
---|
527 | pDstPgLst->cPages = cPages;
|
---|
528 | for (iPage = 0; iPage < cPages; iPage++)
|
---|
529 | pDstPgLst->aPages[iPage] = pSrcPgLst->aPages[iPage];
|
---|
530 |
|
---|
531 | offExtra += RT_OFFSETOF(HGCMPageListInfo, aPages[cPages]);
|
---|
532 | }
|
---|
533 | else
|
---|
534 | pDstParm->u.PageList.offset = 0;
|
---|
535 | break;
|
---|
536 |
|
---|
537 | case VMMDevHGCMParmType_LinAddr_Locked_In:
|
---|
538 | case VMMDevHGCMParmType_LinAddr_Locked_Out:
|
---|
539 | case VMMDevHGCMParmType_LinAddr_Locked:
|
---|
540 | if (!VBGLR0_CAN_USE_PHYS_PAGE_LIST(/*a_fLocked =*/ true))
|
---|
541 | {
|
---|
542 | *pDstParm = *pSrcParm;
|
---|
543 | pDstParm->type = vbglR0HGCMInternalConvertLinAddrType(pSrcParm->type);
|
---|
544 | break;
|
---|
545 | }
|
---|
546 | /* fall thru */
|
---|
547 |
|
---|
548 | case VMMDevHGCMParmType_LinAddr_In:
|
---|
549 | case VMMDevHGCMParmType_LinAddr_Out:
|
---|
550 | case VMMDevHGCMParmType_LinAddr:
|
---|
551 | if (pSrcParm->u.Pointer.size != 0)
|
---|
552 | {
|
---|
553 | #ifdef USE_BOUNCE_BUFFERS
|
---|
554 | void *pvSmallBuf = pParmInfo->aLockBufs[iLockBuf].pvSmallBuf;
|
---|
555 | #endif
|
---|
556 | RTR0MEMOBJ hObj = pParmInfo->aLockBufs[iLockBuf].hObj;
|
---|
557 | Assert(iParm == pParmInfo->aLockBufs[iLockBuf].iParm);
|
---|
558 |
|
---|
559 | if (VBGLR0_CAN_USE_PHYS_PAGE_LIST(/*a_fLocked =*/ false))
|
---|
560 | {
|
---|
561 | HGCMPageListInfo *pDstPgLst = (HGCMPageListInfo *)((uint8_t *)pHGCMCall + offExtra);
|
---|
562 | size_t const cPages = RTR0MemObjSize(hObj) >> PAGE_SHIFT;
|
---|
563 | size_t iPage;
|
---|
564 |
|
---|
565 | pDstParm->type = VMMDevHGCMParmType_PageList;
|
---|
566 | pDstParm->u.PageList.size = pSrcParm->u.Pointer.size;
|
---|
567 | pDstParm->u.PageList.offset = offExtra;
|
---|
568 | pDstPgLst->flags = vbglR0HGCMInternalLinAddrTypeToPageListFlags(pSrcParm->type);
|
---|
569 | #ifdef USE_BOUNCE_BUFFERS
|
---|
570 | if (fIsUser)
|
---|
571 | pDstPgLst->offFirstPage = (uintptr_t)pvSmallBuf & PAGE_OFFSET_MASK;
|
---|
572 | else
|
---|
573 | #endif
|
---|
574 | pDstPgLst->offFirstPage = pSrcParm->u.Pointer.u.linearAddr & PAGE_OFFSET_MASK;
|
---|
575 | pDstPgLst->cPages = cPages; Assert(pDstPgLst->cPages == cPages);
|
---|
576 | for (iPage = 0; iPage < cPages; iPage++)
|
---|
577 | {
|
---|
578 | pDstPgLst->aPages[iPage] = RTR0MemObjGetPagePhysAddr(hObj, iPage);
|
---|
579 | Assert(pDstPgLst->aPages[iPage] != NIL_RTHCPHYS);
|
---|
580 | }
|
---|
581 |
|
---|
582 | offExtra += RT_OFFSETOF(HGCMPageListInfo, aPages[cPages]);
|
---|
583 | }
|
---|
584 | else
|
---|
585 | {
|
---|
586 | pDstParm->type = vbglR0HGCMInternalConvertLinAddrType(pSrcParm->type);
|
---|
587 | pDstParm->u.Pointer.size = pSrcParm->u.Pointer.size;
|
---|
588 | #ifdef USE_BOUNCE_BUFFERS
|
---|
589 | if (fIsUser)
|
---|
590 | pDstParm->u.Pointer.u.linearAddr = pvSmallBuf
|
---|
591 | ? (uintptr_t)pvSmallBuf
|
---|
592 | : (uintptr_t)RTR0MemObjAddress(hObj);
|
---|
593 | else
|
---|
594 | #endif
|
---|
595 | pDstParm->u.Pointer.u.linearAddr = pSrcParm->u.Pointer.u.linearAddr;
|
---|
596 | }
|
---|
597 | iLockBuf++;
|
---|
598 | }
|
---|
599 | else
|
---|
600 | {
|
---|
601 | pDstParm->type = vbglR0HGCMInternalConvertLinAddrType(pSrcParm->type);
|
---|
602 | pDstParm->u.Pointer.size = 0;
|
---|
603 | pDstParm->u.Pointer.u.linearAddr = 0;
|
---|
604 | }
|
---|
605 | break;
|
---|
606 |
|
---|
607 | default:
|
---|
608 | AssertFailed();
|
---|
609 | pDstParm->type = VMMDevHGCMParmType_Invalid;
|
---|
610 | break;
|
---|
611 | }
|
---|
612 | }
|
---|
613 | }
|
---|
614 |
|
---|
615 |
|
---|
616 | /**
|
---|
617 | * Performs the call and completion wait.
|
---|
618 | *
|
---|
619 | * @returns VBox status code of this operation, not necessarily the call.
|
---|
620 | *
|
---|
621 | * @param pHGCMCall The HGCM call info.
|
---|
622 | * @param pfnAsyncCallback The async callback that will wait for the call
|
---|
623 | * to complete.
|
---|
624 | * @param pvAsyncData Argument for the callback.
|
---|
625 | * @param u32AsyncData Argument for the callback.
|
---|
626 | * @param pfLeakIt Where to return the leak it / free it,
|
---|
627 | * indicator. Cancellation fun.
|
---|
628 | */
|
---|
629 | static int vbglR0HGCMInternalDoCall(VMMDevHGCMCall *pHGCMCall, PFNVBGLHGCMCALLBACK pfnAsyncCallback,
|
---|
630 | void *pvAsyncData, uint32_t u32AsyncData, bool *pfLeakIt)
|
---|
631 | {
|
---|
632 | int rc;
|
---|
633 |
|
---|
634 | Log(("calling VbglGRPerform\n"));
|
---|
635 | rc = VbglGRPerform(&pHGCMCall->header.header);
|
---|
636 | Log(("VbglGRPerform rc = %Rrc (header rc=%d)\n", rc, pHGCMCall->header.result));
|
---|
637 |
|
---|
638 | /*
|
---|
639 | * If the call failed, but as a result of the request itself, then pretend
|
---|
640 | * success. Upper layers will interpret the result code in the packet.
|
---|
641 | */
|
---|
642 | if ( RT_FAILURE(rc)
|
---|
643 | && rc == pHGCMCall->header.result)
|
---|
644 | {
|
---|
645 | Assert(pHGCMCall->header.fu32Flags & VBOX_HGCM_REQ_DONE);
|
---|
646 | rc = VINF_SUCCESS;
|
---|
647 | }
|
---|
648 |
|
---|
649 | /*
|
---|
650 | * Check if host decides to process the request asynchronously,
|
---|
651 | * if so, we wait for it to complete using the caller supplied callback.
|
---|
652 | */
|
---|
653 | *pfLeakIt = false;
|
---|
654 | if (rc == VINF_HGCM_ASYNC_EXECUTE)
|
---|
655 | {
|
---|
656 | Log(("Processing HGCM call asynchronously\n"));
|
---|
657 | rc = pfnAsyncCallback(&pHGCMCall->header, pvAsyncData, u32AsyncData);
|
---|
658 | if (pHGCMCall->header.fu32Flags & VBOX_HGCM_REQ_DONE)
|
---|
659 | {
|
---|
660 | Assert(!(pHGCMCall->header.fu32Flags & VBOX_HGCM_REQ_CANCELLED));
|
---|
661 | rc = VINF_SUCCESS;
|
---|
662 | }
|
---|
663 | else
|
---|
664 | {
|
---|
665 | /*
|
---|
666 | * The request didn't complete in time or the call was interrupted,
|
---|
667 | * the RC from the callback indicates which. Try cancel the request.
|
---|
668 | *
|
---|
669 | * This is a bit messy because we're racing request completion. Sorry.
|
---|
670 | */
|
---|
671 | /** @todo It would be nice if we could use the waiter callback to do further
|
---|
672 | * waiting in case of a completion race. If it wasn't for WINNT having its own
|
---|
673 | * version of all that stuff, I would've done it already. */
|
---|
674 | VMMDevHGCMCancel2 *pCancelReq;
|
---|
675 | int rc2 = VbglGRAlloc((VMMDevRequestHeader **)&pCancelReq, sizeof(*pCancelReq), VMMDevReq_HGCMCancel2);
|
---|
676 | if (RT_SUCCESS(rc2))
|
---|
677 | {
|
---|
678 | pCancelReq->physReqToCancel = VbglPhysHeapGetPhysAddr(pHGCMCall);
|
---|
679 | rc2 = VbglGRPerform(&pCancelReq->header);
|
---|
680 | VbglGRFree(&pCancelReq->header);
|
---|
681 | }
|
---|
682 | #if 1 /** @todo ADDVER: Remove this on next minor version change. */
|
---|
683 | if (rc2 == VERR_NOT_IMPLEMENTED)
|
---|
684 | {
|
---|
685 | /* host is too old, or we're out of heap. */
|
---|
686 | pHGCMCall->header.fu32Flags |= VBOX_HGCM_REQ_CANCELLED;
|
---|
687 | pHGCMCall->header.header.requestType = VMMDevReq_HGCMCancel;
|
---|
688 | rc2 = VbglGRPerform(&pHGCMCall->header.header);
|
---|
689 | if (rc2 == VERR_INVALID_PARAMETER)
|
---|
690 | rc2 = VERR_NOT_FOUND;
|
---|
691 | else if (RT_SUCCESS(rc))
|
---|
692 | RTThreadSleep(1);
|
---|
693 | }
|
---|
694 | #endif
|
---|
695 | if (RT_SUCCESS(rc)) rc = VERR_INTERRUPTED; /** @todo weed this out from the WINNT VBoxGuest code. */
|
---|
696 | if (RT_SUCCESS(rc2))
|
---|
697 | {
|
---|
698 | Log(("vbglR0HGCMInternalDoCall: successfully cancelled\n"));
|
---|
699 | pHGCMCall->header.fu32Flags |= VBOX_HGCM_REQ_CANCELLED;
|
---|
700 | }
|
---|
701 | else
|
---|
702 | {
|
---|
703 | /*
|
---|
704 | * Wait for a bit while the host (hopefully) completes it.
|
---|
705 | */
|
---|
706 | uint64_t u64Start = RTTimeSystemMilliTS();
|
---|
707 | uint32_t cMilliesToWait = rc2 == VERR_NOT_FOUND || rc2 == VERR_SEM_DESTROYED ? 500 : 2000;
|
---|
708 | uint64_t cElapsed = 0;
|
---|
709 | if (rc2 != VERR_NOT_FOUND)
|
---|
710 | {
|
---|
711 | static unsigned s_cErrors = 0;
|
---|
712 | if (s_cErrors++ < 32)
|
---|
713 | LogRel(("vbglR0HGCMInternalDoCall: Failed to cancel the HGCM call on %Rrc: rc2=%Rrc\n", rc, rc2));
|
---|
714 | }
|
---|
715 | else
|
---|
716 | Log(("vbglR0HGCMInternalDoCall: Cancel race rc=%Rrc rc2=%Rrc\n", rc, rc2));
|
---|
717 |
|
---|
718 | do
|
---|
719 | {
|
---|
720 | ASMCompilerBarrier(); /* paranoia */
|
---|
721 | if (pHGCMCall->header.fu32Flags & VBOX_HGCM_REQ_DONE)
|
---|
722 | break;
|
---|
723 | RTThreadSleep(1);
|
---|
724 | cElapsed = RTTimeSystemMilliTS() - u64Start;
|
---|
725 | } while (cElapsed < cMilliesToWait);
|
---|
726 |
|
---|
727 | ASMCompilerBarrier(); /* paranoia^2 */
|
---|
728 | if (pHGCMCall->header.fu32Flags & VBOX_HGCM_REQ_DONE)
|
---|
729 | rc = VINF_SUCCESS;
|
---|
730 | else
|
---|
731 | {
|
---|
732 | LogRel(("vbglR0HGCMInternalDoCall: Leaking %u bytes. Pending call to %u with %u parms. (rc2=%Rrc)\n",
|
---|
733 | pHGCMCall->header.header.size, pHGCMCall->u32Function, pHGCMCall->cParms, rc2));
|
---|
734 | *pfLeakIt = true;
|
---|
735 | }
|
---|
736 | Log(("vbglR0HGCMInternalDoCall: Cancel race ended with rc=%Rrc (rc2=%Rrc) after %llu ms\n", rc, rc2, cElapsed));
|
---|
737 | }
|
---|
738 | }
|
---|
739 | }
|
---|
740 |
|
---|
741 | Log(("GstHGCMCall: rc=%Rrc result=%Rrc fu32Flags=%#x fLeakIt=%d\n",
|
---|
742 | rc, pHGCMCall->header.result, pHGCMCall->header.fu32Flags, *pfLeakIt));
|
---|
743 | return rc;
|
---|
744 | }
|
---|
745 |
|
---|
746 |
|
---|
747 | /**
|
---|
748 | * Copies the result of the call back to the caller info structure and user
|
---|
749 | * buffers (if using bounce buffers).
|
---|
750 | *
|
---|
751 | * @returns rc, unless RTR0MemUserCopyTo fails.
|
---|
752 | * @param pCallInfo Call info structure to update.
|
---|
753 | * @param pHGCMCall HGCM call request.
|
---|
754 | * @param pParmInfo Parameter locking/buffering info.
|
---|
755 | * @param fIsUser Is it a user (true) or kernel request.
|
---|
756 | * @param rc The current result code. Passed along to
|
---|
757 | * preserve informational status codes.
|
---|
758 | */
|
---|
759 | static int vbglR0HGCMInternalCopyBackResult(VBoxGuestHGCMCallInfo *pCallInfo, VMMDevHGCMCall const *pHGCMCall,
|
---|
760 | struct VbglR0ParmInfo *pParmInfo, bool fIsUser, int rc)
|
---|
761 | {
|
---|
762 | HGCMFunctionParameter const *pSrcParm = VMMDEV_HGCM_CALL_PARMS(pHGCMCall);
|
---|
763 | HGCMFunctionParameter *pDstParm = VBOXGUEST_HGCM_CALL_PARMS(pCallInfo);
|
---|
764 | uint32_t cParms = pCallInfo->cParms;
|
---|
765 | #ifdef USE_BOUNCE_BUFFERS
|
---|
766 | uint32_t iLockBuf = 0;
|
---|
767 | #endif
|
---|
768 | uint32_t iParm;
|
---|
769 |
|
---|
770 | /*
|
---|
771 | * The call result.
|
---|
772 | */
|
---|
773 | pCallInfo->result = pHGCMCall->header.result;
|
---|
774 |
|
---|
775 | /*
|
---|
776 | * Copy back parameters.
|
---|
777 | */
|
---|
778 | for (iParm = 0; iParm < cParms; iParm++, pSrcParm++, pDstParm++)
|
---|
779 | {
|
---|
780 | switch (pDstParm->type)
|
---|
781 | {
|
---|
782 | case VMMDevHGCMParmType_32bit:
|
---|
783 | case VMMDevHGCMParmType_64bit:
|
---|
784 | *pDstParm = *pSrcParm;
|
---|
785 | break;
|
---|
786 |
|
---|
787 | case VMMDevHGCMParmType_PageList:
|
---|
788 | pDstParm->u.PageList.size = pSrcParm->u.PageList.size;
|
---|
789 | break;
|
---|
790 |
|
---|
791 | case VMMDevHGCMParmType_LinAddr_Locked_In:
|
---|
792 | case VMMDevHGCMParmType_LinAddr_In:
|
---|
793 | #ifdef USE_BOUNCE_BUFFERS
|
---|
794 | if ( fIsUser
|
---|
795 | && iLockBuf < pParmInfo->cLockBufs
|
---|
796 | && iParm == pParmInfo->aLockBufs[iLockBuf].iParm)
|
---|
797 | iLockBuf++;
|
---|
798 | #endif
|
---|
799 | pDstParm->u.Pointer.size = pSrcParm->u.Pointer.size;
|
---|
800 | break;
|
---|
801 |
|
---|
802 | case VMMDevHGCMParmType_LinAddr_Locked_Out:
|
---|
803 | case VMMDevHGCMParmType_LinAddr_Locked:
|
---|
804 | if (!VBGLR0_CAN_USE_PHYS_PAGE_LIST(/*a_fLocked =*/ true))
|
---|
805 | {
|
---|
806 | pDstParm->u.Pointer.size = pSrcParm->u.Pointer.size;
|
---|
807 | break;
|
---|
808 | }
|
---|
809 | /* fall thru */
|
---|
810 |
|
---|
811 | case VMMDevHGCMParmType_LinAddr_Out:
|
---|
812 | case VMMDevHGCMParmType_LinAddr:
|
---|
813 | {
|
---|
814 | #ifdef USE_BOUNCE_BUFFERS
|
---|
815 | if (fIsUser)
|
---|
816 | {
|
---|
817 | size_t cbOut = RT_MIN(pSrcParm->u.Pointer.size, pDstParm->u.Pointer.size);
|
---|
818 | if (cbOut)
|
---|
819 | {
|
---|
820 | int rc2;
|
---|
821 | Assert(pParmInfo->aLockBufs[iLockBuf].iParm == iParm);
|
---|
822 | rc2 = RTR0MemUserCopyTo((RTR3PTR)pDstParm->u.Pointer.u.linearAddr,
|
---|
823 | pParmInfo->aLockBufs[iLockBuf].pvSmallBuf
|
---|
824 | ? pParmInfo->aLockBufs[iLockBuf].pvSmallBuf
|
---|
825 | : RTR0MemObjAddress(pParmInfo->aLockBufs[iLockBuf].hObj),
|
---|
826 | cbOut);
|
---|
827 | if (RT_FAILURE(rc2))
|
---|
828 | return rc2;
|
---|
829 | iLockBuf++;
|
---|
830 | }
|
---|
831 | else if ( iLockBuf < pParmInfo->cLockBufs
|
---|
832 | && iParm == pParmInfo->aLockBufs[iLockBuf].iParm)
|
---|
833 | iLockBuf++;
|
---|
834 | }
|
---|
835 | #endif
|
---|
836 | pDstParm->u.Pointer.size = pSrcParm->u.Pointer.size;
|
---|
837 | break;
|
---|
838 | }
|
---|
839 |
|
---|
840 | default:
|
---|
841 | AssertFailed();
|
---|
842 | rc = VERR_INTERNAL_ERROR_4;
|
---|
843 | break;
|
---|
844 | }
|
---|
845 | }
|
---|
846 |
|
---|
847 | #ifdef USE_BOUNCE_BUFFERS
|
---|
848 | Assert(!fIsUser || pParmInfo->cLockBufs == iLockBuf);
|
---|
849 | #endif
|
---|
850 | return rc;
|
---|
851 | }
|
---|
852 |
|
---|
853 |
|
---|
854 | DECLR0VBGL(int) VbglR0HGCMInternalCall(VBoxGuestHGCMCallInfo *pCallInfo, uint32_t cbCallInfo, uint32_t fFlags,
|
---|
855 | PFNVBGLHGCMCALLBACK pfnAsyncCallback, void *pvAsyncData, uint32_t u32AsyncData)
|
---|
856 | {
|
---|
857 | bool fIsUser = (fFlags & VBGLR0_HGCMCALL_F_MODE_MASK) == VBGLR0_HGCMCALL_F_USER;
|
---|
858 | struct VbglR0ParmInfo ParmInfo;
|
---|
859 | size_t cbExtra;
|
---|
860 | int rc;
|
---|
861 |
|
---|
862 | /*
|
---|
863 | * Basic validation.
|
---|
864 | */
|
---|
865 | AssertMsgReturn( !pCallInfo
|
---|
866 | || !pfnAsyncCallback
|
---|
867 | || pCallInfo->cParms > VBOX_HGCM_MAX_PARMS
|
---|
868 | || !(fFlags & ~VBGLR0_HGCMCALL_F_MODE_MASK),
|
---|
869 | ("pCallInfo=%p pfnAsyncCallback=%p fFlags=%#x\n", pCallInfo, pfnAsyncCallback, fFlags),
|
---|
870 | VERR_INVALID_PARAMETER);
|
---|
871 | AssertReturn( cbCallInfo >= sizeof(VBoxGuestHGCMCallInfo)
|
---|
872 | || cbCallInfo >= pCallInfo->cParms * sizeof(HGCMFunctionParameter),
|
---|
873 | VERR_INVALID_PARAMETER);
|
---|
874 |
|
---|
875 | Log(("GstHGCMCall: u32ClientID=%#x u32Function=%u cParms=%u cbCallInfo=%#x fFlags=%#x\n",
|
---|
876 | pCallInfo->u32ClientID, pCallInfo->u32ClientID, pCallInfo->u32Function, pCallInfo->cParms, cbCallInfo, fFlags));
|
---|
877 |
|
---|
878 | /*
|
---|
879 | * Validate, lock and buffer the parameters for the call.
|
---|
880 | * This will calculate the amount of extra space for physical page list.
|
---|
881 | */
|
---|
882 | rc = vbglR0HGCMInternalPreprocessCall(pCallInfo, cbCallInfo, fIsUser, &ParmInfo, &cbExtra);
|
---|
883 | if (RT_SUCCESS(rc))
|
---|
884 | {
|
---|
885 | /*
|
---|
886 | * Allocate the request buffer and recreate the call request.
|
---|
887 | */
|
---|
888 | VMMDevHGCMCall *pHGCMCall;
|
---|
889 | rc = VbglGRAlloc((VMMDevRequestHeader **)&pHGCMCall,
|
---|
890 | sizeof(VMMDevHGCMCall) + pCallInfo->cParms * sizeof(HGCMFunctionParameter) + cbExtra,
|
---|
891 | VMMDevReq_HGCMCall);
|
---|
892 | if (RT_SUCCESS(rc))
|
---|
893 | {
|
---|
894 | bool fLeakIt;
|
---|
895 | vbglR0HGCMInternalInitCall(pHGCMCall, pCallInfo, cbCallInfo, fIsUser, &ParmInfo);
|
---|
896 |
|
---|
897 | /*
|
---|
898 | * Perform the call.
|
---|
899 | */
|
---|
900 | rc = vbglR0HGCMInternalDoCall(pHGCMCall, pfnAsyncCallback, pvAsyncData, u32AsyncData, &fLeakIt);
|
---|
901 | if (RT_SUCCESS(rc))
|
---|
902 | {
|
---|
903 | /*
|
---|
904 | * Copy back the result (parameters and buffers that changed).
|
---|
905 | */
|
---|
906 | rc = vbglR0HGCMInternalCopyBackResult(pCallInfo, pHGCMCall, &ParmInfo, fIsUser, rc);
|
---|
907 | }
|
---|
908 | else
|
---|
909 | {
|
---|
910 | if ( rc != VERR_INTERRUPTED
|
---|
911 | && rc != VERR_TIMEOUT)
|
---|
912 | {
|
---|
913 | static unsigned s_cErrors = 0;
|
---|
914 | if (s_cErrors++ < 32)
|
---|
915 | LogRel(("VbglR0HGCMInternalCall: vbglR0HGCMInternalDoCall failed. rc=%Rrc\n", rc));
|
---|
916 | }
|
---|
917 | }
|
---|
918 |
|
---|
919 | if (!fLeakIt)
|
---|
920 | VbglGRFree(&pHGCMCall->header.header);
|
---|
921 | }
|
---|
922 | }
|
---|
923 | else
|
---|
924 | LogRel(("VbglR0HGCMInternalCall: vbglR0HGCMInternalPreprocessCall failed. rc=%Rrc\n", rc));
|
---|
925 |
|
---|
926 | /*
|
---|
927 | * Release locks and free bounce buffers.
|
---|
928 | */
|
---|
929 | if (ParmInfo.cLockBufs)
|
---|
930 | while (ParmInfo.cLockBufs-- > 0)
|
---|
931 | {
|
---|
932 | RTR0MemObjFree(ParmInfo.aLockBufs[ParmInfo.cLockBufs].hObj, false /*fFreeMappings*/);
|
---|
933 | #ifdef USE_BOUNCE_BUFFERS
|
---|
934 | RTMemTmpFree(ParmInfo.aLockBufs[ParmInfo.cLockBufs].pvSmallBuf);
|
---|
935 | #endif
|
---|
936 | }
|
---|
937 |
|
---|
938 | return rc;
|
---|
939 | }
|
---|
940 |
|
---|
941 |
|
---|
942 | #if ARCH_BITS == 64
|
---|
943 | DECLR0VBGL(int) VbglR0HGCMInternalCall32(VBoxGuestHGCMCallInfo *pCallInfo, uint32_t cbCallInfo, uint32_t fFlags,
|
---|
944 | PFNVBGLHGCMCALLBACK pfnAsyncCallback, void *pvAsyncData, uint32_t u32AsyncData)
|
---|
945 | {
|
---|
946 | VBoxGuestHGCMCallInfo *pCallInfo64 = NULL;
|
---|
947 | HGCMFunctionParameter *pParm64 = NULL;
|
---|
948 | HGCMFunctionParameter32 *pParm32 = NULL;
|
---|
949 | uint32_t cParms = 0;
|
---|
950 | uint32_t iParm = 0;
|
---|
951 | int rc = VINF_SUCCESS;
|
---|
952 |
|
---|
953 | /*
|
---|
954 | * Input validation.
|
---|
955 | */
|
---|
956 | AssertMsgReturn( !pCallInfo
|
---|
957 | || !pfnAsyncCallback
|
---|
958 | || pCallInfo->cParms > VBOX_HGCM_MAX_PARMS
|
---|
959 | || !(fFlags & ~VBGLR0_HGCMCALL_F_MODE_MASK),
|
---|
960 | ("pCallInfo=%p pfnAsyncCallback=%p fFlags=%#x\n", pCallInfo, pfnAsyncCallback, fFlags),
|
---|
961 | VERR_INVALID_PARAMETER);
|
---|
962 | AssertReturn( cbCallInfo >= sizeof(VBoxGuestHGCMCallInfo)
|
---|
963 | || cbCallInfo >= pCallInfo->cParms * sizeof(HGCMFunctionParameter32),
|
---|
964 | VERR_INVALID_PARAMETER);
|
---|
965 |
|
---|
966 | /* This Assert does not work on Solaris/Windows 64/32 mixed mode, not sure why, skipping for now */
|
---|
967 | #if !defined(RT_OS_SOLARIS) && !defined(RT_OS_WINDOWS)
|
---|
968 | AssertReturn((fFlags & VBGLR0_HGCMCALL_F_MODE_MASK) == VBGLR0_HGCMCALL_F_KERNEL, VERR_WRONG_ORDER);
|
---|
969 | #endif
|
---|
970 |
|
---|
971 | cParms = pCallInfo->cParms;
|
---|
972 | Log(("VbglR0HGCMInternalCall32: cParms=%d, u32Function=%d, fFlags=%#x\n", cParms, pCallInfo->u32Function, fFlags));
|
---|
973 |
|
---|
974 | /*
|
---|
975 | * The simple approach, allocate a temporary request and convert the parameters.
|
---|
976 | */
|
---|
977 | pCallInfo64 = (VBoxGuestHGCMCallInfo *)RTMemTmpAllocZ(sizeof(*pCallInfo64) + cParms * sizeof(HGCMFunctionParameter));
|
---|
978 | if (!pCallInfo64)
|
---|
979 | return VERR_NO_TMP_MEMORY;
|
---|
980 |
|
---|
981 | *pCallInfo64 = *pCallInfo;
|
---|
982 | pParm32 = VBOXGUEST_HGCM_CALL_PARMS32(pCallInfo);
|
---|
983 | pParm64 = VBOXGUEST_HGCM_CALL_PARMS(pCallInfo64);
|
---|
984 | for (iParm = 0; iParm < cParms; iParm++, pParm32++, pParm64++)
|
---|
985 | {
|
---|
986 | switch (pParm32->type)
|
---|
987 | {
|
---|
988 | case VMMDevHGCMParmType_32bit:
|
---|
989 | pParm64->type = VMMDevHGCMParmType_32bit;
|
---|
990 | pParm64->u.value32 = pParm32->u.value32;
|
---|
991 | break;
|
---|
992 |
|
---|
993 | case VMMDevHGCMParmType_64bit:
|
---|
994 | pParm64->type = VMMDevHGCMParmType_64bit;
|
---|
995 | pParm64->u.value64 = pParm32->u.value64;
|
---|
996 | break;
|
---|
997 |
|
---|
998 | case VMMDevHGCMParmType_LinAddr_Out:
|
---|
999 | case VMMDevHGCMParmType_LinAddr:
|
---|
1000 | case VMMDevHGCMParmType_LinAddr_In:
|
---|
1001 | pParm64->type = pParm32->type;
|
---|
1002 | pParm64->u.Pointer.size = pParm32->u.Pointer.size;
|
---|
1003 | pParm64->u.Pointer.u.linearAddr = pParm32->u.Pointer.u.linearAddr;
|
---|
1004 | break;
|
---|
1005 |
|
---|
1006 | default:
|
---|
1007 | rc = VERR_INVALID_PARAMETER;
|
---|
1008 | LogRel(("VbglR0HGCMInternalCall32: pParm32 type %#x invalid.\n", pParm32->type));
|
---|
1009 | break;
|
---|
1010 | }
|
---|
1011 | if (RT_FAILURE(rc))
|
---|
1012 | break;
|
---|
1013 | }
|
---|
1014 | if (RT_SUCCESS(rc))
|
---|
1015 | {
|
---|
1016 | rc = VbglR0HGCMInternalCall(pCallInfo64, sizeof(*pCallInfo64) + cParms * sizeof(HGCMFunctionParameter), fFlags,
|
---|
1017 | pfnAsyncCallback, pvAsyncData, u32AsyncData);
|
---|
1018 |
|
---|
1019 | if (RT_SUCCESS(rc))
|
---|
1020 | {
|
---|
1021 | *pCallInfo = *pCallInfo64;
|
---|
1022 |
|
---|
1023 | /*
|
---|
1024 | * Copy back.
|
---|
1025 | */
|
---|
1026 | pParm32 = VBOXGUEST_HGCM_CALL_PARMS32(pCallInfo);
|
---|
1027 | pParm64 = VBOXGUEST_HGCM_CALL_PARMS(pCallInfo64);
|
---|
1028 | for (iParm = 0; iParm < cParms; iParm++, pParm32++, pParm64++)
|
---|
1029 | {
|
---|
1030 | switch (pParm64->type)
|
---|
1031 | {
|
---|
1032 | case VMMDevHGCMParmType_32bit:
|
---|
1033 | pParm32->u.value32 = pParm64->u.value32;
|
---|
1034 | break;
|
---|
1035 |
|
---|
1036 | case VMMDevHGCMParmType_64bit:
|
---|
1037 | pParm32->u.value64 = pParm64->u.value64;
|
---|
1038 | break;
|
---|
1039 |
|
---|
1040 | case VMMDevHGCMParmType_LinAddr_Out:
|
---|
1041 | case VMMDevHGCMParmType_LinAddr:
|
---|
1042 | case VMMDevHGCMParmType_LinAddr_In:
|
---|
1043 | pParm32->u.Pointer.size = pParm64->u.Pointer.size;
|
---|
1044 | break;
|
---|
1045 |
|
---|
1046 | default:
|
---|
1047 | LogRel(("VbglR0HGCMInternalCall32: failed invalid pParm32 type %d\n", pParm32->type));
|
---|
1048 | rc = VERR_INTERNAL_ERROR_3;
|
---|
1049 | break;
|
---|
1050 | }
|
---|
1051 | }
|
---|
1052 | }
|
---|
1053 | else
|
---|
1054 | {
|
---|
1055 | static unsigned s_cErrors = 0;
|
---|
1056 | if (s_cErrors++ < 32)
|
---|
1057 | LogRel(("VbglR0HGCMInternalCall32: VbglR0HGCMInternalCall failed. rc=%Rrc\n", rc));
|
---|
1058 | }
|
---|
1059 | }
|
---|
1060 | else
|
---|
1061 | {
|
---|
1062 | static unsigned s_cErrors = 0;
|
---|
1063 | if (s_cErrors++ < 32)
|
---|
1064 | LogRel(("VbglR0HGCMInternalCall32: failed. rc=%Rrc\n", rc));
|
---|
1065 | }
|
---|
1066 |
|
---|
1067 | RTMemTmpFree(pCallInfo64);
|
---|
1068 | return rc;
|
---|
1069 | }
|
---|
1070 | #endif /* ARCH_BITS == 64 */
|
---|
1071 |
|
---|
1072 | #endif /* VBGL_VBOXGUEST */
|
---|
1073 |
|
---|