vboxhgcm.c revision d23c3743b10c0b51bdc58319c55a06b0800870c8
/* $Id$ */
/** @file
* VBox HGCM connection
*/
/*
* Copyright (C) 2008 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
#ifdef RT_OS_WINDOWS
#include <windows.h>
#include <ddraw.h>
#else
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#endif
#include "cr_error.h"
#include "cr_net.h"
#include "cr_bufpool.h"
#include "cr_mem.h"
#include "cr_string.h"
#include "cr_endian.h"
#include "cr_threads.h"
#include "net_internals.h"
#include <VBox/VBoxGuest.h>
typedef struct {
int initialized;
int num_conns;
#ifdef CHROMIUM_THREADSAFE
#endif
#ifdef RT_OS_WINDOWS
#else
int iGuestDrv;
#endif
static CRVBOXHGCMDATA g_crvboxhgcm = {0,};
typedef enum {
#ifdef RT_OS_WINDOWS
#endif
#define CR_VBOXHGCM_BUFFER_MAGIC 0xABCDE321
typedef struct CRVBOXHGCMBUFFER {
#ifdef RT_OS_WINDOWS
#endif
#ifndef RT_OS_WINDOWS
#define TRUE true
#define FALSE false
#define INVALID_HANDLE_VALUE (-1)
#endif
/* Some forward declarations */
{
return FALSE;
return TRUE;
}
/*@todo get rid of it*/
{
/* make sure there's host buffer and it's clear */
{
crDebug("Host buffer too small %d out of requsted %d bytes, reallocating", conn->cbHostBufferAllocated, len);
if (!conn->pHostBuffer)
{
conn->cbHostBufferAllocated = 0;
return FALSE;
}
}
return TRUE;
}
/**
* Send an HGCM request
*
* @return VBox status code
* @param pvData Data pointer
* @param cbData Data size
*/
/*@todo use vbglR3DoIOCtl here instead */
{
#ifdef IN_GUEST
#ifdef RT_OS_WINDOWS
NULL))
{
return VINF_SUCCESS;
}
return VERR_NOT_SUPPORTED;
#else
{
return VINF_SUCCESS;
}
return VERR_NOT_SUPPORTED;
#endif /*#ifdef RT_OS_WINDOWS*/
#else /*#ifdef IN_GUEST*/
crError("crVBoxHGCMCall called on host side!");
return VERR_NOT_SUPPORTED;
#endif
}
{
#ifdef CHROMIUM_THREADSAFE
#endif
if (!buf)
{
crDebug("Buffer pool %p was empty; allocating new %d byte buffer.",
(void *) g_crvboxhgcm.bufpool,
#if defined(IN_GUEST) && defined(RT_OS_WINDOWS)
/* Try to start DDRAW on guest side */
if (!g_crvboxhgcm.pDirectDraw && 0)
{
{
}
else
{
{
}
crDebug("Created DirectDraw and set CooperativeLevel successfully\n");
}
}
/* Try to allocate buffer via DDRAW */
if (g_crvboxhgcm.pDirectDraw)
{
/* @todo DDSCAPS_VIDEOMEMORY ain't working for some reason
* also, it would be better to request dwLinearSize but it fails too
* ddsd.dwLinearSize = sizeof(CRVBOXHGCMBUFFER) + conn->buffer_size;
*/
/* use 1 byte per pixel format */
/* request given buffer size, rounded to 1k */
{
}
else
{
{
}
else
{
}
}
}
#endif
/* We're either on host side, or we failed to allocate DDRAW buffer */
if (!buf)
{
crDebug("Using system malloc\n");
#ifdef RT_OS_WINDOWS
#endif
}
}
#ifdef CHROMIUM_THREADSAFE
#endif
return (void *)( buf + 1 );
}
{
int rc;
{
}
}
{
int rc;
{
return;
}
{
}
}
/* Same as crVBoxHGCMWriteExact, but combined with read of writeback data.
* This halves the number of HGCM calls we do,
* most likely crVBoxHGCMPollHost shouldn't be called at all now.
*/
static void
crVBoxHGCMWriteReadExact(CRConnection *conn, const void *buf, unsigned int len, CRVBOXHGCMBUFFERKIND bufferKind)
{
int rc;
//if (bufferKind != CR_VBOXHGCM_DDRAW_SURFACE)
{
}
/*else //@todo it fails badly, have to check why
{
parms.pBuffer.type = VMMDevHGCMParmType_PhysAddr;
parms.pBuffer.u.Pointer.size = len;
parms.pBuffer.u.Pointer.u.physAddr = (VMMDEVHYPPHYS32) buf;
}*/
{
{
/* reallocate buffer and retry */
crDebug("Reallocating host buffer from %d to %d bytes", conn->cbHostBufferAllocated, parms.cbWriteback.u.value32);
return;
}
else
{
return;
}
}
{
}
}
{
if (!bufp) /* We're sending a user-allocated buffer. */
{
#ifndef IN_GUEST
//@todo remove temp buffer allocation in unpacker
/* we're at the host side, so just store data until guest polls us */
#else
#endif
return;
}
/* The region [start .. start + len + 1] lies within a buffer that
* was allocated with crVBoxHGCMAlloc() and can be put into the free
* buffer pool when we're done sending it.
*/
/* Length would be passed as part of HGCM pointer description
* No need to prepend it to the buffer
*/
/* Reclaim this pointer for reuse */
#ifdef CHROMIUM_THREADSAFE
#endif
#ifdef CHROMIUM_THREADSAFE
#endif
/* Since the buffer's now in the 'free' buffer pool, the caller can't
* use it any more. Setting bufp to NULL will make sure the caller
* doesn't try to re-use the buffer.
*/
}
{
int rc;
{
return;
}
{
}
}
{
}
{
/*@todo wrong len for redir buffers*/
switch (hgcm_buffer->kind)
{
case CR_VBOXHGCM_MEMORY:
#ifdef RT_OS_WINDOWS
#endif
#ifdef CHROMIUM_THREADSAFE
#endif
if (g_crvboxhgcm.bufpool) {
//@todo o'rly?
/* pool may have been deallocated just a bit earlier in response
* to a SIGPIPE (Broken Pipe) signal.
*/
}
#ifdef CHROMIUM_THREADSAFE
#endif
break;
case CR_VBOXHGCM_MEMORY_BIG:
crFree( hgcm_buffer );
break;
default:
}
}
{
#ifndef IN_GUEST
if (conn->allow_redir_ptr)
{
#endif //IN_GUEST
#ifndef IN_GUEST
}
else
{
{
/* put in pre-allocated buffer */
}
else
{
/* allocate new buffer,
* not using pool here as it's most likely one time transfer of huge texture
*/
#ifdef RT_OS_WINDOWS
#endif
}
}
#endif //IN_GUEST
/* CR_MESSAGE_OPCODES is freed in crserverlib/server_stream.c with crNetFree.
* OOB messages are the programmer's problem. -- Humper 12/17/01
*/
if (cached_type != CR_MESSAGE_OPCODES
&& cached_type != CR_MESSAGE_OOB
&& cached_type != CR_MESSAGE_GATHER)
{
}
}
/*
* Called on host side only, to "accept" client connection
*/
{
#ifdef IN_GUEST
#endif
}
/**
* The function that actually connects. This should only be called by clients,
* guests in vbox case.
* Servers go through crVBoxHGCMAccept;
*/
/*@todo use vbglR3Something here */
{
#ifdef IN_GUEST
#ifdef RT_OS_WINDOWS
{
/* open VBox guest driver */
NULL,
NULL);
/* @todo check if we could rollback to softwareopengl */
{
return FALSE;
}
}
#else
{
{
return FALSE;
}
}
#endif
#ifdef RT_OS_WINDOWS
NULL))
#else
/*@todo it'd fail */
#endif
{
{
}
else
{
return FALSE;
}
}
else
{
#ifdef RT_OS_WINDOWS
#else
#endif
return FALSE;
}
return TRUE;
#else /*#ifdef IN_GUEST*/
crError("crVBoxHGCMDoConnect called on host side!");
return FALSE;
#endif
}
/*@todo same, replace DeviceIoControl with vbglR3DoIOCtl */
{
#ifdef RT_OS_WINDOWS
#endif
int i;
if (conn->pHostBuffer)
{
conn->cbHostBuffer = 0;
conn->cbHostBufferAllocated = 0;
}
//@todo hold lock here?
{
{
}
}
#ifndef IN_GUEST
#else
if (conn->u32ClientID)
{
#ifdef RT_OS_WINDOWS
NULL) )
{
}
#else
{
}
#endif
conn->u32ClientID = 0;
}
/* see if any connections remain */
for (i = 0; i < g_crvboxhgcm.num_conns; i++)
break;
/* close guest additions driver*/
if (i>=g_crvboxhgcm.num_conns)
{
#ifdef RT_OS_WINDOWS
#else
#endif
}
#endif
}
{
}
{
}
{
(void) mtu;
if (g_crvboxhgcm.initialized)
{
return;
}
g_crvboxhgcm.num_conns = 0;
/* Can't open VBox guest driver here, because it gets called for host side as well */
/*@todo as we have 2 dll versions, can do it now.*/
#ifdef RT_OS_WINDOWS
#else
#endif
#ifdef CHROMIUM_THREADSAFE
#endif
}
/* Callback function used to free buffer pool entries */
void crVBoxHGCMBufferFree(void *data)
{
#ifdef RT_OS_WINDOWS
#endif
switch (hgcm_buffer->kind)
{
case CR_VBOXHGCM_MEMORY:
crFree( hgcm_buffer );
break;
#ifdef RT_OS_WINDOWS
break;
#endif
case CR_VBOXHGCM_MEMORY_BIG:
crFree( hgcm_buffer );
break;
default:
}
}
void crVBoxHGCMTearDown(void)
{
if (!g_crvboxhgcm.initialized) return;
/* Connection count would be changed while we disconnect link, also the array of connections would be shifted*/
for (i=0; i<cCons; i++)
{
}
#ifdef CHROMIUM_THREADSAFE
#endif
if (g_crvboxhgcm.bufpool)
g_crvboxhgcm.initialized = 0;
#ifdef RT_OS_WINDOWS
if (g_crvboxhgcm.pDirectDraw)
{
crDebug("DirectDraw released\n");
}
#endif
}
{
int i, found = 0;
int n_bytes;
conn->krecv_buf_size = 0;
//@todo remove this crap at all later
conn->cbHostBuffer = 0;
/* Find a free slot */
for (i = 0; i < g_crvboxhgcm.num_conns; i++) {
found = 1;
break;
}
}
/* Realloc connection stack if we couldn't find a free slot */
if (found == 0) {
}
}
int crVBoxHGCMRecv(void)
{
int32_t i;
#ifdef IN_GUEST
/* we're on guest side, poll host if it got something for us */
for (i=0; i<g_crvboxhgcm.num_conns; i++)
{
continue;
{
}
}
#endif
for (i=0; i<g_crvboxhgcm.num_conns; i++)
{
continue;
{
}
}
return 0;
}
{
return g_crvboxhgcm.conns;
}