VirtualBox

Changeset 387 in vbox for trunk


Ignore:
Timestamp:
Jan 27, 2007 8:09:16 PM (18 years ago)
Author:
vboxsync
Message:

Use RTR0ProcHandleSelf / RTProcSelf. Implemented clientDied() cleanup on darwin.

Location:
trunk/src/VBox/HostDrivers/Support
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostDrivers/Support/Makefile

    r378 r387  
    5656        include/iprt/mem.h=$(PATH_ROOT)/include/iprt/mem.h \
    5757        include/iprt/param.h=$(PATH_ROOT)/include/iprt/param.h \
     58        include/iprt/process.h=$(PATH_ROOT)/include/iprt/process.h \
    5859        include/iprt/semaphore.h=$(PATH_ROOT)/include/iprt/semaphore.h \
    5960        include/iprt/spinlock.h=$(PATH_ROOT)/include/iprt/spinlock.h \
  • trunk/src/VBox/HostDrivers/Support/SUPDRV.h

    r378 r387  
    543543    /** The process (id) of the session. (Set by the OS part.) */
    544544    RTPROCESS                   Process;
     545    /** Which process this session is associated with. */
     546    RTR0PROCESS                 R0Process;
    545547#if defined(__DARWIN__) || defined(__OS2__)
    546548    /** Pointer to the next session with the same has. */
    547549    PSUPDRVSESSION              pNextHash;
    548 #endif
    549 #if defined(__DARWIN__)
    550     /** Pointer to the org_virtualbox_SupDrvClient client that's associated with the session. */
    551     void                       *pvClient;
    552550#endif
    553551} SUPDRVSESSION;
     
    669667int  VBOXCALL   supdrvCreateSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION *ppSession);
    670668void VBOXCALL   supdrvCloseSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession);
     669void VBOXCALL   supdrvCleanupSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession);
    671670int  VBOXCALL   supdrvGipInit(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE pGip, RTHCPHYS HCPhys, uint64_t u64NanoTS, unsigned uUpdateHz);
    672671void VBOXCALL   supdrvGipTerm(PSUPGLOBALINFOPAGE pGip);
  • trunk/src/VBox/HostDrivers/Support/SUPDRVShared.c

    r378 r387  
    327327void VBOXCALL supdrvCloseSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession)
    328328{
     329    /*
     330     * Cleanup the session first.
     331     */
     332    supdrvCleanupSession(pDevExt, pSession);
     333
     334    /*
     335     * Free the rest of the session stuff.
     336     */
     337    RTSpinlockDestroy(pSession->Spinlock);
     338    pSession->Spinlock = NIL_RTSPINLOCK;
     339    pSession->pDevExt = NULL;
     340    RTMemFree(pSession);
     341    dprintf2(("supdrvCloseSession: returns\n"));
     342}
     343
     344
     345/**
     346 * Shared code for cleaning up a session (but not quite freeing it).
     347 *
     348 * This is primarily intended for MAC OS X where we have to clean up the memory
     349 * stuff before the file handle is closed.
     350 *
     351 * @param   pDevExt     Device extension.
     352 * @param   pSession    Session data.
     353 *                      This data will be freed by this routine.
     354 */
     355void VBOXCALL supdrvCleanupSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession)
     356{
    329357    PSUPDRVBUNDLE       pBundle;
    330     dprintf(("supdrvCloseSession: pSession=%p\n", pSession));
     358    dprintf(("supdrvCleanupSession: pSession=%p\n", pSession));
    331359
    332360    /*
     
    429457                AssertRC(rc); /** @todo figure out how to handle this. */
    430458                pBundle->aMem[i].MemObj = NIL_RTR0MEMOBJ;
     459                pBundle->aMem[i].eType = MEMREF_TYPE_UNUSED;
    431460            }
    432461
     
    454483                        break;
    455484                }
     485                pBundle->aMem[i].eType = MEMREF_TYPE_UNUSED;
    456486            }
    457487#endif /* !USE_NEW_OS_INTERFACE */
     
    472502
    473503    /*
    474      * Loaded images needs to be dereferenced and possibly freed.
     504     * Loaded images needs to be dereferenced and possibly freed up.
    475505     */
    476506    RTSemFastMutexRequest(pDevExt->mtxLdr);
     
    513543    }
    514544    dprintf2(("umapping GIP - done\n"));
    515 
    516     /*
    517      * Free the rest of the session stuff.
    518      */
    519     RTSpinlockDestroy(pSession->Spinlock);
    520     pSession->Spinlock = NIL_RTSPINLOCK;
    521     pSession->pDevExt = NULL;
    522     RTMemFree(pSession);
    523     dprintf2(("supdrvCloseSession: returns\n"));
    524545}
    525546
  • trunk/src/VBox/HostDrivers/Support/darwin/SUPDrv-darwin.cpp

    r378 r387  
    3737#include <iprt/spinlock.h>
    3838#include <iprt/semaphore.h>
     39#include <iprt/process.h>
    3940#include <iprt/alloc.h>
    4041
     
    114115    virtual bool initWithTask(task_t OwningTask, void *pvSecurityId, UInt32 u32Type);
    115116    virtual bool start(IOService *pProvider);
    116     virtual void stop(IOService *pProvider);
    117117    virtual IOReturn clientClose(void);
    118118    virtual IOReturn clientDied(void);
    119119    virtual bool terminate(IOOptionBits fOptions = 0);
    120120    virtual bool finalize(IOOptionBits fOptions);
     121    virtual void stop(IOService *pProvider);
    121122};
    122123
     
    313314            pSession->Gid = pCred->cr_gid;
    314315        }
    315         pSession->Process = proc_pid(pProcess);
     316        pSession->Process = RTProcSelf();
     317        pSession->R0Process = RTR0ProcHandleSelf();
    316318
    317319        /*
     
    768770IOReturn org_virtualbox_SupDrvClient::clientDied(void)
    769771{
    770     dprintf(("org_virtualbox_SupDrvClient::clientDied([%p])\n", this));
    771 
    772     /* IOUserClient::clientDied() only calls calls close... */
     772    dprintf(("org_virtualbox_SupDrvClient::clientDied([%p]) m_Task=%p R0Process=%p Process=%d\n",
     773             this, m_Task, RTR0ProcHandleSelf(), RTProcSelf()));
     774
     775    /*
     776     * Do early session cleanup (if there is a session) so
     777     * we avoid hanging in vm_map_remove().
     778     */
     779    const RTR0PROCESS   R0Process = (RTR0PROCESS)m_Task;
     780    RTSPINLOCKTMP       Tmp = RTSPINLOCKTMP_INITIALIZER;
     781    RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
     782    for (unsigned i = 0; i < RT_ELEMENTS(g_apSessionHashTab); i++)
     783    {
     784        for (PSUPDRVSESSION pSession = g_apSessionHashTab[i]; pSession; pSession = pSession->pNextHash)
     785        {
     786            dprintf2(("pSession=%p R0Process=%p (=? %p)\n", pSession, pSession->R0Process, R0Process));
     787            if (pSession->R0Process == R0Process)
     788            {
     789                /*
     790                 * It is safe to leave the spinlock here; the session shouldn't be able
     791                 * to go away while we're cleaning it up, changes to pNextHash will not
     792                 * harm us, and new sessions can't possibly be added for this process.
     793                 */
     794                RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
     795                supdrvCleanupSession(&g_DevExt, pSession);
     796                RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
     797            }
     798        }
     799    }
     800    RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
     801
     802    /* IOUserClient::clientDied() calls close... */
    773803    return IOUserClient::clientDied();
     804}
     805
     806
     807/**
     808 * Terminate the service (initiate the destruction).
     809 */
     810bool org_virtualbox_SupDrvClient::terminate(IOOptionBits fOptions)
     811{
     812    dprintf(("org_virtualbox_SupDrvClient::terminate([%p], %#x)\n", this, fOptions));
     813    return IOUserClient::terminate(fOptions);
     814}
     815
     816
     817/**
     818 * The final stage of the client service destruction.
     819 */
     820bool org_virtualbox_SupDrvClient::finalize(IOOptionBits fOptions)
     821{
     822    dprintf(("org_virtualbox_SupDrvClient::finalize([%p], %#x)\n", this, fOptions));
     823    return IOUserClient::finalize(fOptions);
    774824}
    775825
     
    784834}
    785835
    786 
    787 /**
    788  * Terminate the service (initiate the destruction).
    789  */
    790 bool org_virtualbox_SupDrvClient::terminate(IOOptionBits fOptions)
    791 {
    792     dprintf(("org_virtualbox_SupDrvClient::terminate([%p], %#x)\n", this, fOptions));
    793     return IOUserClient::terminate(fOptions);
    794 }
    795 
    796 
    797 /**
    798  * The final stage of the client service destruction.
    799  */
    800 bool org_virtualbox_SupDrvClient::finalize(IOOptionBits fOptions)
    801 {
    802     dprintf(("org_virtualbox_SupDrvClient::finalize([%p], %#x)\n", this, fOptions));
    803     return IOUserClient::finalize(fOptions);
    804 }
    805 
  • trunk/src/VBox/HostDrivers/Support/darwin/SUPLib-darwin.cpp

    r1 r387  
    2525*   Header Files                                                               *
    2626*******************************************************************************/
     27#define LOG_GROUP LOG_GROUP_SUP
    2728#include <VBox/types.h>
    2829#include <VBox/sup.h>
    2930#include <VBox/param.h>
    3031#include <VBox/err.h>
     32#include <VBox/log.h>
    3133#include <iprt/path.h>
    3234#include <iprt/assert.h>
     
    4143#include <unistd.h>
    4244#include <stdlib.h>
     45#include <mach/mach_port.h>
     46#include <IOKit/IOKitLib.h>
    4347
    4448
     
    4852/** BSD Device name. */
    4953#define DEVICE_NAME     "/dev/vboxdrv"
     54/** The IOClass key of the service (see SUPDrv-darwin.cpp / Info.plist). */
     55#define IOCLASS_NAME    "org_virtualbox_SupDrv"
    5056
    5157
     
    5460*******************************************************************************/
    5561/** Handle to the open device. */
    56 static int      g_hDevice = -1;
     62static int              g_hDevice = -1;
     63/** The IOMasterPort. */
     64static mach_port_t      g_MasterPort = 0;
     65/** The current service connection. */
     66static io_connect_t     g_Connection = NULL;
    5767
    5868
     
    6676
    6777    /*
    68      * Try open the device.
     78     * Open the IOKit client first - The first step is finding the service.
     79     */
     80    mach_port_t MasterPort;
     81    kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &MasterPort);
     82    if (kr != kIOReturnSuccess)
     83    {
     84        LogRel(("IOMasterPort -> %d\n", kr));
     85        return VERR_GENERAL_FAILURE;
     86    }
     87
     88    CFDictionaryRef ClassToMatch = IOServiceMatching(IOCLASS_NAME);
     89    if (!ClassToMatch)
     90    {
     91        LogRel(("IOServiceMatching(\"%s\") failed.\n", IOCLASS_NAME));
     92        return VERR_GENERAL_FAILURE;
     93    }
     94
     95    /* Create an io_iterator_t for all instances of our drivers class that exist in the IORegistry. */
     96    io_iterator_t Iterator;
     97    kr = IOServiceGetMatchingServices(g_MasterPort, ClassToMatch, &Iterator);
     98    if (kr != kIOReturnSuccess)
     99    {
     100        LogRel(("IOServiceGetMatchingServices returned %d\n", kr));
     101        return VERR_GENERAL_FAILURE;
     102    }
     103
     104    /* Get the first item in the iterator and release it. */
     105    io_service_t ServiceObject = IOIteratorNext(Iterator);
     106    IOObjectRelease(Iterator);
     107    if (!ServiceObject)
     108    {
     109        LogRel(("Couldn't find any matches.\n"));
     110        return VERR_GENERAL_FAILURE;
     111    }
     112
     113    /*
     114     * Open the service.
     115     * This will cause the user client class in SUPDrv-darwin.cpp to be instantiated.
     116     */
     117    kr = IOServiceOpen(ServiceObject, mach_task_self(), 0, &g_Connection);
     118    IOObjectRelease(ServiceObject);
     119    if (kr != kIOReturnSuccess)
     120    {
     121        LogRel(("IOServiceOpen returned %d\n", kr));
     122        return VERR_GENERAL_FAILURE;
     123    }
     124
     125    /*
     126     * Now, try open the BSD device.
    69127     */
    70128    g_hDevice = open(DEVICE_NAME, O_RDWR, 0);
    71129    if (g_hDevice < 0)
    72130    {
    73         /*
    74          * Try load the device.
    75          */
    76         //todo suplibOsLoadKernelModule();
    77         g_hDevice = open(DEVICE_NAME, O_RDWR, 0);
    78         if (g_hDevice < 0)
    79             return RTErrConvertFromErrno(errno);
     131        int rc = errno;
     132        LogRel(("Failed to open \"%s\", errno=%d\n", rc));
     133        kr = IOServiceClose(g_Connection);
     134        if (kr != kIOReturnSuccess)
     135            LogRel(("Warning: IOServiceClose(%p) returned %d\n", g_Connection, kr));
     136        return RTErrConvertFromErrno(rc);
    80137    }
    81138
     
    98155            AssertFailed();
    99156        g_hDevice = -1;
     157    }
     158
     159    /*
     160     * Close the connection to the IOService and destroy the connection handle.
     161     */
     162    if (g_Connection)
     163    {
     164        kern_return_t kr = IOServiceClose(g_Connection);
     165        if (kr != kIOReturnSuccess)
     166        {
     167            LogRel(("Warning: IOServiceClose(%p) returned %d\n", g_Connection, kr));
     168            AssertFailed();
     169        }
     170        g_Connection = NULL;
    100171    }
    101172
  • trunk/src/VBox/HostDrivers/Support/linux/SUPDrv-linux.c

    r331 r387  
    2727#include <iprt/semaphore.h>
    2828#include <iprt/initterm.h>
     29#include <iprt/process.h>
    2930#include <iprt/err.h>
    3031#include <iprt/mem.h>
     
    221222
    222223#ifdef __AMD64__
    223 /** 
     224/**
    224225 * Memory for the executable memory heap (in IPRT).
    225226 */
     
    233234        ".size g_abExecMemory, 1572864\n\t"
    234235        ".text\n\t");
    235 #endif 
     236#endif
    236237
    237238
     
    363364nmi_activated:
    364365#  endif
    365                 printk(KERN_ERR DEVICE_NAME 
     366                printk(KERN_ERR DEVICE_NAME
    366367                ": NMI watchdog active -- refused to load the kernel module! Please disable\n"
    367                                 DEVICE_NAME 
     368                                DEVICE_NAME
    368369                ": the NMI watchdog by specifying 'nmi_watchdog=0' at kernel command line.\n");
    369370                return -EINVAL;
     
    534535        pSession->Uid       = current->euid;
    535536        pSession->Gid       = current->egid;
    536         pSession->Process   = (RTPROCESS)current->tgid;
     537        pSession->Process   = RTProcSelf();
     538        pSession->R0Process = RTR0ProcHandleSelf();
    537539    }
    538540
  • trunk/src/VBox/HostDrivers/Support/win32/SUPDrv-win32.cpp

    r1 r387  
    223223        pSession->Uid       = NIL_RTUID;
    224224        pSession->Gid       = NIL_RTGID;
    225         pSession->Process   = NIL_RTPROCESS;
     225        pSession->Process   = RTProcSelf();
     226        pSession->R0Process = RTR0ProcHandleSelf();
    226227        pFileObj->FsContext = pSession;
    227228    }
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette