VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/util/vboxhgcm.c@ 78292

Last change on this file since 78292 was 78263, checked in by vboxsync, 6 years ago

Config.kmk,GuestHost\OpenGL,HostServices\SharedOpenGL: Fix a bunch of compiler warnings and enable them again

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 71.0 KB
Line 
1/* $Id: vboxhgcm.c 78263 2019-04-23 18:41:06Z vboxsync $ */
2/** @file
3 * VBox HGCM connection
4 */
5
6/*
7 * Copyright (C) 2008-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifdef RT_OS_WINDOWS
19# include <iprt/win/windows.h>
20# include <ddraw.h>
21#else
22# include <sys/ioctl.h>
23# include <errno.h>
24# include <fcntl.h>
25# include <string.h>
26# include <unistd.h>
27#endif
28
29#include "cr_error.h"
30#include "cr_net.h"
31#include "cr_bufpool.h"
32#include "cr_mem.h"
33#include "cr_string.h"
34#include "cr_threads.h"
35#include "net_internals.h"
36#include "cr_process.h"
37
38#include <iprt/initterm.h>
39#include <iprt/thread.h>
40
41#include <VBox/VBoxGuestLib.h>
42#include <VBox/HostServices/VBoxCrOpenGLSvc.h>
43
44#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
45#include <VBoxCrHgsmi.h>
46#endif
47
48/** @todo move define somewhere else, and make sure it's less than VBGLR0_MAX_HGCM_KERNEL_PARM*/
49/*If we fail to pass data in one chunk, send it in chunks of this size instead*/
50#define CR_HGCM_SPLIT_BUFFER_SIZE (8*_1M)
51
52#ifndef MIN
53# define MIN(a, b) ((a) < (b) ? (a) : (b))
54#endif
55
56#ifdef DEBUG_misha
57#ifdef CRASSERT
58# undef CRASSERT
59#endif
60#define CRASSERT Assert
61#endif
62/*#define IN_GUEST
63#if defined(IN_GUEST)
64#define VBOX_WITH_CRHGSMIPROFILE
65#endif */
66#ifdef VBOX_WITH_CRHGSMIPROFILE
67#include <iprt/time.h>
68#include <stdio.h>
69
70typedef struct VBOXCRHGSMIPROFILE
71{
72 uint64_t cStartTime;
73 uint64_t cStepsTime;
74 uint64_t cSteps;
75} VBOXCRHGSMIPROFILE, *PVBOXCRHGSMIPROFILE;
76
77#define VBOXCRHGSMIPROFILE_GET_TIME_NANO() RTTimeNanoTS()
78#define VBOXCRHGSMIPROFILE_GET_TIME_MILLI() RTTimeMilliTS()
79
80/* 10 sec */
81#define VBOXCRHGSMIPROFILE_LOG_STEP_TIME (10000000000.)
82
83DECLINLINE(void) vboxCrHgsmiProfileStart(PVBOXCRHGSMIPROFILE pProfile)
84{
85 pProfile->cStepsTime = 0;
86 pProfile->cSteps = 0;
87 pProfile->cStartTime = VBOXCRHGSMIPROFILE_GET_TIME_NANO();
88}
89
90DECLINLINE(void) vboxCrHgsmiProfileStep(PVBOXCRHGSMIPROFILE pProfile, uint64_t cStepTime)
91{
92 pProfile->cStepsTime += cStepTime;
93 ++pProfile->cSteps;
94}
95
96typedef struct VBOXCRHGSMIPROFILE_SCOPE
97{
98 uint64_t cStartTime;
99/* bool bDisable;*/
100} VBOXCRHGSMIPROFILE_SCOPE, *PVBOXCRHGSMIPROFILE_SCOPE;
101
102static VBOXCRHGSMIPROFILE g_VBoxProfile;
103
104static void vboxCrHgsmiLog(char * szString, ...)
105{
106 char szBuffer[4096] = {0};
107 va_list pArgList;
108 va_start(pArgList, szString);
109 _vsnprintf(szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0]), szString, pArgList);
110 va_end(pArgList);
111
112#ifdef VBOX_WITH_CRHGSMI
113 VBoxCrHgsmiLog(szBuffer);
114#else
115 OutputDebugString(szBuffer);
116#endif
117}
118
119DECLINLINE(void) vboxCrHgsmiProfileLog(PVBOXCRHGSMIPROFILE pProfile, uint64_t cTime)
120{
121 uint64_t profileTime = cTime - pProfile->cStartTime;
122 double percent = ((double)100.0) * pProfile->cStepsTime / profileTime;
123 double cps = ((double)1000000000.) * pProfile->cSteps / profileTime;
124 vboxCrHgsmiLog("hgcm: cps: %.1f, host %.1f%%\n", cps, percent);
125}
126
127DECLINLINE(void) vboxCrHgsmiProfileScopeEnter(PVBOXCRHGSMIPROFILE_SCOPE pScope)
128{
129/* pScope->bDisable = false; */
130 pScope->cStartTime = VBOXCRHGSMIPROFILE_GET_TIME_NANO();
131}
132
133DECLINLINE(void) vboxCrHgsmiProfileScopeExit(PVBOXCRHGSMIPROFILE_SCOPE pScope)
134{
135/* if (!pScope->bDisable) */
136 {
137 uint64_t cTime = VBOXCRHGSMIPROFILE_GET_TIME_NANO();
138 vboxCrHgsmiProfileStep(&g_VBoxProfile, cTime - pScope->cStartTime);
139 if (VBOXCRHGSMIPROFILE_LOG_STEP_TIME < cTime - g_VBoxProfile.cStartTime)
140 {
141 vboxCrHgsmiProfileLog(&g_VBoxProfile, cTime);
142 vboxCrHgsmiProfileStart(&g_VBoxProfile);
143 }
144 }
145}
146
147
148#define VBOXCRHGSMIPROFILE_INIT() vboxCrHgsmiProfileStart(&g_VBoxProfile)
149#define VBOXCRHGSMIPROFILE_TERM() do {} while (0)
150
151#define VBOXCRHGSMIPROFILE_FUNC_PROLOGUE() \
152 VBOXCRHGSMIPROFILE_SCOPE __vboxCrHgsmiProfileScope; \
153 vboxCrHgsmiProfileScopeEnter(&__vboxCrHgsmiProfileScope);
154
155#define VBOXCRHGSMIPROFILE_FUNC_EPILOGUE() \
156 vboxCrHgsmiProfileScopeExit(&__vboxCrHgsmiProfileScope); \
157
158
159#else
160#define VBOXCRHGSMIPROFILE_INIT() do {} while (0)
161#define VBOXCRHGSMIPROFILE_TERM() do {} while (0)
162#define VBOXCRHGSMIPROFILE_FUNC_PROLOGUE() do {} while (0)
163#define VBOXCRHGSMIPROFILE_FUNC_EPILOGUE() do {} while (0)
164#endif
165
166typedef struct {
167 int initialized;
168 int num_conns;
169 CRConnection **conns;
170 CRBufferPool *bufpool;
171#ifdef CHROMIUM_THREADSAFE
172 CRmutex mutex;
173 CRmutex recvmutex;
174#endif
175 CRNetReceiveFuncList *recv_list;
176 CRNetCloseFuncList *close_list;
177#ifdef RT_OS_WINDOWS
178 LPDIRECTDRAW pDirectDraw;
179#endif
180#ifdef IN_GUEST
181 uint32_t u32HostCaps;
182 bool fHostCapsInitialized;
183#endif
184#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
185 bool bHgsmiOn;
186#endif
187} CRVBOXHGCMDATA;
188
189static CRVBOXHGCMDATA g_crvboxhgcm = {0,};
190
191typedef enum {
192 CR_VBOXHGCM_USERALLOCATED,
193 CR_VBOXHGCM_MEMORY,
194 CR_VBOXHGCM_MEMORY_BIG
195#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
196 ,CR_VBOXHGCM_UHGSMI_BUFFER
197#endif
198} CRVBOXHGCMBUFFERKIND;
199
200#define CR_VBOXHGCM_BUFFER_MAGIC 0xABCDE321
201
202typedef struct CRVBOXHGCMBUFFER {
203 uint32_t magic;
204 CRVBOXHGCMBUFFERKIND kind;
205 union
206 {
207 struct
208 {
209 uint32_t len;
210 uint32_t allocated;
211 };
212
213#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
214 PVBOXUHGSMI_BUFFER pBuffer;
215#endif
216 };
217} CRVBOXHGCMBUFFER;
218
219#ifndef RT_OS_WINDOWS
220 #define TRUE true
221 #define FALSE false
222 #define INVALID_HANDLE_VALUE (-1)
223#endif
224
225
226#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
227
228/* add sizeof header + page align */
229#define CRVBOXHGSMI_PAGE_ALIGN(_s) (((_s) + 0xfff) & ~0xfff)
230#define CRVBOXHGSMI_BUF_HDR_SIZE() (sizeof (CRVBOXHGCMBUFFER))
231#define CRVBOXHGSMI_BUF_SIZE(_s) CRVBOXHGSMI_PAGE_ALIGN((_s) + CRVBOXHGSMI_BUF_HDR_SIZE())
232#define CRVBOXHGSMI_BUF_LOCK_SIZE(_s) ((_s) + CRVBOXHGSMI_BUF_HDR_SIZE())
233#define CRVBOXHGSMI_BUF_DATA(_p) ((void*)(((CRVBOXHGCMBUFFER*)(_p)) + 1))
234#define CRVBOXHGSMI_BUF_HDR(_p) (((CRVBOXHGCMBUFFER*)(_p)) - 1)
235#define CRVBOXHGSMI_BUF_OFFSET(_st2, _st1) ((uint32_t)(((uint8_t*)(_st2)) - ((uint8_t*)(_st1))))
236
237static int _crVBoxHGSMIClientInit(PCRVBOXHGSMI_CLIENT pClient, PVBOXUHGSMI pHgsmi)
238{
239 int rc;
240 VBOXUHGSMI_BUFFER_TYPE_FLAGS Flags = {0};
241 pClient->pHgsmi = pHgsmi;
242 Flags.s.fCommand = 1;
243 rc = pHgsmi->pfnBufferCreate(pHgsmi, CRVBOXHGSMI_PAGE_ALIGN(1), Flags, &pClient->pCmdBuffer);
244 if (RT_SUCCESS(rc))
245 {
246 Flags.Value = 0;
247 rc = pHgsmi->pfnBufferCreate(pHgsmi, CRVBOXHGSMI_PAGE_ALIGN(1), Flags, &pClient->pHGBuffer);
248 if (RT_SUCCESS(rc))
249 {
250 pClient->pvHGBuffer = NULL;
251 pClient->bufpool = crBufferPoolInit(16);
252 return VINF_SUCCESS;
253 }
254 else
255 crWarning("_crVBoxHGSMIClientInit: pfnBufferCreate failed to allocate host->guest buffer");
256
257 pClient->pCmdBuffer->pfnDestroy(pClient->pCmdBuffer);
258 }
259 else
260 crWarning("_crVBoxHGSMIClientInit: pfnBufferCreate failed to allocate cmd buffer");
261
262 pClient->pHgsmi = NULL;
263 return rc;
264}
265
266void _crVBoxHGSMIBufferFree(void *data)
267{
268 PVBOXUHGSMI_BUFFER pBuffer = (PVBOXUHGSMI_BUFFER)data;
269 pBuffer->pfnDestroy(pBuffer);
270}
271
272static int _crVBoxHGSMIClientTerm(PCRVBOXHGSMI_CLIENT pClient, PVBOXUHGSMI *ppHgsmi)
273{
274 if (pClient->bufpool)
275 crBufferPoolCallbackFree(pClient->bufpool, _crVBoxHGSMIBufferFree);
276 pClient->bufpool = NULL;
277
278 if (pClient->pHGBuffer)
279 {
280 pClient->pHGBuffer->pfnDestroy(pClient->pHGBuffer);
281 pClient->pHGBuffer = NULL;
282 }
283
284 if (pClient->pCmdBuffer)
285 {
286 pClient->pCmdBuffer->pfnDestroy(pClient->pCmdBuffer);
287 pClient->pCmdBuffer = NULL;
288 }
289
290 if (ppHgsmi)
291 {
292 *ppHgsmi = pClient->pHgsmi;
293 }
294 pClient->pHgsmi = NULL;
295
296 return VINF_SUCCESS;
297}
298
299
300#ifdef VBOX_CRHGSMI_WITH_D3DDEV
301
302static DECLCALLBACK(HVBOXCRHGSMI_CLIENT) _crVBoxHGSMIClientCreate(PVBOXUHGSMI pHgsmi)
303{
304 PCRVBOXHGSMI_CLIENT pClient = crAlloc(sizeof (CRVBOXHGSMI_CLIENT));
305
306 if (pClient)
307 {
308 int rc = _crVBoxHGSMIClientInit(pClient, pHgsmi);
309 if (RT_SUCCESS(rc))
310 return (HVBOXCRHGSMI_CLIENT)pClient;
311 else
312 crWarning("_crVBoxHGSMIClientCreate: _crVBoxHGSMIClientInit failed rc %d", rc);
313
314 crFree(pCLient);
315 }
316
317 return NULL;
318}
319
320static DECLCALLBACK(void) _crVBoxHGSMIClientDestroy(HVBOXCRHGSMI_CLIENT hClient)
321{
322 PCRVBOXHGSMI_CLIENT pClient = (PCRVBOXHGSMI_CLIENT)hClient;
323 _crVBoxHGSMIClientTerm(pClient, NULL);
324 crFree(pClient);
325}
326#endif
327
328DECLINLINE(PCRVBOXHGSMI_CLIENT) _crVBoxHGSMIClientGet(CRConnection *conn)
329{
330#ifdef VBOX_CRHGSMI_WITH_D3DDEV
331 PCRVBOXHGSMI_CLIENT pClient = (PCRVBOXHGSMI_CLIENT)VBoxCrHgsmiQueryClient();
332 CRASSERT(pClient);
333 return pClient;
334#else
335 if (conn->HgsmiClient.pHgsmi)
336 return &conn->HgsmiClient;
337 {
338 PVBOXUHGSMI pHgsmi = conn->pExternalHgsmi ? conn->pExternalHgsmi : VBoxCrHgsmiCreate();
339 if (pHgsmi)
340 {
341 int rc = _crVBoxHGSMIClientInit(&conn->HgsmiClient, pHgsmi);
342 if (RT_SUCCESS(rc))
343 {
344 CRASSERT(conn->HgsmiClient.pHgsmi);
345 return &conn->HgsmiClient;
346 }
347 else
348 crWarning("_crVBoxHGSMIClientGet: _crVBoxHGSMIClientInit failed rc %d", rc);
349 if (!conn->pExternalHgsmi)
350 VBoxCrHgsmiDestroy(pHgsmi);
351 }
352 else
353 {
354 crWarning("VBoxCrHgsmiCreate failed");
355 }
356 }
357 return NULL;
358#endif
359}
360
361static PVBOXUHGSMI_BUFFER _crVBoxHGSMIBufAlloc(PCRVBOXHGSMI_CLIENT pClient, uint32_t cbSize)
362{
363 PVBOXUHGSMI_BUFFER buf;
364 int rc;
365
366 buf = (PVBOXUHGSMI_BUFFER ) crBufferPoolPop(pClient->bufpool, cbSize);
367
368 if (!buf)
369 {
370 VBOXUHGSMI_BUFFER_TYPE_FLAGS Flags = {0};
371 crDebug("Buffer pool %p was empty; allocating new %d byte buffer.",
372 (void *) pClient->bufpool,
373 cbSize);
374 rc = pClient->pHgsmi->pfnBufferCreate(pClient->pHgsmi, cbSize, Flags, &buf);
375 if (RT_FAILURE(rc))
376 crWarning("_crVBoxHGSMIBufAlloc: Failed to Create a buffer of size(%d), rc(%d)\n", cbSize, rc);
377 }
378 return buf;
379}
380
381static PVBOXUHGSMI_BUFFER _crVBoxHGSMIBufFromHdr(CRVBOXHGCMBUFFER *pHdr)
382{
383 PVBOXUHGSMI_BUFFER pBuf;
384 int rc;
385 CRASSERT(pHdr->magic == CR_VBOXHGCM_BUFFER_MAGIC);
386 CRASSERT(pHdr->kind == CR_VBOXHGCM_UHGSMI_BUFFER);
387 pBuf = pHdr->pBuffer;
388 rc = pBuf->pfnUnlock(pBuf);
389 if (RT_FAILURE(rc))
390 {
391 crWarning("_crVBoxHGSMIBufFromHdr: pfnUnlock failed rc %d", rc);
392 return NULL;
393 }
394 return pBuf;
395}
396
397static void _crVBoxHGSMIBufFree(PCRVBOXHGSMI_CLIENT pClient, PVBOXUHGSMI_BUFFER pBuf)
398{
399 crBufferPoolPush(pClient->bufpool, pBuf, pBuf->cbBuffer);
400}
401
402static CRVBOXHGSMIHDR *_crVBoxHGSMICmdBufferLock(PCRVBOXHGSMI_CLIENT pClient, uint32_t cbBuffer)
403{
404 /* in theory it is OK to use one cmd buffer for asynch cmd submission
405 * because bDiscard flag should result in allocating a new memory backend if the
406 * allocation is still in use.
407 * However, NOTE: since one and the same semaphore synch event is used for completion notification,
408 * for the notification mechanism working as expected
409 * 1. host must complete commands in the same order as it receives them
410 * (to avoid situation when guest receives notification for another command completion)
411 * 2. guest must eventually wait for command completion unless he specified bDoNotSignalCompletion
412 * 3. guest must wait for command completion in the same order as it submits them
413 * in case we can not satisfy any of the above, we should introduce multiple command buffers */
414 CRVBOXHGSMIHDR * pHdr;
415 VBOXUHGSMI_BUFFER_LOCK_FLAGS fFlags;
416 int rc;
417 fFlags.Value = 0;
418 fFlags.s.fDiscard = 1;
419 rc = pClient->pCmdBuffer->pfnLock(pClient->pCmdBuffer, 0, cbBuffer, fFlags, (void**)&pHdr);
420 if (RT_SUCCESS(rc))
421 return pHdr;
422 crWarning("_crVBoxHGSMICmdBufferLock: pfnLock failed rc %d", rc);
423
424 crWarning("Failed to Lock the command buffer of size(%d), rc(%d)\n", cbBuffer, rc);
425 return NULL;
426}
427
428static CRVBOXHGSMIHDR *_crVBoxHGSMICmdBufferLockRo(PCRVBOXHGSMI_CLIENT pClient, uint32_t cbBuffer)
429{
430 /* in theory it is OK to use one cmd buffer for asynch cmd submission
431 * because bDiscard flag should result in allocating a new memory backend if the
432 * allocation is still in use.
433 * However, NOTE: since one and the same semaphore synch event is used for completion notification,
434 * for the notification mechanism working as expected
435 * 1. host must complete commands in the same order as it receives them
436 * (to avoid situation when guest receives notification for another command completion)
437 * 2. guest must eventually wait for command completion unless he specified bDoNotSignalCompletion
438 * 3. guest must wait for command completion in the same order as it submits them
439 * in case we can not satisfy any of the above, we should introduce multiple command buffers */
440 CRVBOXHGSMIHDR * pHdr = NULL;
441 VBOXUHGSMI_BUFFER_LOCK_FLAGS fFlags;
442 int rc;
443 fFlags.Value = 0;
444 fFlags.s.fReadOnly = 1;
445 rc = pClient->pCmdBuffer->pfnLock(pClient->pCmdBuffer, 0, cbBuffer, fFlags, (void**)&pHdr);
446 if (RT_FAILURE(rc))
447 crWarning("Failed to Lock the command buffer of size(%d), rc(%d)\n", cbBuffer, rc);
448 return pHdr;
449}
450
451static void _crVBoxHGSMICmdBufferUnlock(PCRVBOXHGSMI_CLIENT pClient)
452{
453 int rc = pClient->pCmdBuffer->pfnUnlock(pClient->pCmdBuffer);
454 if (RT_FAILURE(rc))
455 crError("Failed to Unlock the command buffer rc(%d)\n", rc);
456}
457
458static int32_t _crVBoxHGSMICmdBufferGetRc(PCRVBOXHGSMI_CLIENT pClient)
459{
460 CRVBOXHGSMIHDR * pHdr;
461 VBOXUHGSMI_BUFFER_LOCK_FLAGS fFlags;
462 int rc;
463
464 fFlags.Value = 0;
465 fFlags.s.fReadOnly = 1;
466 rc = pClient->pCmdBuffer->pfnLock(pClient->pCmdBuffer, 0, sizeof (*pHdr), fFlags, (void**)&pHdr);
467 if (RT_FAILURE(rc))
468 {
469 crWarning("Failed to Lock the command buffer of size(%d), rc(%d)\n", sizeof (*pHdr), rc);
470 return rc;
471 }
472
473 rc = pHdr->result;
474 AssertRC(rc);
475 pClient->pCmdBuffer->pfnUnlock(pClient->pCmdBuffer);
476
477 return rc;
478}
479
480DECLINLINE(PVBOXUHGSMI_BUFFER) _crVBoxHGSMIRecvBufGet(PCRVBOXHGSMI_CLIENT pClient)
481{
482 if (pClient->pvHGBuffer)
483 {
484 int rc = pClient->pHGBuffer->pfnUnlock(pClient->pHGBuffer);
485 if (RT_FAILURE(rc))
486 {
487 return NULL;
488 }
489 pClient->pvHGBuffer = NULL;
490 }
491 return pClient->pHGBuffer;
492}
493
494DECLINLINE(void*) _crVBoxHGSMIRecvBufData(PCRVBOXHGSMI_CLIENT pClient, uint32_t cbBuffer)
495{
496 VBOXUHGSMI_BUFFER_LOCK_FLAGS fFlags;
497 int rc;
498 CRASSERT(!pClient->pvHGBuffer);
499 fFlags.Value = 0;
500 rc = pClient->pHGBuffer->pfnLock(pClient->pHGBuffer, 0, cbBuffer, fFlags, &pClient->pvHGBuffer);
501 if (RT_SUCCESS(rc))
502 return pClient->pvHGBuffer;
503 else
504 crWarning("_crVBoxHGSMIRecvBufData: pfnLock failed rc %d", rc);
505
506 return NULL;
507}
508
509DECLINLINE(void) _crVBoxHGSMIFillCmd(VBOXUHGSMI_BUFFER_SUBMIT *pSubm, PCRVBOXHGSMI_CLIENT pClient, uint32_t cbData)
510{
511 pSubm->pBuf = pClient->pCmdBuffer;
512 pSubm->offData = 0;
513 pSubm->cbData = cbData;
514 pSubm->fFlags.Value = 0;
515 pSubm->fFlags.s.fDoNotRetire = 1;
516# if 0
517 pSubm->fFlags.bDoNotSignalCompletion = 1; /* <- we do not need that actually since
518 * in case we want completion,
519 * we will block in _crVBoxHGSMICmdBufferGetRc (when locking the buffer)
520 * which is needed for getting the result */
521# endif
522}
523#endif
524
525/* Some forward declarations */
526static void _crVBoxHGCMReceiveMessage(CRConnection *conn);
527
528#ifndef IN_GUEST
529static bool _crVBoxHGCMReadBytes(CRConnection *conn, void *buf, uint32_t len)
530{
531 CRASSERT(conn && buf);
532
533 if (!conn->pBuffer || (conn->cbBuffer<len))
534 return FALSE;
535
536 crMemcpy(buf, conn->pBuffer, len);
537
538 conn->cbBuffer -= len;
539 conn->pBuffer = conn->cbBuffer>0 ? (uint8_t*)conn->pBuffer+len : NULL;
540
541 return TRUE;
542}
543#endif
544
545#ifndef IN_GUEST
546/** @todo get rid of it*/
547static bool _crVBoxHGCMWriteBytes(CRConnection *conn, const void *buf, uint32_t len)
548{
549 CRASSERT(conn && buf);
550
551 /* make sure there's host buffer and it's clear */
552 CRASSERT(conn->pHostBuffer && !conn->cbHostBuffer);
553
554 if (conn->cbHostBufferAllocated < len)
555 {
556 crDebug("Host buffer too small %d out of requested %d bytes, reallocating", conn->cbHostBufferAllocated, len);
557 crFree(conn->pHostBuffer);
558 conn->pHostBuffer = crAlloc(len);
559 if (!conn->pHostBuffer)
560 {
561 conn->cbHostBufferAllocated = 0;
562 crError("OUT_OF_MEMORY trying to allocate %d bytes", len);
563 return FALSE;
564 }
565 conn->cbHostBufferAllocated = len;
566 }
567
568 crMemcpy(conn->pHostBuffer, buf, len);
569 conn->cbHostBuffer = len;
570
571 return TRUE;
572}
573#endif
574
575/**
576 * Send an HGCM request
577 *
578 * @return VBox status code
579 * @param pvData Data pointer
580 * @param cbData Data size
581 */
582static int crVBoxHGCMCall(CRConnection *conn, PVBGLIOCHGCMCALL pData, unsigned cbData)
583{
584#ifdef IN_GUEST
585 int rc;
586# ifndef VBOX_WITH_CRHGSMI
587 RT_NOREF(conn);
588# else
589 PCRVBOXHGSMI_CLIENT pClient = g_crvboxhgcm.bHgsmiOn ? _crVBoxHGSMIClientGet(conn) : NULL;
590 if (pClient)
591 rc = VBoxCrHgsmiCtlConCall(pClient->pHgsmi, pData, cbData);
592 else
593# endif
594 {
595 rc = VbglR3HGCMCall(pData, cbData);
596 if (RT_SUCCESS(rc))
597 { /* likely */ }
598 else
599 {
600 crWarning("vboxCall failed with VBox status code %Rrc\n", rc);
601# ifndef RT_OS_WINDOWS
602 if (rc == VERR_INTERRUPTED)
603 {
604 /* Not sure why we're doing the sleep stuff here. The original coder didn't
605 bother to mention why he thought it necessary. :-( */
606 RTMSINTERVAL msSleep;
607 int i;
608 for (i = 0, msSleep = 50; i < 6; i++, msSleep = msSleep * 2)
609 {
610 RTThreadSleep(msSleep);
611 rc = VbglR3HGCMCall(pData, cbData);
612 if (rc != VERR_INTERRUPTED)
613 {
614 if (RT_SUCCESS(rc))
615 crWarning("vboxCall retry(%i) succeeded", i + 1);
616 else
617 crWarning("vboxCall retry(%i) failed with VBox status code %Rrc", i + 1, rc);
618 break;
619 }
620 }
621 }
622# endif
623 }
624 }
625 return rc;
626
627#else /* IN_GUEST */
628 RT_NOREF(conn, pData, cbData);
629 crError("crVBoxHGCMCall called on host side!");
630 CRASSERT(FALSE);
631 return VERR_NOT_SUPPORTED;
632#endif /* IN_GUEST */
633}
634
635static void *_crVBoxHGCMAlloc(CRConnection *conn)
636{
637 CRVBOXHGCMBUFFER *buf;
638
639#ifdef CHROMIUM_THREADSAFE
640 crLockMutex(&g_crvboxhgcm.mutex);
641#endif
642
643 buf = (CRVBOXHGCMBUFFER *) crBufferPoolPop(g_crvboxhgcm.bufpool, conn->buffer_size);
644
645 if (!buf)
646 {
647 crDebug("Buffer pool %p was empty; allocating new %d byte buffer.",
648 (void *) g_crvboxhgcm.bufpool,
649 (unsigned int)sizeof(CRVBOXHGCMBUFFER) + conn->buffer_size);
650
651 /* We're either on host side, or we failed to allocate DDRAW buffer */
652 if (!buf)
653 {
654 crDebug("Using system malloc\n");
655 buf = (CRVBOXHGCMBUFFER *) crAlloc( sizeof(CRVBOXHGCMBUFFER) + conn->buffer_size );
656 CRASSERT(buf);
657 buf->magic = CR_VBOXHGCM_BUFFER_MAGIC;
658 buf->kind = CR_VBOXHGCM_MEMORY;
659 buf->allocated = conn->buffer_size;
660 }
661 }
662
663#ifdef CHROMIUM_THREADSAFE
664 crUnlockMutex(&g_crvboxhgcm.mutex);
665#endif
666
667 return (void *)( buf + 1 );
668
669}
670
671static void *crVBoxHGCMAlloc(CRConnection *conn)
672{
673 void *pvBuff;
674 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
675#ifdef CHROMIUM_THREADSAFE
676 crLockMutex(&g_crvboxhgcm.mutex);
677#endif
678 pvBuff = _crVBoxHGCMAlloc(conn);
679#ifdef CHROMIUM_THREADSAFE
680 crUnlockMutex(&g_crvboxhgcm.mutex);
681#endif
682 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
683 return pvBuff;
684}
685
686static void _crVBoxHGCMWriteExact(CRConnection *conn, const void *buf, unsigned int len)
687{
688 int rc;
689 int32_t callRes;
690
691#ifdef IN_GUEST
692 if (conn->u32InjectClientID)
693 {
694 CRVBOXHGCMINJECT parms;
695
696 VBGL_HGCM_HDR_INIT(&parms.hdr, conn->u32ClientID, SHCRGL_GUEST_FN_INJECT, SHCRGL_CPARMS_INJECT);
697
698 parms.u32ClientID.type = VMMDevHGCMParmType_32bit;
699 parms.u32ClientID.u.value32 = conn->u32InjectClientID;
700
701 parms.pBuffer.type = VMMDevHGCMParmType_LinAddr_In;
702 parms.pBuffer.u.Pointer.size = len;
703 parms.pBuffer.u.Pointer.u.linearAddr = (uintptr_t) buf;
704
705 rc = crVBoxHGCMCall(conn, &parms.hdr, sizeof(parms));
706 callRes = parms.hdr.Hdr.rc; /** @todo now rc == parms.hdr.Hdr.rc */
707 }
708 else
709#endif
710 {
711 CRVBOXHGCMWRITE parms;
712
713 VBGL_HGCM_HDR_INIT(&parms.hdr, conn->u32ClientID, SHCRGL_GUEST_FN_WRITE, SHCRGL_CPARMS_WRITE);
714
715 parms.pBuffer.type = VMMDevHGCMParmType_LinAddr_In;
716 parms.pBuffer.u.Pointer.size = len;
717 parms.pBuffer.u.Pointer.u.linearAddr = (uintptr_t) buf;
718
719 rc = crVBoxHGCMCall(conn, &parms.hdr, sizeof(parms));
720 callRes = parms.hdr.Hdr.rc; /** @todo now rc == parms.hdr.Hdr.rc */
721 }
722
723 if (RT_FAILURE(rc) || RT_FAILURE(callRes))
724 {
725 crWarning("SHCRGL_GUEST_FN_WRITE failed with %x %x\n", rc, callRes);
726 }
727}
728
729static void crVBoxHGCMWriteExact(CRConnection *conn, const void *buf, unsigned int len)
730{
731 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
732#ifdef CHROMIUM_THREADSAFE
733 crLockMutex(&g_crvboxhgcm.mutex);
734#endif
735 _crVBoxHGCMWriteExact(conn, buf, len);
736#ifdef CHROMIUM_THREADSAFE
737 crUnlockMutex(&g_crvboxhgcm.mutex);
738#endif
739 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
740}
741
742static void crVBoxHGCMReadExact( CRConnection *conn, const void *buf, unsigned int len )
743{
744 CRVBOXHGCMREAD parms;
745 int rc;
746 RT_NOREF(buf, len);
747
748 VBGL_HGCM_HDR_INIT(&parms.hdr, conn->u32ClientID, SHCRGL_GUEST_FN_READ, SHCRGL_CPARMS_READ);
749
750 CRASSERT(!conn->pBuffer); /* make sure there's no data to process */
751 parms.pBuffer.type = VMMDevHGCMParmType_LinAddr_Out;
752 parms.pBuffer.u.Pointer.size = conn->cbHostBufferAllocated;
753 parms.pBuffer.u.Pointer.u.linearAddr = (uintptr_t) conn->pHostBuffer;
754
755 parms.cbBuffer.type = VMMDevHGCMParmType_32bit;
756 parms.cbBuffer.u.value32 = 0;
757
758 rc = crVBoxHGCMCall(conn, &parms.hdr, sizeof(parms));
759
760 if (RT_FAILURE(rc) || RT_FAILURE(parms.hdr.Hdr.rc) /** @todo now rc == parms.hdr.Hdr.rc */)
761 {
762 crWarning("SHCRGL_GUEST_FN_READ failed with %x %x\n", rc, parms.hdr.Hdr.rc);
763 return;
764 }
765
766 if (parms.cbBuffer.u.value32)
767 {
768 /*conn->pBuffer = (uint8_t*) parms.pBuffer.u.Pointer.u.linearAddr; */
769 conn->pBuffer = conn->pHostBuffer;
770 conn->cbBuffer = parms.cbBuffer.u.value32;
771 }
772
773 if (conn->cbBuffer)
774 _crVBoxHGCMReceiveMessage(conn);
775
776}
777
778/* Same as crVBoxHGCMWriteExact, but combined with read of writeback data.
779 * This halves the number of HGCM calls we do,
780 * most likely crVBoxHGCMPollHost shouldn't be called at all now.
781 */
782static void
783crVBoxHGCMWriteReadExact(CRConnection *conn, const void *buf, unsigned int len, CRVBOXHGCMBUFFERKIND bufferKind)
784{
785 CRVBOXHGCMWRITEREAD parms;
786 int rc;
787
788 VBGL_HGCM_HDR_INIT(&parms.hdr, conn->u32ClientID, SHCRGL_GUEST_FN_WRITE_READ, SHCRGL_CPARMS_WRITE_READ);
789
790 parms.pBuffer.type = VMMDevHGCMParmType_LinAddr_In;
791 parms.pBuffer.u.Pointer.size = len;
792 parms.pBuffer.u.Pointer.u.linearAddr = (uintptr_t) buf;
793
794 CRASSERT(!conn->pBuffer); /*make sure there's no data to process*/
795 parms.pWriteback.type = VMMDevHGCMParmType_LinAddr_Out;
796 parms.pWriteback.u.Pointer.size = conn->cbHostBufferAllocated;
797 parms.pWriteback.u.Pointer.u.linearAddr = (uintptr_t) conn->pHostBuffer;
798
799 parms.cbWriteback.type = VMMDevHGCMParmType_32bit;
800 parms.cbWriteback.u.value32 = 0;
801
802 rc = crVBoxHGCMCall(conn, &parms.hdr, sizeof(parms));
803
804#if defined(RT_OS_LINUX) || defined(RT_OS_WINDOWS)
805 if (VERR_OUT_OF_RANGE==rc && CR_VBOXHGCM_USERALLOCATED==bufferKind)
806 {
807 /*Buffer is too big, so send it in split chunks*/
808 CRVBOXHGCMWRITEBUFFER wbParms;
809
810 VBGL_HGCM_HDR_INIT(&wbParms.hdr, conn->u32ClientID, SHCRGL_GUEST_FN_WRITE_BUFFER, SHCRGL_CPARMS_WRITE_BUFFER);
811
812 wbParms.iBufferID.type = VMMDevHGCMParmType_32bit;
813 wbParms.iBufferID.u.value32 = 0;
814
815 wbParms.cbBufferSize.type = VMMDevHGCMParmType_32bit;
816 wbParms.cbBufferSize.u.value32 = len;
817
818 wbParms.ui32Offset.type = VMMDevHGCMParmType_32bit;
819 wbParms.ui32Offset.u.value32 = 0;
820
821 wbParms.pBuffer.type = VMMDevHGCMParmType_LinAddr_In;
822 wbParms.pBuffer.u.Pointer.size = MIN(CR_HGCM_SPLIT_BUFFER_SIZE, len);
823 wbParms.pBuffer.u.Pointer.u.linearAddr = (uintptr_t) buf;
824
825 if (len<CR_HGCM_SPLIT_BUFFER_SIZE)
826 {
827 crError("VERR_OUT_OF_RANGE in crVBoxHGCMWriteReadExact for %u bytes write", len);
828 return;
829 }
830
831 while (wbParms.pBuffer.u.Pointer.size)
832 {
833 crDebug("SHCRGL_GUEST_FN_WRITE_BUFFER, offset=%u, size=%u", wbParms.ui32Offset.u.value32, wbParms.pBuffer.u.Pointer.size);
834
835 rc = crVBoxHGCMCall(conn, &wbParms.hdr, sizeof(wbParms));
836 if (RT_FAILURE(rc) || RT_FAILURE(wbParms.hdr.Hdr.rc) /** @todo now rc == wbParms.hdr.Hdr.rc */)
837 {
838 crError("SHCRGL_GUEST_FN_WRITE_BUFFER (%i) failed with %x %x\n", wbParms.pBuffer.u.Pointer.size, rc, wbParms.hdr.Hdr.rc);
839 return;
840 }
841
842 wbParms.ui32Offset.u.value32 += wbParms.pBuffer.u.Pointer.size;
843 wbParms.pBuffer.u.Pointer.u.linearAddr += (uintptr_t) wbParms.pBuffer.u.Pointer.size;
844 wbParms.pBuffer.u.Pointer.size = MIN(CR_HGCM_SPLIT_BUFFER_SIZE, len-wbParms.ui32Offset.u.value32);
845 }
846
847 /*now issue GUEST_FN_WRITE_READ_BUFFERED referencing the buffer we'd made*/
848 {
849 CRVBOXHGCMWRITEREADBUFFERED wrbParms;
850
851 VBGL_HGCM_HDR_INIT(&wrbParms.hdr, conn->u32ClientID, SHCRGL_GUEST_FN_WRITE_READ_BUFFERED, SHCRGL_CPARMS_WRITE_READ_BUFFERED);
852
853 crMemcpy(&wrbParms.iBufferID, &wbParms.iBufferID, sizeof(HGCMFunctionParameter));
854 crMemcpy(&wrbParms.pWriteback, &parms.pWriteback, sizeof(HGCMFunctionParameter));
855 crMemcpy(&wrbParms.cbWriteback, &parms.cbWriteback, sizeof(HGCMFunctionParameter));
856
857 rc = crVBoxHGCMCall(conn, &wrbParms.hdr, sizeof(wrbParms));
858
859 /*bit of hack to reuse code below*/
860 parms.hdr.Hdr.rc = wrbParms.hdr.Hdr.rc;
861 crMemcpy(&parms.cbWriteback, &wrbParms.cbWriteback, sizeof(HGCMFunctionParameter));
862 crMemcpy(&parms.pWriteback, &wrbParms.pWriteback, sizeof(HGCMFunctionParameter));
863 }
864 }
865#endif
866
867 if (RT_FAILURE(rc) || RT_FAILURE(parms.hdr.Hdr.rc) /** @todo now rc == parms.hdr.Hdr.rc */)
868 {
869
870 if ((VERR_BUFFER_OVERFLOW == parms.hdr.Hdr.rc) /* && RT_SUCCESS(rc) */)
871 {
872 /* reallocate buffer and retry */
873
874 CRASSERT(parms.cbWriteback.u.value32>conn->cbHostBufferAllocated);
875
876 crDebug("Reallocating host buffer from %d to %d bytes", conn->cbHostBufferAllocated, parms.cbWriteback.u.value32);
877
878 crFree(conn->pHostBuffer);
879 conn->cbHostBufferAllocated = parms.cbWriteback.u.value32;
880 conn->pHostBuffer = crAlloc(conn->cbHostBufferAllocated);
881
882 crVBoxHGCMReadExact(conn, buf, len);
883
884 return;
885 }
886 else
887 {
888 crWarning("SHCRGL_GUEST_FN_WRITE_READ (%i) failed with %x %x\n", len, rc, parms.hdr.Hdr.rc);
889 return;
890 }
891 }
892
893 if (parms.cbWriteback.u.value32)
894 {
895 /*conn->pBuffer = (uint8_t*) parms.pWriteback.u.Pointer.u.linearAddr;*/
896 conn->pBuffer = conn->pHostBuffer;
897 conn->cbBuffer = parms.cbWriteback.u.value32;
898 }
899
900 if (conn->cbBuffer)
901 _crVBoxHGCMReceiveMessage(conn);
902}
903
904static void crVBoxHGCMSend(CRConnection *conn, void **bufp,
905 const void *start, unsigned int len)
906{
907 CRVBOXHGCMBUFFER *hgcm_buffer;
908 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
909
910#ifdef CHROMIUM_THREADSAFE
911 crLockMutex(&g_crvboxhgcm.mutex);
912#endif
913
914 if (!bufp) /* We're sending a user-allocated buffer. */
915 {
916#ifndef IN_GUEST
917 /** @todo remove temp buffer allocation in unpacker*/
918 /* we're at the host side, so just store data until guest polls us */
919 _crVBoxHGCMWriteBytes(conn, start, len);
920#else
921 CRASSERT(!conn->u32InjectClientID);
922 crDebug("SHCRGL: sending userbuf with %d bytes\n", len);
923 crVBoxHGCMWriteReadExact(conn, start, len, CR_VBOXHGCM_USERALLOCATED);
924#endif
925#ifdef CHROMIUM_THREADSAFE
926 crUnlockMutex(&g_crvboxhgcm.mutex);
927#endif
928 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
929 return;
930 }
931
932 /* The region [start .. start + len + 1] lies within a buffer that
933 * was allocated with crVBoxHGCMAlloc() and can be put into the free
934 * buffer pool when we're done sending it.
935 */
936
937 hgcm_buffer = (CRVBOXHGCMBUFFER *)(*bufp) - 1;
938 CRASSERT(hgcm_buffer->magic == CR_VBOXHGCM_BUFFER_MAGIC);
939
940 /* Length would be passed as part of HGCM pointer description
941 * No need to prepend it to the buffer
942 */
943#ifdef IN_GUEST
944 if (conn->u32InjectClientID)
945 {
946 _crVBoxHGCMWriteExact(conn, start, len);
947 }
948 else
949#endif
950 crVBoxHGCMWriteReadExact(conn, start, len, hgcm_buffer->kind);
951
952 /* Reclaim this pointer for reuse */
953#ifdef CHROMIUM_THREADSAFE
954 crLockMutex(&g_crvboxhgcm.mutex);
955#endif
956 crBufferPoolPush(g_crvboxhgcm.bufpool, hgcm_buffer, hgcm_buffer->allocated);
957#ifdef CHROMIUM_THREADSAFE
958 crUnlockMutex(&g_crvboxhgcm.mutex);
959#endif
960
961 /* Since the buffer's now in the 'free' buffer pool, the caller can't
962 * use it any more. Setting bufp to NULL will make sure the caller
963 * doesn't try to re-use the buffer.
964 */
965 *bufp = NULL;
966
967#ifdef CHROMIUM_THREADSAFE
968 crUnlockMutex(&g_crvboxhgcm.mutex);
969#endif
970
971 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
972}
973
974static void crVBoxHGCMPollHost(CRConnection *conn)
975{
976 CRVBOXHGCMREAD parms;
977 int rc;
978
979 CRASSERT(!conn->pBuffer);
980
981 VBGL_HGCM_HDR_INIT(&parms.hdr, conn->u32ClientID, SHCRGL_GUEST_FN_READ, SHCRGL_CPARMS_READ);
982
983 parms.pBuffer.type = VMMDevHGCMParmType_LinAddr_Out;
984 parms.pBuffer.u.Pointer.size = conn->cbHostBufferAllocated;
985 parms.pBuffer.u.Pointer.u.linearAddr = (uintptr_t) conn->pHostBuffer;
986
987 parms.cbBuffer.type = VMMDevHGCMParmType_32bit;
988 parms.cbBuffer.u.value32 = 0;
989
990 rc = crVBoxHGCMCall(conn, &parms.hdr, sizeof(parms));
991
992 if (RT_FAILURE(rc) || RT_FAILURE(parms.hdr.Hdr.rc) /** @todo now rc == parms.hdr.Hdr.rc */)
993 {
994 crDebug("SHCRGL_GUEST_FN_READ failed with %x %x\n", rc, parms.hdr.Hdr.rc);
995 return;
996 }
997
998 if (parms.cbBuffer.u.value32)
999 {
1000 conn->pBuffer = (uint8_t*) parms.pBuffer.u.Pointer.u.linearAddr;
1001 conn->cbBuffer = parms.cbBuffer.u.value32;
1002 }
1003}
1004
1005static void crVBoxHGCMSingleRecv(CRConnection *conn, void *buf, unsigned int len)
1006{
1007 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
1008#ifdef CHROMIUM_THREADSAFE
1009 crLockMutex(&g_crvboxhgcm.mutex);
1010#endif
1011 crVBoxHGCMReadExact(conn, buf, len);
1012#ifdef CHROMIUM_THREADSAFE
1013 crUnlockMutex(&g_crvboxhgcm.mutex);
1014#endif
1015 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1016}
1017
1018static void _crVBoxHGCMFree(CRConnection *conn, void *buf)
1019{
1020 CRVBOXHGCMBUFFER *hgcm_buffer = (CRVBOXHGCMBUFFER *) buf - 1;
1021
1022 CRASSERT(hgcm_buffer->magic == CR_VBOXHGCM_BUFFER_MAGIC);
1023
1024 /** @todo wrong len for redir buffers*/
1025 conn->recv_credits += hgcm_buffer->len;
1026
1027 switch (hgcm_buffer->kind)
1028 {
1029 case CR_VBOXHGCM_MEMORY:
1030#ifdef CHROMIUM_THREADSAFE
1031 crLockMutex(&g_crvboxhgcm.mutex);
1032#endif
1033 if (g_crvboxhgcm.bufpool) {
1034 /** @todo o'rly? */
1035 /* pool may have been deallocated just a bit earlier in response
1036 * to a SIGPIPE (Broken Pipe) signal.
1037 */
1038 crBufferPoolPush(g_crvboxhgcm.bufpool, hgcm_buffer, hgcm_buffer->allocated);
1039 }
1040#ifdef CHROMIUM_THREADSAFE
1041 crUnlockMutex(&g_crvboxhgcm.mutex);
1042#endif
1043 break;
1044
1045 case CR_VBOXHGCM_MEMORY_BIG:
1046 crFree( hgcm_buffer );
1047 break;
1048
1049 default:
1050 crError( "Weird buffer kind trying to free in crVBoxHGCMFree: %d", hgcm_buffer->kind );
1051 }
1052}
1053
1054static void crVBoxHGCMFree(CRConnection *conn, void *buf)
1055{
1056 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
1057#ifdef CHROMIUM_THREADSAFE
1058 crLockMutex(&g_crvboxhgcm.mutex);
1059#endif
1060 _crVBoxHGCMFree(conn, buf);
1061#ifdef CHROMIUM_THREADSAFE
1062 crUnlockMutex(&g_crvboxhgcm.mutex);
1063#endif
1064 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1065}
1066
1067static void _crVBoxHGCMReceiveMessage(CRConnection *conn)
1068{
1069 uint32_t len;
1070 CRVBOXHGCMBUFFER *hgcm_buffer;
1071 CRMessage *msg;
1072 CRMessageType cached_type;
1073
1074 len = conn->cbBuffer;
1075 CRASSERT(len > 0);
1076 CRASSERT(conn->pBuffer);
1077
1078#ifndef IN_GUEST
1079 /* Expect only CR_MESSAGE_OPCODES from the guest. */
1080 AssertPtrReturnVoid(conn->pBuffer);
1081
1082 if ( conn->cbBuffer >= sizeof(CRMessageHeader)
1083 && ((CRMessageHeader*) (conn->pBuffer))->type == CR_MESSAGE_OPCODES)
1084 {
1085 /* Looks good. */
1086 }
1087 else
1088 {
1089 AssertFailed();
1090 /** @todo Find out if this is the expected cleanup. */
1091 conn->cbBuffer = 0;
1092 conn->pBuffer = NULL;
1093 return;
1094 }
1095#endif
1096
1097#ifndef IN_GUEST
1098 if (conn->allow_redir_ptr)
1099 {
1100#endif
1101 CRASSERT(conn->buffer_size >= sizeof(CRMessageRedirPtr));
1102
1103 hgcm_buffer = (CRVBOXHGCMBUFFER *) _crVBoxHGCMAlloc( conn ) - 1;
1104 hgcm_buffer->len = sizeof(CRMessageRedirPtr);
1105
1106 msg = (CRMessage *) (hgcm_buffer + 1);
1107
1108 msg->header.type = CR_MESSAGE_REDIR_PTR;
1109 msg->redirptr.pMessage = (CRMessageHeader*) (conn->pBuffer);
1110 msg->header.conn_id = msg->redirptr.pMessage->conn_id;
1111
1112#if defined(VBOX_WITH_CRHGSMI) && !defined(IN_GUEST)
1113 msg->redirptr.CmdData = conn->CmdData;
1114 CRVBOXHGSMI_CMDDATA_ASSERT_CONSISTENT(&msg->redirptr.CmdData);
1115 CRVBOXHGSMI_CMDDATA_CLEANUP(&conn->CmdData);
1116#endif
1117
1118 cached_type = msg->redirptr.pMessage->type;
1119
1120 conn->cbBuffer = 0;
1121 conn->pBuffer = NULL;
1122#ifndef IN_GUEST
1123 }
1124 else
1125 {
1126 /* we should NEVER have redir_ptr disabled with HGSMI command now */
1127 CRASSERT(!conn->CmdData.pvCmd);
1128 if ( len <= conn->buffer_size )
1129 {
1130 /* put in pre-allocated buffer */
1131 hgcm_buffer = (CRVBOXHGCMBUFFER *) _crVBoxHGCMAlloc( conn ) - 1;
1132 }
1133 else
1134 {
1135 /* allocate new buffer,
1136 * not using pool here as it's most likely one time transfer of huge texture
1137 */
1138 hgcm_buffer = (CRVBOXHGCMBUFFER *) crAlloc( sizeof(CRVBOXHGCMBUFFER) + len );
1139 hgcm_buffer->magic = CR_VBOXHGCM_BUFFER_MAGIC;
1140 hgcm_buffer->kind = CR_VBOXHGCM_MEMORY_BIG;
1141 hgcm_buffer->allocated = sizeof(CRVBOXHGCMBUFFER) + len;
1142 }
1143
1144 hgcm_buffer->len = len;
1145 _crVBoxHGCMReadBytes(conn, hgcm_buffer + 1, len);
1146
1147 msg = (CRMessage *) (hgcm_buffer + 1);
1148 cached_type = msg->header.type;
1149 }
1150#endif /* !IN_GUEST*/
1151
1152 conn->recv_credits -= len;
1153 conn->total_bytes_recv += len;
1154 conn->recv_count++;
1155
1156 crNetDispatchMessage( g_crvboxhgcm.recv_list, conn, msg, len );
1157
1158 /* CR_MESSAGE_OPCODES is freed in crserverlib/server_stream.c with crNetFree.
1159 * OOB messages are the programmer's problem. -- Humper 12/17/01
1160 */
1161 if (cached_type != CR_MESSAGE_OPCODES
1162 && cached_type != CR_MESSAGE_OOB
1163 && cached_type != CR_MESSAGE_GATHER)
1164 {
1165 _crVBoxHGCMFree(conn, msg);
1166 }
1167}
1168
1169static void crVBoxHGCMReceiveMessage(CRConnection *conn)
1170{
1171 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
1172#ifdef CHROMIUM_THREADSAFE
1173 crLockMutex(&g_crvboxhgcm.mutex);
1174#endif
1175 _crVBoxHGCMReceiveMessage(conn);
1176#ifdef CHROMIUM_THREADSAFE
1177 crUnlockMutex(&g_crvboxhgcm.mutex);
1178#endif
1179 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1180}
1181
1182
1183/*
1184 * Called on host side only, to "accept" client connection
1185 */
1186static void crVBoxHGCMAccept( CRConnection *conn)
1187{
1188 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
1189 CRASSERT(conn && conn->pHostBuffer);
1190#ifdef IN_GUEST
1191 CRASSERT(FALSE);
1192#endif
1193 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1194}
1195
1196static int crVBoxHGCMSetVersion(CRConnection *conn, unsigned int vMajor, unsigned int vMinor)
1197{
1198 CRVBOXHGCMSETVERSION parms;
1199 int rc;
1200 RT_NOREF(vMajor, vMinor);
1201
1202 VBGL_HGCM_HDR_INIT(&parms.hdr, conn->u32ClientID, SHCRGL_GUEST_FN_SET_VERSION, SHCRGL_CPARMS_SET_VERSION);
1203
1204 parms.vMajor.type = VMMDevHGCMParmType_32bit;
1205 parms.vMajor.u.value32 = CR_PROTOCOL_VERSION_MAJOR;
1206 parms.vMinor.type = VMMDevHGCMParmType_32bit;
1207 parms.vMinor.u.value32 = CR_PROTOCOL_VERSION_MINOR;
1208
1209 rc = crVBoxHGCMCall(conn, &parms.hdr, sizeof(parms));
1210
1211 if (RT_SUCCESS(rc))
1212 {
1213 rc = parms.hdr.Hdr.rc; /** @todo now rc == parms.hdr.Hdr.rc */
1214 if (RT_SUCCESS(rc))
1215 {
1216 conn->vMajor = CR_PROTOCOL_VERSION_MAJOR;
1217 conn->vMinor = CR_PROTOCOL_VERSION_MINOR;
1218
1219 return VINF_SUCCESS;
1220 }
1221 else
1222 WARN(("Host doesn't accept our version %d.%d. Make sure you have appropriate additions installed!",
1223 parms.vMajor.u.value32, parms.vMinor.u.value32));
1224 }
1225 else
1226 WARN(("crVBoxHGCMCall failed %d", rc));
1227
1228 return rc;
1229}
1230
1231static int crVBoxHGCMGetHostCapsLegacy(CRConnection *conn, uint32_t *pu32HostCaps)
1232{
1233 CRVBOXHGCMGETCAPS caps;
1234 int rc;
1235
1236 VBGL_HGCM_HDR_INIT(&caps.hdr, conn->u32ClientID, SHCRGL_GUEST_FN_GET_CAPS_LEGACY, SHCRGL_CPARMS_GET_CAPS_LEGACY);
1237
1238 caps.Caps.type = VMMDevHGCMParmType_32bit;
1239 caps.Caps.u.value32 = 0;
1240
1241 rc = crVBoxHGCMCall(conn, &caps.hdr, sizeof(caps));
1242
1243 if (RT_SUCCESS(rc))
1244 {
1245 rc = caps.hdr.Hdr.rc;
1246 if (RT_SUCCESS(rc))
1247 {
1248 *pu32HostCaps = caps.Caps.u.value32;
1249 return VINF_SUCCESS;
1250 }
1251 else
1252 WARN(("SHCRGL_GUEST_FN_GET_CAPS failed %d", rc));
1253 return FALSE;
1254 }
1255 else
1256 WARN(("crVBoxHGCMCall failed %d", rc));
1257
1258 *pu32HostCaps = 0;
1259
1260 return rc;
1261}
1262
1263static int crVBoxHGCMSetPID(CRConnection *conn, unsigned long long pid)
1264{
1265 CRVBOXHGCMSETPID parms;
1266 int rc;
1267
1268 VBGL_HGCM_HDR_INIT(&parms.hdr, conn->u32ClientID, SHCRGL_GUEST_FN_SET_PID, SHCRGL_CPARMS_SET_PID);
1269
1270 parms.u64PID.type = VMMDevHGCMParmType_64bit;
1271 parms.u64PID.u.value64 = pid;
1272
1273 rc = crVBoxHGCMCall(conn, &parms.hdr, sizeof(parms));
1274
1275 if (RT_FAILURE(rc) || RT_FAILURE(parms.hdr.Hdr.rc) /** @todo now rc == parms.hdr.Hdr.rc */)
1276 {
1277 crWarning("SHCRGL_GUEST_FN_SET_PID failed!");
1278 return FALSE;
1279 }
1280
1281 return TRUE;
1282}
1283
1284/**
1285 * The function that actually connects. This should only be called by clients,
1286 * guests in vbox case.
1287 * Servers go through crVBoxHGCMAccept;
1288 */
1289static int crVBoxHGCMDoConnect( CRConnection *conn )
1290{
1291#ifdef IN_GUEST
1292 int rc;
1293 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
1294 RTR3InitDll(RTR3INIT_FLAGS_UNOBTRUSIVE);
1295 rc = VbglR3InitUser();
1296 if (RT_SUCCESS(rc))
1297 {
1298 uint32_t idClient;
1299 rc = VbglR3HGCMConnect("VBoxSharedCrOpenGL", &idClient);
1300 if (RT_SUCCESS(rc))
1301 {
1302 conn->u32ClientID = idClient;
1303 crDebug("HGCM connect was successful: client id =0x%x\n", conn->u32ClientID);
1304
1305 rc = crVBoxHGCMSetVersion(conn, CR_PROTOCOL_VERSION_MAJOR, CR_PROTOCOL_VERSION_MINOR);
1306 if (RT_FAILURE(rc))
1307 {
1308 WARN(("crVBoxHGCMSetVersion failed %d", rc));
1309 return FALSE;
1310 }
1311#ifdef RT_OS_WINDOWS
1312 rc = crVBoxHGCMSetPID(conn, GetCurrentProcessId());
1313#else
1314 rc = crVBoxHGCMSetPID(conn, crGetPID());
1315#endif
1316 if (RT_FAILURE(rc))
1317 {
1318 WARN(("crVBoxHGCMSetPID failed %Rrc", rc));
1319 return FALSE;
1320 }
1321
1322 if (!g_crvboxhgcm.fHostCapsInitialized)
1323 {
1324 rc = crVBoxHGCMGetHostCapsLegacy(conn, &g_crvboxhgcm.u32HostCaps);
1325 if (RT_FAILURE(rc))
1326 {
1327 WARN(("VBoxCrHgsmiCtlConGetHostCaps failed %Rrc", rc));
1328 g_crvboxhgcm.u32HostCaps = 0;
1329 }
1330
1331 /* host may not support it, ignore any failures */
1332 g_crvboxhgcm.fHostCapsInitialized = true;
1333 }
1334
1335 if (g_crvboxhgcm.u32HostCaps & CR_VBOX_CAP_HOST_CAPS_NOT_SUFFICIENT)
1336 {
1337 crDebug("HGCM connect: insufficient host capabilities\n");
1338 g_crvboxhgcm.u32HostCaps = 0;
1339 return FALSE;
1340 }
1341
1342 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1343 return TRUE;
1344 }
1345
1346 crDebug("HGCM connect failed: %Rrc\n", rc);
1347 VbglR3Term();
1348 }
1349 else
1350 crDebug("Failed to initialize VbglR3 library: %Rrc\n", rc);
1351
1352 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1353 return FALSE;
1354
1355#else /* !IN_GUEST */
1356 RT_NOREF(conn);
1357 crError("crVBoxHGCMDoConnect called on host side!");
1358 CRASSERT(FALSE);
1359 return FALSE;
1360#endif /* !IN_GUEST */
1361}
1362
1363static bool _crVBoxCommonDoDisconnectLocked( CRConnection *conn )
1364{
1365 int i;
1366 if (conn->pHostBuffer)
1367 {
1368 crFree(conn->pHostBuffer);
1369 conn->pHostBuffer = NULL;
1370 conn->cbHostBuffer = 0;
1371 conn->cbHostBufferAllocated = 0;
1372 }
1373
1374 conn->pBuffer = NULL;
1375 conn->cbBuffer = 0;
1376
1377 if (conn->type == CR_VBOXHGCM)
1378 {
1379 --g_crvboxhgcm.num_conns;
1380
1381 if (conn->index < g_crvboxhgcm.num_conns)
1382 {
1383 g_crvboxhgcm.conns[conn->index] = g_crvboxhgcm.conns[g_crvboxhgcm.num_conns];
1384 g_crvboxhgcm.conns[conn->index]->index = conn->index;
1385 }
1386 else g_crvboxhgcm.conns[conn->index] = NULL;
1387
1388 conn->type = CR_NO_CONNECTION;
1389 }
1390
1391 for (i = 0; i < g_crvboxhgcm.num_conns; i++)
1392 if (g_crvboxhgcm.conns[i] && g_crvboxhgcm.conns[i]->type != CR_NO_CONNECTION)
1393 return true;
1394 return false;
1395}
1396
1397/** @todo same, replace DeviceIoControl with vbglR3DoIOCtl */
1398static void crVBoxHGCMDoDisconnect( CRConnection *conn )
1399{
1400 bool fHasActiveCons = false;
1401
1402 if (!g_crvboxhgcm.initialized) return;
1403
1404#ifdef CHROMIUM_THREADSAFE
1405 crLockMutex(&g_crvboxhgcm.mutex);
1406#endif
1407
1408 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
1409
1410 fHasActiveCons = _crVBoxCommonDoDisconnectLocked(conn);
1411
1412#ifndef IN_GUEST
1413#else /* IN_GUEST */
1414 if (conn->u32ClientID)
1415 {
1416 int rc = VbglR3HGCMDisconnect(conn->u32ClientID);
1417 if (RT_FAILURE(rc))
1418 crDebug("Disconnect failed with %Rrc\n", rc);
1419 conn->u32ClientID = 0;
1420
1421 VbglR3Term();
1422 }
1423#endif /* IN_GUEST */
1424
1425 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1426
1427#ifdef CHROMIUM_THREADSAFE
1428 crUnlockMutex(&g_crvboxhgcm.mutex);
1429#endif
1430}
1431
1432static void crVBoxHGCMInstantReclaim(CRConnection *conn, CRMessage *mess)
1433{
1434 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
1435#ifdef CHROMIUM_THREADSAFE
1436 crLockMutex(&g_crvboxhgcm.mutex);
1437#endif
1438 _crVBoxHGCMFree(conn, mess);
1439 CRASSERT(FALSE);
1440#ifdef CHROMIUM_THREADSAFE
1441 crUnlockMutex(&g_crvboxhgcm.mutex);
1442#endif
1443 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1444}
1445
1446static void crVBoxHGCMHandleNewMessage( CRConnection *conn, CRMessage *msg, unsigned int len )
1447{
1448 RT_NOREF(conn, msg, len);
1449 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
1450 CRASSERT(FALSE);
1451 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1452}
1453
1454#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
1455
1456bool _crVBoxHGSMIInit()
1457{
1458#ifndef VBOX_CRHGSMI_WITH_D3DDEV
1459 static
1460#endif
1461 int bHasHGSMI = -1;
1462
1463 if (bHasHGSMI < 0)
1464 {
1465 int rc;
1466 rc = VBoxCrHgsmiInit();
1467 if (RT_SUCCESS(rc))
1468 bHasHGSMI = 1;
1469 else
1470 bHasHGSMI = 0;
1471
1472 crDebug("CrHgsmi is %s", bHasHGSMI ? "ENABLED" : "DISABLED");
1473 }
1474
1475 CRASSERT(bHasHGSMI >= 0);
1476
1477 return bHasHGSMI;
1478}
1479
1480void _crVBoxHGSMITearDown()
1481{
1482 VBoxCrHgsmiTerm();
1483}
1484
1485static void *_crVBoxHGSMIDoAlloc(CRConnection *conn, PCRVBOXHGSMI_CLIENT pClient)
1486{
1487 PVBOXUHGSMI_BUFFER buf;
1488 CRVBOXHGCMBUFFER *pData = NULL;
1489 uint32_t cbSize = conn->buffer_size;
1490 int rc;
1491
1492 buf = _crVBoxHGSMIBufAlloc(pClient, CRVBOXHGSMI_BUF_SIZE(cbSize));
1493 if (buf)
1494 {
1495 VBOXUHGSMI_BUFFER_LOCK_FLAGS fFlags;
1496 buf->pvUserData = pClient;
1497 fFlags.Value = 0;
1498 fFlags.s.fDiscard = 1;
1499 rc = buf->pfnLock(buf, 0, CRVBOXHGSMI_BUF_LOCK_SIZE(cbSize), fFlags, (void**)&pData);
1500 if (RT_SUCCESS(rc))
1501 {
1502 pData->magic = CR_VBOXHGCM_BUFFER_MAGIC;
1503 pData->kind = CR_VBOXHGCM_UHGSMI_BUFFER;
1504 pData->pBuffer = buf;
1505 }
1506 else
1507 {
1508 crWarning("Failed to Lock the buffer, rc(%d)\n", rc);
1509 }
1510 return CRVBOXHGSMI_BUF_DATA(pData);
1511 }
1512 else
1513 {
1514 crWarning("_crVBoxHGSMIBufAlloc failed to allocate buffer of size (%d)", CRVBOXHGSMI_BUF_SIZE(cbSize));
1515 }
1516
1517 /* fall back */
1518 return _crVBoxHGCMAlloc(conn);
1519}
1520
1521static void _crVBoxHGSMIFree(CRConnection *conn, void *buf)
1522{
1523 CRVBOXHGCMBUFFER *hgcm_buffer = (CRVBOXHGCMBUFFER *) buf - 1;
1524
1525 CRASSERT(hgcm_buffer->magic == CR_VBOXHGCM_BUFFER_MAGIC);
1526
1527 if (hgcm_buffer->kind == CR_VBOXHGCM_UHGSMI_BUFFER)
1528 {
1529 PVBOXUHGSMI_BUFFER pBuf = _crVBoxHGSMIBufFromHdr(hgcm_buffer);
1530 PCRVBOXHGSMI_CLIENT pClient = (PCRVBOXHGSMI_CLIENT)pBuf->pvUserData;
1531 _crVBoxHGSMIBufFree(pClient, pBuf);
1532 }
1533 else
1534 {
1535 _crVBoxHGCMFree(conn, buf);
1536 }
1537}
1538
1539static void *crVBoxHGSMIAlloc(CRConnection *conn)
1540{
1541 PCRVBOXHGSMI_CLIENT pClient;
1542 void *pvBuf;
1543
1544 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
1545
1546#ifdef CHROMIUM_THREADSAFE
1547 crLockMutex(&g_crvboxhgcm.mutex);
1548#endif
1549
1550 pClient = _crVBoxHGSMIClientGet(conn);
1551 if (pClient)
1552 {
1553 pvBuf = _crVBoxHGSMIDoAlloc(conn, pClient);
1554 CRASSERT(pvBuf);
1555 }
1556 else
1557 {
1558 pvBuf = _crVBoxHGCMAlloc(conn);
1559 }
1560
1561#ifdef CHROMIUM_THREADSAFE
1562 crUnlockMutex(&g_crvboxhgcm.mutex);
1563#endif
1564
1565 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1566
1567 return pvBuf;
1568}
1569
1570static void crVBoxHGSMIFree(CRConnection *conn, void *buf)
1571{
1572 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
1573#ifdef CHROMIUM_THREADSAFE
1574 crLockMutex(&g_crvboxhgcm.mutex);
1575#endif
1576 _crVBoxHGSMIFree(conn, buf);
1577#ifdef CHROMIUM_THREADSAFE
1578 crUnlockMutex(&g_crvboxhgcm.mutex);
1579#endif
1580 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1581}
1582
1583static void _crVBoxHGSMIPollHost(CRConnection *conn, PCRVBOXHGSMI_CLIENT pClient)
1584{
1585 CRVBOXHGSMIREAD *parms = (CRVBOXHGSMIREAD *)_crVBoxHGSMICmdBufferLock(pClient, sizeof (*parms));
1586 int rc;
1587 VBOXUHGSMI_BUFFER_SUBMIT aSubmit[2];
1588 PVBOXUHGSMI_BUFFER pRecvBuffer;
1589 uint32_t cbBuffer;
1590
1591 CRASSERT(parms);
1592
1593 parms->hdr.result = VERR_WRONG_ORDER;
1594 parms->hdr.u32ClientID = conn->u32ClientID;
1595 parms->hdr.u32Function = SHCRGL_GUEST_FN_READ;
1596/* parms->hdr.u32Reserved = 0;*/
1597
1598 CRASSERT(!conn->pBuffer); /* make sure there's no data to process */
1599 parms->iBuffer = 1;
1600 parms->cbBuffer = 0;
1601
1602 _crVBoxHGSMICmdBufferUnlock(pClient);
1603
1604 pRecvBuffer = _crVBoxHGSMIRecvBufGet(pClient);
1605 CRASSERT(pRecvBuffer);
1606 if (!pRecvBuffer)
1607 return;
1608
1609 _crVBoxHGSMIFillCmd(&aSubmit[0], pClient, sizeof (*parms));
1610
1611 aSubmit[1].pBuf = pRecvBuffer;
1612 aSubmit[1].offData = 0;
1613 aSubmit[1].cbData = pRecvBuffer->cbBuffer;
1614 aSubmit[1].fFlags.Value = 0;
1615 aSubmit[1].fFlags.s.fHostWriteOnly = 1;
1616
1617 rc = pClient->pHgsmi->pfnBufferSubmit(pClient->pHgsmi, aSubmit, 2);
1618 if (RT_FAILURE(rc))
1619 {
1620 crError("pfnBufferSubmit failed with %d \n", rc);
1621 return;
1622 }
1623
1624 parms = (CRVBOXHGSMIREAD *)_crVBoxHGSMICmdBufferLockRo(pClient, sizeof (*parms));
1625 CRASSERT(parms);
1626 if (!parms)
1627 {
1628 crWarning("_crVBoxHGSMICmdBufferLockRo failed\n");
1629 return;
1630 }
1631
1632 if (RT_SUCCESS(parms->hdr.result))
1633 cbBuffer = parms->cbBuffer;
1634 else
1635 cbBuffer = 0;
1636
1637 _crVBoxHGSMICmdBufferUnlock(pClient);
1638
1639 if (cbBuffer)
1640 {
1641 void *pvData = _crVBoxHGSMIRecvBufData(pClient, cbBuffer);
1642 CRASSERT(pvData);
1643 if (pvData)
1644 {
1645 conn->pBuffer = pvData;
1646 conn->cbBuffer = cbBuffer;
1647 }
1648 }
1649}
1650
1651static void _crVBoxHGSMIReadExact(CRConnection *conn, PCRVBOXHGSMI_CLIENT pClient)
1652{
1653 _crVBoxHGSMIPollHost(conn, pClient);
1654
1655 if (conn->cbBuffer)
1656 _crVBoxHGCMReceiveMessage(conn);
1657}
1658
1659/* Same as crVBoxHGCMWriteExact, but combined with read of writeback data.
1660 * This halves the number of HGCM calls we do,
1661 * most likely crVBoxHGCMPollHost shouldn't be called at all now.
1662 */
1663static void
1664_crVBoxHGSMIWriteReadExact(CRConnection *conn, PCRVBOXHGSMI_CLIENT pClient, void *buf, uint32_t offBuffer, unsigned int len, bool bIsBuffer)
1665{
1666 CRVBOXHGSMIWRITEREAD *parms = (CRVBOXHGSMIWRITEREAD*)_crVBoxHGSMICmdBufferLock(pClient, sizeof (*parms));
1667 int rc;
1668 VBOXUHGSMI_BUFFER_SUBMIT aSubmit[3];
1669 PVBOXUHGSMI_BUFFER pBuf = NULL;
1670 VBOXUHGSMI_BUFFER_LOCK_FLAGS fFlags;
1671/* uint32_t cbBuffer;*/
1672
1673 parms->hdr.result = VERR_WRONG_ORDER;
1674 parms->hdr.u32ClientID = conn->u32ClientID;
1675 parms->hdr.u32Function = SHCRGL_GUEST_FN_WRITE_READ;
1676/* parms->hdr.u32Reserved = 0;*/
1677
1678 parms->iBuffer = 1;
1679
1680 CRASSERT(!conn->pBuffer); /* make sure there's no data to process */
1681 parms->iWriteback = 2;
1682 parms->cbWriteback = 0;
1683
1684 _crVBoxHGSMICmdBufferUnlock(pClient);
1685
1686 if (!bIsBuffer)
1687 {
1688 void *pvBuf;
1689 pBuf = _crVBoxHGSMIBufAlloc(pClient, len);
1690
1691 if (!pBuf)
1692 {
1693 /* fallback */
1694 crVBoxHGCMWriteReadExact(conn, buf, len, CR_VBOXHGCM_USERALLOCATED);
1695 return;
1696 }
1697
1698 CRASSERT(!offBuffer);
1699
1700 offBuffer = 0;
1701 fFlags.Value = 0;
1702 fFlags.s.fDiscard = 1;
1703 fFlags.s.fWriteOnly = 1;
1704 rc = pBuf->pfnLock(pBuf, 0, len, fFlags, &pvBuf);
1705 if (RT_SUCCESS(rc))
1706 {
1707 memcpy(pvBuf, buf, len);
1708 rc = pBuf->pfnUnlock(pBuf);
1709 CRASSERT(RT_SUCCESS(rc));
1710 }
1711 else
1712 {
1713 crWarning("_crVBoxHGSMIWriteReadExact: pfnUnlock failed rc %d", rc);
1714 _crVBoxHGSMIBufFree(pClient, pBuf);
1715 /* fallback */
1716 crVBoxHGCMWriteReadExact(conn, buf, len, CR_VBOXHGCM_USERALLOCATED);
1717 return;
1718 }
1719 }
1720 else
1721 {
1722 pBuf = (PVBOXUHGSMI_BUFFER)buf;
1723 }
1724
1725 do
1726 {
1727 PVBOXUHGSMI_BUFFER pRecvBuffer = _crVBoxHGSMIRecvBufGet(pClient);
1728 CRASSERT(pRecvBuffer);
1729 if (!pRecvBuffer)
1730 {
1731 break;
1732 }
1733
1734 _crVBoxHGSMIFillCmd(&aSubmit[0], pClient, sizeof (*parms));
1735
1736 aSubmit[1].pBuf = pBuf;
1737 aSubmit[1].offData = offBuffer;
1738 aSubmit[1].cbData = len;
1739 aSubmit[1].fFlags.Value = 0;
1740 aSubmit[1].fFlags.s.fHostReadOnly = 1;
1741
1742 aSubmit[2].pBuf = pRecvBuffer;
1743 aSubmit[2].offData = 0;
1744 aSubmit[2].cbData = pRecvBuffer->cbBuffer;
1745 aSubmit[2].fFlags.Value = 0;
1746
1747 rc = pClient->pHgsmi->pfnBufferSubmit(pClient->pHgsmi, aSubmit, 3);
1748 if (RT_FAILURE(rc))
1749 {
1750 crError("pfnBufferSubmit failed with %d \n", rc);
1751 break;
1752 }
1753
1754 parms = (CRVBOXHGSMIWRITEREAD *)_crVBoxHGSMICmdBufferLockRo(pClient, sizeof (*parms));
1755 CRASSERT(parms);
1756 if (parms)
1757 {
1758 uint32_t cbWriteback = parms->cbWriteback;
1759 rc = parms->hdr.result;
1760#ifdef DEBUG_misha
1761 /* catch it here to test the buffer */
1762 Assert(RT_SUCCESS(parms->hdr.Hdr.rc) || parms->hdr.Hdr.rc == VERR_BUFFER_OVERFLOW);
1763#endif
1764 _crVBoxHGSMICmdBufferUnlock(pClient);
1765#ifdef DEBUG
1766 parms = NULL;
1767#endif
1768 if (RT_SUCCESS(rc))
1769 {
1770 if (cbWriteback)
1771 {
1772 void *pvData = _crVBoxHGSMIRecvBufData(pClient, cbWriteback);
1773 CRASSERT(pvData);
1774 if (pvData)
1775 {
1776 conn->pBuffer = pvData;
1777 conn->cbBuffer = cbWriteback;
1778 _crVBoxHGCMReceiveMessage(conn);
1779 }
1780 }
1781 }
1782 else if (VERR_BUFFER_OVERFLOW == rc)
1783 {
1784 VBOXUHGSMI_BUFFER_TYPE_FLAGS Flags = {0};
1785 PVBOXUHGSMI_BUFFER pNewBuf;
1786 CRASSERT(!pClient->pvHGBuffer);
1787 CRASSERT(cbWriteback>pClient->pHGBuffer->cbBuffer);
1788 crDebug("Reallocating host buffer from %d to %d bytes", conn->cbHostBufferAllocated, cbWriteback);
1789
1790 rc = pClient->pHgsmi->pfnBufferCreate(pClient->pHgsmi, CRVBOXHGSMI_PAGE_ALIGN(cbWriteback), Flags, &pNewBuf);
1791 if (RT_SUCCESS(rc))
1792 {
1793 rc = pClient->pHGBuffer->pfnDestroy(pClient->pHGBuffer);
1794 CRASSERT(RT_SUCCESS(rc));
1795
1796 pClient->pHGBuffer = pNewBuf;
1797
1798 _crVBoxHGSMIReadExact(conn, pClient/*, cbWriteback*/);
1799 }
1800 else
1801 {
1802 crWarning("_crVBoxHGSMIWriteReadExact: pfnBufferCreate(%d) failed!", CRVBOXHGSMI_PAGE_ALIGN(cbWriteback));
1803 if (conn->cbHostBufferAllocated < cbWriteback)
1804 {
1805 crFree(conn->pHostBuffer);
1806 conn->cbHostBufferAllocated = cbWriteback;
1807 conn->pHostBuffer = crAlloc(conn->cbHostBufferAllocated);
1808 }
1809 crVBoxHGCMReadExact(conn, NULL, cbWriteback);
1810 }
1811 }
1812 else
1813 {
1814 crWarning("SHCRGL_GUEST_FN_WRITE_READ (%i) failed with %x \n", len, rc);
1815 }
1816 }
1817 else
1818 {
1819 crWarning("_crVBoxHGSMICmdBufferLockRo failed\n");
1820 break;
1821 }
1822 } while (0);
1823
1824 if (!bIsBuffer)
1825 _crVBoxHGSMIBufFree(pClient, pBuf);
1826
1827 return;
1828}
1829
1830static void _crVBoxHGSMIWriteExact(CRConnection *conn, PCRVBOXHGSMI_CLIENT pClient, PVBOXUHGSMI_BUFFER pBuf, uint32_t offStart, unsigned int len)
1831{
1832 int rc;
1833 int32_t callRes = VINF_SUCCESS; /* Shut up MSC. */
1834 VBOXUHGSMI_BUFFER_SUBMIT aSubmit[2];
1835
1836#ifdef IN_GUEST
1837 if (conn->u32InjectClientID)
1838 {
1839 CRVBOXHGSMIINJECT *parms = (CRVBOXHGSMIINJECT *)_crVBoxHGSMICmdBufferLock(pClient, sizeof (*parms));
1840 CRASSERT(parms);
1841 if (!parms)
1842 {
1843 return;
1844 }
1845
1846 parms->hdr.result = VERR_WRONG_ORDER;
1847 parms->hdr.u32ClientID = conn->u32ClientID;
1848 parms->hdr.u32Function = SHCRGL_GUEST_FN_INJECT;
1849/* parms->hdr.u32Reserved = 0;*/
1850
1851 parms->u32ClientID = conn->u32InjectClientID;
1852
1853 parms->iBuffer = 1;
1854 _crVBoxHGSMICmdBufferUnlock(pClient);
1855
1856 _crVBoxHGSMIFillCmd(&aSubmit[0], pClient, sizeof (*parms));
1857
1858 aSubmit[1].pBuf = pBuf;
1859 aSubmit[1].offData = offStart;
1860 aSubmit[1].cbData = len;
1861 aSubmit[1].fFlags.Value = 0;
1862 aSubmit[1].fFlags.s.fHostReadOnly = 1;
1863
1864 rc = pClient->pHgsmi->pfnBufferSubmit(pClient->pHgsmi, aSubmit, 2);
1865 if (RT_SUCCESS(rc))
1866 {
1867 callRes = _crVBoxHGSMICmdBufferGetRc(pClient);
1868 }
1869 else
1870 {
1871 /* we can not recover at this point, report error & exit */
1872 crError("pfnBufferSubmit failed with %d \n", rc);
1873 }
1874 }
1875 else
1876#endif
1877 {
1878 CRVBOXHGSMIWRITE * parms = (CRVBOXHGSMIWRITE *)_crVBoxHGSMICmdBufferLock(pClient, sizeof (*parms));;
1879
1880 parms->hdr.result = VERR_WRONG_ORDER;
1881 parms->hdr.u32ClientID = conn->u32ClientID;
1882 parms->hdr.u32Function = SHCRGL_GUEST_FN_WRITE;
1883/* parms->hdr.u32Reserved = 0; */
1884
1885 parms->iBuffer = 1;
1886 _crVBoxHGSMICmdBufferUnlock(pClient);
1887
1888 _crVBoxHGSMIFillCmd(&aSubmit[0], pClient, sizeof (*parms));
1889
1890 aSubmit[1].pBuf = pBuf;
1891 aSubmit[1].offData = offStart;
1892 aSubmit[1].cbData = len;
1893 aSubmit[1].fFlags.Value = 0;
1894 aSubmit[1].fFlags.s.fHostReadOnly = 1;
1895
1896 rc = pClient->pHgsmi->pfnBufferSubmit(pClient->pHgsmi, aSubmit, 2);
1897 if (RT_SUCCESS(rc))
1898 {
1899 callRes = _crVBoxHGSMICmdBufferGetRc(pClient);
1900 }
1901 else
1902 {
1903 /* we can not recover at this point, report error & exit */
1904 crError("Failed to submit CrHhgsmi buffer");
1905 }
1906 }
1907
1908 if (RT_FAILURE(rc) || RT_FAILURE(callRes))
1909 {
1910 crWarning("SHCRGL_GUEST_FN_WRITE failed with %x %x\n", rc, callRes);
1911 }
1912}
1913
1914static void crVBoxHGSMISend(CRConnection *conn, void **bufp,
1915 const void *start, unsigned int len)
1916{
1917 PCRVBOXHGSMI_CLIENT pClient;
1918 PVBOXUHGSMI_BUFFER pBuf;
1919 CRVBOXHGCMBUFFER *hgcm_buffer;
1920
1921 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
1922
1923#ifdef CHROMIUM_THREADSAFE
1924 crLockMutex(&g_crvboxhgcm.mutex);
1925#endif
1926
1927 if (!bufp) /* We're sending a user-allocated buffer. */
1928 {
1929 pClient = _crVBoxHGSMIClientGet(conn);
1930 if (pClient)
1931 {
1932#ifndef IN_GUEST
1933 /** @todo remove temp buffer allocation in unpacker */
1934 /* we're at the host side, so just store data until guest polls us */
1935 _crVBoxHGCMWriteBytes(conn, start, len);
1936#else
1937 CRASSERT(!conn->u32InjectClientID);
1938 crDebug("SHCRGL: sending userbuf with %d bytes\n", len);
1939 _crVBoxHGSMIWriteReadExact(conn, pClient, (void*)start, 0, len, false);
1940#endif
1941#ifdef CHROMIUM_THREADSAFE
1942 crUnlockMutex(&g_crvboxhgcm.mutex);
1943#endif
1944 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1945 return;
1946 }
1947
1948 /* fallback */
1949 crVBoxHGCMSend(conn, bufp, start, len);
1950#ifdef CHROMIUM_THREADSAFE
1951 crUnlockMutex(&g_crvboxhgcm.mutex);
1952#endif
1953 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1954 return;
1955 }
1956
1957 hgcm_buffer = (CRVBOXHGCMBUFFER *) *bufp - 1;
1958 CRASSERT(hgcm_buffer->magic == CR_VBOXHGCM_BUFFER_MAGIC);
1959 if (hgcm_buffer->magic != CR_VBOXHGCM_BUFFER_MAGIC)
1960 {
1961 crError("HGCM buffer magic mismatch");
1962 }
1963
1964
1965 if (hgcm_buffer->kind != CR_VBOXHGCM_UHGSMI_BUFFER)
1966 {
1967 /* fallback */
1968 crVBoxHGCMSend(conn, bufp, start, len);
1969#ifdef CHROMIUM_THREADSAFE
1970 crUnlockMutex(&g_crvboxhgcm.mutex);
1971#endif
1972 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1973 return;
1974 }
1975
1976 /* The region [start .. start + len + 1] lies within a buffer that
1977 * was allocated with crVBoxHGCMAlloc() and can be put into the free
1978 * buffer pool when we're done sending it.
1979 */
1980
1981 pBuf = _crVBoxHGSMIBufFromHdr(hgcm_buffer);
1982 CRASSERT(pBuf);
1983 if (!pBuf)
1984 {
1985 crVBoxHGCMSend(conn, bufp, start, len);
1986#ifdef CHROMIUM_THREADSAFE
1987 crUnlockMutex(&g_crvboxhgcm.mutex);
1988#endif
1989 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1990 return;
1991 }
1992
1993 pClient = (PCRVBOXHGSMI_CLIENT)pBuf->pvUserData;
1994 if (pClient != &conn->HgsmiClient)
1995 {
1996 crError("HGSMI client mismatch");
1997 }
1998
1999 /* Length would be passed as part of HGCM pointer description
2000 * No need to prepend it to the buffer
2001 */
2002#ifdef IN_GUEST
2003 if (conn->u32InjectClientID)
2004 {
2005 _crVBoxHGSMIWriteExact(conn, pClient, pBuf, CRVBOXHGSMI_BUF_OFFSET(start, *bufp) + CRVBOXHGSMI_BUF_HDR_SIZE(), len);
2006 }
2007 else
2008#endif
2009 {
2010 _crVBoxHGSMIWriteReadExact(conn, pClient, pBuf, CRVBOXHGSMI_BUF_OFFSET(start, *bufp) + CRVBOXHGSMI_BUF_HDR_SIZE(), len, true);
2011 }
2012
2013 /* Reclaim this pointer for reuse */
2014 _crVBoxHGSMIBufFree(pClient, pBuf);
2015 /* Since the buffer's now in the 'free' buffer pool, the caller can't
2016 * use it any more. Setting bufp to NULL will make sure the caller
2017 * doesn't try to re-use the buffer.
2018 */
2019 *bufp = NULL;
2020
2021#ifdef CHROMIUM_THREADSAFE
2022 crUnlockMutex(&g_crvboxhgcm.mutex);
2023#endif
2024
2025 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2026}
2027
2028static void crVBoxHGSMIWriteExact(CRConnection *conn, const void *buf, unsigned int len)
2029{
2030 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
2031
2032 CRASSERT(0);
2033
2034 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2035}
2036
2037static void crVBoxHGSMISingleRecv(CRConnection *conn, void *buf, unsigned int len)
2038{
2039 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
2040
2041 CRASSERT(0);
2042
2043 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2044}
2045
2046static void crVBoxHGSMIReceiveMessage(CRConnection *conn)
2047{
2048 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
2049
2050#ifdef CHROMIUM_THREADSAFE
2051 crLockMutex(&g_crvboxhgcm.mutex);
2052#endif
2053
2054 CRASSERT(0);
2055
2056 _crVBoxHGCMReceiveMessage(conn);
2057
2058#ifdef CHROMIUM_THREADSAFE
2059 crUnlockMutex(&g_crvboxhgcm.mutex);
2060#endif
2061
2062 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2063}
2064
2065/*
2066 * Called on host side only, to "accept" client connection
2067 */
2068static void crVBoxHGSMIAccept( CRConnection *conn)
2069{
2070 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
2071 CRASSERT(0);
2072
2073 CRASSERT(conn && conn->pHostBuffer);
2074#ifdef IN_GUEST
2075 CRASSERT(FALSE);
2076#endif
2077 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2078}
2079
2080static int crVBoxHGSMIDoConnect( CRConnection *conn )
2081{
2082 PCRVBOXHGSMI_CLIENT pClient;
2083 int rc = VINF_SUCCESS;
2084
2085#ifdef CHROMIUM_THREADSAFE
2086 crLockMutex(&g_crvboxhgcm.mutex);
2087#endif
2088
2089 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
2090
2091 pClient = _crVBoxHGSMIClientGet(conn);
2092 if (pClient)
2093 {
2094 rc = VBoxCrHgsmiCtlConGetClientID(pClient->pHgsmi, &conn->u32ClientID);
2095 if (RT_FAILURE(rc))
2096 {
2097 WARN(("VBoxCrHgsmiCtlConGetClientID failed %d", rc));
2098 }
2099 if (!g_crvboxhgcm.fHostCapsInitialized)
2100 {
2101 rc = VBoxCrHgsmiCtlConGetHostCaps(pClient->pHgsmi, &g_crvboxhgcm.u32HostCaps);
2102 if (RT_SUCCESS(rc))
2103 {
2104 g_crvboxhgcm.fHostCapsInitialized = true;
2105 }
2106 else
2107 {
2108 WARN(("VBoxCrHgsmiCtlConGetHostCaps failed %d", rc));
2109 g_crvboxhgcm.u32HostCaps = 0;
2110 }
2111 }
2112 }
2113 else
2114 {
2115 WARN(("_crVBoxHGSMIClientGet failed %d", rc));
2116 rc = VERR_GENERAL_FAILURE;
2117 }
2118
2119 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2120
2121#ifdef CHROMIUM_THREADSAFE
2122 crUnlockMutex(&g_crvboxhgcm.mutex);
2123#endif
2124 return RT_SUCCESS(rc);
2125}
2126
2127static void crVBoxHGSMIDoDisconnect( CRConnection *conn )
2128{
2129 bool fHasActiveCons = false;
2130
2131 if (!g_crvboxhgcm.initialized) return;
2132
2133 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
2134
2135#ifdef CHROMIUM_THREADSAFE
2136 crLockMutex(&g_crvboxhgcm.mutex);
2137#endif
2138
2139 fHasActiveCons = _crVBoxCommonDoDisconnectLocked(conn);
2140
2141#ifndef VBOX_CRHGSMI_WITH_D3DDEV
2142 if (conn->HgsmiClient.pHgsmi)
2143 {
2144 PVBOXUHGSMI pHgsmi;
2145 _crVBoxHGSMIClientTerm(&conn->HgsmiClient, &pHgsmi);
2146 CRASSERT(pHgsmi);
2147 if (!conn->pExternalHgsmi)
2148 VBoxCrHgsmiDestroy(pHgsmi);
2149 }
2150#else
2151# error "port me!"
2152#endif
2153
2154 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2155
2156#ifdef CHROMIUM_THREADSAFE
2157 crUnlockMutex(&g_crvboxhgcm.mutex);
2158#endif
2159}
2160
2161static void crVBoxHGSMIInstantReclaim(CRConnection *conn, CRMessage *mess)
2162{
2163 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
2164#ifdef CHROMIUM_THREADSAFE
2165 crLockMutex(&g_crvboxhgcm.mutex);
2166#endif
2167 CRASSERT(0);
2168
2169 _crVBoxHGSMIFree(conn, mess);
2170
2171#ifdef CHROMIUM_THREADSAFE
2172 crUnlockMutex(&g_crvboxhgcm.mutex);
2173#endif
2174 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2175}
2176
2177static void crVBoxHGSMIHandleNewMessage( CRConnection *conn, CRMessage *msg, unsigned int len )
2178{
2179 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
2180 CRASSERT(0);
2181
2182 CRASSERT(FALSE);
2183 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2184}
2185#endif
2186
2187void crVBoxHGCMInit(CRNetReceiveFuncList *rfl, CRNetCloseFuncList *cfl)
2188{
2189 g_crvboxhgcm.recv_list = rfl;
2190 g_crvboxhgcm.close_list = cfl;
2191 if (g_crvboxhgcm.initialized)
2192 {
2193 return;
2194 }
2195
2196 VBOXCRHGSMIPROFILE_INIT();
2197
2198 g_crvboxhgcm.initialized = 1;
2199
2200#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
2201 g_crvboxhgcm.bHgsmiOn = _crVBoxHGSMIInit();
2202#endif
2203
2204 g_crvboxhgcm.num_conns = 0;
2205 g_crvboxhgcm.conns = NULL;
2206
2207 /* Can't open VBox guest driver here, because it gets called for host side as well */
2208 /** @todo as we have 2 dll versions, can do it now.*/
2209
2210#ifdef RT_OS_WINDOWS
2211 g_crvboxhgcm.pDirectDraw = NULL;
2212#endif
2213
2214#ifdef CHROMIUM_THREADSAFE
2215 crInitMutex(&g_crvboxhgcm.mutex);
2216 crInitMutex(&g_crvboxhgcm.recvmutex);
2217#endif
2218 g_crvboxhgcm.bufpool = crBufferPoolInit(16);
2219
2220#ifdef IN_GUEST
2221 g_crvboxhgcm.fHostCapsInitialized = false;
2222 g_crvboxhgcm.u32HostCaps = 0;
2223#endif
2224}
2225
2226/* Callback function used to free buffer pool entries */
2227static void crVBoxHGCMBufferFree(void *data)
2228{
2229 CRVBOXHGCMBUFFER *hgcm_buffer = (CRVBOXHGCMBUFFER *) data;
2230
2231 CRASSERT(hgcm_buffer->magic == CR_VBOXHGCM_BUFFER_MAGIC);
2232
2233 switch (hgcm_buffer->kind)
2234 {
2235 case CR_VBOXHGCM_MEMORY:
2236 crDebug("crVBoxHGCMBufferFree: CR_VBOXHGCM_MEMORY: %p", hgcm_buffer);
2237 crFree( hgcm_buffer );
2238 break;
2239 case CR_VBOXHGCM_MEMORY_BIG:
2240 crDebug("crVBoxHGCMBufferFree: CR_VBOXHGCM_MEMORY_BIG: %p", hgcm_buffer);
2241 crFree( hgcm_buffer );
2242 break;
2243
2244 default:
2245 crError( "Weird buffer kind trying to free in crVBoxHGCMBufferFree: %d", hgcm_buffer->kind );
2246 }
2247}
2248
2249void crVBoxHGCMTearDown(void)
2250{
2251 int32_t i, cCons;
2252
2253 if (!g_crvboxhgcm.initialized) return;
2254
2255#ifdef CHROMIUM_THREADSAFE
2256 crLockMutex(&g_crvboxhgcm.mutex);
2257#endif
2258
2259 /* Connection count would be changed in calls to crNetDisconnect, so we have to store original value.
2260 * Walking array backwards is not a good idea as it could cause some issues if we'd disconnect clients not in the
2261 * order of their connection.
2262 */
2263 cCons = g_crvboxhgcm.num_conns;
2264 for (i=0; i<cCons; i++)
2265 {
2266 /* Note that [0] is intended, as the connections array would be shifted in each call to crNetDisconnect */
2267 crNetDisconnect(g_crvboxhgcm.conns[0]);
2268 }
2269 CRASSERT(0==g_crvboxhgcm.num_conns);
2270
2271 g_crvboxhgcm.initialized = 0;
2272
2273 if (g_crvboxhgcm.bufpool)
2274 crBufferPoolCallbackFree(g_crvboxhgcm.bufpool, crVBoxHGCMBufferFree);
2275 g_crvboxhgcm.bufpool = NULL;
2276
2277#ifdef CHROMIUM_THREADSAFE
2278 crUnlockMutex(&g_crvboxhgcm.mutex);
2279 crFreeMutex(&g_crvboxhgcm.mutex);
2280 crFreeMutex(&g_crvboxhgcm.recvmutex);
2281#endif
2282
2283 crFree(g_crvboxhgcm.conns);
2284 g_crvboxhgcm.conns = NULL;
2285
2286#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
2287 if (g_crvboxhgcm.bHgsmiOn)
2288 {
2289 _crVBoxHGSMITearDown();
2290 }
2291#endif
2292
2293#ifdef RT_OS_WINDOWS
2294 if (g_crvboxhgcm.pDirectDraw)
2295 {
2296 IDirectDraw_Release(g_crvboxhgcm.pDirectDraw);
2297 g_crvboxhgcm.pDirectDraw = NULL;
2298 crDebug("DirectDraw released\n");
2299 }
2300#endif
2301}
2302
2303void crVBoxHGCMConnection(CRConnection *conn
2304#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
2305 , struct VBOXUHGSMI *pHgsmi
2306#endif
2307 )
2308{
2309 int i, found = 0;
2310 int n_bytes;
2311
2312 CRASSERT(g_crvboxhgcm.initialized);
2313
2314#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
2315 if (g_crvboxhgcm.bHgsmiOn)
2316 {
2317 conn->type = CR_VBOXHGCM;
2318 conn->Alloc = crVBoxHGSMIAlloc;
2319 conn->Send = crVBoxHGSMISend;
2320 conn->SendExact = crVBoxHGSMIWriteExact;
2321 conn->Recv = crVBoxHGSMISingleRecv;
2322 conn->RecvMsg = crVBoxHGSMIReceiveMessage;
2323 conn->Free = crVBoxHGSMIFree;
2324 conn->Accept = crVBoxHGSMIAccept;
2325 conn->Connect = crVBoxHGSMIDoConnect;
2326 conn->Disconnect = crVBoxHGSMIDoDisconnect;
2327 conn->InstantReclaim = crVBoxHGSMIInstantReclaim;
2328 conn->HandleNewMessage = crVBoxHGSMIHandleNewMessage;
2329 conn->pExternalHgsmi = pHgsmi;
2330 }
2331 else
2332#endif
2333 {
2334 conn->type = CR_VBOXHGCM;
2335 conn->Alloc = crVBoxHGCMAlloc;
2336 conn->Send = crVBoxHGCMSend;
2337 conn->SendExact = crVBoxHGCMWriteExact;
2338 conn->Recv = crVBoxHGCMSingleRecv;
2339 conn->RecvMsg = crVBoxHGCMReceiveMessage;
2340 conn->Free = crVBoxHGCMFree;
2341 conn->Accept = crVBoxHGCMAccept;
2342 conn->Connect = crVBoxHGCMDoConnect;
2343 conn->Disconnect = crVBoxHGCMDoDisconnect;
2344 conn->InstantReclaim = crVBoxHGCMInstantReclaim;
2345 conn->HandleNewMessage = crVBoxHGCMHandleNewMessage;
2346 }
2347 conn->sizeof_buffer_header = sizeof(CRVBOXHGCMBUFFER);
2348 conn->actual_network = 1;
2349
2350 conn->krecv_buf_size = 0;
2351
2352 conn->pBuffer = NULL;
2353 conn->cbBuffer = 0;
2354 conn->allow_redir_ptr = 1;
2355
2356 /** @todo remove this crap at all later */
2357 conn->cbHostBufferAllocated = 2*1024;
2358 conn->pHostBuffer = (uint8_t*) crAlloc(conn->cbHostBufferAllocated);
2359 CRASSERT(conn->pHostBuffer);
2360 conn->cbHostBuffer = 0;
2361
2362#if !defined(IN_GUEST)
2363 RTListInit(&conn->PendingMsgList);
2364#endif
2365
2366#ifdef CHROMIUM_THREADSAFE
2367 crLockMutex(&g_crvboxhgcm.mutex);
2368#endif
2369 /* Find a free slot */
2370 for (i = 0; i < g_crvboxhgcm.num_conns; i++) {
2371 if (g_crvboxhgcm.conns[i] == NULL) {
2372 conn->index = i;
2373 g_crvboxhgcm.conns[i] = conn;
2374 found = 1;
2375 break;
2376 }
2377 }
2378
2379 /* Realloc connection stack if we couldn't find a free slot */
2380 if (found == 0) {
2381 n_bytes = ( g_crvboxhgcm.num_conns + 1 ) * sizeof(*g_crvboxhgcm.conns);
2382 crRealloc( (void **) &g_crvboxhgcm.conns, n_bytes );
2383 conn->index = g_crvboxhgcm.num_conns;
2384 g_crvboxhgcm.conns[g_crvboxhgcm.num_conns++] = conn;
2385 }
2386#ifdef CHROMIUM_THREADSAFE
2387 crUnlockMutex(&g_crvboxhgcm.mutex);
2388#endif
2389}
2390
2391#if defined(IN_GUEST)
2392static void _crVBoxHGCMPerformPollHost(CRConnection *conn)
2393{
2394 if (conn->type == CR_NO_CONNECTION )
2395 return;
2396
2397 if (!conn->pBuffer)
2398 {
2399#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
2400 PCRVBOXHGSMI_CLIENT pClient;
2401 if (g_crvboxhgcm.bHgsmiOn && !!(pClient = _crVBoxHGSMIClientGet(conn)))
2402 {
2403 _crVBoxHGSMIPollHost(conn, pClient);
2404 }
2405 else
2406#endif
2407 {
2408 crVBoxHGCMPollHost(conn);
2409 }
2410 }
2411}
2412#endif
2413
2414static void _crVBoxHGCMPerformReceiveMessage(CRConnection *conn)
2415{
2416 if ( conn->type == CR_NO_CONNECTION )
2417 return;
2418
2419 if (conn->cbBuffer>0)
2420 {
2421 _crVBoxHGCMReceiveMessage(conn);
2422 }
2423}
2424
2425#ifdef IN_GUEST
2426uint32_t crVBoxHGCMHostCapsGet(void)
2427{
2428 Assert(g_crvboxhgcm.fHostCapsInitialized);
2429 return g_crvboxhgcm.u32HostCaps;
2430}
2431#endif
2432
2433int crVBoxHGCMRecv(
2434#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
2435 CRConnection *conn
2436#else
2437 void
2438#endif
2439 )
2440{
2441 int32_t i;
2442
2443 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
2444
2445#ifdef CHROMIUM_THREADSAFE
2446 crLockMutex(&g_crvboxhgcm.mutex);
2447#endif
2448
2449#ifdef IN_GUEST
2450# if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
2451 if (conn && g_crvboxhgcm.bHgsmiOn)
2452 {
2453 _crVBoxHGCMPerformPollHost(conn);
2454 _crVBoxHGCMPerformReceiveMessage(conn);
2455 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2456 return 0;
2457 }
2458# endif
2459 /* we're on guest side, poll host if it got something for us */
2460 for (i=0; i<g_crvboxhgcm.num_conns; i++)
2461 {
2462 CRConnection *conn = g_crvboxhgcm.conns[i];
2463
2464 if ( !conn )
2465 continue;
2466
2467 _crVBoxHGCMPerformPollHost(conn);
2468 }
2469#endif
2470
2471 for (i=0; i<g_crvboxhgcm.num_conns; i++)
2472 {
2473 CRConnection *conn = g_crvboxhgcm.conns[i];
2474
2475 if ( !conn )
2476 continue;
2477
2478 _crVBoxHGCMPerformReceiveMessage(conn);
2479 }
2480
2481#ifdef CHROMIUM_THREADSAFE
2482 crUnlockMutex(&g_crvboxhgcm.mutex);
2483#endif
2484
2485 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2486
2487 return 0;
2488}
2489
2490CRConnection** crVBoxHGCMDump( int *num )
2491{
2492 *num = g_crvboxhgcm.num_conns;
2493
2494 return g_crvboxhgcm.conns;
2495}
Note: See TracBrowser for help on using the repository browser.

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