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