VirtualBox

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

Last change on this file since 38314 was 38314, checked in by vboxsync, 13 years ago

crogl: fix hgcm con raisings, that cause WinSAT, immage viewer, etc. crashes

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