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/string.h>
|
---|
31 | #include <VBox/cdefs.h>
|
---|
32 | #include <VBox/types.h>
|
---|
33 | #include <VBox/err.h>
|
---|
34 | #ifdef VBOX_TEST_HGCM_PARMS
|
---|
35 | # include <iprt/test.h>
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | /** @todo proper comments. */
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Service interface version.
|
---|
42 | *
|
---|
43 | * Includes layout of both VBOXHGCMSVCFNTABLE and VBOXHGCMSVCHELPERS.
|
---|
44 | *
|
---|
45 | * A service can work with these structures if major version
|
---|
46 | * is equal and minor version of service is <= version of the
|
---|
47 | * structures.
|
---|
48 | *
|
---|
49 | * For example when a new helper is added at the end of helpers
|
---|
50 | * structure, then the minor version will be increased. All older
|
---|
51 | * services still can work because they have their old helpers
|
---|
52 | * unchanged.
|
---|
53 | *
|
---|
54 | * Revision history.
|
---|
55 | * 1.1->2.1 Because the pfnConnect now also has the pvClient parameter.
|
---|
56 | * 2.1->2.2 Because pfnSaveState and pfnLoadState were added
|
---|
57 | * 2.2->3.1 Because pfnHostCall is now synchronous, returns rc, and parameters were changed
|
---|
58 | * 3.1->3.2 Because pfnRegisterExtension was added
|
---|
59 | * 3.2->3.3 Because pfnDisconnectClient helper was added
|
---|
60 | * 3.3->4.1 Because the pvService entry and parameter was added
|
---|
61 | * 4.1->4.2 Because the VBOX_HGCM_SVC_PARM_CALLBACK parameter type was added
|
---|
62 | * 4.2->5.1 Removed the VBOX_HGCM_SVC_PARM_CALLBACK parameter type, as
|
---|
63 | * this problem is already solved by service extension callbacks
|
---|
64 | */
|
---|
65 | #define VBOX_HGCM_SVC_VERSION_MAJOR (0x0005)
|
---|
66 | #define VBOX_HGCM_SVC_VERSION_MINOR (0x0001)
|
---|
67 | #define VBOX_HGCM_SVC_VERSION ((VBOX_HGCM_SVC_VERSION_MAJOR << 16) + VBOX_HGCM_SVC_VERSION_MINOR)
|
---|
68 |
|
---|
69 |
|
---|
70 | /** Typed pointer to distinguish a call to service. */
|
---|
71 | struct VBOXHGCMCALLHANDLE_TYPEDEF;
|
---|
72 | typedef struct VBOXHGCMCALLHANDLE_TYPEDEF *VBOXHGCMCALLHANDLE;
|
---|
73 |
|
---|
74 | /** Service helpers pointers table. */
|
---|
75 | typedef struct VBOXHGCMSVCHELPERS
|
---|
76 | {
|
---|
77 | /** The service has processed the Call request. */
|
---|
78 | DECLR3CALLBACKMEMBER(void, pfnCallComplete, (VBOXHGCMCALLHANDLE callHandle, int32_t rc));
|
---|
79 |
|
---|
80 | void *pvInstance;
|
---|
81 |
|
---|
82 | /** The service disconnects the client. */
|
---|
83 | DECLR3CALLBACKMEMBER(void, pfnDisconnectClient, (void *pvInstance, uint32_t u32ClientID));
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Check if the @a callHandle is for a call restored and re-submitted from saved state.
|
---|
87 | *
|
---|
88 | * @returns true if restored, false if not.
|
---|
89 | * @param callHandle The call we're checking up on.
|
---|
90 | */
|
---|
91 | DECLR3CALLBACKMEMBER(bool, pfnIsCallRestored, (VBOXHGCMCALLHANDLE callHandle));
|
---|
92 |
|
---|
93 | } VBOXHGCMSVCHELPERS;
|
---|
94 |
|
---|
95 | typedef VBOXHGCMSVCHELPERS *PVBOXHGCMSVCHELPERS;
|
---|
96 |
|
---|
97 |
|
---|
98 | #define VBOX_HGCM_SVC_PARM_INVALID (0U)
|
---|
99 | #define VBOX_HGCM_SVC_PARM_32BIT (1U)
|
---|
100 | #define VBOX_HGCM_SVC_PARM_64BIT (2U)
|
---|
101 | #define VBOX_HGCM_SVC_PARM_PTR (3U)
|
---|
102 |
|
---|
103 | typedef struct VBOXHGCMSVCPARM
|
---|
104 | {
|
---|
105 | /** VBOX_HGCM_SVC_PARM_* values. */
|
---|
106 | uint32_t type;
|
---|
107 |
|
---|
108 | union
|
---|
109 | {
|
---|
110 | uint32_t uint32;
|
---|
111 | uint64_t uint64;
|
---|
112 | struct
|
---|
113 | {
|
---|
114 | uint32_t size;
|
---|
115 | void *addr;
|
---|
116 | } pointer;
|
---|
117 | } u;
|
---|
118 | #ifdef __cplusplus
|
---|
119 | /** Extract an uint32_t value from an HGCM parameter structure */
|
---|
120 | int getUInt32(uint32_t *u32)
|
---|
121 | {
|
---|
122 | AssertPtrReturn(u32, VERR_INVALID_POINTER);
|
---|
123 | int rc = VINF_SUCCESS;
|
---|
124 | if (type != VBOX_HGCM_SVC_PARM_32BIT)
|
---|
125 | rc = VERR_INVALID_PARAMETER;
|
---|
126 | if (RT_SUCCESS(rc))
|
---|
127 | *u32 = u.uint32;
|
---|
128 | return rc;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /** Extract a uint64_t value from an HGCM parameter structure */
|
---|
132 | int getUInt64(uint64_t *u64)
|
---|
133 | {
|
---|
134 | AssertPtrReturn(u64, VERR_INVALID_POINTER);
|
---|
135 | int rc = VINF_SUCCESS;
|
---|
136 | if (type != VBOX_HGCM_SVC_PARM_64BIT)
|
---|
137 | rc = VERR_INVALID_PARAMETER;
|
---|
138 | if (RT_SUCCESS(rc))
|
---|
139 | *u64 = u.uint64;
|
---|
140 | return rc;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /** Extract a pointer value from an HGCM parameter structure */
|
---|
144 | int getPointer(void **ppv, uint32_t *pcb)
|
---|
145 | {
|
---|
146 | AssertPtrReturn(ppv, VERR_INVALID_POINTER);
|
---|
147 | AssertPtrReturn(pcb, VERR_INVALID_POINTER);
|
---|
148 | if (type == VBOX_HGCM_SVC_PARM_PTR)
|
---|
149 | {
|
---|
150 | *ppv = u.pointer.addr;
|
---|
151 | *pcb = u.pointer.size;
|
---|
152 | return VINF_SUCCESS;
|
---|
153 | }
|
---|
154 |
|
---|
155 | return VERR_INVALID_PARAMETER;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /** Extract a constant pointer value from an HGCM parameter structure */
|
---|
159 | int getPointer(const void **ppcv, uint32_t *pcb)
|
---|
160 | {
|
---|
161 | AssertPtrReturn(ppcv, VERR_INVALID_POINTER);
|
---|
162 | AssertPtrReturn(pcb, VERR_INVALID_POINTER);
|
---|
163 | void *pv;
|
---|
164 | int rc = getPointer(&pv, pcb);
|
---|
165 | *ppcv = pv;
|
---|
166 | return rc;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /** Extract a pointer value to a non-empty buffer from an HGCM parameter
|
---|
170 | * structure */
|
---|
171 | int getBuffer(void **ppv, uint32_t *pcb)
|
---|
172 | {
|
---|
173 | AssertPtrReturn(ppv, VERR_INVALID_POINTER);
|
---|
174 | AssertPtrReturn(pcb, VERR_INVALID_POINTER);
|
---|
175 | void *pv = NULL;
|
---|
176 | uint32_t cb = 0;
|
---|
177 | int rc = getPointer(&pv, &cb);
|
---|
178 | if ( RT_SUCCESS(rc)
|
---|
179 | && VALID_PTR(pv)
|
---|
180 | && cb > 0)
|
---|
181 | {
|
---|
182 | *ppv = pv;
|
---|
183 | *pcb = cb;
|
---|
184 | return VINF_SUCCESS;
|
---|
185 | }
|
---|
186 |
|
---|
187 | return VERR_INVALID_PARAMETER;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /** Extract a pointer value to a non-empty constant buffer from an HGCM
|
---|
191 | * parameter structure */
|
---|
192 | int getBuffer(const void **ppcv, uint32_t *pcb)
|
---|
193 | {
|
---|
194 | AssertPtrReturn(ppcv, VERR_INVALID_POINTER);
|
---|
195 | AssertPtrReturn(pcb, VERR_INVALID_POINTER);
|
---|
196 | void *pcv = NULL;
|
---|
197 | int rc = getBuffer(&pcv, pcb);
|
---|
198 | *ppcv = pcv;
|
---|
199 | return rc;
|
---|
200 | }
|
---|
201 |
|
---|
202 | /** Extract a string value from an HGCM parameter structure */
|
---|
203 | int getString(char **ppch, uint32_t *pcb)
|
---|
204 | {
|
---|
205 | uint32_t cb = 0;
|
---|
206 | char *pch = NULL;
|
---|
207 | int rc = getBuffer((void **)&pch, &cb);
|
---|
208 | if (RT_FAILURE(rc))
|
---|
209 | {
|
---|
210 | *ppch = NULL;
|
---|
211 | *pcb = 0;
|
---|
212 | return rc;
|
---|
213 | }
|
---|
214 | rc = RTStrValidateEncodingEx(pch, cb,
|
---|
215 | RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED);
|
---|
216 | *ppch = pch;
|
---|
217 | *pcb = cb;
|
---|
218 | return rc;
|
---|
219 | }
|
---|
220 |
|
---|
221 | /** Extract a constant string value from an HGCM parameter structure */
|
---|
222 | int getString(const char **ppch, uint32_t *pcb)
|
---|
223 | {
|
---|
224 | char *pch = NULL;
|
---|
225 | int rc = getString(&pch, pcb);
|
---|
226 | *ppch = pch;
|
---|
227 | return rc;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /** Set a uint32_t value to an HGCM parameter structure */
|
---|
231 | void setUInt32(uint32_t u32)
|
---|
232 | {
|
---|
233 | type = VBOX_HGCM_SVC_PARM_32BIT;
|
---|
234 | u.uint32 = u32;
|
---|
235 | }
|
---|
236 |
|
---|
237 | /** Set a uint64_t value to an HGCM parameter structure */
|
---|
238 | void setUInt64(uint64_t u64)
|
---|
239 | {
|
---|
240 | type = VBOX_HGCM_SVC_PARM_64BIT;
|
---|
241 | u.uint64 = u64;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /** Set a pointer value to an HGCM parameter structure */
|
---|
245 | void setPointer(void *pv, uint32_t cb)
|
---|
246 | {
|
---|
247 | type = VBOX_HGCM_SVC_PARM_PTR;
|
---|
248 | u.pointer.addr = pv;
|
---|
249 | u.pointer.size = cb;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /** Set a const string value to an HGCM parameter structure */
|
---|
253 | void setString(const char *psz)
|
---|
254 | {
|
---|
255 | type = VBOX_HGCM_SVC_PARM_PTR;
|
---|
256 | u.pointer.addr = (void *)psz;
|
---|
257 | u.pointer.size = (uint32_t)strlen(psz) + 1;
|
---|
258 | }
|
---|
259 |
|
---|
260 | #ifdef ___iprt_cpp_ministring_h
|
---|
261 | /** Set a const string value to an HGCM parameter structure */
|
---|
262 | void setCppString(const RTCString &rString)
|
---|
263 | {
|
---|
264 | type = VBOX_HGCM_SVC_PARM_PTR;
|
---|
265 | u.pointer.addr = (void *)rString.c_str();
|
---|
266 | u.pointer.size = (uint32_t)rString.length() + 1;
|
---|
267 | }
|
---|
268 | #endif
|
---|
269 |
|
---|
270 | #ifdef VBOX_TEST_HGCM_PARMS
|
---|
271 | /** Test the getString member function. Indirectly tests the getPointer
|
---|
272 | * and getBuffer APIs.
|
---|
273 | * @param hTest an running IPRT test
|
---|
274 | * @param aType the type that the parameter should be set to before
|
---|
275 | * calling getString
|
---|
276 | * @param apcc the value that the parameter should be set to before
|
---|
277 | * calling getString, and also the address (!) which we
|
---|
278 | * expect getString to return. Stricter than needed of
|
---|
279 | * course, but I was feeling lazy.
|
---|
280 | * @param acb the size that the parameter should be set to before
|
---|
281 | * calling getString, and also the size which we expect
|
---|
282 | * getString to return.
|
---|
283 | * @param rcExp the expected return value of the call to getString.
|
---|
284 | */
|
---|
285 | void doTestGetString(RTTEST hTest, uint32_t aType, const char *apcc,
|
---|
286 | uint32_t acb, int rcExp)
|
---|
287 | {
|
---|
288 | /* An RTTest API like this, which would print out an additional line
|
---|
289 | * of context if a test failed, would be nice. This is because the
|
---|
290 | * line number alone doesn't help much here, given that this is a
|
---|
291 | * subroutine called many times. */
|
---|
292 | /*
|
---|
293 | RTTestContextF(hTest,
|
---|
294 | ("doTestGetString, aType=%u, apcc=%p, acp=%u, rcExp=%Rrc",
|
---|
295 | aType, apcc, acp, rcExp));
|
---|
296 | */
|
---|
297 | setPointer((void *)apcc, acb);
|
---|
298 | type = aType; /* in case we don't want VBOX_HGCM_SVC_PARM_PTR */
|
---|
299 | const char *pcc = NULL;
|
---|
300 | uint32_t cb = 0;
|
---|
301 | int rc = getString(&pcc, &cb);
|
---|
302 | RTTEST_CHECK_RC(hTest, rc, rcExp);
|
---|
303 | if (RT_SUCCESS(rcExp))
|
---|
304 | {
|
---|
305 | RTTEST_CHECK_MSG_RETV(hTest, (pcc == apcc),
|
---|
306 | (hTest, "expected %p, got %p", apcc, pcc));
|
---|
307 | RTTEST_CHECK_MSG_RETV(hTest, (cb == acb),
|
---|
308 | (hTest, "expected %u, got %u", acb, cb));
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | /** Run some unit tests on the getString method and indirectly test
|
---|
313 | * getPointer and getBuffer as well. */
|
---|
314 | void testGetString(RTTEST hTest)
|
---|
315 | {
|
---|
316 | RTTestSub(hTest, "HGCM string parameter handling");
|
---|
317 | doTestGetString(hTest, VBOX_HGCM_SVC_PARM_32BIT, "test", 3,
|
---|
318 | VERR_INVALID_PARAMETER);
|
---|
319 | doTestGetString(hTest, VBOX_HGCM_SVC_PARM_PTR, "test", 5,
|
---|
320 | VINF_SUCCESS);
|
---|
321 | doTestGetString(hTest, VBOX_HGCM_SVC_PARM_PTR, "test", 3,
|
---|
322 | VERR_BUFFER_OVERFLOW);
|
---|
323 | doTestGetString(hTest, VBOX_HGCM_SVC_PARM_PTR, "test\xf0", 6,
|
---|
324 | VERR_INVALID_UTF8_ENCODING);
|
---|
325 | doTestGetString(hTest, VBOX_HGCM_SVC_PARM_PTR, "test", 0,
|
---|
326 | VERR_INVALID_PARAMETER);
|
---|
327 | doTestGetString(hTest, VBOX_HGCM_SVC_PARM_PTR, (const char *)0x1, 5,
|
---|
328 | VERR_INVALID_PARAMETER);
|
---|
329 | RTTestSubDone(hTest);
|
---|
330 | }
|
---|
331 | #endif
|
---|
332 |
|
---|
333 | VBOXHGCMSVCPARM() : type(VBOX_HGCM_SVC_PARM_INVALID) {}
|
---|
334 | #endif
|
---|
335 | } VBOXHGCMSVCPARM;
|
---|
336 |
|
---|
337 | typedef VBOXHGCMSVCPARM *PVBOXHGCMSVCPARM;
|
---|
338 |
|
---|
339 | #ifdef VBOX_WITH_CRHGSMI
|
---|
340 | typedef void * HGCMCVSHANDLE;
|
---|
341 |
|
---|
342 | typedef DECLCALLBACK(void) HGCMHOSTFASTCALLCB (int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam, void *pvContext);
|
---|
343 | typedef HGCMHOSTFASTCALLCB *PHGCMHOSTFASTCALLCB;
|
---|
344 | #endif
|
---|
345 |
|
---|
346 |
|
---|
347 | /** Service specific extension callback.
|
---|
348 | * This callback is called by the service to perform service specific operation.
|
---|
349 | *
|
---|
350 | * @param pvExtension The extension pointer.
|
---|
351 | * @param u32Function What the callback is supposed to do.
|
---|
352 | * @param pvParm The function parameters.
|
---|
353 | * @param cbParm The size of the function parameters.
|
---|
354 | */
|
---|
355 | typedef DECLCALLBACK(int) FNHGCMSVCEXT(void *pvExtension, uint32_t u32Function, void *pvParm, uint32_t cbParms);
|
---|
356 | typedef FNHGCMSVCEXT *PFNHGCMSVCEXT;
|
---|
357 |
|
---|
358 | /** The Service DLL entry points.
|
---|
359 | *
|
---|
360 | * HGCM will call the DLL "VBoxHGCMSvcLoad"
|
---|
361 | * function and the DLL must fill in the VBOXHGCMSVCFNTABLE
|
---|
362 | * with function pointers.
|
---|
363 | */
|
---|
364 |
|
---|
365 | /* The structure is used in separately compiled binaries so an explicit packing is required. */
|
---|
366 | #pragma pack(1) /** @todo r=bird: The pragma pack(1) is not at all required!! */
|
---|
367 | typedef struct VBOXHGCMSVCFNTABLE
|
---|
368 | {
|
---|
369 | /** @name Filled by HGCM
|
---|
370 | * @{ */
|
---|
371 |
|
---|
372 | /** Size of the structure. */
|
---|
373 | uint32_t cbSize;
|
---|
374 |
|
---|
375 | /** Version of the structure, including the helpers. */
|
---|
376 | uint32_t u32Version;
|
---|
377 |
|
---|
378 | PVBOXHGCMSVCHELPERS pHelpers;
|
---|
379 | /** @} */
|
---|
380 |
|
---|
381 | /** @name Filled in by the service.
|
---|
382 | * @{ */
|
---|
383 |
|
---|
384 | /** Size of client information the service want to have. */
|
---|
385 | uint32_t cbClient;
|
---|
386 | #if ARCH_BITS == 64
|
---|
387 | /** Ensure that the following pointers are properly aligned on 64-bit system. */
|
---|
388 | uint32_t u32Alignment0;
|
---|
389 | #endif
|
---|
390 |
|
---|
391 | /** Uninitialize service */
|
---|
392 | DECLR3CALLBACKMEMBER(int, pfnUnload, (void *pvService));
|
---|
393 |
|
---|
394 | /** Inform the service about a client connection. */
|
---|
395 | DECLR3CALLBACKMEMBER(int, pfnConnect, (void *pvService, uint32_t u32ClientID, void *pvClient));
|
---|
396 |
|
---|
397 | /** Inform the service that the client wants to disconnect. */
|
---|
398 | DECLR3CALLBACKMEMBER(int, pfnDisconnect, (void *pvService, uint32_t u32ClientID, void *pvClient));
|
---|
399 |
|
---|
400 | /** Service entry point.
|
---|
401 | * Return code is passed to pfnCallComplete callback.
|
---|
402 | */
|
---|
403 | DECLR3CALLBACKMEMBER(void, pfnCall, (void *pvService, VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID, void *pvClient,
|
---|
404 | uint32_t function, uint32_t cParms, VBOXHGCMSVCPARM paParms[]));
|
---|
405 |
|
---|
406 | /** Host Service entry point meant for privileged features invisible to the guest.
|
---|
407 | * Return code is passed to pfnCallComplete callback.
|
---|
408 | */
|
---|
409 | DECLR3CALLBACKMEMBER(int, pfnHostCall, (void *pvService, uint32_t function, uint32_t cParms, VBOXHGCMSVCPARM paParms[]));
|
---|
410 |
|
---|
411 | /** Inform the service about a VM save operation. */
|
---|
412 | DECLR3CALLBACKMEMBER(int, pfnSaveState, (void *pvService, uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM));
|
---|
413 |
|
---|
414 | /** Inform the service about a VM load operation. */
|
---|
415 | DECLR3CALLBACKMEMBER(int, pfnLoadState, (void *pvService, uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM));
|
---|
416 |
|
---|
417 | /** Register a service extension callback. */
|
---|
418 | DECLR3CALLBACKMEMBER(int, pfnRegisterExtension, (void *pvService, PFNHGCMSVCEXT pfnExtension, void *pvExtension));
|
---|
419 |
|
---|
420 | /** User/instance data pointer for the service. */
|
---|
421 | void *pvService;
|
---|
422 |
|
---|
423 | /** @} */
|
---|
424 | } VBOXHGCMSVCFNTABLE;
|
---|
425 | #pragma pack()
|
---|
426 |
|
---|
427 |
|
---|
428 | /** Service initialization entry point. */
|
---|
429 | typedef DECLCALLBACK(int) VBOXHGCMSVCLOAD(VBOXHGCMSVCFNTABLE *ptable);
|
---|
430 | typedef VBOXHGCMSVCLOAD *PFNVBOXHGCMSVCLOAD;
|
---|
431 | #define VBOX_HGCM_SVCLOAD_NAME "VBoxHGCMSvcLoad"
|
---|
432 |
|
---|
433 | #endif
|
---|