1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
|
/* $Id: HGCMMock.h $ */
/** @file
* HGCMMock.h: Mocking framework for testing HGCM-based host services +
* Vbgl code on the host side.
*
* Goal is to run host service + Vbgl code as unmodified as
* possible as part of testcases to gain test coverage which
* otherwise wouldn't possible for heavily user-centric features
* like Shared Clipboard or drag'n drop (DnD).
*/
/*
* Copyright (C) 2022 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
* in the VirtualBox distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
*/
#ifndef VBOX_INCLUDED_GuestHost_HGCMMock_h
#define VBOX_INCLUDED_GuestHost_HGCMMock_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif
#include <iprt/types.h>
#include <iprt/asm.h>
#include <iprt/assert.h>
#include <iprt/list.h>
#include <iprt/mem.h>
#include <iprt/rand.h>
#include <iprt/semaphore.h>
#include <iprt/test.h>
#include <iprt/time.h>
#include <iprt/thread.h>
#include <iprt/utf16.h>
#include <VBox/err.h>
#include <VBox/VBoxGuestLib.h>
#include <VBox/hgcmsvc.h>
/*********************************************************************************************************************************
* Definitions. *
*********************************************************************************************************************************/
#if defined(IN_RING3) /* Only R3 parts implemented so far. */
RT_C_DECLS_BEGIN
DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *ptable);
RT_C_DECLS_END
# define VBGLR3DECL(type) DECL_HIDDEN_NOTHROW(type) VBOXCALL
/** Simple call handle structure for the guest call completion callback. */
typedef struct VBOXHGCMCALLHANDLE_TYPEDEF
{
/** Where to store the result code on call completion. */
int32_t rc;
} VBOXHGCMCALLHANDLE_TYPEDEF;
/**
* Enumeration for a HGCM mock function type.
*/
typedef enum TSTHGCMMOCKFNTYPE
{
TSTHGCMMOCKFNTYPE_NONE = 0,
TSTHGCMMOCKFNTYPE_CONNECT,
TSTHGCMMOCKFNTYPE_DISCONNECT,
TSTHGCMMOCKFNTYPE_CALL,
TSTHGCMMOCKFNTYPE_HOST_CALL
} TSTHGCMMOCKFNTYPE;
/** Pointer to a mock HGCM service. */
typedef struct TSTHGCMMOCKSVC *PTSTHGCMMOCKSVC;
/**
* Structure for mocking a server-side HGCM client.
*/
typedef struct TSTHGCMMOCKCLIENT
{
/** Pointer to to mock service instance this client belongs to. */
PTSTHGCMMOCKSVC pSvc;
/** Assigned HGCM client ID. */
uint32_t idClient;
/** Opaque pointer to service-specific client data.
* Can be NULL if not being used. */
void *pvClient;
/** Size (in bytes) of \a pvClient. */
size_t cbClient;
/** The client's current HGCM call handle. */
VBOXHGCMCALLHANDLE_TYPEDEF hCall;
/** Whether the current client call has an asynchronous
* call pending or not. */
bool fAsyncExec;
/** Event semaphore to signal call completion. */
RTSEMEVENT hEvent;
} TSTHGCMMOCKCLIENT;
/** Pointer to a mock HGCM client. */
typedef TSTHGCMMOCKCLIENT *PTSTHGCMMOCKCLIENT;
/**
* Structure for keeping HGCM mock function parameters.
*/
typedef struct TSTHGCMMOCKFN
{
/** List node for storing this struct into a queue. */
RTLISTNODE Node;
/** Function type. */
TSTHGCMMOCKFNTYPE enmType;
/** Pointer to associated client. */
PTSTHGCMMOCKCLIENT pClient;
/** Union keeping function-specific parameters,
* depending on \a enmType. */
union
{
struct
{
int32_t iFunc;
uint32_t cParms;
PVBOXHGCMSVCPARM pParms;
VBOXHGCMCALLHANDLE hCall;
} Call;
struct
{
int32_t iFunc;
uint32_t cParms;
PVBOXHGCMSVCPARM pParms;
} HostCall;
} u;
} TSTHGCMMOCKFN;
/** Pointer to a HGCM mock function parameters structure. */
typedef TSTHGCMMOCKFN *PTSTHGCMMOCKFN;
/**
* Structure for keeping a HGCM mock service instance.
*/
typedef struct TSTHGCMMOCKSVC
{
/** HGCM helper table to use. */
VBOXHGCMSVCHELPERS fnHelpers;
/** HGCM service function table to use. */
VBOXHGCMSVCFNTABLE fnTable;
/** Next HGCM client ID to assign.
* 0 is considered as being invalid. */
HGCMCLIENTID uNextClientId;
/** Size (in bytes) of opaque pvClient area to reserve
* for a connected client. */
size_t cbClient;
/** Array of connected HGCM mock clients.
* Currently limited to 4 clients maximum. */
TSTHGCMMOCKCLIENT aHgcmClient[4];
/** Thread handle for the service's main loop. */
RTTHREAD hThread;
/** Event semaphore for signalling a message
* queue change. */
RTSEMEVENT hEventQueue;
/** Event semaphore for clients connecting to the server. */
RTSEMEVENT hEventConnect;
/** Number of current host calls being served.
* Currently limited to one call at a time. */
uint8_t cHostCallers;
/** Result code of last returned host call. */
int rcHostCall;
/** Event semaphore for host calls. */
RTSEMEVENT hEventHostCall;
/** List (queue) of function calls to process. */
RTLISTANCHOR lstCall;
/** Shutdown indicator flag. */
volatile bool fShutdown;
} TSTHGCMMOCKSVC;
/** Static HGCM service to mock. */
static TSTHGCMMOCKSVC s_tstHgcmSvc;
/*********************************************************************************************************************************
* Prototypes. *
*********************************************************************************************************************************/
PTSTHGCMMOCKSVC TstHgcmMockSvcInst(void);
PTSTHGCMMOCKCLIENT TstHgcmMockSvcWaitForConnectEx(PTSTHGCMMOCKSVC pSvc, RTMSINTERVAL msTimeout);
PTSTHGCMMOCKCLIENT TstHgcmMockSvcWaitForConnect(PTSTHGCMMOCKSVC pSvc);
int TstHgcmMockSvcCreate(PTSTHGCMMOCKSVC pSvc, size_t cbClient);
int TstHgcmMockSvcDestroy(PTSTHGCMMOCKSVC pSvc);
int TstHgcmMockSvcStart(PTSTHGCMMOCKSVC pSvc);
int TstHgcmMockSvcStop(PTSTHGCMMOCKSVC pSvc);
int TstHgcmMockSvcHostCall(PTSTHGCMMOCKSVC pSvc, void *pvService, int32_t function, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
VBGLR3DECL(int) VbglR3HGCMConnect(const char *pszServiceName, HGCMCLIENTID *pidClient);
VBGLR3DECL(int) VbglR3HGCMDisconnect(HGCMCLIENTID idClient);
VBGLR3DECL(int) VbglR3HGCMCall(PVBGLIOCHGCMCALL pInfo, size_t cbInfo);
/*********************************************************************************************************************************
* Internal functions *
*********************************************************************************************************************************/
/**
* Initializes a HGCM mock client.
*
* @return VBox status code.
* @param pClient Client instance to initialize.
* @param idClient HGCM client ID to assign.
* @param cbClient Size (in bytes) of service-specific (opaque) client data to allocate.
*/
static int tstHgcmMockClientInit(PTSTHGCMMOCKCLIENT pClient, uint32_t idClient, size_t cbClient)
{
RT_BZERO(pClient, sizeof(TSTHGCMMOCKCLIENT));
pClient->idClient = idClient;
if (cbClient)
{
pClient->pvClient = RTMemAllocZ(cbClient);
AssertPtrReturn(pClient->pvClient, VERR_NO_MEMORY);
pClient->cbClient = cbClient;
}
return RTSemEventCreate(&pClient->hEvent);
}
/**
* Destroys a HGCM mock client.
*
* @return VBox status code.
* @param pClient Client instance to destroy.
*/
static int tstHgcmMockClientDestroy(PTSTHGCMMOCKCLIENT pClient)
{
int rc = RTSemEventDestroy(pClient->hEvent);
if (RT_SUCCESS(rc))
{
if (pClient->pvClient)
{
Assert(pClient->cbClient);
RTMemFree(pClient->pvClient);
pClient->pvClient = NULL;
pClient->cbClient = 0;
}
pClient->hEvent = NIL_RTSEMEVENT;
}
return rc;
}
/* @copydoc VBOXHGCMSVCFNTABLE::pfnConnect */
static DECLCALLBACK(int) tstHgcmMockSvcConnect(PTSTHGCMMOCKSVC pSvc, void *pvService, uint32_t *pidClient)
{
RT_NOREF(pvService);
PTSTHGCMMOCKFN pFn = (PTSTHGCMMOCKFN)RTMemAllocZ(sizeof(TSTHGCMMOCKFN));
AssertPtrReturn(pFn, VERR_NO_MEMORY);
PTSTHGCMMOCKCLIENT pClient = &pSvc->aHgcmClient[pSvc->uNextClientId];
int rc = tstHgcmMockClientInit(pClient, pSvc->uNextClientId, pSvc->cbClient);
if (RT_FAILURE(rc))
return rc;
pFn->enmType = TSTHGCMMOCKFNTYPE_CONNECT;
pFn->pClient = pClient;
RTListAppend(&pSvc->lstCall, &pFn->Node);
pFn = NULL; /* Thread takes ownership now. */
int rc2 = RTSemEventSignal(pSvc->hEventQueue);
AssertRCReturn(rc2, rc2);
rc2 = RTSemEventWait(pClient->hEvent, RT_MS_30SEC);
AssertRCReturn(rc2, rc2);
ASMAtomicIncU32(&pSvc->uNextClientId);
rc2 = RTSemEventSignal(pSvc->hEventConnect);
AssertRCReturn(rc2, rc2);
*pidClient = pClient->idClient;
return VINF_SUCCESS;
}
/* @copydoc VBOXHGCMSVCFNTABLE::pfnDisconnect */
static DECLCALLBACK(int) tstHgcmMockSvcDisconnect(PTSTHGCMMOCKSVC pSvc, void *pvService, uint32_t idClient)
{
RT_NOREF(pvService);
PTSTHGCMMOCKCLIENT pClient = &pSvc->aHgcmClient[idClient];
PTSTHGCMMOCKFN pFn = (PTSTHGCMMOCKFN)RTMemAllocZ(sizeof(TSTHGCMMOCKFN));
AssertPtrReturn(pFn, VERR_NO_MEMORY);
pFn->enmType = TSTHGCMMOCKFNTYPE_DISCONNECT;
pFn->pClient = pClient;
RTListAppend(&pSvc->lstCall, &pFn->Node);
pFn = NULL; /* Thread takes ownership now. */
int rc2 = RTSemEventSignal(pSvc->hEventQueue);
AssertRCReturn(rc2, rc2);
rc2 = RTSemEventWait(pClient->hEvent, RT_MS_30SEC);
AssertRCReturn(rc2, rc2);
return tstHgcmMockClientDestroy(pClient);
}
/* @copydoc VBOXHGCMSVCFNTABLE::pfnCall */
static DECLCALLBACK(int) tstHgcmMockSvcCall(PTSTHGCMMOCKSVC pSvc, void *pvService, VBOXHGCMCALLHANDLE callHandle, uint32_t idClient, void *pvClient,
int32_t function, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
{
RT_NOREF(pvService, pvClient);
PTSTHGCMMOCKCLIENT pClient = &pSvc->aHgcmClient[idClient];
PTSTHGCMMOCKFN pFn = (PTSTHGCMMOCKFN)RTMemAllocZ(sizeof(TSTHGCMMOCKFN));
AssertPtrReturn(pFn, VERR_NO_MEMORY);
const size_t cbParms = cParms * sizeof(VBOXHGCMSVCPARM);
pFn->enmType = TSTHGCMMOCKFNTYPE_CALL;
pFn->pClient = pClient;
pFn->u.Call.hCall = callHandle;
pFn->u.Call.iFunc = function;
pFn->u.Call.pParms = (PVBOXHGCMSVCPARM)RTMemDup(paParms, cbParms);
AssertPtrReturn(pFn->u.Call.pParms, VERR_NO_MEMORY);
pFn->u.Call.cParms = cParms;
RTListAppend(&pSvc->lstCall, &pFn->Node);
int rc2 = RTSemEventSignal(pSvc->hEventQueue);
AssertRCReturn(rc2, rc2);
rc2 = RTSemEventWait(pClient->hEvent, RT_INDEFINITE_WAIT);
AssertRCReturn(rc2, rc2);
memcpy(paParms, pFn->u.Call.pParms, cbParms);
return VINF_SUCCESS; /** @todo Return host call rc */
}
/* @copydoc VBOXHGCMSVCFNTABLE::pfnHostCall */
/** Note: Public for also being able to test host calls via testcases. */
int TstHgcmMockSvcHostCall(PTSTHGCMMOCKSVC pSvc, void *pvService, int32_t function, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
{
RT_NOREF(pvService);
AssertReturn(pSvc->cHostCallers == 0, VERR_WRONG_ORDER); /* Only one host call at a time. */
pSvc->cHostCallers++;
PTSTHGCMMOCKFN pFn = (PTSTHGCMMOCKFN)RTMemAllocZ(sizeof(TSTHGCMMOCKFN));
AssertPtrReturn(pFn, VERR_INVALID_POINTER);
pFn->enmType = TSTHGCMMOCKFNTYPE_HOST_CALL;
pFn->u.HostCall.iFunc = function;
if (cParms)
{
pFn->u.HostCall.pParms = (PVBOXHGCMSVCPARM)RTMemDup(paParms, cParms * sizeof(VBOXHGCMSVCPARM));
AssertPtrReturn(pFn->u.HostCall.pParms, VERR_NO_MEMORY);
pFn->u.HostCall.cParms = cParms;
}
RTListAppend(&pSvc->lstCall, &pFn->Node);
pFn = NULL; /* Thread takes ownership now. */
int rc2 = RTSemEventSignal(pSvc->hEventQueue);
AssertRC(rc2);
rc2 = RTSemEventWait(pSvc->hEventHostCall, RT_INDEFINITE_WAIT);
AssertRCReturn(rc2, rc2);
Assert(pSvc->cHostCallers);
pSvc->cHostCallers--;
return pSvc->rcHostCall;
}
/**
* Call completion callback for guest calls.
*
* @return VBox status code.
* @param callHandle Call handle to complete.
* @param rc Return code to return to the caller.
*/
static DECLCALLBACK(int) tstHgcmMockSvcCallComplete(VBOXHGCMCALLHANDLE callHandle, int32_t rc)
{
PTSTHGCMMOCKSVC pSvc = TstHgcmMockSvcInst();
size_t i = 0;
for (; RT_ELEMENTS(pSvc->aHgcmClient); i++)
{
PTSTHGCMMOCKCLIENT pClient = &pSvc->aHgcmClient[i];
if (&pClient->hCall == callHandle) /* Slow, but works for now. */
{
if (rc == VINF_HGCM_ASYNC_EXECUTE)
{
Assert(pClient->fAsyncExec == false);
}
else /* Complete call + notify client. */
{
callHandle->rc = rc;
int rc2 = RTSemEventSignal(pClient->hEvent);
AssertRCReturn(rc2, rc2);
}
return VINF_SUCCESS;
}
}
return VERR_NOT_FOUND;
}
/**
* Main thread of HGCM mock service.
*
* @return VBox status code.
* @param hThread Thread handle.
* @param pvUser User-supplied data of type PTSTHGCMMOCKSVC.
*/
static DECLCALLBACK(int) tstHgcmMockSvcThread(RTTHREAD hThread, void *pvUser)
{
RT_NOREF(hThread);
PTSTHGCMMOCKSVC pSvc = (PTSTHGCMMOCKSVC)pvUser;
pSvc->uNextClientId = 0;
pSvc->fnTable.cbSize = sizeof(pSvc->fnTable);
pSvc->fnTable.u32Version = VBOX_HGCM_SVC_VERSION;
RT_ZERO(pSvc->fnHelpers);
pSvc->fnHelpers.pfnCallComplete = tstHgcmMockSvcCallComplete;
pSvc->fnTable.pHelpers = &pSvc->fnHelpers;
int rc = VBoxHGCMSvcLoad(&pSvc->fnTable);
if (RT_SUCCESS(rc))
{
RTThreadUserSignal(hThread);
for (;;)
{
rc = RTSemEventWait(pSvc->hEventQueue, 10 /* ms */);
if (ASMAtomicReadBool(&pSvc->fShutdown))
{
rc = VINF_SUCCESS;
break;
}
if (rc == VERR_TIMEOUT)
continue;
PTSTHGCMMOCKFN pFn = RTListGetFirst(&pSvc->lstCall, TSTHGCMMOCKFN, Node);
if (pFn)
{
switch (pFn->enmType)
{
case TSTHGCMMOCKFNTYPE_CONNECT:
{
rc = pSvc->fnTable.pfnConnect(pSvc->fnTable.pvService,
pFn->pClient->idClient, pFn->pClient->pvClient,
VMMDEV_REQUESTOR_USR_NOT_GIVEN /* fRequestor */, false /* fRestoring */);
int rc2 = RTSemEventSignal(pFn->pClient->hEvent);
AssertRC(rc2);
break;
}
case TSTHGCMMOCKFNTYPE_DISCONNECT:
{
rc = pSvc->fnTable.pfnDisconnect(pSvc->fnTable.pvService,
pFn->pClient->idClient, pFn->pClient->pvClient);
int rc2 = RTSemEventSignal(pFn->pClient->hEvent);
AssertRC(rc2);
break;
}
case TSTHGCMMOCKFNTYPE_CALL:
{
pSvc->fnTable.pfnCall(NULL, pFn->u.Call.hCall, pFn->pClient->idClient, pFn->pClient->pvClient,
pFn->u.Call.iFunc, pFn->u.Call.cParms, pFn->u.Call.pParms, RTTimeMilliTS());
/* Note: Call will be completed in the call completion callback. */
break;
}
case TSTHGCMMOCKFNTYPE_HOST_CALL:
{
pSvc->rcHostCall = pSvc->fnTable.pfnHostCall(NULL, pFn->u.HostCall.iFunc, pFn->u.HostCall.cParms, pFn->u.HostCall.pParms);
int rc2 = RTSemEventSignal(pSvc->hEventHostCall);
AssertRC(rc2);
break;
}
default:
AssertFailed();
break;
}
RTListNodeRemove(&pFn->Node);
RTMemFree(pFn);
}
}
}
return rc;
}
/*********************************************************************************************************************************
* Public functions *
*********************************************************************************************************************************/
/**
* Returns the pointer to the HGCM mock service instance.
*
* @return Pointer to HGCM mock service instance.
*/
PTSTHGCMMOCKSVC TstHgcmMockSvcInst(void)
{
return &s_tstHgcmSvc;
}
/**
* Waits for a HGCM mock client to connect, extended version.
*
* @return Pointer to connected client, or NULL if ran into timeout.
* @param pSvc HGCM mock service instance.
* @param msTimeout Timeout (in ms) to wait for connection.
*/
PTSTHGCMMOCKCLIENT TstHgcmMockSvcWaitForConnectEx(PTSTHGCMMOCKSVC pSvc, RTMSINTERVAL msTimeout)
{
int rc = RTSemEventWait(pSvc->hEventConnect, msTimeout);
if (RT_SUCCESS(rc))
{
Assert(pSvc->uNextClientId);
return &pSvc->aHgcmClient[pSvc->uNextClientId - 1];
}
return NULL;
}
/**
* Waits for a HGCM mock client to connect.
*
* @return Pointer to connected client, or NULL if waiting for connection was aborted.
* @param pSvc HGCM mock service instance.
*/
PTSTHGCMMOCKCLIENT TstHgcmMockSvcWaitForConnect(PTSTHGCMMOCKSVC pSvc)
{
return TstHgcmMockSvcWaitForConnectEx(pSvc, RT_MS_30SEC);
}
/**
* Creates a HGCM mock service instance.
*
* @return VBox status code.
* @param pSvc HGCM mock service instance to create.
* @param cbClient Size (in bytes) of service-specific client data to
* allocate for a HGCM mock client.
*/
int TstHgcmMockSvcCreate(PTSTHGCMMOCKSVC pSvc, size_t cbClient)
{
AssertReturn(cbClient, VERR_INVALID_PARAMETER);
RT_ZERO(pSvc->aHgcmClient);
pSvc->fShutdown = false;
int rc = RTSemEventCreate(&pSvc->hEventQueue);
if (RT_SUCCESS(rc))
{
rc = RTSemEventCreate(&pSvc->hEventHostCall);
if (RT_SUCCESS(rc))
{
rc = RTSemEventCreate(&pSvc->hEventConnect);
if (RT_SUCCESS(rc))
{
RTListInit(&pSvc->lstCall);
pSvc->cbClient = cbClient;
}
}
}
return rc;
}
/**
* Destroys a HGCM mock service instance.
*
* @return VBox status code.
* @param pSvc HGCM mock service instance to destroy.
*/
int TstHgcmMockSvcDestroy(PTSTHGCMMOCKSVC pSvc)
{
int rc = RTSemEventDestroy(pSvc->hEventQueue);
if (RT_SUCCESS(rc))
{
rc = RTSemEventDestroy(pSvc->hEventHostCall);
if (RT_SUCCESS(rc))
RTSemEventDestroy(pSvc->hEventConnect);
}
return rc;
}
/**
* Starts a HGCM mock service instance.
*
* @return VBox status code.
* @param pSvc HGCM mock service instance to start.
*/
int TstHgcmMockSvcStart(PTSTHGCMMOCKSVC pSvc)
{
int rc = RTThreadCreate(&pSvc->hThread, tstHgcmMockSvcThread, pSvc, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE,
"MockSvc");
if (RT_SUCCESS(rc))
rc = RTThreadUserWait(pSvc->hThread, RT_MS_30SEC);
return rc;
}
/**
* Stops a HGCM mock service instance.
*
* @return VBox status code.
* @param pSvc HGCM mock service instance to stop.
*/
int TstHgcmMockSvcStop(PTSTHGCMMOCKSVC pSvc)
{
ASMAtomicWriteBool(&pSvc->fShutdown, true);
int rcThread;
int rc = RTThreadWait(pSvc->hThread, RT_MS_30SEC, &rcThread);
if (RT_SUCCESS(rc))
rc = rcThread;
if (RT_SUCCESS(rc))
{
pSvc->hThread = NIL_RTTHREAD;
}
return rc;
}
/*********************************************************************************************************************************
* VbglR3 stubs *
*********************************************************************************************************************************/
/**
* Connects to an HGCM mock service.
*
* @returns VBox status code
* @param pszServiceName Name of the host service.
* @param pidClient Where to put the client ID on success. The client ID
* must be passed to all the other calls to the service.
*/
VBGLR3DECL(int) VbglR3HGCMConnect(const char *pszServiceName, HGCMCLIENTID *pidClient)
{
RT_NOREF(pszServiceName);
PTSTHGCMMOCKSVC pSvc = TstHgcmMockSvcInst();
return tstHgcmMockSvcConnect(pSvc, pSvc->fnTable.pvService, pidClient);
}
/**
* Disconnect from an HGCM mock service.
*
* @returns VBox status code.
* @param idClient The client id returned by VbglR3HGCMConnect().
*/
VBGLR3DECL(int) VbglR3HGCMDisconnect(HGCMCLIENTID idClient)
{
PTSTHGCMMOCKSVC pSvc = TstHgcmMockSvcInst();
return tstHgcmMockSvcDisconnect(pSvc, pSvc->fnTable.pvService, idClient);
}
/**
* Makes a fully prepared HGCM call to an HGCM mock service.
*
* @returns VBox status code.
* @param pInfo Fully prepared HGCM call info.
* @param cbInfo Size of the info. This may sometimes be larger than
* what the parameter count indicates because of
* parameter changes between versions and such.
*/
VBGLR3DECL(int) VbglR3HGCMCall(PVBGLIOCHGCMCALL pInfo, size_t cbInfo)
{
RT_NOREF(cbInfo);
AssertMsg(pInfo->Hdr.cbIn == cbInfo, ("cbIn=%#x cbInfo=%#zx\n", pInfo->Hdr.cbIn, cbInfo));
AssertMsg(pInfo->Hdr.cbOut == cbInfo, ("cbOut=%#x cbInfo=%#zx\n", pInfo->Hdr.cbOut, cbInfo));
Assert(sizeof(*pInfo) + pInfo->cParms * sizeof(HGCMFunctionParameter) <= cbInfo);
HGCMFunctionParameter *offSrcParms = VBGL_HGCM_GET_CALL_PARMS(pInfo);
PVBOXHGCMSVCPARM paDstParms = (PVBOXHGCMSVCPARM)RTMemAlloc(pInfo->cParms * sizeof(VBOXHGCMSVCPARM));
uint16_t i = 0;
for (; i < pInfo->cParms; i++)
{
switch (offSrcParms->type)
{
case VMMDevHGCMParmType_32bit:
{
paDstParms[i].type = VBOX_HGCM_SVC_PARM_32BIT;
paDstParms[i].u.uint32 = offSrcParms->u.value32;
break;
}
case VMMDevHGCMParmType_64bit:
{
paDstParms[i].type = VBOX_HGCM_SVC_PARM_64BIT;
paDstParms[i].u.uint64 = offSrcParms->u.value64;
break;
}
case VMMDevHGCMParmType_LinAddr:
{
paDstParms[i].type = VBOX_HGCM_SVC_PARM_PTR;
paDstParms[i].u.pointer.addr = (void *)offSrcParms->u.LinAddr.uAddr;
paDstParms[i].u.pointer.size = offSrcParms->u.LinAddr.cb;
break;
}
default:
AssertFailed();
break;
}
offSrcParms++;
}
PTSTHGCMMOCKSVC const pSvc = TstHgcmMockSvcInst();
int rc2 = tstHgcmMockSvcCall(pSvc, pSvc->fnTable.pvService, &pSvc->aHgcmClient[pInfo->u32ClientID].hCall,
pInfo->u32ClientID, pSvc->aHgcmClient[pInfo->u32ClientID].pvClient,
pInfo->u32Function, pInfo->cParms, paDstParms);
if (RT_SUCCESS(rc2))
{
offSrcParms = VBGL_HGCM_GET_CALL_PARMS(pInfo);
for (i = 0; i < pInfo->cParms; i++)
{
paDstParms[i].type = offSrcParms->type;
switch (paDstParms[i].type)
{
case VMMDevHGCMParmType_32bit:
offSrcParms->u.value32 = paDstParms[i].u.uint32;
break;
case VMMDevHGCMParmType_64bit:
offSrcParms->u.value64 = paDstParms[i].u.uint64;
break;
case VMMDevHGCMParmType_LinAddr:
{
offSrcParms->u.LinAddr.cb = paDstParms[i].u.pointer.size;
break;
}
default:
AssertFailed();
break;
}
offSrcParms++;
}
}
RTMemFree(paDstParms);
if (RT_SUCCESS(rc2))
rc2 = pSvc->aHgcmClient[pInfo->u32ClientID].hCall.rc;
return rc2;
}
#endif /* IN_RING3 */
#endif /* !VBOX_INCLUDED_GuestHost_HGCMMock_h */
|