SUPDrv-darwin.cpp revision f4db180328f833f9fc9cb07a1a4a0bc948a47afe
/** @file
* VBox host drivers - Ring-0 support drivers - Darwin host:
* Darwin driver C code
*/
/*
* Copyright (C) 2006 InnoTek Systemberatung 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
/* Deal with conflicts first.
* (This is mess inherited from BSD. The *BSDs has clean this up long ago.) */
#include "SUPDRV.h"
#include <iprt/initterm.h>
#include <iprt/spinlock.h>
#include <iprt/semaphore.h>
#include <IOKit/IOService.h>
#include <IOKit/IOUserclient.h>
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** The module name. */
#define DEVICE_NAME "vboxdrv"
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static int VBoxSupDrvIOCtl(dev_t Dev, u_long iCmd, caddr_t pData, int fFlags, struct proc *pProcess);
static int VBoxSupDrvIOCtlSlow(PSUPDRVSESSION pSession, u_long iCmd, caddr_t pData, struct proc *pProcess);
static int VBoxSupDrvErr2DarwinErr(int rc);
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* The service class.
* This is just a formality really.
*/
class org_virtualbox_SupDrv : public IOService
{
public:
virtual void free(void);
};
/**
* An attempt at getting that clientDied() notification.
* port right.
*/
class org_virtualbox_SupDrvClient : public IOUserClient
{
private:
public:
virtual IOReturn clientClose(void);
virtual IOReturn clientDied(void);
};
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/**
* Declare the module stuff.
*/
int _kext_apple_cc = __APPLE_CC__;
/**
* Device extention & session data association structure.
*/
static SUPDRVDEVEXT g_DevExt;
/**
* The character device switch table for the driver.
*/
{
/** @todo g++ doesn't like this syntax - it worked with gcc before renaming to .cpp. */
/*.d_open = */VBoxSupDrvOpen,
/*.d_close = */VBoxSupDrvClose,
/*.d_read = */eno_rdwrt,
/*.d_write = */eno_rdwrt,
/*.d_ioctl = */VBoxSupDrvIOCtl,
/*.d_stop = */eno_stop,
/*.d_reset = */eno_reset,
/*.d_ttys = */NULL,
/*.d_select= */eno_select,
/*.d_mmap = */eno_mmap,
/*.d_strategy = */eno_strat,
/*.d_getc = */eno_getc,
/*.d_putc = */eno_putc,
/*.d_type = */0
};
/** Major device number. */
static int g_iMajorDeviceNo = -1;
/** Registered devfs device handle. */
static void *g_hDevFsDevice = NULL;
/** Spinlock protecting g_apSessionHashTab. */
/** Hash table */
/** Calculates the index into g_apSessionHashTab.*/
/**
* Start the kernel module.
*/
{
int rc;
dprintf(("VBoxSupDrvStart\n"));
/*
* Initialize IPRT.
*/
if (RT_SUCCESS(rc))
{
/*
* Initialize the device extension.
*/
if (RT_SUCCESS(rc))
{
/*
* Initialize the session hash table.
*/
if (RT_SUCCESS(rc))
{
/*
* Registering ourselves as a character device.
*/
if (g_iMajorDeviceNo >= 0)
{
if (g_hDevFsDevice)
{
return KMOD_RETURN_SUCCESS;
}
OSDBGPRINT(("VBoxDrv: devfs_make_node(makedev(%d,0),,,,%s) failed\n",
g_iMajorDeviceNo = -1;
}
else
}
else
}
else
RTR0Term();
}
else
return KMOD_RETURN_FAILURE;
}
/**
* Stop the kernel module.
*/
{
int rc;
dprintf(("VBoxSupDrvStop\n"));
/** @todo I've got a nagging feeling that we'll have to keep track of users and refuse
* unloading if we're busy. Investigate and implement this! */
/*
* Undo the work done during start (in reverse order).
*/
g_iMajorDeviceNo = -1;
RTR0Term();
dprintf(("VBoxSupDrvStop - done\n"));
return KMOD_RETURN_SUCCESS;
}
/**
*
* @param pInode Pointer to inode info structure.
* @param pFilp Associated file pointer.
*/
{
int rc;
dprintf(("VBoxSupDrvOpen:\n"));
/*
* Create a new session.
*/
if (RT_SUCCESS(rc))
{
unsigned iHash;
if (pCred)
{
}
/*
* Insert it into the hash table.
*/
}
dprintf(("VBoxSupDrvOpen: g_DevExt=%p pSession=%p rc=%d pid=%d\n", &g_DevExt, pSession, rc, proc_pid(pProcess)));
return VBoxSupDrvErr2DarwinErr(rc);
}
/**
* Close device.
*
*/
{
/*
* Remove from the hash table.
*/
if (pSession)
{
{
}
else
{
while (pSession)
{
{
break;
}
/* next */
}
}
}
if (!pSession)
{
OSDBGPRINT(("VBoxSupDrvIoctl: WHUT?!? pSession == NULL! This must be a mistake... pid=%d\n", (int)Process));
return EINVAL;
}
/*
* Close the session.
*/
return 0;
}
/**
* Device I/O Control entry point.
*
* @returns Darwin for slow IOCtls and VBox status code for the fast ones.
* @param Dev The device number (major+minor).
* @param iCmd The IOCtl command.
* @param pData Pointer to the data (if any it's a SUPDRVIOCTLDATA (kernel copy)).
* @param fFlags Flag saying we're a character device (like we didn't know already).
* @param pProcess The process issuing this request.
*/
static int VBoxSupDrvIOCtl(dev_t Dev, u_long iCmd, caddr_t pData, int fFlags, struct proc *pProcess)
{
/*
* Find the session.
*/
{
}
if (!pSession)
{
OSDBGPRINT(("VBoxSupDrvIoctl: WHUT?!? pSession == NULL! This must be a mistake... pid=%d\n", (int)Process));
return EINVAL;
}
/*
* 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 ( iCmd == SUP_IOCTL_FAST_DO_RAW_RUN
|| iCmd == SUP_IOCTL_FAST_DO_NOP)
}
/**
* Worker for VBoxSupDrvIOCtl that takes the slow IOCtl functions.
*
* @returns Darwin errno.
*
* @param pSession The session.
* @param iCmd The IOCtl command.
* @param pData Pointer to the kernel copy of the SUPDRVIOCTLDATA buffer.
* @param pProcess The calling process.
*/
static int VBoxSupDrvIOCtlSlow(PSUPDRVSESSION pSession, u_long iCmd, caddr_t pData, struct proc *pProcess)
{
int rc;
unsigned long cbBuf = 0;
unsigned cbOut = 0;
dprintf(("VBoxSupDrvIOCtl: pSession=%p iCmd=%p pData=%p pProcess=%p\n", pSession, iCmd, pData, pProcess));
/*
* Copy ioctl data structure from user space.
*/
{
return EINVAL;
}
/*
* Allocate and copy user space input data buffer to kernel space.
*/
{
{
return ENOMEM;
}
if (rc)
{
if (pvPageBuf)
else
return rc;
}
}
/*
* Process the IOCtl.
*/
/*
* Copy ioctl data and output buffer back to user space.
*/
if (rc)
{
dprintf(("VBoxSupDrvIOCtl: pid=%d iCmd=%x pData=%p failed, rc=%d (darwin rc=%d)\n",
}
else if (cbOut > 0)
{
{
if (rc2)
{
}
}
else
{
}
}
if (pvPageBuf)
else if (pvBuf)
return rc;
}
/**
* 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;
}
/**
* Converts a supdrv error code to a darwin error code.
*
* @returns corresponding darwin error code.
* @param rc supdrv error code (SUPDRV_ERR_* defines).
*/
static int VBoxSupDrvErr2DarwinErr(int rc)
{
switch (rc)
{
case 0: return 0;
case SUPDRV_ERR_GENERAL_FAILURE: return EACCES;
case SUPDRV_ERR_INVALID_PARAM: return EINVAL;
case SUPDRV_ERR_INVALID_MAGIC: return EILSEQ;
case SUPDRV_ERR_INVALID_HANDLE: return ENXIO;
case SUPDRV_ERR_INVALID_POINTER: return EFAULT;
case SUPDRV_ERR_LOCK_FAILED: return ENOLCK;
case SUPDRV_ERR_ALREADY_LOADED: return EEXIST;
case SUPDRV_ERR_PERMISSION_DENIED: return EPERM;
case SUPDRV_ERR_VERSION_MISMATCH: return ENOSYS;
}
return EPERM;
}
/** @todo move this to assembly where a simple "jmp printf" will to the trick. */
{
char szMsg[512];
return 0;
}
/** Runtime assert implementation for Darwin Ring-0. */
RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
{
printf("!!Assertion Failed!!\n"
"Expression: %s\n"
"Location : %s(%d) %s\n",
}
/** Runtime assert implementation for the Darwin Ring-0 driver.
* @todo this one needs fixing! */
{ /* forwarder. */
char msg[256];
}
/*
*
* org_virtualbox_SupDrv
*
*/
/**
* Initialize the object.
*/
{
{
/* init members. */
return true;
}
return false;
}
/**
* Free the object.
*/
void org_virtualbox_SupDrv::free(void)
{
dprintf(("IOService::free([%p])\n", this));
}
/**
* Check if it's ok to start this service.
* It's always ok by us, so it's up to IOService to decide really.
*/
{
dprintf(("org_virtualbox_SupDrv::probe([%p])\n", this));
}
/**
* Start this service.
*/
{
dprintf(("org_virtualbox_SupDrv::start([%p])\n", this));
{
/* register the service. */
return true;
}
return false;
}
/**
* Stop this service.
*/
{
}
/*
*
* org_virtualbox_SupDrvClient
*
*/
/**
* Initializer called when the client opens the service.
*/
bool org_virtualbox_SupDrvClient::initWithTask(task_t OwningTask, void *pvSecurityId, UInt32 u32Type)
{
dprintf(("org_virtualbox_SupDrvClient::initWithTask([%p], %#x, %p, %#x)\n", this, OwningTask, pvSecurityId, u32Type));
if (!OwningTask)
return false;
{
m_Task = OwningTask;
m_pSession = NULL;
m_pProvider = NULL;
return true;
}
return false;
}
/**
* Start the client service.
*/
{
{
if (m_pProvider)
{
/* this is where we could create the section. */
return true;
}
}
return false;
}
/**
* Client exits normally.
*/
{
dprintf(("org_virtualbox_SupDrvClient::clientClose([%p])\n", this));
m_pProvider = NULL;
terminate();
return kIOReturnSuccess;
}
/**
* The client exits abnormally / forgets to do cleanups.
*/
{
dprintf(("org_virtualbox_SupDrvClient::clientDied([%p]) m_Task=%p R0Process=%p Process=%d\n",
/*
* Do early session cleanup (if there is a session) so
* we avoid hanging in vm_map_remove().
*/
for (unsigned i = 0; i < RT_ELEMENTS(g_apSessionHashTab); i++)
{
{
{
/*
* It is safe to leave the spinlock here; the session shouldn't be able
* to go away while we're cleaning it up, changes to pNextHash will not
* harm us, and new sessions can't possibly be added for this process.
*/
}
}
}
/* IOUserClient::clientDied() calls close... */
return IOUserClient::clientDied();
}
/**
* Terminate the service (initiate the destruction).
*/
{
}
/**
* The final stage of the client service destruction.
*/
{
}
/**
* Stop the client service.
*/
{
dprintf(("org_virtualbox_SupDrvClient::stop([%p])\n", this));
}