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