VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuestLib/HGCMInternal.cpp@ 21510

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

page list bufix.

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