SUPDrv-solaris.c revision e39c22eba1ddc781ed559ce99abb93e992c73496
/* $Id$ */
/** @file
* VBoxDrv - The VirtualBox Support Driver - Solaris specifics.
*/
/*
* Copyright (C) 2006-2014 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_SUP_DRV
#ifdef DEBUG_ramshankar
# define LOG_ENABLED
# define LOG_INSTANCE RTLogRelDefaultInstance()
#endif
#include <sys/kobj_impl.h>
#include <sys/priv_names.h>
#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 system device name. */
#define DEVICE_NAME_SYS "vboxdrv"
/** The user device name. */
#define DEVICE_NAME_USR "vboxdrvu"
/** The module description as seen in 'modinfo'. */
#define DEVICE_DESC "VirtualBox HostDrv"
/** 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;
/** Hash table */
/** Spinlock protecting g_apSessionHashTab. */
/** Calculates bucket index into g_apSessionHashTab.*/
/**
* Kernel entry points
*/
int _init(void)
{
#if 0 /* No IPRT logging before RTR0Init() is done! */
LogFlowFunc(("vboxdrv:_init\n"));
#endif
/*
* Prevent module autounloading.
*/
if (pModCtl)
else
/*
* 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))
{
if (!rc)
{
if (!rc)
return rc; /* success */
}
else
LogRel(("vboxdrv: failed to initialize soft state.\n"));
}
else
{
LogRel(("VBoxDrvSolarisAttach: RTSpinlockCreate failed\n"));
}
}
else
{
LogRel(("VBoxDrvSolarisAttach: supdrvInitDevExt failed\n"));
}
}
else
{
LogRel(("VBoxDrvSolarisAttach: failed to init R0Drv\n"));
}
return rc;
}
int _fini(void)
{
LogFlowFunc(("vboxdrv:_fini\n"));
/*
* Undo the work we did at start (in the reverse order).
*/
if (rc != 0)
return rc;
return 0;
}
{
#if 0 /* No IPRT logging before RTR0Init() is done! And yes this is called before _init()!*/
LogFlowFunc(("vboxdrv:_init\n"));
#endif
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.
*/
{
LogFlowFunc(("VBoxDrvSolarisAttach\n"));
switch (enmCmd)
{
case DDI_ATTACH:
{
int rc;
#ifdef USE_SESSION_HASH
{
LogRel(("VBoxDrvSolarisAttach: state alloc failed\n"));
return DDI_FAILURE;
}
#endif
/*
*/
"pm-hardware-state", "needs-suspend-resume", sizeof("needs-suspend-resume"));
if (rc != DDI_PROP_SUCCESS)
/*
* Register ourselves as a character device, pseudo-driver
*/
#ifdef VBOX_WITH_HARDENING
#else
0, "none", "none", 0666);
#endif
if (rc == DDI_SUCCESS)
{
0, "none", "none", 0666);
if (rc == DDI_SUCCESS)
{
#ifdef USE_SESSION_HASH
#endif
return DDI_SUCCESS;
}
}
return DDI_FAILURE;
}
case DDI_RESUME:
{
#if 0
#endif
LogFlow(("vboxdrv: Awakened from suspend.\n"));
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.
*/
{
LogFlowFunc(("VBoxDrvSolarisDetach\n"));
switch (enmCmd)
{
case DDI_DETACH:
{
#ifndef USE_SESSION_HASH
#else
#endif
return DDI_SUCCESS;
}
case DDI_SUSPEND:
{
#if 0
#endif
LogFlow(("vboxdrv: Falling to suspend mode.\n"));
return DDI_SUCCESS;
}
default:
return DDI_FAILURE;
}
}
/**
* Quiesce not-needed entry point, as Solaris 10 doesn't have any
* ddi_quiesce_not_needed() function.
*
* @param pDip The module structure instance.
*
* @return corresponding solaris error code.
*/
{
return DDI_SUCCESS;
}
/**
* open() worker.
*/
{
int rc;
/*
* Validate input
*/
return EINVAL; /* See mmopen for precedent. */
#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)
{
LogRel(("VBoxDrvSolarisOpen: too many open instances.\n"));
return ENXIO;
}
/*
* Create a new session.
*/
if (RT_SUCCESS(rc))
{
LogFlow(("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.
*/
# error "Only one entry per process!"
LogFlow(("VBoxDrvSolarisOpen success\n"));
}
int instance;
{
if (pState)
break;
}
if (instance >= DEVICE_MAXINSTANCES)
{
LogRel(("VBoxDrvSolarisOpen: All instances exhausted\n"));
return ENXIO;
}
#endif
return VBoxSupDrvErr2SolarisErr(rc);
}
{
#ifndef USE_SESSION_HASH
/*
* Get the session and free the soft state item.
*/
if (!pState)
{
return EFAULT;
}
if (!pSession)
{
return EFAULT;
}
LogFlow(("VBoxDrvSolarisClose: Dev=%#x pSession=%p pid=%d r0proc=%p thread=%p\n",
#else
/*
* Remove from the hash table.
*/
if (pSession)
{
{
}
else
{
while (pSession)
{
{
break;
}
/* next */
}
}
}
if (!pSession)
{
LogRel(("VBoxDrvSolarisClose: WHAT?!? pSession == NULL! This must be a mistake... pid=%d (close)\n", (int)Process));
return EFAULT;
}
#endif
/*
* Close the session.
*/
return 0;
}
{
LogFlowFunc(("VBoxDrvSolarisRead"));
return 0;
}
{
LogFlowFunc(("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)
{
LogRel(("VBoxSupDrvIOCtl: WHAT?!? pSession == NULL! This must be a mistake... pid=%d iCmd=%#x Dev=%#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_HM_RUN
|| Cmd == SUP_IOCTL_FAST_DO_NOP)
&& pSession->fUnrestricted)
{
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;
union
{
} StackBuf;
/*
* Read the header.
*/
{
LogRel(("VBoxDrvSolarisIOCtlSlow: iCmd=%#x len %d expected %d\n", iCmd, IOCPARM_LEN(iCmd), sizeof(StackBuf.Hdr)));
return EINVAL;
}
if (RT_UNLIKELY(rc))
{
return EFAULT;
}
{
LogRel(("VBoxDrvSolarisIOCtlSlow: bad header magic %#x; iCmd=%#x\n", StackBuf.Hdr.fFlags & SUPREQHDR_FLAGS_MAGIC_MASK, iCmd));
return EINVAL;
}
{
LogRel(("VBoxDrvSolarisIOCtlSlow: max(%#x,%#x); iCmd=%#x\n", StackBuf.Hdr.cbIn, StackBuf.Hdr.cbOut, iCmd));
return EINVAL;
}
/*
* Buffer the request.
*/
else
{
if (RT_UNLIKELY(!pHdr))
{
LogRel(("VBoxDrvSolarisIOCtlSlow: failed to allocate buffer of %d bytes for iCmd=%#x.\n", cbBuf, iCmd));
return ENOMEM;
}
}
if (RT_UNLIKELY(rc))
{
LogRel(("VBoxDrvSolarisIOCtlSlow: copy_from_user(,%#lx, %#x) failed; iCmd=%#x. rc=%d\n", iArg, cbBuf, iCmd, rc));
return EFAULT;
}
/*
* Process the IOCtl.
*/
/*
* Copy ioctl data and output buffer back to user space.
*/
{
{
}
if (RT_UNLIKELY(rc != 0))
{
/* this is really bad */
}
}
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 IPRT status code.
*/
static int VBoxSupDrvErr2SolarisErr(int rc)
{
switch (rc)
{
case VINF_SUCCESS: return 0;
case VERR_GENERAL_FAILURE: return EACCES;
case VERR_INVALID_PARAMETER: return EINVAL;
case VERR_INVALID_MAGIC: return EILSEQ;
case VERR_INVALID_HANDLE: return ENXIO;
case VERR_INVALID_POINTER: return EFAULT;
case VERR_LOCK_FAILED: return ENOLCK;
case VERR_ALREADY_LOADED: return EEXIST;
case VERR_PERMISSION_DENIED: return EPERM;
case VERR_VERSION_MISMATCH: return ENOSYS;
}
return EPERM;
}
{
}
void VBOXCALL supdrvOSSessionHashTabInserted(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, void *pvUser)
{
}
void VBOXCALL supdrvOSSessionHashTabRemoved(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, void *pvUser)
{
}
/**
* 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;
}
#if defined(VBOX_WITH_NATIVE_SOLARIS_LOADING) \
&& !defined(VBOX_WITHOUT_NATIVE_R0_LOADER)
int VBOXCALL supdrvOSLdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const char *pszFilename)
{
# if 1 /* This approach requires _init/_fini/_info stubs. */
/*
* Construct a filename that escapes the module search path and let us
* specify a root path.
*/
/** @todo change this to use modctl and use_path=0. */
char *pszSubDir = RTStrAPrintf2("../../../../../../../../../../..%.*s", pszName - pszFilename - 1, pszFilename);
if (!pszSubDir)
return VERR_NO_STR_MEMORY;
if (idMod == -1)
{
/* This is an horrible hack for avoiding the mod-present check in
modrload on S10. Fortunately, nobody else seems to be using that
variable... */
extern int swaploaded;
int saved_swaploaded = swaploaded;
swaploaded = 0;
}
if (idMod == -1)
{
return VERR_LDR_GENERAL_FAILURE;
}
if (!pModCtl)
{
/* No point in calling modunload. */
return VERR_LDR_GENERAL_FAILURE;
}
# else
const int idMod = -1;
if (!pModCtl)
{
return VERR_LDR_GENERAL_FAILURE;
}
if (rc != 0)
{
return RTErrConvertFromErrno(rc);
}
# endif
/*
* Get the module info.
*
* Note! The text section is actually not at mi_base, but and the next
* alignment boundrary and there seems to be no easy way of
* getting at this address. This sabotages supdrvOSLdrLoad.
* Bastards!
*/
LogRel(("supdrvOSLdrOpen: succeeded for '%s' (mi_base=%p mi_size=%#x), id=%d ctl=%p\n",
return VINF_SUCCESS;
}
{
}
int VBOXCALL supdrvOSLdrValidatePointer(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, void *pv, const uint8_t *pbImageBits)
{
return VERR_INVALID_PARAMETER;
return VINF_SUCCESS;
}
/**
* Resolves a module entry point address.
*
* @returns VBox status code.
* @param pImage The image.
* @param pszSymbol The symbol name.
* @param ppvValue Where to store the value. On input this holds
* the symbol value SUPLib calculated.
*/
{
/* Don't try resolve symbols which, according to SUPLib, aren't there. */
if (!*ppvValue)
return VINF_SUCCESS;
if (!uValue)
{
return VERR_SYMBOL_NOT_FOUND;
}
return VINF_SUCCESS;
}
int VBOXCALL supdrvOSLdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const uint8_t *pbImageBits, PSUPLDRLOAD pReq)
{
#if 0 /* This doesn't work because of text alignment. */
/*
* Comparing is very very difficult since text and data may be allocated
* separately.
*/
{
return VERR_LDR_MISMATCH_NATIVE;
}
#endif
/*
* Get the exported symbol addresses.
*/
int rc;
{
while (iSym-- > 0)
{
if (!uValue)
{
LogRel(("supdrvOSLdrLoad on %s failed to resolve the exported symbol: '%s'\n", pImage->szName, pszSymbol));
break;
}
{
LogRel(("supdrvOSLdrLoad on %s symbol out of range: %p (%s) \n", pImage->szName, offSymbol, pszSymbol));
break;
}
}
/*
* Get the standard module entry points.
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
case SUPLDRLOADEP_VMMR0:
{
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
rc = supdrvSolLdrResolvEp(pImage, "VMMR0EntryFast", (void **)&pReq->u.In.EP.VMMR0.pvVMMR0EntryFast);
if (RT_SUCCESS(rc))
break;
}
case SUPLDRLOADEP_SERVICE:
{
/** @todo we need the name of the entry point. */
return VERR_NOT_SUPPORTED;
}
}
}
}
else
{
}
return rc;
}
{
# if 1
if (rc)
# else
# endif
}
#else /* !VBOX_WITH_NATIVE_SOLARIS_LOADING */
int VBOXCALL supdrvOSLdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const char *pszFilename)
{
return VERR_NOT_SUPPORTED;
}
{
}
int VBOXCALL supdrvOSLdrValidatePointer(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, void *pv, const uint8_t *pbImageBits)
{
return VERR_NOT_SUPPORTED;
}
int VBOXCALL supdrvOSLdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const uint8_t *pbImageBits, PSUPLDRLOAD pReq)
{
return VERR_NOT_SUPPORTED;
}
{
}
#endif /* !VBOX_WITH_NATIVE_SOLARIS_LOADING */
#ifdef SUPDRV_WITH_MSR_PROBER
{
/** @todo cmi_hdl_rdmsr can safely do this. there is also the on_trap() fun
* for catching traps that could possibly be used directly. */
return VERR_NOT_SUPPORTED;
}
{
/** @todo cmi_hdl_wrmsr can safely do this. */
return VERR_NOT_SUPPORTED;
}
{
return VERR_NOT_SUPPORTED;
}
#endif /* SUPDRV_WITH_MSR_PROBER */
{
char szMsg[512];
/* cmn_err() acquires adaptive mutexes. Not preemption safe, see @bugref{6657}. */
return 0;
return 0;
}
/**
* Returns configuration flags of the host kernel.
*/
{
return 0;
}