Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 31 Oct 2011 12:07:11 +0100
From:      Ed Schouten <ed@80386.nl>
To:        Bernhard Froehlich <decke@FreeBSD.org>
Cc:        Marco Steinbach <coco@executive-computing.de>, Vladimir Kushnir <vkushnir@bigmir.net>, freebsd-ports <freebsd-ports@freebsd.org>
Subject:   Re: VirtualBox-kmod fails to build on -CURRENT
Message-ID:  <20111031110711.GJ2258@hoeg.nl>
In-Reply-To: <20111031102602.GI2258@hoeg.nl>
References:  <alpine.BSF.2.00.1110261224240.14451@kushnir1.kiev.ua> <4EAE690D.1070609@executive-computing.de> <01c0ad0f006967454da602c2812981ce@bluelife.at> <20111031102602.GI2258@hoeg.nl>

next in thread | previous in thread | raw e-mail | index | archive | help

--MFZs98Tklfu0WsCO
Content-Type: multipart/mixed; boundary="CNfT9TXqV7nd4cfk"
Content-Disposition: inline


--CNfT9TXqV7nd4cfk
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

* Ed Schouten <ed@80386.nl>, 20111031 11:26:
> I think I'll take a closer look at this code more thoroughly sometime
> this week.

Ugh. Too impatient. Please try the attached patch. It should build on
7.x and higher. I have only compile-tested it, because I am not a
VirtualBox user (yet).

| SUPDrv-freebsd.c |  153 ++++++-----------------------------------------
| 1 file changed, 21 insertions(+), 132 deletions(-)

--=20
 Ed Schouten <ed@80386.nl>
 WWW: http://80386.nl/

--CNfT9TXqV7nd4cfk
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=patch-cdev-fixes
Content-Transfer-Encoding: quoted-printable

--- src/VBox/HostDrivers/Support/freebsd/SUPDrv-freebsd.c
+++ src/VBox/HostDrivers/Support/freebsd/SUPDrv-freebsd.c
@@ -69,10 +69,9 @@
 static int VBoxDrvFreeBSDModuleEvent(struct module *pMod, int enmEventType=
, void *pvArg);
 static int VBoxDrvFreeBSDLoad(void);
 static int VBoxDrvFreeBSDUnload(void);
-static void VBoxDrvFreeBSDClone(void *pvArg, struct ucred *pCred, char *pa=
chName, int cchName, struct cdev **ppDev);
=20
-static d_fdopen_t   VBoxDrvFreeBSDOpen;
-static d_close_t    VBoxDrvFreeBSDClose;
+static d_open_t     VBoxDrvFreeBSDOpen;
+static void         VBoxDrvFreeBSDDtr(void *pData);
 static d_ioctl_t    VBoxDrvFreeBSDIOCtl;
 static int          VBoxDrvFreeBSDIOCtlSlow(PSUPDRVSESSION pSession, u_lon=
g ulCmd, caddr_t pvData, struct thread *pTd);
=20
@@ -100,21 +99,13 @@
 static struct cdevsw        g_VBoxDrvFreeBSDChrDevSW =3D
 {
     .d_version =3D        D_VERSION,
-#if __FreeBSD_version > 800061
-    .d_flags =3D          D_PSEUDO | D_TRACKCLOSE | D_NEEDMINOR,
-#else
-    .d_flags =3D          D_PSEUDO | D_TRACKCLOSE,
-#endif
-    .d_fdopen =3D         VBoxDrvFreeBSDOpen,
-    .d_close =3D          VBoxDrvFreeBSDClose,
+    .d_open =3D           VBoxDrvFreeBSDOpen,
     .d_ioctl =3D          VBoxDrvFreeBSDIOCtl,
     .d_name =3D           "vboxdrv"
 };
=20
 /** List of cloned device. Managed by the kernel. */
-static struct clonedevs    *g_pVBoxDrvFreeBSDClones;
-/** The dev_clone event handler tag. */
-static eventhandler_tag     g_VBoxDrvFreeBSDEHTag;
+static struct cdev         *g_pVBoxDrvFreeBSDChrDev;
 /** Reference counter. */
 static volatile uint32_t    g_cUsers;
=20
@@ -176,20 +167,10 @@
         if (RT_SUCCESS(rc))
         {
             /*
-             * Configure device cloning.
+             * Configure character device.
              */
-            clone_setup(&g_pVBoxDrvFreeBSDClones);
-            g_VBoxDrvFreeBSDEHTag =3D EVENTHANDLER_REGISTER(dev_clone, VBo=
xDrvFreeBSDClone, 0, 1000);
-            if (g_VBoxDrvFreeBSDEHTag)
-            {
-                Log(("VBoxDrvFreeBSDLoad: returns successfully\n"));
-                return VINF_SUCCESS;
-            }
-
-            printf("vboxdrv: EVENTHANDLER_REGISTER(dev_clone,,,) failed\n"=
);
-            clone_cleanup(&g_pVBoxDrvFreeBSDClones);
-            rc =3D VERR_ALREADY_LOADED;
-            supdrvDeleteDevExt(&g_VBoxDrvFreeBSDDevExt);
+            g_pVBoxDrvFreeBSDChrDev =3D make_dev(&g_VBoxDrvFreeBSDChrDevSW=
, 0, UID_ROOT, GID_WHEEL, VBOXDRV_PERM, "vboxdrv");
+            return VINF_SUCCESS;
         }
         else
             printf("vboxdrv: supdrvInitDevExt failed, rc=3D%d\n", rc);
@@ -210,8 +191,7 @@
     /*
      * Reserve what we did in VBoxDrvFreeBSDInit.
      */
-    EVENTHANDLER_DEREGISTER(dev_clone, g_VBoxDrvFreeBSDEHTag);
-    clone_cleanup(&g_pVBoxDrvFreeBSDClones);
+    destroy_dev(g_pVBoxDrvFreeBSDChrDev);
=20
     supdrvDeleteDevExt(&g_VBoxDrvFreeBSDDevExt);
=20
@@ -225,59 +205,6 @@
=20
=20
 /**
- * DEVFS event handler.
- */
-static void VBoxDrvFreeBSDClone(void *pvArg, struct ucred *pCred, char *ps=
zName, int cchName, struct cdev **ppDev)
-{
-    int iUnit;
-    int rc;
-
-    Log(("VBoxDrvFreeBSDClone: pszName=3D%s ppDev=3D%p\n", pszName, ppDev)=
);
-
-    /*
-     * One device node per user, si_drv1 points to the session.
-     * /dev/vboxdrv<N> where N =3D {0...255}.
-     */
-    if (!ppDev)
-        return;
-    if (dev_stdclone(pszName, NULL, "vboxdrv", &iUnit) !=3D 1)
-        return;
-    if (iUnit >=3D 256 || iUnit < 0)
-    {
-        Log(("VBoxDrvFreeBSDClone: iUnit=3D%d >=3D 256 - rejected\n", iUni=
t));
-        return;
-    }
-
-    Log(("VBoxDrvFreeBSDClone: pszName=3D%s iUnit=3D%d\n", pszName, iUnit)=
);
-
-    rc =3D clone_create(&g_pVBoxDrvFreeBSDClones, &g_VBoxDrvFreeBSDChrDevS=
W, &iUnit, ppDev, 0);
-    Log(("VBoxDrvFreeBSDClone: clone_create -> %d; iUnit=3D%d\n", rc, iUni=
t));
-    if (rc)
-    {
-#if __FreeBSD_version > 800061
-        *ppDev =3D make_dev(&g_VBoxDrvFreeBSDChrDevSW, iUnit, UID_ROOT, GI=
D_WHEEL, VBOXDRV_PERM, "vboxdrv%d", iUnit);
-#else
-        *ppDev =3D make_dev(&g_VBoxDrvFreeBSDChrDevSW, unit2minor(iUnit), =
UID_ROOT, GID_WHEEL, VBOXDRV_PERM, "vboxdrv%d", iUnit);
-#endif
-        if (*ppDev)
-        {
-            dev_ref(*ppDev);
-            (*ppDev)->si_flags |=3D SI_CHEAPCLONE;
-            Log(("VBoxDrvFreeBSDClone: Created *ppDev=3D%p iUnit=3D%d si_d=
rv1=3D%p si_drv2=3D%p\n",
-                 *ppDev, iUnit, (*ppDev)->si_drv1, (*ppDev)->si_drv2));
-            (*ppDev)->si_drv1 =3D (*ppDev)->si_drv2 =3D NULL;
-        }
-        else
-            OSDBGPRINT(("VBoxDrvFreeBSDClone: make_dev iUnit=3D%d failed\n=
", iUnit));
-    }
-    else
-        Log(("VBoxDrvFreeBSDClone: Existing *ppDev=3D%p iUnit=3D%d si_drv1=
=3D%p si_drv2=3D%p\n",
-             *ppDev, iUnit, (*ppDev)->si_drv1, (*ppDev)->si_drv2));
-}
-
-
-
-/**
  *
  * @returns 0 on success, errno on failure.
  *          EBUSY if the device is used by someone else.
@@ -287,21 +214,11 @@
  * @param   pFd     The file descriptor. FreeBSD 7.0 and later.
  * @param   iFd     The file descriptor index(?). Pre FreeBSD 7.0.
  */
-#if __FreeBSD__ >=3D 7
-static int VBoxDrvFreeBSDOpen(struct cdev *pDev, int fOpen, struct thread =
*pTd, struct file *pFd)
-#else
-static int VBoxDrvFreeBSDOpen(struct cdev *pDev, int fOpen, struct thread =
*pTd, int iFd)
-#endif
+static int VBoxDrvFreeBSDOpen(struct cdev *pDev, int fOpen, int iDevtype, =
struct thread *pTd)
 {
     PSUPDRVSESSION pSession;
     int rc;
=20
-#if __FreeBSD_version < 800062
-    Log(("VBoxDrvFreeBSDOpen: fOpen=3D%#x iUnit=3D%d\n", fOpen, minor2unit=
(minor(pDev))));
-#else
-    Log(("VBoxDrvFreeBSDOpen: fOpen=3D%#x iUnit=3D%d\n", fOpen, minor(dev2=
udev(pDev))));
-#endif
-
     /*
      * Let's be a bit picky about the flags...
      */
@@ -312,12 +229,6 @@
     }
=20
     /*
-     * Try grab it (we don't grab the giant, remember).
-     */
-    if (!ASMAtomicCmpXchgPtr(&pDev->si_drv1, (void *)0x42, NULL))
-        return EBUSY;
-
-    /*
      * Create a new session.
      */
     rc =3D supdrvCreateSession(&g_VBoxDrvFreeBSDDevExt, true /* fUser */, =
&pSession);
@@ -326,14 +237,10 @@
         /** @todo get (r)uid and (r)gid.
         pSession->Uid =3D stuff;
         pSession->Gid =3D stuff; */
-        if (ASMAtomicCmpXchgPtr(&pDev->si_drv1, pSession, (void *)0x42))
-        {
-            ASMAtomicIncU32(&g_cUsers);
-            return 0;
-        }
-
-        OSDBGPRINT(("VBoxDrvFreeBSDOpen: si_drv1=3D%p, expected 0x42!\n", =
pDev->si_drv1));
-        supdrvCloseSession(&g_VBoxDrvFreeBSDDevExt, pSession);
+        devfs_set_cdevpriv(pSession, VBoxDrvFreeBSDDtr);
+        Log(("VBoxDrvFreeBSDOpen: pSession=3D%p\n", pSession));
+        ASMAtomicIncU32(&g_cUsers);
+        return 0;
     }
=20
     return RTErrConvertToErrno(rc);
@@ -349,30 +256,16 @@
  * @param   DevType     The device type (CHR.
  * @param   pTd         The calling thread.
  */
-static int VBoxDrvFreeBSDClose(struct cdev *pDev, int fFile, int DevType, =
struct thread *pTd)
+static void VBoxDrvFreeBSDDtr(void *pData)
 {
-    PSUPDRVSESSION pSession =3D (PSUPDRVSESSION)pDev->si_drv1;
-#if __FreeBSD_version < 800062
-    Log(("VBoxDrvFreeBSDClose: fFile=3D%#x iUnit=3D%d pSession=3D%p\n", fF=
ile, minor2unit(minor(pDev)), pSession));
-#else
-    Log(("VBoxDrvFreeBSDClose: fFile=3D%#x iUnit=3D%d pSession=3D%p\n", fF=
ile, minor(dev2udev(pDev)), pSession));
-#endif
+    PSUPDRVSESSION pSession =3D pData;
+    Log(("VBoxDrvFreeBSDDtr: pSession=3D%p\n", pSession));
=20
     /*
-     * Close the session if it's still hanging on to the device...
+     * Close the session.
      */
-    if (VALID_PTR(pSession))
-    {
-        supdrvCloseSession(&g_VBoxDrvFreeBSDDevExt, pSession);
-        if (!ASMAtomicCmpXchgPtr(&pDev->si_drv1, NULL, pSession))
-            OSDBGPRINT(("VBoxDrvFreeBSDClose: si_drv1=3D%p expected %p!\n"=
, pDev->si_drv1, pSession));
-        ASMAtomicDecU32(&g_cUsers);
-        /* Don't use destroy_dev here because it may sleep resulting in a =
hanging user process. */
-        destroy_dev_sched(pDev);
-    }
-    else
-        OSDBGPRINT(("VBoxDrvFreeBSDClose: si_drv1=3D%p!\n", pSession));
-    return 0;
+    supdrvCloseSession(&g_VBoxDrvFreeBSDDevExt, pSession);
+    ASMAtomicDecU32(&g_cUsers);
 }
=20
=20
@@ -388,12 +281,8 @@
  */
 static int VBoxDrvFreeBSDIOCtl(struct cdev *pDev, u_long ulCmd, caddr_t pv=
Data, int fFile, struct thread *pTd)
 {
-    /*
-     * Validate the input.
-     */
-    PSUPDRVSESSION pSession =3D (PSUPDRVSESSION)pDev->si_drv1;
-    if (RT_UNLIKELY(!VALID_PTR(pSession)))
-        return EINVAL;
+    PSUPDRVSESSION pSession;
+    devfs_get_cdevpriv((void **)&pSession);
=20
     /*
      * Deal with the fast ioctl path first.

--CNfT9TXqV7nd4cfk--

--MFZs98Tklfu0WsCO
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (FreeBSD)

iQIcBAEBAgAGBQJOroFeAAoJEG5e2P40kaK7YfQP+wf9vqOrkUOu2M3Vq43WZBsp
vmHzt1RkK4QrgGYMZtgYulzIn0a45L2Hpe9k8dTvXI3MoytMtnp/w8wwIphE+GHI
KF/OGFMnDoVh3opK+bLYAwdh+DywW9fJpxeeNeh1fikgTjYR5xjI4AfGIltyeDMq
KI1kwt1jfkxqUw18fP9a53CU4FeBlqkCNkfqvU79nT06RPJbQunloIRxJGXzV1Zo
c1RgB+YaK1Y7RiFbKGz2D0aWMhqIZpj02pAD+BuYTNsR6XqlUU9KwwRwU22sOhu3
MHKHyOyTU5qkRowhcg9QUpBTrwocYd6uf/3B5q2krwR77kxNDqGi71E3QtoHujrb
/4l9HC1lu8pNLoNaPGd2ULtO+U0spltfkWbYLN2UZC94DhoB4XTuXG8dzE9v8GMe
MIMVbgbTwRPBK2QhlArISdM0YCZbhVg9+nFyK/gOcR5UTnHH57vr0OcxN9jb181s
UdjPWyTI2Ndj8BMVN2nYHzLICM6IH4T6hJNBlBL6/hIuZpeIHbD9pj5WGrhV/OtD
9URLICF1/UVh8Sk/QRRnGTu/mwAFMA7wc9pegxYWLwJNVWTnUyIiCSrSMm4uDID1
QfhTTx0Z8qfy3ZtCgwWBu6c/W4ftSjxUfxP4nDdnxjLZnIFaim/c7tytQpJmXomG
0QzRZIE8U7v30KqSDQpi
=mcsJ
-----END PGP SIGNATURE-----

--MFZs98Tklfu0WsCO--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20111031110711.GJ2258>