SUPDrvInternal.h revision 344115a50dfcaf97123d38b056161195a7e4e48c
/* $Revision$ */
/** @file
* VirtualBox Support Driver - Internal header.
*/
/*
* Copyright (C) 2006-2007 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.
*/
#ifndef ___SUPDrvInternal_h
#define ___SUPDrvInternal_h
/*******************************************************************************
* Header Files *
*******************************************************************************/
#ifdef SUPDRV_AGNOSTIC
/* do nothing */
#elif defined(RT_OS_WINDOWS)
# include <ntddk.h>
# pragma warning(default : 4163)
# else
# include <ntddk.h>
# endif
# include <memory.h>
#elif defined(RT_OS_LINUX)
# include <generated/autoconf.h>
# else
# ifndef AUTOCONF_INCLUDED
# include <linux/autoconf.h>
# endif
# endif
# if defined(CONFIG_MODVERSIONS) && !defined(MODVERSIONS)
# define MODVERSIONS
# include <linux/modversions.h>
# endif
# endif
# endif
# ifndef KBUILD_STR
# define KBUILD_STR(s) s
# else
# define KBUILD_STR(s) #s
# endif
# endif
# include <linux/spinlock.h>
# include <linux/semaphore.h>
# else /* older kernels */
# include <asm/semaphore.h>
# endif /* older kernels */
#elif defined(RT_OS_DARWIN)
#elif defined(RT_OS_FREEBSD)
# define memset libkern_memset /** @todo these are just hacks to get it compiling, check out later. */
# define memcmp libkern_memcmp
# define strchr libkern_strchr
# define strrchr libkern_strrchr
# define ffsl libkern_ffsl
# define fls libkern_fls
# define flsl libkern_flsl
#elif defined(RT_OS_SOLARIS)
#else
# error "unsupported OS."
#endif
#include "SUPDrvIOC.h"
#include "SUPDrvIDC.h"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/*
* Hardcoded cookies.
*/
#ifdef RT_OS_WINDOWS
/** Use a normal mutex for the loader so we remain at the same IRQL after
* taking it.
* @todo fix the mutex implementation on linux and make this the default. */
# define SUPDRV_USE_MUTEX_FOR_LDR
/** Use a normal mutex for the GIP so we remain at the same IRQL after
* taking it.
* @todo fix the mutex implementation on linux and make this the default. */
# define SUPDRV_USE_MUTEX_FOR_GIP
#endif
/**
* OS debug print macro.
*/
#define OSDBGPRINT(a) SUPR0Printf a
/** @name Context values for the per-session handle tables.
* The context value is used to distinguish between the different kinds of
* handles, making the handle table API do all the work.
* @{ */
/** Handle context value for single release event handles. */
/** Handle context value for multiple release event handles. */
/** @} */
/**
* Validates a session pointer.
*
* @param pSession The session.
*/
#define SUP_IS_SESSION_VALID(pSession) \
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/** Pointer to the device extension. */
typedef struct SUPDRVDEVEXT *PSUPDRVDEVEXT;
/**
* Memory reference types.
*/
typedef enum
{
/** Unused entry */
MEMREF_TYPE_UNUSED = 0,
/** Locked memory (r3 mapping only). */
/** Continuous memory block (r3 and r0 mapping). */
/** Low memory block (r3 and r0 mapping). */
/** Memory block (r3 and r0 mapping). */
/** Locked memory (r3 mapping only) allocated by the support driver. */
/** Blow the type up to 32-bit and mark the end. */
MEMREF_TYPE_32BIT_HACK = 0x7fffffff
/**
* Structure used for tracking memory a session
* references in one way or another.
*/
typedef struct SUPDRVMEMREF
{
/** The memory object handle. */
/** The ring-3 mapping memory object handle. */
/** Type of memory. */
/**
* Bundle of locked memory ranges.
*/
typedef struct SUPDRVBUNDLE
{
/** Pointer to the next bundle. */
struct SUPDRVBUNDLE * volatile pNext;
/** Referenced memory. */
/** Number of entries used. */
/**
* Loaded image.
*/
typedef struct SUPDRVLDRIMAGE
{
/** Next in chain. */
struct SUPDRVLDRIMAGE * volatile pNext;
/** Pointer to the image. */
void *pvImage;
/** Pointer to the allocated image buffer.
* pvImage is 32-byte aligned or it may governed by the native loader (this
* member is NULL then). */
void *pvImageAlloc;
/** Size of the image including the tables. This is mainly for verification
* of the load request. */
/** Size of the image. */
/** The number of entries in the symbol table. */
/** Pointer to the symbol table. */
/** The offset of the string table. */
char *pachStrTab;
/** Size of the string table. */
/** Pointer to the optional module initialization callback. */
/** Pointer to the optional module termination callback. */
/** Service request handler. This is NULL for non-service modules. */
/** The ldr image state. (IOCtl code of last operation.) */
/** Usage count. */
#ifdef RT_OS_WINDOWS
/** The section object for the loaded image (fNative=true). */
void *pvNtSectionObj;
/** Lock object. */
#endif
#if defined(RT_OS_SOLARIS) && defined(VBOX_WITH_NATIVE_SOLARIS_LOADING)
/** The Solaris module ID. */
int idSolMod;
/** Pointer to the module control structure. */
struct modctl *pSolModCtl;
#endif
/** Whether it's loaded by the native loader or not. */
bool fNative;
/** Image name. */
char szName[32];
/** Image usage record. */
typedef struct SUPDRVLDRUSAGE
{
/** Next in chain. */
struct SUPDRVLDRUSAGE * volatile pNext;
/** The image. */
/** Load count. */
/**
* Component factory registration record.
*/
typedef struct SUPDRVFACTORYREG
{
/** Pointer to the next registration. */
struct SUPDRVFACTORYREG *pNext;
/** Pointer to the registered factory. */
/** The session owning the factory.
* Used for deregistration and session cleanup. */
/** Length of the name. */
/** Pointer to a component factory registration record. */
typedef SUPDRVFACTORYREG *PSUPDRVFACTORYREG;
/** Pointer to a const component factory registration record. */
typedef SUPDRVFACTORYREG const *PCSUPDRVFACTORYREG;
/**
* Registered object.
* This takes care of reference counting and tracking data for access checks.
*/
typedef struct SUPDRVOBJ
{
/** Magic value (SUPDRVOBJ_MAGIC). */
/** The object type. */
/** Pointer to the next in the global list. */
/** Pointer to the object destructor.
* This may be set to NULL if the image containing the destructor get unloaded. */
/** User argument 1. */
void *pvUser1;
/** User argument 2. */
void *pvUser2;
/** The total sum of all per-session usage. */
/** The creator user id. */
/** The creator group id. */
/** The creator process id. */
} SUPDRVOBJ, *PSUPDRVOBJ;
/** Magic number for SUPDRVOBJ::u32Magic. (Dame Agatha Mary Clarissa Christie). */
#define SUPDRVOBJ_MAGIC 0x18900915
/** Dead number magic for SUPDRVOBJ::u32Magic. */
#define SUPDRVOBJ_MAGIC_DEAD 0x19760112
/**
* The per-session object usage record.
*/
typedef struct SUPDRVUSAGE
{
/** Pointer to the next in the list. */
struct SUPDRVUSAGE * volatile pNext;
/** Pointer to the object we're recording usage for. */
/** The usage count. */
} SUPDRVUSAGE, *PSUPDRVUSAGE;
/**
* Per session data.
* This is mainly for memory tracking.
*/
typedef struct SUPDRVSESSION
{
/** Pointer to the device extension. */
/** Session Cookie. */
/** The VM associated with the session. */
/** Handle table for IPRT semaphore wrapper APIs.
* Programmable from R0 and R3. */
/** Load usage records. (protected by SUPDRVDEVEXT::mtxLdr) */
PSUPDRVLDRUSAGE volatile pLdrUsage;
/** Spinlock protecting the bundles and the GIP members. */
/** The ring-3 mapping of the GIP (readonly). */
/** Set if the session is using the GIP. */
/** Bundle of locked memory objects. */
/** List of generic usage records. (protected by SUPDRVDEVEXT::SpinLock) */
PSUPDRVUSAGE volatile pUsage;
/** The user id of the session. (Set by the OS part.) */
/** The group id of the session. (Set by the OS part.) */
/** The process (id) of the session. */
/** Which process this session is associated with.
* This is NIL_RTR0PROCESS for kernel sessions and valid for user ones. */
#ifndef SUPDRV_AGNOSTIC
# if defined(RT_OS_DARWIN)
/** Pointer to the associated org_virtualbox_SupDrvClient object. */
void *pvSupDrvClient;
/** Whether this session has been opened or not. */
bool fOpened;
# endif
# if defined(RT_OS_OS2)
/** The system file number of this session. */
# endif
/** Pointer to the next session with the same hash. */
# endif
#endif /* !SUPDRV_AGNOSTIC */
/**
* Device extension.
*/
typedef struct SUPDRVDEVEXT
{
/** Spinlock to serialize the initialization, usage counting and objects. */
/** List of registered objects. Protected by the spinlock. */
PSUPDRVOBJ volatile pObjs;
/** List of free object usage records. */
PSUPDRVUSAGE volatile pUsageFree;
/** Global cookie. */
/** The actual size of SUPDRVSESSION. (SUPDRV_AGNOSTIC) */
/** Loader mutex.
* This protects pvVMMR0, pvVMMR0Entry, pImages and SUPDRVSESSION::pLdrUsage. */
#ifdef SUPDRV_USE_MUTEX_FOR_LDR
#else
#endif
/** VMM Module 'handle'.
* 0 if the code VMM isn't loaded and Idt are nops. */
void * volatile pvVMMR0;
/** VMMR0EntryInt() pointer. */
/** VMMR0EntryFast() pointer. */
/** VMMR0EntryEx() pointer. */
DECLR0CALLBACKMEMBER(int, pfnVMMR0EntryEx, (PVM pVM, VMCPUID idCpu, unsigned uOperation, PSUPVMMR0REQHDR pReq, uint64_t u64Arg, PSUPDRVSESSION pSession));
/** Linked list of loaded code. */
PSUPDRVLDRIMAGE volatile pLdrImages;
/** GIP mutex.
* Any changes to any of the GIP members requires ownership of this mutex,
* except on driver init and termination. */
#ifdef SUPDRV_USE_MUTEX_FOR_GIP
#else
#endif
/** Pointer to the Global Info Page (GIP). */
/** The physical address of the GIP. */
/** Number of processes using the GIP.
* (The updates are suspend while cGipUsers is 0.)*/
/** The ring-0 memory object handle for the GIP page. */
/** The GIP timer handle. */
/** If non-zero we've successfully called RTTimerRequestSystemGranularity(). */
/** The CPU id of the GIP master.
* This CPU is responsible for the updating the common GIP data. */
RTCPUID volatile idGipMaster;
/** Component factory mutex.
* This protects pComponentFactoryHead and component factory querying. */
/** The head of the list of registered component factories. */
#ifndef SUPDRV_AGNOSTIC
# ifdef RT_OS_WINDOWS
/* Callback object returned by ExCreateCallback. */
/* Callback handle returned by ExRegisterCallback. */
# endif
#endif
} SUPDRVDEVEXT;
/*******************************************************************************
* OS Specific Functions *
*******************************************************************************/
bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc);
/**
* Try open the image using the native loader.
*
* @returns IPRT status code.
* @retval VERR_NOT_SUPPORTED if native loading isn't supported.
*
* @param pDevExt The device globals.
* @param pImage The image handle. pvImage should be set on
* success, pvImageAlloc can also be set if
* appropriate.
* @param pszFilename The file name - UTF-8, may containing UNIX
* slashes on non-UNIX systems.
*/
int VBOXCALL supdrvOSLdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const char *pszFilename);
/**
* Validates an entry point address.
*
* Called before supdrvOSLdrLoad.
*
* @returns IPRT status code.
* @param pDevExt The device globals.
* @param pImage The image data (still in the open state).
* @param pv The address within the image.
* @param pbImageBits The image bits as loaded by ring-3.
*/
/**
* Load the image.
*
* @returns IPRT status code.
* @param pDevExt The device globals.
* @param pImage The image data (up to date except for some
* entry point pointers).
* @param pbImageBits The image bits as loaded by ring-3.
*/
int VBOXCALL supdrvOSLdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const uint8_t *pbImageBits);
/**
* Unload the image.
*
* @param pDevExt The device globals.
* @param pImage The image data (mostly still valid).
*/
/*******************************************************************************
* Shared Functions *
*******************************************************************************/
/* SUPDrv.c */
int VBOXCALL supdrvIOCtl(uintptr_t uIOCtl, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPREQHDR pReqHdr);
int VBOXCALL supdrvIOCtlFast(uintptr_t uIOCtl, VMCPUID idCpu, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession);
int VBOXCALL supdrvIDC(uintptr_t uIOCtl, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVIDCREQHDR pReqHdr);
#endif