VirtualBox

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

Last change on this file since 51330 was 51330, checked in by vboxsync, 11 years ago

crOpenGL: more extended flexible caps info, backwards compatibility fixes

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