SUPDRV.h revision 0a769a6be37f526faeabe88f77422ee6291afa37
/* $Revision$ */
/** @file
* VirtualBox Support Driver - Internal header.
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* 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 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.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
#ifndef __SUPDRV_h__
#define __SUPDRV_h__
/*******************************************************************************
* Header Files *
*******************************************************************************/
#ifdef USE_NEW_OS_INTERFACE
#endif
#if defined(RT_OS_WINDOWS)
# include <ntddk.h>
# else
# include <ntddk.h>
# endif
# include <memory.h>
#elif defined(RT_OS_LINUX)
# include <linux/autoconf.h>
# 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 <asm/semaphore.h>
#elif defined(RT_OS_DARWIN)
#elif defined(RT_OS_FREEBSD)
#elif defined(RT_OS_SOLARIS)
#else
# error "unsupported OS."
#endif
#include "SUPDRVIOC.h"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/*
* Hardcoded cookies.
*/
/*
* Win32
*/
#if defined(RT_OS_WINDOWS)
/* debug printf */
# define OSDBGPRINT(a) DbgPrint a
/** Maximum number of bytes we try to lock down in one go.
* This is supposed to have a limit right below 256MB, but this appears
* to actually be much lower. The values here have been determined experimentally.
*/
#ifdef RT_ARCH_X86
#endif
#ifdef RT_ARCH_AMD64
#endif
/*
* Linux
*/
#elif defined(RT_OS_LINUX)
/* check kernel version */
#endif
int linux_dprintf(const char *format, ...);
/* debug printf */
# define OSDBGPRINT(a) printk a
/*
* Darwin
*/
#elif defined(RT_OS_DARWIN)
/* debug printf */
# define OSDBGPRINT(a) printf a
/*
* OS/2
*/
/* No log API in OS/2 only COM port. */
# define OSDBGPRINT(a) SUPR0Printf a
/*
* FreeBSD
*/
#elif defined(RT_OS_FREEBSD)
/* No log API in OS/2 only COM port. */
# define OSDBGPRINT(a) printf a
/*
* Solaris
*/
#elif defined(RT_OS_SOLARIS)
# define OSDBGPRINT(a) printf a
#else
/** @todo other os'es */
# error "OS interface defines is not done for this OS!"
#endif
/* dprintf */
# ifdef LOG_TO_COM
# define dprintf(a) RTLogComPrintf a
# else
# define dprintf(a) OSDBGPRINT(a)
# endif
#else
# define dprintf(a) do {} while (0)
#endif
/* dprintf2 - extended logging. */
#else
# define dprintf2(a) do { } while (0)
#endif
/*
* Error codes.
*/
/** Invalid parameter. */
#define SUPDRV_ERR_GENERAL_FAILURE (-1)
/** Invalid parameter. */
#define SUPDRV_ERR_INVALID_PARAM (-2)
/** Invalid magic or cookie. */
#define SUPDRV_ERR_INVALID_MAGIC (-3)
/** Invalid loader handle. */
#define SUPDRV_ERR_INVALID_HANDLE (-4)
/** Failed to lock the address range. */
#define SUPDRV_ERR_LOCK_FAILED (-5)
/** Invalid memory pointer. */
#define SUPDRV_ERR_INVALID_POINTER (-6)
/** Failed to patch the IDT. */
#define SUPDRV_ERR_IDT_FAILED (-7)
/** Memory allocation failed. */
#define SUPDRV_ERR_NO_MEMORY (-8)
/** Already loaded. */
#define SUPDRV_ERR_ALREADY_LOADED (-9)
/** Permission denied. */
#define SUPDRV_ERR_PERMISSION_DENIED (-10)
/** Version mismatch. */
#define SUPDRV_ERR_VERSION_MISMATCH (-11)
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/** Pointer to the device extension. */
typedef struct SUPDRVDEVEXT *PSUPDRVDEVEXT;
#ifndef VBOX_WITHOUT_IDT_PATCHING
/**
* An IDT Entry.
*/
typedef struct SUPDRVIDTE
{
/** Low offset word. */
/** Segment Selector. */
#ifdef RT_ARCH_AMD64
/** Interrupt Stack Table index. */
/** Reserved, ignored. */
#else
/** Reserved. */
/** IDT Type part one (not used for task gate). */
#endif
/** IDT Type part two. */
/** Descriptor Privilege level. */
/** Present flag. */
/** High offset word. */
#ifdef RT_ARCH_AMD64
/** The upper top part of the address. */
/** Reserved dword for qword (aligning the struct), ignored. */
#endif
} SUPDRVIDTE, *PSUPDRVIDTE;
/** The u5Type2 value for an interrupt gate. */
#define SUPDRV_IDTE_TYPE2_INTERRUPT_GATE 0x0e
/**
* Patch code.
*/
typedef struct SUPDRVPATCH
{
#define SUPDRV_PATCH_CODE_SIZE 0x50
/** Patch code. */
/** Changed IDT entry (for parnoid UnpatchIdt()). */
/** Saved IDT entry. */
/** Pointer to the IDT.
* We ASSUME the IDT is not re(al)located after bootup and use this as key
* for the patches rather than processor number. This prevents some
* stupid nesting stuff from happening in case of processors sharing the
* IDT.
* We're fucked if the processors have different physical mapping for
* the(se) page(s), but we'll find that out soon enough in VBOX_STRICT mode.
*/
void *pvIdt;
/** Pointer to the IDT entry. */
SUPDRVIDTE volatile *pIdtEntry;
/** Usage counter. */
/** The offset into auCode of the VMMR0Entry fixup. */
/** The offset into auCode of the stub function. */
/** Pointer to the next patch. */
struct SUPDRVPATCH * volatile pNext;
} SUPDRVPATCH, *PSUPDRVPATCH;
/**
* Usage record for a patch.
*/
typedef struct SUPDRVPATCHUSAGE
{
/** Next in the chain. */
struct SUPDRVPATCHUSAGE * volatile pNext;
/** The patch this usage applies to. */
/** Usage count. */
#endif /* !VBOX_WITHOUT_IDT_PATCHING */
/**
* Memory reference types.
*/
typedef enum
{
/** Unused entry */
MEMREF_TYPE_UNUSED = 0,
/** Locked memory (r3 mapping only). */
/** Continous memory block (r3 and r0 mapping). */
/** Low memory block (r3 and r0 mapping). */
/** Memory block (r3 and r0 mapping). */
/** Blow the type up to 32-bit and mark the end. */
MEMREG_TYPE_32BIT_HACK = 0x7fffffff
/**
* Structure used for tracking memory a session
* references in one way or another.
*/
typedef struct SUPDRVMEMREF
{
#ifdef USE_NEW_OS_INTERFACE
/** The memory object handle. */
/** The ring-3 mapping memory object handle. */
/** Type of memory. */
#else /* !USE_NEW_OS_INTERFACE */
/** Pointer to the R0 mapping of the memory.
* Set to NULL if N/A. */
void *pvR0;
/** Pointer to the R3 mapping of the memory.
* Set to NULL if N/A. */
/** Size of the locked memory. */
unsigned cb;
/** Type of memory. */
/** memory type specific information. */
union
{
struct
{
#if defined(RT_OS_WINDOWS)
/** Pointer to memory descriptor list (MDL). */
unsigned cMdls;
#elif defined(RT_OS_LINUX)
unsigned cPages;
#else
# error "Either no target was defined or we haven't ported the driver to the target yet."
#endif
} locked;
struct
{
#if defined(RT_OS_WINDOWS)
/** Pointer to memory descriptor list (MDL). */
#elif defined(RT_OS_LINUX)
unsigned cPages;
#else
# error "Either no target was defined or we haven't ported the driver to the target yet."
#endif
} cont;
struct
{
#if defined(RT_OS_WINDOWS)
/** Pointer to memory descriptor list (MDL). */
#elif defined(RT_OS_LINUX)
/** Pointer to the array of page pointers. */
/** Number of pages in papPages. */
unsigned cPages;
#else
# error "Either no target was defined or we haven't ported the driver to the target yet."
#endif
} mem;
} u;
#endif /* !USE_NEW_OS_INTERFACE */
/**
* 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 optional module initialization callback. */
/** Pointer to the optional module termination callback. */
/** Size of the image. */
/** The offset of the symbol table. */
/** The number of entries in the symbol table. */
/** The offset of the string table. */
/** Size of the string table. */
/** The ldr image state. (IOCtl code of last opration.) */
/** Usage count. */
/** Image name. */
char szName[32];
/** Image usage record. */
typedef struct SUPDRVLDRUSAGE
{
/** Next in chain. */
struct SUPDRVLDRUSAGE * volatile pNext;
/** The image. */
/** Load count. */
/**
* 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. */
/** 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
/**
* 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. */
/** Load usage records. (protected by SUPDRVDEVEXT::mtxLdr) */
PSUPDRVLDRUSAGE volatile pLdrUsage;
#ifndef VBOX_WITHOUT_IDT_PATCHING
/** Patch usage records. (protected by SUPDRVDEVEXT::SpinLock) */
PSUPDRVPATCHUSAGE volatile pPatchUsage;
#else
/** The VM associated with the session. */
#endif
/** List of generic usage records. (protected by SUPDRVDEVEXT::SpinLock) */
PSUPDRVUSAGE volatile pUsage;
/** Spinlock protecting the bundles and the GIP members. */
#ifdef USE_NEW_OS_INTERFACE
/** The ring-3 mapping of the GIP (readonly). */
#else
/** The read-only usermode mapping address of the GID.
* This is NULL if the GIP hasn't been mapped. */
#endif
/** Set if the session is using the GIP. */
/** Bundle of locked memory objects. */
/** 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. (Set by the OS part.) */
/** Which process this session is associated with. */
#if defined(RT_OS_OS2)
/** The system file number of this session. */
#endif
/** Pointer to the next session with the same hash. */
#endif
/**
* Device extension.
*/
typedef struct SUPDRVDEVEXT
{
/** Spinlock to serialize the initialization,
* usage counting and destruction of the IDT entry override. */
#ifndef VBOX_WITHOUT_IDT_PATCHING
/** List of patches. */
PSUPDRVPATCH volatile pIdtPatches;
/** List of patches Free. */
PSUPDRVPATCH volatile pIdtPatchesFree;
#endif
/** List of registered objects. */
PSUPDRVOBJ volatile pObjs;
/** List of free object usage records. */
PSUPDRVUSAGE volatile pUsageFree;
/** Global cookie. */
/** The IDT entry number.
* Only valid if pIdtPatches is set. */
/** Loader mutex.
* This protects pvVMMR0, pvVMMR0Entry, pImages and SUPDRVSESSION::pLdrUsage. */
/** VMM Module 'handle'.
* 0 if the code VMM isn't loaded and Idt are nops. */
void * volatile pvVMMR0;
/** VMMR0Entry() pointer. */
/** 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. */
/** 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.)*/
#ifdef USE_NEW_OS_INTERFACE
/** The ring-0 memory object handle for the GIP page. */
/** The GIP timer handle. */
/** If non-zero we've successfully called RTTimerRequestSystemGranularity(). */
#endif
#ifdef RT_OS_WINDOWS
/** The GIP timer object. */
/** The GIP DPC object associated with GipTimer. */
/** The GIP DPC objects for updating per-cpu data. */
/** Pointer to the MDL for the pGip page. */
/** GIP timer interval (ms). */
#endif
#ifdef RT_OS_LINUX
/** The last jiffies. */
unsigned long ulLastJiffies;
/** The last mono time stamp. */
uint64_t volatile u64LastMonotime;
/** Set when GIP is suspended to prevent the timers from re-registering themselves). */
uint8_t volatile fGIPSuspended;
# ifdef CONFIG_SMP
/** Array of per CPU data for SUPGIPMODE_ASYNC_TSC. */
struct LINUXCPU
{
/** The last mono time stamp. */
uint64_t volatile u64LastMonotime;
/** The last jiffies. */
unsigned long ulLastJiffies;
/** The Linux Process ID. */
unsigned iSmpProcessorId;
/** The per cpu timer. */
struct timer_list Timer;
} aCPUs[256];
# endif
#endif
} SUPDRVDEVEXT;
/*******************************************************************************
* OS Specific Functions *
*******************************************************************************/
bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc);
#ifndef USE_NEW_OS_INTERFACE
int VBOXCALL supdrvOSContAllocOne(PSUPDRVMEMREF pMem, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PRTHCPHYS pHCPhys);
int VBOXCALL supdrvOSLowAllocOne(PSUPDRVMEMREF pMem, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PSUPPAGE paPages);
unsigned VBOXCALL supdrvOSGetCPUCount(void);
bool VBOXCALL supdrvOSGetForcedAsyncTscMode(void);
#endif
/*******************************************************************************
* Shared Functions *
*******************************************************************************/
#ifdef VBOX_WITHOUT_IDT_PATCHING
#endif
int VBOXCALL supdrvGipInit(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE pGip, RTHCPHYS HCPhys, uint64_t u64NanoTS, unsigned uUpdateHz);
#endif