VirtualBox

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

Last change on this file since 73529 was 72064, checked in by vboxsync, 7 years ago

HGCM: LogRel nit.

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