SUPDrvInternal.h revision f45a3488706ab0f2c1e2f699bfdc6bf5df0d1122
/* $Revision$ */
/** @file
* VirtualBox Support Driver - Internal header.
*/
/*
* 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.
*/
#ifndef ___SUPDrvInternal_h
#define ___SUPDrvInternal_h
/*******************************************************************************
* Header Files *
*******************************************************************************/
#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>
# if 0
# define VBOX_HRTIMER
# endif
#elif defined(RT_OS_DARWIN)
#elif defined(RT_OS_FREEBSD)
#elif defined(RT_OS_SOLARIS)
#else
# error "unsupported OS."
#endif
#include "SUPDrvIOC.h"
#include "SUPDrvIDC.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)
/* debug printf */
# define OSDBGPRINT(a) printf a
/*
* Solaris
*/
#elif defined(RT_OS_SOLARIS)
# define OSDBGPRINT(a) SUPR0Printf 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.
*/
/** @todo retire the SUPDRV_ERR_* stuff, we ship err.h now. */
/** 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;
#ifdef VBOX_WITH_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_WITH_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). */
/** Locked memory (r3 mapping only) allocated by the support driver. */
/** 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
{
/** 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 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.
* 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
/**
* 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;
#ifdef VBOX_WITH_IDT_PATCHING
/** Patch usage records. (protected by SUPDRVDEVEXT::SpinLock) */
PSUPDRVPATCHUSAGE volatile pPatchUsage;
#endif
/** The VM associated with the session. */
/** List of generic usage records. (protected by SUPDRVDEVEXT::SpinLock) */
PSUPDRVUSAGE volatile pUsage;
/** 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. */
/** 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.
* This is NIL_RTR0PROCESS for kernel sessions and valid for user ones. */
#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 and objects. */
#ifdef VBOX_WITH_IDT_PATCHING
/** List of patches. */
PSUPDRVPATCH volatile pIdtPatches;
/** List of patches Free. */
PSUPDRVPATCH volatile pIdtPatchesFree;
#endif
/** List of registered objects. Protected by the spinlock. */
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;
/** VMMR0EntryInt() pointer. */
/** VMMR0EntryFast() pointer. */
/** VMMR0EntryEx() pointer. */
DECLR0CALLBACKMEMBER(int, pfnVMMR0EntryEx, (PVM pVM, unsigned uOperation, PSUPVMMR0REQHDR pReq, uint64_t u64Arg));
/** 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.)*/
/** 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;
} SUPDRVDEVEXT;
/*******************************************************************************
* OS Specific Functions *
*******************************************************************************/
bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc);
/*******************************************************************************
* Shared Functions *
*******************************************************************************/
int VBOXCALL supdrvIOCtl(uintptr_t uIOCtl, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPREQHDR pReqHdr);
int VBOXCALL supdrvIDC(uintptr_t uIOCtl, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVIDCREQHDR pReqHdr);
int VBOXCALL supdrvGipInit(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE pGip, RTHCPHYS HCPhys, uint64_t u64NanoTS, unsigned uUpdateHz);
#endif