VirtualBox

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

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

crOpenGL: fix hgcm

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 76.2 KB
Line 
1/* $Id: vboxhgcm.c 51160 2014-04-29 09:41:03Z 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 crVBoxHGCMGetHostCaps(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;
1297 caps.hdr.cParms = SHCRGL_CPARMS_GET_CAPS;
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
1324
1325static int crVBoxHGCMSetPID(CRConnection *conn, unsigned long long pid)
1326{
1327 CRVBOXHGCMSETPID parms;
1328 int rc;
1329
1330 parms.hdr.result = VERR_WRONG_ORDER;
1331 parms.hdr.u32ClientID = conn->u32ClientID;
1332 parms.hdr.u32Function = SHCRGL_GUEST_FN_SET_PID;
1333 parms.hdr.cParms = SHCRGL_CPARMS_SET_PID;
1334
1335 parms.u64PID.type = VMMDevHGCMParmType_64bit;
1336 parms.u64PID.u.value64 = pid;
1337
1338 rc = crVBoxHGCMCall(conn, &parms, sizeof(parms));
1339
1340 if (RT_FAILURE(rc) || RT_FAILURE(parms.hdr.result))
1341 {
1342 crWarning("SHCRGL_GUEST_FN_SET_PID failed!");
1343 return FALSE;
1344 }
1345
1346 return TRUE;
1347}
1348
1349/**
1350 * The function that actually connects. This should only be called by clients,
1351 * guests in vbox case.
1352 * Servers go through crVBoxHGCMAccept;
1353 */
1354/*@todo use vbglR3Something here */
1355static int crVBoxHGCMDoConnect( CRConnection *conn )
1356{
1357#ifdef IN_GUEST
1358 VBoxGuestHGCMConnectInfo info;
1359
1360#ifdef RT_OS_WINDOWS
1361 DWORD cbReturned;
1362
1363 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
1364
1365 if (g_crvboxhgcm.hGuestDrv == INVALID_HANDLE_VALUE)
1366 {
1367 /* open VBox guest driver */
1368 g_crvboxhgcm.hGuestDrv = CreateFile(VBOXGUEST_DEVICE_NAME,
1369 GENERIC_READ | GENERIC_WRITE,
1370 FILE_SHARE_READ | FILE_SHARE_WRITE,
1371 NULL,
1372 OPEN_EXISTING,
1373 FILE_ATTRIBUTE_NORMAL,
1374 NULL);
1375
1376 /* @todo check if we could rollback to softwareopengl */
1377 if (g_crvboxhgcm.hGuestDrv == INVALID_HANDLE_VALUE)
1378 {
1379 crWarning("could not open VBox Guest Additions driver! rc = %d\n", GetLastError());
1380 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1381 return FALSE;
1382 }
1383 }
1384#else
1385 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
1386 if (g_crvboxhgcm.iGuestDrv == INVALID_HANDLE_VALUE)
1387 {
1388 g_crvboxhgcm.iGuestDrv = open(VBOXGUEST_USER_DEVICE_NAME, O_RDWR, 0);
1389 if (g_crvboxhgcm.iGuestDrv == INVALID_HANDLE_VALUE)
1390 {
1391 crDebug("could not open Guest Additions kernel module! rc = %d\n", errno);
1392 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1393 return FALSE;
1394 }
1395 }
1396#endif
1397
1398 memset (&info, 0, sizeof (info));
1399 info.Loc.type = VMMDevHGCMLoc_LocalHost_Existing;
1400 strcpy (info.Loc.u.host.achName, "VBoxSharedCrOpenGL");
1401
1402#ifdef RT_OS_WINDOWS
1403 if (DeviceIoControl(g_crvboxhgcm.hGuestDrv,
1404 VBOXGUEST_IOCTL_HGCM_CONNECT,
1405 &info, sizeof (info),
1406 &info, sizeof (info),
1407 &cbReturned,
1408 NULL))
1409#elif defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
1410 VBGLBIGREQ Hdr;
1411 Hdr.u32Magic = VBGLBIGREQ_MAGIC;
1412 Hdr.cbData = sizeof(info);
1413 Hdr.pvDataR3 = &info;
1414# if HC_ARCH_BITS == 32
1415 Hdr.u32Padding = 0;
1416# endif
1417 if (ioctl(g_crvboxhgcm.iGuestDrv, VBOXGUEST_IOCTL_HGCM_CONNECT, &Hdr) >= 0)
1418#else
1419 if (ioctl(g_crvboxhgcm.iGuestDrv, VBOXGUEST_IOCTL_HGCM_CONNECT, &info, sizeof (info)) >= 0)
1420#endif
1421 {
1422 if (info.result == VINF_SUCCESS)
1423 {
1424 int rc;
1425 conn->u32ClientID = info.u32ClientID;
1426 crDebug("HGCM connect was successful: client id =0x%x\n", conn->u32ClientID);
1427
1428 rc = crVBoxHGCMSetVersion(conn, CR_PROTOCOL_VERSION_MAJOR, CR_PROTOCOL_VERSION_MINOR);
1429 if (RT_FAILURE(rc))
1430 {
1431 WARN(("crVBoxHGCMSetVersion failed %d", rc));
1432 return FALSE;
1433 }
1434#ifdef RT_OS_WINDOWS
1435 rc = crVBoxHGCMSetPID(conn, GetCurrentProcessId());
1436#else
1437 rc = crVBoxHGCMSetPID(conn, crGetPID());
1438#endif
1439 if (RT_FAILURE(rc))
1440 {
1441 WARN(("crVBoxHGCMSetPID failed %d", rc));
1442 return FALSE;
1443 }
1444
1445 if (!g_crvboxhgcm.fHostCapsInitialized)
1446 {
1447 rc = crVBoxHGCMGetHostCaps(conn, &g_crvboxhgcm.u32HostCaps);
1448 if (RT_FAILURE(rc))
1449 {
1450 WARN(("VBoxCrHgsmiCtlConGetHostCaps failed %d", rc));
1451 g_crvboxhgcm.u32HostCaps = 0;
1452 }
1453
1454 /* host may not support it, ignore any failures */
1455 g_crvboxhgcm.fHostCapsInitialized = true;
1456 rc = VINF_SUCCESS;
1457 }
1458
1459 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1460 return RT_SUCCESS(rc);
1461 }
1462 else
1463 {
1464 crDebug("HGCM connect failed with rc=0x%x\n", info.result);
1465
1466 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1467 return FALSE;
1468 }
1469 }
1470 else
1471 {
1472#ifdef RT_OS_WINDOWS
1473 DWORD winEr = GetLastError();
1474 crDebug("IOCTL for HGCM connect failed with rc=0x%x\n", winEr);
1475#else
1476 crDebug("IOCTL for HGCM connect failed with rc=0x%x\n", errno);
1477#endif
1478 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1479 return FALSE;
1480 }
1481
1482 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1483 return FALSE;
1484
1485#else /*#ifdef IN_GUEST*/
1486 crError("crVBoxHGCMDoConnect called on host side!");
1487 CRASSERT(FALSE);
1488 return FALSE;
1489#endif
1490}
1491
1492static bool _crVBoxCommonDoDisconnectLocked( CRConnection *conn )
1493{
1494 int i;
1495 if (conn->pHostBuffer)
1496 {
1497 crFree(conn->pHostBuffer);
1498 conn->pHostBuffer = NULL;
1499 conn->cbHostBuffer = 0;
1500 conn->cbHostBufferAllocated = 0;
1501 }
1502
1503 conn->pBuffer = NULL;
1504 conn->cbBuffer = 0;
1505
1506 if (conn->type == CR_VBOXHGCM)
1507 {
1508 --g_crvboxhgcm.num_conns;
1509
1510 if (conn->index < g_crvboxhgcm.num_conns)
1511 {
1512 g_crvboxhgcm.conns[conn->index] = g_crvboxhgcm.conns[g_crvboxhgcm.num_conns];
1513 g_crvboxhgcm.conns[conn->index]->index = conn->index;
1514 }
1515 else g_crvboxhgcm.conns[conn->index] = NULL;
1516
1517 conn->type = CR_NO_CONNECTION;
1518 }
1519
1520 for (i = 0; i < g_crvboxhgcm.num_conns; i++)
1521 if (g_crvboxhgcm.conns[i] && g_crvboxhgcm.conns[i]->type != CR_NO_CONNECTION)
1522 return true;
1523 return false;
1524}
1525
1526/*@todo same, replace DeviceIoControl with vbglR3DoIOCtl */
1527static void crVBoxHGCMDoDisconnect( CRConnection *conn )
1528{
1529#ifdef IN_GUEST
1530 VBoxGuestHGCMDisconnectInfo info;
1531# ifdef RT_OS_WINDOWS
1532 DWORD cbReturned;
1533# endif
1534#endif
1535 bool fHasActiveCons = false;
1536
1537 if (!g_crvboxhgcm.initialized) return;
1538
1539#ifdef CHROMIUM_THREADSAFE
1540 crLockMutex(&g_crvboxhgcm.mutex);
1541#endif
1542
1543 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
1544
1545 fHasActiveCons = _crVBoxCommonDoDisconnectLocked(conn);
1546
1547#ifndef IN_GUEST
1548#else /* IN_GUEST */
1549 if (conn->u32ClientID)
1550 {
1551 memset (&info, 0, sizeof (info));
1552 info.u32ClientID = conn->u32ClientID;
1553
1554# ifdef RT_OS_WINDOWS
1555 if ( !DeviceIoControl(g_crvboxhgcm.hGuestDrv,
1556 VBOXGUEST_IOCTL_HGCM_DISCONNECT,
1557 &info, sizeof (info),
1558 &info, sizeof (info),
1559 &cbReturned,
1560 NULL) )
1561 {
1562 crDebug("Disconnect failed with %x\n", GetLastError());
1563 }
1564# elif defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
1565 VBGLBIGREQ Hdr;
1566 Hdr.u32Magic = VBGLBIGREQ_MAGIC;
1567 Hdr.cbData = sizeof(info);
1568 Hdr.pvDataR3 = &info;
1569# if HC_ARCH_BITS == 32
1570 Hdr.u32Padding = 0;
1571# endif
1572 if (ioctl(g_crvboxhgcm.iGuestDrv, VBOXGUEST_IOCTL_HGCM_DISCONNECT, &Hdr) >= 0)
1573# else
1574 if (ioctl(g_crvboxhgcm.iGuestDrv, VBOXGUEST_IOCTL_HGCM_DISCONNECT, &info, sizeof (info)) < 0)
1575 {
1576 crDebug("Disconnect failed with %x\n", errno);
1577 }
1578# endif
1579
1580 conn->u32ClientID = 0;
1581 }
1582
1583 /* close guest additions driver*/
1584 if (!fHasActiveCons)
1585 {
1586# ifdef RT_OS_WINDOWS
1587 CloseHandle(g_crvboxhgcm.hGuestDrv);
1588 g_crvboxhgcm.hGuestDrv = INVALID_HANDLE_VALUE;
1589# else
1590 close(g_crvboxhgcm.iGuestDrv);
1591 g_crvboxhgcm.iGuestDrv = INVALID_HANDLE_VALUE;
1592# endif
1593 }
1594#endif /* IN_GUEST */
1595
1596 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1597
1598#ifdef CHROMIUM_THREADSAFE
1599 crUnlockMutex(&g_crvboxhgcm.mutex);
1600#endif
1601}
1602
1603static void crVBoxHGCMInstantReclaim(CRConnection *conn, CRMessage *mess)
1604{
1605 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
1606#ifdef CHROMIUM_THREADSAFE
1607 crLockMutex(&g_crvboxhgcm.mutex);
1608#endif
1609 _crVBoxHGCMFree(conn, mess);
1610 CRASSERT(FALSE);
1611#ifdef CHROMIUM_THREADSAFE
1612 crUnlockMutex(&g_crvboxhgcm.mutex);
1613#endif
1614 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1615}
1616
1617static void crVBoxHGCMHandleNewMessage( CRConnection *conn, CRMessage *msg, unsigned int len )
1618{
1619 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
1620 CRASSERT(FALSE);
1621 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1622}
1623
1624#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
1625
1626bool _crVBoxHGSMIInit()
1627{
1628#ifndef VBOX_CRHGSMI_WITH_D3DDEV
1629 static
1630#endif
1631 int bHasHGSMI = -1;
1632
1633 if (bHasHGSMI < 0)
1634 {
1635 int rc;
1636#ifndef VBOX_CRHGSMI_WITH_D3DDEV
1637 rc = VBoxCrHgsmiInit(CR_PROTOCOL_VERSION_MAJOR, CR_PROTOCOL_VERSION_MINOR);
1638#else
1639 VBOXCRHGSMI_CALLBACKS Callbacks;
1640 Callbacks.pfnClientCreate = _crVBoxHGSMIClientCreate;
1641 Callbacks.pfnClientDestroy = _crVBoxHGSMIClientDestroy;
1642 rc = VBoxCrHgsmiInit(&Callbacks);
1643#endif
1644 if (RT_SUCCESS(rc))
1645 bHasHGSMI = 1;
1646 else
1647 bHasHGSMI = 0;
1648
1649 crDebug("CrHgsmi is %s", bHasHGSMI ? "ENABLED" : "DISABLED");
1650 }
1651
1652 CRASSERT(bHasHGSMI >= 0);
1653
1654 return bHasHGSMI;
1655}
1656
1657void _crVBoxHGSMITearDown()
1658{
1659 VBoxCrHgsmiTerm();
1660}
1661
1662static void *_crVBoxHGSMIDoAlloc(CRConnection *conn, PCRVBOXHGSMI_CLIENT pClient)
1663{
1664 PVBOXUHGSMI_BUFFER buf;
1665 CRVBOXHGCMBUFFER *pData = NULL;
1666 uint32_t cbSize = conn->buffer_size;
1667 int rc;
1668
1669 buf = _crVBoxHGSMIBufAlloc(pClient, CRVBOXHGSMI_BUF_SIZE(cbSize));
1670 if (buf)
1671 {
1672 VBOXUHGSMI_BUFFER_LOCK_FLAGS fFlags;
1673 buf->pvUserData = pClient;
1674 fFlags.Value = 0;
1675 fFlags.bDiscard = 1;
1676 rc = buf->pfnLock(buf, 0, CRVBOXHGSMI_BUF_LOCK_SIZE(cbSize), fFlags, (void**)&pData);
1677 if (RT_SUCCESS(rc))
1678 {
1679 pData->magic = CR_VBOXHGCM_BUFFER_MAGIC;
1680 pData->kind = CR_VBOXHGCM_UHGSMI_BUFFER;
1681 pData->pBuffer = buf;
1682 }
1683 else
1684 {
1685 crWarning("Failed to Lock the buffer, rc(%d)\n", rc);
1686 }
1687 return CRVBOXHGSMI_BUF_DATA(pData);
1688 }
1689 else
1690 {
1691 crWarning("_crVBoxHGSMIBufAlloc failed to allocate buffer of size (%d)", CRVBOXHGSMI_BUF_SIZE(cbSize));
1692 }
1693
1694 /* fall back */
1695 return _crVBoxHGCMAlloc(conn);
1696}
1697
1698static void _crVBoxHGSMIFree(CRConnection *conn, void *buf)
1699{
1700 CRVBOXHGCMBUFFER *hgcm_buffer = (CRVBOXHGCMBUFFER *) buf - 1;
1701
1702 CRASSERT(hgcm_buffer->magic == CR_VBOXHGCM_BUFFER_MAGIC);
1703
1704 if (hgcm_buffer->kind == CR_VBOXHGCM_UHGSMI_BUFFER)
1705 {
1706 PVBOXUHGSMI_BUFFER pBuf = _crVBoxHGSMIBufFromHdr(hgcm_buffer);
1707 PCRVBOXHGSMI_CLIENT pClient = (PCRVBOXHGSMI_CLIENT)pBuf->pvUserData;
1708 _crVBoxHGSMIBufFree(pClient, pBuf);
1709 }
1710 else
1711 {
1712 _crVBoxHGCMFree(conn, buf);
1713 }
1714}
1715
1716static void *crVBoxHGSMIAlloc(CRConnection *conn)
1717{
1718 PCRVBOXHGSMI_CLIENT pClient;
1719 void *pvBuf;
1720
1721 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
1722
1723#ifdef CHROMIUM_THREADSAFE
1724 crLockMutex(&g_crvboxhgcm.mutex);
1725#endif
1726
1727 pClient = _crVBoxHGSMIClientGet(conn);
1728 if (pClient)
1729 {
1730 pvBuf = _crVBoxHGSMIDoAlloc(conn, pClient);
1731 CRASSERT(pvBuf);
1732 }
1733 else
1734 {
1735 pvBuf = _crVBoxHGCMAlloc(conn);
1736 }
1737
1738#ifdef CHROMIUM_THREADSAFE
1739 crUnlockMutex(&g_crvboxhgcm.mutex);
1740#endif
1741
1742 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1743
1744 return pvBuf;
1745}
1746
1747static void crVBoxHGSMIFree(CRConnection *conn, void *buf)
1748{
1749 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
1750#ifdef CHROMIUM_THREADSAFE
1751 crLockMutex(&g_crvboxhgcm.mutex);
1752#endif
1753 _crVBoxHGSMIFree(conn, buf);
1754#ifdef CHROMIUM_THREADSAFE
1755 crUnlockMutex(&g_crvboxhgcm.mutex);
1756#endif
1757 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
1758}
1759
1760static void _crVBoxHGSMIPollHost(CRConnection *conn, PCRVBOXHGSMI_CLIENT pClient)
1761{
1762 CRVBOXHGSMIREAD *parms = (CRVBOXHGSMIREAD *)_crVBoxHGSMICmdBufferLock(pClient, sizeof (*parms));
1763 int rc;
1764 VBOXUHGSMI_BUFFER_SUBMIT aSubmit[2];
1765 PVBOXUHGSMI_BUFFER pRecvBuffer;
1766 uint32_t cbBuffer;
1767
1768 CRASSERT(parms);
1769
1770 parms->hdr.result = VERR_WRONG_ORDER;
1771 parms->hdr.u32ClientID = conn->u32ClientID;
1772 parms->hdr.u32Function = SHCRGL_GUEST_FN_READ;
1773// parms->hdr.u32Reserved = 0;
1774
1775 CRASSERT(!conn->pBuffer); //make sure there's no data to process
1776 parms->iBuffer = 1;
1777 parms->cbBuffer = 0;
1778
1779 _crVBoxHGSMICmdBufferUnlock(pClient);
1780
1781 pRecvBuffer = _crVBoxHGSMIRecvBufGet(pClient);
1782 CRASSERT(pRecvBuffer);
1783 if (!pRecvBuffer)
1784 return;
1785
1786 _crVBoxHGSMIFillCmd(&aSubmit[0], pClient, sizeof (*parms));
1787
1788 aSubmit[1].pBuf = pRecvBuffer;
1789 aSubmit[1].offData = 0;
1790 aSubmit[1].cbData = pRecvBuffer->cbBuffer;
1791 aSubmit[1].fFlags.Value = 0;
1792 aSubmit[1].fFlags.bHostWriteOnly = 1;
1793
1794 rc = pClient->pHgsmi->pfnBufferSubmit(pClient->pHgsmi, aSubmit, 2);
1795 if (RT_FAILURE(rc))
1796 {
1797 crError("pfnBufferSubmit failed with %d \n", rc);
1798 return;
1799 }
1800
1801 parms = (CRVBOXHGSMIREAD *)_crVBoxHGSMICmdBufferLockRo(pClient, sizeof (*parms));
1802 CRASSERT(parms);
1803 if (!parms)
1804 {
1805 crWarning("_crVBoxHGSMICmdBufferLockRo failed\n");
1806 return;
1807 }
1808
1809 if (RT_SUCCESS(parms->hdr.result))
1810 cbBuffer = parms->cbBuffer;
1811 else
1812 cbBuffer = 0;
1813
1814 _crVBoxHGSMICmdBufferUnlock(pClient);
1815
1816 if (cbBuffer)
1817 {
1818 void *pvData = _crVBoxHGSMIRecvBufData(pClient, cbBuffer);
1819 CRASSERT(pvData);
1820 if (pvData)
1821 {
1822 conn->pBuffer = pvData;
1823 conn->cbBuffer = cbBuffer;
1824 }
1825 }
1826}
1827
1828static void _crVBoxHGSMIReadExact(CRConnection *conn, PCRVBOXHGSMI_CLIENT pClient)
1829{
1830 _crVBoxHGSMIPollHost(conn, pClient);
1831
1832 if (conn->cbBuffer)
1833 _crVBoxHGCMReceiveMessage(conn);
1834}
1835
1836/* Same as crVBoxHGCMWriteExact, but combined with read of writeback data.
1837 * This halves the number of HGCM calls we do,
1838 * most likely crVBoxHGCMPollHost shouldn't be called at all now.
1839 */
1840static void
1841_crVBoxHGSMIWriteReadExact(CRConnection *conn, PCRVBOXHGSMI_CLIENT pClient, void *buf, uint32_t offBuffer, unsigned int len, bool bIsBuffer)
1842{
1843 CRVBOXHGSMIWRITEREAD *parms = (CRVBOXHGSMIWRITEREAD*)_crVBoxHGSMICmdBufferLock(pClient, sizeof (*parms));
1844 int rc;
1845 VBOXUHGSMI_BUFFER_SUBMIT aSubmit[3];
1846 PVBOXUHGSMI_BUFFER pBuf = NULL;
1847 VBOXUHGSMI_BUFFER_LOCK_FLAGS fFlags;
1848// uint32_t cbBuffer;
1849
1850 parms->hdr.result = VERR_WRONG_ORDER;
1851 parms->hdr.u32ClientID = conn->u32ClientID;
1852 parms->hdr.u32Function = SHCRGL_GUEST_FN_WRITE_READ;
1853// parms->hdr.u32Reserved = 0;
1854
1855 parms->iBuffer = 1;
1856
1857 CRASSERT(!conn->pBuffer); //make sure there's no data to process
1858 parms->iWriteback = 2;
1859 parms->cbWriteback = 0;
1860
1861 _crVBoxHGSMICmdBufferUnlock(pClient);
1862
1863 if (!bIsBuffer)
1864 {
1865 void *pvBuf;
1866 pBuf = _crVBoxHGSMIBufAlloc(pClient, len);
1867
1868 if (!pBuf)
1869 {
1870 /* fallback */
1871 crVBoxHGCMWriteReadExact(conn, buf, len, CR_VBOXHGCM_USERALLOCATED);
1872 return;
1873 }
1874
1875 CRASSERT(!offBuffer);
1876
1877 offBuffer = 0;
1878 fFlags.Value = 0;
1879 fFlags.bDiscard = 1;
1880 fFlags.bWriteOnly = 1;
1881 rc = pBuf->pfnLock(pBuf, 0, len, fFlags, &pvBuf);
1882 if (RT_SUCCESS(rc))
1883 {
1884 memcpy(pvBuf, buf, len);
1885 rc = pBuf->pfnUnlock(pBuf);
1886 CRASSERT(RT_SUCCESS(rc));
1887 }
1888 else
1889 {
1890 crWarning("_crVBoxHGSMIWriteReadExact: pfnUnlock failed rc %d", rc);
1891 _crVBoxHGSMIBufFree(pClient, pBuf);
1892 /* fallback */
1893 crVBoxHGCMWriteReadExact(conn, buf, len, CR_VBOXHGCM_USERALLOCATED);
1894 return;
1895 }
1896 }
1897 else
1898 {
1899 pBuf = (PVBOXUHGSMI_BUFFER)buf;
1900 }
1901
1902 do
1903 {
1904 PVBOXUHGSMI_BUFFER pRecvBuffer = _crVBoxHGSMIRecvBufGet(pClient);
1905 CRASSERT(pRecvBuffer);
1906 if (!pRecvBuffer)
1907 {
1908 break;
1909 }
1910
1911 _crVBoxHGSMIFillCmd(&aSubmit[0], pClient, sizeof (*parms));
1912
1913 aSubmit[1].pBuf = pBuf;
1914 aSubmit[1].offData = offBuffer;
1915 aSubmit[1].cbData = len;
1916 aSubmit[1].fFlags.Value = 0;
1917 aSubmit[1].fFlags.bHostReadOnly = 1;
1918
1919 aSubmit[2].pBuf = pRecvBuffer;
1920 aSubmit[2].offData = 0;
1921 aSubmit[2].cbData = pRecvBuffer->cbBuffer;
1922 aSubmit[2].fFlags.Value = 0;
1923
1924 rc = pClient->pHgsmi->pfnBufferSubmit(pClient->pHgsmi, aSubmit, 3);
1925 if (RT_FAILURE(rc))
1926 {
1927 crError("pfnBufferSubmit failed with %d \n", rc);
1928 break;
1929 }
1930
1931 parms = (CRVBOXHGSMIWRITEREAD *)_crVBoxHGSMICmdBufferLockRo(pClient, sizeof (*parms));
1932 CRASSERT(parms);
1933 if (parms)
1934 {
1935 uint32_t cbWriteback = parms->cbWriteback;
1936 rc = parms->hdr.result;
1937#ifdef DEBUG_misha
1938 /* catch it here to test the buffer */
1939 Assert(RT_SUCCESS(parms->hdr.result) || parms->hdr.result == VERR_BUFFER_OVERFLOW);
1940#endif
1941 _crVBoxHGSMICmdBufferUnlock(pClient);
1942#ifdef DEBUG
1943 parms = NULL;
1944#endif
1945 if (RT_SUCCESS(rc))
1946 {
1947 if (cbWriteback)
1948 {
1949 void *pvData = _crVBoxHGSMIRecvBufData(pClient, cbWriteback);
1950 CRASSERT(pvData);
1951 if (pvData)
1952 {
1953 conn->pBuffer = pvData;
1954 conn->cbBuffer = cbWriteback;
1955 _crVBoxHGCMReceiveMessage(conn);
1956 }
1957 }
1958 }
1959 else if (VERR_BUFFER_OVERFLOW == rc)
1960 {
1961 VBOXUHGSMI_BUFFER_TYPE_FLAGS Flags = {0};
1962 PVBOXUHGSMI_BUFFER pNewBuf;
1963 CRASSERT(!pClient->pvHGBuffer);
1964 CRASSERT(cbWriteback>pClient->pHGBuffer->cbBuffer);
1965 crDebug("Reallocating host buffer from %d to %d bytes", conn->cbHostBufferAllocated, cbWriteback);
1966
1967 rc = pClient->pHgsmi->pfnBufferCreate(pClient->pHgsmi, CRVBOXHGSMI_PAGE_ALIGN(cbWriteback), Flags, &pNewBuf);
1968 if (RT_SUCCESS(rc))
1969 {
1970 rc = pClient->pHGBuffer->pfnDestroy(pClient->pHGBuffer);
1971 CRASSERT(RT_SUCCESS(rc));
1972
1973 pClient->pHGBuffer = pNewBuf;
1974
1975 _crVBoxHGSMIReadExact(conn, pClient/*, cbWriteback*/);
1976 }
1977 else
1978 {
1979 crWarning("_crVBoxHGSMIWriteReadExact: pfnBufferCreate(%d) failed!", CRVBOXHGSMI_PAGE_ALIGN(cbWriteback));
1980 if (conn->cbHostBufferAllocated < cbWriteback)
1981 {
1982 crFree(conn->pHostBuffer);
1983 conn->cbHostBufferAllocated = cbWriteback;
1984 conn->pHostBuffer = crAlloc(conn->cbHostBufferAllocated);
1985 }
1986 crVBoxHGCMReadExact(conn, NULL, cbWriteback);
1987 }
1988 }
1989 else
1990 {
1991 crWarning("SHCRGL_GUEST_FN_WRITE_READ (%i) failed with %x \n", len, rc);
1992 }
1993 }
1994 else
1995 {
1996 crWarning("_crVBoxHGSMICmdBufferLockRo failed\n");
1997 break;
1998 }
1999 } while (0);
2000
2001 if (!bIsBuffer)
2002 _crVBoxHGSMIBufFree(pClient, pBuf);
2003
2004 return;
2005}
2006
2007static void _crVBoxHGSMIWriteExact(CRConnection *conn, PCRVBOXHGSMI_CLIENT pClient, PVBOXUHGSMI_BUFFER pBuf, uint32_t offStart, unsigned int len)
2008{
2009 int rc;
2010 int32_t callRes;
2011 VBOXUHGSMI_BUFFER_SUBMIT aSubmit[2];
2012
2013#ifdef IN_GUEST
2014 if (conn->u32InjectClientID)
2015 {
2016 CRVBOXHGSMIINJECT *parms = (CRVBOXHGSMIINJECT *)_crVBoxHGSMICmdBufferLock(pClient, sizeof (*parms));
2017 CRASSERT(parms);
2018 if (!parms)
2019 {
2020 return;
2021 }
2022
2023 parms->hdr.result = VERR_WRONG_ORDER;
2024 parms->hdr.u32ClientID = conn->u32ClientID;
2025 parms->hdr.u32Function = SHCRGL_GUEST_FN_INJECT;
2026// parms->hdr.u32Reserved = 0;
2027
2028 parms->u32ClientID = conn->u32InjectClientID;
2029
2030 parms->iBuffer = 1;
2031 _crVBoxHGSMICmdBufferUnlock(pClient);
2032
2033 _crVBoxHGSMIFillCmd(&aSubmit[0], pClient, sizeof (*parms));
2034
2035 aSubmit[1].pBuf = pBuf;
2036 aSubmit[1].offData = offStart;
2037 aSubmit[1].cbData = len;
2038 aSubmit[1].fFlags.Value = 0;
2039 aSubmit[1].fFlags.bHostReadOnly = 1;
2040
2041 rc = pClient->pHgsmi->pfnBufferSubmit(pClient->pHgsmi, aSubmit, 2);
2042 if (RT_SUCCESS(rc))
2043 {
2044 callRes = _crVBoxHGSMICmdBufferGetRc(pClient);
2045 }
2046 else
2047 {
2048 /* we can not recover at this point, report error & exit */
2049 crError("pfnBufferSubmit failed with %d \n", rc);
2050 }
2051 }
2052 else
2053#endif
2054 {
2055 CRVBOXHGSMIWRITE * parms = (CRVBOXHGSMIWRITE *)_crVBoxHGSMICmdBufferLock(pClient, sizeof (*parms));;
2056
2057 parms->hdr.result = VERR_WRONG_ORDER;
2058 parms->hdr.u32ClientID = conn->u32ClientID;
2059 parms->hdr.u32Function = SHCRGL_GUEST_FN_WRITE;
2060// parms->hdr.u32Reserved = 0;
2061
2062 parms->iBuffer = 1;
2063 _crVBoxHGSMICmdBufferUnlock(pClient);
2064
2065 _crVBoxHGSMIFillCmd(&aSubmit[0], pClient, sizeof (*parms));
2066
2067 aSubmit[1].pBuf = pBuf;
2068 aSubmit[1].offData = offStart;
2069 aSubmit[1].cbData = len;
2070 aSubmit[1].fFlags.Value = 0;
2071 aSubmit[1].fFlags.bHostReadOnly = 1;
2072
2073 rc = pClient->pHgsmi->pfnBufferSubmit(pClient->pHgsmi, aSubmit, 2);
2074 if (RT_SUCCESS(rc))
2075 {
2076 callRes = _crVBoxHGSMICmdBufferGetRc(pClient);
2077 }
2078 else
2079 {
2080 /* we can not recover at this point, report error & exit */
2081 crError("Failed to submit CrHhgsmi buffer");
2082 }
2083 }
2084
2085 if (RT_FAILURE(rc) || RT_FAILURE(callRes))
2086 {
2087 crWarning("SHCRGL_GUEST_FN_WRITE failed with %x %x\n", rc, callRes);
2088 }
2089}
2090
2091static void crVBoxHGSMISend(CRConnection *conn, void **bufp,
2092 const void *start, unsigned int len)
2093{
2094 PCRVBOXHGSMI_CLIENT pClient;
2095 PVBOXUHGSMI_BUFFER pBuf;
2096 CRVBOXHGCMBUFFER *hgcm_buffer;
2097
2098 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
2099
2100#ifdef CHROMIUM_THREADSAFE
2101 crLockMutex(&g_crvboxhgcm.mutex);
2102#endif
2103
2104 if (!bufp) /* We're sending a user-allocated buffer. */
2105 {
2106 pClient = _crVBoxHGSMIClientGet(conn);
2107 if (pClient)
2108 {
2109#ifndef IN_GUEST
2110 //@todo remove temp buffer allocation in unpacker
2111 /* we're at the host side, so just store data until guest polls us */
2112 _crVBoxHGCMWriteBytes(conn, start, len);
2113#else
2114 CRASSERT(!conn->u32InjectClientID);
2115 crDebug("SHCRGL: sending userbuf with %d bytes\n", len);
2116 _crVBoxHGSMIWriteReadExact(conn, pClient, (void*)start, 0, len, false);
2117#endif
2118#ifdef CHROMIUM_THREADSAFE
2119 crUnlockMutex(&g_crvboxhgcm.mutex);
2120#endif
2121 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2122 return;
2123 }
2124
2125 /* fallback */
2126 crVBoxHGCMSend(conn, bufp, start, len);
2127#ifdef CHROMIUM_THREADSAFE
2128 crUnlockMutex(&g_crvboxhgcm.mutex);
2129#endif
2130 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2131 return;
2132 }
2133
2134 hgcm_buffer = (CRVBOXHGCMBUFFER *) *bufp - 1;
2135 CRASSERT(hgcm_buffer->magic == CR_VBOXHGCM_BUFFER_MAGIC);
2136 if (hgcm_buffer->magic != CR_VBOXHGCM_BUFFER_MAGIC)
2137 {
2138 crError("HGCM buffer magic mismatch");
2139 }
2140
2141
2142 if (hgcm_buffer->kind != CR_VBOXHGCM_UHGSMI_BUFFER)
2143 {
2144 /* fallback */
2145 crVBoxHGCMSend(conn, bufp, start, len);
2146#ifdef CHROMIUM_THREADSAFE
2147 crUnlockMutex(&g_crvboxhgcm.mutex);
2148#endif
2149 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2150 return;
2151 }
2152
2153 /* The region [start .. start + len + 1] lies within a buffer that
2154 * was allocated with crVBoxHGCMAlloc() and can be put into the free
2155 * buffer pool when we're done sending it.
2156 */
2157
2158 pBuf = _crVBoxHGSMIBufFromHdr(hgcm_buffer);
2159 CRASSERT(pBuf);
2160 if (!pBuf)
2161 {
2162 crVBoxHGCMSend(conn, bufp, start, len);
2163#ifdef CHROMIUM_THREADSAFE
2164 crUnlockMutex(&g_crvboxhgcm.mutex);
2165#endif
2166 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2167 return;
2168 }
2169
2170 pClient = (PCRVBOXHGSMI_CLIENT)pBuf->pvUserData;
2171 if (pClient != &conn->HgsmiClient)
2172 {
2173 crError("HGSMI client mismatch");
2174 }
2175
2176 /* Length would be passed as part of HGCM pointer description
2177 * No need to prepend it to the buffer
2178 */
2179#ifdef IN_GUEST
2180 if (conn->u32InjectClientID)
2181 {
2182 _crVBoxHGSMIWriteExact(conn, pClient, pBuf, CRVBOXHGSMI_BUF_OFFSET(start, *bufp) + CRVBOXHGSMI_BUF_HDR_SIZE(), len);
2183 }
2184 else
2185#endif
2186 {
2187 _crVBoxHGSMIWriteReadExact(conn, pClient, pBuf, CRVBOXHGSMI_BUF_OFFSET(start, *bufp) + CRVBOXHGSMI_BUF_HDR_SIZE(), len, true);
2188 }
2189
2190 /* Reclaim this pointer for reuse */
2191 _crVBoxHGSMIBufFree(pClient, pBuf);
2192 /* Since the buffer's now in the 'free' buffer pool, the caller can't
2193 * use it any more. Setting bufp to NULL will make sure the caller
2194 * doesn't try to re-use the buffer.
2195 */
2196 *bufp = NULL;
2197
2198#ifdef CHROMIUM_THREADSAFE
2199 crUnlockMutex(&g_crvboxhgcm.mutex);
2200#endif
2201
2202 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2203}
2204
2205static void crVBoxHGSMIWriteExact(CRConnection *conn, const void *buf, unsigned int len)
2206{
2207 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
2208
2209 CRASSERT(0);
2210
2211 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2212}
2213
2214static void crVBoxHGSMISingleRecv(CRConnection *conn, void *buf, unsigned int len)
2215{
2216 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
2217
2218 CRASSERT(0);
2219
2220 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2221}
2222
2223static void crVBoxHGSMIReceiveMessage(CRConnection *conn)
2224{
2225 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
2226
2227#ifdef CHROMIUM_THREADSAFE
2228 crLockMutex(&g_crvboxhgcm.mutex);
2229#endif
2230
2231 CRASSERT(0);
2232
2233 _crVBoxHGCMReceiveMessage(conn);
2234
2235#ifdef CHROMIUM_THREADSAFE
2236 crUnlockMutex(&g_crvboxhgcm.mutex);
2237#endif
2238
2239 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2240}
2241
2242/*
2243 * Called on host side only, to "accept" client connection
2244 */
2245static void crVBoxHGSMIAccept( CRConnection *conn, const char *hostname, unsigned short port )
2246{
2247 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
2248 CRASSERT(0);
2249
2250 CRASSERT(conn && conn->pHostBuffer);
2251#ifdef IN_GUEST
2252 CRASSERT(FALSE);
2253#endif
2254 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2255}
2256
2257static int crVBoxHGSMIDoConnect( CRConnection *conn )
2258{
2259 PCRVBOXHGSMI_CLIENT pClient;
2260 int rc = VINF_SUCCESS;
2261
2262#ifdef CHROMIUM_THREADSAFE
2263 crLockMutex(&g_crvboxhgcm.mutex);
2264#endif
2265
2266 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
2267
2268 pClient = _crVBoxHGSMIClientGet(conn);
2269 if (pClient)
2270 {
2271 rc = VBoxCrHgsmiCtlConGetClientID(pClient->pHgsmi, &conn->u32ClientID);
2272 if (RT_FAILURE(rc))
2273 {
2274 WARN(("VBoxCrHgsmiCtlConGetClientID failed %d", rc));
2275 }
2276 if (!g_crvboxhgcm.fHostCapsInitialized)
2277 {
2278 rc = VBoxCrHgsmiCtlConGetHostCaps(pClient->pHgsmi, &g_crvboxhgcm.u32HostCaps);
2279 if (RT_SUCCESS(rc))
2280 {
2281 g_crvboxhgcm.fHostCapsInitialized = true;
2282 }
2283 else
2284 {
2285 WARN(("VBoxCrHgsmiCtlConGetHostCaps failed %d", rc));
2286 g_crvboxhgcm.u32HostCaps = 0;
2287 }
2288 }
2289 }
2290 else
2291 {
2292 WARN(("_crVBoxHGSMIClientGet failed %d", rc));
2293 rc = VERR_GENERAL_FAILURE;
2294 }
2295
2296 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2297
2298#ifdef CHROMIUM_THREADSAFE
2299 crUnlockMutex(&g_crvboxhgcm.mutex);
2300#endif
2301 return RT_SUCCESS(rc);
2302}
2303
2304static void crVBoxHGSMIDoDisconnect( CRConnection *conn )
2305{
2306 bool fHasActiveCons = false;
2307
2308 if (!g_crvboxhgcm.initialized) return;
2309
2310 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
2311
2312#ifdef CHROMIUM_THREADSAFE
2313 crLockMutex(&g_crvboxhgcm.mutex);
2314#endif
2315
2316 fHasActiveCons = _crVBoxCommonDoDisconnectLocked(conn);
2317
2318#ifndef VBOX_CRHGSMI_WITH_D3DDEV
2319 if (conn->HgsmiClient.pHgsmi)
2320 {
2321 PVBOXUHGSMI pHgsmi;
2322 _crVBoxHGSMIClientTerm(&conn->HgsmiClient, &pHgsmi);
2323 CRASSERT(pHgsmi);
2324 if (!conn->pExternalHgsmi)
2325 VBoxCrHgsmiDestroy(pHgsmi);
2326 }
2327#else
2328# error "port me!"
2329#endif
2330
2331 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2332
2333#ifdef CHROMIUM_THREADSAFE
2334 crUnlockMutex(&g_crvboxhgcm.mutex);
2335#endif
2336}
2337
2338static void crVBoxHGSMIInstantReclaim(CRConnection *conn, CRMessage *mess)
2339{
2340 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
2341#ifdef CHROMIUM_THREADSAFE
2342 crLockMutex(&g_crvboxhgcm.mutex);
2343#endif
2344 CRASSERT(0);
2345
2346 _crVBoxHGSMIFree(conn, mess);
2347
2348#ifdef CHROMIUM_THREADSAFE
2349 crUnlockMutex(&g_crvboxhgcm.mutex);
2350#endif
2351 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2352}
2353
2354static void crVBoxHGSMIHandleNewMessage( CRConnection *conn, CRMessage *msg, unsigned int len )
2355{
2356 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
2357 CRASSERT(0);
2358
2359 CRASSERT(FALSE);
2360 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2361}
2362#endif
2363
2364void crVBoxHGCMInit(CRNetReceiveFuncList *rfl, CRNetCloseFuncList *cfl, unsigned int mtu)
2365{
2366 (void) mtu;
2367
2368 g_crvboxhgcm.recv_list = rfl;
2369 g_crvboxhgcm.close_list = cfl;
2370 if (g_crvboxhgcm.initialized)
2371 {
2372 return;
2373 }
2374
2375 VBOXCRHGSMIPROFILE_INIT();
2376
2377 g_crvboxhgcm.initialized = 1;
2378
2379#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
2380 g_crvboxhgcm.bHgsmiOn = _crVBoxHGSMIInit();
2381#endif
2382
2383 g_crvboxhgcm.num_conns = 0;
2384 g_crvboxhgcm.conns = NULL;
2385
2386 /* Can't open VBox guest driver here, because it gets called for host side as well */
2387 /*@todo as we have 2 dll versions, can do it now.*/
2388
2389#ifdef RT_OS_WINDOWS
2390 g_crvboxhgcm.hGuestDrv = INVALID_HANDLE_VALUE;
2391 g_crvboxhgcm.pDirectDraw = NULL;
2392#else
2393 g_crvboxhgcm.iGuestDrv = INVALID_HANDLE_VALUE;
2394#endif
2395
2396#ifdef CHROMIUM_THREADSAFE
2397 crInitMutex(&g_crvboxhgcm.mutex);
2398 crInitMutex(&g_crvboxhgcm.recvmutex);
2399#endif
2400 g_crvboxhgcm.bufpool = crBufferPoolInit(16);
2401
2402#ifdef IN_GUEST
2403 g_crvboxhgcm.fHostCapsInitialized = false;
2404 g_crvboxhgcm.u32HostCaps = 0;
2405#endif
2406}
2407
2408/* Callback function used to free buffer pool entries */
2409void crVBoxHGCMBufferFree(void *data)
2410{
2411 CRVBOXHGCMBUFFER *hgcm_buffer = (CRVBOXHGCMBUFFER *) data;
2412
2413 CRASSERT(hgcm_buffer->magic == CR_VBOXHGCM_BUFFER_MAGIC);
2414
2415 switch (hgcm_buffer->kind)
2416 {
2417 case CR_VBOXHGCM_MEMORY:
2418 crFree( hgcm_buffer );
2419 break;
2420 case CR_VBOXHGCM_MEMORY_BIG:
2421 crFree( hgcm_buffer );
2422 break;
2423
2424 default:
2425 crError( "Weird buffer kind trying to free in crVBoxHGCMBufferFree: %d", hgcm_buffer->kind );
2426 }
2427}
2428
2429void crVBoxHGCMTearDown(void)
2430{
2431 int32_t i, cCons;
2432
2433 if (!g_crvboxhgcm.initialized) return;
2434
2435#ifdef CHROMIUM_THREADSAFE
2436 crLockMutex(&g_crvboxhgcm.mutex);
2437#endif
2438
2439 /* Connection count would be changed in calls to crNetDisconnect, so we have to store original value.
2440 * Walking array backwards is not a good idea as it could cause some issues if we'd disconnect clients not in the
2441 * order of their connection.
2442 */
2443 cCons = g_crvboxhgcm.num_conns;
2444 for (i=0; i<cCons; i++)
2445 {
2446 /* Note that [0] is intended, as the connections array would be shifted in each call to crNetDisconnect */
2447 crNetDisconnect(g_crvboxhgcm.conns[0]);
2448 }
2449 CRASSERT(0==g_crvboxhgcm.num_conns);
2450
2451 g_crvboxhgcm.initialized = 0;
2452
2453#ifdef CHROMIUM_THREADSAFE
2454 crUnlockMutex(&g_crvboxhgcm.mutex);
2455 crFreeMutex(&g_crvboxhgcm.mutex);
2456 crFreeMutex(&g_crvboxhgcm.recvmutex);
2457#endif
2458
2459 if (g_crvboxhgcm.bufpool)
2460 crBufferPoolCallbackFree(g_crvboxhgcm.bufpool, crVBoxHGCMBufferFree);
2461 g_crvboxhgcm.bufpool = NULL;
2462
2463 crFree(g_crvboxhgcm.conns);
2464 g_crvboxhgcm.conns = NULL;
2465
2466#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
2467 if (g_crvboxhgcm.bHgsmiOn)
2468 {
2469 _crVBoxHGSMITearDown();
2470 }
2471#endif
2472
2473#ifdef RT_OS_WINDOWS
2474 if (g_crvboxhgcm.pDirectDraw)
2475 {
2476 IDirectDraw_Release(g_crvboxhgcm.pDirectDraw);
2477 g_crvboxhgcm.pDirectDraw = NULL;
2478 crDebug("DirectDraw released\n");
2479 }
2480#endif
2481}
2482
2483void crVBoxHGCMConnection(CRConnection *conn
2484#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
2485 , struct VBOXUHGSMI *pHgsmi
2486#endif
2487 )
2488{
2489 int i, found = 0;
2490 int n_bytes;
2491
2492 CRASSERT(g_crvboxhgcm.initialized);
2493
2494#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
2495 if (g_crvboxhgcm.bHgsmiOn)
2496 {
2497 conn->type = CR_VBOXHGCM;
2498 conn->Alloc = crVBoxHGSMIAlloc;
2499 conn->Send = crVBoxHGSMISend;
2500 conn->SendExact = crVBoxHGSMIWriteExact;
2501 conn->Recv = crVBoxHGSMISingleRecv;
2502 conn->RecvMsg = crVBoxHGSMIReceiveMessage;
2503 conn->Free = crVBoxHGSMIFree;
2504 conn->Accept = crVBoxHGSMIAccept;
2505 conn->Connect = crVBoxHGSMIDoConnect;
2506 conn->Disconnect = crVBoxHGSMIDoDisconnect;
2507 conn->InstantReclaim = crVBoxHGSMIInstantReclaim;
2508 conn->HandleNewMessage = crVBoxHGSMIHandleNewMessage;
2509 conn->pExternalHgsmi = pHgsmi;
2510 }
2511 else
2512#endif
2513 {
2514 conn->type = CR_VBOXHGCM;
2515 conn->Alloc = crVBoxHGCMAlloc;
2516 conn->Send = crVBoxHGCMSend;
2517 conn->SendExact = crVBoxHGCMWriteExact;
2518 conn->Recv = crVBoxHGCMSingleRecv;
2519 conn->RecvMsg = crVBoxHGCMReceiveMessage;
2520 conn->Free = crVBoxHGCMFree;
2521 conn->Accept = crVBoxHGCMAccept;
2522 conn->Connect = crVBoxHGCMDoConnect;
2523 conn->Disconnect = crVBoxHGCMDoDisconnect;
2524 conn->InstantReclaim = crVBoxHGCMInstantReclaim;
2525 conn->HandleNewMessage = crVBoxHGCMHandleNewMessage;
2526 }
2527 conn->sizeof_buffer_header = sizeof(CRVBOXHGCMBUFFER);
2528 conn->actual_network = 1;
2529
2530 conn->krecv_buf_size = 0;
2531
2532 conn->pBuffer = NULL;
2533 conn->cbBuffer = 0;
2534 conn->allow_redir_ptr = 1;
2535
2536 //@todo remove this crap at all later
2537 conn->cbHostBufferAllocated = 2*1024;
2538 conn->pHostBuffer = (uint8_t*) crAlloc(conn->cbHostBufferAllocated);
2539 CRASSERT(conn->pHostBuffer);
2540 conn->cbHostBuffer = 0;
2541
2542#if !defined(IN_GUEST)
2543 RTListInit(&conn->PendingMsgList);
2544#endif
2545
2546#ifdef CHROMIUM_THREADSAFE
2547 crLockMutex(&g_crvboxhgcm.mutex);
2548#endif
2549 /* Find a free slot */
2550 for (i = 0; i < g_crvboxhgcm.num_conns; i++) {
2551 if (g_crvboxhgcm.conns[i] == NULL) {
2552 conn->index = i;
2553 g_crvboxhgcm.conns[i] = conn;
2554 found = 1;
2555 break;
2556 }
2557 }
2558
2559 /* Realloc connection stack if we couldn't find a free slot */
2560 if (found == 0) {
2561 n_bytes = ( g_crvboxhgcm.num_conns + 1 ) * sizeof(*g_crvboxhgcm.conns);
2562 crRealloc( (void **) &g_crvboxhgcm.conns, n_bytes );
2563 conn->index = g_crvboxhgcm.num_conns;
2564 g_crvboxhgcm.conns[g_crvboxhgcm.num_conns++] = conn;
2565 }
2566#ifdef CHROMIUM_THREADSAFE
2567 crUnlockMutex(&g_crvboxhgcm.mutex);
2568#endif
2569}
2570
2571#if defined(IN_GUEST)
2572void _crVBoxHGCMPerformPollHost(CRConnection *conn)
2573{
2574 if (conn->type == CR_NO_CONNECTION )
2575 return;
2576
2577 if (!conn->pBuffer)
2578 {
2579#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
2580 PCRVBOXHGSMI_CLIENT pClient;
2581 if (g_crvboxhgcm.bHgsmiOn && !!(pClient = _crVBoxHGSMIClientGet(conn)))
2582 {
2583 _crVBoxHGSMIPollHost(conn, pClient);
2584 }
2585 else
2586#endif
2587 {
2588 crVBoxHGCMPollHost(conn);
2589 }
2590 }
2591}
2592#endif
2593
2594void _crVBoxHGCMPerformReceiveMessage(CRConnection *conn)
2595{
2596 if ( conn->type == CR_NO_CONNECTION )
2597 return;
2598
2599 if (conn->cbBuffer>0)
2600 {
2601 _crVBoxHGCMReceiveMessage(conn);
2602 }
2603}
2604
2605#ifdef IN_GUEST
2606uint32_t crVBoxHGCMHostCapsGet()
2607{
2608 Assert(g_crvboxhgcm.fHostCapsInitialized);
2609 return g_crvboxhgcm.u32HostCaps;
2610}
2611#endif
2612
2613int crVBoxHGCMRecv(
2614#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
2615 CRConnection *conn
2616#endif
2617 )
2618{
2619 int32_t i;
2620
2621 VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
2622
2623#ifdef CHROMIUM_THREADSAFE
2624 crLockMutex(&g_crvboxhgcm.mutex);
2625#endif
2626
2627#ifdef IN_GUEST
2628# if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
2629 CRASSERT(!g_crvboxhgcm.bHgsmiOn == !conn);
2630 if (conn && g_crvboxhgcm.bHgsmiOn)
2631 {
2632 _crVBoxHGCMPerformPollHost(conn);
2633 _crVBoxHGCMPerformReceiveMessage(conn);
2634 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2635 return 0;
2636 }
2637# endif
2638 /* we're on guest side, poll host if it got something for us */
2639 for (i=0; i<g_crvboxhgcm.num_conns; i++)
2640 {
2641 CRConnection *conn = g_crvboxhgcm.conns[i];
2642
2643 if ( !conn )
2644 continue;
2645
2646 _crVBoxHGCMPerformPollHost(conn);
2647 }
2648#endif
2649
2650 for (i=0; i<g_crvboxhgcm.num_conns; i++)
2651 {
2652 CRConnection *conn = g_crvboxhgcm.conns[i];
2653
2654 if ( !conn )
2655 continue;
2656
2657 _crVBoxHGCMPerformReceiveMessage(conn);
2658 }
2659
2660#ifdef CHROMIUM_THREADSAFE
2661 crUnlockMutex(&g_crvboxhgcm.mutex);
2662#endif
2663
2664 VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
2665
2666 return 0;
2667}
2668
2669CRConnection** crVBoxHGCMDump( int *num )
2670{
2671 *num = g_crvboxhgcm.num_conns;
2672
2673 return g_crvboxhgcm.conns;
2674}
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