VirtualBox

Changeset 8188 in vbox for trunk/src


Ignore:
Timestamp:
Apr 19, 2008 8:27:39 PM (17 years ago)
Author:
vboxsync
Message:

Additions/FreeBSD: Cleanup, rename variables to follow coding guidlines, removed session hash table and use device cloning instead

Location:
trunk/src/VBox/Additions
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/common/VBoxGuest/VBoxGuest-freebsd.c

    r8155 r8188  
    4949#include <iprt/process.h>
    5050#include <iprt/mem.h>
     51#include <iprt/asm.h>
    5152
    5253/** The module name. */
    53 #define DEVICE_NAME              "vboxguest"
     54#define DEVICE_NAME    "vboxguest"
    5455
    5556struct VBoxGuestDeviceState
    5657{
    5758    /** file node minor code */
    58     unsigned          minor;
    59     /** first IO port */
    60     int                io_port_resid;
    61     struct resource   *io_port;
    62     bus_space_tag_t    io_port_tag;
    63     bus_space_handle_t io_port_handle;
    64     /** IO Port. */
     59    unsigned           uMinor;
     60    /** Resource ID of the I/O port */
     61    int                iIOPortResId;
     62    /** Pointer to the I/O port resource. */
     63    struct resource   *pIOPortRes;
     64    /** Start address of the IO Port. */
    6565    uint16_t           uIOPortBase;
    66     /** device memory resources */
    67     int                vmmdevmem_resid;
    68     struct resource   *vmmdevmem;
    69     bus_space_tag_t    vmmdevmem_tag;
    70     bus_space_handle_t vmmdevmem_handle;
    71     bus_size_t         vmmdevmem_size;
    72     /** physical address of adapter memory */
    73     uint32_t           vmmdevmem_addr;
     66    /** Resource ID of the MMIO area */
     67    int                iVMMDevMemResId;
     68    /** Pointer to the MMIO resource. */
     69    struct resource   *pVMMDevMemRes;
     70    /** Handle of the MMIO resource. */
     71    bus_space_handle_t VMMDevMemHandle;
     72    /** Size of the memory area. */
     73    bus_size_t         VMMDevMemSize;
    7474    /** Mapping of the register space */
    7575    void              *pMMIOBase;
    7676    /** IRQ number */
    77     int                irq_resid;
    78     struct resource   *irq;
    79     void              *irq_handler;
     77    int                iIrqResId;
     78    /** IRQ resource handle. */
     79    struct resource   *pIrqRes;
     80    /** Pointer to the IRQ handler. */
     81    void              *pfnIrqHandler;
    8082    /** VMMDev version */
    8183    uint32_t           u32Version;
     
    8486static MALLOC_DEFINE(M_VBOXDEV, "vboxdev_pci", "VirtualBox Guest driver PCI");
    8587
    86 #if FreeBSD >= 700
    87 static int VBoxGuestFreeBSDOpen(struct cdev *dev, int flags, int fmt, struct thread *td, struct file *pFd);
    88 #else
    89 static int VBoxGuestFreeBSDOpen(struct cdev *dev, int flags, int fmt, struct thread *td);
    90 #endif
    91 static int VBoxGuestFreeBSDClose(struct cdev *dev, int flags, int fmt, struct thread *td);
    92 static int VBoxGuestFreeBSDIOCtl(struct cdev *dev, u_long cmd, caddr_t data,
    93                                  int fflag, struct thread *td);
    94 static ssize_t VBoxGuestFreeBSDWrite (struct cdev *dev, struct uio *uio, int ioflag);
    95 static ssize_t VBoxGuestFreeBSDRead (struct cdev *dev, struct uio *uio, int ioflag);
    96 static void VBoxGuestFreeBSDRemoveIRQ(device_t self, void *pvState);
    97 static int VBoxGuestFreeBSDAddIRQ(device_t self, void *pvState);
    98 int VBoxGuestFreeBSDISR(void *pvState);
    99 DECLVBGL(int) VBoxGuestFreeBSDServiceCall(void *pvSession, unsigned iCmd, void *pvData, size_t cbData, size_t *pcbDataReturned);
     88/*
     89 * Character device file handlers.
     90 */
     91static d_fdopen_t VBoxGuestFreeBSDOpen;
     92static d_close_t  VBoxGuestFreeBSDClose;
     93static d_ioctl_t  VBoxGuestFreeBSDIOCtl;
     94static d_write_t  VBoxGuestFreeBSDWrite;
     95static d_read_t   VBoxGuestFreeBSDRead;
     96
     97/*
     98 * IRQ related functions.
     99 */
     100static void VBoxGuestFreeBSDRemoveIRQ(device_t pDevice, void *pvState);
     101static int  VBoxGuestFreeBSDAddIRQ(device_t pDevice, void *pvState);
     102static int  VBoxGuestFreeBSDISR(void *pvState);
     103
     104/*
     105 * Available functions for kernel drivers.
     106 */
     107DECLVBGL(int)    VBoxGuestFreeBSDServiceCall(void *pvSession, unsigned uCmd, void *pvData, size_t cbData, size_t *pcbDataReturned);
    100108DECLVBGL(void *) VBoxGuestFreeBSDServiceOpen(uint32_t *pu32Version);
    101 DECLVBGL(int) VBoxGuestFreeBSDServiceClose(void *pvSession);
     109DECLVBGL(int)    VBoxGuestFreeBSDServiceClose(void *pvSession);
    102110
    103111/*
    104112 * Device node entry points.
    105113 */
    106 static struct cdevsw    g_VBoxAddFreeBSDChrDevSW =
     114static struct cdevsw    g_VBoxGuestFreeBSDChrDevSW =
    107115{
    108116    .d_version =        D_VERSION,
    109117    .d_flags =          D_TRACKCLOSE,
    110     .d_open =           VBoxGuestFreeBSDOpen,
     118    .d_fdopen =         VBoxGuestFreeBSDOpen,
    111119    .d_close =          VBoxGuestFreeBSDClose,
    112120    .d_ioctl =          VBoxGuestFreeBSDIOCtl,
     
    116124};
    117125
    118 /** The make_dev result. */
    119 static struct cdev         *g_pVBoxAddFreeBSDChrDev;
    120126/** Device extention & session data association structure. */
    121127static VBOXGUESTDEVEXT      g_DevExt;
    122 /** Spinlock protecting g_apSessionHashTab. */
    123 static RTSPINLOCK           g_Spinlock = NIL_RTSPINLOCK;
    124 /** Hash table */
    125 static PVBOXGUESTSESSION    g_apSessionHashTab[19];
    126 /** Calculates the index into g_apSessionHashTab.*/
    127 #define SESSION_HASH(sfn) ((sfn) % RT_ELEMENTS(g_apSessionHashTab))
    128 
    129 /** @todo r=bird: fork() and session hash table didn't work well for solaris, so I doubt it works for
    130  * FreeBSD... A different solution is needed unless the current can be improved. */
     128/** List of cloned device. Managed by the kernel. */
     129static struct clonedevs    *g_pVBoxGuestFreeBSDClones;
     130/** The dev_clone event handler tag. */
     131static eventhandler_tag     g_VBoxGuestFreeBSDEHTag;
    131132
    132133/**
     
    135136 * @returns VBox error code.
    136137 * @param   pvSession           Opaque pointer to the session.
    137  * @param   iCmd                Requested function.
     138 * @param   uCmd                Requested function.
    138139 * @param   pvData              IO data buffer.
    139140 * @param   cbData              Size of the data buffer.
    140141 * @param   pcbDataReturned     Where to store the amount of returned data.
    141142 */
    142 DECLVBGL(int) VBoxGuestFreeBSDServiceCall(void *pvSession, unsigned iCmd, void *pvData, size_t cbData, size_t *pcbDataReturned)
    143 {
    144     LogFlow((DEVICE_NAME ":VBoxGuestSolarisServiceCall %pvSesssion=%p Cmd=%u pvData=%p cbData=%d\n", pvSession, iCmd, pvData, cbData));
     143DECLVBGL(int) VBoxGuestFreeBSDServiceCall(void *pvSession, unsigned uCmd, void *pvData, size_t cbData, size_t *pcbDataReturned)
     144{
     145    LogFlow((DEVICE_NAME ":VBoxGuestFreeBSDServiceCall %pvSesssion=%p uCmd=%u pvData=%p cbData=%d\n", pvSession, uCmd, pvData, cbData));
    145146
    146147    PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pvSession;
     
    149150                    ("SC: %p != %p\n", pSession->pDevExt, &g_DevExt), VERR_INVALID_HANDLE);
    150151
    151     return VBoxGuestCommonIOCtl(iCmd, &g_DevExt, pSession, pvData, cbData, pcbDataReturned);
     152    return VBoxGuestCommonIOCtl(uCmd, &g_DevExt, pSession, pvData, cbData, pcbDataReturned);
    152153}
    153154
     
    161162DECLVBGL(void *) VBoxGuestFreeBSDServiceOpen(uint32_t *pu32Version)
    162163{
    163     LogFlow((DEVICE_NAME ":VBoxGuestSolarisServiceOpen\n"));
     164    LogFlow((DEVICE_NAME ":VBoxGuestFreeBSDServiceOpen\n"));
    164165
    165166    AssertPtrReturn(pu32Version, NULL);
     
    184185DECLVBGL(int) VBoxGuestFreeBSDServiceClose(void *pvSession)
    185186{
    186     LogFlow((DEVICE_NAME ":VBoxGuestSolarisServiceClose\n"));
     187    LogFlow((DEVICE_NAME ":VBoxGuestFreeBSDServiceClose\n"));
    187188
    188189    PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pvSession;
     
    198199
    199200/**
     201 * DEVFS event handler.
     202 */
     203static void VBoxGuestFreeBSDClone(void *pvArg, struct ucred *pCred, char *pszName, int cchName, struct cdev **ppDev)
     204{
     205    int iUnit;
     206    int rc;
     207
     208    Log(("VBoxGuestFreeBSDClone: pszName=%s ppDev=%p\n", pszName, ppDev));
     209
     210    /*
     211     * One device node per user, si_drv1 points to the session.
     212     * /dev/vboxguest<N> where N = {0...255}.
     213     */
     214    if (!ppDev)
     215        return;
     216    if (dev_stdclone(pszName, NULL, "vboxguest", &iUnit) != 1)
     217        return;
     218    if (iUnit >= 256 || iUnit < 0)
     219    {
     220        Log(("VBoxGuestFreeBSDClone: iUnit=%d >= 256 - rejected\n", iUnit));
     221        return;
     222    }
     223
     224    Log(("VBoxGuestFreeBSDClone: pszName=%s iUnit=%d\n", pszName, iUnit));
     225
     226    rc = clone_create(&g_pVBoxGuestFreeBSDClones, &g_VBoxGuestFreeBSDChrDevSW, &iUnit, ppDev, 0);
     227    Log(("VBoxGuestFreeBSDClone: clone_create -> %d; iUnit=%d\n", rc, iUnit));
     228    if (rc)
     229    {
     230        *ppDev = make_dev(&g_VBoxGuestFreeBSDChrDevSW,
     231                          unit2minor(iUnit),
     232                          UID_ROOT,
     233                          GID_WHEEL,
     234                          0644,
     235                          "vboxguest%d", iUnit);
     236        if (*ppDev)
     237        {
     238            dev_ref(*ppDev);
     239            (*ppDev)->si_flags |= SI_CHEAPCLONE;
     240            Log(("VBoxGuestFreeBSDClone: Created *ppDev=%p iUnit=%d si_drv1=%p si_drv2=%p\n",
     241                     *ppDev, iUnit, (*ppDev)->si_drv1, (*ppDev)->si_drv2));
     242            (*ppDev)->si_drv1 = (*ppDev)->si_drv2 = NULL;
     243        }
     244        else
     245            Log(("VBoxGuestFreeBSDClone: make_dev iUnit=%d failed\n", iUnit));
     246    }
     247    else
     248        Log(("VBoxGuestFreeBSDClone: Existing *ppDev=%p iUnit=%d si_drv1=%p si_drv2=%p\n",
     249             *ppDev, iUnit, (*ppDev)->si_drv1, (*ppDev)->si_drv2));
     250}
     251
     252/**
    200253 * File open handler
    201254 *
    202255 */
    203 #if FreeBSD >= 700
    204 static int VBoxGuestFreeBSDOpen(struct cdev *dev, int flags, int fmt, struct thread *td, struct file *pFd)
     256#if __FreeBSD_version >= 700000
     257static int VBoxGuestFreeBSDOpen(struct cdev *pDev, int fOpen, struct thread *pTd, struct file *pFd)
    205258#else
    206 static int VBoxGuestFreeBSDOpen(struct cdev *dev, int flags, int fmt, struct thread *td)
     259static int VBoxGuestFreeBSDOpen(struct cdev *pDev, int fOpen, struct thread *pTd)
    207260#endif
    208261{
     
    213266
    214267    /*
     268     * Try grab it (we don't grab the giant, remember).
     269     */
     270    if (!ASMAtomicCmpXchgPtr(&pDev->si_drv1, (void *)0x42, NULL))
     271        return EBUSY;
     272
     273    /*
    215274     * Create a new session.
    216275     */
     
    218277    if (RT_SUCCESS(rc))
    219278    {
    220         /*
    221          * Insert it into the hash table.
    222          */
    223         unsigned iHash = SESSION_HASH(pSession->Process);
    224         RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
    225         RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
    226         pSession->pNextHash = g_apSessionHashTab[iHash];
    227         g_apSessionHashTab[iHash] = pSession;
    228         RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
    229 
    230         Log((DEVICE_NAME ":VBoxGuestFreeBSDOpen success: g_DevExt=%p pSession=%p rc=%d pid=%d\n", &g_DevExt, pSession, rc, (int)RTProcSelf()));
    231         return 0;
    232     }
    233 
    234     LogRel((DEVICE_NAME ":VBoxGuestFreeBSDOpen: VBoxGuestCreateUserSession failed. rc=%d\n", rc));
    235     return EFAULT;
     279        if (ASMAtomicCmpXchgPtr(&pDev->si_drv1, pSession, (void *)0x42))
     280        {
     281            Log((DEVICE_NAME ":VBoxGuestFreeBSDOpen success: g_DevExt=%p pSession=%p rc=%d pid=%d\n", &g_DevExt, pSession, rc, (int)RTProcSelf()));
     282            return 0;
     283        }
     284
     285        VBoxGuestCloseSession(&g_DevExt, pSession);
     286    }
     287
     288    LogRel((DEVICE_NAME ":VBoxGuestFreeBSDOpen: failed. rc=%d\n", rc));
     289    return RTErrConvertToErrno(rc);
    236290}
    237291
     
    240294 *
    241295 */
    242 static int VBoxGuestFreeBSDClose(struct cdev *dev, int flags, int fmt, struct thread *td)
    243 {
    244     LogFlow((DEVICE_NAME ":VBoxGuestFreeBSDClose pid=%d\n", (int)RTProcSelf()));
    245 
    246     /*
    247      * Remove from the hash table.
    248      */
    249     PVBOXGUESTSESSION   pSession;
    250     const RTPROCESS     Process = RTProcSelf();
    251     const unsigned      iHash = SESSION_HASH(Process);
    252     RTSPINLOCKTMP       Tmp = RTSPINLOCKTMP_INITIALIZER;
    253     RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
    254 
    255     pSession = g_apSessionHashTab[iHash];
    256     if (pSession)
    257     {
    258         if (pSession->Process == Process)
    259         {
    260             g_apSessionHashTab[iHash] = pSession->pNextHash;
    261             pSession->pNextHash = NULL;
    262         }
    263         else
    264         {
    265             PVBOXGUESTSESSION pPrev = pSession;
    266             pSession = pSession->pNextHash;
    267             while (pSession)
    268             {
    269                 if (pSession->Process == Process)
    270                 {
    271                     pPrev->pNextHash = pSession->pNextHash;
    272                     pSession->pNextHash = NULL;
    273                     break;
    274                 }
    275 
    276                 /* next */
    277                 pPrev = pSession;
    278                 pSession = pSession->pNextHash;
    279             }
    280         }
    281     }
    282     RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
    283     if (!pSession)
    284     {
    285         Log((DEVICE_NAME ":VBoxGuestFreeBSDClose: WHUT?!? pSession == NULL! This must be a mistake... pid=%d", (int)Process));
    286         return EFAULT;
    287     }
    288     Log((DEVICE_NAME ":VBoxGuestFreeBSDClose: pid=%d\n", (int)Process));
    289 
    290     /*
    291      * Close the session.
    292      */
    293     VBoxGuestCloseSession(&g_DevExt, pSession);
     296static int VBoxGuestFreeBSDClose(struct cdev *pDev, int fFile, int DevType, struct thread *pTd)
     297{
     298    PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pDev->si_drv1;
     299    Log(("VBoxGuestFreeBSDClose: fFile=%#x iUnit=%d pSession=%p\n", fFile, minor2unit(minor(pDev)), pSession));
     300
     301    /*
     302     * Close the session if it's still hanging on to the device...
     303     */
     304    if (VALID_PTR(pSession))
     305    {
     306        VBoxGuestCloseSession(&g_DevExt, pSession);
     307        if (!ASMAtomicCmpXchgPtr(&pDev->si_drv1, NULL, pSession))
     308            Log(("VBoxGuestFreeBSDClose: si_drv1=%p expected %p!\n", pDev->si_drv1, pSession));
     309    }
     310    else
     311        Log(("VBoxGuestFreeBSDClose: si_drv1=%p!\n", pSession));
    294312    return 0;
    295313}
     
    299317 *
    300318 */
    301 static int VBoxGuestFreeBSDIOCtl(struct cdev *dev, u_long cmd, caddr_t data,
    302                                      int fflag, struct thread *td)
     319static int VBoxGuestFreeBSDIOCtl(struct cdev *pDev, u_long ulCmd, caddr_t pvData, int fFile, struct thread *pTd)
    303320{
    304321    LogFlow((DEVICE_NAME ":VBoxGuestFreeBSDIOCtl\n"));
    305322
    306     RTSPINLOCKTMP       Tmp = RTSPINLOCKTMP_INITIALIZER;
    307     const RTPROCESS     Process = RTProcSelf();
    308     const unsigned      iHash = SESSION_HASH(Process);
    309     PVBOXGUESTSESSION   pSession;
    310     int                 rc = 0;
    311 
    312     /*
    313      * Find the session.
    314      */
    315     RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
    316     pSession = g_apSessionHashTab[iHash];
    317     if (pSession && pSession->Process != Process)
    318     {
    319         do pSession = pSession->pNextHash;
    320         while (pSession && pSession->Process != Process);
    321     }
    322     RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
    323     if (!pSession)
    324     {
    325         Log((DEVICE_NAME ":VBoxGuestFreeBSDIOCtl: WHAT?!? pSession == NULL! This must be a mistake... pid=%d iCmd=%#lx\n", (int)Process, cmd));
     323    int rc = 0;
     324
     325    /*
     326     * Validate the input.
     327     */
     328    PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pDev->si_drv1;
     329    if (RT_UNLIKELY(!VALID_PTR(pSession)))
    326330        return EINVAL;
    327     }
    328331
    329332    /*
    330333     * Validate the request wrapper.
    331334     */
    332     if (IOCPARM_LEN(cmd) != sizeof(VBGLBIGREQ))
    333     {
    334         Log((DEVICE_NAME ": VBoxGuestFreeBSDIOCtl: bad request %lu size=%lu expected=%d\n", cmd, IOCPARM_LEN(cmd), sizeof(VBGLBIGREQ)));
     335    if (IOCPARM_LEN(ulCmd) != sizeof(VBGLBIGREQ))
     336    {
     337        Log((DEVICE_NAME ": VBoxGuestFreeBSDIOCtl: bad request %lu size=%lu expected=%d\n", ulCmd, IOCPARM_LEN(ulCmd), sizeof(VBGLBIGREQ)));
    335338        return ENOTTY;
    336339    }
    337340
    338     PVBGLBIGREQ ReqWrap = (PVBGLBIGREQ)data;
     341    PVBGLBIGREQ ReqWrap = (PVBGLBIGREQ)pvData;
    339342    if (ReqWrap->u32Magic != VBGLBIGREQ_MAGIC)
    340343    {
    341         Log((DEVICE_NAME ": VBoxGuestFreeBSDIOCtl: bad magic %#x; pArg=%p Cmd=%lu.\n", ReqWrap->u32Magic, data, cmd));
     344        Log((DEVICE_NAME ": VBoxGuestFreeBSDIOCtl: bad magic %#x; pArg=%p Cmd=%lu.\n", ReqWrap->u32Magic, pvData, ulCmd));
    342345        return EINVAL;
    343346    }
     
    345348                    || ReqWrap->cbData > _1M*16))
    346349    {
    347         printf(DEVICE_NAME ": VBoxGuestFreeBSDIOCtl: bad size %#x; pArg=%p Cmd=%lu.\n", ReqWrap->cbData, data, cmd);
     350        printf(DEVICE_NAME ": VBoxGuestFreeBSDIOCtl: bad size %#x; pArg=%p Cmd=%lu.\n", ReqWrap->cbData, pvData, ulCmd);
    348351        return EINVAL;
    349352    }
     
    363366    {
    364367        RTMemTmpFree(pvBuf);
    365         Log((DEVICE_NAME ":VBoxGuestFreeBSDIOCtl: copyin failed; pvBuf=%p pArg=%p Cmd=%lu. rc=%d\n", pvBuf, data, cmd, rc));
     368        Log((DEVICE_NAME ":VBoxGuestFreeBSDIOCtl: copyin failed; pvBuf=%p pArg=%p Cmd=%lu. rc=%d\n", pvBuf, pvData, ulCmd, rc));
    366369        return EFAULT;
    367370    }
     
    379382     */
    380383    size_t cbDataReturned;
    381     rc = VBoxGuestCommonIOCtl(cmd, &g_DevExt, pSession, pvBuf, ReqWrap->cbData, &cbDataReturned);
     384    rc = VBoxGuestCommonIOCtl(ulCmd, &g_DevExt, pSession, pvBuf, ReqWrap->cbData, &cbDataReturned);
    382385    if (RT_SUCCESS(rc))
    383386    {
     
    393396            if (RT_UNLIKELY(rc))
    394397            {
    395                 Log((DEVICE_NAME ":VBoxGuestFreeBSDIOCtl: copyout failed; pvBuf=%p pArg=%p Cmd=%lu. rc=%d\n", pvBuf, data, cmd, rc));
     398                Log((DEVICE_NAME ":VBoxGuestFreeBSDIOCtl: copyout failed; pvBuf=%p pArg=%p Cmd=%lu. rc=%d\n", pvBuf, pvData, ulCmd, rc));
    396399                rc = EFAULT;
    397400            }
     
    407410}
    408411
    409 static ssize_t VBoxGuestFreeBSDWrite (struct cdev *dev, struct uio *uio, int ioflag)
     412static ssize_t VBoxGuestFreeBSDWrite (struct cdev *pDev, struct uio *pUio, int fIo)
    410413{
    411414    return 0;
    412415}
    413416
    414 static ssize_t VBoxGuestFreeBSDRead (struct cdev *dev, struct uio *uio, int ioflag)
     417static ssize_t VBoxGuestFreeBSDRead (struct cdev *pDev, struct uio *pUio, int fIo)
    415418{
    416419    return 0;
    417420}
    418421
    419 static int VBoxGuestFreeBSDDetach(device_t self)
    420 {
    421     struct VBoxGuestDeviceState *pState = (struct VBoxGuestDeviceState *)device_get_softc(self);
    422 
    423     VBoxGuestFreeBSDRemoveIRQ(self, pState);
    424 
    425     if (pState->vmmdevmem)
    426         bus_release_resource(self, SYS_RES_MEMORY, pState->vmmdevmem_resid, pState->vmmdevmem);
    427     if (pState->io_port)
    428         bus_release_resource(self, SYS_RES_IOPORT, pState->io_port_resid, pState->io_port);
     422static int VBoxGuestFreeBSDDetach(device_t pDevice)
     423{
     424    struct VBoxGuestDeviceState *pState = (struct VBoxGuestDeviceState *)device_get_softc(pDevice);
     425
     426    /*
     427     * Reserve what we did in VBoxGuestFreeBSDAttach.
     428     */
     429    clone_cleanup(&g_pVBoxGuestFreeBSDClones);
     430
     431    VBoxGuestFreeBSDRemoveIRQ(pDevice, pState);
     432
     433    if (pState->pVMMDevMemRes)
     434        bus_release_resource(pDevice, SYS_RES_MEMORY, pState->iVMMDevMemResId, pState->pVMMDevMemRes);
     435    if (pState->pIOPortRes)
     436        bus_release_resource(pDevice, SYS_RES_IOPORT, pState->iIOPortResId, pState->pIOPortRes);
    429437
    430438    VBoxGuestDeleteDevExt(&g_DevExt);
    431 
    432     RTSpinlockDestroy(g_Spinlock);
    433     g_Spinlock = NIL_RTSPINLOCK;
    434439
    435440    free(pState, M_VBOXDEV);
     
    445450 * @param   pvState Opaque pointer to the device state.
    446451 */
    447 int VBoxGuestFreeBSDISR(void *pvState)
     452static int VBoxGuestFreeBSDISR(void *pvState)
    448453{
    449454    LogFlow((DEVICE_NAME ":VBoxGuestFreeBSDISR pvState=%p\n", pvState));
     
    458463 *
    459464 * @returns FreeBSD error code.
    460  * @param   self     Pointer to the device info structure.
     465 * @param   pDevice  Pointer to the device info structure.
    461466 * @param   pvState  Pointer to the state info structure.
    462467 */
    463 static int VBoxGuestFreeBSDAddIRQ(device_t self, void *pvState)
    464 {
    465     int res_id = 0;
     468static int VBoxGuestFreeBSDAddIRQ(device_t pDevice, void *pvState)
     469{
     470    int iResId = 0;
    466471    int rc = 0;
    467472    struct VBoxGuestDeviceState *pState = (struct VBoxGuestDeviceState *)pvState;
    468473
    469     pState->irq = bus_alloc_resource_any(self, SYS_RES_IRQ, &res_id, RF_SHAREABLE | RF_ACTIVE);
     474    pState->pIrqRes = bus_alloc_resource_any(pDevice, SYS_RES_IRQ, &iResId, RF_SHAREABLE | RF_ACTIVE);
    470475
    471476#if __FreeBSD_version >= 700000
    472     rc = bus_setup_intr(self, pState->irq, INTR_TYPE_BIO, NULL, (driver_intr_t *)VBoxGuestFreeBSDISR, pState, &pState->irq_handler);
     477    rc = bus_setup_intr(pDevice, pState->pIrqRes, INTR_TYPE_BIO, NULL, (driver_intr_t *)VBoxGuestFreeBSDISR, pState, &pState->pfnIrqHandler);
    473478#else
    474     rc = bus_setup_intr(self, pState->irq, INTR_TYPE_BIO, (driver_intr_t *)VBoxGuestFreeBSDISR, pState, &pState->irq_handler);
     479    rc = bus_setup_intr(pDevice, pState->pIrqRes, INTR_TYPE_BIO, (driver_intr_t *)VBoxGuestFreeBSDISR, pState, &pState->pfnIrqHandler);
    475480#endif
    476481
    477482    if (rc)
    478483    {
    479         pState->irq_handler = NULL;
     484        pState->pfnIrqHandler = NULL;
    480485        return VERR_DEV_IO_ERROR;
    481486    }
    482487
    483     pState->irq_resid = res_id;
     488    pState->iIrqResId = iResId;
    484489
    485490    return VINF_SUCCESS;
     
    489494 * Removes IRQ for VMMDev.
    490495 *
    491  * @param   self     Pointer to the device info structure.
     496 * @param   pDevice  Pointer to the device info structure.
    492497 * @param   pvState  Opaque pointer to the state info structure.
    493498 */
    494 static void VBoxGuestFreeBSDRemoveIRQ(device_t self, void *pvState)
     499static void VBoxGuestFreeBSDRemoveIRQ(device_t pDevice, void *pvState)
    495500{
    496501    struct VBoxGuestDeviceState *pState = (struct VBoxGuestDeviceState *)pvState;
    497502
    498     if (pState->irq)
    499     {
    500         bus_teardown_intr(self, pState->irq, pState->irq_handler);
    501         bus_release_resource(self, SYS_RES_IRQ, 0, pState->irq);
    502     }
    503 }
    504 
    505 static int VBoxGuestFreeBSDAttach(device_t self)
     503    if (pState->pIrqRes)
     504    {
     505        bus_teardown_intr(pDevice, pState->pIrqRes, pState->pfnIrqHandler);
     506        bus_release_resource(pDevice, SYS_RES_IRQ, 0, pState->pIrqRes);
     507    }
     508}
     509
     510static int VBoxGuestFreeBSDAttach(device_t pDevice)
    506511{
    507512    int rc = VINF_SUCCESS;
    508     int res_id = 0;
     513    int iResId = 0;
    509514    struct VBoxGuestDeviceState *pState = NULL;
    510515
     
    519524    }
    520525
    521     pState = device_get_softc(self);
     526    pState = device_get_softc(pDevice);
    522527    if (!pState)
    523528    {
     
    526531            return ENOMEM;
    527532
    528         device_set_softc(self, pState);
    529     }
    530 
    531     /*
    532      * Initialize the session hash table.
    533      */
    534     rc = RTSpinlockCreate(&g_Spinlock);
    535     if (RT_SUCCESS(rc))
     533        device_set_softc(pDevice, pState);
     534    }
     535
     536    /*
     537     * Allocate I/O port resource.
     538     */
     539    iResId                 = PCIR_BAR(0);
     540    pState->pIOPortRes     = bus_alloc_resource_any(pDevice, SYS_RES_IOPORT, &iResId, RF_ACTIVE);
     541    pState->uIOPortBase    = bus_get_resource_start(pDevice, SYS_RES_IOPORT, iResId);
     542    pState->iIOPortResId   = iResId;
     543    if (pState->uIOPortBase)
    536544    {
    537545        /*
    538          * Map the register address space.
     546         * Map the MMIO region.
    539547         */
    540         res_id                 =  PCIR_BAR(0);
    541         pState->io_port        = bus_alloc_resource_any(self, SYS_RES_IOPORT, &res_id, RF_ACTIVE);
    542         pState->io_port_tag    = rman_get_bustag(pState->io_port);
    543         pState->io_port_handle = rman_get_bushandle(pState->io_port);
    544         pState->uIOPortBase    = bus_get_resource_start(self, SYS_RES_IOPORT, res_id);
    545         pState->io_port_resid  = res_id;
    546         if (pState->uIOPortBase)
     548        iResId                   = PCIR_BAR(1);
     549        pState->pVMMDevMemRes    = bus_alloc_resource_any(pDevice, SYS_RES_MEMORY, &iResId, RF_ACTIVE);
     550        pState->VMMDevMemHandle  = rman_get_bushandle(pState->pVMMDevMemRes);
     551        pState->VMMDevMemSize    = bus_get_resource_count(pDevice, SYS_RES_MEMORY, iResId);
     552
     553        pState->pMMIOBase = (void *)pState->VMMDevMemHandle;
     554        pState->iVMMDevMemResId = iResId;
     555        if (pState->pMMIOBase)
    547556        {
    548557            /*
    549              * Map the MMIO region.
     558             * Add IRQ of VMMDev.
    550559             */
    551             res_id = PCIR_BAR(1);
    552             pState->vmmdevmem        = bus_alloc_resource_any(self, SYS_RES_MEMORY, &res_id, RF_ACTIVE);
    553             pState->vmmdevmem_tag    = rman_get_bustag(pState->vmmdevmem);
    554             pState->vmmdevmem_handle = rman_get_bushandle(pState->vmmdevmem);
    555             pState->vmmdevmem_addr   = bus_get_resource_start(self, SYS_RES_MEMORY, res_id);
    556             pState->vmmdevmem_size   = bus_get_resource_count(self, SYS_RES_MEMORY, res_id);
    557 
    558             pState->pMMIOBase = (void *)pState->vmmdevmem_handle;
    559             pState->vmmdevmem_resid = res_id;
    560             if (pState->pMMIOBase)
     560            rc = VBoxGuestFreeBSDAddIRQ(pDevice, pState);
     561            if (RT_SUCCESS(rc))
    561562            {
    562563                /*
    563                  * Add IRQ of VMMDev.
     564                 * Call the common device extension initializer.
    564565                 */
    565                 rc = VBoxGuestFreeBSDAddIRQ(self, pState);
     566                rc = VBoxGuestInitDevExt(&g_DevExt, pState->uIOPortBase, pState->pMMIOBase,
     567                                            pState->VMMDevMemSize, VBOXOSTYPE_FreeBSD);
    566568                if (RT_SUCCESS(rc))
    567569                {
    568570                    /*
    569                      * Call the common device extension initializer.
     571                     * Configure device cloning.
    570572                     */
    571                     rc = VBoxGuestInitDevExt(&g_DevExt, pState->uIOPortBase, pState->pMMIOBase,
    572                                              pState->vmmdevmem_size, VBOXOSTYPE_FreeBSD);
    573                     printf("rc=%d\n", rc);
    574                     if (RT_SUCCESS(rc))
     573                    clone_setup(&g_pVBoxGuestFreeBSDClones);
     574                    g_VBoxGuestFreeBSDEHTag = EVENTHANDLER_REGISTER(dev_clone, VBoxGuestFreeBSDClone, 0, 1000);
     575                    if (g_VBoxGuestFreeBSDEHTag)
    575576                    {
    576                         /*
    577                          * Create device node.
    578                          */
    579                         g_pVBoxAddFreeBSDChrDev = make_dev(&g_VBoxAddFreeBSDChrDevSW,
    580                                                            0,
    581                                                            UID_ROOT,
    582                                                            GID_WHEEL,
    583                                                            0666,
    584                                                            DEVICE_NAME);
    585                         g_pVBoxAddFreeBSDChrDev->si_drv1 = self;
     577                        printf(DEVICE_NAME ": loaded successfully\n");
    586578                        return 0;
    587579                    }
    588                     else
    589                         printf((DEVICE_NAME ":VBoxGuestInitDevExt failed.\n"));
    590                     VBoxGuestFreeBSDRemoveIRQ(self, pState);
     580
     581                    printf("vboxdrv: EVENTHANDLER_REGISTER(dev_clone,,,) failed\n");
     582                    clone_cleanup(&g_pVBoxGuestFreeBSDClones);
     583                    VBoxGuestDeleteDevExt(&g_DevExt);
    591584                }
    592585                else
    593                     printf((DEVICE_NAME ":VBoxGuestFreeBSDAddIRQ failed.\n"));
     586                    printf((DEVICE_NAME ":VBoxGuestInitDevExt failed.\n"));
     587                VBoxGuestFreeBSDRemoveIRQ(pDevice, pState);
    594588            }
    595589            else
    596                 printf((DEVICE_NAME ":MMIO region setup failed.\n"));
     590                printf((DEVICE_NAME ":VBoxGuestFreeBSDAddIRQ failed.\n"));
    597591        }
    598592        else
    599             printf((DEVICE_NAME ":IOport setup failed.\n"));
    600         RTSpinlockDestroy(g_Spinlock);
    601         g_Spinlock = NIL_RTSPINLOCK;
     593            printf((DEVICE_NAME ":MMIO region setup failed.\n"));
    602594    }
    603595    else
    604         printf((DEVICE_NAME ":RTSpinlockCreate failed.\n"));
     596        printf((DEVICE_NAME ":IOport setup failed.\n"));
    605597
    606598    RTR0Term();
     
    608600}
    609601
    610 static int VBoxGuestFreeBSDProbe(device_t self)
    611 {
    612     if ((pci_get_vendor(self) == VMMDEV_VENDORID) && (pci_get_device(self) == VMMDEV_DEVICEID))
    613     {
    614         Log(("Found VBox device\n"));
     602static int VBoxGuestFreeBSDProbe(device_t pDevice)
     603{
     604    if ((pci_get_vendor(pDevice) == VMMDEV_VENDORID) && (pci_get_device(pDevice) == VMMDEV_DEVICEID))
    615605        return 0;
    616     }
    617606
    618607    return ENXIO;
     
    622611{
    623612    /* Device interface. */
    624     DEVMETHOD(device_probe, VBoxGuestFreeBSDProbe),
     613    DEVMETHOD(device_probe,  VBoxGuestFreeBSDProbe),
    625614    DEVMETHOD(device_attach, VBoxGuestFreeBSDAttach),
    626615    DEVMETHOD(device_detach, VBoxGuestFreeBSDDetach),
  • trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3Lib.cpp

    r8155 r8188  
    151151    g_File = hf;
    152152
    153 #elif defined(VBOX_VBGLR3_XFREE86)
     153#elif defined(VBOX_VBGLR3_XFREE86) && !defined(RT_OS_FREEBSD)
    154154    int File = xf86open(VBOXGUEST_DEVICE_NAME, XF86_O_RDWR);
    155155    if (File == -1)
     
    157157    g_File = File;
    158158
     159#elif defined(RT_OS_FREEBSD)
     160    /*
     161     * Try open the BSD device.
     162     */
     163#if defined(VBOX_VBGLR3_XFREE86)
     164    int File = 0;
     165#else
     166    RTFILE File = 0;
     167#endif
     168    int rc;
     169    char szDevice[sizeof(VBOXGUEST_DEVICE_NAME) + 16];
     170    for (unsigned iUnit = 0; iUnit < 1024; iUnit++)
     171    {
     172        RTStrPrintf(szDevice, sizeof(szDevice), VBOXGUEST_DEVICE_NAME "%d", iUnit);
     173#if defined(VBOX_VBGLR3_XFREE86)
     174        File = xf86open(szDevice, XF86_O_RDWR);
     175        if (File >= 0)
     176            break;
     177#else
     178        rc = RTFileOpen(&File, szDevice, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
     179        if (RT_SUCCESS(rc))
     180            break;
     181#endif
     182    }
     183
     184#if defined(VBOX_VBGLR3_XFREE86)
     185    if (File == -1)
     186        return VERR_OPEN_FAILED;
     187#else
     188    if (RT_FAILURE(rc))
     189        return rc;
     190#endif
     191
     192    g_File = File;
    159193#else
    160194    /* the default implemenation. (linux, solaris) */
  • trunk/src/VBox/Additions/freebsd/vboxvfs/vboxvfs_vfsops.c

    r8155 r8188  
    7171MODULE_DEPEND(vboxvfs, vboxguest, 1, 1, 1);
    7272
    73 static int
    74 vboxvfs_cmount(struct mntarg *ma, void * data, int flags, struct thread *td)
     73static int vboxvfs_cmount(struct mntarg *ma, void * data, int flags, struct thread *td)
    7574{
    7675    struct vboxvfs_mount_info args;
     
    213212}
    214213
    215 static int vboxvfs_quotactl(mp, cmd, uid, arg, td)
    216         struct mount *mp;
    217         int cmd;
    218         uid_t uid;
    219         void *arg;
    220         struct thread *td;
     214static int vboxvfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg, struct thread *td)
    221215{
    222216    return EOPNOTSUPP;
     
    230224    rc = vboxInit();
    231225    if (VBOX_FAILURE(rc))
    232         return ENXIO;
     226        return ENXIO;
    233227
    234228    /* Connect to the host service. */
     
    236230    if (VBOX_FAILURE(rc))
    237231    {
    238         printf("Failed to get connection to host! rc=%d\n", rc);
    239         vboxUninit();
    240         return ENXIO;
     232        printf("Failed to get connection to host! rc=%d\n", rc);
     233        vboxUninit();
     234        return ENXIO;
    241235    }
    242236
     
    245239    {
    246240        printf("vboxCallSetUtf8 failed, rc=%d\n", rc);
    247         vboxDisconnect(&g_vboxSFClient);
    248         vboxUninit();
    249         return EPROTO;
     241        vboxDisconnect(&g_vboxSFClient);
     242        vboxUninit();
     243        return EPROTO;
    250244    }
    251245
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