VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/HGCM.cpp@ 75769

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

VMMDev,HGCM: Give access to VMMDevRequestHeader::fRequestor. bugref:9105

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 78.6 KB
Line 
1/* $Id: HGCM.cpp 75769 2018-11-27 12:30:00Z vboxsync $ */
2/** @file
3 * HGCM (Host-Guest Communication Manager)
4 */
5
6/*
7 * Copyright (C) 2006-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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
18#define LOG_GROUP LOG_GROUP_HGCM
19#include "LoggingNew.h"
20
21#include "HGCM.h"
22#include "HGCMThread.h"
23
24#include <VBox/err.h>
25#include <VBox/hgcmsvc.h>
26#include <VBox/vmm/ssm.h>
27#include <VBox/vmm/stam.h>
28#include <VBox/sup.h>
29
30#include <iprt/alloc.h>
31#include <iprt/avl.h>
32#include <iprt/critsect.h>
33#include <iprt/asm.h>
34#include <iprt/ldr.h>
35#include <iprt/param.h>
36#include <iprt/path.h>
37#include <iprt/string.h>
38#include <iprt/semaphore.h>
39#include <iprt/thread.h>
40
41#include <VBox/VMMDev.h>
42#include <new>
43
44/**
45 * A service gets one thread, which synchronously delivers messages to
46 * the service. This is good for serialization.
47 *
48 * Some services may want to process messages asynchronously, and will want
49 * a next message to be delivered, while a previous message is still being
50 * processed.
51 *
52 * The dedicated service thread delivers a next message when service
53 * returns after fetching a previous one. The service will call a message
54 * completion callback when message is actually processed. So returning
55 * from the service call means only that the service is processing message.
56 *
57 * 'Message processed' condition is indicated by service, which call the
58 * callback, even if the callback is called synchronously in the dedicated
59 * thread.
60 *
61 * This message completion callback is only valid for Call requests.
62 * Connect and Disconnect are processed synchronously by the service.
63 */
64
65
66/* The maximum allowed size of a service name in bytes. */
67#define VBOX_HGCM_SVC_NAME_MAX_BYTES 1024
68
69struct _HGCMSVCEXTHANDLEDATA
70{
71 char *pszServiceName;
72 /* The service name follows. */
73};
74
75/** Internal helper service object. HGCM code would use it to
76 * hold information about services and communicate with services.
77 * The HGCMService is an (in future) abstract class that implements
78 * common functionality. There will be derived classes for specific
79 * service types.
80 */
81
82class HGCMService
83{
84 private:
85 VBOXHGCMSVCHELPERS m_svcHelpers;
86
87 static HGCMService *sm_pSvcListHead;
88 static HGCMService *sm_pSvcListTail;
89
90 static int sm_cServices;
91
92 HGCMThread *m_pThread;
93 friend DECLCALLBACK(void) hgcmServiceThread(HGCMThread *pThread, void *pvUser);
94
95 uint32_t volatile m_u32RefCnt;
96
97 HGCMService *m_pSvcNext;
98 HGCMService *m_pSvcPrev;
99
100 char *m_pszSvcName;
101 char *m_pszSvcLibrary;
102
103 RTLDRMOD m_hLdrMod;
104 PFNVBOXHGCMSVCLOAD m_pfnLoad;
105
106 VBOXHGCMSVCFNTABLE m_fntable;
107
108 uint32_t m_cClients;
109 uint32_t m_cClientsAllocated;
110
111 uint32_t *m_paClientIds;
112
113#ifdef VBOX_WITH_CRHGSMI
114 uint32_t m_cHandleAcquires;
115#endif
116
117 HGCMSVCEXTHANDLE m_hExtension;
118
119 PUVM m_pUVM;
120
121 /** @name Statistics
122 * @{ */
123 STAMPROFILE m_StatHandleMsg;
124 /** @} */
125
126 int loadServiceDLL(void);
127 void unloadServiceDLL(void);
128
129 /*
130 * Main HGCM thread methods.
131 */
132 int instanceCreate(const char *pszServiceLibrary, const char *pszServiceName, PUVM pUVM);
133 void instanceDestroy(void);
134
135 int saveClientState(uint32_t u32ClientId, PSSMHANDLE pSSM);
136 int loadClientState(uint32_t u32ClientId, PSSMHANDLE pSSM);
137
138 HGCMService();
139 ~HGCMService() {};
140
141 static DECLCALLBACK(int) svcHlpCallComplete(VBOXHGCMCALLHANDLE callHandle, int32_t rc);
142 static DECLCALLBACK(void) svcHlpDisconnectClient(void *pvInstance, uint32_t u32ClientId);
143 static DECLCALLBACK(bool) svcHlpIsCallRestored(VBOXHGCMCALLHANDLE callHandle);
144 static DECLCALLBACK(int) svcHlpStamRegisterV(void *pvInstance, void *pvSample, STAMTYPE enmType,
145 STAMVISIBILITY enmVisibility, STAMUNIT enmUnit, const char *pszDesc,
146 const char *pszName, va_list va);
147 static DECLCALLBACK(int) svcHlpStamDeregisterV(void *pvInstance, const char *pszPatFmt, va_list va);
148 static DECLCALLBACK(int) svcHlpInfoRegister(void *pvInstance, const char *pszName, const char *pszDesc,
149 PFNDBGFHANDLEREXT pfnHandler, void *pvUser);
150 static DECLCALLBACK(int) svcHlpInfoDeregister(void *pvInstance, const char *pszName);
151 static DECLCALLBACK(uint32_t) svcHlpGetRequestor(VBOXHGCMCALLHANDLE hCall);
152
153 public:
154
155 /*
156 * Main HGCM thread methods.
157 */
158 static int LoadService(const char *pszServiceLibrary, const char *pszServiceName, PUVM pUVM);
159 void UnloadService(bool fUvmIsInvalid);
160
161 static void UnloadAll(bool fUvmIsInvalid);
162
163 static int ResolveService(HGCMService **ppsvc, const char *pszServiceName);
164 void ReferenceService(void);
165 void ReleaseService(void);
166
167 static void Reset(void);
168
169 static int SaveState(PSSMHANDLE pSSM);
170 static int LoadState(PSSMHANDLE pSSM);
171
172 int CreateAndConnectClient(uint32_t *pu32ClientIdOut, uint32_t u32ClientIdIn);
173 int DisconnectClient(uint32_t u32ClientId, bool fFromService);
174
175 int HostCall(uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM *paParms);
176
177#ifdef VBOX_WITH_CRHGSMI
178 int HandleAcquired();
179 int HandleReleased();
180 int HostFastCallAsync(uint32_t u32Function, VBOXHGCMSVCPARM *pParm, PHGCMHOSTFASTCALLCB pfnCompletion,
181 void *pvCompletion);
182#endif
183
184 uint32_t SizeOfClient(void) { return m_fntable.cbClient; };
185
186 int RegisterExtension(HGCMSVCEXTHANDLE handle, PFNHGCMSVCEXT pfnExtension, void *pvExtension);
187 void UnregisterExtension(HGCMSVCEXTHANDLE handle);
188
189 /*
190 * The service thread methods.
191 */
192
193 int GuestCall(PPDMIHGCMPORT pHGCMPort, PVBOXHGCMCMD pCmd, uint32_t u32ClientId,
194 uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM aParms[], uint64_t tsArrival);
195};
196
197
198class HGCMClient: public HGCMObject
199{
200 public:
201 HGCMClient() : HGCMObject(HGCMOBJ_CLIENT), pService(NULL),
202 pvData(NULL) {};
203 ~HGCMClient();
204
205 int Init(HGCMService *pSvc);
206
207 /** Service that the client is connected to. */
208 HGCMService *pService;
209
210 /** Client specific data. */
211 void *pvData;
212};
213
214HGCMClient::~HGCMClient()
215{
216 if (pService->SizeOfClient() > 0)
217 RTMemFree(pvData);
218}
219
220int HGCMClient::Init(HGCMService *pSvc)
221{
222 pService = pSvc;
223
224 if (pService->SizeOfClient() > 0)
225 {
226 pvData = RTMemAllocZ(pService->SizeOfClient());
227
228 if (!pvData)
229 {
230 return VERR_NO_MEMORY;
231 }
232 }
233
234 return VINF_SUCCESS;
235}
236
237
238#define HGCM_CLIENT_DATA(pService, pClient)(pClient->pvData)
239
240
241
242HGCMService *HGCMService::sm_pSvcListHead = NULL;
243HGCMService *HGCMService::sm_pSvcListTail = NULL;
244int HGCMService::sm_cServices = 0;
245
246HGCMService::HGCMService()
247 :
248 m_pThread (NULL),
249 m_u32RefCnt (0),
250 m_pSvcNext (NULL),
251 m_pSvcPrev (NULL),
252 m_pszSvcName (NULL),
253 m_pszSvcLibrary (NULL),
254 m_hLdrMod (NIL_RTLDRMOD),
255 m_pfnLoad (NULL),
256 m_cClients (0),
257 m_cClientsAllocated (0),
258 m_paClientIds (NULL),
259#ifdef VBOX_WITH_CRHGSMI
260 m_cHandleAcquires (0),
261#endif
262 m_hExtension (NULL),
263 m_pUVM (NULL)
264{
265 RT_ZERO(m_fntable);
266}
267
268
269static bool g_fResetting = false;
270static bool g_fSaveState = false;
271
272
273/** Helper function to load a local service DLL.
274 *
275 * @return VBox code
276 */
277int HGCMService::loadServiceDLL(void)
278{
279 LogFlowFunc(("m_pszSvcLibrary = %s\n", m_pszSvcLibrary));
280
281 if (m_pszSvcLibrary == NULL)
282 {
283 return VERR_INVALID_PARAMETER;
284 }
285
286 RTERRINFOSTATIC ErrInfo;
287 RTErrInfoInitStatic(&ErrInfo);
288
289 int rc;
290
291 if (RTPathHasPath(m_pszSvcLibrary))
292 rc = SUPR3HardenedLdrLoadPlugIn(m_pszSvcLibrary, &m_hLdrMod, &ErrInfo.Core);
293 else
294 rc = SUPR3HardenedLdrLoadAppPriv(m_pszSvcLibrary, &m_hLdrMod, RTLDRLOAD_FLAGS_LOCAL, &ErrInfo.Core);
295
296 if (RT_SUCCESS(rc))
297 {
298 LogFlowFunc(("successfully loaded the library.\n"));
299
300 m_pfnLoad = NULL;
301
302 rc = RTLdrGetSymbol(m_hLdrMod, VBOX_HGCM_SVCLOAD_NAME, (void**)&m_pfnLoad);
303
304 if (RT_FAILURE(rc) || !m_pfnLoad)
305 {
306 Log(("HGCMService::loadServiceDLL: Error resolving the service entry point %s, rc = %d, m_pfnLoad = %p\n",
307 VBOX_HGCM_SVCLOAD_NAME, rc, m_pfnLoad));
308
309 if (RT_SUCCESS(rc))
310 {
311 /* m_pfnLoad was NULL */
312 rc = VERR_SYMBOL_NOT_FOUND;
313 }
314 }
315
316 if (RT_SUCCESS(rc))
317 {
318 RT_ZERO(m_fntable);
319
320 m_fntable.cbSize = sizeof(m_fntable);
321 m_fntable.u32Version = VBOX_HGCM_SVC_VERSION;
322 m_fntable.pHelpers = &m_svcHelpers;
323
324 rc = m_pfnLoad(&m_fntable);
325
326 LogFlowFunc(("m_pfnLoad rc = %Rrc\n", rc));
327
328 if (RT_SUCCESS(rc))
329 {
330 if ( m_fntable.pfnUnload == NULL
331 || m_fntable.pfnConnect == NULL
332 || m_fntable.pfnDisconnect == NULL
333 || m_fntable.pfnCall == NULL
334 )
335 {
336 Log(("HGCMService::loadServiceDLL: at least one of function pointers is NULL\n"));
337
338 rc = VERR_INVALID_PARAMETER;
339
340 if (m_fntable.pfnUnload)
341 {
342 m_fntable.pfnUnload(m_fntable.pvService);
343 }
344 }
345 }
346 }
347 }
348 else
349 {
350 LogRel(("HGCM: Failed to load the service library: [%s], rc = %Rrc - %s. The service will be not available.\n",
351 m_pszSvcLibrary, rc, ErrInfo.Core.pszMsg));
352 m_hLdrMod = NIL_RTLDRMOD;
353 }
354
355 if (RT_FAILURE(rc))
356 {
357 unloadServiceDLL();
358 }
359
360 return rc;
361}
362
363/** Helper function to free a local service DLL.
364 *
365 * @return VBox code
366 */
367void HGCMService::unloadServiceDLL(void)
368{
369 if (m_hLdrMod)
370 {
371 RTLdrClose(m_hLdrMod);
372 }
373
374 RT_ZERO(m_fntable);
375 m_pfnLoad = NULL;
376 m_hLdrMod = NIL_RTLDRMOD;
377}
378
379/*
380 * Messages processed by service threads. These threads only call the service entry points.
381 */
382
383#define SVC_MSG_LOAD (0) /* Load the service library and call VBOX_HGCM_SVCLOAD_NAME entry point. */
384#define SVC_MSG_UNLOAD (1) /* call pfnUnload and unload the service library. */
385#define SVC_MSG_CONNECT (2) /* pfnConnect */
386#define SVC_MSG_DISCONNECT (3) /* pfnDisconnect */
387#define SVC_MSG_GUESTCALL (4) /* pfnGuestCall */
388#define SVC_MSG_HOSTCALL (5) /* pfnHostCall */
389#define SVC_MSG_LOADSTATE (6) /* pfnLoadState. */
390#define SVC_MSG_SAVESTATE (7) /* pfnSaveState. */
391#define SVC_MSG_QUIT (8) /* Terminate the thread. */
392#define SVC_MSG_REGEXT (9) /* pfnRegisterExtension */
393#define SVC_MSG_UNREGEXT (10) /* pfnRegisterExtension */
394#ifdef VBOX_WITH_CRHGSMI
395# define SVC_MSG_HOSTFASTCALLASYNC (21) /* pfnHostCall */
396#endif
397
398class HGCMMsgSvcLoad: public HGCMMsgCore
399{
400 public:
401 HGCMMsgSvcLoad() : HGCMMsgCore(), pUVM() {}
402
403 /** The user mode VM handle (for statistics and such). */
404 PUVM pUVM;
405};
406
407class HGCMMsgSvcUnload: public HGCMMsgCore
408{
409};
410
411class HGCMMsgSvcConnect: public HGCMMsgCore
412{
413 public:
414 /* client identifier */
415 uint32_t u32ClientId;
416};
417
418class HGCMMsgSvcDisconnect: public HGCMMsgCore
419{
420 public:
421 /* client identifier */
422 uint32_t u32ClientId;
423};
424
425class HGCMMsgHeader: public HGCMMsgCore
426{
427 public:
428 HGCMMsgHeader() : pCmd(NULL), pHGCMPort(NULL) {};
429
430 /* Command pointer/identifier. */
431 PVBOXHGCMCMD pCmd;
432
433 /* Port to be informed on message completion. */
434 PPDMIHGCMPORT pHGCMPort;
435};
436
437
438class HGCMMsgCall: public HGCMMsgHeader
439{
440 public:
441 HGCMMsgCall() {}
442
443 HGCMMsgCall(HGCMThread *pThread)
444 {
445 InitializeCore(SVC_MSG_GUESTCALL, pThread);
446 Initialize();
447 }
448 ~HGCMMsgCall() { Log(("~HGCMMsgCall %p\n", this)); }
449
450 /* client identifier */
451 uint32_t u32ClientId;
452
453 /* function number */
454 uint32_t u32Function;
455
456 /* number of parameters */
457 uint32_t cParms;
458
459 VBOXHGCMSVCPARM *paParms;
460
461 /** The STAM_GET_TS() value when the request arrived. */
462 uint64_t tsArrival;
463};
464
465class HGCMMsgLoadSaveStateClient: public HGCMMsgCore
466{
467 public:
468 uint32_t u32ClientId;
469 PSSMHANDLE pSSM;
470};
471
472class HGCMMsgHostCallSvc: public HGCMMsgCore
473{
474 public:
475 /* function number */
476 uint32_t u32Function;
477
478 /* number of parameters */
479 uint32_t cParms;
480
481 VBOXHGCMSVCPARM *paParms;
482};
483
484class HGCMMsgSvcRegisterExtension: public HGCMMsgCore
485{
486 public:
487 /* Handle of the extension to be registered. */
488 HGCMSVCEXTHANDLE handle;
489 /* The extension entry point. */
490 PFNHGCMSVCEXT pfnExtension;
491 /* The extension pointer. */
492 void *pvExtension;
493};
494
495class HGCMMsgSvcUnregisterExtension: public HGCMMsgCore
496{
497 public:
498 /* Handle of the registered extension. */
499 HGCMSVCEXTHANDLE handle;
500};
501
502#ifdef VBOX_WITH_CRHGSMI
503class HGCMMsgHostFastCallAsyncSvc: public HGCMMsgCore
504{
505 public:
506 /* function number */
507 uint32_t u32Function;
508 /* parameter */
509 VBOXHGCMSVCPARM Param;
510 /* completion info */
511 PHGCMHOSTFASTCALLCB pfnCompletion;
512 void *pvCompletion;
513};
514#endif
515
516static HGCMMsgCore *hgcmMessageAllocSvc(uint32_t u32MsgId)
517{
518 switch (u32MsgId)
519 {
520#ifdef VBOX_WITH_CRHGSMI
521 case SVC_MSG_HOSTFASTCALLASYNC: return new HGCMMsgHostFastCallAsyncSvc();
522#endif
523 case SVC_MSG_LOAD: return new HGCMMsgSvcLoad();
524 case SVC_MSG_UNLOAD: return new HGCMMsgSvcUnload();
525 case SVC_MSG_CONNECT: return new HGCMMsgSvcConnect();
526 case SVC_MSG_DISCONNECT: return new HGCMMsgSvcDisconnect();
527 case SVC_MSG_HOSTCALL: return new HGCMMsgHostCallSvc();
528 case SVC_MSG_GUESTCALL: return new HGCMMsgCall();
529 case SVC_MSG_LOADSTATE:
530 case SVC_MSG_SAVESTATE: return new HGCMMsgLoadSaveStateClient();
531 case SVC_MSG_REGEXT: return new HGCMMsgSvcRegisterExtension();
532 case SVC_MSG_UNREGEXT: return new HGCMMsgSvcUnregisterExtension();
533 default:
534 AssertReleaseMsgFailed(("Msg id = %08X\n", u32MsgId));
535 }
536
537 return NULL;
538}
539
540/*
541 * The service thread. Loads the service library and calls the service entry points.
542 */
543DECLCALLBACK(void) hgcmServiceThread(HGCMThread *pThread, void *pvUser)
544{
545 HGCMService *pSvc = (HGCMService *)pvUser;
546 AssertRelease(pSvc != NULL);
547
548 bool fQuit = false;
549
550 while (!fQuit)
551 {
552 HGCMMsgCore *pMsgCore;
553 int rc = hgcmMsgGet(pThread, &pMsgCore);
554
555 if (RT_FAILURE(rc))
556 {
557 /* The error means some serious unrecoverable problem in the hgcmMsg/hgcmThread layer. */
558 AssertMsgFailed(("%Rrc\n", rc));
559 break;
560 }
561
562 STAM_REL_PROFILE_START(&pSvc->m_StatHandleMsg, a);
563
564 /* Cache required information to avoid unnecessary pMsgCore access. */
565 uint32_t u32MsgId = pMsgCore->MsgId();
566
567 switch (u32MsgId)
568 {
569#ifdef VBOX_WITH_CRHGSMI
570 case SVC_MSG_HOSTFASTCALLASYNC:
571 {
572 HGCMMsgHostFastCallAsyncSvc *pMsg = (HGCMMsgHostFastCallAsyncSvc *)pMsgCore;
573
574 LogFlowFunc(("SVC_MSG_HOSTFASTCALLASYNC u32Function = %d, pParm = %p\n", pMsg->u32Function, &pMsg->Param));
575
576 rc = pSvc->m_fntable.pfnHostCall(pSvc->m_fntable.pvService, pMsg->u32Function, 1, &pMsg->Param);
577 } break;
578#endif
579 case SVC_MSG_LOAD:
580 {
581 LogFlowFunc(("SVC_MSG_LOAD\n"));
582 rc = pSvc->loadServiceDLL();
583 } break;
584
585 case SVC_MSG_UNLOAD:
586 {
587 LogFlowFunc(("SVC_MSG_UNLOAD\n"));
588 if (pSvc->m_fntable.pfnUnload)
589 {
590 pSvc->m_fntable.pfnUnload(pSvc->m_fntable.pvService);
591 }
592
593 pSvc->unloadServiceDLL();
594 fQuit = true;
595 } break;
596
597 case SVC_MSG_CONNECT:
598 {
599 HGCMMsgSvcConnect *pMsg = (HGCMMsgSvcConnect *)pMsgCore;
600
601 LogFlowFunc(("SVC_MSG_CONNECT u32ClientId = %d\n", pMsg->u32ClientId));
602
603 HGCMClient *pClient = (HGCMClient *)hgcmObjReference(pMsg->u32ClientId, HGCMOBJ_CLIENT);
604
605 if (pClient)
606 {
607 rc = pSvc->m_fntable.pfnConnect(pSvc->m_fntable.pvService, pMsg->u32ClientId,
608 HGCM_CLIENT_DATA(pSvc, pClient));
609
610 hgcmObjDereference(pClient);
611 }
612 else
613 {
614 rc = VERR_HGCM_INVALID_CLIENT_ID;
615 }
616 } break;
617
618 case SVC_MSG_DISCONNECT:
619 {
620 HGCMMsgSvcDisconnect *pMsg = (HGCMMsgSvcDisconnect *)pMsgCore;
621
622 LogFlowFunc(("SVC_MSG_DISCONNECT u32ClientId = %d\n", pMsg->u32ClientId));
623
624 HGCMClient *pClient = (HGCMClient *)hgcmObjReference(pMsg->u32ClientId, HGCMOBJ_CLIENT);
625
626 if (pClient)
627 {
628 rc = pSvc->m_fntable.pfnDisconnect(pSvc->m_fntable.pvService, pMsg->u32ClientId,
629 HGCM_CLIENT_DATA(pSvc, pClient));
630
631 hgcmObjDereference(pClient);
632 }
633 else
634 {
635 rc = VERR_HGCM_INVALID_CLIENT_ID;
636 }
637 } break;
638
639 case SVC_MSG_GUESTCALL:
640 {
641 HGCMMsgCall *pMsg = (HGCMMsgCall *)pMsgCore;
642
643 LogFlowFunc(("SVC_MSG_GUESTCALL u32ClientId = %d, u32Function = %d, cParms = %d, paParms = %p\n",
644 pMsg->u32ClientId, pMsg->u32Function, pMsg->cParms, pMsg->paParms));
645
646 HGCMClient *pClient = (HGCMClient *)hgcmObjReference(pMsg->u32ClientId, HGCMOBJ_CLIENT);
647
648 if (pClient)
649 {
650 pSvc->m_fntable.pfnCall(pSvc->m_fntable.pvService, (VBOXHGCMCALLHANDLE)pMsg, pMsg->u32ClientId,
651 HGCM_CLIENT_DATA(pSvc, pClient), pMsg->u32Function,
652 pMsg->cParms, pMsg->paParms, pMsg->tsArrival);
653
654 hgcmObjDereference(pClient);
655 }
656 else
657 {
658 rc = VERR_HGCM_INVALID_CLIENT_ID;
659 }
660 } break;
661
662 case SVC_MSG_HOSTCALL:
663 {
664 HGCMMsgHostCallSvc *pMsg = (HGCMMsgHostCallSvc *)pMsgCore;
665
666 LogFlowFunc(("SVC_MSG_HOSTCALL u32Function = %d, cParms = %d, paParms = %p\n",
667 pMsg->u32Function, pMsg->cParms, pMsg->paParms));
668
669 rc = pSvc->m_fntable.pfnHostCall(pSvc->m_fntable.pvService, pMsg->u32Function, pMsg->cParms, pMsg->paParms);
670 } break;
671
672 case SVC_MSG_LOADSTATE:
673 {
674 HGCMMsgLoadSaveStateClient *pMsg = (HGCMMsgLoadSaveStateClient *)pMsgCore;
675
676 LogFlowFunc(("SVC_MSG_LOADSTATE\n"));
677
678 HGCMClient *pClient = (HGCMClient *)hgcmObjReference(pMsg->u32ClientId, HGCMOBJ_CLIENT);
679
680 if (pClient)
681 {
682 if (pSvc->m_fntable.pfnLoadState)
683 {
684 rc = pSvc->m_fntable.pfnLoadState(pSvc->m_fntable.pvService, pMsg->u32ClientId,
685 HGCM_CLIENT_DATA(pSvc, pClient), pMsg->pSSM);
686 }
687
688 hgcmObjDereference(pClient);
689 }
690 else
691 {
692 rc = VERR_HGCM_INVALID_CLIENT_ID;
693 }
694 } break;
695
696 case SVC_MSG_SAVESTATE:
697 {
698 HGCMMsgLoadSaveStateClient *pMsg = (HGCMMsgLoadSaveStateClient *)pMsgCore;
699
700 LogFlowFunc(("SVC_MSG_SAVESTATE\n"));
701
702 HGCMClient *pClient = (HGCMClient *)hgcmObjReference(pMsg->u32ClientId, HGCMOBJ_CLIENT);
703
704 rc = VINF_SUCCESS;
705
706 if (pClient)
707 {
708 if (pSvc->m_fntable.pfnSaveState)
709 {
710 g_fSaveState = true;
711 rc = pSvc->m_fntable.pfnSaveState(pSvc->m_fntable.pvService, pMsg->u32ClientId,
712 HGCM_CLIENT_DATA(pSvc, pClient), pMsg->pSSM);
713 g_fSaveState = false;
714 }
715
716 hgcmObjDereference(pClient);
717 }
718 else
719 {
720 rc = VERR_HGCM_INVALID_CLIENT_ID;
721 }
722 } break;
723
724 case SVC_MSG_REGEXT:
725 {
726 HGCMMsgSvcRegisterExtension *pMsg = (HGCMMsgSvcRegisterExtension *)pMsgCore;
727
728 LogFlowFunc(("SVC_MSG_REGEXT handle = %p, pfn = %p\n", pMsg->handle, pMsg->pfnExtension));
729
730 if (pSvc->m_hExtension)
731 {
732 rc = VERR_NOT_SUPPORTED;
733 }
734 else
735 {
736 if (pSvc->m_fntable.pfnRegisterExtension)
737 {
738 rc = pSvc->m_fntable.pfnRegisterExtension(pSvc->m_fntable.pvService, pMsg->pfnExtension,
739 pMsg->pvExtension);
740 }
741 else
742 {
743 rc = VERR_NOT_SUPPORTED;
744 }
745
746 if (RT_SUCCESS(rc))
747 {
748 pSvc->m_hExtension = pMsg->handle;
749 }
750 }
751 } break;
752
753 case SVC_MSG_UNREGEXT:
754 {
755 HGCMMsgSvcUnregisterExtension *pMsg = (HGCMMsgSvcUnregisterExtension *)pMsgCore;
756
757 LogFlowFunc(("SVC_MSG_UNREGEXT handle = %p\n", pMsg->handle));
758
759 if (pSvc->m_hExtension != pMsg->handle)
760 {
761 rc = VERR_NOT_SUPPORTED;
762 }
763 else
764 {
765 if (pSvc->m_fntable.pfnRegisterExtension)
766 {
767 rc = pSvc->m_fntable.pfnRegisterExtension(pSvc->m_fntable.pvService, NULL, NULL);
768 }
769 else
770 {
771 rc = VERR_NOT_SUPPORTED;
772 }
773
774 pSvc->m_hExtension = NULL;
775 }
776 } break;
777
778 default:
779 {
780 AssertMsgFailed(("hgcmServiceThread::Unsupported message number %08X\n", u32MsgId));
781 rc = VERR_NOT_SUPPORTED;
782 } break;
783 }
784
785 if (u32MsgId != SVC_MSG_GUESTCALL)
786 {
787 /* For SVC_MSG_GUESTCALL the service calls the completion helper.
788 * Other messages have to be completed here.
789 */
790 hgcmMsgComplete (pMsgCore, rc);
791 }
792 STAM_REL_PROFILE_STOP(&pSvc->m_StatHandleMsg, a);
793 }
794}
795
796/**
797 * @interface_method_impl{VBOXHGCMSVCHELPERS,pfnCallComplete}
798 */
799/* static */ DECLCALLBACK(int) HGCMService::svcHlpCallComplete(VBOXHGCMCALLHANDLE callHandle, int32_t rc)
800{
801 HGCMMsgCore *pMsgCore = (HGCMMsgCore *)callHandle;
802
803 /* Only call the completion for these messages. The helper
804 * is called by the service, and the service does not get
805 * any other messages.
806 */
807 AssertMsgReturn(pMsgCore->MsgId() == SVC_MSG_GUESTCALL, ("%d\n", pMsgCore->MsgId()), VERR_WRONG_TYPE);
808 return hgcmMsgComplete(pMsgCore, rc);
809}
810
811/**
812 * @interface_method_impl{VBOXHGCMSVCHELPERS,pfnDisconnectClient}
813 */
814/* static */ DECLCALLBACK(void) HGCMService::svcHlpDisconnectClient(void *pvInstance, uint32_t u32ClientId)
815{
816 HGCMService *pService = static_cast <HGCMService *> (pvInstance);
817
818 if (pService)
819 {
820 pService->DisconnectClient(u32ClientId, true);
821 }
822}
823
824/**
825 * @interface_method_impl{VBOXHGCMSVCHELPERS,pfnIsCallRestored}
826 */
827/* static */ DECLCALLBACK(bool) HGCMService::svcHlpIsCallRestored(VBOXHGCMCALLHANDLE callHandle)
828{
829 HGCMMsgHeader *pMsgHdr = (HGCMMsgHeader *)(callHandle);
830 AssertPtrReturn(pMsgHdr, false);
831
832 PVBOXHGCMCMD pCmd = pMsgHdr->pCmd;
833 AssertPtrReturn(pCmd, false);
834
835 PPDMIHGCMPORT pHgcmPort = pMsgHdr->pHGCMPort;
836 AssertPtrReturn(pHgcmPort, false);
837
838 return pHgcmPort->pfnIsCmdRestored(pHgcmPort, pCmd);
839}
840
841/**
842 * @interface_method_impl{VBOXHGCMSVCHELPERS,pfnStamRegisterV}
843 */
844/* static */ DECLCALLBACK(int)
845HGCMService::svcHlpStamRegisterV(void *pvInstance, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility,
846 STAMUNIT enmUnit, const char *pszDesc, const char *pszName, va_list va)
847{
848 HGCMService *pService = static_cast <HGCMService *>(pvInstance);
849 AssertPtrReturn(pService, VERR_INVALID_PARAMETER);
850
851 return STAMR3RegisterVU(pService->m_pUVM, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName, va);
852}
853
854/**
855 * @interface_method_impl{VBOXHGCMSVCHELPERS,pfnStamDeregisterV}
856 */
857/* static */ DECLCALLBACK(int) HGCMService::svcHlpStamDeregisterV(void *pvInstance, const char *pszPatFmt, va_list va)
858{
859 HGCMService *pService = static_cast <HGCMService *>(pvInstance);
860 AssertPtrReturn(pService, VERR_INVALID_PARAMETER);
861
862 if (pService->m_pUVM)
863 return STAMR3DeregisterV(pService->m_pUVM, pszPatFmt, va);
864 return VINF_SUCCESS;
865}
866
867/**
868 * @interface_method_impl{VBOXHGCMSVCHELPERS,pfnInfoRegister}
869 */
870/* static */ DECLCALLBACK(int) HGCMService::svcHlpInfoRegister(void *pvInstance, const char *pszName, const char *pszDesc,
871 PFNDBGFHANDLEREXT pfnHandler, void *pvUser)
872{
873 HGCMService *pService = static_cast <HGCMService *>(pvInstance);
874 AssertPtrReturn(pService, VERR_INVALID_PARAMETER);
875
876 return DBGFR3InfoRegisterExternal(pService->m_pUVM, pszName, pszDesc, pfnHandler, pvUser);
877}
878
879/**
880 * @interface_method_impl{VBOXHGCMSVCHELPERS,pfnInfoDeregister}
881 */
882/* static */ DECLCALLBACK(int) HGCMService::svcHlpInfoDeregister(void *pvInstance, const char *pszName)
883{
884 HGCMService *pService = static_cast <HGCMService *>(pvInstance);
885 AssertPtrReturn(pService, VERR_INVALID_PARAMETER);
886 if (pService->m_pUVM)
887 return DBGFR3InfoDeregisterExternal(pService->m_pUVM, pszName);
888 return VINF_SUCCESS;
889}
890
891
892/**
893 * @interface_method_impl{VBOXHGCMSVCHELPERS,pfnGetRequestor}
894 */
895/* static */ DECLCALLBACK(uint32_t) HGCMService::svcHlpGetRequestor(VBOXHGCMCALLHANDLE hCall)
896{
897 HGCMMsgHeader *pMsgHdr = (HGCMMsgHeader *)(hCall);
898 AssertPtrReturn(pMsgHdr, 0);
899
900 PVBOXHGCMCMD pCmd = pMsgHdr->pCmd;
901 AssertPtrReturn(pCmd, 0);
902
903 PPDMIHGCMPORT pHgcmPort = pMsgHdr->pHGCMPort;
904 AssertPtrReturn(pHgcmPort, 0);
905
906 return pHgcmPort->pfnGetRequestor(pHgcmPort, pCmd);
907}
908
909
910static DECLCALLBACK(int) hgcmMsgCompletionCallback(int32_t result, HGCMMsgCore *pMsgCore)
911{
912 /* Call the VMMDev port interface to issue IRQ notification. */
913 HGCMMsgHeader *pMsgHdr = (HGCMMsgHeader *)pMsgCore;
914
915 LogFlow(("MAIN::hgcmMsgCompletionCallback: message %p\n", pMsgCore));
916
917 if (pMsgHdr->pHGCMPort)
918 {
919 if (!g_fResetting)
920 return pMsgHdr->pHGCMPort->pfnCompleted(pMsgHdr->pHGCMPort,
921 g_fSaveState ? VINF_HGCM_SAVE_STATE : result, pMsgHdr->pCmd);
922 return VERR_ALREADY_RESET; /* best I could find. */
923 }
924 return VERR_NOT_AVAILABLE;
925}
926
927/*
928 * The main HGCM methods of the service.
929 */
930
931int HGCMService::instanceCreate(const char *pszServiceLibrary, const char *pszServiceName, PUVM pUVM)
932{
933 LogFlowFunc(("name %s, lib %s\n", pszServiceName, pszServiceLibrary));
934 /* The maximum length of the thread name, allowed by the RT is 15. */
935 char szThreadName[16];
936 if (!strncmp(pszServiceName, RT_STR_TUPLE("VBoxShared")))
937 RTStrPrintf(szThreadName, sizeof(szThreadName), "Sh%s", pszServiceName + 10);
938 else if (!strncmp(pszServiceName, RT_STR_TUPLE("VBox")))
939 RTStrCopy(szThreadName, sizeof(szThreadName), pszServiceName + 4);
940 else
941 RTStrCopy(szThreadName, sizeof(szThreadName), pszServiceName);
942
943 int rc = hgcmThreadCreate(&m_pThread, szThreadName, hgcmServiceThread, this, pszServiceName, pUVM);
944
945 if (RT_SUCCESS(rc))
946 {
947 m_pszSvcName = RTStrDup(pszServiceName);
948 m_pszSvcLibrary = RTStrDup(pszServiceLibrary);
949
950 if (!m_pszSvcName || !m_pszSvcLibrary)
951 {
952 RTStrFree(m_pszSvcLibrary);
953 m_pszSvcLibrary = NULL;
954
955 RTStrFree(m_pszSvcName);
956 m_pszSvcName = NULL;
957
958 rc = VERR_NO_MEMORY;
959 }
960 else
961 {
962 /* Register statistics: */
963 m_pUVM = pUVM;
964 STAMR3RegisterFU(pUVM, &m_StatHandleMsg, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
965 "Message handling", "/HGCM/%s/Msg", pszServiceName);
966
967 /* Initialize service helpers table. */
968 m_svcHelpers.pfnCallComplete = svcHlpCallComplete;
969 m_svcHelpers.pvInstance = this;
970 m_svcHelpers.pfnDisconnectClient = svcHlpDisconnectClient;
971 m_svcHelpers.pfnIsCallRestored = svcHlpIsCallRestored;
972 m_svcHelpers.pfnStamRegisterV = svcHlpStamRegisterV;
973 m_svcHelpers.pfnStamDeregisterV = svcHlpStamDeregisterV;
974 m_svcHelpers.pfnInfoRegister = svcHlpInfoRegister;
975 m_svcHelpers.pfnInfoDeregister = svcHlpInfoDeregister;
976 m_svcHelpers.pfnGetRequestor = svcHlpGetRequestor;
977
978 /* Execute the load request on the service thread. */
979 HGCMMsgCore *pCoreMsg;
980 rc = hgcmMsgAlloc(m_pThread, &pCoreMsg, SVC_MSG_LOAD, hgcmMessageAllocSvc);
981
982 if (RT_SUCCESS(rc))
983 {
984 HGCMMsgSvcLoad *pMsg = (HGCMMsgSvcLoad *)pCoreMsg;
985
986 pMsg->pUVM = pUVM;
987
988 rc = hgcmMsgSend(pMsg);
989 }
990 }
991 }
992
993 if (RT_FAILURE(rc))
994 {
995 instanceDestroy();
996 }
997
998 LogFlowFunc(("rc = %Rrc\n", rc));
999 return rc;
1000}
1001
1002void HGCMService::instanceDestroy(void)
1003{
1004 LogFlowFunc(("%s\n", m_pszSvcName));
1005
1006 HGCMMsgCore *pMsg;
1007 int rc = hgcmMsgAlloc(m_pThread, &pMsg, SVC_MSG_UNLOAD, hgcmMessageAllocSvc);
1008
1009 if (RT_SUCCESS(rc))
1010 {
1011 rc = hgcmMsgSend(pMsg);
1012
1013 if (RT_SUCCESS(rc))
1014 hgcmThreadWait(m_pThread);
1015 }
1016
1017 if (m_pszSvcName && m_pUVM)
1018 STAMR3DeregisterF(m_pUVM, "/HGCM/%s/*", m_pszSvcName);
1019 m_pUVM = NULL;
1020
1021 RTStrFree(m_pszSvcLibrary);
1022 m_pszSvcLibrary = NULL;
1023
1024 RTStrFree(m_pszSvcName);
1025 m_pszSvcName = NULL;
1026}
1027
1028int HGCMService::saveClientState(uint32_t u32ClientId, PSSMHANDLE pSSM)
1029{
1030 LogFlowFunc(("%s\n", m_pszSvcName));
1031
1032 HGCMMsgCore *pCoreMsg;
1033 int rc = hgcmMsgAlloc(m_pThread, &pCoreMsg, SVC_MSG_SAVESTATE, hgcmMessageAllocSvc);
1034
1035 if (RT_SUCCESS(rc))
1036 {
1037 HGCMMsgLoadSaveStateClient *pMsg = (HGCMMsgLoadSaveStateClient *)pCoreMsg;
1038
1039 pMsg->u32ClientId = u32ClientId;
1040 pMsg->pSSM = pSSM;
1041
1042 rc = hgcmMsgSend(pMsg);
1043 }
1044
1045 LogFlowFunc(("rc = %Rrc\n", rc));
1046 return rc;
1047}
1048
1049int HGCMService::loadClientState(uint32_t u32ClientId, PSSMHANDLE pSSM)
1050{
1051 LogFlowFunc(("%s\n", m_pszSvcName));
1052
1053 HGCMMsgCore *pCoreMsg;
1054 int rc = hgcmMsgAlloc(m_pThread, &pCoreMsg, SVC_MSG_LOADSTATE, hgcmMessageAllocSvc);
1055
1056 if (RT_SUCCESS(rc))
1057 {
1058 HGCMMsgLoadSaveStateClient *pMsg = (HGCMMsgLoadSaveStateClient *)pCoreMsg;
1059
1060 pMsg->u32ClientId = u32ClientId;
1061 pMsg->pSSM = pSSM;
1062
1063 rc = hgcmMsgSend(pMsg);
1064 }
1065
1066 LogFlowFunc(("rc = %Rrc\n", rc));
1067 return rc;
1068}
1069
1070
1071/** The method creates a service and references it.
1072 *
1073 * @param pszServiceLibrary The library to be loaded.
1074 * @param pszServiceName The name of the service.
1075 * @param pUVM The user mode VM handle (for statistics and such).
1076 * @return VBox rc.
1077 * @thread main HGCM
1078 */
1079/* static */ int HGCMService::LoadService(const char *pszServiceLibrary, const char *pszServiceName, PUVM pUVM)
1080{
1081 LogFlowFunc(("lib %s, name = %s, pUVM = %p\n", pszServiceLibrary, pszServiceName, pUVM));
1082
1083 /* Look at already loaded services to avoid double loading. */
1084
1085 HGCMService *pSvc;
1086 int rc = HGCMService::ResolveService(&pSvc, pszServiceName);
1087
1088 if (RT_SUCCESS(rc))
1089 {
1090 /* The service is already loaded. */
1091 pSvc->ReleaseService();
1092 rc = VERR_HGCM_SERVICE_EXISTS;
1093 }
1094 else
1095 {
1096 /* Create the new service. */
1097 pSvc = new (std::nothrow) HGCMService();
1098
1099 if (!pSvc)
1100 {
1101 rc = VERR_NO_MEMORY;
1102 }
1103 else
1104 {
1105 /* Load the library and call the initialization entry point. */
1106 rc = pSvc->instanceCreate(pszServiceLibrary, pszServiceName, pUVM);
1107
1108 if (RT_SUCCESS(rc))
1109 {
1110 /* Insert the just created service to list for future references. */
1111 pSvc->m_pSvcNext = sm_pSvcListHead;
1112 pSvc->m_pSvcPrev = NULL;
1113
1114 if (sm_pSvcListHead)
1115 {
1116 sm_pSvcListHead->m_pSvcPrev = pSvc;
1117 }
1118 else
1119 {
1120 sm_pSvcListTail = pSvc;
1121 }
1122
1123 sm_pSvcListHead = pSvc;
1124
1125 sm_cServices++;
1126
1127 /* Reference the service (for first time) until it is unloaded on HGCM termination. */
1128 AssertRelease(pSvc->m_u32RefCnt == 0);
1129 pSvc->ReferenceService();
1130
1131 LogFlowFunc(("service %p\n", pSvc));
1132 }
1133 }
1134 }
1135
1136 LogFlowFunc(("rc = %Rrc\n", rc));
1137 return rc;
1138}
1139
1140/** The method unloads a service.
1141 *
1142 * @thread main HGCM
1143 */
1144void HGCMService::UnloadService(bool fUvmIsInvalid)
1145{
1146 LogFlowFunc(("name = %s\n", m_pszSvcName));
1147
1148 if (fUvmIsInvalid)
1149 m_pUVM = NULL;
1150
1151 /* Remove the service from the list. */
1152 if (m_pSvcNext)
1153 {
1154 m_pSvcNext->m_pSvcPrev = m_pSvcPrev;
1155 }
1156 else
1157 {
1158 sm_pSvcListTail = m_pSvcPrev;
1159 }
1160
1161 if (m_pSvcPrev)
1162 {
1163 m_pSvcPrev->m_pSvcNext = m_pSvcNext;
1164 }
1165 else
1166 {
1167 sm_pSvcListHead = m_pSvcNext;
1168 }
1169
1170 sm_cServices--;
1171
1172 /* The service must be unloaded only if all clients were disconnected. */
1173 LogFlowFunc(("m_u32RefCnt = %d\n", m_u32RefCnt));
1174 AssertRelease(m_u32RefCnt == 1);
1175
1176 /* Now the service can be released. */
1177 ReleaseService();
1178}
1179
1180/** The method unloads all services.
1181 *
1182 * @thread main HGCM
1183 */
1184/* static */ void HGCMService::UnloadAll(bool fUvmIsInvalid)
1185{
1186 while (sm_pSvcListHead)
1187 {
1188 sm_pSvcListHead->UnloadService(fUvmIsInvalid);
1189 }
1190}
1191
1192/** The method obtains a referenced pointer to the service with
1193 * specified name. The caller must call ReleaseService when
1194 * the pointer is no longer needed.
1195 *
1196 * @param ppSvc Where to store the pointer to the service.
1197 * @param pszServiceName The name of the service.
1198 * @return VBox rc.
1199 * @thread main HGCM
1200 */
1201/* static */ int HGCMService::ResolveService(HGCMService **ppSvc, const char *pszServiceName)
1202{
1203 LogFlowFunc(("ppSvc = %p name = %s\n",
1204 ppSvc, pszServiceName));
1205
1206 if (!ppSvc || !pszServiceName)
1207 {
1208 return VERR_INVALID_PARAMETER;
1209 }
1210
1211 HGCMService *pSvc = sm_pSvcListHead;
1212
1213 while (pSvc)
1214 {
1215 if (strcmp(pSvc->m_pszSvcName, pszServiceName) == 0)
1216 {
1217 break;
1218 }
1219
1220 pSvc = pSvc->m_pSvcNext;
1221 }
1222
1223 LogFlowFunc(("lookup in the list is %p\n", pSvc));
1224
1225 if (pSvc == NULL)
1226 {
1227 *ppSvc = NULL;
1228 return VERR_HGCM_SERVICE_NOT_FOUND;
1229 }
1230
1231 pSvc->ReferenceService();
1232
1233 *ppSvc = pSvc;
1234
1235 return VINF_SUCCESS;
1236}
1237
1238/** The method increases reference counter.
1239 *
1240 * @thread main HGCM
1241 */
1242void HGCMService::ReferenceService(void)
1243{
1244 ASMAtomicIncU32(&m_u32RefCnt);
1245 LogFlowFunc(("[%s] m_u32RefCnt = %d\n", m_pszSvcName, m_u32RefCnt));
1246}
1247
1248/** The method dereferences a service and deletes it when no more refs.
1249 *
1250 * @thread main HGCM
1251 */
1252void HGCMService::ReleaseService(void)
1253{
1254 LogFlowFunc(("m_u32RefCnt = %d\n", m_u32RefCnt));
1255 uint32_t u32RefCnt = ASMAtomicDecU32(&m_u32RefCnt);
1256 AssertRelease(u32RefCnt != ~0U);
1257
1258 LogFlowFunc(("u32RefCnt = %d, name %s\n", u32RefCnt, m_pszSvcName));
1259
1260 if (u32RefCnt == 0)
1261 {
1262 instanceDestroy();
1263 delete this;
1264 }
1265}
1266
1267/** The method is called when the VM is being reset or terminated
1268 * and disconnects all clients from all services.
1269 *
1270 * @thread main HGCM
1271 */
1272/* static */ void HGCMService::Reset(void)
1273{
1274 g_fResetting = true;
1275
1276 HGCMService *pSvc = sm_pSvcListHead;
1277
1278 while (pSvc)
1279 {
1280 while (pSvc->m_cClients && pSvc->m_paClientIds)
1281 {
1282 LogFlowFunc(("handle %d\n", pSvc->m_paClientIds[0]));
1283 pSvc->DisconnectClient(pSvc->m_paClientIds[0], false);
1284 }
1285
1286#ifdef VBOX_WITH_CRHGSMI
1287 /** @todo could this actually happen that the service is destroyed on ReleaseService? */
1288 HGCMService *pNextSvc = pSvc->m_pSvcNext;
1289 while (pSvc->m_cHandleAcquires)
1290 {
1291 pSvc->HandleReleased();
1292 pSvc->ReleaseService();
1293 }
1294 pSvc = pNextSvc;
1295#else
1296 pSvc = pSvc->m_pSvcNext;
1297#endif
1298 }
1299
1300 g_fResetting = false;
1301}
1302
1303/** The method saves the HGCM state.
1304 *
1305 * @param pSSM The saved state context.
1306 * @return VBox rc.
1307 * @thread main HGCM
1308 */
1309/* static */ int HGCMService::SaveState(PSSMHANDLE pSSM)
1310{
1311 /* Save the current handle count and restore afterwards to avoid client id conflicts. */
1312 int rc = SSMR3PutU32(pSSM, hgcmObjQueryHandleCount());
1313 AssertRCReturn(rc, rc);
1314
1315 LogFlowFunc(("%d services to be saved:\n", sm_cServices));
1316
1317 /* Save number of services. */
1318 rc = SSMR3PutU32(pSSM, sm_cServices);
1319 AssertRCReturn(rc, rc);
1320
1321 /* Save every service. */
1322 HGCMService *pSvc = sm_pSvcListHead;
1323
1324 while (pSvc)
1325 {
1326 LogFlowFunc(("Saving service [%s]\n", pSvc->m_pszSvcName));
1327
1328 /* Save the length of the service name. */
1329 rc = SSMR3PutU32(pSSM, (uint32_t) strlen(pSvc->m_pszSvcName) + 1);
1330 AssertRCReturn(rc, rc);
1331
1332 /* Save the name of the service. */
1333 rc = SSMR3PutStrZ(pSSM, pSvc->m_pszSvcName);
1334 AssertRCReturn(rc, rc);
1335
1336 /* Save the number of clients. */
1337 rc = SSMR3PutU32(pSSM, pSvc->m_cClients);
1338 AssertRCReturn(rc, rc);
1339
1340 /* Call the service for every client. Normally a service must not have
1341 * a global state to be saved: only per client info is relevant.
1342 * The global state of a service is configured during VM startup.
1343 */
1344 uint32_t i;
1345
1346 for (i = 0; i < pSvc->m_cClients; i++)
1347 {
1348 uint32_t u32ClientId = pSvc->m_paClientIds[i];
1349
1350 Log(("client id 0x%08X\n", u32ClientId));
1351
1352 /* Save the client id. */
1353 rc = SSMR3PutU32(pSSM, u32ClientId);
1354 AssertRCReturn(rc, rc);
1355
1356 /* Call the service, so the operation is executed by the service thread. */
1357 rc = pSvc->saveClientState(u32ClientId, pSSM);
1358 AssertRCReturn(rc, rc);
1359 }
1360
1361 pSvc = pSvc->m_pSvcNext;
1362 }
1363
1364 return VINF_SUCCESS;
1365}
1366
1367/** The method loads saved HGCM state.
1368 *
1369 * @param pSSM The saved state context.
1370 * @return VBox rc.
1371 * @thread main HGCM
1372 */
1373/* static */ int HGCMService::LoadState(PSSMHANDLE pSSM)
1374{
1375 /* Restore handle count to avoid client id conflicts. */
1376 uint32_t u32;
1377
1378 int rc = SSMR3GetU32(pSSM, &u32);
1379 AssertRCReturn(rc, rc);
1380
1381 hgcmObjSetHandleCount(u32);
1382
1383 /* Get the number of services. */
1384 uint32_t cServices;
1385
1386 rc = SSMR3GetU32(pSSM, &cServices);
1387 AssertRCReturn(rc, rc);
1388
1389 LogFlowFunc(("%d services to be restored:\n", cServices));
1390
1391 while (cServices--)
1392 {
1393 /* Get the length of the service name. */
1394 rc = SSMR3GetU32(pSSM, &u32);
1395 AssertRCReturn(rc, rc);
1396 AssertReturn(u32 <= VBOX_HGCM_SVC_NAME_MAX_BYTES, VERR_SSM_UNEXPECTED_DATA);
1397
1398 /* Get the service name. */
1399 char szServiceName[VBOX_HGCM_SVC_NAME_MAX_BYTES];
1400 rc = SSMR3GetStrZ(pSSM, szServiceName, u32);
1401 AssertRCReturn(rc, rc);
1402
1403 LogRel(("HGCM: Restoring [%s]\n", szServiceName));
1404
1405 /* Resolve the service instance. */
1406 HGCMService *pSvc;
1407 rc = ResolveService(&pSvc, szServiceName);
1408 AssertLogRelMsgReturn(pSvc, ("rc=%Rrc, %s\n", rc, szServiceName), VERR_SSM_UNEXPECTED_DATA);
1409
1410 /* Get the number of clients. */
1411 uint32_t cClients;
1412 rc = SSMR3GetU32(pSSM, &cClients);
1413 if (RT_FAILURE(rc))
1414 {
1415 pSvc->ReleaseService();
1416 AssertFailed();
1417 return rc;
1418 }
1419
1420 while (cClients--)
1421 {
1422 /* Get the client id. */
1423 uint32_t u32ClientId;
1424 rc = SSMR3GetU32(pSSM, &u32ClientId);
1425 if (RT_FAILURE(rc))
1426 {
1427 pSvc->ReleaseService();
1428 AssertFailed();
1429 return rc;
1430 }
1431
1432 /* Connect the client. */
1433 rc = pSvc->CreateAndConnectClient(NULL, u32ClientId);
1434 if (RT_FAILURE(rc))
1435 {
1436 pSvc->ReleaseService();
1437 AssertLogRelMsgFailed(("rc=%Rrc %s\n", rc, szServiceName));
1438 return rc;
1439 }
1440
1441 /* Call the service, so the operation is executed by the service thread. */
1442 rc = pSvc->loadClientState(u32ClientId, pSSM);
1443 if (RT_FAILURE(rc))
1444 {
1445 pSvc->ReleaseService();
1446 AssertLogRelMsgFailed(("rc=%Rrc %s\n", rc, szServiceName));
1447 return rc;
1448 }
1449 }
1450
1451 pSvc->ReleaseService();
1452 }
1453
1454 return VINF_SUCCESS;
1455}
1456
1457/* Create a new client instance and connect it to the service.
1458 *
1459 * @param pu32ClientIdOut If not NULL, then the method must generate a new handle for the client.
1460 * If NULL, use the given 'u32ClientIdIn' handle.
1461 * @param u32ClientIdIn The handle for the client, when 'pu32ClientIdOut' is NULL.
1462 * @return VBox rc.
1463 */
1464int HGCMService::CreateAndConnectClient(uint32_t *pu32ClientIdOut, uint32_t u32ClientIdIn)
1465{
1466 LogFlowFunc(("pu32ClientIdOut = %p, u32ClientIdIn = %d\n", pu32ClientIdOut, u32ClientIdIn));
1467
1468 /* Allocate a client information structure. */
1469 HGCMClient *pClient = new HGCMClient();
1470
1471 if (!pClient)
1472 {
1473 Log1WarningFunc(("Could not allocate HGCMClient!!!\n"));
1474 return VERR_NO_MEMORY;
1475 }
1476
1477 uint32_t handle;
1478
1479 if (pu32ClientIdOut != NULL)
1480 {
1481 handle = hgcmObjGenerateHandle(pClient);
1482 }
1483 else
1484 {
1485 handle = hgcmObjAssignHandle(pClient, u32ClientIdIn);
1486 }
1487
1488 LogFlowFunc(("client id = %d\n", handle));
1489
1490 AssertRelease(handle);
1491
1492 /* Initialize the HGCM part of the client. */
1493 int rc = pClient->Init(this);
1494
1495 if (RT_SUCCESS(rc))
1496 {
1497 /* Call the service. */
1498 HGCMMsgCore *pCoreMsg;
1499
1500 rc = hgcmMsgAlloc(m_pThread, &pCoreMsg, SVC_MSG_CONNECT, hgcmMessageAllocSvc);
1501
1502 if (RT_SUCCESS(rc))
1503 {
1504 HGCMMsgSvcConnect *pMsg = (HGCMMsgSvcConnect *)pCoreMsg;
1505
1506 pMsg->u32ClientId = handle;
1507
1508 rc = hgcmMsgSend(pMsg);
1509
1510 if (RT_SUCCESS(rc))
1511 {
1512 /* Add the client Id to the array. */
1513 if (m_cClients == m_cClientsAllocated)
1514 {
1515 const uint32_t cDelta = 64;
1516
1517 /* Guards against integer overflow on 32bit arch and also limits size of m_paClientIds array to 4GB*/
1518 if (m_cClientsAllocated < UINT32_MAX / sizeof(m_paClientIds[0]) - cDelta)
1519 {
1520 uint32_t *paClientIdsNew;
1521
1522 paClientIdsNew = (uint32_t *)RTMemRealloc(m_paClientIds,
1523 (m_cClientsAllocated + cDelta) * sizeof(m_paClientIds[0]));
1524 Assert(paClientIdsNew);
1525
1526 if (paClientIdsNew)
1527 {
1528 m_paClientIds = paClientIdsNew;
1529 m_cClientsAllocated += cDelta;
1530 }
1531 else
1532 {
1533 rc = VERR_NO_MEMORY;
1534 }
1535 }
1536 else
1537 {
1538 rc = VERR_NO_MEMORY;
1539 }
1540 }
1541
1542 m_paClientIds[m_cClients] = handle;
1543 m_cClients++;
1544 }
1545 }
1546 }
1547
1548 if (RT_FAILURE(rc))
1549 {
1550 hgcmObjDeleteHandle(handle);
1551 }
1552 else
1553 {
1554 if (pu32ClientIdOut != NULL)
1555 {
1556 *pu32ClientIdOut = handle;
1557 }
1558
1559 ReferenceService();
1560 }
1561
1562 LogFlowFunc(("rc = %Rrc\n", rc));
1563 return rc;
1564}
1565
1566/* Disconnect the client from the service and delete the client handle.
1567 *
1568 * @param u32ClientId The handle of the client.
1569 * @return VBox rc.
1570 */
1571int HGCMService::DisconnectClient(uint32_t u32ClientId, bool fFromService)
1572{
1573 int rc = VINF_SUCCESS;
1574
1575 LogFlowFunc(("client id = %d, fFromService = %d\n", u32ClientId, fFromService));
1576
1577 if (!fFromService)
1578 {
1579 /* Call the service. */
1580 HGCMMsgCore *pCoreMsg;
1581
1582 rc = hgcmMsgAlloc(m_pThread, &pCoreMsg, SVC_MSG_DISCONNECT, hgcmMessageAllocSvc);
1583
1584 if (RT_SUCCESS(rc))
1585 {
1586 HGCMMsgSvcDisconnect *pMsg = (HGCMMsgSvcDisconnect *)pCoreMsg;
1587
1588 pMsg->u32ClientId = u32ClientId;
1589
1590 rc = hgcmMsgSend(pMsg);
1591 }
1592 else
1593 {
1594 LogRel(("(%d, %d) [%s] hgcmMsgAlloc(%p, SVC_MSG_DISCONNECT) failed %Rrc\n",
1595 u32ClientId, fFromService, RT_VALID_PTR(m_pszSvcName)? m_pszSvcName: "", m_pThread, rc));
1596 }
1597 }
1598
1599 /* Remove the client id from the array in any case, rc does not matter. */
1600 uint32_t i;
1601
1602 for (i = 0; i < m_cClients; i++)
1603 {
1604 if (m_paClientIds[i] == u32ClientId)
1605 {
1606 m_cClients--;
1607
1608 if (m_cClients > i)
1609 memmove(&m_paClientIds[i], &m_paClientIds[i + 1], sizeof(m_paClientIds[0]) * (m_cClients - i));
1610
1611 /* Delete the client handle. */
1612 hgcmObjDeleteHandle(u32ClientId);
1613
1614 /* The service must be released. */
1615 ReleaseService();
1616
1617 break;
1618 }
1619 }
1620
1621 LogFlowFunc(("rc = %Rrc\n", rc));
1622 return rc;
1623}
1624
1625int HGCMService::RegisterExtension(HGCMSVCEXTHANDLE handle,
1626 PFNHGCMSVCEXT pfnExtension,
1627 void *pvExtension)
1628{
1629 LogFlowFunc(("%s\n", handle->pszServiceName));
1630
1631 /* Forward the message to the service thread. */
1632 HGCMMsgCore *pCoreMsg;
1633 int rc = hgcmMsgAlloc(m_pThread, &pCoreMsg, SVC_MSG_REGEXT, hgcmMessageAllocSvc);
1634
1635 if (RT_SUCCESS(rc))
1636 {
1637 HGCMMsgSvcRegisterExtension *pMsg = (HGCMMsgSvcRegisterExtension *)pCoreMsg;
1638
1639 pMsg->handle = handle;
1640 pMsg->pfnExtension = pfnExtension;
1641 pMsg->pvExtension = pvExtension;
1642
1643 rc = hgcmMsgSend(pMsg);
1644 }
1645
1646 LogFlowFunc(("rc = %Rrc\n", rc));
1647 return rc;
1648}
1649
1650void HGCMService::UnregisterExtension(HGCMSVCEXTHANDLE handle)
1651{
1652 /* Forward the message to the service thread. */
1653 HGCMMsgCore *pCoreMsg;
1654 int rc = hgcmMsgAlloc(m_pThread, &pCoreMsg, SVC_MSG_UNREGEXT, hgcmMessageAllocSvc);
1655
1656 if (RT_SUCCESS(rc))
1657 {
1658 HGCMMsgSvcUnregisterExtension *pMsg = (HGCMMsgSvcUnregisterExtension *)pCoreMsg;
1659
1660 pMsg->handle = handle;
1661
1662 rc = hgcmMsgSend(pMsg);
1663 }
1664
1665 LogFlowFunc(("rc = %Rrc\n", rc));
1666}
1667
1668/** Perform a guest call to the service.
1669 *
1670 * @param pHGCMPort The port to be used for completion confirmation.
1671 * @param pCmd The VBox HGCM context.
1672 * @param u32ClientId The client handle to be disconnected and deleted.
1673 * @param u32Function The function number.
1674 * @param cParms Number of parameters.
1675 * @param paParms Pointer to array of parameters.
1676 * @param tsArrival The STAM_GET_TS() value when the request arrived.
1677 * @return VBox rc.
1678 * @retval VINF_HGCM_ASYNC_EXECUTE on success.
1679 */
1680int HGCMService::GuestCall(PPDMIHGCMPORT pHGCMPort, PVBOXHGCMCMD pCmd, uint32_t u32ClientId, uint32_t u32Function,
1681 uint32_t cParms, VBOXHGCMSVCPARM paParms[], uint64_t tsArrival)
1682{
1683 LogFlow(("MAIN::HGCMService::Call\n"));
1684
1685 int rc;
1686 HGCMMsgCall *pMsg = new (std::nothrow) HGCMMsgCall(m_pThread);
1687 if (pMsg)
1688 {
1689 pMsg->Reference();
1690
1691 pMsg->pCmd = pCmd;
1692 pMsg->pHGCMPort = pHGCMPort;
1693 pMsg->u32ClientId = u32ClientId;
1694 pMsg->u32Function = u32Function;
1695 pMsg->cParms = cParms;
1696 pMsg->paParms = paParms;
1697 pMsg->tsArrival = tsArrival;
1698
1699 rc = hgcmMsgPost(pMsg, hgcmMsgCompletionCallback);
1700 }
1701 else
1702 {
1703 Log(("MAIN::HGCMService::Call: Message allocation failed\n"));
1704 rc = VERR_NO_MEMORY;
1705 }
1706
1707 LogFlowFunc(("rc = %Rrc\n", rc));
1708 return rc;
1709}
1710
1711/** Perform a host call the service.
1712 *
1713 * @param u32Function The function number.
1714 * @param cParms Number of parameters.
1715 * @param paParms Pointer to array of parameters.
1716 * @return VBox rc.
1717 */
1718int HGCMService::HostCall(uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM *paParms)
1719{
1720 LogFlowFunc(("%s u32Function = %d, cParms = %d, paParms = %p\n",
1721 m_pszSvcName, u32Function, cParms, paParms));
1722
1723 HGCMMsgCore *pCoreMsg;
1724 int rc = hgcmMsgAlloc(m_pThread, &pCoreMsg, SVC_MSG_HOSTCALL, hgcmMessageAllocSvc);
1725
1726 if (RT_SUCCESS(rc))
1727 {
1728 HGCMMsgHostCallSvc *pMsg = (HGCMMsgHostCallSvc *)pCoreMsg;
1729
1730 pMsg->u32Function = u32Function;
1731 pMsg->cParms = cParms;
1732 pMsg->paParms = paParms;
1733
1734 rc = hgcmMsgSend(pMsg);
1735 }
1736
1737 LogFlowFunc(("rc = %Rrc\n", rc));
1738 return rc;
1739}
1740
1741#ifdef VBOX_WITH_CRHGSMI
1742
1743static DECLCALLBACK(int) hgcmMsgFastCallCompletionCallback(int32_t result, HGCMMsgCore *pMsgCore)
1744{
1745 /* Call the VMMDev port interface to issue IRQ notification. */
1746 LogFlow(("MAIN::hgcmMsgFastCallCompletionCallback: message %p\n", pMsgCore));
1747
1748 HGCMMsgHostFastCallAsyncSvc *pMsg = (HGCMMsgHostFastCallAsyncSvc *)pMsgCore;
1749 if (pMsg->pfnCompletion)
1750 pMsg->pfnCompletion(result, pMsg->u32Function, &pMsg->Param, pMsg->pvCompletion);
1751 return VINF_SUCCESS;
1752}
1753
1754int HGCMService::HandleAcquired()
1755{
1756 ++m_cHandleAcquires;
1757 return VINF_SUCCESS;
1758}
1759
1760int HGCMService::HandleReleased()
1761{
1762 Assert(m_cHandleAcquires);
1763 if (m_cHandleAcquires)
1764 {
1765 --m_cHandleAcquires;
1766 return VINF_SUCCESS;
1767 }
1768 return VERR_INVALID_STATE;
1769}
1770
1771int HGCMService::HostFastCallAsync(uint32_t u32Function, VBOXHGCMSVCPARM *pParm, PHGCMHOSTFASTCALLCB pfnCompletion,
1772 void *pvCompletion)
1773{
1774 LogFlowFunc(("%s u32Function = %d, pParm = %p\n",
1775 m_pszSvcName, u32Function, pParm));
1776
1777 HGCMMsgCore *pCoreMsg;
1778 int rc = hgcmMsgAlloc(m_pThread, &pCoreMsg, SVC_MSG_HOSTFASTCALLASYNC, hgcmMessageAllocSvc);
1779
1780 if (RT_SUCCESS(rc))
1781 {
1782 HGCMMsgHostFastCallAsyncSvc *pMsg = (HGCMMsgHostFastCallAsyncSvc *)pCoreMsg;
1783
1784 pMsg->u32Function = u32Function;
1785 pMsg->Param = *pParm;
1786 pMsg->pfnCompletion = pfnCompletion;
1787 pMsg->pvCompletion = pvCompletion;
1788
1789 rc = hgcmMsgPost(pMsg, hgcmMsgFastCallCompletionCallback);
1790 }
1791
1792 LogFlowFunc(("rc = %Rrc\n", rc));
1793 return rc;
1794}
1795
1796#endif /* VBOX_WITH_CRHGSMI */
1797
1798/*
1799 * Main HGCM thread that manages services.
1800 */
1801
1802/* Messages processed by the main HGCM thread. */
1803#define HGCM_MSG_CONNECT (10) /* Connect a client to a service. */
1804#define HGCM_MSG_DISCONNECT (11) /* Disconnect the specified client id. */
1805#define HGCM_MSG_LOAD (12) /* Load the service. */
1806#define HGCM_MSG_HOSTCALL (13) /* Call the service. */
1807#define HGCM_MSG_LOADSTATE (14) /* Load saved state for the specified service. */
1808#define HGCM_MSG_SAVESTATE (15) /* Save state for the specified service. */
1809#define HGCM_MSG_RESET (16) /* Disconnect all clients from the specified service. */
1810#define HGCM_MSG_QUIT (17) /* Unload all services and terminate the thread. */
1811#define HGCM_MSG_REGEXT (18) /* Register a service extension. */
1812#define HGCM_MSG_UNREGEXT (19) /* Unregister a service extension. */
1813#ifdef VBOX_WITH_CRHGSMI
1814# define HGCM_MSG_SVCAQUIRE (30) /* Acquire a service handle (for fast host calls) */
1815# define HGCM_MSG_SVCRELEASE (31) /* Release a service */
1816#endif
1817
1818class HGCMMsgMainConnect: public HGCMMsgHeader
1819{
1820 public:
1821 /* Service name. */
1822 const char *pszServiceName;
1823 /* Where to store the client handle. */
1824 uint32_t *pu32ClientId;
1825};
1826
1827class HGCMMsgMainDisconnect: public HGCMMsgHeader
1828{
1829 public:
1830 /* Handle of the client to be disconnected. */
1831 uint32_t u32ClientId;
1832};
1833
1834class HGCMMsgMainLoad: public HGCMMsgCore
1835{
1836 public:
1837 /* Name of the library to be loaded. */
1838 const char *pszServiceLibrary;
1839 /* Name to be assigned to the service. */
1840 const char *pszServiceName;
1841 /** The user mode VM handle (for statistics and such). */
1842 PUVM pUVM;
1843};
1844
1845class HGCMMsgMainHostCall: public HGCMMsgCore
1846{
1847 public:
1848 /* Which service to call. */
1849 const char *pszServiceName;
1850 /* Function number. */
1851 uint32_t u32Function;
1852 /* Number of the function parameters. */
1853 uint32_t cParms;
1854 /* Pointer to array of the function parameters. */
1855 VBOXHGCMSVCPARM *paParms;
1856};
1857
1858class HGCMMsgMainLoadSaveState: public HGCMMsgCore
1859{
1860 public:
1861 /* SSM context. */
1862 PSSMHANDLE pSSM;
1863};
1864
1865class HGCMMsgMainReset: public HGCMMsgCore
1866{
1867};
1868
1869class HGCMMsgMainQuit: public HGCMMsgCore
1870{
1871 public:
1872 /** Whether UVM has gone invalid already or not. */
1873 bool fUvmIsInvalid;
1874};
1875
1876class HGCMMsgMainRegisterExtension: public HGCMMsgCore
1877{
1878 public:
1879 /** Returned handle to be used in HGCMMsgMainUnregisterExtension. */
1880 HGCMSVCEXTHANDLE *pHandle;
1881 /** Name of the service. */
1882 const char *pszServiceName;
1883 /** The extension entry point. */
1884 PFNHGCMSVCEXT pfnExtension;
1885 /** The extension pointer. */
1886 void *pvExtension;
1887};
1888
1889class HGCMMsgMainUnregisterExtension: public HGCMMsgCore
1890{
1891 public:
1892 /* Handle of the registered extension. */
1893 HGCMSVCEXTHANDLE handle;
1894};
1895
1896#ifdef VBOX_WITH_CRHGSMI
1897class HGCMMsgMainSvcAcquire: public HGCMMsgCore
1898{
1899 public:
1900 /* Which service to call. */
1901 const char *pszServiceName;
1902 /* Returned service. */
1903 HGCMService *pService;
1904};
1905
1906class HGCMMsgMainSvcRelease: public HGCMMsgCore
1907{
1908 public:
1909 /* Svc . */
1910 HGCMService *pService;
1911};
1912#endif
1913
1914
1915static HGCMMsgCore *hgcmMainMessageAlloc (uint32_t u32MsgId)
1916{
1917 switch (u32MsgId)
1918 {
1919 case HGCM_MSG_CONNECT: return new HGCMMsgMainConnect();
1920 case HGCM_MSG_DISCONNECT: return new HGCMMsgMainDisconnect();
1921 case HGCM_MSG_LOAD: return new HGCMMsgMainLoad();
1922 case HGCM_MSG_HOSTCALL: return new HGCMMsgMainHostCall();
1923 case HGCM_MSG_LOADSTATE:
1924 case HGCM_MSG_SAVESTATE: return new HGCMMsgMainLoadSaveState();
1925 case HGCM_MSG_RESET: return new HGCMMsgMainReset();
1926 case HGCM_MSG_QUIT: return new HGCMMsgMainQuit();
1927 case HGCM_MSG_REGEXT: return new HGCMMsgMainRegisterExtension();
1928 case HGCM_MSG_UNREGEXT: return new HGCMMsgMainUnregisterExtension();
1929#ifdef VBOX_WITH_CRHGSMI
1930 case HGCM_MSG_SVCAQUIRE: return new HGCMMsgMainSvcAcquire();
1931 case HGCM_MSG_SVCRELEASE: return new HGCMMsgMainSvcRelease();
1932#endif
1933
1934 default:
1935 AssertReleaseMsgFailed(("Msg id = %08X\n", u32MsgId));
1936 }
1937
1938 return NULL;
1939}
1940
1941
1942/* The main HGCM thread handler. */
1943static DECLCALLBACK(void) hgcmThread(HGCMThread *pThread, void *pvUser)
1944{
1945 LogFlowFunc(("pThread = %p, pvUser = %p\n", pThread, pvUser));
1946
1947 NOREF(pvUser);
1948
1949 bool fQuit = false;
1950
1951 while (!fQuit)
1952 {
1953 HGCMMsgCore *pMsgCore;
1954 int rc = hgcmMsgGet(pThread, &pMsgCore);
1955
1956 if (RT_FAILURE(rc))
1957 {
1958 /* The error means some serious unrecoverable problem in the hgcmMsg/hgcmThread layer. */
1959 AssertMsgFailed(("%Rrc\n", rc));
1960 break;
1961 }
1962
1963 uint32_t u32MsgId = pMsgCore->MsgId();
1964
1965 switch (u32MsgId)
1966 {
1967 case HGCM_MSG_CONNECT:
1968 {
1969 HGCMMsgMainConnect *pMsg = (HGCMMsgMainConnect *)pMsgCore;
1970
1971 LogFlowFunc(("HGCM_MSG_CONNECT pszServiceName %s, pu32ClientId %p\n",
1972 pMsg->pszServiceName, pMsg->pu32ClientId));
1973
1974 /* Resolve the service name to the pointer to service instance.
1975 */
1976 HGCMService *pService;
1977 rc = HGCMService::ResolveService(&pService, pMsg->pszServiceName);
1978
1979 if (RT_SUCCESS(rc))
1980 {
1981 /* Call the service instance method. */
1982 rc = pService->CreateAndConnectClient(pMsg->pu32ClientId, 0);
1983
1984 /* Release the service after resolve. */
1985 pService->ReleaseService();
1986 }
1987 } break;
1988
1989 case HGCM_MSG_DISCONNECT:
1990 {
1991 HGCMMsgMainDisconnect *pMsg = (HGCMMsgMainDisconnect *)pMsgCore;
1992
1993 LogFlowFunc(("HGCM_MSG_DISCONNECT u32ClientId = %d\n",
1994 pMsg->u32ClientId));
1995
1996 HGCMClient *pClient = (HGCMClient *)hgcmObjReference(pMsg->u32ClientId, HGCMOBJ_CLIENT);
1997
1998 if (!pClient)
1999 {
2000 rc = VERR_HGCM_INVALID_CLIENT_ID;
2001 break;
2002 }
2003
2004 /* The service the client belongs to. */
2005 HGCMService *pService = pClient->pService;
2006
2007 /* Call the service instance to disconnect the client. */
2008 rc = pService->DisconnectClient(pMsg->u32ClientId, false);
2009
2010 hgcmObjDereference(pClient);
2011 } break;
2012
2013 case HGCM_MSG_LOAD:
2014 {
2015 HGCMMsgMainLoad *pMsg = (HGCMMsgMainLoad *)pMsgCore;
2016
2017 LogFlowFunc(("HGCM_MSG_LOAD pszServiceName = %s, pMsg->pszServiceLibrary = %s, pMsg->pUVM = %p\n",
2018 pMsg->pszServiceName, pMsg->pszServiceLibrary, pMsg->pUVM));
2019
2020 rc = HGCMService::LoadService(pMsg->pszServiceLibrary, pMsg->pszServiceName, pMsg->pUVM);
2021 } break;
2022
2023 case HGCM_MSG_HOSTCALL:
2024 {
2025 HGCMMsgMainHostCall *pMsg = (HGCMMsgMainHostCall *)pMsgCore;
2026
2027 LogFlowFunc(("HGCM_MSG_HOSTCALL pszServiceName %s, u32Function %d, cParms %d, paParms %p\n",
2028 pMsg->pszServiceName, pMsg->u32Function, pMsg->cParms, pMsg->paParms));
2029
2030 /* Resolve the service name to the pointer to service instance. */
2031 HGCMService *pService;
2032 rc = HGCMService::ResolveService(&pService, pMsg->pszServiceName);
2033
2034 if (RT_SUCCESS(rc))
2035 {
2036 rc = pService->HostCall(pMsg->u32Function, pMsg->cParms, pMsg->paParms);
2037
2038 pService->ReleaseService();
2039 }
2040 } break;
2041
2042#ifdef VBOX_WITH_CRHGSMI
2043 case HGCM_MSG_SVCAQUIRE:
2044 {
2045 HGCMMsgMainSvcAcquire *pMsg = (HGCMMsgMainSvcAcquire *)pMsgCore;
2046
2047 LogFlowFunc(("HGCM_MSG_SVCAQUIRE pszServiceName %s\n", pMsg->pszServiceName));
2048
2049 /* Resolve the service name to the pointer to service instance. */
2050 HGCMService *pService;
2051 rc = HGCMService::ResolveService(&pService, pMsg->pszServiceName);
2052 if (RT_SUCCESS(rc))
2053 {
2054 rc = pService->HandleAcquired();
2055 if (RT_SUCCESS(rc))
2056 {
2057 pMsg->pService = pService;
2058 }
2059 else
2060 {
2061 pService->ReleaseService();
2062 }
2063 }
2064 } break;
2065
2066 case HGCM_MSG_SVCRELEASE:
2067 {
2068 HGCMMsgMainSvcRelease *pMsg = (HGCMMsgMainSvcRelease *)pMsgCore;
2069
2070 LogFlowFunc(("HGCM_MSG_SVCARELEASE pService %p\n", pMsg->pService));
2071
2072 /* Resolve the service name to the pointer to service instance. */
2073
2074 rc = pMsg->pService->HandleReleased();
2075 if (RT_SUCCESS(rc))
2076 {
2077 pMsg->pService->ReleaseService();
2078 }
2079 } break;
2080#endif
2081
2082 case HGCM_MSG_RESET:
2083 {
2084 LogFlowFunc(("HGCM_MSG_RESET\n"));
2085
2086 HGCMService::Reset();
2087 } break;
2088
2089 case HGCM_MSG_LOADSTATE:
2090 {
2091 HGCMMsgMainLoadSaveState *pMsg = (HGCMMsgMainLoadSaveState *)pMsgCore;
2092
2093 LogFlowFunc(("HGCM_MSG_LOADSTATE\n"));
2094
2095 rc = HGCMService::LoadState(pMsg->pSSM);
2096 } break;
2097
2098 case HGCM_MSG_SAVESTATE:
2099 {
2100 HGCMMsgMainLoadSaveState *pMsg = (HGCMMsgMainLoadSaveState *)pMsgCore;
2101
2102 LogFlowFunc(("HGCM_MSG_SAVESTATE\n"));
2103
2104 rc = HGCMService::SaveState(pMsg->pSSM);
2105 } break;
2106
2107 case HGCM_MSG_QUIT:
2108 {
2109 HGCMMsgMainQuit *pMsg = (HGCMMsgMainQuit *)pMsgCore;
2110 LogFlowFunc(("HGCM_MSG_QUIT\n"));
2111
2112 HGCMService::UnloadAll(pMsg->fUvmIsInvalid);
2113
2114 fQuit = true;
2115 } break;
2116
2117 case HGCM_MSG_REGEXT:
2118 {
2119 HGCMMsgMainRegisterExtension *pMsg = (HGCMMsgMainRegisterExtension *)pMsgCore;
2120
2121 LogFlowFunc(("HGCM_MSG_REGEXT\n"));
2122
2123 /* Allocate the handle data. */
2124 HGCMSVCEXTHANDLE handle = (HGCMSVCEXTHANDLE)RTMemAllocZ(sizeof(struct _HGCMSVCEXTHANDLEDATA)
2125 + strlen(pMsg->pszServiceName)
2126 + sizeof(char));
2127
2128 if (handle == NULL)
2129 {
2130 rc = VERR_NO_MEMORY;
2131 }
2132 else
2133 {
2134 handle->pszServiceName = (char *)((uint8_t *)handle + sizeof(struct _HGCMSVCEXTHANDLEDATA));
2135 strcpy(handle->pszServiceName, pMsg->pszServiceName);
2136
2137 HGCMService *pService;
2138 rc = HGCMService::ResolveService(&pService, handle->pszServiceName);
2139
2140 if (RT_SUCCESS(rc))
2141 {
2142 pService->RegisterExtension(handle, pMsg->pfnExtension, pMsg->pvExtension);
2143
2144 pService->ReleaseService();
2145 }
2146
2147 if (RT_FAILURE(rc))
2148 {
2149 RTMemFree(handle);
2150 }
2151 else
2152 {
2153 *pMsg->pHandle = handle;
2154 }
2155 }
2156 } break;
2157
2158 case HGCM_MSG_UNREGEXT:
2159 {
2160 HGCMMsgMainUnregisterExtension *pMsg = (HGCMMsgMainUnregisterExtension *)pMsgCore;
2161
2162 LogFlowFunc(("HGCM_MSG_UNREGEXT\n"));
2163
2164 HGCMService *pService;
2165 rc = HGCMService::ResolveService(&pService, pMsg->handle->pszServiceName);
2166
2167 if (RT_SUCCESS(rc))
2168 {
2169 pService->UnregisterExtension(pMsg->handle);
2170
2171 pService->ReleaseService();
2172 }
2173
2174 RTMemFree(pMsg->handle);
2175 } break;
2176
2177 default:
2178 {
2179 AssertMsgFailed(("hgcmThread: Unsupported message number %08X!!!\n", u32MsgId));
2180 rc = VERR_NOT_SUPPORTED;
2181 } break;
2182 }
2183
2184 /* Complete the message processing. */
2185 hgcmMsgComplete(pMsgCore, rc);
2186
2187 LogFlowFunc(("message processed %Rrc\n", rc));
2188 }
2189}
2190
2191
2192/*
2193 * The HGCM API.
2194 */
2195
2196/** The main hgcm thread. */
2197static HGCMThread *g_pHgcmThread = 0;
2198
2199/*
2200 * Public HGCM functions.
2201 *
2202 * hgcmGuest* - called as a result of the guest HGCM requests.
2203 * hgcmHost* - called by the host.
2204 */
2205
2206/* Load a HGCM service from the specified library.
2207 * Assign the specified name to the service.
2208 *
2209 * @param pszServiceLibrary The library to be loaded.
2210 * @param pszServiceName The name to be assigned to the service.
2211 * @param pUVM The user mode VM handle (for statistics and such).
2212 * @return VBox rc.
2213 */
2214int HGCMHostLoad(const char *pszServiceLibrary,
2215 const char *pszServiceName,
2216 PUVM pUVM)
2217{
2218 LogFlowFunc(("lib = %s, name = %s\n", pszServiceLibrary, pszServiceName));
2219
2220 if (!pszServiceLibrary || !pszServiceName)
2221 {
2222 return VERR_INVALID_PARAMETER;
2223 }
2224
2225 /* Forward the request to the main hgcm thread. */
2226 HGCMMsgCore *pCoreMsg;
2227 int rc = hgcmMsgAlloc(g_pHgcmThread, &pCoreMsg, HGCM_MSG_LOAD, hgcmMainMessageAlloc);
2228
2229 if (RT_SUCCESS(rc))
2230 {
2231 /* Initialize the message. Since the message is synchronous, use the supplied pointers. */
2232 HGCMMsgMainLoad *pMsg = (HGCMMsgMainLoad *)pCoreMsg;
2233
2234 pMsg->pszServiceLibrary = pszServiceLibrary;
2235 pMsg->pszServiceName = pszServiceName;
2236 pMsg->pUVM = pUVM;
2237
2238 rc = hgcmMsgSend(pMsg);
2239 }
2240
2241 LogFlowFunc(("rc = %Rrc\n", rc));
2242 return rc;
2243}
2244
2245/* Register a HGCM service extension.
2246 *
2247 * @param pHandle Returned handle for the registered extension.
2248 * @param pszServiceName The name of the service.
2249 * @param pfnExtension The extension entry point (callback).
2250 * @param pvExtension The extension pointer.
2251 * @return VBox rc.
2252 */
2253int HGCMHostRegisterServiceExtension(HGCMSVCEXTHANDLE *pHandle,
2254 const char *pszServiceName,
2255 PFNHGCMSVCEXT pfnExtension,
2256 void *pvExtension)
2257{
2258 LogFlowFunc(("pHandle = %p, name = %s, pfn = %p, rv = %p\n", pHandle, pszServiceName, pfnExtension, pvExtension));
2259
2260 if (!pHandle || !pszServiceName || !pfnExtension)
2261 {
2262 return VERR_INVALID_PARAMETER;
2263 }
2264
2265 /* Forward the request to the main hgcm thread. */
2266 HGCMMsgCore *pCoreMsg;
2267 int rc = hgcmMsgAlloc(g_pHgcmThread, &pCoreMsg, HGCM_MSG_REGEXT, hgcmMainMessageAlloc);
2268
2269 if (RT_SUCCESS(rc))
2270 {
2271 /* Initialize the message. Since the message is synchronous, use the supplied pointers. */
2272 HGCMMsgMainRegisterExtension *pMsg = (HGCMMsgMainRegisterExtension *)pCoreMsg;
2273
2274 pMsg->pHandle = pHandle;
2275 pMsg->pszServiceName = pszServiceName;
2276 pMsg->pfnExtension = pfnExtension;
2277 pMsg->pvExtension = pvExtension;
2278
2279 rc = hgcmMsgSend(pMsg);
2280 }
2281
2282 LogFlowFunc(("*pHandle = %p, rc = %Rrc\n", *pHandle, rc));
2283 return rc;
2284}
2285
2286void HGCMHostUnregisterServiceExtension(HGCMSVCEXTHANDLE handle)
2287{
2288 LogFlowFunc(("handle = %p\n", handle));
2289
2290 /* Forward the request to the main hgcm thread. */
2291 HGCMMsgCore *pCoreMsg;
2292 int rc = hgcmMsgAlloc(g_pHgcmThread, &pCoreMsg, HGCM_MSG_UNREGEXT, hgcmMainMessageAlloc);
2293
2294 if (RT_SUCCESS(rc))
2295 {
2296 /* Initialize the message. */
2297 HGCMMsgMainUnregisterExtension *pMsg = (HGCMMsgMainUnregisterExtension *)pCoreMsg;
2298
2299 pMsg->handle = handle;
2300
2301 rc = hgcmMsgSend(pMsg);
2302 }
2303
2304 LogFlowFunc(("rc = %Rrc\n", rc));
2305 return;
2306}
2307
2308/* Find a service and inform it about a client connection, create a client handle.
2309 *
2310 * @param pHGCMPort The port to be used for completion confirmation.
2311 * @param pCmd The VBox HGCM context.
2312 * @param pszServiceName The name of the service to be connected to.
2313 * @param pu32ClientId Where the store the created client handle.
2314 * @return VBox rc.
2315 */
2316int HGCMGuestConnect(PPDMIHGCMPORT pHGCMPort,
2317 PVBOXHGCMCMD pCmd,
2318 const char *pszServiceName,
2319 uint32_t *pu32ClientId)
2320{
2321 LogFlowFunc(("pHGCMPort = %p, pCmd = %p, name = %s, pu32ClientId = %p\n",
2322 pHGCMPort, pCmd, pszServiceName, pu32ClientId));
2323
2324 if (pHGCMPort == NULL || pCmd == NULL || pszServiceName == NULL || pu32ClientId == NULL)
2325 {
2326 return VERR_INVALID_PARAMETER;
2327 }
2328
2329 /* Forward the request to the main hgcm thread. */
2330 HGCMMsgCore *pCoreMsg;
2331 int rc = hgcmMsgAlloc(g_pHgcmThread, &pCoreMsg, HGCM_MSG_CONNECT, hgcmMainMessageAlloc);
2332
2333 if (RT_SUCCESS(rc))
2334 {
2335 /* Initialize the message. Since 'pszServiceName' and 'pu32ClientId'
2336 * will not be deallocated by the caller until the message is completed,
2337 * use the supplied pointers.
2338 */
2339 HGCMMsgMainConnect *pMsg = (HGCMMsgMainConnect *)pCoreMsg;
2340
2341 pMsg->pHGCMPort = pHGCMPort;
2342 pMsg->pCmd = pCmd;
2343 pMsg->pszServiceName = pszServiceName;
2344 pMsg->pu32ClientId = pu32ClientId;
2345
2346 rc = hgcmMsgPost(pMsg, hgcmMsgCompletionCallback);
2347 }
2348
2349 LogFlowFunc(("rc = %Rrc\n", rc));
2350 return rc;
2351}
2352
2353/* Tell a service that the client is disconnecting, destroy the client handle.
2354 *
2355 * @param pHGCMPort The port to be used for completion confirmation.
2356 * @param pCmd The VBox HGCM context.
2357 * @param u32ClientId The client handle to be disconnected and deleted.
2358 * @return VBox rc.
2359 */
2360int HGCMGuestDisconnect(PPDMIHGCMPORT pHGCMPort,
2361 PVBOXHGCMCMD pCmd,
2362 uint32_t u32ClientId)
2363{
2364 LogFlowFunc(("pHGCMPort = %p, pCmd = %p, u32ClientId = %d\n",
2365 pHGCMPort, pCmd, u32ClientId));
2366
2367 if (!pHGCMPort || !pCmd || !u32ClientId)
2368 {
2369 return VERR_INVALID_PARAMETER;
2370 }
2371
2372 /* Forward the request to the main hgcm thread. */
2373 HGCMMsgCore *pCoreMsg;
2374 int rc = hgcmMsgAlloc(g_pHgcmThread, &pCoreMsg, HGCM_MSG_DISCONNECT, hgcmMainMessageAlloc);
2375
2376 if (RT_SUCCESS(rc))
2377 {
2378 /* Initialize the message. */
2379 HGCMMsgMainDisconnect *pMsg = (HGCMMsgMainDisconnect *)pCoreMsg;
2380
2381 pMsg->pCmd = pCmd;
2382 pMsg->pHGCMPort = pHGCMPort;
2383 pMsg->u32ClientId = u32ClientId;
2384
2385 rc = hgcmMsgPost(pMsg, hgcmMsgCompletionCallback);
2386 }
2387
2388 LogFlowFunc(("rc = %Rrc\n", rc));
2389 return rc;
2390}
2391
2392/* Helper to send either HGCM_MSG_SAVESTATE or HGCM_MSG_LOADSTATE messages to the main HGCM thread.
2393 *
2394 * @param pSSM The SSM handle.
2395 * @param u32MsgId The message to be sent: HGCM_MSG_SAVESTATE or HGCM_MSG_LOADSTATE.
2396 * @return VBox rc.
2397 */
2398static int hgcmHostLoadSaveState(PSSMHANDLE pSSM,
2399 uint32_t u32MsgId)
2400{
2401 LogFlowFunc(("pSSM = %p, u32MsgId = %d\n", pSSM, u32MsgId));
2402
2403 HGCMMsgCore *pCoreMsg;
2404 int rc = hgcmMsgAlloc(g_pHgcmThread, &pCoreMsg, u32MsgId, hgcmMainMessageAlloc);
2405
2406 if (RT_SUCCESS(rc))
2407 {
2408 HGCMMsgMainLoadSaveState *pMsg = (HGCMMsgMainLoadSaveState *)pCoreMsg;
2409 AssertRelease(pMsg);
2410
2411 pMsg->pSSM = pSSM;
2412
2413 rc = hgcmMsgSend(pMsg);
2414 }
2415
2416 LogFlowFunc(("rc = %Rrc\n", rc));
2417 return rc;
2418}
2419
2420/* Save the state of services.
2421 *
2422 * @param pSSM The SSM handle.
2423 * @return VBox rc.
2424 */
2425int HGCMHostSaveState(PSSMHANDLE pSSM)
2426{
2427 return hgcmHostLoadSaveState(pSSM, HGCM_MSG_SAVESTATE);
2428}
2429
2430/* Load the state of services.
2431 *
2432 * @param pSSM The SSM handle.
2433 * @return VBox rc.
2434 */
2435int HGCMHostLoadState(PSSMHANDLE pSSM)
2436{
2437 return hgcmHostLoadSaveState(pSSM, HGCM_MSG_LOADSTATE);
2438}
2439
2440/* The guest calls the service.
2441 *
2442 * @param pHGCMPort The port to be used for completion confirmation.
2443 * @param pCmd The VBox HGCM context.
2444 * @param u32ClientId The client handle to be disconnected and deleted.
2445 * @param u32Function The function number.
2446 * @param cParms Number of parameters.
2447 * @param paParms Pointer to array of parameters.
2448 * @param tsArrival The STAM_GET_TS() value when the request arrived.
2449 * @return VBox rc.
2450 */
2451int HGCMGuestCall(PPDMIHGCMPORT pHGCMPort,
2452 PVBOXHGCMCMD pCmd,
2453 uint32_t u32ClientId,
2454 uint32_t u32Function,
2455 uint32_t cParms,
2456 VBOXHGCMSVCPARM *paParms,
2457 uint64_t tsArrival)
2458{
2459 LogFlowFunc(("pHGCMPort = %p, pCmd = %p, u32ClientId = %d, u32Function = %d, cParms = %d, paParms = %p\n",
2460 pHGCMPort, pCmd, u32ClientId, u32Function, cParms, paParms));
2461
2462 if (!pHGCMPort || !pCmd || u32ClientId == 0)
2463 {
2464 return VERR_INVALID_PARAMETER;
2465 }
2466
2467 int rc = VERR_HGCM_INVALID_CLIENT_ID;
2468
2469 /* Resolve the client handle to the client instance pointer. */
2470 HGCMClient *pClient = (HGCMClient *)hgcmObjReference(u32ClientId, HGCMOBJ_CLIENT);
2471
2472 if (pClient)
2473 {
2474 AssertRelease(pClient->pService);
2475
2476 /* Forward the message to the service thread. */
2477 rc = pClient->pService->GuestCall(pHGCMPort, pCmd, u32ClientId, u32Function, cParms, paParms, tsArrival);
2478
2479 hgcmObjDereference(pClient);
2480 }
2481
2482 LogFlowFunc(("rc = %Rrc\n", rc));
2483 return rc;
2484}
2485
2486/* The host calls the service.
2487 *
2488 * @param pszServiceName The service name to be called.
2489 * @param u32Function The function number.
2490 * @param cParms Number of parameters.
2491 * @param paParms Pointer to array of parameters.
2492 * @return VBox rc.
2493 */
2494int HGCMHostCall(const char *pszServiceName,
2495 uint32_t u32Function,
2496 uint32_t cParms,
2497 VBOXHGCMSVCPARM *paParms)
2498{
2499 LogFlowFunc(("name = %s, u32Function = %d, cParms = %d, paParms = %p\n",
2500 pszServiceName, u32Function, cParms, paParms));
2501
2502 if (!pszServiceName)
2503 {
2504 return VERR_INVALID_PARAMETER;
2505 }
2506
2507 /* Host calls go to main HGCM thread that resolves the service name to the
2508 * service instance pointer and then, using the service pointer, forwards
2509 * the message to the service thread.
2510 * So it is slow but host calls are intended mostly for configuration and
2511 * other non-time-critical functions.
2512 */
2513 HGCMMsgCore *pCoreMsg;
2514 int rc = hgcmMsgAlloc(g_pHgcmThread, &pCoreMsg, HGCM_MSG_HOSTCALL, hgcmMainMessageAlloc);
2515
2516 if (RT_SUCCESS(rc))
2517 {
2518 HGCMMsgMainHostCall *pMsg = (HGCMMsgMainHostCall *)pCoreMsg;
2519
2520 pMsg->pszServiceName = (char *)pszServiceName;
2521 pMsg->u32Function = u32Function;
2522 pMsg->cParms = cParms;
2523 pMsg->paParms = paParms;
2524
2525 rc = hgcmMsgSend(pMsg);
2526 }
2527
2528 LogFlowFunc(("rc = %Rrc\n", rc));
2529 return rc;
2530}
2531
2532#ifdef VBOX_WITH_CRHGSMI
2533int HGCMHostSvcHandleCreate(const char *pszServiceName, HGCMCVSHANDLE * phSvc)
2534{
2535 LogFlowFunc(("name = %s\n", pszServiceName));
2536
2537 if (!pszServiceName)
2538 {
2539 return VERR_INVALID_PARAMETER;
2540 }
2541
2542 if (!phSvc)
2543 {
2544 return VERR_INVALID_PARAMETER;
2545 }
2546
2547 /* Host calls go to main HGCM thread that resolves the service name to the
2548 * service instance pointer and then, using the service pointer, forwards
2549 * the message to the service thread.
2550 * So it is slow but host calls are intended mostly for configuration and
2551 * other non-time-critical functions.
2552 */
2553 HGCMMsgCore *pCoreMsg;
2554 int rc = hgcmMsgAlloc(g_pHgcmThread, &pCoreMsg, HGCM_MSG_SVCAQUIRE, hgcmMainMessageAlloc);
2555
2556 if (RT_SUCCESS(rc))
2557 {
2558 HGCMMsgMainSvcAcquire *pMsg = (HGCMMsgMainSvcAcquire *)pCoreMsg;
2559
2560 pMsg->pszServiceName = (char *)pszServiceName;
2561 pMsg->pService = NULL;
2562
2563 pMsg->Reference();
2564
2565 rc = hgcmMsgSend(pMsg);
2566 if (RT_SUCCESS(rc))
2567 {
2568 /* for simplicity just use a svc ptr as handle for now */
2569 *phSvc = (HGCMCVSHANDLE)pMsg->pService;
2570 }
2571 pMsg->Dereference();
2572 }
2573
2574 LogFlowFunc(("rc = %Rrc\n", rc));
2575 return rc;
2576}
2577
2578int HGCMHostSvcHandleDestroy(HGCMCVSHANDLE hSvc)
2579{
2580 LogFlowFunc(("hSvc = %p\n", hSvc));
2581
2582 if (!hSvc)
2583 {
2584 return VERR_INVALID_PARAMETER;
2585 }
2586
2587 /* Host calls go to main HGCM thread that resolves the service name to the
2588 * service instance pointer and then, using the service pointer, forwards
2589 * the message to the service thread.
2590 * So it is slow but host calls are intended mostly for configuration and
2591 * other non-time-critical functions.
2592 */
2593 HGCMMsgCore *pCoreMsg;
2594 int rc = hgcmMsgAlloc(g_pHgcmThread, &pCoreMsg, HGCM_MSG_SVCRELEASE, hgcmMainMessageAlloc);
2595
2596 if (RT_SUCCESS(rc))
2597 {
2598 HGCMMsgMainSvcRelease *pMsg = (HGCMMsgMainSvcRelease *)pCoreMsg;
2599
2600 pMsg->pService = (HGCMService *)hSvc;
2601
2602 rc = hgcmMsgSend(pMsg);
2603 }
2604
2605 LogFlowFunc(("rc = %Rrc\n", rc));
2606 return rc;
2607}
2608
2609int HGCMHostFastCallAsync(HGCMCVSHANDLE hSvc, uint32_t function, VBOXHGCMSVCPARM *pParm, PHGCMHOSTFASTCALLCB pfnCompletion,
2610 void *pvCompletion)
2611{
2612 LogFlowFunc(("hSvc = %p, u32Function = %d, pParm = %p\n",
2613 hSvc, function, pParm));
2614
2615 if (!hSvc)
2616 {
2617 return VERR_INVALID_PARAMETER;
2618 }
2619
2620 HGCMService *pService = (HGCMService *)hSvc;
2621 int rc = pService->HostFastCallAsync(function, pParm, pfnCompletion, pvCompletion);
2622
2623 LogFlowFunc(("rc = %Rrc\n", rc));
2624 return rc;
2625}
2626#endif
2627
2628int HGCMHostReset(void)
2629{
2630 LogFlowFunc(("\n"));
2631
2632 /* Disconnect all clients.
2633 */
2634
2635 HGCMMsgCore *pMsg;
2636 int rc = hgcmMsgAlloc(g_pHgcmThread, &pMsg, HGCM_MSG_RESET, hgcmMainMessageAlloc);
2637
2638 if (RT_SUCCESS(rc))
2639 rc = hgcmMsgSend(pMsg);
2640
2641 LogFlowFunc(("rc = %Rrc\n", rc));
2642 return rc;
2643}
2644
2645int HGCMHostInit(void)
2646{
2647 LogFlowFunc(("\n"));
2648
2649 int rc = hgcmThreadInit();
2650
2651 if (RT_SUCCESS(rc))
2652 {
2653 /*
2654 * Start main HGCM thread.
2655 */
2656
2657 rc = hgcmThreadCreate(&g_pHgcmThread, "MainHGCMthread", hgcmThread, NULL /*pvUser*/, NULL /*pszStatsSubDir*/, NULL /*pUVM*/);
2658
2659 if (RT_FAILURE(rc))
2660 LogRel(("Failed to start HGCM thread. HGCM services will be unavailable!!! rc = %Rrc\n", rc));
2661 }
2662
2663 LogFlowFunc(("rc = %Rrc\n", rc));
2664 return rc;
2665}
2666
2667int HGCMHostShutdown(bool fUvmIsInvalid /*= false*/)
2668{
2669 LogFlowFunc(("\n"));
2670
2671 /*
2672 * Do HGCMReset and then unload all services.
2673 */
2674
2675 int rc = HGCMHostReset();
2676
2677 if (RT_SUCCESS(rc))
2678 {
2679 /* Send the quit message to the main hgcmThread. */
2680 HGCMMsgCore *pMsgCore;
2681 rc = hgcmMsgAlloc(g_pHgcmThread, &pMsgCore, HGCM_MSG_QUIT, hgcmMainMessageAlloc);
2682
2683 if (RT_SUCCESS(rc))
2684 {
2685 HGCMMsgMainQuit *pMsg = (HGCMMsgMainQuit *)pMsgCore;
2686 pMsg->fUvmIsInvalid = fUvmIsInvalid;
2687
2688 rc = hgcmMsgSend(pMsg);
2689
2690 if (RT_SUCCESS(rc))
2691 {
2692 /* Wait for the thread termination. */
2693 hgcmThreadWait(g_pHgcmThread);
2694 g_pHgcmThread = NULL;
2695
2696 hgcmThreadUninit();
2697 }
2698 }
2699 }
2700
2701 LogFlowFunc(("rc = %Rrc\n", rc));
2702 return rc;
2703}
2704
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette