SUPDrv-solaris.c revision 54c2d91adf1ceb1ee7c9e10be29a321294a1a8fb
/* $Id$ */
/** @file
* VirtualBox Support Driver - Solaris Driver Code.
*/
/*
* Copyright (C) 2006-2007 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.
*
* 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.
*
* 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#undef u /* /usr/include/sys/user.h:249:1 is where this is defined to (curproc->p_user). very cool. */
#include "../SUPDrvInternal.h"
#include <iprt/semaphore.h>
#include <iprt/spinlock.h>
#include <iprt/initterm.h>
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** The module name. */
#define DEVICE_NAME "vboxdrv"
/** The module description as seen in 'modinfo'. */
#define DEVICE_DESC "VirtualBox Host Driver"
/** Maximum number of driver instances. */
#define DEVICE_MAXINSTANCES 16
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static int VBoxDrvSolarisIOCtl (dev_t Dev, int Cmd, intptr_t pArgs, int mode, cred_t *pCred, int *pVal);
static int VBoxSupDrvErr2SolarisErr(int rc);
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/**
*/
static struct cb_ops g_VBoxDrvSolarisCbOps =
{
nodev, /* b strategy */
nodev, /* b dump */
nodev, /* b print */
nodev, /* c devmap */
nodev, /* c mmap */
nodev, /* c segmap */
nochpoll, /* c poll */
ddi_prop_op, /* property ops */
NULL, /* streamtab */
CB_REV /* revision */
};
/**
* dev_ops: for driver device operations
*/
static struct dev_ops g_VBoxDrvSolarisDevOps =
{
DEVO_REV, /* driver build revision */
0, /* ref count */
nulldev, /* get info */
nulldev, /* identify */
nulldev, /* probe */
nodev, /* reset */
(struct bus_ops *)0,
nodev /* power */
};
/**
* modldrv: export driver specifics to the kernel
*/
static struct modldrv g_VBoxDrvSolarisModule =
{
&mod_driverops, /* extern from kernel */
};
/**
*/
static struct modlinkage g_VBoxDrvSolarisModLinkage =
{
MODREV_1, /* loadable module system revision */
NULL /* terminate array of linkage structures */
};
#ifndef USE_SESSION_HASH
/**
* State info for each open file handle.
*/
typedef struct
{
/**< Pointer to the session data. */
#else
/** State info. for each driver instance. */
typedef struct
{
#endif
/** Opaque pointer to list of state */
static void *g_pVBoxDrvSolarisState;
/** Device extention & session data association structure */
static SUPDRVDEVEXT g_DevExt;
/* GCC C++ hack. */
unsigned __gxx_personality_v0 = 0xcccccccc;
/** Hash table */
/** Spinlock protecting g_apSessionHashTab. */
/** Calculates bucket index into g_apSessionHashTab.*/
/**
* Kernel entry points
*/
int _init(void)
{
dprintf(("VBoxDrvSolaris _init"));
if (!rc)
{
if (!rc)
return 0; /* success */
}
return rc;
}
int _fini(void)
{
dprintf(("VBoxDrvSolaris _fini"));
int e = mod_remove(&g_VBoxDrvSolarisModLinkage);
if (e != 0)
return e;
return e;
}
{
dprintf(("VBoxDrvSolaris _info"));
dprintf(("VBoxDrvSolaris _info returns %d", e));
return e;
}
/**
* Attach entry point, to attach a device to the system or resume it.
*
* @param pDip The module structure instance.
*
* @return corresponding solaris error code.
*/
{
dprintf(("VBoxDrvSolarisAttach"));
switch (enmCmd)
{
case DDI_ATTACH:
{
int rc;
#ifdef USE_SESSION_HASH
{
return DDI_FAILURE;
}
#endif
/*
* Initialize IPRT R0 driver, which internally calls OS-specific r0 init.
*/
if (RT_SUCCESS(rc))
{
/*
* Initialize the device extension
*/
if (RT_SUCCESS(rc))
{
/*
* Initialize the session hash table.
*/
if (RT_SUCCESS(rc))
{
/*
* Register ourselves as a character device, pseudo-driver
*/
{
#ifdef USE_SESSION_HASH
#endif
return DDI_SUCCESS;
}
/* Is this really necessary? */
}
else
}
else
RTR0Term();
}
else
break;
}
case DDI_RESUME:
{
return DDI_SUCCESS;
}
default:
return DDI_FAILURE;
}
return DDI_FAILURE;
}
/**
* Detach entry point, to detach a device to the system or suspend it.
*
* @param pDip The module structure instance.
*
* @return corresponding solaris error code.
*/
{
int rc = VINF_SUCCESS;
dprintf(("VBoxDrvSolarisDetach"));
switch (enmCmd)
{
case DDI_DETACH:
{
#ifndef USE_SESSION_HASH
#else
#endif
RTR0Term();
dprintf(("VBoxDrvSolarisDetach: Clean Up Done."));
return DDI_SUCCESS;
}
case DDI_SUSPEND:
{
return DDI_SUCCESS;
}
default:
return DDI_FAILURE;
}
}
/**
* User context entry points
*/
{
int rc;
#ifndef USE_SESSION_HASH
/*
* Locate a new device open instance.
*
* For each open call we'll allocate an item in the soft state of the device.
* The item index is stored in the dev_t. I hope this is ok...
*/
unsigned iOpenInstance;
{
{
break;
}
}
if (!pState)
{
return ENXIO;
}
/*
* Create a new session.
*/
if (RT_SUCCESS(rc))
{
OSDBGPRINT(("VBoxDrvSolarisOpen: Dev=%#x pSession=%p pid=%d r0proc=%p thread=%p\n",
return 0;
}
/* failed - clean up */
#else
/*
* Create a new session.
* Sessions in Solaris driver are mostly useless. It's however needed
* in VBoxDrvSolarisIOCtlSlow() while calling supdrvIOCtl()
*/
if (RT_SUCCESS(rc))
{
unsigned iHash;
/*
* Insert it into the hash table.
*/
OSDBGPRINT(("VBoxDrvSolarisOpen success"));
}
int instance;
{
if (pState)
break;
}
if (instance >= DEVICE_MAXINSTANCES)
{
return ENXIO;
}
return VBoxSupDrvErr2SolarisErr(rc);
#endif
}
{
#ifndef USE_SESSION_HASH
/*
* Get the session and free the soft state item.
*/
if (!pState)
{
return EFAULT;
}
if (!pSession)
{
return EFAULT;
}
dprintf(("VBoxDrvSolarisClose: Dev=%#x pSession=%p pid=%d r0proc=%p thread=%p\n", Dev, pSession, RTProcSelf(), RTR0ProcHandleSelf(), RTThreadNativeSelf() ));
#else
/*
* Remove from the hash table.
*/
if (pSession)
{
{
}
else
{
while (pSession)
{
{
break;
}
/* next */
}
}
}
if (!pSession)
{
OSDBGPRINT(("VBoxDrvSolarisClose: WHAT?!? pSession == NULL! This must be a mistake... pid=%d (close)\n",
(int)Process));
return EFAULT;
}
#endif
/*
* Close the session.
*/
dprintf(("VBoxDrvSolarisClose: returns\n"));
return 0;
}
{
dprintf(("VBoxDrvSolarisRead"));
return 0;
}
{
dprintf(("VBoxDrvSolarisWrite"));
return 0;
}
/**
* Driver ioctl, an alternate entry point for this character driver.
*
* @param Dev Device number
* @param Cmd Operation identifier
* @param pArg Arguments from user to driver
* @param pCred User credentials
* @param pVal Return value for calling process.
*
* @return corresponding solaris error code.
*/
static int VBoxDrvSolarisIOCtl(dev_t Dev, int Cmd, intptr_t pArgs, int Mode, cred_t *pCred, int *pVal)
{
#ifndef USE_SESSION_HASH
/*
* Get the session from the soft state item.
*/
if (!pState)
{
return EINVAL;
}
if (!pSession)
{
return DDI_SUCCESS;
}
#else
/*
* Find the session.
*/
{
}
if (!pSession)
{
OSDBGPRINT(("VBoxSupDrvIOCtl: WHAT?!? pSession == NULL! This must be a mistake... pid=%d iCmd=%#x\n",
return EINVAL;
}
#endif
/*
* Deal with the two high-speed IOCtl that takes it's arguments from
* the session and iCmd, and only returns a VBox status code.
*/
if ( Cmd == SUP_IOCTL_FAST_DO_RAW_RUN
|| Cmd == SUP_IOCTL_FAST_DO_NOP)
{
return 0;
}
}
/** @def IOCPARM_LEN
* Gets the length from the ioctl number.
*/
#ifndef IOCPARM_LEN
#endif
/**
* Worker for VBoxSupDrvIOCtl that takes the slow IOCtl functions.
*
* @returns Solaris errno.
*
* @param pSession The session.
* @param Cmd The IOCtl command.
* @param Mode Information bitfield (for specifying ownership of data)
* @param iArg User space address of the request buffer.
*/
{
int rc;
/*
* Read the header.
*/
{
OSDBGPRINT(("VBoxDrvSolarisIOCtlSlow: iCmd=%#x len %d expected %d\n", iCmd, IOCPARM_LEN(iCmd), sizeof(Hdr)));
return EINVAL;
}
if (RT_UNLIKELY(rc))
{
OSDBGPRINT(("VBoxDrvSolarisIOCtlSlow: ddi_copyin(,%#lx,) failed; iCmd=%#x. rc=%d\n", iArg, iCmd, rc));
return EFAULT;
}
{
OSDBGPRINT(("VBoxDrvSolarisIOCtlSlow: bad header magic %#x; iCmd=%#x\n", Hdr.fFlags & SUPREQHDR_FLAGS_MAGIC_MASK, iCmd));
return EINVAL;
}
{
return EINVAL;
}
/*
* Buffer the request.
*/
if (RT_UNLIKELY(!pHdr))
{
OSDBGPRINT(("VBoxDrvSolarisIOCtlSlow: failed to allocate buffer of %d bytes for iCmd=%#x.\n", cbBuf, iCmd));
return ENOMEM;
}
if (RT_UNLIKELY(rc))
{
dprintf(("VBoxDrvSolarisIOCtlSlow: copy_from_user(,%#lx, %#x) failed; iCmd=%#x. rc=%d\n", iArg, Hdr.cbIn, iCmd, rc));
return EFAULT;
}
/*
* Process the IOCtl.
*/
/*
* Copy ioctl data and output buffer back to user space.
*/
{
{
OSDBGPRINT(("VBoxDrvSolarisIOCtlSlow: too much output! %#x > %#x; iCmd=%#x!\n", cbOut, cbBuf, iCmd));
}
if (RT_UNLIKELY(rc != 0))
{
/* this is really bad */
OSDBGPRINT(("VBoxDrvSolarisIOCtlSlow: ddi_copyout(,%p,%d) failed. rc=%d\n", (void *)iArg, cbBuf, rc));
}
}
else
return rc;
}
/**
* The SUPDRV IDC entry point.
*
* @returns VBox status code, see supdrvIDC.
* @param iReq The request code.
* @param pReq The request.
*/
{
/*
* Some quick validations.
*/
return VERR_INVALID_POINTER;
if (pSession)
{
return VERR_INVALID_PARAMETER;
return VERR_INVALID_PARAMETER;
}
return VERR_INVALID_PARAMETER;
/*
* Do the job.
*/
}
/**
* Converts an supdrv error code to a solaris error code.
*
* @returns corresponding solaris error code.
* @param rc supdrv error code (SUPDRV_ERR_* defines).
*/
static int VBoxSupDrvErr2SolarisErr(int rc)
{
switch (rc)
{
case 0: return 0;
case SUPDRV_ERR_GENERAL_FAILURE: return EACCES;
case SUPDRV_ERR_INVALID_PARAM: return EINVAL;
case SUPDRV_ERR_INVALID_MAGIC: return EILSEQ;
case SUPDRV_ERR_INVALID_HANDLE: return ENXIO;
case SUPDRV_ERR_INVALID_POINTER: return EFAULT;
case SUPDRV_ERR_LOCK_FAILED: return ENOLCK;
case SUPDRV_ERR_ALREADY_LOADED: return EEXIST;
case SUPDRV_ERR_PERMISSION_DENIED: return EPERM;
case SUPDRV_ERR_VERSION_MISMATCH: return ENOSYS;
}
return EPERM;
}
/**
* Initializes any OS specific object creator fields.
*/
{
}
/**
* Checks if the session can access the object.
*
* @returns true if a decision has been made.
* @returns false if the default access policy should be applied.
*
* @param pObj The object in question.
* @param pSession The session wanting to access the object.
* @param pszObjName The object name, can be NULL.
* @param prc Where to store the result when returning true.
*/
bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
{
return false;
}
{
return false;
}
{
char szMsg[512];
#if 0
#endif
#if 1
#endif
return 0;
}