SUPDrv-freebsd.c revision 1c2c968fd241148110002d75b2c0fdeddc211e14
/* $Id$ */
/** @file
* VBoxDrv - The VirtualBox Support Driver - FreeBSD specifics.
*/
/*
* Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_SUP_DRV
/* Deal with conflicts first. */
#include "../SUPDrvInternal.h"
#include <iprt/initterm.h>
#include <iprt/spinlock.h>
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static int VBoxDrvFreeBSDLoad(void);
static int VBoxDrvFreeBSDUnload(void);
static void VBoxDrvFreeBSDClone(void *pvArg, struct ucred *pCred, char *pachName, int cchName, struct cdev **ppDev);
static d_fdopen_t VBoxDrvFreeBSDOpen;
static d_close_t VBoxDrvFreeBSDClose;
static d_ioctl_t VBoxDrvFreeBSDIOCtl;
static int VBoxDrvFreeBSDIOCtlSlow(PSUPDRVSESSION pSession, u_long ulCmd, caddr_t pvData, struct thread *pTd);
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/**
* Module info structure used by the kernel.
*/
static moduledata_t g_VBoxDrvFreeBSDModule =
{
"vboxdrv",
};
/** Declare the module as a pseudo device. */
/**
*/
static struct cdevsw g_VBoxDrvFreeBSDChrDevSW =
{
.d_name = "vboxdrv"
};
/** List of cloned device. Managed by the kernel. */
static struct clonedevs *g_pVBoxDrvFreeBSDClones;
/** The dev_clone event handler tag. */
/** The device extention. */
static SUPDRVDEVEXT g_VBoxDrvFreeBSDDevExt;
/**
* Module event handler.
*
* @param pMod The module structure.
* @param enmEventType The event type (modeventtype_t).
* @param pvArg Module argument. NULL.
*
* @return 0 on success, errno.h status code on failure.
*/
{
int rc;
switch (enmEventType)
{
case MOD_LOAD:
rc = VBoxDrvFreeBSDLoad();
break;
case MOD_UNLOAD:
rc = VBoxDrvFreeBSDUnload();
break;
case MOD_SHUTDOWN:
case MOD_QUIESCE:
default:
return EOPNOTSUPP;
}
if (RT_SUCCESS(rc))
return 0;
return RTErrConvertToErrno(rc);
}
static int VBoxDrvFreeBSDLoad(void)
{
dprintf(("VBoxDrvFreeBSDLoad:\n"));
/*
* Initialize the runtime.
*/
if (RT_SUCCESS(rc))
{
/*
* Initialize the device extension.
*/
if (RT_SUCCESS(rc))
{
/*
* Configure device cloning.
*/
{
dprintf(("VBoxDrvFreeBSDLoad: returns successfully\n"));
return VINF_SUCCESS;
}
printf("vboxdrv: EVENTHANDLER_REGISTER(dev_clone,,,) failed\n");
}
else
RTR0Term();
}
else
return rc;
}
static int VBoxDrvFreeBSDUnload(void)
{
dprintf(("VBoxDrvFreeBSDUnload:\n"));
/** @todo verify that FreeBSD does reference counting. */
/*
* Reserve what we did in VBoxDrvFreeBSDInit.
*/
RTR0Term();
dprintf(("VBoxDrvFreeBSDUnload: returns\n"));
return VINF_SUCCESS;
}
/**
* DEVFS event handler.
*/
static void VBoxDrvFreeBSDClone(void *pvArg, struct ucred *pCred, char *pszName, int cchName, struct cdev **ppDev)
{
int iUnit;
int rc;
/*
* One device node per user, si_drv1 points to the session.
*/
if (!ppDev)
return;
return;
{
return;
}
if (rc)
{
#ifdef VBOX_WITH_HARDENING
*ppDev = make_dev(&g_VBoxDrvFreeBSDChrDevSW, unit2minor(iUnit), UID_ROOT, GID_WHEEL, 0600, "vboxdrv%d", iUnit);
#else
*ppDev = make_dev(&g_VBoxDrvFreeBSDChrDevSW, unit2minor(iUnit), UID_ROOT, GID_WHEEL, 0666, "vboxdrv%d", iUnit);
#endif
if (*ppDev)
{
dprintf(("VBoxDrvFreeBSDClone: Created *ppDev=%p iUnit=%d si_drv1=%p si_drv2=%p\n",
}
else
}
else
dprintf(("VBoxDrvFreeBSDClone: Existing *ppDev=%p iUnit=%d si_drv1=%p si_drv2=%p\n",
}
/**
*
* @returns 0 on success, errno on failure.
* EBUSY if the device is used by someone else.
* @param pDev The device node.
* @param fOpen The open flags.
* @param pTd The thread.
* @param pFd The file descriptor. FreeBSD 7.0 and later.
* @param iFd The file descriptor index(?). Pre FreeBSD 7.0.
*/
#else
#endif
{
int rc;
/*
* Let's be a bit picky about the flags...
*/
{
return EINVAL;
}
/*
* Try grab it (we don't grab the giant, remember).
*/
return EBUSY;
/*
* Create a new session.
*/
if (RT_SUCCESS(rc))
{
/** @todo get (r)uid and (r)gid.
pSession->Uid = stuff;
pSession->Gid = stuff; */
return 0;
}
return RTErrConvertToErrno(rc);
}
/**
* Close a file device previously opened by VBoxDrvFreeBSDOpen
*
* @returns 0 on success.
* @param pDev The device.
* @param fFile The file descriptor flags.
* @param DevType The device type (CHR.
* @param pTd The calling thread.
*/
{
dprintf(("VBoxDrvFreeBSDClose: fFile=%#x iUnit=%d pSession=%p\n", fFile, minor2unit(minor(pDev)), pSession));
/*
* Close the session if it's still hanging on to the device...
*/
{
}
else
return 0;
}
/**
* I/O control request.
*
* @returns depends...
* @param pDev The device.
* @param ulCmd The command.
* @param pvData Pointer to the data.
* @param fFile The file descriptor flags.
* @param pTd The calling thread.
*/
static int VBoxDrvFreeBSDIOCtl(struct cdev *pDev, u_long ulCmd, caddr_t pvData, int fFile, struct thread *pTd)
{
/*
* Validate the input.
*/
return EINVAL;
/*
* Deal with the fast ioctl path first.
*/
if ( ulCmd == SUP_IOCTL_FAST_DO_RAW_RUN
|| ulCmd == SUP_IOCTL_FAST_DO_NOP)
}
/**
* Deal with the 'slow' I/O control requests.
*
* @returns 0 on success, appropriate errno on failure.
* @param pSession The session.
* @param ulCmd The command.
* @param pvData The request data.
* @param pTd The calling thread.
*/
static int VBoxDrvFreeBSDIOCtlSlow(PSUPDRVSESSION pSession, u_long ulCmd, caddr_t pvData, struct thread *pTd)
{
/*
* Buffered request?
*/
{
{
OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: cbReq=%#x < %#x; ulCmd=%#lx\n", cbReq, (int)sizeof(*pHdr), ulCmd));
return EINVAL;
}
{
return EINVAL;
}
{
OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: max(%#x,%#x) != %#x; ulCmd=%#lx\n", pHdr->cbIn, pHdr->cbOut, cbReq, ulCmd));
return EINVAL;
}
}
/*
* Big unbuffered request?
*/
{
/*
* Read the header, validate it and figure out how much that needs to be buffered.
*/
if (RT_UNLIKELY(rc))
{
return rc;
}
{
return EINVAL;
}
{
return EINVAL;
}
/*
* Allocate buffer and copy in the data.
*/
if (RT_UNLIKELY(!pHdr))
{
OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: failed to allocate buffer of %d bytes; ulCmd=%#lx\n", cbReq, ulCmd));
return ENOMEM;
}
if (RT_UNLIKELY(rc))
{
OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: copyin(%p,%p,%#x) -> %#x; ulCmd=%#lx\n",
return rc;
}
}
else
{
return EINVAL;
}
/*
* Process the IOCtl.
*/
{
/*
* If unbuffered, copy back the result before returning.
*/
if (pvUser)
{
{
OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: too much output! %#x > %#x; uCmd=%#lx!\n", cbOut, cbReq, ulCmd));
}
if (RT_UNLIKELY(rc))
OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: copyout(%p,%p,%#x) -> %d; uCmd=%#lx!\n", pHdr, pvUser, cbOut, rc, ulCmd));
/* cleanup */
}
}
else
{
/*
* The request failed, just clean up.
*/
if (pvUser)
}
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.
*/
}
{
}
bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
{
return false;
}
{
return false;
}
{
char szMsg[256];
int cch;
return cch;
}