VirtualBox

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

Last change on this file since 14352 was 14352, checked in by vboxsync, 16 years ago

Additions/HGCM: merged code for HGCMCall and HGCMCallTimed, as per todo

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 48.5 KB
Line 
1/** $Id: */
2/** @file
3 * VBoxGuest - Guest Additions Driver.
4 */
5
6/*
7 * Copyright (C) 2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23
24/*******************************************************************************
25* Header Files *
26*******************************************************************************/
27#define LOG_GROUP LOG_GROUP_DEFAULT
28#include "VBoxGuestInternal.h"
29#include <VBox/VBoxDev.h> /* for VMMDEV_RAM_SIZE */
30#include <VBox/log.h>
31#include <iprt/mem.h>
32#include <iprt/time.h>
33#include <iprt/memobj.h>
34#include <iprt/asm.h>
35#include <iprt/string.h>
36#include <iprt/process.h>
37#include <iprt/assert.h>
38#include <iprt/param.h>
39#ifdef VBOX_WITH_HGCM
40# include <iprt/thread.h>
41#endif
42
43
44/*******************************************************************************
45* Internal Functions *
46*******************************************************************************/
47#ifdef VBOX_WITH_HGCM
48static DECLCALLBACK(void) VBoxGuestHGCMAsyncWaitCallback(VMMDevHGCMRequestHeader *pHdrNonVolatile, void *pvUser, uint32_t u32User);
49#endif
50
51
52
53/**
54 * Reserves memory in which the VMM can relocate any guest mappings
55 * that are floating around.
56 *
57 * This operation is a little bit tricky since the VMM might not accept
58 * just any address because of address clashes between the three contexts
59 * it operates in, so use a small stack to perform this operation.
60 *
61 * @returns VBox status code (ignored).
62 * @param pDevExt The device extension.
63 */
64static int vboxGuestInitFixateGuestMappings(PVBOXGUESTDEVEXT pDevExt)
65{
66 /** @todo implement this using RTR0MemObjReserveKernel() (it needs to be implemented everywhere too). */
67 return VINF_SUCCESS;
68}
69
70
71/**
72 * Initializes the interrupt filter mask.
73 *
74 * This will ASSUME that we're the ones in carge over the mask, so
75 * we'll simply clear all bits we don't set.
76 *
77 * @returns VBox status code (ignored).
78 * @param pDevExt The device extension.
79 * @param fMask The new mask.
80 */
81static int vboxGuestInitFilterMask(PVBOXGUESTDEVEXT pDevExt, uint32_t fMask)
82{
83 VMMDevCtlGuestFilterMask *pReq;
84 int rc = VbglGRAlloc((VMMDevRequestHeader **)&pReq, sizeof(*pReq), VMMDevReq_CtlGuestFilterMask);
85 if (RT_SUCCESS(rc))
86 {
87 pReq->u32OrMask = fMask;
88 pReq->u32NotMask = ~fMask; /* It's an AND mask. */
89 rc = VbglGRPerform(&pReq->header);
90 if ( RT_FAILURE(rc)
91 || RT_FAILURE(pReq->header.rc))
92 LogRel(("vboxGuestInitCtlFilterMask: failed with rc=%Rrc and VMMDev rc=%Rrc\n",
93 rc, pReq->header.rc));
94 VbglGRFree(&pReq->header);
95 }
96 return rc;
97}
98
99
100/**
101 * Report guest information to the VMMDev.
102 *
103 * @returns VBox status code.
104 * @param pDevExt The device extension.
105 * @param enmOSType The OS type to report.
106 */
107static int vboxGuestInitReportGuestInfo(PVBOXGUESTDEVEXT pDevExt, VBOXOSTYPE enmOSType)
108{
109 VMMDevReportGuestInfo *pReq;
110 int rc = VbglGRAlloc((VMMDevRequestHeader **)&pReq, sizeof(*pReq), VMMDevReq_ReportGuestInfo);
111 if (RT_SUCCESS(rc))
112 {
113 pReq->guestInfo.additionsVersion = VMMDEV_VERSION;
114 pReq->guestInfo.osType = enmOSType;
115 rc = VbglGRPerform(&pReq->header);
116 if ( RT_FAILURE(rc)
117 || RT_FAILURE(pReq->header.rc))
118 LogRel(("vboxGuestInitReportGuestInfo: failed with rc=%Rrc and VMMDev rc=%Rrc\n",
119 rc, pReq->header.rc));
120 VbglGRFree(&pReq->header);
121 }
122 return rc;
123}
124
125
126/**
127 * Initializes the VBoxGuest device extension when the
128 * device driver is loaded.
129 *
130 * The native code locates the VMMDev on the PCI bus and retrieve
131 * the MMIO and I/O port ranges, this function will take care of
132 * mapping the MMIO memory (if present). Upon successful return
133 * the native code should set up the interrupt handler.
134 *
135 * @returns VBox status code.
136 *
137 * @param pDevExt The device extension. Allocated by the native code.
138 * @param IOPortBase The base of the I/O port range.
139 * @param pvMMIOBase The base of the MMIO memory mapping.
140 * This is optional, pass NULL if not present.
141 * @param cbMMIO The size of the MMIO memory mapping.
142 * This is optional, pass 0 if not present.
143 * @param enmOSType The guest OS type to report to the VMMDev.
144 */
145int VBoxGuestInitDevExt(PVBOXGUESTDEVEXT pDevExt, uint16_t IOPortBase,
146 void *pvMMIOBase, uint32_t cbMMIO, VBOXOSTYPE enmOSType)
147{
148 int rc, rc2;
149
150 /*
151 * Initalize the data.
152 */
153 pDevExt->IOPortBase = IOPortBase;
154 pDevExt->pVMMDevMemory = NULL;
155 pDevExt->pIrqAckEvents = NULL;
156 pDevExt->WaitList.pHead = NULL;
157 pDevExt->WaitList.pTail = NULL;
158#ifdef VBOX_WITH_HGCM
159 pDevExt->HGCMWaitList.pHead = NULL;
160 pDevExt->HGCMWaitList.pTail = NULL;
161#endif
162 pDevExt->FreeList.pHead = NULL;
163 pDevExt->FreeList.pTail = NULL;
164 pDevExt->f32PendingEvents = 0;
165 pDevExt->u32ClipboardClientId = 0;
166
167 /*
168 * If there is an MMIO region validate the version and size.
169 */
170 if (pvMMIOBase)
171 {
172 Assert(cbMMIO);
173 VMMDevMemory *pVMMDev = (VMMDevMemory *)pvMMIOBase;
174 if ( pVMMDev->u32Version == VMMDEV_MEMORY_VERSION
175 && pVMMDev->u32Size >= 32
176 && pVMMDev->u32Size <= cbMMIO)
177 {
178 pDevExt->pVMMDevMemory = pVMMDev;
179 Log(("VBoxGuestInitDevExt: VMMDevMemory: mapping=%p size=%#RX32 (%#RX32) version=%#RX32\n",
180 pVMMDev, pVMMDev->u32Size, cbMMIO, pVMMDev->u32Version));
181 }
182 else /* try live without it. */
183 LogRel(("VBoxGuestInitDevExt: Bogus VMMDev memory; u32Version=%RX32 (expected %RX32) u32Size=%RX32 (expected <= %RX32)\n",
184 pVMMDev->u32Version, VMMDEV_MEMORY_VERSION, pVMMDev->u32Size, cbMMIO));
185 }
186
187 /*
188 * Create the wait and seesion spinlocks.
189 */
190 rc = RTSpinlockCreate(&pDevExt->WaitSpinlock);
191 if (RT_SUCCESS(rc))
192 rc = RTSpinlockCreate(&pDevExt->SessionSpinlock);
193 if (RT_FAILURE(rc))
194 {
195 Log(("VBoxGuestInitDevExt: failed to spinlock, rc=%d!\n", rc));
196 if (pDevExt->WaitSpinlock != NIL_RTSPINLOCK)
197 RTSpinlockDestroy(pDevExt->WaitSpinlock);
198 return rc;
199 }
200
201 /*
202 * Initialize the guest library and report the guest info back to VMMDev,
203 * set the interrupt control filter mask, and fixate the guest mappings
204 * made by the VMM.
205 */
206 rc = VbglInit(pDevExt->IOPortBase, (VMMDevMemory *)pDevExt->pVMMDevMemory);
207 if (RT_SUCCESS(rc))
208 {
209 rc = VbglGRAlloc((VMMDevRequestHeader **)&pDevExt->pIrqAckEvents, sizeof(VMMDevEvents), VMMDevReq_AcknowledgeEvents);
210 if (RT_SUCCESS(rc))
211 {
212 rc = vboxGuestInitReportGuestInfo(pDevExt, enmOSType);
213 if (RT_SUCCESS(rc))
214 {
215#ifdef VBOX_WITH_HGCM
216 rc = vboxGuestInitFilterMask(pDevExt, VMMDEV_EVENT_HGCM);
217#else
218 rc = vboxGuestInitFilterMask(pDevExt, 0);
219#endif
220 if (RT_SUCCESS(rc))
221 {
222 /*
223 * Disable guest graphics capability by default. The guest specific
224 * graphics driver will re-enable this when it is necessary.
225 */
226 rc = VBoxGuestSetGuestCapabilities(0, VMMDEV_GUEST_SUPPORTS_GRAPHICS);
227 if (RT_SUCCESS(rc))
228 {
229 vboxGuestInitFixateGuestMappings(pDevExt);
230 Log(("VBoxGuestInitDevExt: returns success\n"));
231 return VINF_SUCCESS;
232 }
233 }
234 }
235
236 /* failure cleanup */
237 }
238 else
239 Log(("VBoxGuestInitDevExt: VBoxGRAlloc failed, rc=%Rrc\n", rc));
240
241 VbglTerminate();
242 }
243 else
244 Log(("VBoxGuestInitDevExt: VbglInit failed, rc=%Rrc\n", rc));
245
246 rc2 = RTSpinlockDestroy(pDevExt->WaitSpinlock); AssertRC(rc2);
247 rc2 = RTSpinlockDestroy(pDevExt->SessionSpinlock); AssertRC(rc2);
248 return rc; /* (failed) */
249}
250
251
252/**
253 * Deletes all the items in a wait chain.
254 * @param pWait The head of the chain.
255 */
256static void VBoxGuestDeleteWaitList(PVBOXGUESTWAITLIST pList)
257{
258 while (pList->pHead)
259 {
260 PVBOXGUESTWAIT pWait = pList->pHead;
261 pList->pHead = pWait->pNext;
262
263 pWait->pNext = NULL;
264 pWait->pPrev = NULL;
265 int rc2 = RTSemEventMultiDestroy(pWait->Event); AssertRC(rc2);
266 pWait->Event = NIL_RTSEMEVENTMULTI;
267 RTMemFree(pWait);
268 }
269 pList->pHead = NULL;
270 pList->pTail = NULL;
271}
272
273
274/**
275 * Destroys the VBoxGuest device extension.
276 *
277 * The native code should call this before the driver is loaded,
278 * but don't call this on shutdown.
279 *
280 * @param pDevExt The device extension.
281 */
282void VBoxGuestDeleteDevExt(PVBOXGUESTDEVEXT pDevExt)
283{
284 int rc2;
285 Log(("VBoxGuestDeleteDevExt:\n"));
286
287/** @todo tell VMMDev that the guest additions are no longer running (clear all capability masks).
288 * Like calling VBoxGuestSetGuestCapabilities. This wasn't done initially since it was not
289 * relevant for OS/2. On solaris modules can be unloaded, so we should implement it.
290 */
291
292 rc2 = RTSpinlockDestroy(pDevExt->WaitSpinlock); AssertRC(rc2);
293 rc2 = RTSpinlockDestroy(pDevExt->SessionSpinlock); AssertRC(rc2);
294
295 VBoxGuestDeleteWaitList(&pDevExt->WaitList);
296#ifdef VBOX_WITH_HGCM
297 VBoxGuestDeleteWaitList(&pDevExt->HGCMWaitList);
298#endif
299 VBoxGuestDeleteWaitList(&pDevExt->FreeList);
300
301 VbglTerminate();
302
303 pDevExt->pVMMDevMemory = NULL;
304
305 pDevExt->IOPortBase = 0;
306 pDevExt->pIrqAckEvents = NULL;
307}
308
309
310/**
311 * Creates a VBoxGuest user session.
312 *
313 * The native code calls this when a ring-3 client opens the device.
314 * Use VBoxGuestCreateKernelSession when a ring-0 client connects.
315 *
316 * @returns VBox status code.
317 * @param pDevExt The device extension.
318 * @param ppSession Where to store the session on success.
319 */
320int VBoxGuestCreateUserSession(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION *ppSession)
321{
322 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)RTMemAllocZ(sizeof(*pSession));
323 if (RT_UNLIKELY(!pSession))
324 {
325 LogRel(("VBoxGuestCreateUserSession: no memory!\n"));
326 return VERR_NO_MEMORY;
327 }
328
329 pSession->Process = RTProcSelf();
330 pSession->R0Process = RTR0ProcHandleSelf();
331 pSession->pDevExt = pDevExt;
332
333 *ppSession = pSession;
334 LogFlow(("VBoxGuestCreateUserSession: pSession=%p proc=%RTproc (%d) r0proc=%p\n",
335 pSession, pSession->Process, (int)pSession->Process, (uintptr_t)pSession->R0Process)); /** @todo %RTr0proc */
336 return VINF_SUCCESS;
337}
338
339
340/**
341 * Creates a VBoxGuest kernel session.
342 *
343 * The native code calls this when a ring-0 client connects to the device.
344 * Use VBoxGuestCreateUserSession when a ring-3 client opens the device.
345 *
346 * @returns VBox status code.
347 * @param pDevExt The device extension.
348 * @param ppSession Where to store the session on success.
349 */
350int VBoxGuestCreateKernelSession(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION *ppSession)
351{
352 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)RTMemAllocZ(sizeof(*pSession));
353 if (RT_UNLIKELY(!pSession))
354 {
355 LogRel(("VBoxGuestCreateKernelSession: no memory!\n"));
356 return VERR_NO_MEMORY;
357 }
358
359 pSession->Process = NIL_RTPROCESS;
360 pSession->R0Process = NIL_RTR0PROCESS;
361 pSession->pDevExt = pDevExt;
362
363 *ppSession = pSession;
364 LogFlow(("VBoxGuestCreateKernelSession: pSession=%p proc=%RTproc (%d) r0proc=%p\n",
365 pSession, pSession->Process, (int)pSession->Process, (uintptr_t)pSession->R0Process)); /** @todo %RTr0proc */
366 return VINF_SUCCESS;
367}
368
369
370
371/**
372 * Closes a VBoxGuest session.
373 *
374 * @param pDevExt The device extension.
375 * @param pSession The session to close (and free).
376 */
377void VBoxGuestCloseSession(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession)
378{
379 Log(("VBoxGuestCloseSession: pSession=%p proc=%RTproc (%d) r0proc=%p\n",
380 pSession, pSession->Process, (int)pSession->Process, (uintptr_t)pSession->R0Process)); /** @todo %RTr0proc */
381
382#ifdef VBOX_WITH_HGCM
383 for (unsigned i = 0; i < RT_ELEMENTS(pSession->aHGCMClientIds); i++)
384 if (pSession->aHGCMClientIds[i])
385 {
386 VBoxGuestHGCMDisconnectInfo Info;
387 Info.result = 0;
388 Info.u32ClientID = pSession->aHGCMClientIds[i];
389 pSession->aHGCMClientIds[i] = 0;
390 Log(("VBoxGuestCloseSession: disconnecting client id %#RX32\n", Info.u32ClientID));
391 VbglHGCMDisconnect(&Info, VBoxGuestHGCMAsyncWaitCallback, pDevExt, RT_INDEFINITE_WAIT);
392 }
393#endif
394
395 pSession->pDevExt = NULL;
396 pSession->Process = NIL_RTPROCESS;
397 pSession->R0Process = NIL_RTR0PROCESS;
398 RTMemFree(pSession);
399}
400
401
402/**
403 * Links the wait-for-event entry into the tail of the given list.
404 *
405 * @param pList The list to link it into.
406 * @param pWait The wait for event entry to append.
407 */
408DECLINLINE(void) VBoxGuestWaitAppend(PVBOXGUESTWAITLIST pList, PVBOXGUESTWAIT pWait)
409{
410 const PVBOXGUESTWAIT pTail = pList->pTail;
411 pWait->pNext = NULL;
412 pWait->pPrev = pTail;
413 if (pTail)
414 pTail->pNext = pWait;
415 else
416 pList->pHead = pWait;
417 pList->pTail = pWait;
418}
419
420
421/**
422 * Unlinks the wait-for-event entry.
423 *
424 * @param pList The list to unlink it from.
425 * @param pWait The wait for event entry to unlink.
426 */
427DECLINLINE(void) VBoxGuestWaitUnlink(PVBOXGUESTWAITLIST pList, PVBOXGUESTWAIT pWait)
428{
429 const PVBOXGUESTWAIT pPrev = pWait->pPrev;
430 const PVBOXGUESTWAIT pNext = pWait->pNext;
431 if (pNext)
432 pNext->pPrev = pPrev;
433 else
434 pList->pTail = pPrev;
435 if (pPrev)
436 pPrev->pNext = pNext;
437 else
438 pList->pHead = pNext;
439}
440
441
442/**
443 * Allocates a wiat-for-event entry.
444 *
445 * @returns The wait-for-event entry.
446 * @param pDevExt The device extension.
447 */
448static PVBOXGUESTWAIT VBoxGuestWaitAlloc(PVBOXGUESTDEVEXT pDevExt)
449{
450 /*
451 * Allocate it one way or the other.
452 */
453 PVBOXGUESTWAIT pWait = pDevExt->FreeList.pTail;
454 if (pWait)
455 {
456 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
457 RTSpinlockAcquireNoInts(pDevExt->WaitSpinlock, &Tmp);
458
459 pWait = pDevExt->FreeList.pTail;
460 if (pWait)
461 VBoxGuestWaitUnlink(&pDevExt->FreeList, pWait);
462
463 RTSpinlockReleaseNoInts(pDevExt->WaitSpinlock, &Tmp);
464 }
465 if (!pWait)
466 {
467 static unsigned s_cErrors = 0;
468
469 pWait = (PVBOXGUESTWAIT)RTMemAlloc(sizeof(*pWait));
470 if (!pWait)
471 {
472 if (s_cErrors++ < 32)
473 LogRel(("VBoxGuestWaitAlloc: out-of-memory!\n"));
474 return NULL;
475 }
476
477 int rc = RTSemEventMultiCreate(&pWait->Event);
478 if (RT_FAILURE(rc))
479 {
480 if (s_cErrors++ < 32)
481 LogRel(("VBoxGuestCommonIOCtl: RTSemEventMultiCreate failed with rc=%Rrc!\n", rc));
482 RTMemFree(pWait);
483 return NULL;
484 }
485 }
486
487 /*
488 * Zero members just as an precaution.
489 */
490 pWait->pNext = NULL;
491 pWait->pPrev = NULL;
492 pWait->fReqEvents = 0;
493 pWait->fResEvents = 0;
494#ifdef VBOX_WITH_HGCM
495 pWait->pHGCMReq = NULL;
496#endif
497 RTSemEventMultiReset(pWait->Event);
498 return pWait;
499}
500
501
502/**
503 * Frees the wait-for-event entry.
504 * The caller must own the wait spinlock!
505 *
506 * @param pDevExt The device extension.
507 * @param pWait The wait-for-event entry to free.
508 */
509static void VBoxGuestWaitFreeLocked(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTWAIT pWait)
510{
511 pWait->fReqEvents = 0;
512 pWait->fResEvents = 0;
513#ifdef VBOX_WITH_HGCM
514 pWait->pHGCMReq = NULL;
515#endif
516 VBoxGuestWaitAppend(&pDevExt->FreeList, pWait);
517}
518
519
520/**
521 * Frees the wait-for-event entry.
522 *
523 * @param pDevExt The device extension.
524 * @param pWait The wait-for-event entry to free.
525 */
526static void VBoxGuestWaitFreeUnlocked(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTWAIT pWait)
527{
528 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
529 RTSpinlockAcquireNoInts(pDevExt->WaitSpinlock, &Tmp);
530 VBoxGuestWaitFreeLocked(pDevExt, pWait);
531 RTSpinlockReleaseNoInts(pDevExt->WaitSpinlock, &Tmp);
532}
533
534
535/**
536 * Modifies the guest capabilities.
537 *
538 * Should be called during driver init and termination.
539 *
540 * @returns VBox status code.
541 * @param fOr The Or mask (what to enable).
542 * @param fNot The Not mask (what to disable).
543 */
544int VBoxGuestSetGuestCapabilities(uint32_t fOr, uint32_t fNot)
545{
546 VMMDevReqGuestCapabilities2 *pReq;
547 int rc = VbglGRAlloc((VMMDevRequestHeader **)&pReq, sizeof(*pReq), VMMDevReq_SetGuestCapabilities);
548 if (RT_FAILURE(rc))
549 {
550 Log(("VBoxGuestSetGuestCapabilities: failed to allocate %u (%#x) bytes to cache the request. rc=%d!!\n",
551 sizeof(*pReq), sizeof(*pReq), rc));
552 return rc;
553 }
554
555 pReq->u32OrMask = fOr;
556 pReq->u32NotMask = fNot;
557
558 rc = VbglGRPerform(&pReq->header);
559 if (RT_FAILURE(rc))
560 Log(("VBoxGuestSetGuestCapabilities:VbglGRPerform failed, rc=%Rrc!\n", rc));
561 else if (RT_FAILURE(pReq->header.rc))
562 {
563 Log(("VBoxGuestSetGuestCapabilities: The request failed; VMMDev rc=%Rrc!\n", pReq->header.rc));
564 rc = pReq->header.rc;
565 }
566
567 VbglGRFree(&pReq->header);
568 return rc;
569}
570
571
572/**
573 * Implements the fast (no input or output) type of IOCtls.
574 *
575 * This is currently just a placeholder stub inherited from the support driver code.
576 *
577 * @returns VBox status code.
578 * @param iFunction The IOCtl function number.
579 * @param pDevExt The device extension.
580 * @param pSession The session.
581 */
582int VBoxGuestCommonIOCtlFast(unsigned iFunction, PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession)
583{
584 Log(("VBoxGuestCommonIOCtlFast: iFunction=%#x pDevExt=%p pSession=%p\n", iFunction, pDevExt, pSession));
585
586 return VERR_NOT_SUPPORTED;
587}
588
589
590
591static int VBoxGuestCommonIOCtl_GetVMMDevPort(PVBOXGUESTDEVEXT pDevExt, VBoxGuestPortInfo *pInfo, size_t *pcbDataReturned)
592{
593 Log(("VBoxGuestCommonIOCtl: GETVMMDEVPORT\n"));
594 pInfo->portAddress = pDevExt->IOPortBase;
595 pInfo->pVMMDevMemory = (VMMDevMemory *)pDevExt->pVMMDevMemory;
596 if (pcbDataReturned)
597 *pcbDataReturned = sizeof(*pInfo);
598 return VINF_SUCCESS;
599}
600
601
602/**
603 * Worker VBoxGuestCommonIOCtl_WaitEvent.
604 * The caller enters the spinlock, we may or may not leave it.
605 *
606 * @returns VINF_SUCCESS if we've left the spinlock and can return immediately.
607 */
608DECLINLINE(int) WaitEventCheckCondition(PVBOXGUESTDEVEXT pDevExt, VBoxGuestWaitEventInfo *pInfo,
609 int iEvent, const uint32_t fReqEvents, PRTSPINLOCKTMP pTmp)
610{
611 uint32_t fMatches = pDevExt->f32PendingEvents & fReqEvents;
612 if (fMatches)
613 {
614 ASMAtomicAndU32(&pDevExt->f32PendingEvents, ~fMatches);
615 RTSpinlockReleaseNoInts(pDevExt->WaitSpinlock, pTmp);
616
617 pInfo->u32EventFlagsOut = fMatches;
618 pInfo->u32Result = VBOXGUEST_WAITEVENT_OK;
619 if (fReqEvents & ~((uint32_t)1 << iEvent))
620 Log(("VBoxGuestCommonIOCtl: WAITEVENT: returns %#x\n", pInfo->u32EventFlagsOut));
621 else
622 Log(("VBoxGuestCommonIOCtl: WAITEVENT: returns %#x/%d\n", pInfo->u32EventFlagsOut, iEvent));
623 return VINF_SUCCESS;
624 }
625 return VERR_TIMEOUT;
626}
627
628
629static int VBoxGuestCommonIOCtl_WaitEvent(PVBOXGUESTDEVEXT pDevExt, VBoxGuestWaitEventInfo *pInfo, size_t *pcbDataReturned,
630 bool fInterruptible)
631{
632 pInfo->u32EventFlagsOut = 0;
633 pInfo->u32Result = VBOXGUEST_WAITEVENT_ERROR;
634 if (pcbDataReturned)
635 *pcbDataReturned = sizeof(*pInfo);
636
637 /*
638 * Copy and verify the input mask.
639 */
640 const uint32_t fReqEvents = pInfo->u32EventMaskIn;
641 int iEvent = ASMBitFirstSetU32(fReqEvents) - 1;
642 if (RT_UNLIKELY(iEvent < 0))
643 {
644 Log(("VBoxGuestCommonIOCtl: WAITEVENT: Invalid input mask %#x!!\n", fReqEvents));
645 return VERR_INVALID_PARAMETER;
646 }
647
648 /*
649 * Check the condition up front, before doing the wait-for-event allocations.
650 */
651 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
652 RTSpinlockAcquireNoInts(pDevExt->WaitSpinlock, &Tmp);
653 int rc = WaitEventCheckCondition(pDevExt, pInfo, iEvent, fReqEvents, &Tmp);
654 if (rc == VINF_SUCCESS)
655 return rc;
656 RTSpinlockReleaseNoInts(pDevExt->WaitSpinlock, &Tmp);
657
658 if (!pInfo->u32TimeoutIn)
659 {
660 pInfo->u32Result = VBOXGUEST_WAITEVENT_TIMEOUT;
661 Log(("VBoxGuestCommonIOCtl: WAITEVENT: returns VERR_TIMEOUT\n"));
662 return VERR_TIMEOUT;
663 }
664
665 PVBOXGUESTWAIT pWait = VBoxGuestWaitAlloc(pDevExt);
666 if (!pWait)
667 return VERR_NO_MEMORY;
668 pWait->fReqEvents = fReqEvents;
669
670 /*
671 * We've got the wait entry now, re-enter the spinlock and check for the condition.
672 * If the wait condition is met, return.
673 * Otherwise enter into the list and go to sleep waiting for the ISR to signal us.
674 */
675 RTSpinlockAcquireNoInts(pDevExt->WaitSpinlock, &Tmp);
676 rc = WaitEventCheckCondition(pDevExt, pInfo, iEvent, fReqEvents, &Tmp);
677 if (rc == VINF_SUCCESS)
678 {
679 VBoxGuestWaitFreeUnlocked(pDevExt, pWait);
680 return rc;
681 }
682 VBoxGuestWaitAppend(&pDevExt->WaitList, pWait);
683 RTSpinlockReleaseNoInts(pDevExt->WaitSpinlock, &Tmp);
684
685 if (fInterruptible)
686 rc = RTSemEventMultiWaitNoResume(pWait->Event,
687 pInfo->u32TimeoutIn == UINT32_MAX ? RT_INDEFINITE_WAIT : pInfo->u32TimeoutIn);
688 else
689 rc = RTSemEventMultiWait(pWait->Event,
690 pInfo->u32TimeoutIn == UINT32_MAX ? RT_INDEFINITE_WAIT : pInfo->u32TimeoutIn);
691
692 /*
693 * There is one special case here and that's when the semaphore is
694 * destroyed upon device driver unload. This shouldn't happen of course,
695 * but in case it does, just get out of here ASAP.
696 */
697 if (rc == VERR_SEM_DESTROYED)
698 return rc;
699
700 /*
701 * Unlink the wait item and dispose of it.
702 */
703 RTSpinlockAcquireNoInts(pDevExt->WaitSpinlock, &Tmp);
704 VBoxGuestWaitUnlink(&pDevExt->WaitList, pWait);
705 const uint32_t fResEvents = pWait->fResEvents;
706 VBoxGuestWaitFreeLocked(pDevExt, pWait);
707 RTSpinlockReleaseNoInts(pDevExt->WaitSpinlock, &Tmp);
708
709 /*
710 * Now deal with the return code.
711 */
712 if (fResEvents)
713 {
714 pInfo->u32EventFlagsOut = fResEvents;
715 pInfo->u32Result = VBOXGUEST_WAITEVENT_OK;
716 if (fReqEvents & ~((uint32_t)1 << iEvent))
717 Log(("VBoxGuestCommonIOCtl: WAITEVENT: returns %#x\n", pInfo->u32EventFlagsOut));
718 else
719 Log(("VBoxGuestCommonIOCtl: WAITEVENT: returns %#x/%d\n", pInfo->u32EventFlagsOut, iEvent));
720 rc = VINF_SUCCESS;
721 }
722 else if (rc == VERR_TIMEOUT)
723 {
724 pInfo->u32Result = VBOXGUEST_WAITEVENT_TIMEOUT;
725 Log(("VBoxGuestCommonIOCtl: WAITEVENT: returns VERR_TIMEOUT\n"));
726 }
727 else if (rc == VERR_INTERRUPTED)
728 {
729 pInfo->u32Result = VBOXGUEST_WAITEVENT_INTERRUPTED;
730 Log(("VBoxGuestCommonIOCtl: WAITEVENT: returns VERR_INTERRUPTED\n"));
731 }
732 else
733 {
734 if (RT_SUCCESS(rc))
735 {
736 static unsigned s_cErrors = 0;
737 if (s_cErrors++ < 32)
738 LogRel(("VBoxGuestCommonIOCtl: WAITEVENT: returns %Rrc but no events!\n", rc));
739 rc = VERR_INTERNAL_ERROR;
740 }
741 pInfo->u32Result = VBOXGUEST_WAITEVENT_ERROR;
742 Log(("VBoxGuestCommonIOCtl: WAITEVENT: returns %Rrc\n", rc));
743 }
744
745 return rc;
746}
747
748
749static int VBoxGuestCommonIOCtl_VMMRequest(PVBOXGUESTDEVEXT pDevExt, VMMDevRequestHeader *pReqHdr,
750 size_t cbData, size_t *pcbDataReturned)
751{
752 Log(("VBoxGuestCommonIOCtl: VMMREQUEST type %d\n", pReqHdr->requestType));
753
754 /*
755 * Validate the header and request size.
756 */
757 const uint32_t cbReq = pReqHdr->size;
758 const uint32_t cbMinSize = vmmdevGetRequestSize(pReqHdr->requestType);
759 if (cbReq < cbMinSize)
760 {
761 Log(("VBoxGuestCommonIOCtl: VMMREQUEST: invalid hdr size %#x, expected >= %#x; type=%#x!!\n",
762 cbReq, cbMinSize, pReqHdr->requestType));
763 return VERR_INVALID_PARAMETER;
764 }
765 if (cbReq > cbData)
766 {
767 Log(("VBoxGuestCommonIOCtl: VMMREQUEST: invalid size %#x, expected >= %#x (hdr); type=%#x!!\n",
768 cbData, cbReq, pReqHdr->requestType));
769 return VERR_INVALID_PARAMETER;
770 }
771
772 /*
773 * Make a copy of the request in the physical memory heap so
774 * the VBoxGuestLibrary can more easily deal with the request.
775 * (This is really a waste of time since the OS or the OS specific
776 * code has already buffered or locked the input/output buffer, but
777 * it does makes things a bit simpler wrt to phys address.)
778 */
779 VMMDevRequestHeader *pReqCopy;
780 int rc = VbglGRAlloc(&pReqCopy, cbReq, pReqHdr->requestType);
781 if (RT_FAILURE(rc))
782 {
783 Log(("VBoxGuestCommonIOCtl: VMMREQUEST: failed to allocate %u (%#x) bytes to cache the request. rc=%d!!\n",
784 cbReq, cbReq, rc));
785 return rc;
786 }
787
788 memcpy(pReqCopy, pReqHdr, cbReq);
789 rc = VbglGRPerform(pReqCopy);
790 if ( RT_SUCCESS(rc)
791 && RT_SUCCESS(pReqCopy->rc))
792 {
793 Assert(rc != VINF_HGCM_ASYNC_EXECUTE);
794 Assert(pReqCopy->rc != VINF_HGCM_ASYNC_EXECUTE);
795
796 memcpy(pReqHdr, pReqCopy, cbReq);
797 if (pcbDataReturned)
798 *pcbDataReturned = cbReq;
799 }
800 else if (RT_FAILURE(rc))
801 Log(("VBoxGuestCommonIOCtl: VMMREQUEST: VbglGRPerform - rc=%Rrc!\n", rc));
802 else
803 {
804 Log(("VBoxGuestCommonIOCtl: VMMREQUEST: request execution failed; VMMDev rc=%Rrc!\n", pReqCopy->rc));
805 rc = pReqCopy->rc;
806 }
807
808 VbglGRFree(pReqCopy);
809 return rc;
810}
811
812
813static int VBoxGuestCommonIOCtl_CtlFilterMask(PVBOXGUESTDEVEXT pDevExt, VBoxGuestFilterMaskInfo *pInfo)
814{
815 VMMDevCtlGuestFilterMask *pReq;
816 int rc = VbglGRAlloc((VMMDevRequestHeader **)&pReq, sizeof(*pReq), VMMDevReq_CtlGuestFilterMask);
817 if (RT_FAILURE(rc))
818 {
819 Log(("VBoxGuestCommonIOCtl: CTL_FILTER_MASK: failed to allocate %u (%#x) bytes to cache the request. rc=%d!!\n",
820 sizeof(*pReq), sizeof(*pReq), rc));
821 return rc;
822 }
823
824 pReq->u32OrMask = pInfo->u32OrMask;
825 pReq->u32NotMask = pInfo->u32NotMask;
826
827 rc = VbglGRPerform(&pReq->header);
828 if (RT_FAILURE(rc))
829 Log(("VBoxGuestCommonIOCtl: CTL_FILTER_MASK: VbglGRPerform failed, rc=%Rrc!\n", rc));
830 else if (RT_FAILURE(pReq->header.rc))
831 {
832 Log(("VBoxGuestCommonIOCtl: CTL_FILTER_MASK: The request failed; VMMDev rc=%Rrc!\n", pReq->header.rc));
833 rc = pReq->header.rc;
834 }
835
836 VbglGRFree(&pReq->header);
837 return rc;
838}
839
840#ifdef VBOX_WITH_HGCM
841
842/** Worker for VBoxGuestHGCMAsyncWaitCallback*. */
843static void VBoxGuestHGCMAsyncWaitCallbackWorker(VMMDevHGCMRequestHeader volatile *pHdr, PVBOXGUESTDEVEXT pDevExt,
844 bool fInterruptible, uint32_t u32Timeout)
845{
846
847 /*
848 * Check to see if the condition was met by the time we got here.
849 *
850 * We create a simple poll loop here for dealing with out-of-memory
851 * conditions since the caller isn't necessarily able to deal with
852 * us returning too early.
853 */
854 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
855 PVBOXGUESTWAIT pWait;
856 for (;;)
857 {
858 RTSpinlockAcquireNoInts(pDevExt->WaitSpinlock, &Tmp);
859 if ((pHdr->fu32Flags & VBOX_HGCM_REQ_DONE) != 0)
860 {
861 RTSpinlockReleaseNoInts(pDevExt->WaitSpinlock, &Tmp);
862 return;
863 }
864 RTSpinlockReleaseNoInts(pDevExt->WaitSpinlock, &Tmp);
865
866 pWait = VBoxGuestWaitAlloc(pDevExt);
867 if (pWait)
868 break;
869 if (fInterruptible)
870 return;
871 RTThreadSleep(1);
872 }
873 pWait->fReqEvents = VMMDEV_EVENT_HGCM;
874 pWait->pHGCMReq = pHdr;
875
876 /*
877 * Re-enter the spinlock and re-check for the condition.
878 * If the condition is met, return.
879 * Otherwise link us into the HGCM wait list and go to sleep.
880 */
881 RTSpinlockAcquireNoInts(pDevExt->WaitSpinlock, &Tmp);
882 if ((pHdr->fu32Flags & VBOX_HGCM_REQ_DONE) != 0)
883 {
884 VBoxGuestWaitFreeLocked(pDevExt, pWait);
885 RTSpinlockReleaseNoInts(pDevExt->WaitSpinlock, &Tmp);
886 return;
887 }
888 VBoxGuestWaitAppend(&pDevExt->HGCMWaitList, pWait);
889 RTSpinlockReleaseNoInts(pDevExt->WaitSpinlock, &Tmp);
890
891 int rc;
892 if (fInterruptible)
893 rc = RTSemEventMultiWaitNoResume(pWait->Event, u32Timeout);
894 else
895 rc = RTSemEventMultiWait(pWait->Event, u32Timeout);
896
897 /*
898 * This shouldn't ever return failure...
899 * Unlink, free and return.
900 */
901 if (rc == VERR_SEM_DESTROYED)
902 return;
903 if (RT_FAILURE(rc))
904 LogRel(("VBoxGuestHGCMAsyncWaitCallback: wait failed! %Rrc\n", rc));
905
906 RTSpinlockAcquireNoInts(pDevExt->WaitSpinlock, &Tmp);
907 VBoxGuestWaitUnlink(&pDevExt->HGCMWaitList, pWait);
908 VBoxGuestWaitFreeLocked(pDevExt, pWait);
909 RTSpinlockReleaseNoInts(pDevExt->WaitSpinlock, &Tmp);
910}
911
912/**
913 * This is a callback for dealing with async waits.
914 *
915 * It operates in a manner similar to VBoxGuestCommonIOCtl_WaitEvent.
916 */
917static DECLCALLBACK(void) VBoxGuestHGCMAsyncWaitCallback(VMMDevHGCMRequestHeader *pHdr, void *pvUser, uint32_t u32User)
918{
919 PVBOXGUESTDEVEXT pDevExt = (PVBOXGUESTDEVEXT)pvUser;
920 LogFunc(("requestType=%d\n", pHdr->header.requestType));
921 VBoxGuestHGCMAsyncWaitCallbackWorker((VMMDevHGCMRequestHeader volatile *)pHdr,
922 pDevExt, false /* fInterruptible */,
923 RT_INDEFINITE_WAIT);
924}
925
926/**
927 * This is a callback for dealing with async waits with a timeout.
928 *
929 * It operates in a manner similar to VBoxGuestCommonIOCtl_WaitEvent.
930 */
931static DECLCALLBACK(void) VBoxGuestHGCMAsyncWaitCallbackInterruptible(VMMDevHGCMRequestHeader *pHdr,
932 void *pvUser, uint32_t u32User)
933{
934 PVBOXGUESTDEVEXT pDevExt = (PVBOXGUESTDEVEXT)pvUser;
935 LogFunc(("requestType=%d\n", pHdr->header.requestType));
936 VBoxGuestHGCMAsyncWaitCallbackWorker((VMMDevHGCMRequestHeader volatile *)pHdr,
937 pDevExt, true /* fInterruptible */,
938 u32User);
939}
940
941static int VBoxGuestCommonIOCtl_HGCMConnect(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession, VBoxGuestHGCMConnectInfo *pInfo,
942 size_t *pcbDataReturned)
943{
944 /*
945 * The VbglHGCMConnect call will invoke the callback if the HGCM
946 * call is performed in an ASYNC fashion. The function is not able
947 * to deal with cancelled requests.
948 */
949 Log(("VBoxGuestCommonIOCtl: HGCM_CONNECT: %.128s\n",
950 pInfo->Loc.type == VMMDevHGCMLoc_LocalHost || pInfo->Loc.type == VMMDevHGCMLoc_LocalHost_Existing
951 ? pInfo->Loc.u.host.achName : "<not local host>"));
952
953 int rc = VbglHGCMConnect(pInfo, VBoxGuestHGCMAsyncWaitCallback, pDevExt, RT_INDEFINITE_WAIT);
954 if (RT_SUCCESS(rc))
955 {
956 Log(("VBoxGuestCommonIOCtl: HGCM_CONNECT: u32Client=%RX32 result=%Rrc (rc=%Rrc)\n",
957 pInfo->u32ClientID, pInfo->result, rc));
958 if (RT_SUCCESS(pInfo->result))
959 {
960 /*
961 * Append the client id to the client id table.
962 * If the table has somehow become filled up, we'll disconnect the session.
963 */
964 unsigned i;
965 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
966 RTSpinlockAcquireNoInts(pDevExt->SessionSpinlock, &Tmp);
967 for (i = 0; i < RT_ELEMENTS(pSession->aHGCMClientIds); i++)
968 if (!pSession->aHGCMClientIds[i])
969 {
970 pSession->aHGCMClientIds[i] = pInfo->u32ClientID;
971 break;
972 }
973 RTSpinlockReleaseNoInts(pDevExt->SessionSpinlock, &Tmp);
974 if (i >= RT_ELEMENTS(pSession->aHGCMClientIds))
975 {
976 static unsigned s_cErrors = 0;
977 if (s_cErrors++ < 32)
978 LogRel(("VBoxGuestCommonIOCtl: HGCM_CONNECT: too many HGCMConnect calls for one session!\n"));
979
980 VBoxGuestHGCMDisconnectInfo Info;
981 Info.result = 0;
982 Info.u32ClientID = pInfo->u32ClientID;
983 VbglHGCMDisconnect(&Info, VBoxGuestHGCMAsyncWaitCallback, pDevExt, RT_INDEFINITE_WAIT);
984 return VERR_TOO_MANY_OPEN_FILES;
985 }
986 }
987 if (pcbDataReturned)
988 *pcbDataReturned = sizeof(*pInfo);
989 }
990 return rc;
991}
992
993
994static int VBoxGuestCommonIOCtl_HGCMDisconnect(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession, VBoxGuestHGCMDisconnectInfo *pInfo,
995 size_t *pcbDataReturned)
996{
997 /*
998 * Validate the client id and invalidate its entry while we're in the call.
999 */
1000 const uint32_t u32ClientId = pInfo->u32ClientID;
1001 unsigned i;
1002 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
1003 RTSpinlockAcquireNoInts(pDevExt->SessionSpinlock, &Tmp);
1004 for (i = 0; i < RT_ELEMENTS(pSession->aHGCMClientIds); i++)
1005 if (pSession->aHGCMClientIds[i] == u32ClientId)
1006 {
1007 pSession->aHGCMClientIds[i] = UINT32_MAX;
1008 break;
1009 }
1010 RTSpinlockReleaseNoInts(pDevExt->SessionSpinlock, &Tmp);
1011 if (i >= RT_ELEMENTS(pSession->aHGCMClientIds))
1012 {
1013 static unsigned s_cErrors = 0;
1014 if (s_cErrors++ > 32)
1015 LogRel(("VBoxGuestCommonIOCtl: HGCM_DISCONNECT: u32Client=%RX32\n", u32ClientId));
1016 return VERR_INVALID_HANDLE;
1017 }
1018
1019 /*
1020 * The VbglHGCMConnect call will invoke the callback if the HGCM
1021 * call is performed in an ASYNC fashion. The function is not able
1022 * to deal with cancelled requests.
1023 */
1024 Log(("VBoxGuestCommonIOCtl: HGCM_DISCONNECT: u32Client=%RX32\n", pInfo->u32ClientID));
1025 int rc = VbglHGCMDisconnect(pInfo, VBoxGuestHGCMAsyncWaitCallback, pDevExt, RT_INDEFINITE_WAIT);
1026 if (RT_SUCCESS(rc))
1027 {
1028 Log(("VBoxGuestCommonIOCtl: HGCM_DISCONNECT: result=%Rrc\n", pInfo->result));
1029 if (pcbDataReturned)
1030 *pcbDataReturned = sizeof(*pInfo);
1031 }
1032
1033 /* Update the client id array according to the result. */
1034 RTSpinlockAcquireNoInts(pDevExt->SessionSpinlock, &Tmp);
1035 if (pSession->aHGCMClientIds[i] == UINT32_MAX)
1036 pSession->aHGCMClientIds[i] = RT_SUCCESS(rc) && RT_SUCCESS(pInfo->result) ? 0 : u32ClientId;
1037 RTSpinlockReleaseNoInts(pDevExt->SessionSpinlock, &Tmp);
1038
1039 return rc;
1040}
1041
1042
1043static int VBoxGuestCommonIOCtl_HGCMCall(PVBOXGUESTDEVEXT pDevExt,
1044 PVBOXGUESTSESSION pSession,
1045 VBoxGuestHGCMCallInfo *pInfo,
1046 uint32_t u32Timeout, bool fInterruptible,
1047 size_t cbData, size_t *pcbDataReturned)
1048{
1049 /*
1050 * Some more validations.
1051 */
1052 if (pInfo->cParms > 4096) /* (Just make sure it doesn't overflow the next check.) */
1053 {
1054 Log(("VBoxGuestCommonIOCtl: HGCM_CALL: cParm=%RX32 is not sane\n", pInfo->cParms));
1055 return VERR_INVALID_PARAMETER;
1056 }
1057 const size_t cbActual = sizeof(*pInfo) + pInfo->cParms * sizeof(HGCMFunctionParameter);
1058 if (cbData < cbActual)
1059 {
1060 Log(("VBoxGuestCommonIOCtl: HGCM_CALL: cbData=%#zx (%zu) required size is %#zx (%zu)\n",
1061 cbData, cbActual));
1062 return VERR_INVALID_PARAMETER;
1063 }
1064
1065 /*
1066 * Validate the client id.
1067 */
1068 const uint32_t u32ClientId = pInfo->u32ClientID;
1069 unsigned i;
1070 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
1071 RTSpinlockAcquireNoInts(pDevExt->SessionSpinlock, &Tmp);
1072 for (i = 0; i < RT_ELEMENTS(pSession->aHGCMClientIds); i++)
1073 if (pSession->aHGCMClientIds[i] == u32ClientId)
1074 break;
1075 RTSpinlockReleaseNoInts(pDevExt->SessionSpinlock, &Tmp);
1076 if (RT_UNLIKELY(i >= RT_ELEMENTS(pSession->aHGCMClientIds)))
1077 {
1078 static unsigned s_cErrors = 0;
1079 if (s_cErrors++ > 32)
1080 LogRel(("VBoxGuestCommonIOCtl: HGCM_CALL: Invalid handle. u32Client=%RX32\n", u32ClientId));
1081 return VERR_INVALID_HANDLE;
1082 }
1083
1084 /*
1085 * The VbglHGCMCall call will invoke the callback if the HGCM
1086 * call is performed in an ASYNC fashion. This function can
1087 * deal with cancelled requests, so we let user more requests
1088 * be interruptible (should add a flag for this later I guess).
1089 */
1090 Log(("VBoxGuestCommonIOCtl: HGCM_CALL: u32Client=%RX32\n", pInfo->u32ClientID));
1091 int rc;
1092 if (fInterruptible)
1093 rc = VbglHGCMCall(pInfo, VBoxGuestHGCMAsyncWaitCallbackInterruptible, pDevExt,
1094 u32Timeout);
1095 else
1096 rc = VbglHGCMCall(pInfo, VBoxGuestHGCMAsyncWaitCallback, pDevExt, u32Timeout);
1097 if (RT_SUCCESS(rc))
1098 {
1099 Log(("VBoxGuestCommonIOCtl: HGCM_CALL: result=%Rrc\n", pInfo->result));
1100 if (pcbDataReturned)
1101 *pcbDataReturned = cbActual;
1102 }
1103 Log(("VBoxGuestCommonIOCtl: HGCM_CALL: Failed. rc=%Rrc.\n", rc));
1104 return rc;
1105}
1106
1107/**
1108 * @returns VBox status code. Unlike the other HGCM IOCtls this will combine
1109 * the VbglHGCMConnect/Disconnect return code with the Info.result.
1110 */
1111static int VBoxGuestCommonIOCtl_HGCMClipboardReConnect(PVBOXGUESTDEVEXT pDevExt, uint32_t *pu32ClientId, size_t *pcbDataReturned)
1112{
1113 int rc;
1114 Log(("VBoxGuestCommonIOCtl: CLIPBOARD_CONNECT: Current u32ClientId=%RX32\n", pDevExt->u32ClipboardClientId));
1115
1116
1117 /*
1118 * If there is an old client, try disconnect it first.
1119 */
1120 if (pDevExt->u32ClipboardClientId != 0)
1121 {
1122 VBoxGuestHGCMDisconnectInfo Info;
1123 Info.result = (uint32_t)VERR_WRONG_ORDER; /** @todo Vitali, why is this member unsigned? */
1124 Info.u32ClientID = pDevExt->u32ClipboardClientId;
1125 rc = VbglHGCMDisconnect(&Info, VBoxGuestHGCMAsyncWaitCallback, pDevExt, RT_INDEFINITE_WAIT);
1126 if (RT_SUCCESS(rc))
1127 {
1128 LogRel(("VBoxGuestCommonIOCtl: CLIPBOARD_CONNECT: failed to disconnect old client. VbglHGCMDisconnect -> rc=%Rrc\n", rc));
1129 return rc;
1130 }
1131 if (RT_FAILURE((int32_t)Info.result))
1132 {
1133 Log(("VBoxGuestCommonIOCtl: CLIPBOARD_CONNECT: failed to disconnect old client. Info.result=%Rrc\n", rc));
1134 return Info.result;
1135 }
1136 pDevExt->u32ClipboardClientId = 0;
1137 }
1138
1139 /*
1140 * Try connect.
1141 */
1142 VBoxGuestHGCMConnectInfo Info;
1143 Info.Loc.type = VMMDevHGCMLoc_LocalHost_Existing;
1144 strcpy(Info.Loc.u.host.achName, "VBoxSharedClipboard");
1145 Info.u32ClientID = 0;
1146 Info.result = (uint32_t)VERR_WRONG_ORDER;
1147
1148 rc = VbglHGCMConnect(&Info, VBoxGuestHGCMAsyncWaitCallback, pDevExt, RT_INDEFINITE_WAIT);
1149 if (RT_FAILURE(rc))
1150 {
1151 LogRel(("VBoxGuestCommonIOCtl: CLIPBOARD_CONNECT: VbglHGCMConnected -> rc=%Rrc\n", rc));
1152 return rc;
1153 }
1154 if (RT_FAILURE((int32_t)Info.result))
1155 {
1156 LogRel(("VBoxGuestCommonIOCtl: CLIPBOARD_CONNECT: VbglHGCMConnected -> rc=%Rrc\n", rc));
1157 return rc;
1158 }
1159
1160 Log(("VBoxGuestCommonIOCtl: CLIPBOARD_CONNECT: connected successfully u32ClientId=%RX32\n", Info.u32ClientID));
1161
1162 pDevExt->u32ClipboardClientId = Info.u32ClientID;
1163 *pu32ClientId = Info.u32ClientID;
1164 if (pcbDataReturned)
1165 *pcbDataReturned = sizeof(uint32_t);
1166
1167 return VINF_SUCCESS;
1168}
1169
1170#endif /* VBOX_WITH_HGCM */
1171
1172/**
1173 * Guest backdoor logging.
1174 *
1175 * @returns VBox status code.
1176 *
1177 * @param pch The log message (need not be NULL terminated).
1178 * @param cbData Size of the buffer.
1179 * @param pcbDataReturned Where to store the amount of returned data. Can be NULL.
1180 */
1181static int VBoxGuestCommonIOCtl_Log(const char *pch, size_t cbData, size_t *pcbDataReturned)
1182{
1183 Log(("%.*s", cbData, pch));
1184 if (pcbDataReturned)
1185 *pcbDataReturned = 0;
1186 return VINF_SUCCESS;
1187}
1188
1189
1190/**
1191 * Common IOCtl for user to kernel and kernel to kernel communcation.
1192 *
1193 * This function only does the basic validation and then invokes
1194 * worker functions that takes care of each specific function.
1195 *
1196 * @returns VBox status code.
1197 *
1198 * @param iFunction The requested function.
1199 * @param pDevExt The device extension.
1200 * @param pSession The client session.
1201 * @param pvData The input/output data buffer. Can be NULL depending on the function.
1202 * @param cbData The max size of the data buffer.
1203 * @param pcbDataReturned Where to store the amount of returned data. Can be NULL.
1204 */
1205int VBoxGuestCommonIOCtl(unsigned iFunction, PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession,
1206 void *pvData, size_t cbData, size_t *pcbDataReturned)
1207{
1208 Log(("VBoxGuestCommonIOCtl: iFunction=%#x pDevExt=%p pSession=%p pvData=%p cbData=%zu\n",
1209 iFunction, pDevExt, pSession, pvData, cbData));
1210
1211 /*
1212 * Define some helper macros to simplify validation.
1213 */
1214#define CHECKRET_RING0(mnemonic) \
1215 do { \
1216 if (pSession->R0Process != NIL_RTR0PROCESS) \
1217 { \
1218 Log(("VBoxGuestCommonIOCtl: " mnemonic ": Ring-0 only, caller is %RTproc/%p\n", \
1219 pSession->Process, (uintptr_t)pSession->R0Process)); \
1220 return VERR_PERMISSION_DENIED; \
1221 } \
1222 } while (0)
1223#define CHECKRET_MIN_SIZE(mnemonic, cbMin) \
1224 do { \
1225 if (cbData < (cbMin)) \
1226 { \
1227 Log(("VBoxGuestCommonIOCtl: " mnemonic ": cbData=%#zx (%zu) min is %#zx (%zu)\n", \
1228 cbData, cbData, (size_t)(cbMin), (size_t)(cbMin))); \
1229 return VERR_BUFFER_OVERFLOW; \
1230 } \
1231 if ((cbMin) != 0 && !VALID_PTR(pvData)) \
1232 { \
1233 Log(("VBoxGuestCommonIOCtl: " mnemonic ": Invalid pointer %p\n", pvData)); \
1234 return VERR_INVALID_POINTER; \
1235 } \
1236 } while (0)
1237
1238
1239 /*
1240 * Deal with variably sized requests first.
1241 */
1242 int rc = VINF_SUCCESS;
1243 if (VBOXGUEST_IOCTL_STRIP_SIZE(iFunction) == VBOXGUEST_IOCTL_STRIP_SIZE(VBOXGUEST_IOCTL_VMMREQUEST(0)))
1244 {
1245 CHECKRET_MIN_SIZE("VMMREQUEST", sizeof(VMMDevRequestHeader));
1246 rc = VBoxGuestCommonIOCtl_VMMRequest(pDevExt, (VMMDevRequestHeader *)pvData, cbData, pcbDataReturned);
1247 }
1248#ifdef VBOX_WITH_HGCM
1249 /*
1250 * These ones are tricky and can be done later.
1251 */
1252 else if (VBOXGUEST_IOCTL_STRIP_SIZE(iFunction) == VBOXGUEST_IOCTL_STRIP_SIZE(VBOXGUEST_IOCTL_HGCM_CALL(0)))
1253 {
1254 CHECKRET_MIN_SIZE("HGCM_CALL", sizeof(VBoxGuestHGCMCallInfo));
1255 bool fInterruptible = pSession->R0Process != NIL_RTR0PROCESS;
1256 rc = VBoxGuestCommonIOCtl_HGCMCall(pDevExt, pSession, (VBoxGuestHGCMCallInfo *)pvData,
1257 RT_INDEFINITE_WAIT, fInterruptible,
1258 cbData, pcbDataReturned);
1259 }
1260 else if (VBOXGUEST_IOCTL_STRIP_SIZE(iFunction) == VBOXGUEST_IOCTL_STRIP_SIZE(VBOXGUEST_IOCTL_HGCM_CALL_TIMED(0)))
1261 {
1262 CHECKRET_MIN_SIZE("HGCM_CALL_TIMED", sizeof(VBoxGuestHGCMCallInfoTimed));
1263 VBoxGuestHGCMCallInfoTimed *pInfo = (VBoxGuestHGCMCallInfoTimed *)pvData;
1264 rc = VBoxGuestCommonIOCtl_HGCMCall(pDevExt, pSession, &pInfo->info,
1265 pInfo->u32Timeout, !!pInfo->fInterruptible,
1266 cbData, pcbDataReturned);
1267 }
1268#endif /* VBOX_WITH_HGCM */
1269 else if (VBOXGUEST_IOCTL_STRIP_SIZE(iFunction) == VBOXGUEST_IOCTL_STRIP_SIZE(VBOXGUEST_IOCTL_LOG(0)))
1270 {
1271 CHECKRET_MIN_SIZE("LOG", 1);
1272 rc = VBoxGuestCommonIOCtl_Log((char *)pvData, cbData, pcbDataReturned);
1273 }
1274 else
1275 {
1276 switch (iFunction)
1277 {
1278 case VBOXGUEST_IOCTL_GETVMMDEVPORT:
1279 CHECKRET_RING0("GETVMMDEVPORT");
1280 CHECKRET_MIN_SIZE("GETVMMDEVPORT", sizeof(VBoxGuestPortInfo));
1281 rc = VBoxGuestCommonIOCtl_GetVMMDevPort(pDevExt, (VBoxGuestPortInfo *)pvData, pcbDataReturned);
1282 break;
1283
1284 case VBOXGUEST_IOCTL_WAITEVENT:
1285 CHECKRET_MIN_SIZE("WAITEVENT", sizeof(VBoxGuestWaitEventInfo));
1286 rc = VBoxGuestCommonIOCtl_WaitEvent(pDevExt, (VBoxGuestWaitEventInfo *)pvData, pcbDataReturned,
1287 pSession->R0Process != NIL_RTR0PROCESS);
1288 break;
1289
1290 case VBOXGUEST_IOCTL_CTL_FILTER_MASK:
1291 CHECKRET_MIN_SIZE("CTL_FILTER_MASK", sizeof(VBoxGuestFilterMaskInfo));
1292 rc = VBoxGuestCommonIOCtl_CtlFilterMask(pDevExt, (VBoxGuestFilterMaskInfo *)pvData);
1293 break;
1294
1295#ifdef VBOX_WITH_HGCM
1296 case VBOXGUEST_IOCTL_HGCM_CONNECT:
1297 CHECKRET_MIN_SIZE("HGCM_CONNECT", sizeof(VBoxGuestHGCMConnectInfo));
1298 rc = VBoxGuestCommonIOCtl_HGCMConnect(pDevExt, pSession, (VBoxGuestHGCMConnectInfo *)pvData, pcbDataReturned);
1299 break;
1300
1301 case VBOXGUEST_IOCTL_HGCM_DISCONNECT:
1302 CHECKRET_MIN_SIZE("HGCM_DISCONNECT", sizeof(VBoxGuestHGCMDisconnectInfo));
1303 rc = VBoxGuestCommonIOCtl_HGCMDisconnect(pDevExt, pSession, (VBoxGuestHGCMDisconnectInfo *)pvData, pcbDataReturned);
1304 break;
1305
1306 case VBOXGUEST_IOCTL_CLIPBOARD_CONNECT:
1307 CHECKRET_MIN_SIZE("CLIPBOARD_CONNECT", sizeof(uint32_t));
1308 rc = VBoxGuestCommonIOCtl_HGCMClipboardReConnect(pDevExt, (uint32_t *)pvData, pcbDataReturned);
1309 break;
1310#endif /* VBOX_WITH_HGCM */
1311
1312 default:
1313 {
1314 Log(("VBoxGuestCommonIOCtl: Unknown request iFunction=%#x Stripped size=%#x\n", iFunction,
1315 VBOXGUEST_IOCTL_STRIP_SIZE(iFunction)));
1316 rc = VERR_NOT_SUPPORTED;
1317 break;
1318 }
1319 }
1320 }
1321
1322 Log(("VBoxGuestCommonIOCtl: returns %Rrc *pcbDataReturned=%zu\n", rc, pcbDataReturned ? *pcbDataReturned : 0));
1323 return rc;
1324}
1325
1326
1327
1328/**
1329 * Common interrupt service routine.
1330 *
1331 * This deals with events and with waking up thread waiting for those events.
1332 *
1333 * @returns true if it was our interrupt, false if it wasn't.
1334 * @param pDevExt The VBoxGuest device extension.
1335 */
1336bool VBoxGuestCommonISR(PVBOXGUESTDEVEXT pDevExt)
1337{
1338 /*
1339 * Now we have to find out whether it was our IRQ. Read the event mask
1340 * from our device to see if there are any pending events.
1341 */
1342 bool fOurIrq = pDevExt->pVMMDevMemory->V.V1_04.fHaveEvents;
1343 if (fOurIrq)
1344 {
1345 /* Acknowlegde events. */
1346 VMMDevEvents *pReq = pDevExt->pIrqAckEvents;
1347 int rc = VbglGRPerform(&pReq->header);
1348 if ( RT_SUCCESS(rc)
1349 && RT_SUCCESS(pReq->header.rc))
1350 {
1351 uint32_t fEvents = pReq->events;
1352 Log(("VBoxGuestCommonISR: acknowledge events succeeded %#RX32\n", fEvents));
1353
1354 /*
1355 * Enter the spinlock and examin the waiting threads.
1356 */
1357 int rc2 = 0;
1358 PVBOXGUESTWAIT pWait;
1359 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
1360 RTSpinlockAcquireNoInts(pDevExt->WaitSpinlock, &Tmp);
1361
1362#ifdef VBOX_WITH_HGCM
1363 /* The HGCM event/list is kind of different in that we evaluate all entries. */
1364 if (fEvents & VMMDEV_EVENT_HGCM)
1365 for (pWait = pDevExt->HGCMWaitList.pHead; pWait; pWait = pWait->pNext)
1366 if ( !pWait->fResEvents
1367 && (pWait->pHGCMReq->fu32Flags & VBOX_HGCM_REQ_DONE))
1368 {
1369 pWait->fResEvents = VMMDEV_EVENT_HGCM;
1370 rc2 |= RTSemEventMultiSignal(pWait->Event);
1371 }
1372#endif
1373
1374 /* Normal FIFO evaluation. */
1375 fEvents |= pDevExt->f32PendingEvents;
1376 for (pWait = pDevExt->WaitList.pHead; pWait; pWait = pWait->pNext)
1377 if (!pWait->fResEvents)
1378 {
1379 pWait->fResEvents = pWait->fReqEvents & fEvents;
1380 fEvents &= ~pWait->fResEvents;
1381 rc2 |= RTSemEventMultiSignal(pWait->Event);
1382 if (!fEvents)
1383 break;
1384 }
1385
1386 ASMAtomicXchgU32(&pDevExt->f32PendingEvents, fEvents);
1387 RTSpinlockReleaseNoInts(pDevExt->WaitSpinlock, &Tmp);
1388 Assert(rc2 == 0);
1389 }
1390 else /* something is serious wrong... */
1391 Log(("VBoxGuestCommonISR: acknowledge events failed rc=%d, header rc=%d (events=%#x)!!\n",
1392 rc, pReq->header.rc, pReq->events));
1393 }
1394 else
1395 LogFlow(("VBoxGuestCommonISR: not ours\n"));
1396
1397 return fOurIrq;
1398}
1399
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