VirtualBox

source: vbox/trunk/include/VBox/hgcmsvc.h@ 75495

最後變更 在這個檔案從75495是 75495,由 vboxsync 提交於 6 年 前

HGCM,Main,SharedFolder,SharedClipboard,GuestProperties: Added HGCM service helpers for statistics and dbg info registration/deregistration. A PUVM is passed to HGCMService (where the helpers are implemented) when the service is loaded. Since this drags in both dbg.h and stam.h, LOG_GROUP defines now have to be at the top of the include list as everywhere else (i.e. hgcmsvc.h will define LOG_GROUP default by dragging in log.h). Added generic statistics of HGCM message processing and function level statistics to the shared folder service.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 17.7 KB
 
1/** @file
2 * Host-Guest Communication Manager (HGCM) - Service library definitions.
3 */
4
5/*
6 * Copyright (C) 2006-2017 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.alldomusa.eu.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___VBox_hgcm_h
27#define ___VBox_hgcm_h
28
29#include <iprt/assert.h>
30#include <iprt/stdarg.h>
31#include <iprt/string.h>
32#include <VBox/cdefs.h>
33#include <VBox/types.h>
34#include <VBox/err.h>
35#include <VBox/vmm/stam.h>
36#include <VBox/vmm/dbgf.h>
37#ifdef VBOX_TEST_HGCM_PARMS
38# include <iprt/test.h>
39#endif
40
41/** @todo proper comments. */
42
43/**
44 * Service interface version.
45 *
46 * Includes layout of both VBOXHGCMSVCFNTABLE and VBOXHGCMSVCHELPERS.
47 *
48 * A service can work with these structures if major version
49 * is equal and minor version of service is <= version of the
50 * structures.
51 *
52 * For example when a new helper is added at the end of helpers
53 * structure, then the minor version will be increased. All older
54 * services still can work because they have their old helpers
55 * unchanged.
56 *
57 * Revision history.
58 * 1.1->2.1 Because the pfnConnect now also has the pvClient parameter.
59 * 2.1->2.2 Because pfnSaveState and pfnLoadState were added
60 * 2.2->3.1 Because pfnHostCall is now synchronous, returns rc, and parameters were changed
61 * 3.1->3.2 Because pfnRegisterExtension was added
62 * 3.2->3.3 Because pfnDisconnectClient helper was added
63 * 3.3->4.1 Because the pvService entry and parameter was added
64 * 4.1->4.2 Because the VBOX_HGCM_SVC_PARM_CALLBACK parameter type was added
65 * 4.2->5.1 Removed the VBOX_HGCM_SVC_PARM_CALLBACK parameter type, as
66 * this problem is already solved by service extension callbacks
67 */
68#define VBOX_HGCM_SVC_VERSION_MAJOR (0x0005)
69#define VBOX_HGCM_SVC_VERSION_MINOR (0x0001)
70#define VBOX_HGCM_SVC_VERSION ((VBOX_HGCM_SVC_VERSION_MAJOR << 16) + VBOX_HGCM_SVC_VERSION_MINOR)
71
72
73/** Typed pointer to distinguish a call to service. */
74struct VBOXHGCMCALLHANDLE_TYPEDEF;
75typedef struct VBOXHGCMCALLHANDLE_TYPEDEF *VBOXHGCMCALLHANDLE;
76
77/** Service helpers pointers table. */
78typedef struct VBOXHGCMSVCHELPERS
79{
80 /** The service has processed the Call request. */
81 DECLR3CALLBACKMEMBER(void, pfnCallComplete, (VBOXHGCMCALLHANDLE callHandle, int32_t rc));
82
83 void *pvInstance;
84
85 /** The service disconnects the client. */
86 DECLR3CALLBACKMEMBER(void, pfnDisconnectClient, (void *pvInstance, uint32_t u32ClientID));
87
88 /**
89 * Check if the @a callHandle is for a call restored and re-submitted from saved state.
90 *
91 * @returns true if restored, false if not.
92 * @param callHandle The call we're checking up on.
93 */
94 DECLR3CALLBACKMEMBER(bool, pfnIsCallRestored, (VBOXHGCMCALLHANDLE callHandle));
95
96 /** Access to STAMR3RegisterV. */
97 DECLR3CALLBACKMEMBER(int, pfnStamRegisterV,(void *pvInstance, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility,
98 STAMUNIT enmUnit, const char *pszDesc, const char *pszName, va_list va)
99 RT_IPRT_FORMAT_ATTR(7, 0));
100 /** Access to STAMR3DeregisterV. */
101 DECLR3CALLBACKMEMBER(int, pfnStamDeregisterV,(void *pvInstance, const char *pszPatFmt, va_list va) RT_IPRT_FORMAT_ATTR(2, 0));
102
103 /** Access to DBGFR3InfoRegisterExternal. */
104 DECLR3CALLBACKMEMBER(int, pfnInfoRegister,(void *pvInstance, const char *pszName, const char *pszDesc,
105 PFNDBGFHANDLEREXT pfnHandler, void *pvUser));
106 /** Access to DBGFR3InfoDeregisterExternal. */
107 DECLR3CALLBACKMEMBER(int, pfnInfoDeregister,(void *pvInstance, const char *pszName));
108
109} VBOXHGCMSVCHELPERS;
110
111typedef VBOXHGCMSVCHELPERS *PVBOXHGCMSVCHELPERS;
112
113#if defined(IN_RING3) || defined(IN_SLICKEDIT)
114
115/** Wrapper around STAMR3RegisterF. */
116DECLINLINE(int) RT_IPRT_FORMAT_ATTR(7, 8)
117HGCMSvcHlpStamRegister(PVBOXHGCMSVCHELPERS pHlp, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility,
118 STAMUNIT enmUnit, const char *pszDesc, const char *pszName, ...)
119{
120 int rc;
121 va_list va;
122 va_start(va, pszName);
123 rc = pHlp->pfnStamRegisterV(pHlp->pvInstance, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName, va);
124 va_end(va);
125 return rc;
126}
127
128/** Wrapper around STAMR3RegisterV. */
129DECLINLINE(int) RT_IPRT_FORMAT_ATTR(7, 0)
130HGCMSvcHlpStamRegisterV(PVBOXHGCMSVCHELPERS pHlp, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility,
131 STAMUNIT enmUnit, const char *pszDesc, const char *pszName, va_list va)
132{
133 return pHlp->pfnStamRegisterV(pHlp->pvInstance, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName, va);
134}
135
136/** Wrapper around STAMR3DeregisterF. */
137DECLINLINE(int) RT_IPRT_FORMAT_ATTR(2, 3) HGCMSvcHlpStamDeregister(PVBOXHGCMSVCHELPERS pHlp, const char *pszPatFmt, ...)
138{
139 int rc;
140 va_list va;
141 va_start(va, pszPatFmt);
142 rc = pHlp->pfnStamDeregisterV(pHlp->pvInstance, pszPatFmt, va);
143 va_end(va);
144 return rc;
145}
146
147/** Wrapper around STAMR3DeregisterV. */
148DECLINLINE(int) RT_IPRT_FORMAT_ATTR(2, 0) HGCMSvcHlpStamDeregisterV(PVBOXHGCMSVCHELPERS pHlp, const char *pszPatFmt, va_list va)
149{
150 return pHlp->pfnStamDeregisterV(pHlp->pvInstance, pszPatFmt, va);
151}
152
153/** Wrapper around DBGFR3InfoRegisterExternal. */
154DECLINLINE(int) HGCMSvcHlpInfoRegister(PVBOXHGCMSVCHELPERS pHlp, const char *pszName, const char *pszDesc,
155 PFNDBGFHANDLEREXT pfnHandler, void *pvUser)
156{
157 return pHlp->pfnInfoRegister(pHlp->pvInstance, pszName, pszDesc, pfnHandler, pvUser);
158}
159
160/** Wrapper around DBGFR3InfoDeregisterExternal. */
161DECLINLINE(int) HGCMSvcHlpInfoDeregister(PVBOXHGCMSVCHELPERS pHlp, const char *pszName)
162{
163 return pHlp->pfnInfoDeregister(pHlp->pvInstance, pszName);
164}
165
166#endif /* IN_RING3 */
167
168
169#define VBOX_HGCM_SVC_PARM_INVALID (0U)
170#define VBOX_HGCM_SVC_PARM_32BIT (1U)
171#define VBOX_HGCM_SVC_PARM_64BIT (2U)
172#define VBOX_HGCM_SVC_PARM_PTR (3U)
173
174typedef struct VBOXHGCMSVCPARM
175{
176 /** VBOX_HGCM_SVC_PARM_* values. */
177 uint32_t type;
178
179 union
180 {
181 uint32_t uint32;
182 uint64_t uint64;
183 struct
184 {
185 uint32_t size;
186 void *addr;
187 } pointer;
188 } u;
189#ifdef __cplusplus
190 /** Extract an uint32_t value from an HGCM parameter structure */
191 int getUInt32(uint32_t *u32)
192 {
193 AssertPtrReturn(u32, VERR_INVALID_POINTER);
194 int rc = VINF_SUCCESS;
195 if (type != VBOX_HGCM_SVC_PARM_32BIT)
196 rc = VERR_INVALID_PARAMETER;
197 if (RT_SUCCESS(rc))
198 *u32 = u.uint32;
199 return rc;
200 }
201
202 /** Extract a uint64_t value from an HGCM parameter structure */
203 int getUInt64(uint64_t *u64)
204 {
205 AssertPtrReturn(u64, VERR_INVALID_POINTER);
206 int rc = VINF_SUCCESS;
207 if (type != VBOX_HGCM_SVC_PARM_64BIT)
208 rc = VERR_INVALID_PARAMETER;
209 if (RT_SUCCESS(rc))
210 *u64 = u.uint64;
211 return rc;
212 }
213
214 /** Extract a pointer value from an HGCM parameter structure */
215 int getPointer(void **ppv, uint32_t *pcb)
216 {
217 AssertPtrReturn(ppv, VERR_INVALID_POINTER);
218 AssertPtrReturn(pcb, VERR_INVALID_POINTER);
219 if (type == VBOX_HGCM_SVC_PARM_PTR)
220 {
221 *ppv = u.pointer.addr;
222 *pcb = u.pointer.size;
223 return VINF_SUCCESS;
224 }
225
226 return VERR_INVALID_PARAMETER;
227 }
228
229 /** Extract a constant pointer value from an HGCM parameter structure */
230 int getPointer(const void **ppcv, uint32_t *pcb)
231 {
232 AssertPtrReturn(ppcv, VERR_INVALID_POINTER);
233 AssertPtrReturn(pcb, VERR_INVALID_POINTER);
234 void *pv;
235 int rc = getPointer(&pv, pcb);
236 *ppcv = pv;
237 return rc;
238 }
239
240 /** Extract a pointer value to a non-empty buffer from an HGCM parameter
241 * structure */
242 int getBuffer(void **ppv, uint32_t *pcb)
243 {
244 AssertPtrReturn(ppv, VERR_INVALID_POINTER);
245 AssertPtrReturn(pcb, VERR_INVALID_POINTER);
246 void *pv = NULL;
247 uint32_t cb = 0;
248 int rc = getPointer(&pv, &cb);
249 if ( RT_SUCCESS(rc)
250 && VALID_PTR(pv)
251 && cb > 0)
252 {
253 *ppv = pv;
254 *pcb = cb;
255 return VINF_SUCCESS;
256 }
257
258 return VERR_INVALID_PARAMETER;
259 }
260
261 /** Extract a pointer value to a non-empty constant buffer from an HGCM
262 * parameter structure */
263 int getBuffer(const void **ppcv, uint32_t *pcb)
264 {
265 AssertPtrReturn(ppcv, VERR_INVALID_POINTER);
266 AssertPtrReturn(pcb, VERR_INVALID_POINTER);
267 void *pcv = NULL;
268 int rc = getBuffer(&pcv, pcb);
269 *ppcv = pcv;
270 return rc;
271 }
272
273 /** Extract a string value from an HGCM parameter structure */
274 int getString(char **ppch, uint32_t *pcb)
275 {
276 uint32_t cb = 0;
277 char *pch = NULL;
278 int rc = getBuffer((void **)&pch, &cb);
279 if (RT_FAILURE(rc))
280 {
281 *ppch = NULL;
282 *pcb = 0;
283 return rc;
284 }
285 rc = RTStrValidateEncodingEx(pch, cb,
286 RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED);
287 *ppch = pch;
288 *pcb = cb;
289 return rc;
290 }
291
292 /** Extract a constant string value from an HGCM parameter structure */
293 int getString(const char **ppch, uint32_t *pcb)
294 {
295 char *pch = NULL;
296 int rc = getString(&pch, pcb);
297 *ppch = pch;
298 return rc;
299 }
300
301 /** Set a uint32_t value to an HGCM parameter structure */
302 void setUInt32(uint32_t u32)
303 {
304 type = VBOX_HGCM_SVC_PARM_32BIT;
305 u.uint32 = u32;
306 }
307
308 /** Set a uint64_t value to an HGCM parameter structure */
309 void setUInt64(uint64_t u64)
310 {
311 type = VBOX_HGCM_SVC_PARM_64BIT;
312 u.uint64 = u64;
313 }
314
315 /** Set a pointer value to an HGCM parameter structure */
316 void setPointer(void *pv, uint32_t cb)
317 {
318 type = VBOX_HGCM_SVC_PARM_PTR;
319 u.pointer.addr = pv;
320 u.pointer.size = cb;
321 }
322
323 /** Set a const string value to an HGCM parameter structure */
324 void setString(const char *psz)
325 {
326 type = VBOX_HGCM_SVC_PARM_PTR;
327 u.pointer.addr = (void *)psz;
328 u.pointer.size = (uint32_t)strlen(psz) + 1;
329 }
330
331#ifdef ___iprt_cpp_ministring_h
332 /** Set a const string value to an HGCM parameter structure */
333 void setCppString(const RTCString &rString)
334 {
335 type = VBOX_HGCM_SVC_PARM_PTR;
336 u.pointer.addr = (void *)rString.c_str();
337 u.pointer.size = (uint32_t)rString.length() + 1;
338 }
339#endif
340
341#ifdef VBOX_TEST_HGCM_PARMS
342 /** Test the getString member function. Indirectly tests the getPointer
343 * and getBuffer APIs.
344 * @param hTest an running IPRT test
345 * @param aType the type that the parameter should be set to before
346 * calling getString
347 * @param apcc the value that the parameter should be set to before
348 * calling getString, and also the address (!) which we
349 * expect getString to return. Stricter than needed of
350 * course, but I was feeling lazy.
351 * @param acb the size that the parameter should be set to before
352 * calling getString, and also the size which we expect
353 * getString to return.
354 * @param rcExp the expected return value of the call to getString.
355 */
356 void doTestGetString(RTTEST hTest, uint32_t aType, const char *apcc,
357 uint32_t acb, int rcExp)
358 {
359 /* An RTTest API like this, which would print out an additional line
360 * of context if a test failed, would be nice. This is because the
361 * line number alone doesn't help much here, given that this is a
362 * subroutine called many times. */
363 /*
364 RTTestContextF(hTest,
365 ("doTestGetString, aType=%u, apcc=%p, acp=%u, rcExp=%Rrc",
366 aType, apcc, acp, rcExp));
367 */
368 setPointer((void *)apcc, acb);
369 type = aType; /* in case we don't want VBOX_HGCM_SVC_PARM_PTR */
370 const char *pcc = NULL;
371 uint32_t cb = 0;
372 int rc = getString(&pcc, &cb);
373 RTTEST_CHECK_RC(hTest, rc, rcExp);
374 if (RT_SUCCESS(rcExp))
375 {
376 RTTEST_CHECK_MSG_RETV(hTest, (pcc == apcc),
377 (hTest, "expected %p, got %p", apcc, pcc));
378 RTTEST_CHECK_MSG_RETV(hTest, (cb == acb),
379 (hTest, "expected %u, got %u", acb, cb));
380 }
381 }
382
383 /** Run some unit tests on the getString method and indirectly test
384 * getPointer and getBuffer as well. */
385 void testGetString(RTTEST hTest)
386 {
387 RTTestSub(hTest, "HGCM string parameter handling");
388 doTestGetString(hTest, VBOX_HGCM_SVC_PARM_32BIT, "test", 3,
389 VERR_INVALID_PARAMETER);
390 doTestGetString(hTest, VBOX_HGCM_SVC_PARM_PTR, "test", 5,
391 VINF_SUCCESS);
392 doTestGetString(hTest, VBOX_HGCM_SVC_PARM_PTR, "test", 3,
393 VERR_BUFFER_OVERFLOW);
394 doTestGetString(hTest, VBOX_HGCM_SVC_PARM_PTR, "test\xf0", 6,
395 VERR_INVALID_UTF8_ENCODING);
396 doTestGetString(hTest, VBOX_HGCM_SVC_PARM_PTR, "test", 0,
397 VERR_INVALID_PARAMETER);
398 doTestGetString(hTest, VBOX_HGCM_SVC_PARM_PTR, (const char *)0x1, 5,
399 VERR_INVALID_PARAMETER);
400 RTTestSubDone(hTest);
401 }
402#endif
403
404 VBOXHGCMSVCPARM() : type(VBOX_HGCM_SVC_PARM_INVALID) {}
405#endif
406} VBOXHGCMSVCPARM;
407
408typedef VBOXHGCMSVCPARM *PVBOXHGCMSVCPARM;
409
410#ifdef VBOX_WITH_CRHGSMI
411typedef void * HGCMCVSHANDLE;
412
413typedef DECLCALLBACK(void) HGCMHOSTFASTCALLCB (int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam, void *pvContext);
414typedef HGCMHOSTFASTCALLCB *PHGCMHOSTFASTCALLCB;
415#endif
416
417
418/** Service specific extension callback.
419 * This callback is called by the service to perform service specific operation.
420 *
421 * @param pvExtension The extension pointer.
422 * @param u32Function What the callback is supposed to do.
423 * @param pvParm The function parameters.
424 * @param cbParm The size of the function parameters.
425 */
426typedef DECLCALLBACK(int) FNHGCMSVCEXT(void *pvExtension, uint32_t u32Function, void *pvParm, uint32_t cbParms);
427typedef FNHGCMSVCEXT *PFNHGCMSVCEXT;
428
429/** The Service DLL entry points.
430 *
431 * HGCM will call the DLL "VBoxHGCMSvcLoad"
432 * function and the DLL must fill in the VBOXHGCMSVCFNTABLE
433 * with function pointers.
434 */
435
436/* The structure is used in separately compiled binaries so an explicit packing is required. */
437#pragma pack(1) /** @todo r=bird: The pragma pack(1) is not at all required!! */
438typedef struct VBOXHGCMSVCFNTABLE
439{
440 /** @name Filled by HGCM
441 * @{ */
442
443 /** Size of the structure. */
444 uint32_t cbSize;
445
446 /** Version of the structure, including the helpers. */
447 uint32_t u32Version;
448
449 PVBOXHGCMSVCHELPERS pHelpers;
450 /** @} */
451
452 /** @name Filled in by the service.
453 * @{ */
454
455 /** Size of client information the service want to have. */
456 uint32_t cbClient;
457#if ARCH_BITS == 64
458 /** Ensure that the following pointers are properly aligned on 64-bit system. */
459 uint32_t u32Alignment0;
460#endif
461
462 /** Uninitialize service */
463 DECLR3CALLBACKMEMBER(int, pfnUnload, (void *pvService));
464
465 /** Inform the service about a client connection. */
466 DECLR3CALLBACKMEMBER(int, pfnConnect, (void *pvService, uint32_t u32ClientID, void *pvClient));
467
468 /** Inform the service that the client wants to disconnect. */
469 DECLR3CALLBACKMEMBER(int, pfnDisconnect, (void *pvService, uint32_t u32ClientID, void *pvClient));
470
471 /** Service entry point.
472 * Return code is passed to pfnCallComplete callback.
473 */
474 DECLR3CALLBACKMEMBER(void, pfnCall, (void *pvService, VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID, void *pvClient,
475 uint32_t function, uint32_t cParms, VBOXHGCMSVCPARM paParms[]));
476
477 /** Host Service entry point meant for privileged features invisible to the guest.
478 * Return code is passed to pfnCallComplete callback.
479 */
480 DECLR3CALLBACKMEMBER(int, pfnHostCall, (void *pvService, uint32_t function, uint32_t cParms, VBOXHGCMSVCPARM paParms[]));
481
482 /** Inform the service about a VM save operation. */
483 DECLR3CALLBACKMEMBER(int, pfnSaveState, (void *pvService, uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM));
484
485 /** Inform the service about a VM load operation. */
486 DECLR3CALLBACKMEMBER(int, pfnLoadState, (void *pvService, uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM));
487
488 /** Register a service extension callback. */
489 DECLR3CALLBACKMEMBER(int, pfnRegisterExtension, (void *pvService, PFNHGCMSVCEXT pfnExtension, void *pvExtension));
490
491 /** User/instance data pointer for the service. */
492 void *pvService;
493
494 /** @} */
495} VBOXHGCMSVCFNTABLE;
496#pragma pack()
497
498
499/** Service initialization entry point. */
500typedef DECLCALLBACK(int) VBOXHGCMSVCLOAD(VBOXHGCMSVCFNTABLE *ptable);
501typedef VBOXHGCMSVCLOAD *PFNVBOXHGCMSVCLOAD;
502#define VBOX_HGCM_SVCLOAD_NAME "VBoxHGCMSvcLoad"
503
504#endif
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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