VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/VBoxGuest.cpp@ 76419

Last change on this file since 76419 was 76419, checked in by vboxsync, 6 years ago

VBox/VMMDev.h: Replaced VBox/err.h with iprt/errcore.h. bugref:9344

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 163.2 KB
Line 
1/* $Id: VBoxGuest.cpp 76419 2018-12-23 19:43:43Z vboxsync $ */
2/** @file
3 * VBoxGuest - Guest Additions Driver, Common Code.
4 */
5
6/*
7 * Copyright (C) 2007-2017 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/** @page pg_vbdrv VBoxGuest
28 *
29 * VBoxGuest is the device driver for VMMDev.
30 *
31 * The device driver is shipped as part of the guest additions. It has roots in
32 * the host VMM support driver (usually known as VBoxDrv), so fixes in platform
33 * specific code may apply to both drivers.
34 *
35 * The common code lives in VBoxGuest.cpp and is compiled both as C++ and C.
36 * The VBoxGuest.cpp source file shall not contain platform specific code,
37 * though it must occationally do a few \#ifdef RT_OS_XXX tests to cater for
38 * platform differences. Though, in those cases, it is common that more than
39 * one platform needs special handling.
40 *
41 * On most platforms the device driver should create two device nodes, one for
42 * full (unrestricted) access to the feature set, and one which only provides a
43 * restrict set of functions. These are generally referred to as 'vboxguest'
44 * and 'vboxuser' respectively. Currently, this two device approach is only
45 * implemented on Linux!
46 *
47 */
48
49
50/*********************************************************************************************************************************
51* Header Files *
52*********************************************************************************************************************************/
53#define LOG_GROUP LOG_GROUP_DEFAULT
54#include "VBoxGuestInternal.h"
55#include <VBox/VMMDev.h> /* for VMMDEV_RAM_SIZE */
56#include <VBox/err.h>
57#include <VBox/log.h>
58#include <VBox/HostServices/GuestPropertySvc.h>
59#include <iprt/ctype.h>
60#include <iprt/mem.h>
61#include <iprt/time.h>
62#include <iprt/memobj.h>
63#include <iprt/asm.h>
64#include <iprt/asm-amd64-x86.h>
65#include <iprt/string.h>
66#include <iprt/process.h>
67#include <iprt/assert.h>
68#include <iprt/param.h>
69#include <iprt/timer.h>
70#ifdef VBOX_WITH_HGCM
71# include <iprt/thread.h>
72#endif
73#include "version-generated.h"
74#if defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)
75# include "revision-generated.h"
76#endif
77#if defined(RT_OS_SOLARIS) || defined(RT_OS_DARWIN)
78# include <iprt/rand.h>
79#endif
80
81
82/*********************************************************************************************************************************
83* Defined Constants And Macros *
84*********************************************************************************************************************************/
85#define VBOXGUEST_ACQUIRE_STYLE_EVENTS (VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST | VMMDEV_EVENT_SEAMLESS_MODE_CHANGE_REQUEST)
86
87
88/*********************************************************************************************************************************
89* Internal Functions *
90*********************************************************************************************************************************/
91#ifdef VBOX_WITH_HGCM
92static DECLCALLBACK(int) vgdrvHgcmAsyncWaitCallback(VMMDevHGCMRequestHeader *pHdrNonVolatile, void *pvUser, uint32_t u32User);
93#endif
94static int vgdrvIoCtl_CancelAllWaitEvents(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession);
95static void vgdrvBitUsageTrackerClear(PVBOXGUESTBITUSAGETRACER pTracker);
96static uint32_t vgdrvGetAllowedEventMaskForSession(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession);
97static int vgdrvResetEventFilterOnHost(PVBOXGUESTDEVEXT pDevExt, uint32_t fFixedEvents);
98static int vgdrvResetMouseStatusOnHost(PVBOXGUESTDEVEXT pDevExt);
99static int vgdrvResetCapabilitiesOnHost(PVBOXGUESTDEVEXT pDevExt);
100static int vgdrvSetSessionEventFilter(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession,
101 uint32_t fOrMask, uint32_t fNotMask, bool fSessionTermination);
102static int vgdrvSetSessionMouseStatus(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession,
103 uint32_t fOrMask, uint32_t fNotMask, bool fSessionTermination);
104static int vgdrvSetSessionCapabilities(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession,
105 uint32_t fOrMask, uint32_t fNoMask,
106 uint32_t *pfSessionCaps, uint32_t *pfGlobalCaps, bool fSessionTermination);
107static int vgdrvAcquireSessionCapabilities(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession,
108 uint32_t fOrMask, uint32_t fNotMask, uint32_t fFlags, bool fSessionTermination);
109static int vgdrvDispatchEventsLocked(PVBOXGUESTDEVEXT pDevExt, uint32_t fEvents);
110
111
112/*********************************************************************************************************************************
113* Global Variables *
114*********************************************************************************************************************************/
115static const uint32_t g_cbChangeMemBalloonReq = RT_UOFFSETOF(VMMDevChangeMemBalloon, aPhysPage[VMMDEV_MEMORY_BALLOON_CHUNK_PAGES]);
116
117#if defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS)
118/**
119 * Drag in the rest of IRPT since we share it with the
120 * rest of the kernel modules on Solaris.
121 */
122PFNRT g_apfnVBoxGuestIPRTDeps[] =
123{
124 /* VirtioNet */
125 (PFNRT)RTRandBytes,
126 /* RTSemMutex* */
127 (PFNRT)RTSemMutexCreate,
128 (PFNRT)RTSemMutexDestroy,
129 (PFNRT)RTSemMutexRequest,
130 (PFNRT)RTSemMutexRequestNoResume,
131 (PFNRT)RTSemMutexRequestDebug,
132 (PFNRT)RTSemMutexRequestNoResumeDebug,
133 (PFNRT)RTSemMutexRelease,
134 (PFNRT)RTSemMutexIsOwned,
135 NULL
136};
137#endif /* RT_OS_DARWIN || RT_OS_SOLARIS */
138
139
140/**
141 * Reserves memory in which the VMM can relocate any guest mappings
142 * that are floating around.
143 *
144 * This operation is a little bit tricky since the VMM might not accept
145 * just any address because of address clashes between the three contexts
146 * it operates in, so use a small stack to perform this operation.
147 *
148 * @returns VBox status code (ignored).
149 * @param pDevExt The device extension.
150 */
151static int vgdrvInitFixateGuestMappings(PVBOXGUESTDEVEXT pDevExt)
152{
153 /*
154 * Query the required space.
155 */
156 VMMDevReqHypervisorInfo *pReq;
157 int rc = VbglR0GRAlloc((VMMDevRequestHeader **)&pReq, sizeof(VMMDevReqHypervisorInfo), VMMDevReq_GetHypervisorInfo);
158 if (RT_FAILURE(rc))
159 return rc;
160 pReq->hypervisorStart = 0;
161 pReq->hypervisorSize = 0;
162 rc = VbglR0GRPerform(&pReq->header);
163 if (RT_FAILURE(rc)) /* this shouldn't happen! */
164 {
165 VbglR0GRFree(&pReq->header);
166 return rc;
167 }
168
169 /*
170 * The VMM will report back if there is nothing it wants to map, like for
171 * instance in VT-x and AMD-V mode.
172 */
173 if (pReq->hypervisorSize == 0)
174 Log(("vgdrvInitFixateGuestMappings: nothing to do\n"));
175 else
176 {
177 /*
178 * We have to try several times since the host can be picky
179 * about certain addresses.
180 */
181 RTR0MEMOBJ hFictive = NIL_RTR0MEMOBJ;
182 uint32_t cbHypervisor = pReq->hypervisorSize;
183 RTR0MEMOBJ ahTries[5];
184 uint32_t iTry;
185 bool fBitched = false;
186 Log(("vgdrvInitFixateGuestMappings: cbHypervisor=%#x\n", cbHypervisor));
187 for (iTry = 0; iTry < RT_ELEMENTS(ahTries); iTry++)
188 {
189 /*
190 * Reserve space, or if that isn't supported, create a object for
191 * some fictive physical memory and map that in to kernel space.
192 *
193 * To make the code a bit uglier, most systems cannot help with
194 * 4MB alignment, so we have to deal with that in addition to
195 * having two ways of getting the memory.
196 */
197 uint32_t uAlignment = _4M;
198 RTR0MEMOBJ hObj;
199 rc = RTR0MemObjReserveKernel(&hObj, (void *)-1, RT_ALIGN_32(cbHypervisor, _4M), uAlignment);
200 if (rc == VERR_NOT_SUPPORTED)
201 {
202 uAlignment = PAGE_SIZE;
203 rc = RTR0MemObjReserveKernel(&hObj, (void *)-1, RT_ALIGN_32(cbHypervisor, _4M) + _4M, uAlignment);
204 }
205 /*
206 * If both RTR0MemObjReserveKernel calls above failed because either not supported or
207 * not implemented at all at the current platform, try to map the memory object into the
208 * virtual kernel space.
209 */
210 if (rc == VERR_NOT_SUPPORTED)
211 {
212 if (hFictive == NIL_RTR0MEMOBJ)
213 {
214 rc = RTR0MemObjEnterPhys(&hObj, VBOXGUEST_HYPERVISOR_PHYSICAL_START, cbHypervisor + _4M, RTMEM_CACHE_POLICY_DONT_CARE);
215 if (RT_FAILURE(rc))
216 break;
217 hFictive = hObj;
218 }
219 uAlignment = _4M;
220 rc = RTR0MemObjMapKernel(&hObj, hFictive, (void *)-1, uAlignment, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
221 if (rc == VERR_NOT_SUPPORTED)
222 {
223 uAlignment = PAGE_SIZE;
224 rc = RTR0MemObjMapKernel(&hObj, hFictive, (void *)-1, uAlignment, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
225 }
226 }
227 if (RT_FAILURE(rc))
228 {
229 LogRel(("VBoxGuest: Failed to reserve memory for the hypervisor: rc=%Rrc (cbHypervisor=%#x uAlignment=%#x iTry=%u)\n",
230 rc, cbHypervisor, uAlignment, iTry));
231 fBitched = true;
232 break;
233 }
234
235 /*
236 * Try set it.
237 */
238 pReq->header.requestType = VMMDevReq_SetHypervisorInfo;
239 pReq->header.rc = VERR_INTERNAL_ERROR;
240 pReq->hypervisorSize = cbHypervisor;
241 pReq->hypervisorStart = (RTGCPTR32)(uintptr_t)RTR0MemObjAddress(hObj);
242 if ( uAlignment == PAGE_SIZE
243 && pReq->hypervisorStart & (_4M - 1))
244 pReq->hypervisorStart = RT_ALIGN_32(pReq->hypervisorStart, _4M);
245 AssertMsg(RT_ALIGN_32(pReq->hypervisorStart, _4M) == pReq->hypervisorStart, ("%#x\n", pReq->hypervisorStart));
246
247 rc = VbglR0GRPerform(&pReq->header);
248 if (RT_SUCCESS(rc))
249 {
250 pDevExt->hGuestMappings = hFictive != NIL_RTR0MEMOBJ ? hFictive : hObj;
251 Log(("VBoxGuest: %p LB %#x; uAlignment=%#x iTry=%u hGuestMappings=%p (%s)\n",
252 RTR0MemObjAddress(pDevExt->hGuestMappings),
253 RTR0MemObjSize(pDevExt->hGuestMappings),
254 uAlignment, iTry, pDevExt->hGuestMappings, hFictive != NIL_RTR0PTR ? "fictive" : "reservation"));
255 break;
256 }
257 ahTries[iTry] = hObj;
258 }
259
260 /*
261 * Cleanup failed attempts.
262 */
263 while (iTry-- > 0)
264 RTR0MemObjFree(ahTries[iTry], false /* fFreeMappings */);
265 if ( RT_FAILURE(rc)
266 && hFictive != NIL_RTR0PTR)
267 RTR0MemObjFree(hFictive, false /* fFreeMappings */);
268 if (RT_FAILURE(rc) && !fBitched)
269 LogRel(("VBoxGuest: Warning: failed to reserve %#d of memory for guest mappings.\n", cbHypervisor));
270 }
271 VbglR0GRFree(&pReq->header);
272
273 /*
274 * We ignore failed attempts for now.
275 */
276 return VINF_SUCCESS;
277}
278
279
280/**
281 * Undo what vgdrvInitFixateGuestMappings did.
282 *
283 * @param pDevExt The device extension.
284 */
285static void vgdrvTermUnfixGuestMappings(PVBOXGUESTDEVEXT pDevExt)
286{
287 if (pDevExt->hGuestMappings != NIL_RTR0PTR)
288 {
289 /*
290 * Tell the host that we're going to free the memory we reserved for
291 * it, the free it up. (Leak the memory if anything goes wrong here.)
292 */
293 VMMDevReqHypervisorInfo *pReq;
294 int rc = VbglR0GRAlloc((VMMDevRequestHeader **)&pReq, sizeof(VMMDevReqHypervisorInfo), VMMDevReq_SetHypervisorInfo);
295 if (RT_SUCCESS(rc))
296 {
297 pReq->hypervisorStart = 0;
298 pReq->hypervisorSize = 0;
299 rc = VbglR0GRPerform(&pReq->header);
300 VbglR0GRFree(&pReq->header);
301 }
302 if (RT_SUCCESS(rc))
303 {
304 rc = RTR0MemObjFree(pDevExt->hGuestMappings, true /* fFreeMappings */);
305 AssertRC(rc);
306 }
307 else
308 LogRel(("vgdrvTermUnfixGuestMappings: Failed to unfix the guest mappings! rc=%Rrc\n", rc));
309
310 pDevExt->hGuestMappings = NIL_RTR0MEMOBJ;
311 }
312}
313
314
315
316/**
317 * Report the guest information to the host.
318 *
319 * @returns IPRT status code.
320 * @param enmOSType The OS type to report.
321 */
322static int vgdrvReportGuestInfo(VBOXOSTYPE enmOSType)
323{
324 /*
325 * Allocate and fill in the two guest info reports.
326 */
327 VMMDevReportGuestInfo2 *pReqInfo2 = NULL;
328 VMMDevReportGuestInfo *pReqInfo1 = NULL;
329 int rc = VbglR0GRAlloc((VMMDevRequestHeader **)&pReqInfo2, sizeof (VMMDevReportGuestInfo2), VMMDevReq_ReportGuestInfo2);
330 Log(("vgdrvReportGuestInfo: VbglR0GRAlloc VMMDevReportGuestInfo2 completed with rc=%Rrc\n", rc));
331 if (RT_SUCCESS(rc))
332 {
333 pReqInfo2->guestInfo.additionsMajor = VBOX_VERSION_MAJOR;
334 pReqInfo2->guestInfo.additionsMinor = VBOX_VERSION_MINOR;
335 pReqInfo2->guestInfo.additionsBuild = VBOX_VERSION_BUILD;
336 pReqInfo2->guestInfo.additionsRevision = VBOX_SVN_REV;
337 pReqInfo2->guestInfo.additionsFeatures = VBOXGSTINFO2_F_REQUESTOR_INFO;
338 RTStrCopy(pReqInfo2->guestInfo.szName, sizeof(pReqInfo2->guestInfo.szName), VBOX_VERSION_STRING);
339
340 rc = VbglR0GRAlloc((VMMDevRequestHeader **)&pReqInfo1, sizeof (VMMDevReportGuestInfo), VMMDevReq_ReportGuestInfo);
341 Log(("vgdrvReportGuestInfo: VbglR0GRAlloc VMMDevReportGuestInfo completed with rc=%Rrc\n", rc));
342 if (RT_SUCCESS(rc))
343 {
344 pReqInfo1->guestInfo.interfaceVersion = VMMDEV_VERSION;
345 pReqInfo1->guestInfo.osType = enmOSType;
346
347 /*
348 * There are two protocols here:
349 * 1. Info2 + Info1. Supported by >=3.2.51.
350 * 2. Info1 and optionally Info2. The old protocol.
351 *
352 * We try protocol 1 first. It will fail with VERR_NOT_SUPPORTED
353 * if not supported by the VMMDev (message ordering requirement).
354 */
355 rc = VbglR0GRPerform(&pReqInfo2->header);
356 Log(("vgdrvReportGuestInfo: VbglR0GRPerform VMMDevReportGuestInfo2 completed with rc=%Rrc\n", rc));
357 if (RT_SUCCESS(rc))
358 {
359 rc = VbglR0GRPerform(&pReqInfo1->header);
360 Log(("vgdrvReportGuestInfo: VbglR0GRPerform VMMDevReportGuestInfo completed with rc=%Rrc\n", rc));
361 }
362 else if ( rc == VERR_NOT_SUPPORTED
363 || rc == VERR_NOT_IMPLEMENTED)
364 {
365 rc = VbglR0GRPerform(&pReqInfo1->header);
366 Log(("vgdrvReportGuestInfo: VbglR0GRPerform VMMDevReportGuestInfo completed with rc=%Rrc\n", rc));
367 if (RT_SUCCESS(rc))
368 {
369 rc = VbglR0GRPerform(&pReqInfo2->header);
370 Log(("vgdrvReportGuestInfo: VbglR0GRPerform VMMDevReportGuestInfo2 completed with rc=%Rrc\n", rc));
371 if (rc == VERR_NOT_IMPLEMENTED)
372 rc = VINF_SUCCESS;
373 }
374 }
375 VbglR0GRFree(&pReqInfo1->header);
376 }
377 VbglR0GRFree(&pReqInfo2->header);
378 }
379
380 return rc;
381}
382
383
384/**
385 * Report the guest driver status to the host.
386 *
387 * @returns IPRT status code.
388 * @param fActive Flag whether the driver is now active or not.
389 */
390static int vgdrvReportDriverStatus(bool fActive)
391{
392 /*
393 * Report guest status of the VBox driver to the host.
394 */
395 VMMDevReportGuestStatus *pReq2 = NULL;
396 int rc = VbglR0GRAlloc((VMMDevRequestHeader **)&pReq2, sizeof(*pReq2), VMMDevReq_ReportGuestStatus);
397 Log(("vgdrvReportDriverStatus: VbglR0GRAlloc VMMDevReportGuestStatus completed with rc=%Rrc\n", rc));
398 if (RT_SUCCESS(rc))
399 {
400 pReq2->guestStatus.facility = VBoxGuestFacilityType_VBoxGuestDriver;
401 pReq2->guestStatus.status = fActive ?
402 VBoxGuestFacilityStatus_Active
403 : VBoxGuestFacilityStatus_Inactive;
404 pReq2->guestStatus.flags = 0;
405 rc = VbglR0GRPerform(&pReq2->header);
406 Log(("vgdrvReportDriverStatus: VbglR0GRPerform VMMDevReportGuestStatus completed with fActive=%d, rc=%Rrc\n",
407 fActive ? 1 : 0, rc));
408 if (rc == VERR_NOT_IMPLEMENTED) /* Compatibility with older hosts. */
409 rc = VINF_SUCCESS;
410 VbglR0GRFree(&pReq2->header);
411 }
412
413 return rc;
414}
415
416
417/** @name Memory Ballooning
418 * @{
419 */
420
421/**
422 * Inflate the balloon by one chunk represented by an R0 memory object.
423 *
424 * The caller owns the balloon mutex.
425 *
426 * @returns IPRT status code.
427 * @param pMemObj Pointer to the R0 memory object.
428 * @param pReq The pre-allocated request for performing the VMMDev call.
429 */
430static int vgdrvBalloonInflate(PRTR0MEMOBJ pMemObj, VMMDevChangeMemBalloon *pReq)
431{
432 uint32_t iPage;
433 int rc;
434
435 for (iPage = 0; iPage < VMMDEV_MEMORY_BALLOON_CHUNK_PAGES; iPage++)
436 {
437 RTHCPHYS phys = RTR0MemObjGetPagePhysAddr(*pMemObj, iPage);
438 pReq->aPhysPage[iPage] = phys;
439 }
440
441 pReq->fInflate = true;
442 pReq->header.size = g_cbChangeMemBalloonReq;
443 pReq->cPages = VMMDEV_MEMORY_BALLOON_CHUNK_PAGES;
444
445 rc = VbglR0GRPerform(&pReq->header);
446 if (RT_FAILURE(rc))
447 LogRel(("vgdrvBalloonInflate: VbglR0GRPerform failed. rc=%Rrc\n", rc));
448 return rc;
449}
450
451
452/**
453 * Deflate the balloon by one chunk - info the host and free the memory object.
454 *
455 * The caller owns the balloon mutex.
456 *
457 * @returns IPRT status code.
458 * @param pMemObj Pointer to the R0 memory object.
459 * The memory object will be freed afterwards.
460 * @param pReq The pre-allocated request for performing the VMMDev call.
461 */
462static int vgdrvBalloonDeflate(PRTR0MEMOBJ pMemObj, VMMDevChangeMemBalloon *pReq)
463{
464 uint32_t iPage;
465 int rc;
466
467 for (iPage = 0; iPage < VMMDEV_MEMORY_BALLOON_CHUNK_PAGES; iPage++)
468 {
469 RTHCPHYS phys = RTR0MemObjGetPagePhysAddr(*pMemObj, iPage);
470 pReq->aPhysPage[iPage] = phys;
471 }
472
473 pReq->fInflate = false;
474 pReq->header.size = g_cbChangeMemBalloonReq;
475 pReq->cPages = VMMDEV_MEMORY_BALLOON_CHUNK_PAGES;
476
477 rc = VbglR0GRPerform(&pReq->header);
478 if (RT_FAILURE(rc))
479 {
480 LogRel(("vgdrvBalloonDeflate: VbglR0GRPerform failed. rc=%Rrc\n", rc));
481 return rc;
482 }
483
484 rc = RTR0MemObjFree(*pMemObj, true);
485 if (RT_FAILURE(rc))
486 {
487 LogRel(("vgdrvBalloonDeflate: RTR0MemObjFree(%p,true) -> %Rrc; this is *BAD*!\n", *pMemObj, rc));
488 return rc;
489 }
490
491 *pMemObj = NIL_RTR0MEMOBJ;
492 return VINF_SUCCESS;
493}
494
495
496/**
497 * Inflate/deflate the memory balloon and notify the host.
498 *
499 * This is a worker used by vgdrvIoCtl_CheckMemoryBalloon - it takes the mutex.
500 *
501 * @returns VBox status code.
502 * @param pDevExt The device extension.
503 * @param cBalloonChunks The new size of the balloon in chunks of 1MB.
504 * @param pfHandleInR3 Where to return the handle-in-ring3 indicator
505 * (VINF_SUCCESS if set).
506 */
507static int vgdrvSetBalloonSizeKernel(PVBOXGUESTDEVEXT pDevExt, uint32_t cBalloonChunks, bool *pfHandleInR3)
508{
509 int rc = VINF_SUCCESS;
510
511 if (pDevExt->MemBalloon.fUseKernelAPI)
512 {
513 VMMDevChangeMemBalloon *pReq;
514 uint32_t i;
515
516 if (cBalloonChunks > pDevExt->MemBalloon.cMaxChunks)
517 {
518 LogRel(("vgdrvSetBalloonSizeKernel: illegal balloon size %u (max=%u)\n",
519 cBalloonChunks, pDevExt->MemBalloon.cMaxChunks));
520 return VERR_INVALID_PARAMETER;
521 }
522
523 if (cBalloonChunks == pDevExt->MemBalloon.cMaxChunks)
524 return VINF_SUCCESS; /* nothing to do */
525
526 if ( cBalloonChunks > pDevExt->MemBalloon.cChunks
527 && !pDevExt->MemBalloon.paMemObj)
528 {
529 pDevExt->MemBalloon.paMemObj = (PRTR0MEMOBJ)RTMemAllocZ(sizeof(RTR0MEMOBJ) * pDevExt->MemBalloon.cMaxChunks);
530 if (!pDevExt->MemBalloon.paMemObj)
531 {
532 LogRel(("vgdrvSetBalloonSizeKernel: no memory for paMemObj!\n"));
533 return VERR_NO_MEMORY;
534 }
535 }
536
537 rc = VbglR0GRAlloc((VMMDevRequestHeader **)&pReq, g_cbChangeMemBalloonReq, VMMDevReq_ChangeMemBalloon);
538 if (RT_FAILURE(rc))
539 return rc;
540
541 if (cBalloonChunks > pDevExt->MemBalloon.cChunks)
542 {
543 /* inflate */
544 for (i = pDevExt->MemBalloon.cChunks; i < cBalloonChunks; i++)
545 {
546 rc = RTR0MemObjAllocPhysNC(&pDevExt->MemBalloon.paMemObj[i],
547 VMMDEV_MEMORY_BALLOON_CHUNK_SIZE, NIL_RTHCPHYS);
548 if (RT_FAILURE(rc))
549 {
550 if (rc == VERR_NOT_SUPPORTED)
551 {
552 /* not supported -- fall back to the R3-allocated memory. */
553 rc = VINF_SUCCESS;
554 pDevExt->MemBalloon.fUseKernelAPI = false;
555 Assert(pDevExt->MemBalloon.cChunks == 0);
556 Log(("VBoxGuestSetBalloonSizeKernel: PhysNC allocs not supported, falling back to R3 allocs.\n"));
557 }
558 /* else if (rc == VERR_NO_MEMORY || rc == VERR_NO_PHYS_MEMORY):
559 * cannot allocate more memory => don't try further, just stop here */
560 /* else: XXX what else can fail? VERR_MEMOBJ_INIT_FAILED for instance. just stop. */
561 break;
562 }
563
564 rc = vgdrvBalloonInflate(&pDevExt->MemBalloon.paMemObj[i], pReq);
565 if (RT_FAILURE(rc))
566 {
567 Log(("vboxGuestSetBalloonSize(inflate): failed, rc=%Rrc!\n", rc));
568 RTR0MemObjFree(pDevExt->MemBalloon.paMemObj[i], true);
569 pDevExt->MemBalloon.paMemObj[i] = NIL_RTR0MEMOBJ;
570 break;
571 }
572 pDevExt->MemBalloon.cChunks++;
573 }
574 }
575 else
576 {
577 /* deflate */
578 for (i = pDevExt->MemBalloon.cChunks; i-- > cBalloonChunks;)
579 {
580 rc = vgdrvBalloonDeflate(&pDevExt->MemBalloon.paMemObj[i], pReq);
581 if (RT_FAILURE(rc))
582 {
583 Log(("vboxGuestSetBalloonSize(deflate): failed, rc=%Rrc!\n", rc));
584 break;
585 }
586 pDevExt->MemBalloon.cChunks--;
587 }
588 }
589
590 VbglR0GRFree(&pReq->header);
591 }
592
593 /*
594 * Set the handle-in-ring3 indicator. When set Ring-3 will have to work
595 * the balloon changes via the other API.
596 */
597 *pfHandleInR3 = pDevExt->MemBalloon.fUseKernelAPI ? false : true;
598
599 return rc;
600}
601
602
603/**
604 * Inflate/deflate the balloon by one chunk.
605 *
606 * Worker for vgdrvIoCtl_ChangeMemoryBalloon - it takes the mutex.
607 *
608 * @returns VBox status code.
609 * @param pDevExt The device extension.
610 * @param pSession The session.
611 * @param pvChunk The address of the chunk to add to / remove from the
612 * balloon. (user space address)
613 * @param fInflate Inflate if true, deflate if false.
614 */
615static int vgdrvSetBalloonSizeFromUser(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession, RTR3PTR pvChunk, bool fInflate)
616{
617 VMMDevChangeMemBalloon *pReq;
618 PRTR0MEMOBJ pMemObj = NULL;
619 int rc = VINF_SUCCESS;
620 uint32_t i;
621 RT_NOREF1(pSession);
622
623 if (fInflate)
624 {
625 if ( pDevExt->MemBalloon.cChunks > pDevExt->MemBalloon.cMaxChunks - 1
626 || pDevExt->MemBalloon.cMaxChunks == 0 /* If called without first querying. */)
627 {
628 LogRel(("vgdrvSetBalloonSizeFromUser: cannot inflate balloon, already have %u chunks (max=%u)\n",
629 pDevExt->MemBalloon.cChunks, pDevExt->MemBalloon.cMaxChunks));
630 return VERR_INVALID_PARAMETER;
631 }
632
633 if (!pDevExt->MemBalloon.paMemObj)
634 {
635 pDevExt->MemBalloon.paMemObj = (PRTR0MEMOBJ)RTMemAlloc(sizeof(RTR0MEMOBJ) * pDevExt->MemBalloon.cMaxChunks);
636 if (!pDevExt->MemBalloon.paMemObj)
637 {
638 LogRel(("vgdrvSetBalloonSizeFromUser: no memory for paMemObj!\n"));
639 return VERR_NO_MEMORY;
640 }
641 for (i = 0; i < pDevExt->MemBalloon.cMaxChunks; i++)
642 pDevExt->MemBalloon.paMemObj[i] = NIL_RTR0MEMOBJ;
643 }
644 }
645 else
646 {
647 if (pDevExt->MemBalloon.cChunks == 0)
648 {
649 AssertMsgFailed(("vgdrvSetBalloonSizeFromUser: cannot decrease balloon, already at size 0\n"));
650 return VERR_INVALID_PARAMETER;
651 }
652 }
653
654 /*
655 * Enumerate all memory objects and check if the object is already registered.
656 */
657 for (i = 0; i < pDevExt->MemBalloon.cMaxChunks; i++)
658 {
659 if ( fInflate
660 && !pMemObj
661 && pDevExt->MemBalloon.paMemObj[i] == NIL_RTR0MEMOBJ)
662 pMemObj = &pDevExt->MemBalloon.paMemObj[i]; /* found free object pointer */
663 if (RTR0MemObjAddressR3(pDevExt->MemBalloon.paMemObj[i]) == pvChunk)
664 {
665 if (fInflate)
666 return VERR_ALREADY_EXISTS; /* don't provide the same memory twice */
667 pMemObj = &pDevExt->MemBalloon.paMemObj[i];
668 break;
669 }
670 }
671 if (!pMemObj)
672 {
673 if (fInflate)
674 {
675 /* no free object pointer found -- should not happen */
676 return VERR_NO_MEMORY;
677 }
678
679 /* cannot free this memory as it wasn't provided before */
680 return VERR_NOT_FOUND;
681 }
682
683 /*
684 * Try inflate / default the balloon as requested.
685 */
686 rc = VbglR0GRAlloc((VMMDevRequestHeader **)&pReq, g_cbChangeMemBalloonReq, VMMDevReq_ChangeMemBalloon);
687 if (RT_FAILURE(rc))
688 return rc;
689 pReq->header.fRequestor = pSession->fRequestor;
690
691 if (fInflate)
692 {
693 rc = RTR0MemObjLockUser(pMemObj, pvChunk, VMMDEV_MEMORY_BALLOON_CHUNK_SIZE,
694 RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
695 if (RT_SUCCESS(rc))
696 {
697 rc = vgdrvBalloonInflate(pMemObj, pReq);
698 if (RT_SUCCESS(rc))
699 pDevExt->MemBalloon.cChunks++;
700 else
701 {
702 Log(("vgdrvSetBalloonSizeFromUser(inflate): failed, rc=%Rrc!\n", rc));
703 RTR0MemObjFree(*pMemObj, true);
704 *pMemObj = NIL_RTR0MEMOBJ;
705 }
706 }
707 }
708 else
709 {
710 rc = vgdrvBalloonDeflate(pMemObj, pReq);
711 if (RT_SUCCESS(rc))
712 pDevExt->MemBalloon.cChunks--;
713 else
714 Log(("vgdrvSetBalloonSizeFromUser(deflate): failed, rc=%Rrc!\n", rc));
715 }
716
717 VbglR0GRFree(&pReq->header);
718 return rc;
719}
720
721
722/**
723 * Cleanup the memory balloon of a session.
724 *
725 * Will request the balloon mutex, so it must be valid and the caller must not
726 * own it already.
727 *
728 * @param pDevExt The device extension.
729 * @param pSession The session. Can be NULL at unload.
730 */
731static void vgdrvCloseMemBalloon(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession)
732{
733 RTSemFastMutexRequest(pDevExt->MemBalloon.hMtx);
734 if ( pDevExt->MemBalloon.pOwner == pSession
735 || pSession == NULL /*unload*/)
736 {
737 if (pDevExt->MemBalloon.paMemObj)
738 {
739 VMMDevChangeMemBalloon *pReq;
740 int rc = VbglR0GRAlloc((VMMDevRequestHeader **)&pReq, g_cbChangeMemBalloonReq, VMMDevReq_ChangeMemBalloon);
741 if (RT_SUCCESS(rc))
742 {
743 /* fRequestor is kernel here, as we're cleaning up. */
744
745 uint32_t i;
746 for (i = pDevExt->MemBalloon.cChunks; i-- > 0;)
747 {
748 rc = vgdrvBalloonDeflate(&pDevExt->MemBalloon.paMemObj[i], pReq);
749 if (RT_FAILURE(rc))
750 {
751 LogRel(("vgdrvCloseMemBalloon: Deflate failed with rc=%Rrc. Will leak %u chunks.\n",
752 rc, pDevExt->MemBalloon.cChunks));
753 break;
754 }
755 pDevExt->MemBalloon.paMemObj[i] = NIL_RTR0MEMOBJ;
756 pDevExt->MemBalloon.cChunks--;
757 }
758 VbglR0GRFree(&pReq->header);
759 }
760 else
761 LogRel(("vgdrvCloseMemBalloon: Failed to allocate VMMDev request buffer (rc=%Rrc). Will leak %u chunks.\n",
762 rc, pDevExt->MemBalloon.cChunks));
763 RTMemFree(pDevExt->MemBalloon.paMemObj);
764 pDevExt->MemBalloon.paMemObj = NULL;
765 }
766
767 pDevExt->MemBalloon.pOwner = NULL;
768 }
769 RTSemFastMutexRelease(pDevExt->MemBalloon.hMtx);
770}
771
772/** @} */
773
774
775
776/** @name Heartbeat
777 * @{
778 */
779
780/**
781 * Sends heartbeat to host.
782 *
783 * @returns VBox status code.
784 */
785static int vgdrvHeartbeatSend(PVBOXGUESTDEVEXT pDevExt)
786{
787 int rc;
788 if (pDevExt->pReqGuestHeartbeat)
789 {
790 rc = VbglR0GRPerform(pDevExt->pReqGuestHeartbeat);
791 Log3(("vgdrvHeartbeatSend: VbglR0GRPerform vgdrvHeartbeatSend completed with rc=%Rrc\n", rc));
792 }
793 else
794 rc = VERR_INVALID_STATE;
795 return rc;
796}
797
798
799/**
800 * Callback for heartbeat timer.
801 */
802static DECLCALLBACK(void) vgdrvHeartbeatTimerHandler(PRTTIMER hTimer, void *pvUser, uint64_t iTick)
803{
804 PVBOXGUESTDEVEXT pDevExt = (PVBOXGUESTDEVEXT)pvUser;
805 int rc;
806 AssertReturnVoid(pDevExt);
807
808 rc = vgdrvHeartbeatSend(pDevExt);
809 if (RT_FAILURE(rc))
810 Log(("HB Timer: vgdrvHeartbeatSend failed: rc=%Rrc\n", rc));
811
812 NOREF(hTimer); NOREF(iTick);
813}
814
815
816/**
817 * Configure the host to check guest's heartbeat
818 * and get heartbeat interval from the host.
819 *
820 * @returns VBox status code.
821 * @param pDevExt The device extension.
822 * @param fEnabled Set true to enable guest heartbeat checks on host.
823 */
824static int vgdrvHeartbeatHostConfigure(PVBOXGUESTDEVEXT pDevExt, bool fEnabled)
825{
826 VMMDevReqHeartbeat *pReq;
827 int rc = VbglR0GRAlloc((VMMDevRequestHeader **)&pReq, sizeof(*pReq), VMMDevReq_HeartbeatConfigure);
828 Log(("vgdrvHeartbeatHostConfigure: VbglR0GRAlloc vgdrvHeartbeatHostConfigure completed with rc=%Rrc\n", rc));
829 if (RT_SUCCESS(rc))
830 {
831 pReq->fEnabled = fEnabled;
832 pReq->cNsInterval = 0;
833 rc = VbglR0GRPerform(&pReq->header);
834 Log(("vgdrvHeartbeatHostConfigure: VbglR0GRPerform vgdrvHeartbeatHostConfigure completed with rc=%Rrc\n", rc));
835 pDevExt->cNsHeartbeatInterval = pReq->cNsInterval;
836 VbglR0GRFree(&pReq->header);
837 }
838 return rc;
839}
840
841
842/**
843 * Initializes the heartbeat timer.
844 *
845 * This feature may be disabled by the host.
846 *
847 * @returns VBox status (ignored).
848 * @param pDevExt The device extension.
849 */
850static int vgdrvHeartbeatInit(PVBOXGUESTDEVEXT pDevExt)
851{
852 /*
853 * Make sure that heartbeat checking is disabled.
854 */
855 int rc = vgdrvHeartbeatHostConfigure(pDevExt, false);
856 if (RT_SUCCESS(rc))
857 {
858 rc = vgdrvHeartbeatHostConfigure(pDevExt, true);
859 if (RT_SUCCESS(rc))
860 {
861 /*
862 * Preallocate the request to use it from the timer callback because:
863 * 1) on Windows VbglR0GRAlloc must be called at IRQL <= APC_LEVEL
864 * and the timer callback runs at DISPATCH_LEVEL;
865 * 2) avoid repeated allocations.
866 */
867 rc = VbglR0GRAlloc(&pDevExt->pReqGuestHeartbeat, sizeof(*pDevExt->pReqGuestHeartbeat), VMMDevReq_GuestHeartbeat);
868 if (RT_SUCCESS(rc))
869 {
870 LogRel(("vgdrvHeartbeatInit: Setting up heartbeat to trigger every %RU64 milliseconds\n",
871 pDevExt->cNsHeartbeatInterval / RT_NS_1MS));
872 rc = RTTimerCreateEx(&pDevExt->pHeartbeatTimer, pDevExt->cNsHeartbeatInterval, 0 /*fFlags*/,
873 (PFNRTTIMER)vgdrvHeartbeatTimerHandler, pDevExt);
874 if (RT_SUCCESS(rc))
875 {
876 rc = RTTimerStart(pDevExt->pHeartbeatTimer, 0);
877 if (RT_SUCCESS(rc))
878 return VINF_SUCCESS;
879
880 LogRel(("vgdrvHeartbeatInit: Heartbeat timer failed to start, rc=%Rrc\n", rc));
881 }
882 else
883 LogRel(("vgdrvHeartbeatInit: Failed to create heartbeat timer: %Rrc\n", rc));
884
885 VbglR0GRFree(pDevExt->pReqGuestHeartbeat);
886 pDevExt->pReqGuestHeartbeat = NULL;
887 }
888 else
889 LogRel(("vgdrvHeartbeatInit: VbglR0GRAlloc(VMMDevReq_GuestHeartbeat): %Rrc\n", rc));
890
891 LogRel(("vgdrvHeartbeatInit: Failed to set up the timer, guest heartbeat is disabled\n"));
892 vgdrvHeartbeatHostConfigure(pDevExt, false);
893 }
894 else
895 LogRel(("vgdrvHeartbeatInit: Failed to configure host for heartbeat checking: rc=%Rrc\n", rc));
896 }
897 return rc;
898}
899
900/** @} */
901
902
903/**
904 * Helper to reinit the VMMDev communication after hibernation.
905 *
906 * @returns VBox status code.
907 * @param pDevExt The device extension.
908 * @param enmOSType The OS type.
909 *
910 * @todo Call this on all platforms, not just windows.
911 */
912int VGDrvCommonReinitDevExtAfterHibernation(PVBOXGUESTDEVEXT pDevExt, VBOXOSTYPE enmOSType)
913{
914 int rc = vgdrvReportGuestInfo(enmOSType);
915 if (RT_SUCCESS(rc))
916 {
917 rc = vgdrvReportDriverStatus(true /* Driver is active */);
918 if (RT_FAILURE(rc))
919 Log(("VGDrvCommonReinitDevExtAfterHibernation: could not report guest driver status, rc=%Rrc\n", rc));
920 }
921 else
922 Log(("VGDrvCommonReinitDevExtAfterHibernation: could not report guest information to host, rc=%Rrc\n", rc));
923 LogFlow(("VGDrvCommonReinitDevExtAfterHibernation: returned with rc=%Rrc\n", rc));
924 RT_NOREF1(pDevExt);
925 return rc;
926}
927
928
929/**
930 * Initializes the release logger (debug is implicit), if configured.
931 *
932 * @returns IPRT status code.
933 */
934int VGDrvCommonInitLoggers(void)
935{
936#ifdef VBOX_GUESTDRV_WITH_RELEASE_LOGGER
937 /*
938 * Create the release log.
939 */
940 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
941 PRTLOGGER pRelLogger;
942 int rc = RTLogCreate(&pRelLogger, 0 /*fFlags*/, "all", "VBOXGUEST_RELEASE_LOG", RT_ELEMENTS(s_apszGroups), s_apszGroups,
943 RTLOGDEST_STDOUT | RTLOGDEST_DEBUGGER, NULL);
944 if (RT_SUCCESS(rc))
945 RTLogRelSetDefaultInstance(pRelLogger);
946 /** @todo Add native hook for getting logger config parameters and setting
947 * them. On linux we should use the module parameter stuff... */
948 return rc;
949#else
950 return VINF_SUCCESS;
951#endif
952}
953
954
955/**
956 * Destroys the loggers.
957 */
958void VGDrvCommonDestroyLoggers(void)
959{
960#ifdef VBOX_GUESTDRV_WITH_RELEASE_LOGGER
961 RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
962 RTLogDestroy(RTLogSetDefaultInstance(NULL));
963#endif
964}
965
966
967/**
968 * Initialize the device extension fundament.
969 *
970 * There are no device resources at this point, VGDrvCommonInitDevExtResources
971 * should be called when they are available.
972 *
973 * @returns VBox status code.
974 * @param pDevExt The device extension to init.
975 */
976int VGDrvCommonInitDevExtFundament(PVBOXGUESTDEVEXT pDevExt)
977{
978 int rc;
979 AssertMsg( pDevExt->uInitState != VBOXGUESTDEVEXT_INIT_STATE_FUNDAMENT
980 && pDevExt->uInitState != VBOXGUESTDEVEXT_INIT_STATE_RESOURCES, ("uInitState=%#x\n", pDevExt->uInitState));
981
982 /*
983 * Initialize the data.
984 */
985 pDevExt->IOPortBase = UINT16_MAX;
986 pDevExt->pVMMDevMemory = NULL;
987 pDevExt->hGuestMappings = NIL_RTR0MEMOBJ;
988 pDevExt->EventSpinlock = NIL_RTSPINLOCK;
989 pDevExt->fHostFeatures = 0;
990 pDevExt->pIrqAckEvents = NULL;
991 pDevExt->PhysIrqAckEvents = NIL_RTCCPHYS;
992 RTListInit(&pDevExt->WaitList);
993#ifdef VBOX_WITH_HGCM
994 RTListInit(&pDevExt->HGCMWaitList);
995#endif
996#ifdef VBOXGUEST_USE_DEFERRED_WAKE_UP
997 RTListInit(&pDevExt->WakeUpList);
998#endif
999 RTListInit(&pDevExt->WokenUpList);
1000 RTListInit(&pDevExt->FreeList);
1001 RTListInit(&pDevExt->SessionList);
1002 pDevExt->cSessions = 0;
1003 pDevExt->fLoggingEnabled = false;
1004 pDevExt->f32PendingEvents = 0;
1005 pDevExt->u32MousePosChangedSeq = 0;
1006 pDevExt->SessionSpinlock = NIL_RTSPINLOCK;
1007 pDevExt->MemBalloon.hMtx = NIL_RTSEMFASTMUTEX;
1008 pDevExt->MemBalloon.cChunks = 0;
1009 pDevExt->MemBalloon.cMaxChunks = 0;
1010 pDevExt->MemBalloon.fUseKernelAPI = true;
1011 pDevExt->MemBalloon.paMemObj = NULL;
1012 pDevExt->MemBalloon.pOwner = NULL;
1013 pDevExt->pfnMouseNotifyCallback = NULL;
1014 pDevExt->pvMouseNotifyCallbackArg = NULL;
1015 pDevExt->pReqGuestHeartbeat = NULL;
1016
1017 pDevExt->fFixedEvents = 0;
1018 vgdrvBitUsageTrackerClear(&pDevExt->EventFilterTracker);
1019 pDevExt->fEventFilterHost = UINT32_MAX; /* forces a report */
1020
1021 vgdrvBitUsageTrackerClear(&pDevExt->MouseStatusTracker);
1022 pDevExt->fMouseStatusHost = UINT32_MAX; /* forces a report */
1023
1024 pDevExt->fAcquireModeGuestCaps = 0;
1025 pDevExt->fSetModeGuestCaps = 0;
1026 pDevExt->fAcquiredGuestCaps = 0;
1027 vgdrvBitUsageTrackerClear(&pDevExt->SetGuestCapsTracker);
1028 pDevExt->fGuestCapsHost = UINT32_MAX; /* forces a report */
1029
1030 /*
1031 * Create the wait and session spinlocks as well as the ballooning mutex.
1032 */
1033 rc = RTSpinlockCreate(&pDevExt->EventSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "VBoxGuestEvent");
1034 if (RT_SUCCESS(rc))
1035 {
1036 rc = RTSpinlockCreate(&pDevExt->SessionSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "VBoxGuestSession");
1037 if (RT_SUCCESS(rc))
1038 {
1039 rc = RTSemFastMutexCreate(&pDevExt->MemBalloon.hMtx);
1040 if (RT_SUCCESS(rc))
1041 {
1042 pDevExt->uInitState = VBOXGUESTDEVEXT_INIT_STATE_FUNDAMENT;
1043 return VINF_SUCCESS;
1044 }
1045
1046 LogRel(("VGDrvCommonInitDevExt: failed to create mutex, rc=%Rrc!\n", rc));
1047 RTSpinlockDestroy(pDevExt->SessionSpinlock);
1048 }
1049 else
1050 LogRel(("VGDrvCommonInitDevExt: failed to create spinlock, rc=%Rrc!\n", rc));
1051 RTSpinlockDestroy(pDevExt->EventSpinlock);
1052 }
1053 else
1054 LogRel(("VGDrvCommonInitDevExt: failed to create spinlock, rc=%Rrc!\n", rc));
1055
1056 pDevExt->uInitState = 0;
1057 return rc;
1058}
1059
1060
1061/**
1062 * Counter to VGDrvCommonInitDevExtFundament.
1063 *
1064 * @param pDevExt The device extension.
1065 */
1066void VGDrvCommonDeleteDevExtFundament(PVBOXGUESTDEVEXT pDevExt)
1067{
1068 int rc2;
1069 AssertMsgReturnVoid(pDevExt->uInitState == VBOXGUESTDEVEXT_INIT_STATE_FUNDAMENT, ("uInitState=%#x\n", pDevExt->uInitState));
1070 pDevExt->uInitState = VBOXGUESTDEVEXT_INIT_STATE_DELETED;
1071
1072 rc2 = RTSemFastMutexDestroy(pDevExt->MemBalloon.hMtx); AssertRC(rc2);
1073 rc2 = RTSpinlockDestroy(pDevExt->EventSpinlock); AssertRC(rc2);
1074 rc2 = RTSpinlockDestroy(pDevExt->SessionSpinlock); AssertRC(rc2);
1075}
1076
1077
1078/**
1079 * Initializes the VBoxGuest device extension resource parts.
1080 *
1081 * The native code locates the VMMDev on the PCI bus and retrieve the MMIO and
1082 * I/O port ranges, this function will take care of mapping the MMIO memory (if
1083 * present). Upon successful return the native code should set up the interrupt
1084 * handler.
1085 *
1086 * @returns VBox status code.
1087 *
1088 * @param pDevExt The device extension. Allocated by the native code.
1089 * @param IOPortBase The base of the I/O port range.
1090 * @param pvMMIOBase The base of the MMIO memory mapping.
1091 * This is optional, pass NULL if not present.
1092 * @param cbMMIO The size of the MMIO memory mapping.
1093 * This is optional, pass 0 if not present.
1094 * @param enmOSType The guest OS type to report to the VMMDev.
1095 * @param fFixedEvents Events that will be enabled upon init and no client
1096 * will ever be allowed to mask.
1097 */
1098int VGDrvCommonInitDevExtResources(PVBOXGUESTDEVEXT pDevExt, uint16_t IOPortBase,
1099 void *pvMMIOBase, uint32_t cbMMIO, VBOXOSTYPE enmOSType, uint32_t fFixedEvents)
1100{
1101 int rc;
1102 AssertMsgReturn(pDevExt->uInitState == VBOXGUESTDEVEXT_INIT_STATE_FUNDAMENT, ("uInitState=%#x\n", pDevExt->uInitState),
1103 VERR_INVALID_STATE);
1104
1105 /*
1106 * If there is an MMIO region validate the version and size.
1107 */
1108 if (pvMMIOBase)
1109 {
1110 VMMDevMemory *pVMMDev = (VMMDevMemory *)pvMMIOBase;
1111 Assert(cbMMIO);
1112 if ( pVMMDev->u32Version == VMMDEV_MEMORY_VERSION
1113 && pVMMDev->u32Size >= 32
1114 && pVMMDev->u32Size <= cbMMIO)
1115 {
1116 pDevExt->pVMMDevMemory = pVMMDev;
1117 Log(("VGDrvCommonInitDevExtResources: VMMDevMemory: mapping=%p size=%#RX32 (%#RX32) version=%#RX32\n",
1118 pVMMDev, pVMMDev->u32Size, cbMMIO, pVMMDev->u32Version));
1119 }
1120 else /* try live without it. */
1121 LogRel(("VGDrvCommonInitDevExtResources: Bogus VMMDev memory; u32Version=%RX32 (expected %RX32) u32Size=%RX32 (expected <= %RX32)\n",
1122 pVMMDev->u32Version, VMMDEV_MEMORY_VERSION, pVMMDev->u32Size, cbMMIO));
1123 }
1124
1125 /*
1126 * Initialize the guest library and report the guest info back to VMMDev,
1127 * set the interrupt control filter mask, and fixate the guest mappings
1128 * made by the VMM.
1129 */
1130 pDevExt->IOPortBase = IOPortBase;
1131 rc = VbglR0InitPrimary(pDevExt->IOPortBase, (VMMDevMemory *)pDevExt->pVMMDevMemory, &pDevExt->fHostFeatures);
1132 if (RT_SUCCESS(rc))
1133 {
1134 VMMDevRequestHeader *pAckReq = NULL;
1135 rc = VbglR0GRAlloc(&pAckReq, sizeof(VMMDevEvents), VMMDevReq_AcknowledgeEvents);
1136 if (RT_SUCCESS(rc))
1137 {
1138 pDevExt->PhysIrqAckEvents = VbglR0PhysHeapGetPhysAddr(pAckReq);
1139 Assert(pDevExt->PhysIrqAckEvents != 0);
1140 ASMCompilerBarrier(); /* linux + solaris already have IRQs hooked up at this point, so take care. */
1141 pDevExt->pIrqAckEvents = (VMMDevEvents *)pAckReq;
1142
1143 rc = vgdrvReportGuestInfo(enmOSType);
1144 if (RT_SUCCESS(rc))
1145 {
1146 /*
1147 * Set the fixed event and make sure the host doesn't have any lingering
1148 * the guest capabilities or mouse status bits set.
1149 */
1150#ifdef VBOX_WITH_HGCM
1151 fFixedEvents |= VMMDEV_EVENT_HGCM;
1152#endif
1153 pDevExt->fFixedEvents = fFixedEvents;
1154 rc = vgdrvResetEventFilterOnHost(pDevExt, fFixedEvents);
1155 if (RT_SUCCESS(rc))
1156 {
1157 rc = vgdrvResetCapabilitiesOnHost(pDevExt);
1158 if (RT_SUCCESS(rc))
1159 {
1160 rc = vgdrvResetMouseStatusOnHost(pDevExt);
1161 if (RT_SUCCESS(rc))
1162 {
1163 /*
1164 * Initialize stuff which may fail without requiring the driver init to fail.
1165 */
1166 vgdrvInitFixateGuestMappings(pDevExt);
1167 vgdrvHeartbeatInit(pDevExt);
1168
1169 /*
1170 * Done!
1171 */
1172 rc = vgdrvReportDriverStatus(true /* Driver is active */);
1173 if (RT_FAILURE(rc))
1174 LogRel(("VGDrvCommonInitDevExtResources: VBoxReportGuestDriverStatus failed, rc=%Rrc\n", rc));
1175
1176 pDevExt->uInitState = VBOXGUESTDEVEXT_INIT_STATE_RESOURCES;
1177 LogFlowFunc(("VGDrvCommonInitDevExtResources: returns success\n"));
1178 return VINF_SUCCESS;
1179 }
1180 LogRel(("VGDrvCommonInitDevExtResources: failed to clear mouse status: rc=%Rrc\n", rc));
1181 }
1182 else
1183 LogRel(("VGDrvCommonInitDevExtResources: failed to clear guest capabilities: rc=%Rrc\n", rc));
1184 }
1185 else
1186 LogRel(("VGDrvCommonInitDevExtResources: failed to set fixed event filter: rc=%Rrc\n", rc));
1187 pDevExt->fFixedEvents = 0;
1188 }
1189 else
1190 LogRel(("VGDrvCommonInitDevExtResources: vgdrvReportGuestInfo failed: rc=%Rrc\n", rc));
1191 VbglR0GRFree((VMMDevRequestHeader *)pDevExt->pIrqAckEvents);
1192 }
1193 else
1194 LogRel(("VGDrvCommonInitDevExtResources: VbglR0GRAlloc failed: rc=%Rrc\n", rc));
1195
1196 VbglR0TerminatePrimary();
1197 }
1198 else
1199 LogRel(("VGDrvCommonInitDevExtResources: VbglR0InitPrimary failed: rc=%Rrc\n", rc));
1200 pDevExt->IOPortBase = UINT16_MAX;
1201 return rc;
1202}
1203
1204
1205/**
1206 * Deletes all the items in a wait chain.
1207 * @param pList The head of the chain.
1208 */
1209static void vgdrvDeleteWaitList(PRTLISTNODE pList)
1210{
1211 while (!RTListIsEmpty(pList))
1212 {
1213 int rc2;
1214 PVBOXGUESTWAIT pWait = RTListGetFirst(pList, VBOXGUESTWAIT, ListNode);
1215 RTListNodeRemove(&pWait->ListNode);
1216
1217 rc2 = RTSemEventMultiDestroy(pWait->Event); AssertRC(rc2);
1218 pWait->Event = NIL_RTSEMEVENTMULTI;
1219 pWait->pSession = NULL;
1220 RTMemFree(pWait);
1221 }
1222}
1223
1224
1225/**
1226 * Counter to VGDrvCommonInitDevExtResources.
1227 *
1228 * @param pDevExt The device extension.
1229 */
1230void VGDrvCommonDeleteDevExtResources(PVBOXGUESTDEVEXT pDevExt)
1231{
1232 Log(("VGDrvCommonDeleteDevExtResources:\n"));
1233 AssertMsgReturnVoid(pDevExt->uInitState == VBOXGUESTDEVEXT_INIT_STATE_RESOURCES, ("uInitState=%#x\n", pDevExt->uInitState));
1234 pDevExt->uInitState = VBOXGUESTDEVEXT_INIT_STATE_FUNDAMENT;
1235
1236 /*
1237 * Stop and destroy HB timer and disable host heartbeat checking.
1238 */
1239 if (pDevExt->pHeartbeatTimer)
1240 {
1241 RTTimerDestroy(pDevExt->pHeartbeatTimer);
1242 vgdrvHeartbeatHostConfigure(pDevExt, false);
1243 }
1244
1245 VbglR0GRFree(pDevExt->pReqGuestHeartbeat);
1246 pDevExt->pReqGuestHeartbeat = NULL;
1247
1248 /*
1249 * Clean up the bits that involves the host first.
1250 */
1251 vgdrvTermUnfixGuestMappings(pDevExt);
1252 if (!RTListIsEmpty(&pDevExt->SessionList))
1253 {
1254 LogRelFunc(("session list not empty!\n"));
1255 RTListInit(&pDevExt->SessionList);
1256 }
1257
1258 /*
1259 * Update the host flags (mouse status etc) not to reflect this session.
1260 */
1261 pDevExt->fFixedEvents = 0;
1262 vgdrvResetEventFilterOnHost(pDevExt, 0 /*fFixedEvents*/);
1263 vgdrvResetCapabilitiesOnHost(pDevExt);
1264 vgdrvResetMouseStatusOnHost(pDevExt);
1265
1266 vgdrvCloseMemBalloon(pDevExt, (PVBOXGUESTSESSION)NULL);
1267
1268 /*
1269 * No more IRQs.
1270 */
1271 pDevExt->pIrqAckEvents = NULL; /* Will be freed by VbglR0TerminatePrimary. */
1272 ASMAtomicWriteU32(&pDevExt->fHostFeatures, 0);
1273
1274 /*
1275 * Cleanup all the other resources.
1276 */
1277 vgdrvDeleteWaitList(&pDevExt->WaitList);
1278#ifdef VBOX_WITH_HGCM
1279 vgdrvDeleteWaitList(&pDevExt->HGCMWaitList);
1280#endif
1281#ifdef VBOXGUEST_USE_DEFERRED_WAKE_UP
1282 vgdrvDeleteWaitList(&pDevExt->WakeUpList);
1283#endif
1284 vgdrvDeleteWaitList(&pDevExt->WokenUpList);
1285 vgdrvDeleteWaitList(&pDevExt->FreeList);
1286
1287 VbglR0TerminatePrimary();
1288
1289
1290 pDevExt->pVMMDevMemory = NULL;
1291 pDevExt->IOPortBase = 0;
1292}
1293
1294
1295/**
1296 * Initializes the VBoxGuest device extension when the device driver is loaded.
1297 *
1298 * The native code locates the VMMDev on the PCI bus and retrieve the MMIO and
1299 * I/O port ranges, this function will take care of mapping the MMIO memory (if
1300 * present). Upon successful return the native code should set up the interrupt
1301 * handler.
1302 *
1303 * Instead of calling this method, the host specific code choose to perform a
1304 * more granular initialization using:
1305 * 1. VGDrvCommonInitLoggers
1306 * 2. VGDrvCommonInitDevExtFundament
1307 * 3. VGDrvCommonInitDevExtResources
1308 *
1309 * @returns VBox status code.
1310 *
1311 * @param pDevExt The device extension. Allocated by the native code.
1312 * @param IOPortBase The base of the I/O port range.
1313 * @param pvMMIOBase The base of the MMIO memory mapping.
1314 * This is optional, pass NULL if not present.
1315 * @param cbMMIO The size of the MMIO memory mapping.
1316 * This is optional, pass 0 if not present.
1317 * @param enmOSType The guest OS type to report to the VMMDev.
1318 * @param fFixedEvents Events that will be enabled upon init and no client
1319 * will ever be allowed to mask.
1320 */
1321int VGDrvCommonInitDevExt(PVBOXGUESTDEVEXT pDevExt, uint16_t IOPortBase,
1322 void *pvMMIOBase, uint32_t cbMMIO, VBOXOSTYPE enmOSType, uint32_t fFixedEvents)
1323{
1324 int rc;
1325 VGDrvCommonInitLoggers();
1326
1327 rc = VGDrvCommonInitDevExtFundament(pDevExt);
1328 if (RT_SUCCESS(rc))
1329 {
1330 rc = VGDrvCommonInitDevExtResources(pDevExt, IOPortBase, pvMMIOBase, cbMMIO, enmOSType, fFixedEvents);
1331 if (RT_SUCCESS(rc))
1332 return rc;
1333
1334 VGDrvCommonDeleteDevExtFundament(pDevExt);
1335 }
1336 VGDrvCommonDestroyLoggers();
1337 return rc; /* (failed) */
1338}
1339
1340
1341/**
1342 * Checks if the given option can be taken to not mean 'false'.
1343 *
1344 * @returns true or false accordingly.
1345 * @param pszValue The value to consider.
1346 */
1347bool VBDrvCommonIsOptionValueTrue(const char *pszValue)
1348{
1349 if (pszValue)
1350 {
1351 char ch;
1352 while ( (ch = *pszValue) != '\0'
1353 && RT_C_IS_SPACE(ch))
1354 pszValue++;
1355
1356 return ch != '\0'
1357 && ch != 'n' /* no */
1358 && ch != 'N' /* NO */
1359 && ch != 'd' /* disabled */
1360 && ch != 'f' /* false*/
1361 && ch != 'F' /* FALSE */
1362 && ch != 'D' /* DISABLED */
1363 && ( (ch != 'o' && ch != 'O') /* off, OFF, Off */
1364 || (pszValue[1] != 'f' && pszValue[1] != 'F') )
1365 && (ch != '0' || pszValue[1] != '\0') /* '0' */
1366 ;
1367 }
1368 return false;
1369}
1370
1371
1372/**
1373 * Processes a option.
1374 *
1375 * This will let the OS specific code have a go at it too.
1376 *
1377 * @param pDevExt The device extension.
1378 * @param pszName The option name, sans prefix.
1379 * @param pszValue The option value.
1380 */
1381void VGDrvCommonProcessOption(PVBOXGUESTDEVEXT pDevExt, const char *pszName, const char *pszValue)
1382{
1383 Log(("VGDrvCommonProcessOption: pszName='%s' pszValue='%s'\n", pszName, pszValue));
1384
1385 if ( RTStrICmpAscii(pszName, "r3_log_to_host") == 0
1386 || RTStrICmpAscii(pszName, "LoggingEnabled") == 0 /*legacy*/ )
1387 pDevExt->fLoggingEnabled = VBDrvCommonIsOptionValueTrue(pszValue);
1388 else if ( RTStrNICmpAscii(pszName, RT_STR_TUPLE("log")) == 0
1389 || RTStrNICmpAscii(pszName, RT_STR_TUPLE("dbg_log")) == 0)
1390 {
1391 bool const fLogRel = *pszName == 'd' || *pszName == 'D';
1392 const char *pszSubName = &pszName[fLogRel ? 4 + 3 : 3];
1393 if ( !*pszSubName
1394 || RTStrICmpAscii(pszSubName, "_flags") == 0
1395 || RTStrICmpAscii(pszSubName, "_dest") == 0)
1396 {
1397 PRTLOGGER pLogger = fLogRel ? RTLogRelGetDefaultInstance() : RTLogDefaultInstance();
1398 if (pLogger)
1399 {
1400 if (!*pszSubName)
1401 RTLogGroupSettings(pLogger, pszValue);
1402 else if (RTStrICmpAscii(pszSubName, "_flags"))
1403 RTLogFlags(pLogger, pszValue);
1404 else
1405 RTLogDestinations(pLogger, pszValue);
1406 }
1407 }
1408 else if (!VGDrvNativeProcessOption(pDevExt, pszName, pszValue))
1409 LogRel(("VBoxGuest: Ignoring unknown option '%s' (value '%s')\n", pszName, pszValue));
1410 }
1411 else if (!VGDrvNativeProcessOption(pDevExt, pszName, pszValue))
1412 LogRel(("VBoxGuest: Ignoring unknown option '%s' (value '%s')\n", pszName, pszValue));
1413}
1414
1415
1416/**
1417 * Read driver configuration from the host.
1418 *
1419 * This involves connecting to the guest properties service, which means that
1420 * interrupts needs to work and that the calling thread must be able to block.
1421 *
1422 * @param pDevExt The device extension.
1423 */
1424void VGDrvCommonProcessOptionsFromHost(PVBOXGUESTDEVEXT pDevExt)
1425{
1426 /*
1427 * Create a kernel session without our selves, then connect to the HGCM service.
1428 */
1429 PVBOXGUESTSESSION pSession;
1430 int rc = VGDrvCommonCreateKernelSession(pDevExt, &pSession);
1431 if (RT_SUCCESS(rc))
1432 {
1433 union
1434 {
1435 VBGLIOCHGCMCONNECT Connect;
1436 VBGLIOCHGCMDISCONNECT Disconnect;
1437 GuestPropMsgEnumProperties EnumMsg;
1438 } uBuf;
1439
1440 RT_ZERO(uBuf.Connect);
1441 VBGLREQHDR_INIT(&uBuf.Connect.Hdr, HGCM_CONNECT);
1442 uBuf.Connect.u.In.Loc.type = VMMDevHGCMLoc_LocalHost_Existing;
1443 RTStrCopy(uBuf.Connect.u.In.Loc.u.host.achName, sizeof(uBuf.Connect.u.In.Loc.u.host.achName),
1444 "VBoxGuestPropSvc"); /** @todo Add a define to the header for the name. */
1445 rc = VGDrvCommonIoCtl(VBGL_IOCTL_HGCM_CONNECT, pDevExt, pSession, &uBuf.Connect.Hdr, sizeof(uBuf.Connect));
1446 if (RT_SUCCESS(rc))
1447 {
1448 static const char g_szzPattern[] = "/VirtualBox/GuestAdd/VBoxGuest/*\0";
1449 uint32_t const idClient = uBuf.Connect.u.Out.idClient;
1450 char *pszzStrings = NULL;
1451 uint32_t cbStrings;
1452
1453 /*
1454 * Enumerate all the relevant properties. We try with a 1KB buffer, but
1455 * will double it until we get what we want or go beyond 16KB.
1456 */
1457 for (cbStrings = _1K; cbStrings <= _16K; cbStrings *= 2)
1458 {
1459 pszzStrings = (char *)RTMemAllocZ(cbStrings);
1460 if (pszzStrings)
1461 {
1462 VBGL_HGCM_HDR_INIT(&uBuf.EnumMsg.hdr, idClient, GUEST_PROP_FN_ENUM_PROPS, 3);
1463
1464 uBuf.EnumMsg.patterns.type = VMMDevHGCMParmType_LinAddr;
1465 uBuf.EnumMsg.patterns.u.Pointer.size = sizeof(g_szzPattern);
1466 uBuf.EnumMsg.patterns.u.Pointer.u.linearAddr = (uintptr_t)g_szzPattern;
1467
1468 uBuf.EnumMsg.strings.type = VMMDevHGCMParmType_LinAddr;
1469 uBuf.EnumMsg.strings.u.Pointer.size = cbStrings;
1470 uBuf.EnumMsg.strings.u.Pointer.u.linearAddr = (uintptr_t)pszzStrings;
1471
1472 uBuf.EnumMsg.size.type = VMMDevHGCMParmType_32bit;
1473 uBuf.EnumMsg.size.u.value32 = 0;
1474
1475 rc = VGDrvCommonIoCtl(VBGL_IOCTL_HGCM_CALL(sizeof(uBuf.EnumMsg)), pDevExt, pSession,
1476 &uBuf.EnumMsg.hdr.Hdr, sizeof(uBuf.EnumMsg));
1477 if (RT_SUCCESS(rc))
1478 {
1479 if ( uBuf.EnumMsg.size.type == VMMDevHGCMParmType_32bit
1480 && uBuf.EnumMsg.size.u.value32 <= cbStrings
1481 && uBuf.EnumMsg.size.u.value32 > 0)
1482 cbStrings = uBuf.EnumMsg.size.u.value32;
1483 Log(("VGDrvCommonReadConfigurationFromHost: GUEST_PROP_FN_ENUM_PROPS -> %#x bytes (cbStrings=%#x)\n",
1484 uBuf.EnumMsg.size.u.value32, cbStrings));
1485 break;
1486 }
1487
1488 RTMemFree(pszzStrings);
1489 pszzStrings = NULL;
1490 }
1491 else
1492 {
1493 LogRel(("VGDrvCommonReadConfigurationFromHost: failed to allocate %#x bytes\n", cbStrings));
1494 break;
1495 }
1496 }
1497
1498 /*
1499 * Disconnect and destroy the session.
1500 */
1501 VBGLREQHDR_INIT(&uBuf.Disconnect.Hdr, HGCM_DISCONNECT);
1502 uBuf.Disconnect.u.In.idClient = idClient;
1503 VGDrvCommonIoCtl(VBGL_IOCTL_HGCM_DISCONNECT, pDevExt, pSession, &uBuf.Disconnect.Hdr, sizeof(uBuf.Disconnect));
1504
1505 VGDrvCommonCloseSession(pDevExt, pSession);
1506
1507 /*
1508 * Process the properties if we got any.
1509 *
1510 * The string buffer contains packed strings in groups of four - name, value,
1511 * timestamp (as a decimal string) and flags. It is terminated by four empty
1512 * strings. Layout:
1513 * Name\0Value\0Timestamp\0Flags\0
1514 */
1515 if (pszzStrings)
1516 {
1517 uint32_t off;
1518 for (off = 0; off < cbStrings; off++)
1519 {
1520 /*
1521 * Parse the four fields, checking that it's all plain ASCII w/o any control characters.
1522 */
1523 const char *apszFields[4] = { NULL, NULL, NULL, NULL };
1524 bool fValidFields = true;
1525 unsigned iField;
1526 for (iField = 0; iField < RT_ELEMENTS(apszFields); iField++)
1527 {
1528 apszFields[0] = &pszzStrings[off];
1529 while (off < cbStrings)
1530 {
1531 char ch = pszzStrings[off++];
1532 if ((unsigned)ch < 0x20U || (unsigned)ch > 0x7fU)
1533 {
1534 if (!ch)
1535 break;
1536 if (fValidFields)
1537 Log(("VGDrvCommonReadConfigurationFromHost: Invalid char %#x at %#x (field %u)\n",
1538 ch, off - 1, iField));
1539 fValidFields = false;
1540 }
1541 }
1542 }
1543 if ( off <= cbStrings
1544 && fValidFields
1545 && *apszFields[0] != '\0')
1546 {
1547 /*
1548 * Validate and convert the flags to integer, then process the option.
1549 */
1550 uint32_t fFlags = 0;
1551 rc = GuestPropValidateFlags(apszFields[3], &fFlags);
1552 if (RT_SUCCESS(rc))
1553 {
1554 if (fFlags & GUEST_PROP_F_RDONLYGUEST)
1555 {
1556 apszFields[0] += sizeof(g_szzPattern) - 2;
1557 VGDrvCommonProcessOption(pDevExt, apszFields[0], apszFields[1]);
1558 }
1559 else
1560 LogRel(("VBoxGuest: Ignoring '%s' as it does not have RDONLYGUEST set\n", apszFields[0]));
1561 }
1562 else
1563 LogRel(("VBoxGuest: Invalid flags '%s' for '%s': %Rrc\n", apszFields[2], apszFields[0], rc));
1564 }
1565 else if (off < cbStrings)
1566 {
1567 LogRel(("VBoxGuest: Malformed guest properties enum result!\n"));
1568 Log(("VBoxGuest: off=%#x cbStrings=%#x\n%.*Rhxd\n", off, cbStrings, cbStrings, pszzStrings));
1569 break;
1570 }
1571 else if (!fValidFields)
1572 LogRel(("VBoxGuest: Ignoring %.*Rhxs as it has invalid characters in one or more fields\n",
1573 (int)strlen(apszFields[0]), apszFields[0]));
1574 else
1575 break;
1576 }
1577
1578 RTMemFree(pszzStrings);
1579 }
1580 else
1581 LogRel(("VGDrvCommonReadConfigurationFromHost: failed to enumerate '%s': %Rrc\n", g_szzPattern, rc));
1582
1583 }
1584 else
1585 LogRel(("VGDrvCommonReadConfigurationFromHost: failed to connect: %Rrc\n", rc));
1586 }
1587 else
1588 LogRel(("VGDrvCommonReadConfigurationFromHost: failed to connect: %Rrc\n", rc));
1589}
1590
1591
1592/**
1593 * Destroys the VBoxGuest device extension.
1594 *
1595 * The native code should call this before the driver is unloaded,
1596 * but don't call this on shutdown.
1597 *
1598 * @param pDevExt The device extension.
1599 */
1600void VGDrvCommonDeleteDevExt(PVBOXGUESTDEVEXT pDevExt)
1601{
1602 Log(("VGDrvCommonDeleteDevExt:\n"));
1603 Log(("VBoxGuest: The additions driver is terminating.\n"));
1604 VGDrvCommonDeleteDevExtResources(pDevExt);
1605 VGDrvCommonDeleteDevExtFundament(pDevExt);
1606 VGDrvCommonDestroyLoggers();
1607}
1608
1609
1610/**
1611 * Creates a VBoxGuest user session.
1612 *
1613 * The native code calls this when a ring-3 client opens the device.
1614 * Use VGDrvCommonCreateKernelSession when a ring-0 client connects.
1615 *
1616 * @returns VBox status code.
1617 * @param pDevExt The device extension.
1618 * @param fRequestor VMMDEV_REQUESTOR_XXX.
1619 * @param ppSession Where to store the session on success.
1620 */
1621int VGDrvCommonCreateUserSession(PVBOXGUESTDEVEXT pDevExt, uint32_t fRequestor, PVBOXGUESTSESSION *ppSession)
1622{
1623 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)RTMemAllocZ(sizeof(*pSession));
1624 if (RT_UNLIKELY(!pSession))
1625 {
1626 LogRel(("VGDrvCommonCreateUserSession: no memory!\n"));
1627 return VERR_NO_MEMORY;
1628 }
1629
1630 pSession->Process = RTProcSelf();
1631 pSession->R0Process = RTR0ProcHandleSelf();
1632 pSession->pDevExt = pDevExt;
1633 pSession->fRequestor = fRequestor;
1634 pSession->fUserSession = RT_BOOL(fRequestor & VMMDEV_REQUESTOR_USER_DEVICE);
1635 RTSpinlockAcquire(pDevExt->SessionSpinlock);
1636 RTListAppend(&pDevExt->SessionList, &pSession->ListNode);
1637 pDevExt->cSessions++;
1638 RTSpinlockRelease(pDevExt->SessionSpinlock);
1639
1640 *ppSession = pSession;
1641 LogFlow(("VGDrvCommonCreateUserSession: pSession=%p proc=%RTproc (%d) r0proc=%p\n",
1642 pSession, pSession->Process, (int)pSession->Process, (uintptr_t)pSession->R0Process)); /** @todo %RTr0proc */
1643 return VINF_SUCCESS;
1644}
1645
1646
1647/**
1648 * Creates a VBoxGuest kernel session.
1649 *
1650 * The native code calls this when a ring-0 client connects to the device.
1651 * Use VGDrvCommonCreateUserSession when a ring-3 client opens the device.
1652 *
1653 * @returns VBox status code.
1654 * @param pDevExt The device extension.
1655 * @param ppSession Where to store the session on success.
1656 */
1657int VGDrvCommonCreateKernelSession(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION *ppSession)
1658{
1659 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)RTMemAllocZ(sizeof(*pSession));
1660 if (RT_UNLIKELY(!pSession))
1661 {
1662 LogRel(("VGDrvCommonCreateKernelSession: no memory!\n"));
1663 return VERR_NO_MEMORY;
1664 }
1665
1666 pSession->Process = NIL_RTPROCESS;
1667 pSession->R0Process = NIL_RTR0PROCESS;
1668 pSession->pDevExt = pDevExt;
1669 pSession->fRequestor = VMMDEV_REQUESTOR_KERNEL | VMMDEV_REQUESTOR_USR_DRV_OTHER
1670 | VMMDEV_REQUESTOR_CON_DONT_KNOW | VMMDEV_REQUESTOR_TRUST_NOT_GIVEN;
1671 RTSpinlockAcquire(pDevExt->SessionSpinlock);
1672 RTListAppend(&pDevExt->SessionList, &pSession->ListNode);
1673 pDevExt->cSessions++;
1674 RTSpinlockRelease(pDevExt->SessionSpinlock);
1675
1676 *ppSession = pSession;
1677 LogFlow(("VGDrvCommonCreateKernelSession: pSession=%p proc=%RTproc (%d) r0proc=%p\n",
1678 pSession, pSession->Process, (int)pSession->Process, (uintptr_t)pSession->R0Process)); /** @todo %RTr0proc */
1679 return VINF_SUCCESS;
1680}
1681
1682
1683/**
1684 * Closes a VBoxGuest session.
1685 *
1686 * @param pDevExt The device extension.
1687 * @param pSession The session to close (and free).
1688 */
1689void VGDrvCommonCloseSession(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession)
1690{
1691#ifdef VBOX_WITH_HGCM
1692 unsigned i;
1693#endif
1694 LogFlow(("VGDrvCommonCloseSession: pSession=%p proc=%RTproc (%d) r0proc=%p\n",
1695 pSession, pSession->Process, (int)pSession->Process, (uintptr_t)pSession->R0Process)); /** @todo %RTr0proc */
1696
1697 RTSpinlockAcquire(pDevExt->SessionSpinlock);
1698 RTListNodeRemove(&pSession->ListNode);
1699 pDevExt->cSessions--;
1700 RTSpinlockRelease(pDevExt->SessionSpinlock);
1701 vgdrvAcquireSessionCapabilities(pDevExt, pSession, 0, UINT32_MAX, VBGL_IOC_AGC_FLAGS_DEFAULT, true /*fSessionTermination*/);
1702 vgdrvSetSessionCapabilities(pDevExt, pSession, 0 /*fOrMask*/, UINT32_MAX /*fNotMask*/,
1703 NULL /*pfSessionCaps*/, NULL /*pfGlobalCaps*/, true /*fSessionTermination*/);
1704 vgdrvSetSessionEventFilter(pDevExt, pSession, 0 /*fOrMask*/, UINT32_MAX /*fNotMask*/, true /*fSessionTermination*/);
1705 vgdrvSetSessionMouseStatus(pDevExt, pSession, 0 /*fOrMask*/, UINT32_MAX /*fNotMask*/, true /*fSessionTermination*/);
1706
1707 vgdrvIoCtl_CancelAllWaitEvents(pDevExt, pSession);
1708
1709#ifdef VBOX_WITH_HGCM
1710 for (i = 0; i < RT_ELEMENTS(pSession->aHGCMClientIds); i++)
1711 if (pSession->aHGCMClientIds[i])
1712 {
1713 uint32_t idClient = pSession->aHGCMClientIds[i];
1714 pSession->aHGCMClientIds[i] = 0;
1715 Log(("VGDrvCommonCloseSession: disconnecting client id %#RX32\n", idClient));
1716 VbglR0HGCMInternalDisconnect(idClient, VMMDEV_REQUESTOR_KERNEL | VMMDEV_REQUESTOR_USR_DRV,
1717 vgdrvHgcmAsyncWaitCallback, pDevExt, RT_INDEFINITE_WAIT);
1718 }
1719#endif
1720
1721 pSession->pDevExt = NULL;
1722 pSession->Process = NIL_RTPROCESS;
1723 pSession->R0Process = NIL_RTR0PROCESS;
1724 vgdrvCloseMemBalloon(pDevExt, pSession);
1725 RTMemFree(pSession);
1726}
1727
1728
1729/**
1730 * Allocates a wait-for-event entry.
1731 *
1732 * @returns The wait-for-event entry.
1733 * @param pDevExt The device extension.
1734 * @param pSession The session that's allocating this. Can be NULL.
1735 */
1736static PVBOXGUESTWAIT vgdrvWaitAlloc(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession)
1737{
1738 /*
1739 * Allocate it one way or the other.
1740 */
1741 PVBOXGUESTWAIT pWait = RTListGetFirst(&pDevExt->FreeList, VBOXGUESTWAIT, ListNode);
1742 if (pWait)
1743 {
1744 RTSpinlockAcquire(pDevExt->EventSpinlock);
1745
1746 pWait = RTListGetFirst(&pDevExt->FreeList, VBOXGUESTWAIT, ListNode);
1747 if (pWait)
1748 RTListNodeRemove(&pWait->ListNode);
1749
1750 RTSpinlockRelease(pDevExt->EventSpinlock);
1751 }
1752 if (!pWait)
1753 {
1754 int rc;
1755
1756 pWait = (PVBOXGUESTWAIT)RTMemAlloc(sizeof(*pWait));
1757 if (!pWait)
1758 {
1759 LogRelMax(32, ("vgdrvWaitAlloc: out-of-memory!\n"));
1760 return NULL;
1761 }
1762
1763 rc = RTSemEventMultiCreate(&pWait->Event);
1764 if (RT_FAILURE(rc))
1765 {
1766 LogRelMax(32, ("vgdrvWaitAlloc: RTSemEventMultiCreate failed with rc=%Rrc!\n", rc));
1767 RTMemFree(pWait);
1768 return NULL;
1769 }
1770
1771 pWait->ListNode.pNext = NULL;
1772 pWait->ListNode.pPrev = NULL;
1773 }
1774
1775 /*
1776 * Zero members just as an precaution.
1777 */
1778 pWait->fReqEvents = 0;
1779 pWait->fResEvents = 0;
1780#ifdef VBOXGUEST_USE_DEFERRED_WAKE_UP
1781 pWait->fPendingWakeUp = false;
1782 pWait->fFreeMe = false;
1783#endif
1784 pWait->pSession = pSession;
1785#ifdef VBOX_WITH_HGCM
1786 pWait->pHGCMReq = NULL;
1787#endif
1788 RTSemEventMultiReset(pWait->Event);
1789 return pWait;
1790}
1791
1792
1793/**
1794 * Frees the wait-for-event entry.
1795 *
1796 * The caller must own the wait spinlock !
1797 * The entry must be in a list!
1798 *
1799 * @param pDevExt The device extension.
1800 * @param pWait The wait-for-event entry to free.
1801 */
1802static void vgdrvWaitFreeLocked(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTWAIT pWait)
1803{
1804 pWait->fReqEvents = 0;
1805 pWait->fResEvents = 0;
1806#ifdef VBOX_WITH_HGCM
1807 pWait->pHGCMReq = NULL;
1808#endif
1809#ifdef VBOXGUEST_USE_DEFERRED_WAKE_UP
1810 Assert(!pWait->fFreeMe);
1811 if (pWait->fPendingWakeUp)
1812 pWait->fFreeMe = true;
1813 else
1814#endif
1815 {
1816 RTListNodeRemove(&pWait->ListNode);
1817 RTListAppend(&pDevExt->FreeList, &pWait->ListNode);
1818 }
1819}
1820
1821
1822/**
1823 * Frees the wait-for-event entry.
1824 *
1825 * @param pDevExt The device extension.
1826 * @param pWait The wait-for-event entry to free.
1827 */
1828static void vgdrvWaitFreeUnlocked(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTWAIT pWait)
1829{
1830 RTSpinlockAcquire(pDevExt->EventSpinlock);
1831 vgdrvWaitFreeLocked(pDevExt, pWait);
1832 RTSpinlockRelease(pDevExt->EventSpinlock);
1833}
1834
1835
1836#ifdef VBOXGUEST_USE_DEFERRED_WAKE_UP
1837/**
1838 * Processes the wake-up list.
1839 *
1840 * All entries in the wake-up list gets signalled and moved to the woken-up
1841 * list.
1842 * At least on Windows this function can be invoked concurrently from
1843 * different VCPUs. So, be thread-safe.
1844 *
1845 * @param pDevExt The device extension.
1846 */
1847void VGDrvCommonWaitDoWakeUps(PVBOXGUESTDEVEXT pDevExt)
1848{
1849 if (!RTListIsEmpty(&pDevExt->WakeUpList))
1850 {
1851 RTSpinlockAcquire(pDevExt->EventSpinlock);
1852 for (;;)
1853 {
1854 int rc;
1855 PVBOXGUESTWAIT pWait = RTListGetFirst(&pDevExt->WakeUpList, VBOXGUESTWAIT, ListNode);
1856 if (!pWait)
1857 break;
1858 /* Prevent other threads from accessing pWait when spinlock is released. */
1859 RTListNodeRemove(&pWait->ListNode);
1860
1861 pWait->fPendingWakeUp = true;
1862 RTSpinlockRelease(pDevExt->EventSpinlock);
1863
1864 rc = RTSemEventMultiSignal(pWait->Event);
1865 AssertRC(rc);
1866
1867 RTSpinlockAcquire(pDevExt->EventSpinlock);
1868 Assert(pWait->ListNode.pNext == NULL && pWait->ListNode.pPrev == NULL);
1869 RTListAppend(&pDevExt->WokenUpList, &pWait->ListNode);
1870 pWait->fPendingWakeUp = false;
1871 if (RT_LIKELY(!pWait->fFreeMe))
1872 { /* likely */ }
1873 else
1874 {
1875 pWait->fFreeMe = false;
1876 vgdrvWaitFreeLocked(pDevExt, pWait);
1877 }
1878 }
1879 RTSpinlockRelease(pDevExt->EventSpinlock);
1880 }
1881}
1882#endif /* VBOXGUEST_USE_DEFERRED_WAKE_UP */
1883
1884
1885/**
1886 * Implements the fast (no input or output) type of IOCtls.
1887 *
1888 * This is currently just a placeholder stub inherited from the support driver code.
1889 *
1890 * @returns VBox status code.
1891 * @param iFunction The IOCtl function number.
1892 * @param pDevExt The device extension.
1893 * @param pSession The session.
1894 */
1895int VGDrvCommonIoCtlFast(uintptr_t iFunction, PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession)
1896{
1897 LogFlow(("VGDrvCommonIoCtlFast: iFunction=%#x pDevExt=%p pSession=%p\n", iFunction, pDevExt, pSession));
1898
1899 NOREF(iFunction);
1900 NOREF(pDevExt);
1901 NOREF(pSession);
1902 return VERR_NOT_SUPPORTED;
1903}
1904
1905
1906/**
1907 * Gets the driver I/O control interface version, maybe adjusting it for
1908 * backwards compatibility.
1909 *
1910 * The adjusting is currently not implemented as we only have one major I/O
1911 * control interface version out there to support. This is something we will
1912 * implement as needed.
1913 *
1914 * returns IPRT status code.
1915 * @param pDevExt The device extension.
1916 * @param pSession The session.
1917 * @param pReq The request info.
1918 */
1919static int vgdrvIoCtl_DriverVersionInfo(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession, PVBGLIOCDRIVERVERSIONINFO pReq)
1920{
1921 int rc;
1922 LogFlow(("VBGL_IOCTL_DRIVER_VERSION_INFO: uReqVersion=%#x uMinVersion=%#x uReserved1=%#x uReserved2=%#x\n",
1923 pReq->u.In.uReqVersion, pReq->u.In.uMinVersion, pReq->u.In.uReserved1, pReq->u.In.uReserved2));
1924 RT_NOREF2(pDevExt, pSession);
1925
1926 /*
1927 * Input validation.
1928 */
1929 if ( pReq->u.In.uMinVersion <= pReq->u.In.uReqVersion
1930 && RT_HI_U16(pReq->u.In.uMinVersion) == RT_HI_U16(pReq->u.In.uReqVersion))
1931 {
1932 /*
1933 * Match the version.
1934 * The current logic is very simple, match the major interface version.
1935 */
1936 if ( pReq->u.In.uMinVersion <= VBGL_IOC_VERSION
1937 && RT_HI_U16(pReq->u.In.uMinVersion) == RT_HI_U16(VBGL_IOC_VERSION))
1938 rc = VINF_SUCCESS;
1939 else
1940 {
1941 LogRel(("VBGL_IOCTL_DRIVER_VERSION_INFO: Version mismatch. Requested: %#x Min: %#x Current: %#x\n",
1942 pReq->u.In.uReqVersion, pReq->u.In.uMinVersion, VBGL_IOC_VERSION));
1943 rc = VERR_VERSION_MISMATCH;
1944 }
1945 }
1946 else
1947 {
1948 LogRel(("VBGL_IOCTL_DRIVER_VERSION_INFO: uMinVersion=%#x uMaxVersion=%#x doesn't match!\n",
1949 pReq->u.In.uMinVersion, pReq->u.In.uReqVersion));
1950 rc = VERR_INVALID_PARAMETER;
1951 }
1952
1953 pReq->u.Out.uSessionVersion = RT_SUCCESS(rc) ? VBGL_IOC_VERSION : UINT32_MAX;
1954 pReq->u.Out.uDriverVersion = VBGL_IOC_VERSION;
1955 pReq->u.Out.uDriverRevision = VBOX_SVN_REV;
1956 pReq->u.Out.uReserved1 = 0;
1957 pReq->u.Out.uReserved2 = 0;
1958 return rc;
1959}
1960
1961
1962/**
1963 * Similar to vgdrvIoCtl_DriverVersionInfo, except its for IDC.
1964 *
1965 * returns IPRT status code.
1966 * @param pDevExt The device extension.
1967 * @param pSession The session.
1968 * @param pReq The request info.
1969 */
1970static int vgdrvIoCtl_IdcConnect(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession, PVBGLIOCIDCCONNECT pReq)
1971{
1972 int rc;
1973 LogFlow(("VBGL_IOCTL_IDC_CONNECT: u32MagicCookie=%#x uReqVersion=%#x uMinVersion=%#x uReserved=%#x\n",
1974 pReq->u.In.u32MagicCookie, pReq->u.In.uReqVersion, pReq->u.In.uMinVersion, pReq->u.In.uReserved));
1975 Assert(pSession != NULL);
1976 RT_NOREF(pDevExt);
1977
1978 /*
1979 * Input validation.
1980 */
1981 if (pReq->u.In.u32MagicCookie == VBGL_IOCTL_IDC_CONNECT_MAGIC_COOKIE)
1982 {
1983 if ( pReq->u.In.uMinVersion <= pReq->u.In.uReqVersion
1984 && RT_HI_U16(pReq->u.In.uMinVersion) == RT_HI_U16(pReq->u.In.uReqVersion))
1985 {
1986 /*
1987 * Match the version.
1988 * The current logic is very simple, match the major interface version.
1989 */
1990 if ( pReq->u.In.uMinVersion <= VBGL_IOC_VERSION
1991 && RT_HI_U16(pReq->u.In.uMinVersion) == RT_HI_U16(VBGL_IOC_VERSION))
1992 {
1993 pReq->u.Out.pvSession = pSession;
1994 pReq->u.Out.uSessionVersion = VBGL_IOC_VERSION;
1995 pReq->u.Out.uDriverVersion = VBGL_IOC_VERSION;
1996 pReq->u.Out.uDriverRevision = VBOX_SVN_REV;
1997 pReq->u.Out.uReserved1 = 0;
1998 pReq->u.Out.pvReserved2 = NULL;
1999 return VINF_SUCCESS;
2000
2001 }
2002 LogRel(("VBGL_IOCTL_IDC_CONNECT: Version mismatch. Requested: %#x Min: %#x Current: %#x\n",
2003 pReq->u.In.uReqVersion, pReq->u.In.uMinVersion, VBGL_IOC_VERSION));
2004 rc = VERR_VERSION_MISMATCH;
2005 }
2006 else
2007 {
2008 LogRel(("VBGL_IOCTL_IDC_CONNECT: uMinVersion=%#x uMaxVersion=%#x doesn't match!\n",
2009 pReq->u.In.uMinVersion, pReq->u.In.uReqVersion));
2010 rc = VERR_INVALID_PARAMETER;
2011 }
2012
2013 pReq->u.Out.pvSession = NULL;
2014 pReq->u.Out.uSessionVersion = UINT32_MAX;
2015 pReq->u.Out.uDriverVersion = VBGL_IOC_VERSION;
2016 pReq->u.Out.uDriverRevision = VBOX_SVN_REV;
2017 pReq->u.Out.uReserved1 = 0;
2018 pReq->u.Out.pvReserved2 = NULL;
2019 }
2020 else
2021 {
2022 LogRel(("VBGL_IOCTL_IDC_CONNECT: u32MagicCookie=%#x expected %#x!\n",
2023 pReq->u.In.u32MagicCookie, VBGL_IOCTL_IDC_CONNECT_MAGIC_COOKIE));
2024 rc = VERR_INVALID_PARAMETER;
2025 }
2026 return rc;
2027}
2028
2029
2030/**
2031 * Counterpart to vgdrvIoCtl_IdcConnect, destroys the session.
2032 *
2033 * returns IPRT status code.
2034 * @param pDevExt The device extension.
2035 * @param pSession The session.
2036 * @param pReq The request info.
2037 */
2038static int vgdrvIoCtl_IdcDisconnect(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession, PVBGLIOCIDCDISCONNECT pReq)
2039{
2040 LogFlow(("VBGL_IOCTL_IDC_DISCONNECT: pvSession=%p vs pSession=%p\n", pReq->u.In.pvSession, pSession));
2041 RT_NOREF(pDevExt);
2042 Assert(pSession != NULL);
2043
2044 if (pReq->u.In.pvSession == pSession)
2045 {
2046 VGDrvCommonCloseSession(pDevExt, pSession);
2047 return VINF_SUCCESS;
2048 }
2049 LogRel(("VBGL_IOCTL_IDC_DISCONNECT: In.pvSession=%p is not equal to pSession=%p!\n", pReq->u.In.pvSession, pSession));
2050 return VERR_INVALID_PARAMETER;
2051}
2052
2053
2054/**
2055 * Return the VMM device I/O info.
2056 *
2057 * returns IPRT status code.
2058 * @param pDevExt The device extension.
2059 * @param pInfo The request info.
2060 * @note Ring-0 only, caller checked.
2061 */
2062static int vgdrvIoCtl_GetVMMDevIoInfo(PVBOXGUESTDEVEXT pDevExt, PVBGLIOCGETVMMDEVIOINFO pInfo)
2063{
2064 LogFlow(("VBGL_IOCTL_GET_VMMDEV_IO_INFO\n"));
2065
2066 pInfo->u.Out.IoPort = pDevExt->IOPortBase;
2067 pInfo->u.Out.pvVmmDevMapping = pDevExt->pVMMDevMemory;
2068 pInfo->u.Out.auPadding[0] = 0;
2069#if HC_ARCH_BITS != 32
2070 pInfo->u.Out.auPadding[1] = 0;
2071 pInfo->u.Out.auPadding[2] = 0;
2072#endif
2073 return VINF_SUCCESS;
2074}
2075
2076
2077/**
2078 * Set the callback for the kernel mouse handler.
2079 *
2080 * returns IPRT status code.
2081 * @param pDevExt The device extension.
2082 * @param pNotify The new callback information.
2083 */
2084int vgdrvIoCtl_SetMouseNotifyCallback(PVBOXGUESTDEVEXT pDevExt, PVBGLIOCSETMOUSENOTIFYCALLBACK pNotify)
2085{
2086 LogFlow(("VBOXGUEST_IOCTL_SET_MOUSE_NOTIFY_CALLBACK: pfnNotify=%p pvUser=%p\n", pNotify->u.In.pfnNotify, pNotify->u.In.pvUser));
2087
2088#ifdef VBOXGUEST_MOUSE_NOTIFY_CAN_PREEMPT
2089 VGDrvNativeSetMouseNotifyCallback(pDevExt, pNotify);
2090#else
2091 RTSpinlockAcquire(pDevExt->EventSpinlock);
2092 pDevExt->pfnMouseNotifyCallback = pNotify->u.In.pfnNotify;
2093 pDevExt->pvMouseNotifyCallbackArg = pNotify->u.In.pvUser;
2094 RTSpinlockRelease(pDevExt->EventSpinlock);
2095#endif
2096 return VINF_SUCCESS;
2097}
2098
2099
2100/**
2101 * Worker vgdrvIoCtl_WaitEvent.
2102 *
2103 * The caller enters the spinlock, we leave it.
2104 *
2105 * @returns VINF_SUCCESS if we've left the spinlock and can return immediately.
2106 */
2107DECLINLINE(int) vbdgCheckWaitEventCondition(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession,
2108 PVBGLIOCWAITFOREVENTS pInfo, int iEvent, const uint32_t fReqEvents)
2109{
2110 uint32_t fMatches = pDevExt->f32PendingEvents & fReqEvents;
2111 if (fMatches & VBOXGUEST_ACQUIRE_STYLE_EVENTS)
2112 fMatches &= vgdrvGetAllowedEventMaskForSession(pDevExt, pSession);
2113 if (fMatches || pSession->fPendingCancelWaitEvents)
2114 {
2115 ASMAtomicAndU32(&pDevExt->f32PendingEvents, ~fMatches);
2116 RTSpinlockRelease(pDevExt->EventSpinlock);
2117
2118 pInfo->u.Out.fEvents = fMatches;
2119 if (fReqEvents & ~((uint32_t)1 << iEvent))
2120 LogFlow(("VBOXGUEST_IOCTL_WAITEVENT: returns %#x\n", pInfo->u.Out.fEvents));
2121 else
2122 LogFlow(("VBOXGUEST_IOCTL_WAITEVENT: returns %#x/%d\n", pInfo->u.Out.fEvents, iEvent));
2123 pSession->fPendingCancelWaitEvents = false;
2124 return VINF_SUCCESS;
2125 }
2126
2127 RTSpinlockRelease(pDevExt->EventSpinlock);
2128 return VERR_TIMEOUT;
2129}
2130
2131
2132static int vgdrvIoCtl_WaitForEvents(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession,
2133 PVBGLIOCWAITFOREVENTS pInfo, bool fInterruptible)
2134{
2135 uint32_t const cMsTimeout = pInfo->u.In.cMsTimeOut;
2136 const uint32_t fReqEvents = pInfo->u.In.fEvents;
2137 uint32_t fResEvents;
2138 int iEvent;
2139 PVBOXGUESTWAIT pWait;
2140 int rc;
2141
2142 pInfo->u.Out.fEvents = 0; /* Note! This overwrites pInfo->u.In.* fields! */
2143
2144 /*
2145 * Copy and verify the input mask.
2146 */
2147 iEvent = ASMBitFirstSetU32(fReqEvents) - 1;
2148 if (RT_UNLIKELY(iEvent < 0))
2149 {
2150 LogRel(("VBOXGUEST_IOCTL_WAITEVENT: Invalid input mask %#x!!\n", fReqEvents));
2151 return VERR_INVALID_PARAMETER;
2152 }
2153
2154 /*
2155 * Check the condition up front, before doing the wait-for-event allocations.
2156 */
2157 RTSpinlockAcquire(pDevExt->EventSpinlock);
2158 rc = vbdgCheckWaitEventCondition(pDevExt, pSession, pInfo, iEvent, fReqEvents);
2159 if (rc == VINF_SUCCESS)
2160 return rc;
2161
2162 if (!cMsTimeout)
2163 {
2164 LogFlow(("VBOXGUEST_IOCTL_WAITEVENT: returns VERR_TIMEOUT\n"));
2165 return VERR_TIMEOUT;
2166 }
2167
2168 pWait = vgdrvWaitAlloc(pDevExt, pSession);
2169 if (!pWait)
2170 return VERR_NO_MEMORY;
2171 pWait->fReqEvents = fReqEvents;
2172
2173 /*
2174 * We've got the wait entry now, re-enter the spinlock and check for the condition.
2175 * If the wait condition is met, return.
2176 * Otherwise enter into the list and go to sleep waiting for the ISR to signal us.
2177 */
2178 RTSpinlockAcquire(pDevExt->EventSpinlock);
2179 RTListAppend(&pDevExt->WaitList, &pWait->ListNode);
2180 rc = vbdgCheckWaitEventCondition(pDevExt, pSession, pInfo, iEvent, fReqEvents);
2181 if (rc == VINF_SUCCESS)
2182 {
2183 vgdrvWaitFreeUnlocked(pDevExt, pWait);
2184 return rc;
2185 }
2186
2187 if (fInterruptible)
2188 rc = RTSemEventMultiWaitNoResume(pWait->Event, cMsTimeout == UINT32_MAX ? RT_INDEFINITE_WAIT : cMsTimeout);
2189 else
2190 rc = RTSemEventMultiWait(pWait->Event, cMsTimeout == UINT32_MAX ? RT_INDEFINITE_WAIT : cMsTimeout);
2191
2192 /*
2193 * There is one special case here and that's when the semaphore is
2194 * destroyed upon device driver unload. This shouldn't happen of course,
2195 * but in case it does, just get out of here ASAP.
2196 */
2197 if (rc == VERR_SEM_DESTROYED)
2198 return rc;
2199
2200 /*
2201 * Unlink the wait item and dispose of it.
2202 */
2203 RTSpinlockAcquire(pDevExt->EventSpinlock);
2204 fResEvents = pWait->fResEvents;
2205 vgdrvWaitFreeLocked(pDevExt, pWait);
2206 RTSpinlockRelease(pDevExt->EventSpinlock);
2207
2208 /*
2209 * Now deal with the return code.
2210 */
2211 if ( fResEvents
2212 && fResEvents != UINT32_MAX)
2213 {
2214 pInfo->u.Out.fEvents = fResEvents;
2215 if (fReqEvents & ~((uint32_t)1 << iEvent))
2216 LogFlow(("VBOXGUEST_IOCTL_WAITEVENT: returns %#x\n", pInfo->u.Out.fEvents));
2217 else
2218 LogFlow(("VBOXGUEST_IOCTL_WAITEVENT: returns %#x/%d\n", pInfo->u.Out.fEvents, iEvent));
2219 rc = VINF_SUCCESS;
2220 }
2221 else if ( fResEvents == UINT32_MAX
2222 || rc == VERR_INTERRUPTED)
2223 {
2224 rc = VERR_INTERRUPTED;
2225 LogFlow(("VBOXGUEST_IOCTL_WAITEVENT: returns VERR_INTERRUPTED\n"));
2226 }
2227 else if (rc == VERR_TIMEOUT)
2228 LogFlow(("VBOXGUEST_IOCTL_WAITEVENT: returns VERR_TIMEOUT (2)\n"));
2229 else
2230 {
2231 if (RT_SUCCESS(rc))
2232 {
2233 LogRelMax(32, ("VBOXGUEST_IOCTL_WAITEVENT: returns %Rrc but no events!\n", rc));
2234 rc = VERR_INTERNAL_ERROR;
2235 }
2236 LogFlow(("VBOXGUEST_IOCTL_WAITEVENT: returns %Rrc\n", rc));
2237 }
2238
2239 return rc;
2240}
2241
2242
2243/** @todo the semantics of this IoCtl have been tightened, so that no calls to
2244 * VBOXGUEST_IOCTL_WAITEVENT are allowed in a session after it has been
2245 * called. Change the code to make calls to VBOXGUEST_IOCTL_WAITEVENT made
2246 * after that to return VERR_INTERRUPTED or something appropriate. */
2247static int vgdrvIoCtl_CancelAllWaitEvents(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession)
2248{
2249 PVBOXGUESTWAIT pWait;
2250 PVBOXGUESTWAIT pSafe;
2251 int rc = 0;
2252 /* Was as least one WAITEVENT in process for this session? If not we
2253 * set a flag that the next call should be interrupted immediately. This
2254 * is needed so that a user thread can reliably interrupt another one in a
2255 * WAITEVENT loop. */
2256 bool fCancelledOne = false;
2257
2258 LogFlow(("VBOXGUEST_IOCTL_CANCEL_ALL_WAITEVENTS\n"));
2259
2260 /*
2261 * Walk the event list and wake up anyone with a matching session.
2262 */
2263 RTSpinlockAcquire(pDevExt->EventSpinlock);
2264 RTListForEachSafe(&pDevExt->WaitList, pWait, pSafe, VBOXGUESTWAIT, ListNode)
2265 {
2266 if (pWait->pSession == pSession)
2267 {
2268 fCancelledOne = true;
2269 pWait->fResEvents = UINT32_MAX;
2270 RTListNodeRemove(&pWait->ListNode);
2271#ifdef VBOXGUEST_USE_DEFERRED_WAKE_UP
2272 RTListAppend(&pDevExt->WakeUpList, &pWait->ListNode);
2273#else
2274 rc |= RTSemEventMultiSignal(pWait->Event);
2275 RTListAppend(&pDevExt->WokenUpList, &pWait->ListNode);
2276#endif
2277 }
2278 }
2279 if (!fCancelledOne)
2280 pSession->fPendingCancelWaitEvents = true;
2281 RTSpinlockRelease(pDevExt->EventSpinlock);
2282 Assert(rc == 0);
2283 NOREF(rc);
2284
2285#ifdef VBOXGUEST_USE_DEFERRED_WAKE_UP
2286 VGDrvCommonWaitDoWakeUps(pDevExt);
2287#endif
2288
2289 return VINF_SUCCESS;
2290}
2291
2292
2293/**
2294 * Checks if the VMM request is allowed in the context of the given session.
2295 *
2296 * @returns VINF_SUCCESS or VERR_PERMISSION_DENIED.
2297 * @param pDevExt The device extension.
2298 * @param pSession The calling session.
2299 * @param enmType The request type.
2300 * @param pReqHdr The request.
2301 */
2302static int vgdrvCheckIfVmmReqIsAllowed(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession, VMMDevRequestType enmType,
2303 VMMDevRequestHeader const *pReqHdr)
2304{
2305 /*
2306 * Categorize the request being made.
2307 */
2308 /** @todo This need quite some more work! */
2309 enum
2310 {
2311 kLevel_Invalid, kLevel_NoOne, kLevel_OnlyVBoxGuest, kLevel_OnlyKernel, kLevel_TrustedUsers, kLevel_AllUsers
2312 } enmRequired;
2313 RT_NOREF1(pDevExt);
2314
2315 switch (enmType)
2316 {
2317 /*
2318 * Deny access to anything we don't know or provide specialized I/O controls for.
2319 */
2320#ifdef VBOX_WITH_HGCM
2321 case VMMDevReq_HGCMConnect:
2322 case VMMDevReq_HGCMDisconnect:
2323# ifdef VBOX_WITH_64_BITS_GUESTS
2324 case VMMDevReq_HGCMCall32:
2325 case VMMDevReq_HGCMCall64:
2326# else
2327 case VMMDevReq_HGCMCall:
2328# endif /* VBOX_WITH_64_BITS_GUESTS */
2329 case VMMDevReq_HGCMCancel:
2330 case VMMDevReq_HGCMCancel2:
2331#endif /* VBOX_WITH_HGCM */
2332 case VMMDevReq_SetGuestCapabilities:
2333 default:
2334 enmRequired = kLevel_NoOne;
2335 break;
2336
2337 /*
2338 * There are a few things only this driver can do (and it doesn't use
2339 * the VMMRequst I/O control route anyway, but whatever).
2340 */
2341 case VMMDevReq_ReportGuestInfo:
2342 case VMMDevReq_ReportGuestInfo2:
2343 case VMMDevReq_GetHypervisorInfo:
2344 case VMMDevReq_SetHypervisorInfo:
2345 case VMMDevReq_RegisterPatchMemory:
2346 case VMMDevReq_DeregisterPatchMemory:
2347 case VMMDevReq_GetMemBalloonChangeRequest:
2348 enmRequired = kLevel_OnlyVBoxGuest;
2349 break;
2350
2351 /*
2352 * Trusted users apps only.
2353 */
2354 case VMMDevReq_QueryCredentials:
2355 case VMMDevReq_ReportCredentialsJudgement:
2356 case VMMDevReq_RegisterSharedModule:
2357 case VMMDevReq_UnregisterSharedModule:
2358 case VMMDevReq_WriteCoreDump:
2359 case VMMDevReq_GetCpuHotPlugRequest:
2360 case VMMDevReq_SetCpuHotPlugStatus:
2361 case VMMDevReq_CheckSharedModules:
2362 case VMMDevReq_GetPageSharingStatus:
2363 case VMMDevReq_DebugIsPageShared:
2364 case VMMDevReq_ReportGuestStats:
2365 case VMMDevReq_ReportGuestUserState:
2366 case VMMDevReq_GetStatisticsChangeRequest:
2367 case VMMDevReq_ChangeMemBalloon:
2368 enmRequired = kLevel_TrustedUsers;
2369 break;
2370
2371 /*
2372 * Anyone.
2373 */
2374 case VMMDevReq_GetMouseStatus:
2375 case VMMDevReq_SetMouseStatus:
2376 case VMMDevReq_SetPointerShape:
2377 case VMMDevReq_GetHostVersion:
2378 case VMMDevReq_Idle:
2379 case VMMDevReq_GetHostTime:
2380 case VMMDevReq_SetPowerStatus:
2381 case VMMDevReq_AcknowledgeEvents:
2382 case VMMDevReq_CtlGuestFilterMask:
2383 case VMMDevReq_ReportGuestStatus:
2384 case VMMDevReq_GetDisplayChangeRequest:
2385 case VMMDevReq_VideoModeSupported:
2386 case VMMDevReq_GetHeightReduction:
2387 case VMMDevReq_GetDisplayChangeRequest2:
2388 case VMMDevReq_VideoModeSupported2:
2389 case VMMDevReq_VideoAccelEnable:
2390 case VMMDevReq_VideoAccelFlush:
2391 case VMMDevReq_VideoSetVisibleRegion:
2392 case VMMDevReq_GetDisplayChangeRequestEx:
2393 case VMMDevReq_GetDisplayChangeRequestMulti:
2394 case VMMDevReq_GetSeamlessChangeRequest:
2395 case VMMDevReq_GetVRDPChangeRequest:
2396 case VMMDevReq_LogString:
2397 case VMMDevReq_GetSessionId:
2398 enmRequired = kLevel_AllUsers;
2399 break;
2400
2401 /*
2402 * Depends on the request parameters...
2403 */
2404 /** @todo this have to be changed into an I/O control and the facilities
2405 * tracked in the session so they can automatically be failed when the
2406 * session terminates without reporting the new status.
2407 *
2408 * The information presented by IGuest is not reliable without this! */
2409 case VMMDevReq_ReportGuestCapabilities:
2410 switch (((VMMDevReportGuestStatus const *)pReqHdr)->guestStatus.facility)
2411 {
2412 case VBoxGuestFacilityType_All:
2413 case VBoxGuestFacilityType_VBoxGuestDriver:
2414 enmRequired = kLevel_OnlyVBoxGuest;
2415 break;
2416 case VBoxGuestFacilityType_VBoxService:
2417 enmRequired = kLevel_TrustedUsers;
2418 break;
2419 case VBoxGuestFacilityType_VBoxTrayClient:
2420 case VBoxGuestFacilityType_Seamless:
2421 case VBoxGuestFacilityType_Graphics:
2422 default:
2423 enmRequired = kLevel_AllUsers;
2424 break;
2425 }
2426 break;
2427 }
2428
2429 /*
2430 * Check against the session.
2431 */
2432 switch (enmRequired)
2433 {
2434 default:
2435 case kLevel_NoOne:
2436 break;
2437 case kLevel_OnlyVBoxGuest:
2438 case kLevel_OnlyKernel:
2439 if (pSession->R0Process == NIL_RTR0PROCESS)
2440 return VINF_SUCCESS;
2441 break;
2442 case kLevel_TrustedUsers:
2443 if (pSession->fUserSession)
2444 break;
2445 case kLevel_AllUsers:
2446 return VINF_SUCCESS;
2447 }
2448
2449 return VERR_PERMISSION_DENIED;
2450}
2451
2452static int vgdrvIoCtl_VMMDevRequest(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession,
2453 VMMDevRequestHeader *pReqHdr, size_t cbData)
2454{
2455 int rc;
2456 VMMDevRequestHeader *pReqCopy;
2457
2458 /*
2459 * Validate the header and request size.
2460 */
2461 const VMMDevRequestType enmType = pReqHdr->requestType;
2462 const uint32_t cbReq = pReqHdr->size;
2463 const uint32_t cbMinSize = (uint32_t)vmmdevGetRequestSize(enmType);
2464
2465 LogFlow(("VBOXGUEST_IOCTL_VMMREQUEST: type %d\n", pReqHdr->requestType));
2466
2467 if (cbReq < cbMinSize)
2468 {
2469 LogRel(("VBOXGUEST_IOCTL_VMMREQUEST: invalid hdr size %#x, expected >= %#x; type=%#x!!\n",
2470 cbReq, cbMinSize, enmType));
2471 return VERR_INVALID_PARAMETER;
2472 }
2473 if (cbReq > cbData)
2474 {
2475 LogRel(("VBOXGUEST_IOCTL_VMMREQUEST: invalid size %#x, expected >= %#x (hdr); type=%#x!!\n",
2476 cbData, cbReq, enmType));
2477 return VERR_INVALID_PARAMETER;
2478 }
2479 rc = VbglGR0Verify(pReqHdr, cbData);
2480 if (RT_FAILURE(rc))
2481 {
2482 Log(("VBOXGUEST_IOCTL_VMMREQUEST: invalid header: size %#x, expected >= %#x (hdr); type=%#x; rc=%Rrc!!\n",
2483 cbData, cbReq, enmType, rc));
2484 return rc;
2485 }
2486
2487 rc = vgdrvCheckIfVmmReqIsAllowed(pDevExt, pSession, enmType, pReqHdr);
2488 if (RT_FAILURE(rc))
2489 {
2490 Log(("VBOXGUEST_IOCTL_VMMREQUEST: Operation not allowed! type=%#x rc=%Rrc\n", enmType, rc));
2491 return rc;
2492 }
2493
2494 /*
2495 * Make a copy of the request in the physical memory heap so
2496 * the VBoxGuestLibrary can more easily deal with the request.
2497 * (This is really a waste of time since the OS or the OS specific
2498 * code has already buffered or locked the input/output buffer, but
2499 * it does makes things a bit simpler wrt to phys address.)
2500 */
2501 rc = VbglR0GRAlloc(&pReqCopy, cbReq, enmType);
2502 if (RT_FAILURE(rc))
2503 {
2504 Log(("VBOXGUEST_IOCTL_VMMREQUEST: failed to allocate %u (%#x) bytes to cache the request. rc=%Rrc!!\n",
2505 cbReq, cbReq, rc));
2506 return rc;
2507 }
2508 memcpy(pReqCopy, pReqHdr, cbReq);
2509 Assert(pReqCopy->reserved1 == cbReq);
2510 pReqCopy->reserved1 = 0; /* VGDrvCommonIoCtl or caller sets cbOut, so clear it. */
2511 pReqCopy->fRequestor = pSession->fRequestor;
2512
2513 if (enmType == VMMDevReq_GetMouseStatus) /* clear poll condition. */
2514 pSession->u32MousePosChangedSeq = ASMAtomicUoReadU32(&pDevExt->u32MousePosChangedSeq);
2515
2516 rc = VbglR0GRPerform(pReqCopy);
2517 if ( RT_SUCCESS(rc)
2518 && RT_SUCCESS(pReqCopy->rc))
2519 {
2520 Assert(rc != VINF_HGCM_ASYNC_EXECUTE);
2521 Assert(pReqCopy->rc != VINF_HGCM_ASYNC_EXECUTE);
2522
2523 memcpy(pReqHdr, pReqCopy, cbReq);
2524 pReqHdr->reserved1 = cbReq; /* preserve cbOut */
2525 }
2526 else if (RT_FAILURE(rc))
2527 Log(("VBOXGUEST_IOCTL_VMMREQUEST: VbglR0GRPerform - rc=%Rrc!\n", rc));
2528 else
2529 {
2530 Log(("VBOXGUEST_IOCTL_VMMREQUEST: request execution failed; VMMDev rc=%Rrc!\n", pReqCopy->rc));
2531 rc = pReqCopy->rc;
2532 }
2533
2534 VbglR0GRFree(pReqCopy);
2535 return rc;
2536}
2537
2538
2539#ifdef VBOX_WITH_HGCM
2540
2541AssertCompile(RT_INDEFINITE_WAIT == (uint32_t)RT_INDEFINITE_WAIT); /* assumed by code below */
2542
2543/** Worker for vgdrvHgcmAsyncWaitCallback*. */
2544static int vgdrvHgcmAsyncWaitCallbackWorker(VMMDevHGCMRequestHeader volatile *pHdr, PVBOXGUESTDEVEXT pDevExt,
2545 bool fInterruptible, uint32_t cMillies)
2546{
2547 int rc;
2548
2549 /*
2550 * Check to see if the condition was met by the time we got here.
2551 *
2552 * We create a simple poll loop here for dealing with out-of-memory
2553 * conditions since the caller isn't necessarily able to deal with
2554 * us returning too early.
2555 */
2556 PVBOXGUESTWAIT pWait;
2557 for (;;)
2558 {
2559 RTSpinlockAcquire(pDevExt->EventSpinlock);
2560 if ((pHdr->fu32Flags & VBOX_HGCM_REQ_DONE) != 0)
2561 {
2562 RTSpinlockRelease(pDevExt->EventSpinlock);
2563 return VINF_SUCCESS;
2564 }
2565 RTSpinlockRelease(pDevExt->EventSpinlock);
2566
2567 pWait = vgdrvWaitAlloc(pDevExt, NULL);
2568 if (pWait)
2569 break;
2570 if (fInterruptible)
2571 return VERR_INTERRUPTED;
2572 RTThreadSleep(1);
2573 }
2574 pWait->fReqEvents = VMMDEV_EVENT_HGCM;
2575 pWait->pHGCMReq = pHdr;
2576
2577 /*
2578 * Re-enter the spinlock and re-check for the condition.
2579 * If the condition is met, return.
2580 * Otherwise link us into the HGCM wait list and go to sleep.
2581 */
2582 RTSpinlockAcquire(pDevExt->EventSpinlock);
2583 RTListAppend(&pDevExt->HGCMWaitList, &pWait->ListNode);
2584 if ((pHdr->fu32Flags & VBOX_HGCM_REQ_DONE) != 0)
2585 {
2586 vgdrvWaitFreeLocked(pDevExt, pWait);
2587 RTSpinlockRelease(pDevExt->EventSpinlock);
2588 return VINF_SUCCESS;
2589 }
2590 RTSpinlockRelease(pDevExt->EventSpinlock);
2591
2592 if (fInterruptible)
2593 rc = RTSemEventMultiWaitNoResume(pWait->Event, cMillies);
2594 else
2595 rc = RTSemEventMultiWait(pWait->Event, cMillies);
2596 if (rc == VERR_SEM_DESTROYED)
2597 return rc;
2598
2599 /*
2600 * Unlink, free and return.
2601 */
2602 if ( RT_FAILURE(rc)
2603 && rc != VERR_TIMEOUT
2604 && ( !fInterruptible
2605 || rc != VERR_INTERRUPTED))
2606 LogRel(("vgdrvHgcmAsyncWaitCallback: wait failed! %Rrc\n", rc));
2607
2608 vgdrvWaitFreeUnlocked(pDevExt, pWait);
2609 return rc;
2610}
2611
2612
2613/**
2614 * This is a callback for dealing with async waits.
2615 *
2616 * It operates in a manner similar to vgdrvIoCtl_WaitEvent.
2617 */
2618static DECLCALLBACK(int) vgdrvHgcmAsyncWaitCallback(VMMDevHGCMRequestHeader *pHdr, void *pvUser, uint32_t u32User)
2619{
2620 PVBOXGUESTDEVEXT pDevExt = (PVBOXGUESTDEVEXT)pvUser;
2621 LogFlow(("vgdrvHgcmAsyncWaitCallback: requestType=%d\n", pHdr->header.requestType));
2622 return vgdrvHgcmAsyncWaitCallbackWorker((VMMDevHGCMRequestHeader volatile *)pHdr, pDevExt,
2623 false /* fInterruptible */, u32User /* cMillies */);
2624}
2625
2626
2627/**
2628 * This is a callback for dealing with async waits with a timeout.
2629 *
2630 * It operates in a manner similar to vgdrvIoCtl_WaitEvent.
2631 */
2632static DECLCALLBACK(int) vgdrvHgcmAsyncWaitCallbackInterruptible(VMMDevHGCMRequestHeader *pHdr, void *pvUser, uint32_t u32User)
2633{
2634 PVBOXGUESTDEVEXT pDevExt = (PVBOXGUESTDEVEXT)pvUser;
2635 LogFlow(("vgdrvHgcmAsyncWaitCallbackInterruptible: requestType=%d\n", pHdr->header.requestType));
2636 return vgdrvHgcmAsyncWaitCallbackWorker((VMMDevHGCMRequestHeader volatile *)pHdr, pDevExt,
2637 true /* fInterruptible */, u32User /* cMillies */);
2638}
2639
2640
2641static int vgdrvIoCtl_HGCMConnect(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession, PVBGLIOCHGCMCONNECT pInfo)
2642{
2643 int rc;
2644 HGCMCLIENTID idClient = 0;
2645
2646 /*
2647 * The VbglHGCMConnect call will invoke the callback if the HGCM
2648 * call is performed in an ASYNC fashion. The function is not able
2649 * to deal with cancelled requests.
2650 */
2651 Log(("VBOXGUEST_IOCTL_HGCM_CONNECT: %.128s\n",
2652 pInfo->u.In.Loc.type == VMMDevHGCMLoc_LocalHost || pInfo->u.In.Loc.type == VMMDevHGCMLoc_LocalHost_Existing
2653 ? pInfo->u.In.Loc.u.host.achName : "<not local host>"));
2654
2655 rc = VbglR0HGCMInternalConnect(&pInfo->u.In.Loc, pSession->fRequestor, &idClient,
2656 vgdrvHgcmAsyncWaitCallback, pDevExt, RT_INDEFINITE_WAIT);
2657 Log(("VBOXGUEST_IOCTL_HGCM_CONNECT: idClient=%RX32 (rc=%Rrc)\n", idClient, rc));
2658 if (RT_SUCCESS(rc))
2659 {
2660 /*
2661 * Append the client id to the client id table.
2662 * If the table has somehow become filled up, we'll disconnect the session.
2663 */
2664 unsigned i;
2665 RTSpinlockAcquire(pDevExt->SessionSpinlock);
2666 for (i = 0; i < RT_ELEMENTS(pSession->aHGCMClientIds); i++)
2667 if (!pSession->aHGCMClientIds[i])
2668 {
2669 pSession->aHGCMClientIds[i] = idClient;
2670 break;
2671 }
2672 RTSpinlockRelease(pDevExt->SessionSpinlock);
2673 if (i >= RT_ELEMENTS(pSession->aHGCMClientIds))
2674 {
2675 LogRelMax(32, ("VBOXGUEST_IOCTL_HGCM_CONNECT: too many HGCMConnect calls for one session!\n"));
2676 VbglR0HGCMInternalDisconnect(idClient, pSession->fRequestor, vgdrvHgcmAsyncWaitCallback, pDevExt, RT_INDEFINITE_WAIT);
2677
2678 pInfo->u.Out.idClient = 0;
2679 return VERR_TOO_MANY_OPEN_FILES;
2680 }
2681 }
2682 pInfo->u.Out.idClient = idClient;
2683 return rc;
2684}
2685
2686
2687static int vgdrvIoCtl_HGCMDisconnect(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession, PVBGLIOCHGCMDISCONNECT pInfo)
2688{
2689 /*
2690 * Validate the client id and invalidate its entry while we're in the call.
2691 */
2692 int rc;
2693 const uint32_t idClient = pInfo->u.In.idClient;
2694 unsigned i;
2695 RTSpinlockAcquire(pDevExt->SessionSpinlock);
2696 for (i = 0; i < RT_ELEMENTS(pSession->aHGCMClientIds); i++)
2697 if (pSession->aHGCMClientIds[i] == idClient)
2698 {
2699 pSession->aHGCMClientIds[i] = UINT32_MAX;
2700 break;
2701 }
2702 RTSpinlockRelease(pDevExt->SessionSpinlock);
2703 if (i >= RT_ELEMENTS(pSession->aHGCMClientIds))
2704 {
2705 LogRelMax(32, ("VBOXGUEST_IOCTL_HGCM_DISCONNECT: idClient=%RX32\n", idClient));
2706 return VERR_INVALID_HANDLE;
2707 }
2708
2709 /*
2710 * The VbglHGCMConnect call will invoke the callback if the HGCM
2711 * call is performed in an ASYNC fashion. The function is not able
2712 * to deal with cancelled requests.
2713 */
2714 Log(("VBOXGUEST_IOCTL_HGCM_DISCONNECT: idClient=%RX32\n", idClient));
2715 rc = VbglR0HGCMInternalDisconnect(idClient, pSession->fRequestor, vgdrvHgcmAsyncWaitCallback, pDevExt, RT_INDEFINITE_WAIT);
2716 LogFlow(("VBOXGUEST_IOCTL_HGCM_DISCONNECT: rc=%Rrc\n", rc));
2717
2718 /* Update the client id array according to the result. */
2719 RTSpinlockAcquire(pDevExt->SessionSpinlock);
2720 if (pSession->aHGCMClientIds[i] == UINT32_MAX)
2721 pSession->aHGCMClientIds[i] = RT_SUCCESS(rc) ? 0 : idClient;
2722 RTSpinlockRelease(pDevExt->SessionSpinlock);
2723
2724 return rc;
2725}
2726
2727
2728static int vgdrvIoCtl_HGCMCallInner(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession, PVBGLIOCHGCMCALL pInfo,
2729 uint32_t cMillies, bool fInterruptible, bool f32bit, bool fUserData,
2730 size_t cbExtra, size_t cbData)
2731{
2732 const uint32_t u32ClientId = pInfo->u32ClientID;
2733 uint32_t fFlags;
2734 size_t cbActual;
2735 unsigned i;
2736 int rc;
2737
2738 /*
2739 * Some more validations.
2740 */
2741 if (RT_LIKELY(pInfo->cParms <= VMMDEV_MAX_HGCM_PARMS)) /* (Just make sure it doesn't overflow the next check.) */
2742 { /* likely */}
2743 else
2744 {
2745 LogRel(("VBOXGUEST_IOCTL_HGCM_CALL: cParm=%RX32 is not sane\n", pInfo->cParms));
2746 return VERR_INVALID_PARAMETER;
2747 }
2748
2749 cbActual = cbExtra + sizeof(*pInfo);
2750#ifdef RT_ARCH_AMD64
2751 if (f32bit)
2752 cbActual += pInfo->cParms * sizeof(HGCMFunctionParameter32);
2753 else
2754#endif
2755 cbActual += pInfo->cParms * sizeof(HGCMFunctionParameter);
2756 if (RT_LIKELY(cbData >= cbActual))
2757 { /* likely */}
2758 else
2759 {
2760 LogRel(("VBOXGUEST_IOCTL_HGCM_CALL: cbData=%#zx (%zu) required size is %#zx (%zu)\n",
2761 cbData, cbData, cbActual, cbActual));
2762 return VERR_INVALID_PARAMETER;
2763 }
2764 pInfo->Hdr.cbOut = (uint32_t)cbActual;
2765
2766 /*
2767 * Validate the client id.
2768 */
2769 RTSpinlockAcquire(pDevExt->SessionSpinlock);
2770 for (i = 0; i < RT_ELEMENTS(pSession->aHGCMClientIds); i++)
2771 if (pSession->aHGCMClientIds[i] == u32ClientId)
2772 break;
2773 RTSpinlockRelease(pDevExt->SessionSpinlock);
2774 if (RT_LIKELY(i < RT_ELEMENTS(pSession->aHGCMClientIds)))
2775 { /* likely */}
2776 else
2777 {
2778 LogRelMax(32, ("VBOXGUEST_IOCTL_HGCM_CALL: Invalid handle. u32Client=%RX32\n", u32ClientId));
2779 return VERR_INVALID_HANDLE;
2780 }
2781
2782 /*
2783 * The VbglHGCMCall call will invoke the callback if the HGCM
2784 * call is performed in an ASYNC fashion. This function can
2785 * deal with cancelled requests, so we let user more requests
2786 * be interruptible (should add a flag for this later I guess).
2787 */
2788 LogFlow(("VBOXGUEST_IOCTL_HGCM_CALL: u32Client=%RX32\n", pInfo->u32ClientID));
2789 fFlags = !fUserData && pSession->R0Process == NIL_RTR0PROCESS ? VBGLR0_HGCMCALL_F_KERNEL : VBGLR0_HGCMCALL_F_USER;
2790 uint32_t cbInfo = (uint32_t)(cbData - cbExtra);
2791#ifdef RT_ARCH_AMD64
2792 if (f32bit)
2793 {
2794 if (fInterruptible)
2795 rc = VbglR0HGCMInternalCall32(pInfo, cbInfo, fFlags, pSession->fRequestor,
2796 vgdrvHgcmAsyncWaitCallbackInterruptible, pDevExt, cMillies);
2797 else
2798 rc = VbglR0HGCMInternalCall32(pInfo, cbInfo, fFlags, pSession->fRequestor,
2799 vgdrvHgcmAsyncWaitCallback, pDevExt, cMillies);
2800 }
2801 else
2802#endif
2803 {
2804 if (fInterruptible)
2805 rc = VbglR0HGCMInternalCall(pInfo, cbInfo, fFlags, pSession->fRequestor,
2806 vgdrvHgcmAsyncWaitCallbackInterruptible, pDevExt, cMillies);
2807 else
2808 rc = VbglR0HGCMInternalCall(pInfo, cbInfo, fFlags, pSession->fRequestor,
2809 vgdrvHgcmAsyncWaitCallback, pDevExt, cMillies);
2810 }
2811 if (RT_SUCCESS(rc))
2812 {
2813 rc = pInfo->Hdr.rc;
2814 LogFlow(("VBOXGUEST_IOCTL_HGCM_CALL: result=%Rrc\n", rc));
2815 }
2816 else
2817 {
2818 if ( rc != VERR_INTERRUPTED
2819 && rc != VERR_TIMEOUT)
2820 LogRelMax(32, ("VBOXGUEST_IOCTL_HGCM_CALL: %s Failed. rc=%Rrc (Hdr.rc=%Rrc).\n", f32bit ? "32" : "64", rc, pInfo->Hdr.rc));
2821 else
2822 Log(("VBOXGUEST_IOCTL_HGCM_CALL: %s Failed. rc=%Rrc (Hdr.rc=%Rrc).\n", f32bit ? "32" : "64", rc, pInfo->Hdr.rc));
2823 }
2824 return rc;
2825}
2826
2827
2828static int vgdrvIoCtl_HGCMCallWrapper(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession, PVBGLIOCHGCMCALL pInfo,
2829 bool f32bit, bool fUserData, size_t cbData)
2830{
2831 return vgdrvIoCtl_HGCMCallInner(pDevExt, pSession, pInfo, pInfo->cMsTimeout,
2832 pInfo->fInterruptible || pSession->R0Process != NIL_RTR0PROCESS,
2833 f32bit, fUserData, 0 /*cbExtra*/, cbData);
2834}
2835
2836
2837/**
2838 * Handles a fast HGCM call from another driver.
2839 *
2840 * The driver has provided a fully assembled HGCM call request and all we need
2841 * to do is send it to the host and do the wait processing.
2842 *
2843 * @returns VBox status code of the request submission part.
2844 * @param pDevExt The device extension.
2845 * @param pCallReq The call request.
2846 */
2847static int vgdrvIoCtl_HGCMFastCall(PVBOXGUESTDEVEXT pDevExt, VBGLIOCIDCHGCMFASTCALL volatile *pCallReq)
2848{
2849 VMMDevHGCMCall volatile *pHgcmCall = (VMMDevHGCMCall volatile *)(pCallReq + 1);
2850 int rc;
2851
2852 /*
2853 * Check out the physical address.
2854 */
2855 Assert((pCallReq->GCPhysReq & PAGE_OFFSET_MASK) == ((uintptr_t)pHgcmCall & PAGE_OFFSET_MASK));
2856
2857 AssertReturn(!pCallReq->fInterruptible, VERR_NOT_IMPLEMENTED);
2858
2859 /*
2860 * Submit the request.
2861 */
2862 Log(("vgdrvIoCtl_HGCMFastCall -> host\n"));
2863 ASMOutU32(pDevExt->IOPortBase + VMMDEV_PORT_OFF_REQUEST, (uint32_t)pCallReq->GCPhysReq);
2864
2865 /* Make the compiler aware that the host has changed memory. */
2866 ASMCompilerBarrier();
2867
2868 pCallReq->Hdr.rc = rc = pHgcmCall->header.header.rc;
2869 Log(("vgdrvIoCtl_HGCMFastCall -> %Rrc (header rc=%Rrc)\n", rc, pHgcmCall->header.result));
2870
2871 /*
2872 * The host is likely to engage in asynchronous execution of HGCM, unless it fails.
2873 */
2874 if (rc == VINF_HGCM_ASYNC_EXECUTE)
2875 {
2876 rc = vgdrvHgcmAsyncWaitCallbackWorker(&pHgcmCall->header, pDevExt, false /* fInterruptible */, RT_INDEFINITE_WAIT);
2877 if (pHgcmCall->header.fu32Flags & VBOX_HGCM_REQ_DONE)
2878 {
2879 Assert(!(pHgcmCall->header.fu32Flags & VBOX_HGCM_REQ_CANCELLED));
2880 rc = VINF_SUCCESS;
2881 }
2882 else
2883 {
2884 /*
2885 * Timeout and interrupt scenarios are messy and requires
2886 * cancelation, so implement later.
2887 */
2888 AssertReleaseMsgFailed(("rc=%Rrc\n", rc));
2889 }
2890 }
2891 else
2892 Assert((pHgcmCall->header.fu32Flags & VBOX_HGCM_REQ_DONE) || RT_FAILURE_NP(rc));
2893
2894 Log(("vgdrvIoCtl_HGCMFastCall: rc=%Rrc result=%Rrc fu32Flags=%#x\n", rc, pHgcmCall->header.result, pHgcmCall->header.fu32Flags));
2895 return rc;
2896
2897}
2898
2899#endif /* VBOX_WITH_HGCM */
2900
2901/**
2902 * Handle VBGL_IOCTL_CHECK_BALLOON from R3.
2903 *
2904 * Ask the host for the size of the balloon and try to set it accordingly. If
2905 * this approach fails because it's not supported, return with fHandleInR3 set
2906 * and let the user land supply memory we can lock via the other ioctl.
2907 *
2908 * @returns VBox status code.
2909 *
2910 * @param pDevExt The device extension.
2911 * @param pSession The session.
2912 * @param pInfo The output buffer.
2913 */
2914static int vgdrvIoCtl_CheckMemoryBalloon(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession, PVBGLIOCCHECKBALLOON pInfo)
2915{
2916 VMMDevGetMemBalloonChangeRequest *pReq;
2917 int rc;
2918
2919 LogFlow(("VBGL_IOCTL_CHECK_BALLOON:\n"));
2920 rc = RTSemFastMutexRequest(pDevExt->MemBalloon.hMtx);
2921 AssertRCReturn(rc, rc);
2922
2923 /*
2924 * The first user trying to query/change the balloon becomes the
2925 * owner and owns it until the session is closed (vgdrvCloseMemBalloon).
2926 */
2927 if ( pDevExt->MemBalloon.pOwner != pSession
2928 && pDevExt->MemBalloon.pOwner == NULL)
2929 pDevExt->MemBalloon.pOwner = pSession;
2930
2931 if (pDevExt->MemBalloon.pOwner == pSession)
2932 {
2933 /*
2934 * This is a response to that event. Setting this bit means that
2935 * we request the value from the host and change the guest memory
2936 * balloon according to this value.
2937 */
2938 rc = VbglR0GRAlloc((VMMDevRequestHeader **)&pReq, sizeof(VMMDevGetMemBalloonChangeRequest), VMMDevReq_GetMemBalloonChangeRequest);
2939 if (RT_SUCCESS(rc))
2940 {
2941 pReq->header.fRequestor = pSession->fRequestor;
2942 pReq->eventAck = VMMDEV_EVENT_BALLOON_CHANGE_REQUEST;
2943 rc = VbglR0GRPerform(&pReq->header);
2944 if (RT_SUCCESS(rc))
2945 {
2946 Assert(pDevExt->MemBalloon.cMaxChunks == pReq->cPhysMemChunks || pDevExt->MemBalloon.cMaxChunks == 0);
2947 pDevExt->MemBalloon.cMaxChunks = pReq->cPhysMemChunks;
2948
2949 pInfo->u.Out.cBalloonChunks = pReq->cBalloonChunks;
2950 pInfo->u.Out.fHandleInR3 = false;
2951 pInfo->u.Out.afPadding[0] = false;
2952 pInfo->u.Out.afPadding[1] = false;
2953 pInfo->u.Out.afPadding[2] = false;
2954
2955 rc = vgdrvSetBalloonSizeKernel(pDevExt, pReq->cBalloonChunks, &pInfo->u.Out.fHandleInR3);
2956 /* Ignore various out of memory failures. */
2957 if ( rc == VERR_NO_MEMORY
2958 || rc == VERR_NO_PHYS_MEMORY
2959 || rc == VERR_NO_CONT_MEMORY)
2960 rc = VINF_SUCCESS;
2961 }
2962 else
2963 LogRel(("VBGL_IOCTL_CHECK_BALLOON: VbglR0GRPerform failed. rc=%Rrc\n", rc));
2964 VbglR0GRFree(&pReq->header);
2965 }
2966 }
2967 else
2968 rc = VERR_PERMISSION_DENIED;
2969
2970 RTSemFastMutexRelease(pDevExt->MemBalloon.hMtx);
2971 LogFlow(("VBGL_IOCTL_CHECK_BALLOON returns %Rrc\n", rc));
2972 return rc;
2973}
2974
2975
2976/**
2977 * Handle a request for changing the memory balloon.
2978 *
2979 * @returns VBox status code.
2980 *
2981 * @param pDevExt The device extention.
2982 * @param pSession The session.
2983 * @param pInfo The change request structure (input).
2984 */
2985static int vgdrvIoCtl_ChangeMemoryBalloon(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession, PVBGLIOCCHANGEBALLOON pInfo)
2986{
2987 int rc;
2988 LogFlow(("VBGL_IOCTL_CHANGE_BALLOON: fInflate=%RTbool u64ChunkAddr=%p\n", pInfo->u.In.fInflate, pInfo->u.In.pvChunk));
2989 if ( pInfo->u.In.abPadding[0]
2990 || pInfo->u.In.abPadding[1]
2991 || pInfo->u.In.abPadding[2]
2992 || pInfo->u.In.abPadding[3]
2993 || pInfo->u.In.abPadding[4]
2994 || pInfo->u.In.abPadding[5]
2995 || pInfo->u.In.abPadding[6]
2996#if ARCH_BITS == 32
2997 || pInfo->u.In.abPadding[7]
2998 || pInfo->u.In.abPadding[8]
2999 || pInfo->u.In.abPadding[9]
3000#endif
3001 )
3002 {
3003 Log(("VBGL_IOCTL_CHANGE_BALLOON: Padding isn't all zero: %.*Rhxs\n", sizeof(pInfo->u.In.abPadding), pInfo->u.In.abPadding));
3004 return VERR_INVALID_PARAMETER;
3005 }
3006
3007 rc = RTSemFastMutexRequest(pDevExt->MemBalloon.hMtx);
3008 AssertRCReturn(rc, rc);
3009
3010 if (!pDevExt->MemBalloon.fUseKernelAPI)
3011 {
3012 /*
3013 * The first user trying to query/change the balloon becomes the
3014 * owner and owns it until the session is closed (vgdrvCloseMemBalloon).
3015 */
3016 if ( pDevExt->MemBalloon.pOwner != pSession
3017 && pDevExt->MemBalloon.pOwner == NULL)
3018 pDevExt->MemBalloon.pOwner = pSession;
3019
3020 if (pDevExt->MemBalloon.pOwner == pSession)
3021 rc = vgdrvSetBalloonSizeFromUser(pDevExt, pSession, pInfo->u.In.pvChunk, pInfo->u.In.fInflate != false);
3022 else
3023 rc = VERR_PERMISSION_DENIED;
3024 }
3025 else
3026 rc = VERR_PERMISSION_DENIED;
3027
3028 RTSemFastMutexRelease(pDevExt->MemBalloon.hMtx);
3029 return rc;
3030}
3031
3032
3033/**
3034 * Handle a request for writing a core dump of the guest on the host.
3035 *
3036 * @returns VBox status code.
3037 *
3038 * @param pDevExt The device extension.
3039 * @param pSession The session.
3040 * @param pInfo The output buffer.
3041 */
3042static int vgdrvIoCtl_WriteCoreDump(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession, PVBGLIOCWRITECOREDUMP pInfo)
3043{
3044 VMMDevReqWriteCoreDump *pReq = NULL;
3045 int rc;
3046 LogFlow(("VBOXGUEST_IOCTL_WRITE_CORE_DUMP\n"));
3047 RT_NOREF1(pDevExt);
3048
3049 rc = VbglR0GRAlloc((VMMDevRequestHeader **)&pReq, sizeof(*pReq), VMMDevReq_WriteCoreDump);
3050 if (RT_SUCCESS(rc))
3051 {
3052 pReq->header.fRequestor = pSession->fRequestor;
3053 pReq->fFlags = pInfo->u.In.fFlags;
3054 rc = VbglR0GRPerform(&pReq->header);
3055 if (RT_FAILURE(rc))
3056 Log(("VBOXGUEST_IOCTL_WRITE_CORE_DUMP: VbglR0GRPerform failed, rc=%Rrc!\n", rc));
3057
3058 VbglR0GRFree(&pReq->header);
3059 }
3060 else
3061 Log(("VBOXGUEST_IOCTL_WRITE_CORE_DUMP: failed to allocate %u (%#x) bytes to cache the request. rc=%Rrc!!\n",
3062 sizeof(*pReq), sizeof(*pReq), rc));
3063 return rc;
3064}
3065
3066
3067/**
3068 * Guest backdoor logging.
3069 *
3070 * @returns VBox status code.
3071 *
3072 * @param pDevExt The device extension.
3073 * @param pch The log message (need not be NULL terminated).
3074 * @param cbData Size of the buffer.
3075 * @param fUserSession Copy of VBOXGUESTSESSION::fUserSession for the
3076 * call. True normal user, false root user.
3077 */
3078static int vgdrvIoCtl_Log(PVBOXGUESTDEVEXT pDevExt, const char *pch, size_t cbData, bool fUserSession)
3079{
3080 if (pDevExt->fLoggingEnabled)
3081 RTLogBackdoorPrintf("%.*s", cbData, pch);
3082 else if (!fUserSession)
3083 LogRel(("%.*s", cbData, pch));
3084 else
3085 Log(("%.*s", cbData, pch));
3086 return VINF_SUCCESS;
3087}
3088
3089
3090/** @name Guest Capabilities, Mouse Status and Event Filter
3091 * @{
3092 */
3093
3094/**
3095 * Clears a bit usage tracker (init time).
3096 *
3097 * @param pTracker The tracker to clear.
3098 */
3099static void vgdrvBitUsageTrackerClear(PVBOXGUESTBITUSAGETRACER pTracker)
3100{
3101 uint32_t iBit;
3102 AssertCompile(sizeof(pTracker->acPerBitUsage) == 32 * sizeof(uint32_t));
3103
3104 for (iBit = 0; iBit < 32; iBit++)
3105 pTracker->acPerBitUsage[iBit] = 0;
3106 pTracker->fMask = 0;
3107}
3108
3109
3110#ifdef VBOX_STRICT
3111/**
3112 * Checks that pTracker->fMask is correct and that the usage values are within
3113 * the valid range.
3114 *
3115 * @param pTracker The tracker.
3116 * @param cMax Max valid usage value.
3117 * @param pszWhat Identifies the tracker in assertions.
3118 */
3119static void vgdrvBitUsageTrackerCheckMask(PCVBOXGUESTBITUSAGETRACER pTracker, uint32_t cMax, const char *pszWhat)
3120{
3121 uint32_t fMask = 0;
3122 uint32_t iBit;
3123 AssertCompile(sizeof(pTracker->acPerBitUsage) == 32 * sizeof(uint32_t));
3124
3125 for (iBit = 0; iBit < 32; iBit++)
3126 if (pTracker->acPerBitUsage[iBit])
3127 {
3128 fMask |= RT_BIT_32(iBit);
3129 AssertMsg(pTracker->acPerBitUsage[iBit] <= cMax,
3130 ("%s: acPerBitUsage[%u]=%#x cMax=%#x\n", pszWhat, iBit, pTracker->acPerBitUsage[iBit], cMax));
3131 }
3132
3133 AssertMsg(fMask == pTracker->fMask, ("%s: %#x vs %#x\n", pszWhat, fMask, pTracker->fMask));
3134}
3135#endif
3136
3137
3138/**
3139 * Applies a change to the bit usage tracker.
3140 *
3141 *
3142 * @returns true if the mask changed, false if not.
3143 * @param pTracker The bit usage tracker.
3144 * @param fChanged The bits to change.
3145 * @param fPrevious The previous value of the bits.
3146 * @param cMax The max valid usage value for assertions.
3147 * @param pszWhat Identifies the tracker in assertions.
3148 */
3149static bool vgdrvBitUsageTrackerChange(PVBOXGUESTBITUSAGETRACER pTracker, uint32_t fChanged, uint32_t fPrevious,
3150 uint32_t cMax, const char *pszWhat)
3151{
3152 bool fGlobalChange = false;
3153 AssertCompile(sizeof(pTracker->acPerBitUsage) == 32 * sizeof(uint32_t));
3154
3155 while (fChanged)
3156 {
3157 uint32_t const iBit = ASMBitFirstSetU32(fChanged) - 1;
3158 uint32_t const fBitMask = RT_BIT_32(iBit);
3159 Assert(iBit < 32); Assert(fBitMask & fChanged);
3160
3161 if (fBitMask & fPrevious)
3162 {
3163 pTracker->acPerBitUsage[iBit] -= 1;
3164 AssertMsg(pTracker->acPerBitUsage[iBit] <= cMax,
3165 ("%s: acPerBitUsage[%u]=%#x cMax=%#x\n", pszWhat, iBit, pTracker->acPerBitUsage[iBit], cMax));
3166 if (pTracker->acPerBitUsage[iBit] == 0)
3167 {
3168 fGlobalChange = true;
3169 pTracker->fMask &= ~fBitMask;
3170 }
3171 }
3172 else
3173 {
3174 pTracker->acPerBitUsage[iBit] += 1;
3175 AssertMsg(pTracker->acPerBitUsage[iBit] > 0 && pTracker->acPerBitUsage[iBit] <= cMax,
3176 ("pTracker->acPerBitUsage[%u]=%#x cMax=%#x\n", pszWhat, iBit, pTracker->acPerBitUsage[iBit], cMax));
3177 if (pTracker->acPerBitUsage[iBit] == 1)
3178 {
3179 fGlobalChange = true;
3180 pTracker->fMask |= fBitMask;
3181 }
3182 }
3183
3184 fChanged &= ~fBitMask;
3185 }
3186
3187#ifdef VBOX_STRICT
3188 vgdrvBitUsageTrackerCheckMask(pTracker, cMax, pszWhat);
3189#endif
3190 NOREF(pszWhat); NOREF(cMax);
3191 return fGlobalChange;
3192}
3193
3194
3195/**
3196 * Init and termination worker for resetting the (host) event filter on the host
3197 *
3198 * @returns VBox status code.
3199 * @param pDevExt The device extension.
3200 * @param fFixedEvents Fixed events (init time).
3201 */
3202static int vgdrvResetEventFilterOnHost(PVBOXGUESTDEVEXT pDevExt, uint32_t fFixedEvents)
3203{
3204 VMMDevCtlGuestFilterMask *pReq;
3205 int rc = VbglR0GRAlloc((VMMDevRequestHeader **)&pReq, sizeof(*pReq), VMMDevReq_CtlGuestFilterMask);
3206 if (RT_SUCCESS(rc))
3207 {
3208 pReq->u32NotMask = UINT32_MAX & ~fFixedEvents;
3209 pReq->u32OrMask = fFixedEvents;
3210 rc = VbglR0GRPerform(&pReq->header);
3211 if (RT_FAILURE(rc))
3212 LogRelFunc(("failed with rc=%Rrc\n", rc));
3213 VbglR0GRFree(&pReq->header);
3214 }
3215 RT_NOREF1(pDevExt);
3216 return rc;
3217}
3218
3219
3220/**
3221 * Changes the event filter mask for the given session.
3222 *
3223 * This is called in response to VBGL_IOCTL_CHANGE_FILTER_MASK as well as to do
3224 * session cleanup.
3225 *
3226 * @returns VBox status code.
3227 * @param pDevExt The device extension.
3228 * @param pSession The session.
3229 * @param fOrMask The events to add.
3230 * @param fNotMask The events to remove.
3231 * @param fSessionTermination Set if we're called by the session cleanup code.
3232 * This tweaks the error handling so we perform
3233 * proper session cleanup even if the host
3234 * misbehaves.
3235 *
3236 * @remarks Takes the session spinlock.
3237 */
3238static int vgdrvSetSessionEventFilter(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession,
3239 uint32_t fOrMask, uint32_t fNotMask, bool fSessionTermination)
3240{
3241 VMMDevCtlGuestFilterMask *pReq;
3242 uint32_t fChanged;
3243 uint32_t fPrevious;
3244 int rc;
3245
3246 /*
3247 * Preallocate a request buffer so we can do all in one go without leaving the spinlock.
3248 */
3249 rc = VbglR0GRAlloc((VMMDevRequestHeader **)&pReq, sizeof(*pReq), VMMDevReq_CtlGuestFilterMask);
3250 if (RT_SUCCESS(rc))
3251 { /* nothing */ }
3252 else if (!fSessionTermination)
3253 {
3254 LogRel(("vgdrvSetSessionFilterMask: VbglR0GRAlloc failure: %Rrc\n", rc));
3255 return rc;
3256 }
3257 else
3258 pReq = NULL; /* Ignore failure, we must do session cleanup. */
3259
3260
3261 RTSpinlockAcquire(pDevExt->SessionSpinlock);
3262
3263 /*
3264 * Apply the changes to the session mask.
3265 */
3266 fPrevious = pSession->fEventFilter;
3267 pSession->fEventFilter |= fOrMask;
3268 pSession->fEventFilter &= ~fNotMask;
3269
3270 /*
3271 * If anything actually changed, update the global usage counters.
3272 */
3273 fChanged = fPrevious ^ pSession->fEventFilter;
3274 LogFlow(("vgdrvSetSessionEventFilter: Session->fEventFilter: %#x -> %#x (changed %#x)\n",
3275 fPrevious, pSession->fEventFilter, fChanged));
3276 if (fChanged)
3277 {
3278 bool fGlobalChange = vgdrvBitUsageTrackerChange(&pDevExt->EventFilterTracker, fChanged, fPrevious,
3279 pDevExt->cSessions, "EventFilterTracker");
3280
3281 /*
3282 * If there are global changes, update the event filter on the host.
3283 */
3284 if (fGlobalChange || pDevExt->fEventFilterHost == UINT32_MAX)
3285 {
3286 Assert(pReq || fSessionTermination);
3287 if (pReq)
3288 {
3289 pReq->u32OrMask = pDevExt->fFixedEvents | pDevExt->EventFilterTracker.fMask;
3290 if (pReq->u32OrMask == pDevExt->fEventFilterHost)
3291 rc = VINF_SUCCESS;
3292 else
3293 {
3294 pDevExt->fEventFilterHost = pReq->u32OrMask;
3295 pReq->u32NotMask = ~pReq->u32OrMask;
3296 rc = VbglR0GRPerform(&pReq->header);
3297 if (RT_FAILURE(rc))
3298 {
3299 /*
3300 * Failed, roll back (unless it's session termination time).
3301 */
3302 pDevExt->fEventFilterHost = UINT32_MAX;
3303 if (!fSessionTermination)
3304 {
3305 vgdrvBitUsageTrackerChange(&pDevExt->EventFilterTracker, fChanged, pSession->fEventFilter,
3306 pDevExt->cSessions, "EventFilterTracker");
3307 pSession->fEventFilter = fPrevious;
3308 }
3309 }
3310 }
3311 }
3312 else
3313 rc = VINF_SUCCESS;
3314 }
3315 }
3316
3317 RTSpinlockRelease(pDevExt->SessionSpinlock);
3318 if (pReq)
3319 VbglR0GRFree(&pReq->header);
3320 return rc;
3321}
3322
3323
3324/**
3325 * Handle VBGL_IOCTL_CHANGE_FILTER_MASK.
3326 *
3327 * @returns VBox status code.
3328 *
3329 * @param pDevExt The device extension.
3330 * @param pSession The session.
3331 * @param pInfo The request.
3332 */
3333static int vgdrvIoCtl_ChangeFilterMask(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession, PVBGLIOCCHANGEFILTERMASK pInfo)
3334{
3335 LogFlow(("VBGL_IOCTL_CHANGE_FILTER_MASK: or=%#x not=%#x\n", pInfo->u.In.fOrMask, pInfo->u.In.fNotMask));
3336
3337 if ((pInfo->u.In.fOrMask | pInfo->u.In.fNotMask) & ~VMMDEV_EVENT_VALID_EVENT_MASK)
3338 {
3339 Log(("VBGL_IOCTL_CHANGE_FILTER_MASK: or=%#x not=%#x: Invalid masks!\n", pInfo->u.In.fOrMask, pInfo->u.In.fNotMask));
3340 return VERR_INVALID_PARAMETER;
3341 }
3342
3343 return vgdrvSetSessionEventFilter(pDevExt, pSession, pInfo->u.In.fOrMask, pInfo->u.In.fNotMask, false /*fSessionTermination*/);
3344}
3345
3346
3347/**
3348 * Init and termination worker for set mouse feature status to zero on the host.
3349 *
3350 * @returns VBox status code.
3351 * @param pDevExt The device extension.
3352 */
3353static int vgdrvResetMouseStatusOnHost(PVBOXGUESTDEVEXT pDevExt)
3354{
3355 VMMDevReqMouseStatus *pReq;
3356 int rc = VbglR0GRAlloc((VMMDevRequestHeader **)&pReq, sizeof(*pReq), VMMDevReq_SetMouseStatus);
3357 if (RT_SUCCESS(rc))
3358 {
3359 pReq->mouseFeatures = 0;
3360 pReq->pointerXPos = 0;
3361 pReq->pointerYPos = 0;
3362 rc = VbglR0GRPerform(&pReq->header);
3363 if (RT_FAILURE(rc))
3364 LogRelFunc(("failed with rc=%Rrc\n", rc));
3365 VbglR0GRFree(&pReq->header);
3366 }
3367 RT_NOREF1(pDevExt);
3368 return rc;
3369}
3370
3371
3372/**
3373 * Changes the mouse status mask for the given session.
3374 *
3375 * This is called in response to VBOXGUEST_IOCTL_SET_MOUSE_STATUS as well as to
3376 * do session cleanup.
3377 *
3378 * @returns VBox status code.
3379 * @param pDevExt The device extension.
3380 * @param pSession The session.
3381 * @param fOrMask The status flags to add.
3382 * @param fNotMask The status flags to remove.
3383 * @param fSessionTermination Set if we're called by the session cleanup code.
3384 * This tweaks the error handling so we perform
3385 * proper session cleanup even if the host
3386 * misbehaves.
3387 *
3388 * @remarks Takes the session spinlock.
3389 */
3390static int vgdrvSetSessionMouseStatus(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession,
3391 uint32_t fOrMask, uint32_t fNotMask, bool fSessionTermination)
3392{
3393 VMMDevReqMouseStatus *pReq;
3394 uint32_t fChanged;
3395 uint32_t fPrevious;
3396 int rc;
3397
3398 /*
3399 * Preallocate a request buffer so we can do all in one go without leaving the spinlock.
3400 */
3401 rc = VbglR0GRAlloc((VMMDevRequestHeader **)&pReq, sizeof(*pReq), VMMDevReq_SetMouseStatus);
3402 if (RT_SUCCESS(rc))
3403 {
3404 if (!fSessionTermination)
3405 pReq->header.fRequestor = pSession->fRequestor;
3406 }
3407 else if (!fSessionTermination)
3408 {
3409 LogRel(("vgdrvSetSessionMouseStatus: VbglR0GRAlloc failure: %Rrc\n", rc));
3410 return rc;
3411 }
3412 else
3413 pReq = NULL; /* Ignore failure, we must do session cleanup. */
3414
3415
3416 RTSpinlockAcquire(pDevExt->SessionSpinlock);
3417
3418 /*
3419 * Apply the changes to the session mask.
3420 */
3421 fPrevious = pSession->fMouseStatus;
3422 pSession->fMouseStatus |= fOrMask;
3423 pSession->fMouseStatus &= ~fNotMask;
3424
3425 /*
3426 * If anything actually changed, update the global usage counters.
3427 */
3428 fChanged = fPrevious ^ pSession->fMouseStatus;
3429 if (fChanged)
3430 {
3431 bool fGlobalChange = vgdrvBitUsageTrackerChange(&pDevExt->MouseStatusTracker, fChanged, fPrevious,
3432 pDevExt->cSessions, "MouseStatusTracker");
3433
3434 /*
3435 * If there are global changes, update the event filter on the host.
3436 */
3437 if (fGlobalChange || pDevExt->fMouseStatusHost == UINT32_MAX)
3438 {
3439 Assert(pReq || fSessionTermination);
3440 if (pReq)
3441 {
3442 pReq->mouseFeatures = pDevExt->MouseStatusTracker.fMask;
3443 if (pReq->mouseFeatures == pDevExt->fMouseStatusHost)
3444 rc = VINF_SUCCESS;
3445 else
3446 {
3447 pDevExt->fMouseStatusHost = pReq->mouseFeatures;
3448 pReq->pointerXPos = 0;
3449 pReq->pointerYPos = 0;
3450 rc = VbglR0GRPerform(&pReq->header);
3451 if (RT_FAILURE(rc))
3452 {
3453 /*
3454 * Failed, roll back (unless it's session termination time).
3455 */
3456 pDevExt->fMouseStatusHost = UINT32_MAX;
3457 if (!fSessionTermination)
3458 {
3459 vgdrvBitUsageTrackerChange(&pDevExt->MouseStatusTracker, fChanged, pSession->fMouseStatus,
3460 pDevExt->cSessions, "MouseStatusTracker");
3461 pSession->fMouseStatus = fPrevious;
3462 }
3463 }
3464 }
3465 }
3466 else
3467 rc = VINF_SUCCESS;
3468 }
3469 }
3470
3471 RTSpinlockRelease(pDevExt->SessionSpinlock);
3472 if (pReq)
3473 VbglR0GRFree(&pReq->header);
3474 return rc;
3475}
3476
3477
3478/**
3479 * Sets the mouse status features for this session and updates them globally.
3480 *
3481 * @returns VBox status code.
3482 *
3483 * @param pDevExt The device extention.
3484 * @param pSession The session.
3485 * @param fFeatures New bitmap of enabled features.
3486 */
3487static int vgdrvIoCtl_SetMouseStatus(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession, uint32_t fFeatures)
3488{
3489 LogFlow(("VBGL_IOCTL_SET_MOUSE_STATUS: features=%#x\n", fFeatures));
3490
3491 if (fFeatures & ~VMMDEV_MOUSE_GUEST_MASK)
3492 return VERR_INVALID_PARAMETER;
3493
3494 return vgdrvSetSessionMouseStatus(pDevExt, pSession, fFeatures, ~fFeatures, false /*fSessionTermination*/);
3495}
3496
3497
3498/**
3499 * Return the mask of VMM device events that this session is allowed to see (wrt
3500 * to "acquire" mode guest capabilities).
3501 *
3502 * The events associated with guest capabilities in "acquire" mode will be
3503 * restricted to sessions which has acquired the respective capabilities.
3504 * If someone else tries to wait for acquired events, they won't be woken up
3505 * when the event becomes pending. Should some other thread in the session
3506 * acquire the capability while the corresponding event is pending, the waiting
3507 * thread will woken up.
3508 *
3509 * @returns Mask of events valid for the given session.
3510 * @param pDevExt The device extension.
3511 * @param pSession The session.
3512 *
3513 * @remarks Needs only be called when dispatching events in the
3514 * VBOXGUEST_ACQUIRE_STYLE_EVENTS mask.
3515 */
3516static uint32_t vgdrvGetAllowedEventMaskForSession(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession)
3517{
3518 uint32_t fAcquireModeGuestCaps;
3519 uint32_t fAcquiredGuestCaps;
3520 uint32_t fAllowedEvents;
3521
3522 /*
3523 * Note! Reads pSession->fAcquiredGuestCaps and pDevExt->fAcquireModeGuestCaps
3524 * WITHOUT holding VBOXGUESTDEVEXT::SessionSpinlock.
3525 */
3526 fAcquireModeGuestCaps = ASMAtomicUoReadU32(&pDevExt->fAcquireModeGuestCaps);
3527 if (fAcquireModeGuestCaps == 0)
3528 return VMMDEV_EVENT_VALID_EVENT_MASK;
3529 fAcquiredGuestCaps = ASMAtomicUoReadU32(&pSession->fAcquiredGuestCaps);
3530
3531 /*
3532 * Calculate which events to allow according to the cap config and caps
3533 * acquired by the session.
3534 */
3535 fAllowedEvents = VMMDEV_EVENT_VALID_EVENT_MASK;
3536 if ( !(fAcquiredGuestCaps & VMMDEV_GUEST_SUPPORTS_GRAPHICS)
3537 && (fAcquireModeGuestCaps & VMMDEV_GUEST_SUPPORTS_GRAPHICS))
3538 fAllowedEvents &= ~VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
3539
3540 if ( !(fAcquiredGuestCaps & VMMDEV_GUEST_SUPPORTS_SEAMLESS)
3541 && (fAcquireModeGuestCaps & VMMDEV_GUEST_SUPPORTS_SEAMLESS))
3542 fAllowedEvents &= ~VMMDEV_EVENT_SEAMLESS_MODE_CHANGE_REQUEST;
3543
3544 return fAllowedEvents;
3545}
3546
3547
3548/**
3549 * Init and termination worker for set guest capabilities to zero on the host.
3550 *
3551 * @returns VBox status code.
3552 * @param pDevExt The device extension.
3553 */
3554static int vgdrvResetCapabilitiesOnHost(PVBOXGUESTDEVEXT pDevExt)
3555{
3556 VMMDevReqGuestCapabilities2 *pReq;
3557 int rc = VbglR0GRAlloc((VMMDevRequestHeader **)&pReq, sizeof(*pReq), VMMDevReq_SetGuestCapabilities);
3558 if (RT_SUCCESS(rc))
3559 {
3560 pReq->u32NotMask = UINT32_MAX;
3561 pReq->u32OrMask = 0;
3562 rc = VbglR0GRPerform(&pReq->header);
3563
3564 if (RT_FAILURE(rc))
3565 LogRelFunc(("failed with rc=%Rrc\n", rc));
3566 VbglR0GRFree(&pReq->header);
3567 }
3568 RT_NOREF1(pDevExt);
3569 return rc;
3570}
3571
3572
3573/**
3574 * Sets the guest capabilities to the host while holding the lock.
3575 *
3576 * This will ASSUME that we're the ones in charge of the mask, so
3577 * we'll simply clear all bits we don't set.
3578 *
3579 * @returns VBox status code.
3580 * @param pDevExt The device extension.
3581 * @param pReq The request.
3582 */
3583static int vgdrvUpdateCapabilitiesOnHostWithReqAndLock(PVBOXGUESTDEVEXT pDevExt, VMMDevReqGuestCapabilities2 *pReq)
3584{
3585 int rc;
3586
3587 pReq->u32OrMask = pDevExt->fAcquiredGuestCaps | pDevExt->SetGuestCapsTracker.fMask;
3588 if (pReq->u32OrMask == pDevExt->fGuestCapsHost)
3589 rc = VINF_SUCCESS;
3590 else
3591 {
3592 pDevExt->fGuestCapsHost = pReq->u32OrMask;
3593 pReq->u32NotMask = ~pReq->u32OrMask;
3594 rc = VbglR0GRPerform(&pReq->header);
3595 if (RT_FAILURE(rc))
3596 pDevExt->fGuestCapsHost = UINT32_MAX;
3597 }
3598
3599 return rc;
3600}
3601
3602
3603/**
3604 * Switch a set of capabilities into "acquire" mode and (maybe) acquire them for
3605 * the given session.
3606 *
3607 * This is called in response to VBOXGUEST_IOCTL_GUEST_CAPS_ACQUIRE as well as
3608 * to do session cleanup.
3609 *
3610 * @returns VBox status code.
3611 * @param pDevExt The device extension.
3612 * @param pSession The session.
3613 * @param fOrMask The capabilities to add .
3614 * @param fNotMask The capabilities to remove. Ignored in
3615 * VBOXGUESTCAPSACQUIRE_FLAGS_CONFIG_ACQUIRE_MODE.
3616 * @param fFlags Confusing operation modifier.
3617 * VBOXGUESTCAPSACQUIRE_FLAGS_NONE means to both
3618 * configure and acquire/release the capabilities.
3619 * VBOXGUESTCAPSACQUIRE_FLAGS_CONFIG_ACQUIRE_MODE
3620 * means only configure capabilities in the
3621 * @a fOrMask capabilities for "acquire" mode.
3622 * @param fSessionTermination Set if we're called by the session cleanup code.
3623 * This tweaks the error handling so we perform
3624 * proper session cleanup even if the host
3625 * misbehaves.
3626 *
3627 * @remarks Takes both the session and event spinlocks.
3628 */
3629static int vgdrvAcquireSessionCapabilities(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession,
3630 uint32_t fOrMask, uint32_t fNotMask, uint32_t fFlags,
3631 bool fSessionTermination)
3632{
3633 uint32_t fCurrentOwnedCaps;
3634 uint32_t fSessionRemovedCaps;
3635 uint32_t fSessionAddedCaps;
3636 uint32_t fOtherConflictingCaps;
3637 VMMDevReqGuestCapabilities2 *pReq = NULL;
3638 int rc;
3639
3640
3641 /*
3642 * Validate and adjust input.
3643 */
3644 if (fOrMask & ~( VMMDEV_GUEST_SUPPORTS_SEAMLESS
3645 | VMMDEV_GUEST_SUPPORTS_GUEST_HOST_WINDOW_MAPPING
3646 | VMMDEV_GUEST_SUPPORTS_GRAPHICS ) )
3647 {
3648 LogRel(("vgdrvAcquireSessionCapabilities: invalid fOrMask=%#x (pSession=%p fNotMask=%#x fFlags=%#x)\n",
3649 fOrMask, pSession, fNotMask, fFlags));
3650 return VERR_INVALID_PARAMETER;
3651 }
3652
3653 if ((fFlags & ~VBGL_IOC_AGC_FLAGS_VALID_MASK) != 0)
3654 {
3655 LogRel(("vgdrvAcquireSessionCapabilities: invalid fFlags=%#x (pSession=%p fOrMask=%#x fNotMask=%#x)\n",
3656 fFlags, pSession, fOrMask, fNotMask));
3657 return VERR_INVALID_PARAMETER;
3658 }
3659 Assert(!fOrMask || !fSessionTermination);
3660
3661 /* The fNotMask no need to have all values valid, invalid ones will simply be ignored. */
3662 fNotMask &= ~fOrMask;
3663
3664 /*
3665 * Preallocate a update request if we're about to do more than just configure
3666 * the capability mode.
3667 */
3668 if (!(fFlags & VBGL_IOC_AGC_FLAGS_CONFIG_ACQUIRE_MODE))
3669 {
3670 rc = VbglR0GRAlloc((VMMDevRequestHeader **)&pReq, sizeof(*pReq), VMMDevReq_SetGuestCapabilities);
3671 if (RT_SUCCESS(rc))
3672 {
3673 if (!fSessionTermination)
3674 pReq->header.fRequestor = pSession->fRequestor;
3675 }
3676 else if (!fSessionTermination)
3677 {
3678 LogRel(("vgdrvAcquireSessionCapabilities: pSession=%p fOrMask=%#x fNotMask=%#x fFlags=%#x: VbglR0GRAlloc failure: %Rrc\n",
3679 pSession, fOrMask, fNotMask, fFlags, rc));
3680 return rc;
3681 }
3682 else
3683 pReq = NULL; /* Ignore failure, we must do session cleanup. */
3684 }
3685
3686 /*
3687 * Try switch the capabilities in the OR mask into "acquire" mode.
3688 *
3689 * Note! We currently ignore anyone which may already have "set" the capabilities
3690 * in fOrMask. Perhaps not the best way to handle it, but it's simple...
3691 */
3692 RTSpinlockAcquire(pDevExt->EventSpinlock);
3693
3694 if (!(pDevExt->fSetModeGuestCaps & fOrMask))
3695 pDevExt->fAcquireModeGuestCaps |= fOrMask;
3696 else
3697 {
3698 RTSpinlockRelease(pDevExt->EventSpinlock);
3699
3700 if (pReq)
3701 VbglR0GRFree(&pReq->header);
3702 AssertMsgFailed(("Trying to change caps mode: %#x\n", fOrMask));
3703 LogRel(("vgdrvAcquireSessionCapabilities: pSession=%p fOrMask=%#x fNotMask=%#x fFlags=%#x: calling caps acquire for set caps\n",
3704 pSession, fOrMask, fNotMask, fFlags));
3705 return VERR_INVALID_STATE;
3706 }
3707
3708 /*
3709 * If we only wanted to switch the capabilities into "acquire" mode, we're done now.
3710 */
3711 if (fFlags & VBGL_IOC_AGC_FLAGS_CONFIG_ACQUIRE_MODE)
3712 {
3713 RTSpinlockRelease(pDevExt->EventSpinlock);
3714
3715 Assert(!pReq);
3716 Log(("vgdrvAcquireSessionCapabilities: pSession=%p fOrMask=%#x fNotMask=%#x fFlags=%#x: configured acquire caps: 0x%x\n",
3717 pSession, fOrMask, fNotMask, fFlags));
3718 return VINF_SUCCESS;
3719 }
3720 Assert(pReq || fSessionTermination);
3721
3722 /*
3723 * Caller wants to acquire/release the capabilities too.
3724 *
3725 * Note! The mode change of the capabilities above won't be reverted on
3726 * failure, this is intentional.
3727 */
3728 fCurrentOwnedCaps = pSession->fAcquiredGuestCaps;
3729 fSessionRemovedCaps = fCurrentOwnedCaps & fNotMask;
3730 fSessionAddedCaps = fOrMask & ~fCurrentOwnedCaps;
3731 fOtherConflictingCaps = pDevExt->fAcquiredGuestCaps & ~fCurrentOwnedCaps;
3732 fOtherConflictingCaps &= fSessionAddedCaps;
3733
3734 if (!fOtherConflictingCaps)
3735 {
3736 if (fSessionAddedCaps)
3737 {
3738 pSession->fAcquiredGuestCaps |= fSessionAddedCaps;
3739 pDevExt->fAcquiredGuestCaps |= fSessionAddedCaps;
3740 }
3741
3742 if (fSessionRemovedCaps)
3743 {
3744 pSession->fAcquiredGuestCaps &= ~fSessionRemovedCaps;
3745 pDevExt->fAcquiredGuestCaps &= ~fSessionRemovedCaps;
3746 }
3747
3748 /*
3749 * If something changes (which is very likely), tell the host.
3750 */
3751 if (fSessionAddedCaps || fSessionRemovedCaps || pDevExt->fGuestCapsHost == UINT32_MAX)
3752 {
3753 Assert(pReq || fSessionTermination);
3754 if (pReq)
3755 {
3756 rc = vgdrvUpdateCapabilitiesOnHostWithReqAndLock(pDevExt, pReq);
3757 if (RT_FAILURE(rc) && !fSessionTermination)
3758 {
3759 /* Failed, roll back. */
3760 if (fSessionAddedCaps)
3761 {
3762 pSession->fAcquiredGuestCaps &= ~fSessionAddedCaps;
3763 pDevExt->fAcquiredGuestCaps &= ~fSessionAddedCaps;
3764 }
3765 if (fSessionRemovedCaps)
3766 {
3767 pSession->fAcquiredGuestCaps |= fSessionRemovedCaps;
3768 pDevExt->fAcquiredGuestCaps |= fSessionRemovedCaps;
3769 }
3770
3771 RTSpinlockRelease(pDevExt->EventSpinlock);
3772 LogRel(("vgdrvAcquireSessionCapabilities: vgdrvUpdateCapabilitiesOnHostWithReqAndLock failed: rc=%Rrc\n", rc));
3773 VbglR0GRFree(&pReq->header);
3774 return rc;
3775 }
3776 }
3777 }
3778 }
3779 else
3780 {
3781 RTSpinlockRelease(pDevExt->EventSpinlock);
3782
3783 Log(("vgdrvAcquireSessionCapabilities: Caps %#x were busy\n", fOtherConflictingCaps));
3784 VbglR0GRFree(&pReq->header);
3785 return VERR_RESOURCE_BUSY;
3786 }
3787
3788 RTSpinlockRelease(pDevExt->EventSpinlock);
3789 if (pReq)
3790 VbglR0GRFree(&pReq->header);
3791
3792 /*
3793 * If we added a capability, check if that means some other thread in our
3794 * session should be unblocked because there are events pending.
3795 *
3796 * HACK ALERT! When the seamless support capability is added we generate a
3797 * seamless change event so that the ring-3 client can sync with
3798 * the seamless state. Although this introduces a spurious
3799 * wakeups of the ring-3 client, it solves the problem of client
3800 * state inconsistency in multiuser environment (on Windows).
3801 */
3802 if (fSessionAddedCaps)
3803 {
3804 uint32_t fGenFakeEvents = 0;
3805 if (fSessionAddedCaps & VMMDEV_GUEST_SUPPORTS_SEAMLESS)
3806 fGenFakeEvents |= VMMDEV_EVENT_SEAMLESS_MODE_CHANGE_REQUEST;
3807
3808 RTSpinlockAcquire(pDevExt->EventSpinlock);
3809 if (fGenFakeEvents || pDevExt->f32PendingEvents)
3810 vgdrvDispatchEventsLocked(pDevExt, fGenFakeEvents);
3811 RTSpinlockRelease(pDevExt->EventSpinlock);
3812
3813#ifdef VBOXGUEST_USE_DEFERRED_WAKE_UP
3814 VGDrvCommonWaitDoWakeUps(pDevExt);
3815#endif
3816 }
3817
3818 return VINF_SUCCESS;
3819}
3820
3821
3822/**
3823 * Handle VBGL_IOCTL_ACQUIRE_GUEST_CAPABILITIES.
3824 *
3825 * @returns VBox status code.
3826 *
3827 * @param pDevExt The device extension.
3828 * @param pSession The session.
3829 * @param pAcquire The request.
3830 */
3831static int vgdrvIoCtl_GuestCapsAcquire(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession, PVBGLIOCACQUIREGUESTCAPS pAcquire)
3832{
3833 int rc;
3834 LogFlow(("VBGL_IOCTL_ACQUIRE_GUEST_CAPABILITIES: or=%#x not=%#x flags=%#x\n",
3835 pAcquire->u.In.fOrMask, pAcquire->u.In.fNotMask, pAcquire->u.In.fFlags));
3836
3837 rc = vgdrvAcquireSessionCapabilities(pDevExt, pSession, pAcquire->u.In.fOrMask, pAcquire->u.In.fNotMask,
3838 pAcquire->u.In.fFlags, false /*fSessionTermination*/);
3839 if (RT_FAILURE(rc))
3840 LogRel(("VBGL_IOCTL_ACQUIRE_GUEST_CAPABILITIES failed rc=%Rrc\n", rc));
3841 return rc;
3842}
3843
3844
3845/**
3846 * Sets the guest capabilities for a session.
3847 *
3848 * @returns VBox status code.
3849 * @param pDevExt The device extension.
3850 * @param pSession The session.
3851 * @param fOrMask The capabilities to add.
3852 * @param fNotMask The capabilities to remove.
3853 * @param pfSessionCaps Where to return the guest capabilities reported
3854 * for this session. Optional.
3855 * @param pfGlobalCaps Where to return the guest capabilities reported
3856 * for all the sessions. Optional.
3857 *
3858 * @param fSessionTermination Set if we're called by the session cleanup code.
3859 * This tweaks the error handling so we perform
3860 * proper session cleanup even if the host
3861 * misbehaves.
3862 *
3863 * @remarks Takes the session spinlock.
3864 */
3865static int vgdrvSetSessionCapabilities(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession,
3866 uint32_t fOrMask, uint32_t fNotMask, uint32_t *pfSessionCaps, uint32_t *pfGlobalCaps,
3867 bool fSessionTermination)
3868{
3869 /*
3870 * Preallocate a request buffer so we can do all in one go without leaving the spinlock.
3871 */
3872 VMMDevReqGuestCapabilities2 *pReq;
3873 int rc = VbglR0GRAlloc((VMMDevRequestHeader **)&pReq, sizeof(*pReq), VMMDevReq_SetGuestCapabilities);
3874 if (RT_SUCCESS(rc))
3875 {
3876 if (!fSessionTermination)
3877 pReq->header.fRequestor = pSession->fRequestor;
3878 }
3879 else if (!fSessionTermination)
3880 {
3881 if (pfSessionCaps)
3882 *pfSessionCaps = UINT32_MAX;
3883 if (pfGlobalCaps)
3884 *pfGlobalCaps = UINT32_MAX;
3885 LogRel(("vgdrvSetSessionCapabilities: VbglR0GRAlloc failure: %Rrc\n", rc));
3886 return rc;
3887 }
3888 else
3889 pReq = NULL; /* Ignore failure, we must do session cleanup. */
3890
3891
3892 RTSpinlockAcquire(pDevExt->SessionSpinlock);
3893
3894#ifndef VBOXGUEST_DISREGARD_ACQUIRE_MODE_GUEST_CAPS
3895 /*
3896 * Capabilities in "acquire" mode cannot be set via this API.
3897 * (Acquire mode is only used on windows at the time of writing.)
3898 */
3899 if (!(fOrMask & pDevExt->fAcquireModeGuestCaps))
3900#endif
3901 {
3902 /*
3903 * Apply the changes to the session mask.
3904 */
3905 uint32_t fChanged;
3906 uint32_t fPrevious = pSession->fCapabilities;
3907 pSession->fCapabilities |= fOrMask;
3908 pSession->fCapabilities &= ~fNotMask;
3909
3910 /*
3911 * If anything actually changed, update the global usage counters.
3912 */
3913 fChanged = fPrevious ^ pSession->fCapabilities;
3914 if (fChanged)
3915 {
3916 bool fGlobalChange = vgdrvBitUsageTrackerChange(&pDevExt->SetGuestCapsTracker, fChanged, fPrevious,
3917 pDevExt->cSessions, "SetGuestCapsTracker");
3918
3919 /*
3920 * If there are global changes, update the capabilities on the host.
3921 */
3922 if (fGlobalChange || pDevExt->fGuestCapsHost == UINT32_MAX)
3923 {
3924 Assert(pReq || fSessionTermination);
3925 if (pReq)
3926 {
3927 rc = vgdrvUpdateCapabilitiesOnHostWithReqAndLock(pDevExt, pReq);
3928
3929 /* On failure, roll back (unless it's session termination time). */
3930 if (RT_FAILURE(rc) && !fSessionTermination)
3931 {
3932 vgdrvBitUsageTrackerChange(&pDevExt->SetGuestCapsTracker, fChanged, pSession->fCapabilities,
3933 pDevExt->cSessions, "SetGuestCapsTracker");
3934 pSession->fCapabilities = fPrevious;
3935 }
3936 }
3937 }
3938 }
3939 }
3940#ifndef VBOXGUEST_DISREGARD_ACQUIRE_MODE_GUEST_CAPS
3941 else
3942 rc = VERR_RESOURCE_BUSY;
3943#endif
3944
3945 if (pfSessionCaps)
3946 *pfSessionCaps = pSession->fCapabilities;
3947 if (pfGlobalCaps)
3948 *pfGlobalCaps = pDevExt->fAcquiredGuestCaps | pDevExt->SetGuestCapsTracker.fMask;
3949
3950 RTSpinlockRelease(pDevExt->SessionSpinlock);
3951 if (pReq)
3952 VbglR0GRFree(&pReq->header);
3953 return rc;
3954}
3955
3956
3957/**
3958 * Handle VBGL_IOCTL_CHANGE_GUEST_CAPABILITIES.
3959 *
3960 * @returns VBox status code.
3961 *
3962 * @param pDevExt The device extension.
3963 * @param pSession The session.
3964 * @param pInfo The request.
3965 */
3966static int vgdrvIoCtl_SetCapabilities(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession, PVBGLIOCSETGUESTCAPS pInfo)
3967{
3968 int rc;
3969 LogFlow(("VBGL_IOCTL_CHANGE_GUEST_CAPABILITIES: or=%#x not=%#x\n", pInfo->u.In.fOrMask, pInfo->u.In.fNotMask));
3970
3971 if (!((pInfo->u.In.fOrMask | pInfo->u.In.fNotMask) & ~VMMDEV_GUEST_CAPABILITIES_MASK))
3972 rc = vgdrvSetSessionCapabilities(pDevExt, pSession, pInfo->u.In.fOrMask, pInfo->u.In.fNotMask,
3973 &pInfo->u.Out.fSessionCaps, &pInfo->u.Out.fGlobalCaps, false /*fSessionTermination*/);
3974 else
3975 rc = VERR_INVALID_PARAMETER;
3976
3977 return rc;
3978}
3979
3980/** @} */
3981
3982
3983/**
3984 * Common IOCtl for user to kernel and kernel to kernel communication.
3985 *
3986 * This function only does the basic validation and then invokes
3987 * worker functions that takes care of each specific function.
3988 *
3989 * @returns VBox status code.
3990 *
3991 * @param iFunction The requested function.
3992 * @param pDevExt The device extension.
3993 * @param pSession The client session.
3994 * @param pReqHdr Pointer to the request. This always starts with
3995 * a request common header.
3996 * @param cbReq The max size of the request buffer.
3997 */
3998int VGDrvCommonIoCtl(uintptr_t iFunction, PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession, PVBGLREQHDR pReqHdr, size_t cbReq)
3999{
4000 uintptr_t const iFunctionStripped = VBGL_IOCTL_CODE_STRIPPED(iFunction);
4001 int rc;
4002
4003 LogFlow(("VGDrvCommonIoCtl: iFunction=%#x pDevExt=%p pSession=%p pReqHdr=%p cbReq=%zu\n",
4004 iFunction, pDevExt, pSession, pReqHdr, cbReq));
4005
4006 /*
4007 * Define some helper macros to simplify validation.
4008 */
4009#define REQ_CHECK_SIZES_EX(Name, cbInExpect, cbOutExpect) \
4010 do { \
4011 if (RT_LIKELY( pReqHdr->cbIn == (cbInExpect) \
4012 && ( pReqHdr->cbOut == (cbOutExpect) \
4013 || ((cbInExpect) == (cbOutExpect) && pReqHdr->cbOut == 0) ) )) \
4014 { /* likely */ } \
4015 else \
4016 { \
4017 Log(( #Name ": Invalid input/output sizes. cbIn=%ld expected %ld. cbOut=%ld expected %ld.\n", \
4018 (long)pReqHdr->cbIn, (long)(cbInExpect), (long)pReqHdr->cbOut, (long)(cbOutExpect))); \
4019 return pReqHdr->rc = VERR_INVALID_PARAMETER; \
4020 } \
4021 } while (0)
4022
4023#define REQ_CHECK_SIZES(Name) REQ_CHECK_SIZES_EX(Name, Name ## _SIZE_IN, Name ## _SIZE_OUT)
4024
4025#define REQ_CHECK_SIZE_IN(Name, cbInExpect) \
4026 do { \
4027 if (RT_LIKELY(pReqHdr->cbIn == (cbInExpect))) \
4028 { /* likely */ } \
4029 else \
4030 { \
4031 Log(( #Name ": Invalid input/output sizes. cbIn=%ld expected %ld.\n", \
4032 (long)pReqHdr->cbIn, (long)(cbInExpect))); \
4033 return pReqHdr->rc = VERR_INVALID_PARAMETER; \
4034 } \
4035 } while (0)
4036
4037#define REQ_CHECK_SIZE_OUT(Name, cbOutExpect) \
4038 do { \
4039 if (RT_LIKELY( pReqHdr->cbOut == (cbOutExpect) \
4040 || (pReqHdr->cbOut == 0 && pReqHdr->cbIn == (cbOutExpect)))) \
4041 { /* likely */ } \
4042 else \
4043 { \
4044 Log(( #Name ": Invalid input/output sizes. cbOut=%ld (%ld) expected %ld.\n", \
4045 (long)pReqHdr->cbOut, (long)pReqHdr->cbIn, (long)(cbOutExpect))); \
4046 return pReqHdr->rc = VERR_INVALID_PARAMETER; \
4047 } \
4048 } while (0)
4049
4050#define REQ_CHECK_EXPR(Name, expr) \
4051 do { \
4052 if (RT_LIKELY(!!(expr))) \
4053 { /* likely */ } \
4054 else \
4055 { \
4056 Log(( #Name ": %s\n", #expr)); \
4057 return pReqHdr->rc = VERR_INVALID_PARAMETER; \
4058 } \
4059 } while (0)
4060
4061#define REQ_CHECK_EXPR_FMT(expr, fmt) \
4062 do { \
4063 if (RT_LIKELY(!!(expr))) \
4064 { /* likely */ } \
4065 else \
4066 { \
4067 Log( fmt ); \
4068 return pReqHdr->rc = VERR_INVALID_PARAMETER; \
4069 } \
4070 } while (0)
4071
4072#define REQ_CHECK_RING0(mnemonic) \
4073 do { \
4074 if (pSession->R0Process != NIL_RTR0PROCESS) \
4075 { \
4076 LogFunc((mnemonic ": Ring-0 only, caller is %RTproc/%p\n", \
4077 pSession->Process, (uintptr_t)pSession->R0Process)); \
4078 return pReqHdr->rc = VERR_PERMISSION_DENIED; \
4079 } \
4080 } while (0)
4081
4082
4083 /*
4084 * Validate the request.
4085 */
4086 if (RT_LIKELY(cbReq >= sizeof(*pReqHdr)))
4087 { /* likely */ }
4088 else
4089 {
4090 Log(("VGDrvCommonIoCtl: Bad ioctl request size; cbReq=%#lx\n", (long)cbReq));
4091 return VERR_INVALID_PARAMETER;
4092 }
4093
4094 if (pReqHdr->cbOut == 0)
4095 pReqHdr->cbOut = pReqHdr->cbIn;
4096
4097 if (RT_LIKELY( pReqHdr->uVersion == VBGLREQHDR_VERSION
4098 && pReqHdr->cbIn >= sizeof(*pReqHdr)
4099 && pReqHdr->cbIn <= cbReq
4100 && pReqHdr->cbOut >= sizeof(*pReqHdr)
4101 && pReqHdr->cbOut <= cbReq))
4102 { /* likely */ }
4103 else
4104 {
4105 Log(("VGDrvCommonIoCtl: Bad ioctl request header; cbIn=%#lx cbOut=%#lx version=%#lx\n",
4106 (long)pReqHdr->cbIn, (long)pReqHdr->cbOut, (long)pReqHdr->uVersion));
4107 return VERR_INVALID_PARAMETER;
4108 }
4109
4110 if (RT_LIKELY(RT_VALID_PTR(pSession)))
4111 { /* likely */ }
4112 else
4113 {
4114 Log(("VGDrvCommonIoCtl: Invalid pSession value %p (ioctl=%#x)\n", pSession, iFunction));
4115 return VERR_INVALID_PARAMETER;
4116 }
4117
4118
4119 /*
4120 * Deal with variably sized requests first.
4121 */
4122 rc = VINF_SUCCESS;
4123 if ( iFunctionStripped == VBGL_IOCTL_CODE_STRIPPED(VBGL_IOCTL_VMMDEV_REQUEST(0))
4124 || iFunctionStripped == VBGL_IOCTL_CODE_STRIPPED(VBGL_IOCTL_VMMDEV_REQUEST_BIG) )
4125 {
4126 REQ_CHECK_EXPR(VBGL_IOCTL_VMMDEV_REQUEST, pReqHdr->uType != VBGLREQHDR_TYPE_DEFAULT);
4127 REQ_CHECK_EXPR_FMT(pReqHdr->cbIn == pReqHdr->cbOut,
4128 ("VBGL_IOCTL_VMMDEV_REQUEST: cbIn=%ld != cbOut=%ld\n", (long)pReqHdr->cbIn, (long)pReqHdr->cbOut));
4129 pReqHdr->rc = vgdrvIoCtl_VMMDevRequest(pDevExt, pSession, (VMMDevRequestHeader *)pReqHdr, cbReq);
4130 }
4131 else if (RT_LIKELY(pReqHdr->uType == VBGLREQHDR_TYPE_DEFAULT))
4132 {
4133 if (iFunctionStripped == VBGL_IOCTL_CODE_STRIPPED(VBGL_IOCTL_LOG(0)))
4134 {
4135 REQ_CHECK_SIZE_OUT(VBGL_IOCTL_LOG, VBGL_IOCTL_LOG_SIZE_OUT);
4136 pReqHdr->rc = vgdrvIoCtl_Log(pDevExt, &((PVBGLIOCLOG)pReqHdr)->u.In.szMsg[0], pReqHdr->cbIn - sizeof(VBGLREQHDR),
4137 pSession->fUserSession);
4138 }
4139#ifdef VBOX_WITH_HGCM
4140 else if (iFunction == VBGL_IOCTL_IDC_HGCM_FAST_CALL) /* (is variable size, but we don't bother encoding it) */
4141 {
4142 REQ_CHECK_RING0("VBGL_IOCTL_IDC_HGCM_FAST_CALL");
4143 REQ_CHECK_EXPR(VBGL_IOCTL_IDC_HGCM_FAST_CALL, cbReq >= sizeof(VBGLIOCIDCHGCMFASTCALL) + sizeof(VMMDevHGCMCall));
4144 vgdrvIoCtl_HGCMFastCall(pDevExt, (VBGLIOCIDCHGCMFASTCALL volatile *)pReqHdr);
4145 }
4146 else if ( iFunctionStripped == VBGL_IOCTL_CODE_STRIPPED(VBGL_IOCTL_HGCM_CALL(0))
4147# if ARCH_BITS == 64
4148 || iFunctionStripped == VBGL_IOCTL_CODE_STRIPPED(VBGL_IOCTL_HGCM_CALL_32(0))
4149# endif
4150 )
4151 {
4152 REQ_CHECK_EXPR(VBGL_IOCTL_HGCM_CALL, pReqHdr->cbIn >= sizeof(VBGLIOCHGCMCALL));
4153 REQ_CHECK_EXPR(VBGL_IOCTL_HGCM_CALL, pReqHdr->cbIn == pReqHdr->cbOut);
4154 pReqHdr->rc = vgdrvIoCtl_HGCMCallWrapper(pDevExt, pSession, (PVBGLIOCHGCMCALL)pReqHdr,
4155 iFunctionStripped == VBGL_IOCTL_CODE_STRIPPED(VBGL_IOCTL_HGCM_CALL_32(0)),
4156 false /*fUserData*/, cbReq);
4157 }
4158 else if (iFunctionStripped == VBGL_IOCTL_CODE_STRIPPED(VBGL_IOCTL_HGCM_CALL_WITH_USER_DATA(0)))
4159 {
4160 REQ_CHECK_RING0("VBGL_IOCTL_HGCM_CALL_WITH_USER_DATA");
4161 REQ_CHECK_EXPR(VBGL_IOCTL_HGCM_CALL, pReqHdr->cbIn >= sizeof(VBGLIOCHGCMCALL));
4162 REQ_CHECK_EXPR(VBGL_IOCTL_HGCM_CALL, pReqHdr->cbIn == pReqHdr->cbOut);
4163 pReqHdr->rc = vgdrvIoCtl_HGCMCallWrapper(pDevExt, pSession, (PVBGLIOCHGCMCALL)pReqHdr,
4164 ARCH_BITS == 32, true /*fUserData*/, cbReq);
4165 }
4166#endif /* VBOX_WITH_HGCM */
4167 else
4168 {
4169 switch (iFunction)
4170 {
4171 /*
4172 * Ring-0 only:
4173 */
4174 case VBGL_IOCTL_IDC_CONNECT:
4175 REQ_CHECK_RING0("VBGL_IOCL_IDC_CONNECT");
4176 REQ_CHECK_SIZES(VBGL_IOCTL_IDC_CONNECT);
4177 pReqHdr->rc = vgdrvIoCtl_IdcConnect(pDevExt, pSession, (PVBGLIOCIDCCONNECT)pReqHdr);
4178 break;
4179
4180 case VBGL_IOCTL_IDC_DISCONNECT:
4181 REQ_CHECK_RING0("VBGL_IOCTL_IDC_DISCONNECT");
4182 REQ_CHECK_SIZES(VBGL_IOCTL_IDC_DISCONNECT);
4183 pReqHdr->rc = vgdrvIoCtl_IdcDisconnect(pDevExt, pSession, (PVBGLIOCIDCDISCONNECT)pReqHdr);
4184 break;
4185
4186 case VBGL_IOCTL_GET_VMMDEV_IO_INFO:
4187 REQ_CHECK_RING0("GET_VMMDEV_IO_INFO");
4188 REQ_CHECK_SIZES(VBGL_IOCTL_GET_VMMDEV_IO_INFO);
4189 pReqHdr->rc = vgdrvIoCtl_GetVMMDevIoInfo(pDevExt, (PVBGLIOCGETVMMDEVIOINFO)pReqHdr);
4190 break;
4191
4192 case VBGL_IOCTL_SET_MOUSE_NOTIFY_CALLBACK:
4193 REQ_CHECK_RING0("SET_MOUSE_NOTIFY_CALLBACK");
4194 REQ_CHECK_SIZES(VBGL_IOCTL_SET_MOUSE_NOTIFY_CALLBACK);
4195 pReqHdr->rc = vgdrvIoCtl_SetMouseNotifyCallback(pDevExt, (PVBGLIOCSETMOUSENOTIFYCALLBACK)pReqHdr);
4196 break;
4197
4198 /*
4199 * Ring-3 only:
4200 */
4201 case VBGL_IOCTL_DRIVER_VERSION_INFO:
4202 REQ_CHECK_SIZES(VBGL_IOCTL_DRIVER_VERSION_INFO);
4203 pReqHdr->rc = vgdrvIoCtl_DriverVersionInfo(pDevExt, pSession, (PVBGLIOCDRIVERVERSIONINFO)pReqHdr);
4204 break;
4205
4206 /*
4207 * Both ring-3 and ring-0:
4208 */
4209 case VBGL_IOCTL_WAIT_FOR_EVENTS:
4210 REQ_CHECK_SIZES(VBGL_IOCTL_WAIT_FOR_EVENTS);
4211 pReqHdr->rc = vgdrvIoCtl_WaitForEvents(pDevExt, pSession, (VBGLIOCWAITFOREVENTS *)pReqHdr,
4212 pSession->R0Process != NIL_RTR0PROCESS);
4213 break;
4214
4215 case VBGL_IOCTL_INTERRUPT_ALL_WAIT_FOR_EVENTS:
4216 REQ_CHECK_SIZES(VBGL_IOCTL_INTERRUPT_ALL_WAIT_FOR_EVENTS);
4217 pReqHdr->rc = vgdrvIoCtl_CancelAllWaitEvents(pDevExt, pSession);
4218 break;
4219
4220 case VBGL_IOCTL_CHANGE_FILTER_MASK:
4221 REQ_CHECK_SIZES(VBGL_IOCTL_CHANGE_FILTER_MASK);
4222 pReqHdr->rc = vgdrvIoCtl_ChangeFilterMask(pDevExt, pSession, (PVBGLIOCCHANGEFILTERMASK)pReqHdr);
4223 break;
4224
4225#ifdef VBOX_WITH_HGCM
4226 case VBGL_IOCTL_HGCM_CONNECT:
4227 REQ_CHECK_SIZES(VBGL_IOCTL_HGCM_CONNECT);
4228 pReqHdr->rc = vgdrvIoCtl_HGCMConnect(pDevExt, pSession, (PVBGLIOCHGCMCONNECT)pReqHdr);
4229 break;
4230
4231 case VBGL_IOCTL_HGCM_DISCONNECT:
4232 REQ_CHECK_SIZES(VBGL_IOCTL_HGCM_DISCONNECT);
4233 pReqHdr->rc = vgdrvIoCtl_HGCMDisconnect(pDevExt, pSession, (PVBGLIOCHGCMDISCONNECT)pReqHdr);
4234 break;
4235#endif
4236
4237 case VBGL_IOCTL_CHECK_BALLOON:
4238 REQ_CHECK_SIZES(VBGL_IOCTL_CHECK_BALLOON);
4239 pReqHdr->rc = vgdrvIoCtl_CheckMemoryBalloon(pDevExt, pSession, (PVBGLIOCCHECKBALLOON)pReqHdr);
4240 break;
4241
4242 case VBGL_IOCTL_CHANGE_BALLOON:
4243 REQ_CHECK_SIZES(VBGL_IOCTL_CHANGE_BALLOON);
4244 pReqHdr->rc = vgdrvIoCtl_ChangeMemoryBalloon(pDevExt, pSession, (PVBGLIOCCHANGEBALLOON)pReqHdr);
4245 break;
4246
4247 case VBGL_IOCTL_WRITE_CORE_DUMP:
4248 REQ_CHECK_SIZES(VBGL_IOCTL_WRITE_CORE_DUMP);
4249 pReqHdr->rc = vgdrvIoCtl_WriteCoreDump(pDevExt, pSession, (PVBGLIOCWRITECOREDUMP)pReqHdr);
4250 break;
4251
4252 case VBGL_IOCTL_SET_MOUSE_STATUS:
4253 REQ_CHECK_SIZES(VBGL_IOCTL_SET_MOUSE_STATUS);
4254 pReqHdr->rc = vgdrvIoCtl_SetMouseStatus(pDevExt, pSession, ((PVBGLIOCSETMOUSESTATUS)pReqHdr)->u.In.fStatus);
4255 break;
4256
4257 case VBGL_IOCTL_ACQUIRE_GUEST_CAPABILITIES:
4258 REQ_CHECK_SIZES(VBGL_IOCTL_ACQUIRE_GUEST_CAPABILITIES);
4259 pReqHdr->rc = vgdrvIoCtl_GuestCapsAcquire(pDevExt, pSession, (PVBGLIOCACQUIREGUESTCAPS)pReqHdr);
4260 break;
4261
4262 case VBGL_IOCTL_CHANGE_GUEST_CAPABILITIES:
4263 REQ_CHECK_SIZES(VBGL_IOCTL_CHANGE_GUEST_CAPABILITIES);
4264 pReqHdr->rc = vgdrvIoCtl_SetCapabilities(pDevExt, pSession, (PVBGLIOCSETGUESTCAPS)pReqHdr);
4265 break;
4266
4267#ifdef VBOX_WITH_DPC_LATENCY_CHECKER
4268 case VBGL_IOCTL_DPC_LATENCY_CHECKER:
4269 REQ_CHECK_SIZES(VBGL_IOCTL_DPC_LATENCY_CHECKER);
4270 pReqHdr->rc = VGDrvNtIOCtl_DpcLatencyChecker();
4271 break;
4272#endif
4273
4274 default:
4275 {
4276 LogRel(("VGDrvCommonIoCtl: Unknown request iFunction=%#x (stripped %#x) cbReq=%#x\n",
4277 iFunction, iFunctionStripped, cbReq));
4278 pReqHdr->rc = rc = VERR_NOT_SUPPORTED;
4279 break;
4280 }
4281 }
4282 }
4283 }
4284 else
4285 {
4286 Log(("VGDrvCommonIoCtl: uType=%#x, expected default (ioctl=%#x)\n", pReqHdr->uType, iFunction));
4287 return VERR_INVALID_PARAMETER;
4288 }
4289
4290 LogFlow(("VGDrvCommonIoCtl: returns %Rrc (req: rc=%Rrc cbOut=%#x)\n", rc, pReqHdr->rc, pReqHdr->cbOut));
4291 return rc;
4292}
4293
4294
4295/**
4296 * Used by VGDrvCommonISR as well as the acquire guest capability code.
4297 *
4298 * @returns VINF_SUCCESS on success. On failure, ORed together
4299 * RTSemEventMultiSignal errors (completes processing despite errors).
4300 * @param pDevExt The VBoxGuest device extension.
4301 * @param fEvents The events to dispatch.
4302 */
4303static int vgdrvDispatchEventsLocked(PVBOXGUESTDEVEXT pDevExt, uint32_t fEvents)
4304{
4305 PVBOXGUESTWAIT pWait;
4306 PVBOXGUESTWAIT pSafe;
4307 int rc = VINF_SUCCESS;
4308
4309 fEvents |= pDevExt->f32PendingEvents;
4310
4311 RTListForEachSafe(&pDevExt->WaitList, pWait, pSafe, VBOXGUESTWAIT, ListNode)
4312 {
4313 uint32_t fHandledEvents = pWait->fReqEvents & fEvents;
4314 if ( fHandledEvents != 0
4315 && !pWait->fResEvents)
4316 {
4317 /* Does this one wait on any of the events we're dispatching? We do a quick
4318 check first, then deal with VBOXGUEST_ACQUIRE_STYLE_EVENTS as applicable. */
4319 if (fHandledEvents & VBOXGUEST_ACQUIRE_STYLE_EVENTS)
4320 fHandledEvents &= vgdrvGetAllowedEventMaskForSession(pDevExt, pWait->pSession);
4321 if (fHandledEvents)
4322 {
4323 pWait->fResEvents = pWait->fReqEvents & fEvents & fHandledEvents;
4324 fEvents &= ~pWait->fResEvents;
4325 RTListNodeRemove(&pWait->ListNode);
4326#ifdef VBOXGUEST_USE_DEFERRED_WAKE_UP
4327 RTListAppend(&pDevExt->WakeUpList, &pWait->ListNode);
4328#else
4329 RTListAppend(&pDevExt->WokenUpList, &pWait->ListNode);
4330 rc |= RTSemEventMultiSignal(pWait->Event);
4331#endif
4332 if (!fEvents)
4333 break;
4334 }
4335 }
4336 }
4337
4338 ASMAtomicWriteU32(&pDevExt->f32PendingEvents, fEvents);
4339 return rc;
4340}
4341
4342
4343/**
4344 * Simply checks whether the IRQ is ours or not, does not do any interrupt
4345 * procesing.
4346 *
4347 * @returns true if it was our interrupt, false if it wasn't.
4348 * @param pDevExt The VBoxGuest device extension.
4349 */
4350bool VGDrvCommonIsOurIRQ(PVBOXGUESTDEVEXT pDevExt)
4351{
4352 VMMDevMemory volatile *pVMMDevMemory;
4353 bool fOurIrq;
4354
4355 RTSpinlockAcquire(pDevExt->EventSpinlock);
4356 pVMMDevMemory = pDevExt->pVMMDevMemory;
4357 fOurIrq = pVMMDevMemory ? pVMMDevMemory->V.V1_04.fHaveEvents : false;
4358 RTSpinlockRelease(pDevExt->EventSpinlock);
4359
4360 return fOurIrq;
4361}
4362
4363
4364/**
4365 * Common interrupt service routine.
4366 *
4367 * This deals with events and with waking up thread waiting for those events.
4368 *
4369 * @returns true if it was our interrupt, false if it wasn't.
4370 * @param pDevExt The VBoxGuest device extension.
4371 */
4372bool VGDrvCommonISR(PVBOXGUESTDEVEXT pDevExt)
4373{
4374 VMMDevEvents volatile *pReq;
4375 bool fMousePositionChanged = false;
4376 int rc = 0;
4377 VMMDevMemory volatile *pVMMDevMemory;
4378 bool fOurIrq;
4379
4380 /*
4381 * Make sure we've initialized the device extension.
4382 */
4383 if (RT_LIKELY(pDevExt->fHostFeatures & VMMDEV_HVF_FAST_IRQ_ACK))
4384 pReq = NULL;
4385 else if (RT_LIKELY((pReq = pDevExt->pIrqAckEvents) != NULL))
4386 { /* likely */ }
4387 else
4388 return false;
4389
4390 /*
4391 * Enter the spinlock and check if it's our IRQ or not.
4392 */
4393 RTSpinlockAcquire(pDevExt->EventSpinlock);
4394 pVMMDevMemory = pDevExt->pVMMDevMemory;
4395 fOurIrq = pVMMDevMemory ? pVMMDevMemory->V.V1_04.fHaveEvents : false;
4396 if (fOurIrq)
4397 {
4398 /*
4399 * Acknowledge events.
4400 * We don't use VbglR0GRPerform here as it may take another spinlocks.
4401 */
4402 uint32_t fEvents;
4403 if (!pReq)
4404 {
4405 fEvents = ASMInU32(pDevExt->IOPortBase + VMMDEV_PORT_OFF_REQUEST_FAST);
4406 ASMCompilerBarrier(); /* paranoia */
4407 rc = fEvents != UINT32_MAX ? VINF_SUCCESS : VERR_INTERNAL_ERROR;
4408 }
4409 else
4410 {
4411 pReq->header.rc = VERR_INTERNAL_ERROR;
4412 pReq->events = 0;
4413 ASMCompilerBarrier();
4414 ASMOutU32(pDevExt->IOPortBase + VMMDEV_PORT_OFF_REQUEST, (uint32_t)pDevExt->PhysIrqAckEvents);
4415 ASMCompilerBarrier(); /* paranoia */
4416 fEvents = pReq->events;
4417 rc = pReq->header.rc;
4418 }
4419 if (RT_SUCCESS(rc))
4420 {
4421 Log3(("VGDrvCommonISR: acknowledge events succeeded %#RX32\n", fEvents));
4422
4423 /*
4424 * VMMDEV_EVENT_MOUSE_POSITION_CHANGED can only be polled for.
4425 */
4426 if (fEvents & VMMDEV_EVENT_MOUSE_POSITION_CHANGED)
4427 {
4428 fMousePositionChanged = true;
4429 fEvents &= ~VMMDEV_EVENT_MOUSE_POSITION_CHANGED;
4430#if !defined(VBOXGUEST_MOUSE_NOTIFY_CAN_PREEMPT)
4431 if (pDevExt->pfnMouseNotifyCallback)
4432 pDevExt->pfnMouseNotifyCallback(pDevExt->pvMouseNotifyCallbackArg);
4433#endif
4434 }
4435
4436#ifdef VBOX_WITH_HGCM
4437 /*
4438 * The HGCM event/list is kind of different in that we evaluate all entries.
4439 */
4440 if (fEvents & VMMDEV_EVENT_HGCM)
4441 {
4442 PVBOXGUESTWAIT pWait;
4443 PVBOXGUESTWAIT pSafe;
4444 RTListForEachSafe(&pDevExt->HGCMWaitList, pWait, pSafe, VBOXGUESTWAIT, ListNode)
4445 {
4446 if (pWait->pHGCMReq->fu32Flags & VBOX_HGCM_REQ_DONE)
4447 {
4448 pWait->fResEvents = VMMDEV_EVENT_HGCM;
4449 RTListNodeRemove(&pWait->ListNode);
4450# ifdef VBOXGUEST_USE_DEFERRED_WAKE_UP
4451 RTListAppend(&pDevExt->WakeUpList, &pWait->ListNode);
4452# else
4453 RTListAppend(&pDevExt->WokenUpList, &pWait->ListNode);
4454 rc |= RTSemEventMultiSignal(pWait->Event);
4455# endif
4456 }
4457 }
4458 fEvents &= ~VMMDEV_EVENT_HGCM;
4459 }
4460#endif
4461
4462 /*
4463 * Normal FIFO waiter evaluation.
4464 */
4465 rc |= vgdrvDispatchEventsLocked(pDevExt, fEvents);
4466 }
4467 else /* something is serious wrong... */
4468 Log(("VGDrvCommonISR: acknowledge events failed rc=%Rrc (events=%#x)!!\n", rc, fEvents));
4469 }
4470 else
4471 Log3(("VGDrvCommonISR: not ours\n"));
4472
4473 RTSpinlockRelease(pDevExt->EventSpinlock);
4474
4475 /*
4476 * Execute the mouse notification callback here if it cannot be executed while
4477 * holding the interrupt safe spinlock, see @bugref{8639}.
4478 */
4479#if defined(VBOXGUEST_MOUSE_NOTIFY_CAN_PREEMPT) && !defined(RT_OS_WINDOWS) /* (Windows does this in the Dpc callback) */
4480 if ( fMousePositionChanged
4481 && pDevExt->pfnMouseNotifyCallback)
4482 pDevExt->pfnMouseNotifyCallback(pDevExt->pvMouseNotifyCallbackArg);
4483#endif
4484
4485#if defined(VBOXGUEST_USE_DEFERRED_WAKE_UP) && !defined(RT_OS_WINDOWS)
4486 /*
4487 * Do wake-ups.
4488 * Note. On Windows this isn't possible at this IRQL, so a DPC will take
4489 * care of it. Same on darwin, doing it in the work loop callback.
4490 */
4491 VGDrvCommonWaitDoWakeUps(pDevExt);
4492#endif
4493
4494 /*
4495 * Work the poll and async notification queues on OSes that implements that.
4496 * (Do this outside the spinlock to prevent some recursive spinlocking.)
4497 */
4498 if (fMousePositionChanged)
4499 {
4500 ASMAtomicIncU32(&pDevExt->u32MousePosChangedSeq);
4501 VGDrvNativeISRMousePollEvent(pDevExt);
4502 }
4503
4504 AssertMsg(rc == 0, ("rc=%#x (%d)\n", rc, rc));
4505 return fOurIrq;
4506}
4507
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