vboxms.c revision cec86a0653c8db4f926c25635b6ccc19ced63db1
/* $Id$ */
/** @file
* VirtualBox Guest Additions Driver for Solaris.
*/
/*
* Copyright (C) 2012 Oracle Corporation
*
* 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.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
#define LOG_GROUP LOG_GROUP_DRV_MOUSE
/******************************************************************************
* Header Files *
******************************************************************************/
#include <VBox/VBoxGuestLib.h>
#ifndef TESTCASE
# include <sys/vuid_event.h>
# include <sys/vuid_wheel.h>
#undef u /* /usr/include/sys/user.h:249:1 is where this is defined to (curproc->p_user). very cool. */
#else /* TESTCASE */
# define IN_RING0
#endif /* TESTCASE */
#ifdef TESTCASE /* Include this last as we . */
#endif /* TESTCASE */
/******************************************************************************
* Defined Constants And Macros *
******************************************************************************/
/** The module name. */
#define DEVICE_NAME "vboxmouse"
/** The module description as seen in 'modinfo'. */
#define DEVICE_DESC "VBoxMouseIntegr"
/******************************************************************************
* Internal functions used in global structures *
******************************************************************************/
void **ppvResult);
/******************************************************************************
* Driver global structures *
******************************************************************************/
#ifndef TESTCASE /* I see no value in including these in the test. */
/*
* mod_info: STREAMS module information.
*/
static struct module_info g_vbmsSolModInfo =
{
0, /* module id number */
"vboxms",
0, /* minimum packet size */
INFPSZ, /* maximum packet size accepted */
512, /* high water mark for data flow control */
128 /* low water mark */
};
/*
* rinit: read queue structure for handling messages coming from below. In
* our case this means the host and the virtual hardware, so we do not need
* the put and service procedures.
*/
static struct qinit g_vbmsSolRInit =
{
NULL, /* put */
NULL, /* service thread procedure */
NULL, /* reserved */
NULL /* module statistics structure */
};
/*
* winit: write queue structure for handling messages coming from above. Above
* means user space applications: either Guest Additions user space tools or
* applications reading pointer input. Messages from the last most likely pass
* through at least the "consms" console mouse streams module which multiplexes
* hardware pointer drivers to a single virtual pointer.
*/
static struct qinit g_vbmsSolWInit =
{
NULL, /* service thread procedure */
NULL, /* open */
NULL, /* close */
NULL, /* reserved */
NULL /* module statistics structure */
};
/**
*/
static struct streamtab g_vbmsSolStreamTab =
{
NULL, /* MUX rinit */
NULL /* MUX winit */
};
/**
*/
static struct cb_ops g_vbmsSolCbOps =
{
nodev, /* open */
nodev, /* close */
nodev, /* b strategy */
nodev, /* b dump */
nodev, /* b print */
nodev, /* c read */
nodev, /* c write */
nodev, /* c ioctl */
nodev, /* c devmap */
nodev, /* c mmap */
nodev, /* c segmap */
nochpoll, /* c poll */
ddi_prop_op, /* property ops */
D_MP,
CB_REV /* revision */
};
/**
* dev_ops: for driver device operations
*/
static struct dev_ops g_vbmsSolDevOps =
{
DEVO_REV, /* driver build revision */
0, /* ref count */
nulldev, /* identify */
nulldev, /* probe */
nodev, /* reset */
NULL, /* bus operations */
nodev /* power */
};
/**
* modldrv: export driver specifics to the kernel
*/
static struct modldrv g_vbmsSolModule =
{
&mod_driverops, /* extern from kernel */
};
/**
*/
static struct modlinkage g_vbmsSolModLinkage =
{
MODREV_1, /* loadable module system revision */
NULL /* terminate array of linkage structures */
};
#else /* TESTCASE */
static void *g_vbmsSolModLinkage;
#endif /* TESTCASE */
/**
* State info for each open file handle.
*/
typedef struct
{
/** Device handle. */
/** Mutex protecting the guest library against multiple initialistation or
* uninitialisation. */
/** Initialisation counter for the guest library. */
/** The STREAMS write queue which we need for sending messages up to
* user-space. */
/** Pre-allocated mouse status VMMDev request for use in the IRQ
* handler. */
/* The current greatest horizontal pixel offset on the screen, used for
* absolute mouse position reporting.
*/
int cMaxScreenX;
/* The current greatest vertical pixel offset on the screen, used for
* absolute mouse position reporting.
*/
int cMaxScreenY;
} VBMSSTATE, *PVBMSSTATE;
/******************************************************************************
* Global Variables *
******************************************************************************/
/** Global driver state. Actually this could be allocated dynamically. */
/******************************************************************************
* Kernel entry points *
******************************************************************************/
/** Driver initialisation. */
int _init(void)
{
int rc;
/*
* Prevent module autounloading.
*/
if (pModCtl)
else
return rc;
}
#ifdef TESTCASE
/** Simple test of the flow through _init. */
{
}
#endif
/** Driver cleanup. */
int _fini(void)
{
int rc;
return rc;
}
/** Driver identification. */
{
int rc;
return rc;
}
/******************************************************************************
* Initialisation entry points *
******************************************************************************/
/**
* Attach entry point, to attach a device to the system or resume it.
*
* @param pDip The module structure instance.
* @param enmCmd Attach type (ddi_attach_cmd_t)
*
* @return corresponding solaris error code.
*/
{
switch (enmCmd)
{
case DDI_ATTACH:
{
int rc;
/* Only one instance supported. */
return DDI_FAILURE;
if (rc == DDI_SUCCESS)
return DDI_SUCCESS;
return DDI_FAILURE;
}
case DDI_RESUME:
{
/** @todo implement resume for guest driver. */
return DDI_SUCCESS;
}
default:
return DDI_FAILURE;
}
}
/**
* Detach entry point, to detach a device to the system or suspend it.
*
* @param pDip The module structure instance.
* @param enmCmd Attach type (ddi_attach_cmd_t)
*
* @return corresponding solaris error code.
*/
{
switch (enmCmd)
{
case DDI_DETACH:
{
return DDI_SUCCESS;
}
case DDI_SUSPEND:
{
/** @todo implement suspend for guest driver. */
return DDI_SUCCESS;
}
default:
return DDI_FAILURE;
}
}
/**
* Info entry point, called by solaris kernel for obtaining driver info.
*
* @param pDip The module structure instance (do not use).
* @param enmCmd Information request type.
* @param pvArg Type specific argument.
* @param ppvResult Where to store the requested info.
*
* @return corresponding solaris error code.
*/
void **ppvResult)
{
int rc = DDI_SUCCESS;
switch (enmCmd)
{
case DDI_INFO_DEVT2DEVINFO:
break;
case DDI_INFO_DEVT2INSTANCE:
break;
default:
rc = DDI_FAILURE;
break;
}
return rc;
}
/******************************************************************************
* Main code *
******************************************************************************/
static void vbmsSolNotify(void *pvState);
int cValue);
/**
* Open callback for the read queue, which we use as a generic device open
* handler.
*/
{
int rc = VINF_SUCCESS;
/*
* Sanity check on the mode parameter - only open as a driver, not a
* module, and we do cloning ourselves.
*/
if (fMode)
{
LogRel(("::Open: invalid attempt to clone device."));
return EINVAL;
}
/*
* Check and remember our STREAM queue.
*/
if ( pState->pWriteQueue
{
LogRel((DEVICE_NAME "::Open: unexpectedly called with a different queue to previous calls. Exiting.\n"));
return EINVAL;
}
{
/*
* Initialize IPRT R0 driver, which internally calls OS-specific r0
* init, and create a new session.
*/
if (RT_SUCCESS(rc))
{
sizeof(*pState->pMouseStatusReq),
if (RT_FAILURE(rc))
else
{
int rc2;
/* Initialise user data for the queues to our state and
* vice-versa. */
/* Enable our IRQ handler. */
(void *)pState);
if (RT_FAILURE(rc2))
/* Log the failure. I may well have not understood what
* is going on here, and the logging may help me. */
LogRelFlow(("Failed to install the event handler call-back, rc=%Rrc\n",
rc2));
}
}
}
if (RT_SUCCESS(rc))
if (RT_FAILURE(rc))
{
return EINVAL;
}
return 0;
}
/**
* Notification callback, called when the VBoxGuest mouse pointer is moved.
* We send a VUID event up to user space. We may send a miscalculated event
* if a resolution change is half-way through, but that is pretty much to be
* expected, so we won't worry about it.
*/
void vbmsSolNotify(void *pvState)
{
int rc;
if (RT_SUCCESS(rc))
{
if (cMaxScreenX && cMaxScreenY)
{
x * cMaxScreenX / VMMDEV_MOUSE_RANGE_MAX);
y * cMaxScreenY / VMMDEV_MOUSE_RANGE_MAX);
}
}
}
int cValue)
{
if (!pMBlk)
return; /* If kernel memory is short a missed event is acceptable! */
/* Put the message on the queue immediately if it is not blocked. */
else
}
/**
* Close callback for the read queue, which we use as a generic device close
* handler.
*/
{
if (!pState)
{
return EFAULT;
}
{
/* Disable our IRQ handler. */
/*
* Close the session.
*/
}
return 0;
}
#ifdef TESTCASE
/** Simple test of vbmsSolOpen and vbmsSolClose. */
{
int rc;
doInitQueues(&aQueues[0]);
}
#endif
/* Helper for vbmsSolWPut. */
/**
* Handler for messages sent from above (user-space and upper modules) which
* land in our write queue.
*/
{
{
case M_FLUSH:
LogRelFlow(("M_FLUSH, FLUSHW=%RTbool, FLUSHR=%RTbool\n",
/* Flush the write queue if so requested. */
/* Flush the read queue if so requested. */
/* We have no one below us to pass the message on to. */
return 0;
/* M_IOCDATA is additional data attached to (at least) transparent
* IOCtls. We handle the two together here and separate them further
* down. */
case M_IOCTL:
case M_IOCDATA:
{
int err;
? "M_IOCTL\n" : "M_IOCDATA\n"));
if (!err)
else
break;
}
default:
LogRelFlow(("Unknown command, not acknowledging.\n"));
}
return 0;
}
#ifdef TESTCASE
/* Constants, definitions and test functions for testWPut. */
static const int g_ccTestWPutFirmEvent = VUID_FIRM_EVENT;
# define PVGFORMAT (&g_ccTestWPutFirmEvent)
# define CBGFORMAT (sizeof(g_ccTestWPutFirmEvent))
# define PMSIOSRES (&g_TestResolution)
# define CBMSIOSRES (sizeof(g_TestResolution))
{
(hTest, "pState->cMaxScreenX=%d\n",
pState->cMaxScreenX), false);
(hTest, "pState->cMaxScreenY=%d\n",
pState->cMaxScreenY), false);
return true;
}
/** Data table for testWPut. */
static struct
{
int iIOCCmd;
const void *pvDataIn;
const void *pvDataOut;
int rcExp;
bool fCanTransparent;
} g_asTestWPut[] =
{
/* iIOCCmd cbData pvDataIn cbDataIn
pvDataOut cbDataOut rcExp pfnExtra fCanTransparent */
{ VUIDGFORMAT, sizeof(int), NULL, 0,
NULL, 0, 0, testSetResolution, true },
{ VUIDGWHEELINFO, 0, NULL, 0,
};
/* Helpers for testWPut. */
/** Test WPut's handling of different IOCtls, which is bulk of the logic in
* this file. */
{
unsigned i;
for (i = 0; i < RT_ELEMENTS(g_asTestWPut); ++i)
{
testWPutStreams(hTest, i);
if (g_asTestWPut[i].fCanTransparent)
testWPutTransparent(hTest, i);
testWPutIOCDataIn(hTest, i);
testWPutIOCDataOut(hTest, i);
}
}
#define MSG_DATA_SIZE 1024
/** Simulate sending a streams IOCtl to WPut with the parameters from table
* line @a i. */
{
doInitQueues(&aQueues[0]);
g_asTestWPut[i].cbDataIn);
(hTest, "i=%u, IOCBlk.ioc_error=%d\n", i,
g_asTestWPut[i].cbDataOut),
(hTest, "i=%u\n", i));
/* Hack to ensure that miocpullup() gets called when needed. */
if (g_asTestWPut[i].cbData > 0)
if (!g_asTestWPut[i].rcExp)
(hTest, "i=%u\n", i));
if (g_asTestWPut[i].pfnExtra)
}
#define USER_ADDRESS 0xfeedbacc
/** Simulate sending a transparent IOCtl to WPut with the parameters from table
* line @a i. */
{
/* if (g_asTestWPut[i].cbDataIn == 0 && g_asTestWPut[i].cbDataOut != 0)
return; */ /* This case will be handled once the current ones work. */
doInitQueues(&aQueues[0]);
|| ( g_asTestWPut[i].cbDataOut
|| ( (g_asTestWPut[i].rcExp == 0)
(hTest, "i=%u, db_type=%u\n", i,
/* Our TRANSPARENT IOCtls can only return non-zero if they have no payload.
* Others should either return zero or be non-TRANSPARENT only. */
(hTest, "i=%u, IOCBlk.ioc_error=%d\n", i,
if (g_asTestWPut[i].cbData)
{
== g_asTestWPut[i].cbDataIn
? g_asTestWPut[i].cbDataIn
: g_asTestWPut[i].cbDataOut,
(hTest, "i=%u, cq_size=%llu\n", i,
}
/* Implementation detail - check that the private pointer is correctly
* set to the user address *for two direction IOCtls* or NULL otherwise. */
(hTest, "i=%u, cq_private=%p\n", i,
pCopyReq->cq_private));
(hTest, "i=%u, cq_private=%p\n", i,
pCopyReq->cq_private));
if (!g_asTestWPut[i].rcExp)
(hTest, "i=%u\n", i));
}
/** Simulate sending follow-on IOCData messages to a transparent IOCtl to WPut
* with the parameters from table line @a i. */
{
: NULL;
i);
doInitQueues(&aQueues[0]);
if (g_asTestWPut[i].cbDataOut)
|| ( (g_asTestWPut[i].rcExp == 0)
(hTest, "i=%u, db_type=%u\n", i,
if (g_asTestWPut[i].cbDataOut)
{
(hTest, "i=%u, cq_size=%llu\n", i,
g_asTestWPut[i].cbDataOut),
(hTest, "i=%u\n", i));
}
(hTest, "i=%u, cq_private=%p\n", i,
pCopyReq->cq_private));
if (!g_asTestWPut[i].rcExp)
(hTest, "i=%u\n", i));
}
/** Simulate sending follow-on IOCData messages to a transparent IOCtl to WPut
* with the parameters from table line @a i. */
{
: NULL;
i);
doInitQueues(&aQueues[0]);
(hTest, "i=%u, db_type=%u\n", i,
if (!g_asTestWPut[i].rcExp)
(hTest, "i=%u\n", i));
}
#endif
/** Data transfer direction of an IOCtl. This is used for describing
* transparent IOCtls, and @a UNSPECIFIED is not a valid value for them. */
enum IOCTLDIRECTION
{
/** This IOCtl transfers no data. */
NONE,
/** This IOCtl only transfers data from user to kernel. */
IN,
/** This IOCtl only transfers data from kernel to user. */
OUT,
/** This IOCtl transfers data from user to kernel and back. */
BOTH,
/** We aren't saying anything about how the IOCtl transfers data. */
};
/**
* IOCtl handler function.
* @returns 0 on success, error code on failure.
* @param iCmd The IOCtl command number.
* @param pvData Buffer for the user data.
* @param cbBuffer Size of the buffer in @a pvData or zero.
* @param pcbData Where to set the size of the data returned. Required for
* handlers which return data.
* @param prc Where to store the return code. Default is zero. Only
* used for IOCtls without data for convenience of
* implemention.
*/
typedef FNVBMSSOLIOCTL *PFNVBMSSOLIOCTL;
/* Helpers for vbmsSolDispatchIOCtl. */
enum IOCTLDIRECTION enmDirection);
/** Table of supported VUID IOCtls. */
struct
{
/** The IOCtl number. */
int iCmd;
/** The size of the buffer which needs to be copied between user and kernel
* space, or zero if unknown (must be known for tranparent IOCtls). */
/** The direction the buffer data needs to be copied. This must be
* specified for transparent IOCtls. */
enum IOCTLDIRECTION enmDirection;
} g_aVUIDIOCtlDescriptions[] =
{
{ VUIDGFORMAT, sizeof(int), OUT },
{ VUIDSFORMAT, sizeof(int), IN },
{ VUIDGADDR, 0, UNSPECIFIED },
{ VUIDGADDR, 0, UNSPECIFIED },
{ MSIOBUTTONS, sizeof(int), OUT },
{ VUIDGWHEELCOUNT, sizeof(int), OUT },
{ VUIDGWHEELINFO, 0, UNSPECIFIED },
{ VUIDGWHEELSTATE, 0, UNSPECIFIED },
{ VUIDSWHEELSTATE, 0, UNSPECIFIED }
};
/**
* Handle a STREAMS IOCtl message for our driver on the write stream. This
* function takes care of the IOCtl logic only and does not call qreply() or
* miocnak() at all - the caller must call these on success or failure
* respectively.
* @returns 0 on success or the IOCtl error code on failure.
* @param pState pointer to the state structure.
* @param pMBlk pointer to the STREAMS message block structure.
*/
{
enum IOCTLDIRECTION enmDirection;
switch (iCmdType)
{
case MSIOC:
case VUIOC:
{
unsigned i;
for (i = 0; i < RT_ELEMENTS(g_aVUIDIOCtlDescriptions); ++i)
{
}
return EINVAL;
}
default:
return ENOTTY;
}
}
/* Helpers for vbmsSolHandleIOCtl. */
enum IOCTLDIRECTION enmDirection);
enum IOCTLDIRECTION enmDirection);
{
}
/**
* Generic code for handling STREAMS-specific IOCtl logic and boilerplate. It
* calls the IOCtl handler passed to it without the handler having to be aware
* of STREAMS structures, or whether this is a transparent (traditional) or an
* I_STR (using a STREAMS structure to describe the data) IOCtl. With the
* caveat that we only support transparent IOCtls which pass all data in a
* single buffer of a fixed size (I_STR IOCtls are restricted to a single
* buffer anyway, but the caller can choose the buffer size).
* @returns 0 on success or the IOCtl error code on failure.
* @param pState pointer to the state structure.
* @param pMBlk pointer to the STREAMS message block structure.
* @param pfnHandler pointer to the right IOCtl handler function for this
* IOCtl number.
* @param iCmd IOCtl command number.
* @param cbCmd size of the user space buffer for this IOCtl number,
* used for processing transparent IOCtls. Pass zero
* for IOCtls with no maximum buffer size (which will
* not be able to be handled as transparent) or with
* no argument.
* @param enmDirection data transfer direction of the IOCtl.
*/
{
LogFlowFunc(("iCmd=0x%x, cbBuffer=%d, enmDirection=%d\n",
return EINVAL;
}
/**
* Helper for vbmsSolHandleIOCtl. This rather complicated-looking
* code is basically the standard boilerplate for handling any streams IOCtl
* additional data, which we currently only use for transparent IOCtls.
* @copydoc vbmsSolHandleIOCtl
*/
enum IOCTLDIRECTION enmDirection)
{
LogFlowFunc(("iCmd=0x%x, cbBuffer=%d, enmDirection=%d, cp_rval=%d, cp_private=%p\n",
(void *)pCopyResp->cp_private));
{
return EAGAIN;
}
{
int err;
return EINVAL;
return EINVAL;
vbmsSolAcknowledgeIOCtl(pMBlk, 0, 0);
return err;
}
else
{
vbmsSolAcknowledgeIOCtl(pMBlk, 0, 0);
return 0;
}
}
/**
* Helper for vbmsSolHandleIOCtl. This rather complicated-looking
* code is basically the standard boilerplate for handling transparent IOCtls,
* that is, IOCtls which are not re-packed inside STREAMS IOCtls.
* @copydoc vbmsSolHandleIOCtl
*/
enum IOCTLDIRECTION enmDirection)
{
LogFlowFunc(("iCmd=0x%x, cbBuffer=%d, enmDirection=%d\n",
|| enmDirection == UNSPECIFIED)
return EINVAL;
{
/* We only need state data if there is something to copy back. */
if (enmDirection == BOTH)
}
else if (enmDirection == OUT)
{
void *pvData;
if (!pMBlkOut)
return EAGAIN;
if (!err)
else
}
else
{
if (!err)
}
return err;
}
/**
* Helper for vbmsSolHandleIOCtl. This rather complicated-looking
* code is basically the standard boilerplate for handling any streams IOCtl.
* @copydoc vbmsSolHandleIOCtl
*/
{
LogFlowFunc(("iCmd=0x%x, cbBuffer=%u, b_cont=%p\n",
return EINVAL;
/* Repack the whole buffer into a single message block if needed. */
if (cbBuffer)
{
if (err)
return err;
}
{
}
if (!err)
{
LogRelFlowFunc(("pMBlk=%p, pMBlk->b_datap=%p, pMBlk->b_rptr=%p\n",
}
return err;
}
/**
* Handle a VUID input device IOCtl.
* @copydoc FNVBMSSOLIOCTL
*/
{
switch (iCmd)
{
case VUIDGFORMAT:
{
LogRelFlowFunc(("VUIDGFORMAT\n"));
if (cbBuffer < sizeof(int))
return EINVAL;
*(int *)pvData = VUID_FIRM_EVENT;
*pcbData = sizeof(int);
return 0;
}
case VUIDSFORMAT:
LogRelFlowFunc(("VUIDSFORMAT\n"));
/* We define our native format to be VUID_FIRM_EVENT, so there
* is nothing more to do and we exit here on success or on
* failure. */
return 0;
case VUIDGADDR:
case VUIDSADDR:
LogRelFlowFunc(("VUIDGADDR/VUIDSADDR\n"));
return ENOTTY;
case MSIOGETPARMS:
{
LogRelFlowFunc(("MSIOGETPARMS\n"));
return EINVAL;
return 0;
}
case MSIOSETPARMS:
LogRelFlowFunc(("MSIOSETPARMS\n"));
return 0;
case MSIOSRESOLUTION:
{
int rc;
LogRelFlowFunc(("MSIOSRESOLUTION, cbBuffer=%d, sizeof(Ms_screen_resolution)=%d\n",
(int) cbBuffer,
(int) sizeof(Ms_screen_resolution)));
if (cbBuffer < sizeof(Ms_screen_resolution))
return EINVAL;
pResolution->height));
/* Note: we don't disable this again until session close. */
if (RT_SUCCESS(rc))
return 0;
pState->cMaxScreenX = 0;
pState->cMaxScreenY = 0;
return ENODEV;
}
case MSIOBUTTONS:
{
LogRelFlowFunc(("MSIOBUTTONS\n"));
if (cbBuffer < sizeof(int))
return EINVAL;
*(int *)pvData = 0;
*pcbData = sizeof(int);
return 0;
}
case VUIDGWHEELCOUNT:
{
LogRelFlowFunc(("VUIDGWHEELCOUNT\n"));
if (cbBuffer < sizeof(int))
return EINVAL;
*(int *)pvData = 0;
*pcbData = sizeof(int);
return 0;
}
case VUIDGWHEELINFO:
case VUIDGWHEELSTATE:
case VUIDSWHEELSTATE:
return EINVAL;
default:
return EINVAL;
}
}
#ifdef TESTCASE
int main(void)
{
if (rc)
return rc;
/*
* Summary.
*/
return RTTestSummaryAndDestroy(hTest);
}
#endif