/
vboxhgcm.c
2499 lines (2095 loc) · 71.2 KB
/
vboxhgcm.c
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Id: vboxhgcm.c $ */
/** @file
* VBox HGCM connection
*/
/*
* Copyright (C) 2008-2017 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#ifdef RT_OS_WINDOWS
# include <iprt/win/windows.h>
# include <ddraw.h>
#else
# include <sys/ioctl.h>
# include <errno.h>
# include <fcntl.h>
# include <string.h>
# include <unistd.h>
#endif
#include "cr_error.h"
#include "cr_net.h"
#include "cr_bufpool.h"
#include "cr_mem.h"
#include "cr_string.h"
#include "cr_endian.h"
#include "cr_threads.h"
#include "net_internals.h"
#include "cr_process.h"
#include <iprt/initterm.h>
#include <iprt/thread.h>
#include <VBox/VBoxGuestLib.h>
#include <VBox/HostServices/VBoxCrOpenGLSvc.h>
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
#include <VBoxCrHgsmi.h>
#endif
/** @todo move define somewhere else, and make sure it's less than VBGLR0_MAX_HGCM_KERNEL_PARM*/
/*If we fail to pass data in one chunk, send it in chunks of this size instead*/
#define CR_HGCM_SPLIT_BUFFER_SIZE (8*_1M)
#ifndef MIN
# define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
#ifdef DEBUG_misha
#ifdef CRASSERT
# undef CRASSERT
#endif
#define CRASSERT Assert
#endif
/*#define IN_GUEST
#if defined(IN_GUEST)
#define VBOX_WITH_CRHGSMIPROFILE
#endif */
#ifdef VBOX_WITH_CRHGSMIPROFILE
#include <iprt/time.h>
#include <stdio.h>
typedef struct VBOXCRHGSMIPROFILE
{
uint64_t cStartTime;
uint64_t cStepsTime;
uint64_t cSteps;
} VBOXCRHGSMIPROFILE, *PVBOXCRHGSMIPROFILE;
#define VBOXCRHGSMIPROFILE_GET_TIME_NANO() RTTimeNanoTS()
#define VBOXCRHGSMIPROFILE_GET_TIME_MILLI() RTTimeMilliTS()
/* 10 sec */
#define VBOXCRHGSMIPROFILE_LOG_STEP_TIME (10000000000.)
DECLINLINE(void) vboxCrHgsmiProfileStart(PVBOXCRHGSMIPROFILE pProfile)
{
pProfile->cStepsTime = 0;
pProfile->cSteps = 0;
pProfile->cStartTime = VBOXCRHGSMIPROFILE_GET_TIME_NANO();
}
DECLINLINE(void) vboxCrHgsmiProfileStep(PVBOXCRHGSMIPROFILE pProfile, uint64_t cStepTime)
{
pProfile->cStepsTime += cStepTime;
++pProfile->cSteps;
}
typedef struct VBOXCRHGSMIPROFILE_SCOPE
{
uint64_t cStartTime;
/* bool bDisable;*/
} VBOXCRHGSMIPROFILE_SCOPE, *PVBOXCRHGSMIPROFILE_SCOPE;
static VBOXCRHGSMIPROFILE g_VBoxProfile;
static void vboxCrHgsmiLog(char * szString, ...)
{
char szBuffer[4096] = {0};
va_list pArgList;
va_start(pArgList, szString);
_vsnprintf(szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0]), szString, pArgList);
va_end(pArgList);
#ifdef VBOX_WITH_CRHGSMI
VBoxCrHgsmiLog(szBuffer);
#else
OutputDebugString(szBuffer);
#endif
}
DECLINLINE(void) vboxCrHgsmiProfileLog(PVBOXCRHGSMIPROFILE pProfile, uint64_t cTime)
{
uint64_t profileTime = cTime - pProfile->cStartTime;
double percent = ((double)100.0) * pProfile->cStepsTime / profileTime;
double cps = ((double)1000000000.) * pProfile->cSteps / profileTime;
vboxCrHgsmiLog("hgcm: cps: %.1f, host %.1f%%\n", cps, percent);
}
DECLINLINE(void) vboxCrHgsmiProfileScopeEnter(PVBOXCRHGSMIPROFILE_SCOPE pScope)
{
/* pScope->bDisable = false; */
pScope->cStartTime = VBOXCRHGSMIPROFILE_GET_TIME_NANO();
}
DECLINLINE(void) vboxCrHgsmiProfileScopeExit(PVBOXCRHGSMIPROFILE_SCOPE pScope)
{
/* if (!pScope->bDisable) */
{
uint64_t cTime = VBOXCRHGSMIPROFILE_GET_TIME_NANO();
vboxCrHgsmiProfileStep(&g_VBoxProfile, cTime - pScope->cStartTime);
if (VBOXCRHGSMIPROFILE_LOG_STEP_TIME < cTime - g_VBoxProfile.cStartTime)
{
vboxCrHgsmiProfileLog(&g_VBoxProfile, cTime);
vboxCrHgsmiProfileStart(&g_VBoxProfile);
}
}
}
#define VBOXCRHGSMIPROFILE_INIT() vboxCrHgsmiProfileStart(&g_VBoxProfile)
#define VBOXCRHGSMIPROFILE_TERM() do {} while (0)
#define VBOXCRHGSMIPROFILE_FUNC_PROLOGUE() \
VBOXCRHGSMIPROFILE_SCOPE __vboxCrHgsmiProfileScope; \
vboxCrHgsmiProfileScopeEnter(&__vboxCrHgsmiProfileScope);
#define VBOXCRHGSMIPROFILE_FUNC_EPILOGUE() \
vboxCrHgsmiProfileScopeExit(&__vboxCrHgsmiProfileScope); \
#else
#define VBOXCRHGSMIPROFILE_INIT() do {} while (0)
#define VBOXCRHGSMIPROFILE_TERM() do {} while (0)
#define VBOXCRHGSMIPROFILE_FUNC_PROLOGUE() do {} while (0)
#define VBOXCRHGSMIPROFILE_FUNC_EPILOGUE() do {} while (0)
#endif
typedef struct {
int initialized;
int num_conns;
CRConnection **conns;
CRBufferPool *bufpool;
#ifdef CHROMIUM_THREADSAFE
CRmutex mutex;
CRmutex recvmutex;
#endif
CRNetReceiveFuncList *recv_list;
CRNetCloseFuncList *close_list;
#ifdef RT_OS_WINDOWS
LPDIRECTDRAW pDirectDraw;
#endif
#ifdef IN_GUEST
uint32_t u32HostCaps;
bool fHostCapsInitialized;
#endif
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
bool bHgsmiOn;
#endif
} CRVBOXHGCMDATA;
static CRVBOXHGCMDATA g_crvboxhgcm = {0,};
typedef enum {
CR_VBOXHGCM_USERALLOCATED,
CR_VBOXHGCM_MEMORY,
CR_VBOXHGCM_MEMORY_BIG
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
,CR_VBOXHGCM_UHGSMI_BUFFER
#endif
} CRVBOXHGCMBUFFERKIND;
#define CR_VBOXHGCM_BUFFER_MAGIC 0xABCDE321
typedef struct CRVBOXHGCMBUFFER {
uint32_t magic;
CRVBOXHGCMBUFFERKIND kind;
union
{
struct
{
uint32_t len;
uint32_t allocated;
};
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
PVBOXUHGSMI_BUFFER pBuffer;
#endif
};
} CRVBOXHGCMBUFFER;
#ifndef RT_OS_WINDOWS
#define TRUE true
#define FALSE false
#define INVALID_HANDLE_VALUE (-1)
#endif
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
/* add sizeof header + page align */
#define CRVBOXHGSMI_PAGE_ALIGN(_s) (((_s) + 0xfff) & ~0xfff)
#define CRVBOXHGSMI_BUF_HDR_SIZE() (sizeof (CRVBOXHGCMBUFFER))
#define CRVBOXHGSMI_BUF_SIZE(_s) CRVBOXHGSMI_PAGE_ALIGN((_s) + CRVBOXHGSMI_BUF_HDR_SIZE())
#define CRVBOXHGSMI_BUF_LOCK_SIZE(_s) ((_s) + CRVBOXHGSMI_BUF_HDR_SIZE())
#define CRVBOXHGSMI_BUF_DATA(_p) ((void*)(((CRVBOXHGCMBUFFER*)(_p)) + 1))
#define CRVBOXHGSMI_BUF_HDR(_p) (((CRVBOXHGCMBUFFER*)(_p)) - 1)
#define CRVBOXHGSMI_BUF_OFFSET(_st2, _st1) ((uint32_t)(((uint8_t*)(_st2)) - ((uint8_t*)(_st1))))
static int _crVBoxHGSMIClientInit(PCRVBOXHGSMI_CLIENT pClient, PVBOXUHGSMI pHgsmi)
{
int rc;
VBOXUHGSMI_BUFFER_TYPE_FLAGS Flags = {0};
pClient->pHgsmi = pHgsmi;
Flags.fCommand = 1;
rc = pHgsmi->pfnBufferCreate(pHgsmi, CRVBOXHGSMI_PAGE_ALIGN(1), Flags, &pClient->pCmdBuffer);
if (RT_SUCCESS(rc))
{
Flags.Value = 0;
rc = pHgsmi->pfnBufferCreate(pHgsmi, CRVBOXHGSMI_PAGE_ALIGN(1), Flags, &pClient->pHGBuffer);
if (RT_SUCCESS(rc))
{
pClient->pvHGBuffer = NULL;
pClient->bufpool = crBufferPoolInit(16);
return VINF_SUCCESS;
}
else
crWarning("_crVBoxHGSMIClientInit: pfnBufferCreate failed to allocate host->guest buffer");
pClient->pCmdBuffer->pfnDestroy(pClient->pCmdBuffer);
}
else
crWarning("_crVBoxHGSMIClientInit: pfnBufferCreate failed to allocate cmd buffer");
pClient->pHgsmi = NULL;
return rc;
}
void _crVBoxHGSMIBufferFree(void *data)
{
PVBOXUHGSMI_BUFFER pBuffer = (PVBOXUHGSMI_BUFFER)data;
pBuffer->pfnDestroy(pBuffer);
}
static int _crVBoxHGSMIClientTerm(PCRVBOXHGSMI_CLIENT pClient, PVBOXUHGSMI *ppHgsmi)
{
if (pClient->bufpool)
crBufferPoolCallbackFree(pClient->bufpool, _crVBoxHGSMIBufferFree);
pClient->bufpool = NULL;
if (pClient->pHGBuffer)
{
pClient->pHGBuffer->pfnDestroy(pClient->pHGBuffer);
pClient->pHGBuffer = NULL;
}
if (pClient->pCmdBuffer)
{
pClient->pCmdBuffer->pfnDestroy(pClient->pCmdBuffer);
pClient->pCmdBuffer = NULL;
}
if (ppHgsmi)
{
*ppHgsmi = pClient->pHgsmi;
}
pClient->pHgsmi = NULL;
return VINF_SUCCESS;
}
#ifdef VBOX_CRHGSMI_WITH_D3DDEV
static DECLCALLBACK(HVBOXCRHGSMI_CLIENT) _crVBoxHGSMIClientCreate(PVBOXUHGSMI pHgsmi)
{
PCRVBOXHGSMI_CLIENT pClient = crAlloc(sizeof (CRVBOXHGSMI_CLIENT));
if (pClient)
{
int rc = _crVBoxHGSMIClientInit(pClient, pHgsmi);
if (RT_SUCCESS(rc))
return (HVBOXCRHGSMI_CLIENT)pClient;
else
crWarning("_crVBoxHGSMIClientCreate: _crVBoxHGSMIClientInit failed rc %d", rc);
crFree(pCLient);
}
return NULL;
}
static DECLCALLBACK(void) _crVBoxHGSMIClientDestroy(HVBOXCRHGSMI_CLIENT hClient)
{
PCRVBOXHGSMI_CLIENT pClient = (PCRVBOXHGSMI_CLIENT)hClient;
_crVBoxHGSMIClientTerm(pClient, NULL);
crFree(pClient);
}
#endif
DECLINLINE(PCRVBOXHGSMI_CLIENT) _crVBoxHGSMIClientGet(CRConnection *conn)
{
#ifdef VBOX_CRHGSMI_WITH_D3DDEV
PCRVBOXHGSMI_CLIENT pClient = (PCRVBOXHGSMI_CLIENT)VBoxCrHgsmiQueryClient();
CRASSERT(pClient);
return pClient;
#else
if (conn->HgsmiClient.pHgsmi)
return &conn->HgsmiClient;
{
PVBOXUHGSMI pHgsmi = conn->pExternalHgsmi ? conn->pExternalHgsmi : VBoxCrHgsmiCreate();
if (pHgsmi)
{
int rc = _crVBoxHGSMIClientInit(&conn->HgsmiClient, pHgsmi);
if (RT_SUCCESS(rc))
{
CRASSERT(conn->HgsmiClient.pHgsmi);
return &conn->HgsmiClient;
}
else
crWarning("_crVBoxHGSMIClientGet: _crVBoxHGSMIClientInit failed rc %d", rc);
if (!conn->pExternalHgsmi)
VBoxCrHgsmiDestroy(pHgsmi);
}
else
{
crWarning("VBoxCrHgsmiCreate failed");
}
}
return NULL;
#endif
}
static PVBOXUHGSMI_BUFFER _crVBoxHGSMIBufAlloc(PCRVBOXHGSMI_CLIENT pClient, uint32_t cbSize)
{
PVBOXUHGSMI_BUFFER buf;
int rc;
buf = (PVBOXUHGSMI_BUFFER ) crBufferPoolPop(pClient->bufpool, cbSize);
if (!buf)
{
VBOXUHGSMI_BUFFER_TYPE_FLAGS Flags = {0};
crDebug("Buffer pool %p was empty; allocating new %d byte buffer.",
(void *) pClient->bufpool,
cbSize);
rc = pClient->pHgsmi->pfnBufferCreate(pClient->pHgsmi, cbSize, Flags, &buf);
if (RT_FAILURE(rc))
crWarning("_crVBoxHGSMIBufAlloc: Failed to Create a buffer of size(%d), rc(%d)\n", cbSize, rc);
}
return buf;
}
static PVBOXUHGSMI_BUFFER _crVBoxHGSMIBufFromHdr(CRVBOXHGCMBUFFER *pHdr)
{
PVBOXUHGSMI_BUFFER pBuf;
int rc;
CRASSERT(pHdr->magic == CR_VBOXHGCM_BUFFER_MAGIC);
CRASSERT(pHdr->kind == CR_VBOXHGCM_UHGSMI_BUFFER);
pBuf = pHdr->pBuffer;
rc = pBuf->pfnUnlock(pBuf);
if (RT_FAILURE(rc))
{
crWarning("_crVBoxHGSMIBufFromHdr: pfnUnlock failed rc %d", rc);
return NULL;
}
return pBuf;
}
static void _crVBoxHGSMIBufFree(PCRVBOXHGSMI_CLIENT pClient, PVBOXUHGSMI_BUFFER pBuf)
{
crBufferPoolPush(pClient->bufpool, pBuf, pBuf->cbBuffer);
}
static CRVBOXHGSMIHDR *_crVBoxHGSMICmdBufferLock(PCRVBOXHGSMI_CLIENT pClient, uint32_t cbBuffer)
{
/* in theory it is OK to use one cmd buffer for asynch cmd submission
* because bDiscard flag should result in allocating a new memory backend if the
* allocation is still in use.
* However, NOTE: since one and the same semaphore synch event is used for completion notification,
* for the notification mechanism working as expected
* 1. host must complete commands in the same order as it receives them
* (to avoid situation when guest receives notification for another command completion)
* 2. guest must eventually wait for command completion unless he specified bDoNotSignalCompletion
* 3. guest must wait for command completion in the same order as it submits them
* in case we can not satisfy any of the above, we should introduce multiple command buffers */
CRVBOXHGSMIHDR * pHdr;
VBOXUHGSMI_BUFFER_LOCK_FLAGS fFlags;
int rc;
fFlags.Value = 0;
fFlags.bDiscard = 1;
rc = pClient->pCmdBuffer->pfnLock(pClient->pCmdBuffer, 0, cbBuffer, fFlags, (void**)&pHdr);
if (RT_SUCCESS(rc))
return pHdr;
else
crWarning("_crVBoxHGSMICmdBufferLock: pfnLock failed rc %d", rc);
crWarning("Failed to Lock the command buffer of size(%d), rc(%d)\n", cbBuffer, rc);
return NULL;
}
static CRVBOXHGSMIHDR *_crVBoxHGSMICmdBufferLockRo(PCRVBOXHGSMI_CLIENT pClient, uint32_t cbBuffer)
{
/* in theory it is OK to use one cmd buffer for asynch cmd submission
* because bDiscard flag should result in allocating a new memory backend if the
* allocation is still in use.
* However, NOTE: since one and the same semaphore synch event is used for completion notification,
* for the notification mechanism working as expected
* 1. host must complete commands in the same order as it receives them
* (to avoid situation when guest receives notification for another command completion)
* 2. guest must eventually wait for command completion unless he specified bDoNotSignalCompletion
* 3. guest must wait for command completion in the same order as it submits them
* in case we can not satisfy any of the above, we should introduce multiple command buffers */
CRVBOXHGSMIHDR * pHdr = NULL;
VBOXUHGSMI_BUFFER_LOCK_FLAGS fFlags;
int rc;
fFlags.Value = 0;
fFlags.bReadOnly = 1;
rc = pClient->pCmdBuffer->pfnLock(pClient->pCmdBuffer, 0, cbBuffer, fFlags, (void**)&pHdr);
if (RT_FAILURE(rc))
crWarning("Failed to Lock the command buffer of size(%d), rc(%d)\n", cbBuffer, rc);
return pHdr;
}
static void _crVBoxHGSMICmdBufferUnlock(PCRVBOXHGSMI_CLIENT pClient)
{
int rc = pClient->pCmdBuffer->pfnUnlock(pClient->pCmdBuffer);
if (RT_FAILURE(rc))
crError("Failed to Unlock the command buffer rc(%d)\n", rc);
}
static int32_t _crVBoxHGSMICmdBufferGetRc(PCRVBOXHGSMI_CLIENT pClient)
{
CRVBOXHGSMIHDR * pHdr;
VBOXUHGSMI_BUFFER_LOCK_FLAGS fFlags;
int rc;
fFlags.Value = 0;
fFlags.bReadOnly = 1;
rc = pClient->pCmdBuffer->pfnLock(pClient->pCmdBuffer, 0, sizeof (*pHdr), fFlags, (void**)&pHdr);
if (RT_FAILURE(rc))
{
crWarning("Failed to Lock the command buffer of size(%d), rc(%d)\n", sizeof (*pHdr), rc);
return rc;
}
rc = pHdr->result;
AssertRC(rc);
pClient->pCmdBuffer->pfnUnlock(pClient->pCmdBuffer);
return rc;
}
DECLINLINE(PVBOXUHGSMI_BUFFER) _crVBoxHGSMIRecvBufGet(PCRVBOXHGSMI_CLIENT pClient)
{
if (pClient->pvHGBuffer)
{
int rc = pClient->pHGBuffer->pfnUnlock(pClient->pHGBuffer);
if (RT_FAILURE(rc))
{
return NULL;
}
pClient->pvHGBuffer = NULL;
}
return pClient->pHGBuffer;
}
DECLINLINE(void*) _crVBoxHGSMIRecvBufData(PCRVBOXHGSMI_CLIENT pClient, uint32_t cbBuffer)
{
VBOXUHGSMI_BUFFER_LOCK_FLAGS fFlags;
int rc;
CRASSERT(!pClient->pvHGBuffer);
fFlags.Value = 0;
rc = pClient->pHGBuffer->pfnLock(pClient->pHGBuffer, 0, cbBuffer, fFlags, &pClient->pvHGBuffer);
if (RT_SUCCESS(rc))
return pClient->pvHGBuffer;
else
crWarning("_crVBoxHGSMIRecvBufData: pfnLock failed rc %d", rc);
return NULL;
}
DECLINLINE(void) _crVBoxHGSMIFillCmd(VBOXUHGSMI_BUFFER_SUBMIT *pSubm, PCRVBOXHGSMI_CLIENT pClient, uint32_t cbData)
{
pSubm->pBuf = pClient->pCmdBuffer;
pSubm->offData = 0;
pSubm->cbData = cbData;
pSubm->fFlags.Value = 0;
pSubm->fFlags.bDoNotRetire = 1;
# if 0
pSubm->fFlags.bDoNotSignalCompletion = 1; /* <- we do not need that actually since
* in case we want completion,
* we will block in _crVBoxHGSMICmdBufferGetRc (when locking the buffer)
* which is needed for getting the result */
# endif
}
#endif
/* Some forward declarations */
static void _crVBoxHGCMReceiveMessage(CRConnection *conn);
#ifndef IN_GUEST
static bool _crVBoxHGCMReadBytes(CRConnection *conn, void *buf, uint32_t len)
{
CRASSERT(conn && buf);
if (!conn->pBuffer || (conn->cbBuffer<len))
return FALSE;
crMemcpy(buf, conn->pBuffer, len);
conn->cbBuffer -= len;
conn->pBuffer = conn->cbBuffer>0 ? (uint8_t*)conn->pBuffer+len : NULL;
return TRUE;
}
#endif
#ifndef IN_GUEST
/** @todo get rid of it*/
static bool _crVBoxHGCMWriteBytes(CRConnection *conn, const void *buf, uint32_t len)
{
CRASSERT(conn && buf);
/* make sure there's host buffer and it's clear */
CRASSERT(conn->pHostBuffer && !conn->cbHostBuffer);
if (conn->cbHostBufferAllocated < len)
{
crDebug("Host buffer too small %d out of requested %d bytes, reallocating", conn->cbHostBufferAllocated, len);
crFree(conn->pHostBuffer);
conn->pHostBuffer = crAlloc(len);
if (!conn->pHostBuffer)
{
conn->cbHostBufferAllocated = 0;
crError("OUT_OF_MEMORY trying to allocate %d bytes", len);
return FALSE;
}
conn->cbHostBufferAllocated = len;
}
crMemcpy(conn->pHostBuffer, buf, len);
conn->cbHostBuffer = len;
return TRUE;
}
#endif
/**
* Send an HGCM request
*
* @return VBox status code
* @param pvData Data pointer
* @param cbData Data size
*/
static int crVBoxHGCMCall(CRConnection *conn, PVBGLIOCHGCMCALL pData, unsigned cbData)
{
#ifdef IN_GUEST
int rc;
# ifndef VBOX_WITH_CRHGSMI
RT_NOREF(conn);
# else
PCRVBOXHGSMI_CLIENT pClient = g_crvboxhgcm.bHgsmiOn ? _crVBoxHGSMIClientGet(conn) : NULL;
if (pClient)
rc = VBoxCrHgsmiCtlConCall(pClient->pHgsmi, pData, cbData);
else
# endif
{
rc = VbglR3HGCMCall(pData, cbData);
if (RT_SUCCESS(rc))
{ /* likely */ }
else
{
crWarning("vboxCall failed with VBox status code %Rrc\n", rc);
# ifndef RT_OS_WINDOWS
if (rc == VERR_INTERRUPTED)
{
/* Not sure why we're doing the sleep stuff here. The original coder didn't
bother to mention why he thought it necessary. :-( */
RTMSINTERVAL msSleep;
int i;
for (i = 0, msSleep = 50; i < 6; i++, msSleep = msSleep * 2)
{
RTThreadSleep(msSleep);
rc = VbglR3HGCMCall(pData, cbData);
if (rc != VERR_INTERRUPTED)
{
if (RT_SUCCESS(rc))
crWarning("vboxCall retry(%i) succeeded", i + 1);
else
crWarning("vboxCall retry(%i) failed with VBox status code %Rrc", i + 1, rc);
break;
}
}
}
# endif
}
}
return rc;
#else /* IN_GUEST */
RT_NOREF(conn, pData, cbData);
crError("crVBoxHGCMCall called on host side!");
CRASSERT(FALSE);
return VERR_NOT_SUPPORTED;
#endif /* IN_GUEST */
}
static void *_crVBoxHGCMAlloc(CRConnection *conn)
{
CRVBOXHGCMBUFFER *buf;
#ifdef CHROMIUM_THREADSAFE
crLockMutex(&g_crvboxhgcm.mutex);
#endif
buf = (CRVBOXHGCMBUFFER *) crBufferPoolPop(g_crvboxhgcm.bufpool, conn->buffer_size);
if (!buf)
{
crDebug("Buffer pool %p was empty; allocating new %d byte buffer.",
(void *) g_crvboxhgcm.bufpool,
(unsigned int)sizeof(CRVBOXHGCMBUFFER) + conn->buffer_size);
/* We're either on host side, or we failed to allocate DDRAW buffer */
if (!buf)
{
crDebug("Using system malloc\n");
buf = (CRVBOXHGCMBUFFER *) crAlloc( sizeof(CRVBOXHGCMBUFFER) + conn->buffer_size );
CRASSERT(buf);
buf->magic = CR_VBOXHGCM_BUFFER_MAGIC;
buf->kind = CR_VBOXHGCM_MEMORY;
buf->allocated = conn->buffer_size;
}
}
#ifdef CHROMIUM_THREADSAFE
crUnlockMutex(&g_crvboxhgcm.mutex);
#endif
return (void *)( buf + 1 );
}
static void *crVBoxHGCMAlloc(CRConnection *conn)
{
void *pvBuff;
VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
#ifdef CHROMIUM_THREADSAFE
crLockMutex(&g_crvboxhgcm.mutex);
#endif
pvBuff = _crVBoxHGCMAlloc(conn);
#ifdef CHROMIUM_THREADSAFE
crUnlockMutex(&g_crvboxhgcm.mutex);
#endif
VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
return pvBuff;
}
static void _crVBoxHGCMWriteExact(CRConnection *conn, const void *buf, unsigned int len)
{
int rc;
int32_t callRes;
#ifdef IN_GUEST
if (conn->u32InjectClientID)
{
CRVBOXHGCMINJECT parms;
VBGL_HGCM_HDR_INIT(&parms.hdr, conn->u32ClientID, SHCRGL_GUEST_FN_INJECT, SHCRGL_CPARMS_INJECT);
parms.u32ClientID.type = VMMDevHGCMParmType_32bit;
parms.u32ClientID.u.value32 = conn->u32InjectClientID;
parms.pBuffer.type = VMMDevHGCMParmType_LinAddr_In;
parms.pBuffer.u.Pointer.size = len;
parms.pBuffer.u.Pointer.u.linearAddr = (uintptr_t) buf;
rc = crVBoxHGCMCall(conn, &parms.hdr, sizeof(parms));
callRes = parms.hdr.Hdr.rc; /** @todo now rc == parms.hdr.Hdr.rc */
}
else
#endif
{
CRVBOXHGCMWRITE parms;
VBGL_HGCM_HDR_INIT(&parms.hdr, conn->u32ClientID, SHCRGL_GUEST_FN_WRITE, SHCRGL_CPARMS_WRITE);
parms.pBuffer.type = VMMDevHGCMParmType_LinAddr_In;
parms.pBuffer.u.Pointer.size = len;
parms.pBuffer.u.Pointer.u.linearAddr = (uintptr_t) buf;
rc = crVBoxHGCMCall(conn, &parms.hdr, sizeof(parms));
callRes = parms.hdr.Hdr.rc; /** @todo now rc == parms.hdr.Hdr.rc */
}
if (RT_FAILURE(rc) || RT_FAILURE(callRes))
{
crWarning("SHCRGL_GUEST_FN_WRITE failed with %x %x\n", rc, callRes);
}
}
static void crVBoxHGCMWriteExact(CRConnection *conn, const void *buf, unsigned int len)
{
VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
#ifdef CHROMIUM_THREADSAFE
crLockMutex(&g_crvboxhgcm.mutex);
#endif
_crVBoxHGCMWriteExact(conn, buf, len);
#ifdef CHROMIUM_THREADSAFE
crUnlockMutex(&g_crvboxhgcm.mutex);
#endif
VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
}
static void crVBoxHGCMReadExact( CRConnection *conn, const void *buf, unsigned int len )
{
CRVBOXHGCMREAD parms;
int rc;
RT_NOREF(buf, len);
VBGL_HGCM_HDR_INIT(&parms.hdr, conn->u32ClientID, SHCRGL_GUEST_FN_READ, SHCRGL_CPARMS_READ);
CRASSERT(!conn->pBuffer); /* make sure there's no data to process */
parms.pBuffer.type = VMMDevHGCMParmType_LinAddr_Out;
parms.pBuffer.u.Pointer.size = conn->cbHostBufferAllocated;
parms.pBuffer.u.Pointer.u.linearAddr = (uintptr_t) conn->pHostBuffer;
parms.cbBuffer.type = VMMDevHGCMParmType_32bit;
parms.cbBuffer.u.value32 = 0;
rc = crVBoxHGCMCall(conn, &parms.hdr, sizeof(parms));
if (RT_FAILURE(rc) || RT_FAILURE(parms.hdr.Hdr.rc) /** @todo now rc == parms.hdr.Hdr.rc */)
{
crWarning("SHCRGL_GUEST_FN_READ failed with %x %x\n", rc, parms.hdr.Hdr.rc);
return;
}
if (parms.cbBuffer.u.value32)
{
/*conn->pBuffer = (uint8_t*) parms.pBuffer.u.Pointer.u.linearAddr; */
conn->pBuffer = conn->pHostBuffer;
conn->cbBuffer = parms.cbBuffer.u.value32;
}
if (conn->cbBuffer)
_crVBoxHGCMReceiveMessage(conn);
}
/* Same as crVBoxHGCMWriteExact, but combined with read of writeback data.
* This halves the number of HGCM calls we do,
* most likely crVBoxHGCMPollHost shouldn't be called at all now.
*/
static void
crVBoxHGCMWriteReadExact(CRConnection *conn, const void *buf, unsigned int len, CRVBOXHGCMBUFFERKIND bufferKind)
{
CRVBOXHGCMWRITEREAD parms;
int rc;
VBGL_HGCM_HDR_INIT(&parms.hdr, conn->u32ClientID, SHCRGL_GUEST_FN_WRITE_READ, SHCRGL_CPARMS_WRITE_READ);
parms.pBuffer.type = VMMDevHGCMParmType_LinAddr_In;
parms.pBuffer.u.Pointer.size = len;
parms.pBuffer.u.Pointer.u.linearAddr = (uintptr_t) buf;
CRASSERT(!conn->pBuffer); /*make sure there's no data to process*/
parms.pWriteback.type = VMMDevHGCMParmType_LinAddr_Out;
parms.pWriteback.u.Pointer.size = conn->cbHostBufferAllocated;
parms.pWriteback.u.Pointer.u.linearAddr = (uintptr_t) conn->pHostBuffer;
parms.cbWriteback.type = VMMDevHGCMParmType_32bit;
parms.cbWriteback.u.value32 = 0;
rc = crVBoxHGCMCall(conn, &parms.hdr, sizeof(parms));
#if defined(RT_OS_LINUX) || defined(RT_OS_WINDOWS)
if (VERR_OUT_OF_RANGE==rc && CR_VBOXHGCM_USERALLOCATED==bufferKind)
{
/*Buffer is too big, so send it in split chunks*/
CRVBOXHGCMWRITEBUFFER wbParms;
VBGL_HGCM_HDR_INIT(&wbParms.hdr, conn->u32ClientID, SHCRGL_GUEST_FN_WRITE_BUFFER, SHCRGL_CPARMS_WRITE_BUFFER);
wbParms.iBufferID.type = VMMDevHGCMParmType_32bit;
wbParms.iBufferID.u.value32 = 0;
wbParms.cbBufferSize.type = VMMDevHGCMParmType_32bit;
wbParms.cbBufferSize.u.value32 = len;
wbParms.ui32Offset.type = VMMDevHGCMParmType_32bit;
wbParms.ui32Offset.u.value32 = 0;
wbParms.pBuffer.type = VMMDevHGCMParmType_LinAddr_In;
wbParms.pBuffer.u.Pointer.size = MIN(CR_HGCM_SPLIT_BUFFER_SIZE, len);
wbParms.pBuffer.u.Pointer.u.linearAddr = (uintptr_t) buf;
if (len<CR_HGCM_SPLIT_BUFFER_SIZE)
{
crError("VERR_OUT_OF_RANGE in crVBoxHGCMWriteReadExact for %u bytes write", len);
return;
}
while (wbParms.pBuffer.u.Pointer.size)
{
crDebug("SHCRGL_GUEST_FN_WRITE_BUFFER, offset=%u, size=%u", wbParms.ui32Offset.u.value32, wbParms.pBuffer.u.Pointer.size);
rc = crVBoxHGCMCall(conn, &wbParms.hdr, sizeof(wbParms));
if (RT_FAILURE(rc) || RT_FAILURE(wbParms.hdr.Hdr.rc) /** @todo now rc == wbParms.hdr.Hdr.rc */)
{
crError("SHCRGL_GUEST_FN_WRITE_BUFFER (%i) failed with %x %x\n", wbParms.pBuffer.u.Pointer.size, rc, wbParms.hdr.Hdr.rc);
return;
}
wbParms.ui32Offset.u.value32 += wbParms.pBuffer.u.Pointer.size;
wbParms.pBuffer.u.Pointer.u.linearAddr += (uintptr_t) wbParms.pBuffer.u.Pointer.size;
wbParms.pBuffer.u.Pointer.size = MIN(CR_HGCM_SPLIT_BUFFER_SIZE, len-wbParms.ui32Offset.u.value32);
}
/*now issue GUEST_FN_WRITE_READ_BUFFERED referencing the buffer we'd made*/
{
CRVBOXHGCMWRITEREADBUFFERED wrbParms;
VBGL_HGCM_HDR_INIT(&wrbParms.hdr, conn->u32ClientID, SHCRGL_GUEST_FN_WRITE_READ_BUFFERED, SHCRGL_CPARMS_WRITE_READ_BUFFERED);
crMemcpy(&wrbParms.iBufferID, &wbParms.iBufferID, sizeof(HGCMFunctionParameter));
crMemcpy(&wrbParms.pWriteback, &parms.pWriteback, sizeof(HGCMFunctionParameter));
crMemcpy(&wrbParms.cbWriteback, &parms.cbWriteback, sizeof(HGCMFunctionParameter));
rc = crVBoxHGCMCall(conn, &wrbParms.hdr, sizeof(wrbParms));
/*bit of hack to reuse code below*/
parms.hdr.Hdr.rc = wrbParms.hdr.Hdr.rc;
crMemcpy(&parms.cbWriteback, &wrbParms.cbWriteback, sizeof(HGCMFunctionParameter));
crMemcpy(&parms.pWriteback, &wrbParms.pWriteback, sizeof(HGCMFunctionParameter));
}
}
#endif
if (RT_FAILURE(rc) || RT_FAILURE(parms.hdr.Hdr.rc) /** @todo now rc == parms.hdr.Hdr.rc */)
{
if ((VERR_BUFFER_OVERFLOW == parms.hdr.Hdr.rc) /* && RT_SUCCESS(rc) */)
{
/* reallocate buffer and retry */
CRASSERT(parms.cbWriteback.u.value32>conn->cbHostBufferAllocated);
crDebug("Reallocating host buffer from %d to %d bytes", conn->cbHostBufferAllocated, parms.cbWriteback.u.value32);
crFree(conn->pHostBuffer);
conn->cbHostBufferAllocated = parms.cbWriteback.u.value32;
conn->pHostBuffer = crAlloc(conn->cbHostBufferAllocated);
crVBoxHGCMReadExact(conn, buf, len);
return;
}
else
{
crWarning("SHCRGL_GUEST_FN_WRITE_READ (%i) failed with %x %x\n", len, rc, parms.hdr.Hdr.rc);
return;
}
}
if (parms.cbWriteback.u.value32)
{
/*conn->pBuffer = (uint8_t*) parms.pWriteback.u.Pointer.u.linearAddr;*/
conn->pBuffer = conn->pHostBuffer;
conn->cbBuffer = parms.cbWriteback.u.value32;
}
if (conn->cbBuffer)
_crVBoxHGCMReceiveMessage(conn);
}
static void crVBoxHGCMSend(CRConnection *conn, void **bufp,
const void *start, unsigned int len)
{
CRVBOXHGCMBUFFER *hgcm_buffer;
VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
#ifdef CHROMIUM_THREADSAFE
crLockMutex(&g_crvboxhgcm.mutex);
#endif
if (!bufp) /* We're sending a user-allocated buffer. */
{
#ifndef IN_GUEST
/** @todo remove temp buffer allocation in unpacker*/
/* we're at the host side, so just store data until guest polls us */
_crVBoxHGCMWriteBytes(conn, start, len);
#else
CRASSERT(!conn->u32InjectClientID);
crDebug("SHCRGL: sending userbuf with %d bytes\n", len);
crVBoxHGCMWriteReadExact(conn, start, len, CR_VBOXHGCM_USERALLOCATED);
#endif
#ifdef CHROMIUM_THREADSAFE
crUnlockMutex(&g_crvboxhgcm.mutex);
#endif
VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
return;
}
/* The region [start .. start + len + 1] lies within a buffer that
* was allocated with crVBoxHGCMAlloc() and can be put into the free
* buffer pool when we're done sending it.
*/
hgcm_buffer = (CRVBOXHGCMBUFFER *)(*bufp) - 1;
CRASSERT(hgcm_buffer->magic == CR_VBOXHGCM_BUFFER_MAGIC);
/* Length would be passed as part of HGCM pointer description
* No need to prepend it to the buffer
*/
#ifdef IN_GUEST
if (conn->u32InjectClientID)
{
_crVBoxHGCMWriteExact(conn, start, len);
}
else
#endif
crVBoxHGCMWriteReadExact(conn, start, len, hgcm_buffer->kind);
/* Reclaim this pointer for reuse */
#ifdef CHROMIUM_THREADSAFE
crLockMutex(&g_crvboxhgcm.mutex);
#endif
crBufferPoolPush(g_crvboxhgcm.bufpool, hgcm_buffer, hgcm_buffer->allocated);
#ifdef CHROMIUM_THREADSAFE
crUnlockMutex(&g_crvboxhgcm.mutex);
#endif
/* Since the buffer's now in the 'free' buffer pool, the caller can't
* use it any more. Setting bufp to NULL will make sure the caller
* doesn't try to re-use the buffer.
*/
*bufp = NULL;
#ifdef CHROMIUM_THREADSAFE
crUnlockMutex(&g_crvboxhgcm.mutex);
#endif
VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
}
static void crVBoxHGCMPollHost(CRConnection *conn)
{
CRVBOXHGCMREAD parms;
int rc;
CRASSERT(!conn->pBuffer);
VBGL_HGCM_HDR_INIT(&parms.hdr, conn->u32ClientID, SHCRGL_GUEST_FN_READ, SHCRGL_CPARMS_READ);
parms.pBuffer.type = VMMDevHGCMParmType_LinAddr_Out;
parms.pBuffer.u.Pointer.size = conn->cbHostBufferAllocated;
parms.pBuffer.u.Pointer.u.linearAddr = (uintptr_t) conn->pHostBuffer;
parms.cbBuffer.type = VMMDevHGCMParmType_32bit;
parms.cbBuffer.u.value32 = 0;
rc = crVBoxHGCMCall(conn, &parms.hdr, sizeof(parms));
if (RT_FAILURE(rc) || RT_FAILURE(parms.hdr.Hdr.rc) /** @todo now rc == parms.hdr.Hdr.rc */)
{
crDebug("SHCRGL_GUEST_FN_READ failed with %x %x\n", rc, parms.hdr.Hdr.rc);
return;
}
if (parms.cbBuffer.u.value32)