SUPDrvInternal.h revision b3325f50334bd566860a0a324a137961966f9b33
57775f39a19dc392e5882d5217d5a9148688aa4aMatt Sweeney/* $Revision$ */
57775f39a19dc392e5882d5217d5a9148688aa4aMatt Sweeney/** @file
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai * VirtualBox Support Driver - Internal header.
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai */
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai/*
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai * Copyright (C) 2006-2007 Sun Microsystems, Inc.
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai *
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai * This file is part of VirtualBox Open Source Edition (OSE), as
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai * available from http://www.virtualbox.org. This file is free software;
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai * you can redistribute it and/or modify it under the terms of the GNU
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai * General Public License (GPL) as published by the Free Software
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai * Foundation, in version 2 as it comes in the "COPYING" file of the
eaa9719567e2ec55d96f9298077f5fc0effe1ed3Satyen Desai * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai *
86fa3fa071113dfe732100d1a15cb149f67ffc83Jenny Donnelly * The contents of this file may alternatively be used under the terms
86fa3fa071113dfe732100d1a15cb149f67ffc83Jenny Donnelly * of the Common Development and Distribution License Version 1.0
86fa3fa071113dfe732100d1a15cb149f67ffc83Jenny Donnelly * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
86fa3fa071113dfe732100d1a15cb149f67ffc83Jenny Donnelly * VirtualBox OSE distribution, in which case the provisions of the
86fa3fa071113dfe732100d1a15cb149f67ffc83Jenny Donnelly * CDDL are applicable instead of those of the GPL.
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai *
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai * You may elect to license modified versions of this file under the
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai * terms and conditions of either the GPL or the CDDL or both.
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai *
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai * Clara, CA 95054 USA or visit http://www.sun.com if you need
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai * additional information or have any questions.
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai */
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai#ifndef ___SUPDrvInternal_h
57775f39a19dc392e5882d5217d5a9148688aa4aMatt Sweeney#define ___SUPDrvInternal_h
57775f39a19dc392e5882d5217d5a9148688aa4aMatt Sweeney
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai
feaab31990f7893c59257f33cd9f708a6c0e7ddbSatyen Desai/*******************************************************************************
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai* Header Files *
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai*******************************************************************************/
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai#include <VBox/cdefs.h>
57775f39a19dc392e5882d5217d5a9148688aa4aMatt Sweeney#include <VBox/types.h>
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai#include <iprt/assert.h>
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai#include <iprt/asm.h>
57775f39a19dc392e5882d5217d5a9148688aa4aMatt Sweeney#include <VBox/sup.h>
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai#include <iprt/memobj.h>
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai#include <iprt/time.h>
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai#include <iprt/timer.h>
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai#include <iprt/string.h>
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai#include <iprt/err.h>
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai
eaa9719567e2ec55d96f9298077f5fc0effe1ed3Satyen Desai#if defined(RT_OS_WINDOWS)
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai __BEGIN_DECLS
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# if (_MSC_VER >= 1400) && !defined(VBOX_WITH_PATCHED_DDK)
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# define _InterlockedExchange _InterlockedExchange_StupidDDKVsCompilerCrap
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# define _InterlockedExchangeAdd _InterlockedExchangeAdd_StupidDDKVsCompilerCrap
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# define _InterlockedCompareExchange _InterlockedCompareExchange_StupidDDKVsCompilerCrap
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# define _InterlockedAddLargeStatistic _InterlockedAddLargeStatistic_StupidDDKVsCompilerCrap
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# define _interlockedbittestandset _interlockedbittestandset_StupidDDKVsCompilerCrap
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# define _interlockedbittestandreset _interlockedbittestandreset_StupidDDKVsCompilerCrap
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# define _interlockedbittestandset64 _interlockedbittestandset64_StupidDDKVsCompilerCrap
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# define _interlockedbittestandreset64 _interlockedbittestandreset64_StupidDDKVsCompilerCrap
eaa9719567e2ec55d96f9298077f5fc0effe1ed3Satyen Desai# pragma warning(disable : 4163)
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# include <ntddk.h>
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# pragma warning(default : 4163)
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# undef _InterlockedExchange
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# undef _InterlockedExchangeAdd
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# undef _InterlockedCompareExchange
57775f39a19dc392e5882d5217d5a9148688aa4aMatt Sweeney# undef _InterlockedAddLargeStatistic
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# undef _interlockedbittestandset
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# undef _interlockedbittestandreset
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# undef _interlockedbittestandset64
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# undef _interlockedbittestandreset64
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# else
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# include <ntddk.h>
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# endif
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# include <memory.h>
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# define memcmp(a,b,c) mymemcmp(a,b,c)
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai int VBOXCALL mymemcmp(const void *, const void *, size_t);
1233f432158f66c7692b2d4a9ad206d6ca27e4b6Matt Sweeney __END_DECLS
57775f39a19dc392e5882d5217d5a9148688aa4aMatt Sweeney
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai#elif defined(RT_OS_LINUX)
57775f39a19dc392e5882d5217d5a9148688aa4aMatt Sweeney# include <linux/autoconf.h>
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# include <linux/version.h>
eaa9719567e2ec55d96f9298077f5fc0effe1ed3Satyen Desai# if defined(CONFIG_MODVERSIONS) && !defined(MODVERSIONS)
57775f39a19dc392e5882d5217d5a9148688aa4aMatt Sweeney# define MODVERSIONS
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 71)
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# include <linux/modversions.h>
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai# endif
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# endif
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 0)
57775f39a19dc392e5882d5217d5a9148688aa4aMatt Sweeney# undef ALIGN
57775f39a19dc392e5882d5217d5a9148688aa4aMatt Sweeney# endif
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# ifndef KBUILD_STR
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# define KBUILD_STR(s) s
57775f39a19dc392e5882d5217d5a9148688aa4aMatt Sweeney# else
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# define KBUILD_STR(s) #s
57775f39a19dc392e5882d5217d5a9148688aa4aMatt Sweeney# endif
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# endif
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# include <linux/string.h>
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# include <linux/spinlock.h>
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# include <linux/slab.h>
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
57775f39a19dc392e5882d5217d5a9148688aa4aMatt Sweeney# include <linux/semaphore.h>
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# else /* older kernels */
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# include <asm/semaphore.h>
57775f39a19dc392e5882d5217d5a9148688aa4aMatt Sweeney# endif /* older kernels */
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# include <linux/timer.h>
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# if 0
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# include <linux/hrtimer.h>
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# define VBOX_HRTIMER
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# endif
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai#elif defined(RT_OS_DARWIN)
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# include <libkern/libkern.h>
982119a5f53b6531610b5d29c631550a6222da1fSatyen Desai# include <iprt/string.h>
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai#elif defined(RT_OS_OS2)
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
57775f39a19dc392e5882d5217d5a9148688aa4aMatt Sweeney#elif defined(RT_OS_FREEBSD)
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai# define memset libkern_memset /** @todo these are just hacks to get it compiling, check out later. */
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai# define memcmp libkern_memcmp
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# define strchr libkern_strchr
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai# define strrchr libkern_strrchr
57775f39a19dc392e5882d5217d5a9148688aa4aMatt Sweeney# define ffs libkern_ffs
57775f39a19dc392e5882d5217d5a9148688aa4aMatt Sweeney# define ffsl libkern_ffsl
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# define fls libkern_fls
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# define flsl libkern_flsl
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# include <sys/libkern.h>
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# undef memset
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# undef memcmp
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# undef strchr
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# undef strrchr
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# undef ffs
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# undef ffsl
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai# undef fls
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# undef flsl
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# include <iprt/string.h>
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai#elif defined(RT_OS_SOLARIS)
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai# include <sys/cmn_err.h>
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai# include <iprt/string.h>
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai#else
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai# error "unsupported OS."
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai#endif
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai#include "SUPDrvIOC.h"
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai#include "SUPDrvIDC.h"
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai/*******************************************************************************
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai* Defined Constants And Macros *
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai*******************************************************************************/
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai/*
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai * Hardcoded cookies.
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai */
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai#define BIRD 0x64726962 /* 'bird' */
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai#define BIRD_INV 0x62697264 /* 'drib' */
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai/*
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai * Win32
14039ac20172671b6c453a0b6571795429fbe3b3Satyen Desai */
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai#if defined(RT_OS_WINDOWS)
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai/* debug printf */
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai# define OSDBGPRINT(a) DbgPrint a
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai/** Maximum number of bytes we try to lock down in one go.
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai * This is supposed to have a limit right below 256MB, but this appears
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai * to actually be much lower. The values here have been determined experimentally.
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai */
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai#ifdef RT_ARCH_X86
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai# define MAX_LOCK_MEM_SIZE (32*1024*1024) /* 32mb */
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai#endif
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai#ifdef RT_ARCH_AMD64
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai# define MAX_LOCK_MEM_SIZE (24*1024*1024) /* 24mb */
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai#endif
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai/*
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai * Linux
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai */
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai#elif defined(RT_OS_LINUX)
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai/* check kernel version */
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai# error Unsupported kernel version!
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai#endif
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai__BEGIN_DECLS
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desaiint linux_dprintf(const char *format, ...);
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai__END_DECLS
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai/* debug printf */
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai# define OSDBGPRINT(a) printk a
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai/*
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai * Darwin
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai */
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai#elif defined(RT_OS_DARWIN)
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai/* debug printf */
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai# define OSDBGPRINT(a) printf a
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai/*
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai * OS/2
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai */
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai#elif defined(RT_OS_OS2)
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai/* No log API in OS/2 only COM port. */
75013e52e14f366e77d0a078bc1bfedfa421fe70Satyen Desai# define OSDBGPRINT(a) SUPR0Printf a
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai/*
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai * FreeBSD
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai */
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai#elif defined(RT_OS_FREEBSD)
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai/* debug printf */
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai# define OSDBGPRINT(a) printf a
69ce664b1e66d48ae3ae5be08488f38b4855fc8bSatyen Desai
57775f39a19dc392e5882d5217d5a9148688aa4aMatt Sweeney
57775f39a19dc392e5882d5217d5a9148688aa4aMatt Sweeney/*
aca18adfbc860a76fb2e4bee2f2e1d3b89c52b1bSatyen Desai * 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 */
#if (defined(DEBUG) && !defined(NO_LOGGING))
# ifdef LOG_TO_COM
# include <VBox/log.h>
# define dprintf(a) RTLogComPrintf a
# else
# define dprintf(a) OSDBGPRINT(a)
# endif
#else
# define dprintf(a) do {} while (0)
#endif
/* dprintf2 - extended logging. */
#if defined(RT_OS_DARWIN) || defined(RT_OS_OS2) || defined(RT_OS_FREEBSD)
# define dprintf2 dprintf
#else
# define dprintf2(a) do { } while (0)
#endif
/** @def RT_WITH_W64_UNWIND_HACK
* Changes a function name into the wrapped version if we've
* enabled the unwind hack.
*
* The unwind hack is for making the NT unwind procedures skip
* our dynamically loaded code when they try to walk the call
* stack. Needless to say, they kind of don't expect what
* we're doing here and get kind of confused and may BSOD. */
#ifdef DOXYGEN_RUNNING
# define RT_WITH_W64_UNWIND_HACK
#endif
/** @def UNWIND_WRAP
* If RT_WITH_W64_UNWIND_HACK is defined, the name will be prefixed with
* 'supdrvNtWrap'.
* @param Name The function to wrapper. */
#ifdef RT_WITH_W64_UNWIND_HACK
# define UNWIND_WRAP(Name) supdrvNtWrap##Name
#else
# define UNWIND_WRAP(Name) Name
#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;
/**
* Memory reference types.
*/
typedef enum
{
/** Unused entry */
MEMREF_TYPE_UNUSED = 0,
/** Locked memory (r3 mapping only). */
MEMREF_TYPE_LOCKED,
/** Continous memory block (r3 and r0 mapping). */
MEMREF_TYPE_CONT,
/** Low memory block (r3 and r0 mapping). */
MEMREF_TYPE_LOW,
/** Memory block (r3 and r0 mapping). */
MEMREF_TYPE_MEM,
/** Locked memory (r3 mapping only) allocated by the support driver. */
MEMREF_TYPE_PAGE,
/** Blow the type up to 32-bit and mark the end. */
MEMREG_TYPE_32BIT_HACK = 0x7fffffff
} SUPDRVMEMREFTYPE, *PSUPDRVMEMREFTYPE;
/**
* Structure used for tracking memory a session
* references in one way or another.
*/
typedef struct SUPDRVMEMREF
{
/** The memory object handle. */
RTR0MEMOBJ MemObj;
/** The ring-3 mapping memory object handle. */
RTR0MEMOBJ MapObjR3;
/** Type of memory. */
SUPDRVMEMREFTYPE eType;
} SUPDRVMEMREF, *PSUPDRVMEMREF;
/**
* Bundle of locked memory ranges.
*/
typedef struct SUPDRVBUNDLE
{
/** Pointer to the next bundle. */
struct SUPDRVBUNDLE * volatile pNext;
/** Referenced memory. */
SUPDRVMEMREF aMem[64];
/** Number of entries used. */
uint32_t volatile cUsed;
} SUPDRVBUNDLE, *PSUPDRVBUNDLE;
/**
* 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. */
PFNR0MODULEINIT pfnModuleInit;
/** Pointer to the optional module termination callback. */
PFNR0MODULETERM pfnModuleTerm;
/** Service request handler. This is NULL for non-service modules. */
PFNSUPR0SERVICEREQHANDLER pfnServiceReqHandler;
/** Size of the image. */
uint32_t cbImage;
/** The offset of the symbol table. */
uint32_t offSymbols;
/** The number of entries in the symbol table. */
uint32_t cSymbols;
/** The offset of the string table. */
uint32_t offStrTab;
/** Size of the string table. */
uint32_t cbStrTab;
/** The ldr image state. (IOCtl code of last opration.) */
uint32_t uState;
/** Usage count. */
uint32_t volatile cUsage;
/** Image name. */
char szName[32];
} SUPDRVLDRIMAGE, *PSUPDRVLDRIMAGE;
/** Image usage record. */
typedef struct SUPDRVLDRUSAGE
{
/** Next in chain. */
struct SUPDRVLDRUSAGE * volatile pNext;
/** The image. */
PSUPDRVLDRIMAGE pImage;
/** Load count. */
uint32_t volatile cUsage;
} SUPDRVLDRUSAGE, *PSUPDRVLDRUSAGE;
/**
* Component factory registration record.
*/
typedef struct SUPDRVFACTORYREG
{
/** Pointer to the next registration. */
struct SUPDRVFACTORYREG *pNext;
/** Pointer to the registered factory. */
PCSUPDRVFACTORY pFactory;
/** The session owning the factory.
* Used for deregistration and session cleanup. */
PSUPDRVSESSION pSession;
/** Length of the name. */
size_t cchName;
} SUPDRVFACTORYREG;
/** 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). */
uint32_t u32Magic;
/** The object type. */
SUPDRVOBJTYPE enmType;
/** Pointer to the next in the global list. */
struct SUPDRVOBJ * volatile pNext;
/** Pointer to the object destructor.
* This may be set to NULL if the image containing the destructor get unloaded. */
PFNSUPDRVDESTRUCTOR pfnDestructor;
/** User argument 1. */
void *pvUser1;
/** User argument 2. */
void *pvUser2;
/** The total sum of all per-session usage. */
uint32_t volatile cUsage;
/** The creator user id. */
RTUID CreatorUid;
/** The creator group id. */
RTGID CreatorGid;
/** The creator process id. */
RTPROCESS CreatorProcess;
} 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. */
PSUPDRVOBJ pObj;
/** The usage count. */
uint32_t volatile cUsage;
} SUPDRVUSAGE, *PSUPDRVUSAGE;
/**
* Per session data.
* This is mainly for memory tracking.
*/
typedef struct SUPDRVSESSION
{
/** Pointer to the device extension. */
PSUPDRVDEVEXT pDevExt;
/** Session Cookie. */
uint32_t u32Cookie;
/** Load usage records. (protected by SUPDRVDEVEXT::mtxLdr) */
PSUPDRVLDRUSAGE volatile pLdrUsage;
/** The VM associated with the session. */
PVM pVM;
/** List of generic usage records. (protected by SUPDRVDEVEXT::SpinLock) */
PSUPDRVUSAGE volatile pUsage;
/** Spinlock protecting the bundles and the GIP members. */
RTSPINLOCK Spinlock;
/** The ring-3 mapping of the GIP (readonly). */
RTR0MEMOBJ GipMapObjR3;
/** Set if the session is using the GIP. */
uint32_t fGipReferenced;
/** Bundle of locked memory objects. */
SUPDRVBUNDLE Bundle;
/** The user id of the session. (Set by the OS part.) */
RTUID Uid;
/** The group id of the session. (Set by the OS part.) */
RTGID Gid;
/** The process (id) of the session. */
RTPROCESS Process;
/** Which process this session is associated with.
* This is NIL_RTR0PROCESS for kernel sessions and valid for user ones. */
RTR0PROCESS R0Process;
#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. */
uint16_t sfn;
uint16_t Alignment; /**< Alignment */
#endif
#if defined(RT_OS_DARWIN) || defined(RT_OS_OS2) || defined(RT_OS_SOLARIS)
/** Pointer to the next session with the same hash. */
PSUPDRVSESSION pNextHash;
#endif
} SUPDRVSESSION;
/**
* Device extension.
*/
typedef struct SUPDRVDEVEXT
{
/** Spinlock to serialize the initialization,
* usage counting and destruction of the IDT entry override and objects. */
RTSPINLOCK Spinlock;
/** List of registered objects. Protected by the spinlock. */
PSUPDRVOBJ volatile pObjs;
/** List of free object usage records. */
PSUPDRVUSAGE volatile pUsageFree;
/** Global cookie. */
uint32_t u32Cookie;
/** The IDT entry number.
* Only valid if pIdtPatches is set. */
uint8_t volatile u8Idt;
/** Loader mutex.
* This protects pvVMMR0, pvVMMR0Entry, pImages and SUPDRVSESSION::pLdrUsage. */
RTSEMFASTMUTEX mtxLdr;
/** VMM Module 'handle'.
* 0 if the code VMM isn't loaded and Idt are nops. */
void * volatile pvVMMR0;
/** VMMR0EntryInt() pointer. */
DECLR0CALLBACKMEMBER(int, pfnVMMR0EntryInt, (PVM pVM, unsigned uOperation, void *pvArg));
/** VMMR0EntryFast() pointer. */
DECLR0CALLBACKMEMBER(void, pfnVMMR0EntryFast, (PVM pVM, unsigned idCpu, unsigned uOperation));
/** VMMR0EntryEx() pointer. */
DECLR0CALLBACKMEMBER(int, pfnVMMR0EntryEx, (PVM pVM, unsigned 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. */
RTSEMFASTMUTEX mtxGip;
/** Pointer to the Global Info Page (GIP). */
PSUPGLOBALINFOPAGE pGip;
/** The physical address of the GIP. */
RTHCPHYS HCPhysGip;
/** Number of processes using the GIP.
* (The updates are suspend while cGipUsers is 0.)*/
uint32_t volatile cGipUsers;
/** The ring-0 memory object handle for the GIP page. */
RTR0MEMOBJ GipMemObj;
/** The GIP timer handle. */
PRTTIMER pGipTimer;
/** If non-zero we've successfully called RTTimerRequestSystemGranularity(). */
uint32_t u32SystemTimerGranularityGrant;
/** The CPU id of the GIP master.
* This CPU is responsible for the updating the common GIP data. */
RTCPUID volatile idGipMaster;
#ifdef RT_OS_WINDOWS
/* Callback object returned by ExCreateCallback. */
PCALLBACK_OBJECT pObjPowerCallback;
/* Callback handle returned by ExRegisterCallback. */
PVOID hPowerCallback;
#endif
/** Component factory mutex.
* This protects pComponentFactoryHead and component factory querying. */
RTSEMFASTMUTEX mtxComponentFactory;
/** The head of the list of registered component factories. */
PSUPDRVFACTORYREG pComponentFactoryHead;
} SUPDRVDEVEXT;
__BEGIN_DECLS
/*******************************************************************************
* OS Specific Functions *
*******************************************************************************/
void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession);
bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc);
bool VBOXCALL supdrvOSGetForcedAsyncTscMode(PSUPDRVDEVEXT pDevExt);
int VBOXCALL supdrvOSEnableVTx(bool fEnabled);
/*******************************************************************************
* Shared Functions *
*******************************************************************************/
int VBOXCALL supdrvIOCtl(uintptr_t uIOCtl, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPREQHDR pReqHdr);
int VBOXCALL supdrvIOCtlFast(uintptr_t uIOCtl, unsigned idCpu, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession);
int VBOXCALL supdrvIDC(uintptr_t uIOCtl, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVIDCREQHDR pReqHdr);
int VBOXCALL supdrvInitDevExt(PSUPDRVDEVEXT pDevExt);
void VBOXCALL supdrvDeleteDevExt(PSUPDRVDEVEXT pDevExt);
int VBOXCALL supdrvCreateSession(PSUPDRVDEVEXT pDevExt, bool fUser, PSUPDRVSESSION *ppSession);
void VBOXCALL supdrvCloseSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession);
void VBOXCALL supdrvCleanupSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession);
int VBOXCALL supdrvGipInit(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE pGip, RTHCPHYS HCPhys, uint64_t u64NanoTS, unsigned uUpdateHz);
void VBOXCALL supdrvGipTerm(PSUPGLOBALINFOPAGE pGip);
void VBOXCALL supdrvGipUpdate(PSUPGLOBALINFOPAGE pGip, uint64_t u64NanoTS);
void VBOXCALL supdrvGipUpdatePerCpu(PSUPGLOBALINFOPAGE pGip, uint64_t u64NanoTS, unsigned iCpu);
bool VBOXCALL supdrvDetermineAsyncTsc(uint64_t *pu64DiffCores);
__END_DECLS
#endif