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