From owner-freebsd-emulation@FreeBSD.ORG  Sun Apr  1 12:25:10 2012
Return-Path: <owner-freebsd-emulation@FreeBSD.ORG>
Delivered-To: freebsd-emulation@freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52])
	by hub.freebsd.org (Postfix) with ESMTP id 7887C106566C;
	Sun,  1 Apr 2012 12:25:10 +0000 (UTC)
	(envelope-from to.my.trociny@gmail.com)
Received: from mail-bk0-f54.google.com (mail-bk0-f54.google.com
	[209.85.214.54])
	by mx1.freebsd.org (Postfix) with ESMTP id 9A87C8FC17;
	Sun,  1 Apr 2012 12:25:09 +0000 (UTC)
Received: by bkcjc3 with SMTP id jc3so2006754bkc.13
	for <multiple recipients>; Sun, 01 Apr 2012 05:25:07 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;
	h=from:to:cc:subject:references:x-comment-to:sender:date:in-reply-to
	:message-id:user-agent:mime-version:content-type;
	bh=YloAN7S0g7zEzM7SqrVG8ea/twl04TJBZVRK8dhRSbg=;
	b=NxZKeHfa0EqgSI8yEuOZPzKnK+aYLatqmRxYxvR/jvjb5m/BXzu/JfjnTjS8brVdGG
	/4m0K/LBGDMsGKE7/oSaWD6SKXugW4AEPKUPL+8PYWT/XMe5RuMKuMVeWv+C+XacEGxL
	utBZv2cWm23N64p5fnkUPIqEfG2gU5w/7wfuKbmAHv67nAwDYnZkiz5EvNbWg2ufG9t1
	jf+j6ZkP+l2Tw9x59K9fbmBcJfI42ECOUFZML45/wickIEeADZVRofdiUS4DeMVj6s+0
	A+Ttwn01Utv2dcd0gRHqjGM7YmdflScJvWkxldj3LpZc+bBhOPIwAM+YfoHlY6mJ56jQ
	ZOrQ==
Received: by 10.204.152.72 with SMTP id f8mr1986243bkw.103.1333283107602;
	Sun, 01 Apr 2012 05:25:07 -0700 (PDT)
Received: from localhost ([95.69.173.122]) by mx.google.com with ESMTPS id
	zx16sm31619605bkb.13.2012.04.01.05.25.05
	(version=TLSv1/SSLv3 cipher=OTHER);
	Sun, 01 Apr 2012 05:25:06 -0700 (PDT)
From: Mikolaj Golub <trociny@freebsd.org>
To: Bernhard Froehlich <decke@FreeBSD.org>
References: <867gyg77h0.fsf@kopusha.home.net>
	<12f5de744d0ca920d3b204aa291b4084@bluelife.at>
X-Comment-To: Bernhard Froehlich
Sender: Mikolaj Golub <to.my.trociny@gmail.com>
Date: Sun, 01 Apr 2012 15:25:03 +0300
In-Reply-To: <12f5de744d0ca920d3b204aa291b4084@bluelife.at> (Bernhard
	Froehlich's message of "Fri, 30 Mar 2012 10:13:57 +0200")
Message-ID: <86iphjiqqo.fsf@kopusha.home.net>
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (berkeley-unix)
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
Cc: freebsd-emulation@freebsd.org, freebsd-virtualization@freebsd.org
Subject: Re: [patch] VirtualBox-4.0.14 + FreeBSD/CURRENT + VIMAGE: crash on
	vm shutdown
X-BeenThere: freebsd-emulation@freebsd.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Development of Emulators of other operating systems
	<freebsd-emulation.freebsd.org>
List-Unsubscribe: <http://lists.freebsd.org/mailman/listinfo/freebsd-emulation>, 
	<mailto:freebsd-emulation-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/freebsd-emulation>
List-Post: <mailto:freebsd-emulation@freebsd.org>
List-Help: <mailto:freebsd-emulation-request@freebsd.org?subject=help>
List-Subscribe: <http://lists.freebsd.org/mailman/listinfo/freebsd-emulation>, 
	<mailto:freebsd-emulation-request@freebsd.org?subject=subscribe>
X-List-Received-Date: Sun, 01 Apr 2012 12:25:10 -0000

--=-=-=

Hi,

On Fri, 30 Mar 2012 10:13:57 +0200 Bernhard Froehlich wrote:

 BF> Sorry for the late response and thanks for the patch. Is this patch
 BF> still required for 4.1.8? My guess is yes but it would be great if you
 BF> could confirm that with the latest port.

Sorry, it required some time to check -- I upgraded my box to the latest
current and had some instabilities to fix before testing vbox.

So on the recent current and VirtualBox-4.1.10 (the latest version I have
found in the ports) the VBoxNetFlt patch is still required but VBoxNetAdp also
needs patching: I had a crash on vm start:

#0  doadump (textdump=-2022600704) at pcpu.h:244
#1  0x8051af69 in db_fncall (dummy1=1, dummy2=0, dummy3=-2127583648, dummy4=0xdcb61838 "")
    at /home/golub/freebsd/base/head/sys/ddb/db_command.c:573
#2  0x8051b361 in db_command (last_cmdp=0x811221fc, cmd_table=0x0, dopager=1)
    at /home/golub/freebsd/base/head/sys/ddb/db_command.c:449
#3  0x8051b4ba in db_command_loop () at /home/golub/freebsd/base/head/sys/ddb/db_command.c:502
#4  0x8051d4ad in db_trap (type=12, code=0)
    at /home/golub/freebsd/base/head/sys/ddb/db_main.c:229
#5  0x80a81d96 in kdb_trap (type=12, code=0, tf=0xdcb61ad4)
    at /home/golub/freebsd/base/head/sys/kern/subr_kdb.c:629
#6  0x80dd22af in trap_fatal (frame=0xdcb61ad4, eva=24)
    at /home/golub/freebsd/base/head/sys/i386/i386/trap.c:1014
#7  0x80dd2387 in trap_pfault (frame=0xdcb61ad4, usermode=0, eva=24)
    at /home/golub/freebsd/base/head/sys/i386/i386/trap.c:835
#8  0x80dd3451 in trap (frame=0xdcb61ad4)
    at /home/golub/freebsd/base/head/sys/i386/i386/trap.c:547
#9  0x80dbccac in calltrap () at /home/golub/freebsd/base/head/sys/i386/i386/exception.s:169
#10 0x80b0633e in ifindex_alloc_locked (idxp=0xdcb61b56)
    at /home/golub/freebsd/base/head/sys/net/if.c:279
#11 0x80b09172 in if_alloc (type=6 '\006') at /home/golub/freebsd/base/head/sys/net/if.c:435
#12 0x8b01e8e7 in vboxNetAdpOsCreate () from /boot/modules/vboxnetadp.ko
#13 0x8b01ef00 in vboxNetAdpCreate () from /boot/modules/vboxnetadp.ko
#14 0x8b01ecd8 in VBoxNetAdpFreeBSDCtrlioctl () from /boot/modules/vboxnetadp.ko
#15 0x8095c6bb in devfs_ioctl_f (fp=0x8b6f8118, com=3223352833, data=0x8b022360, 
    cred=0x87759b00, td=0x89fdf2e0)
    at /home/golub/freebsd/base/head/sys/fs/devfs/devfs_vnops.c:757
#16 0x80a9b4ed in kern_ioctl (td=0x89fdf2e0, fd=3, com=3223352833, data=0x8b022360 "vboxnet0")
    at file.h:287
#17 0x80a9b674 in sys_ioctl (td=0x89fdf2e0, uap=0xdcb61cec)
    at /home/golub/freebsd/base/head/sys/kern/sys_generic.c:691
#18 0x80dd29ae in syscall (frame=0xdcb61d28) at subr_syscall.c:135

I am attaching two patches: the first one is for VBoxNetFlt (the same I sent
to the list but against 4.1.10) and the second one for VBoxNetAdp (a fix for
the panic above).

 BF> Do you aggree that this patch is under MIT License so that I can push
 BF> it upstream?

Sure for both patches. Thanks for taking it!

-- 
Mikolaj Golub


--=-=-=
Content-Type: text/x-patch
Content-Disposition: attachment;
	filename=VirtualBox-4.1.10.VBoxNetFlt.freebsd.VIMAGE.patch

diff -rpu VirtualBox-4.1.10.orig/src/VBox/HostDrivers/VBoxNetFlt/freebsd/VBoxNetFlt-freebsd.c VirtualBox-4.1.10/src/VBox/HostDrivers/VBoxNetFlt/freebsd/VBoxNetFlt-freebsd.c
--- VirtualBox-4.1.10.orig/src/VBox/HostDrivers/VBoxNetFlt/freebsd/VBoxNetFlt-freebsd.c	2012-03-13 15:15:44.000000000 +0200
+++ VirtualBox-4.1.10/src/VBox/HostDrivers/VBoxNetFlt/freebsd/VBoxNetFlt-freebsd.c	2012-04-01 13:43:02.000000000 +0300
@@ -651,13 +651,13 @@ bool vboxNetFltOsMaybeRediscovered(PVBOX
         ng_rmnode_self(pThis->u.s.node);
         pThis->u.s.node = NULL;
     }
+    VBOXCURVNET_RESTORE();
 
     if (ifp0 != NULL)
     {
         vboxNetFltOsDeleteInstance(pThis);
         vboxNetFltOsInitInstance(pThis, NULL);
     }
-    VBOXCURVNET_RESTORE();
 
     return !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost);
 }
@@ -671,8 +671,10 @@ void vboxNetFltOsDeleteInstance(PVBOXNET
     mtx_destroy(&pThis->u.s.inq.ifq_mtx);
     mtx_destroy(&pThis->u.s.outq.ifq_mtx);
 
+    VBOXCURVNET_SET_FROM_UCRED();
     if (pThis->u.s.node != NULL)
         ng_rmnode_self(pThis->u.s.node);
+    VBOXCURVNET_RESTORE();
     pThis->u.s.node = NULL;
 }

--=-=-=
Content-Type: text/x-patch
Content-Disposition: attachment;
	filename=VirtualBox-4.1.10.VBoxNetAdp.freebsd.VIMAGE.patch

diff -rpu VirtualBox-4.1.10.orig/src/VBox/HostDrivers/VBoxNetAdp/freebsd/Makefile VirtualBox-4.1.10/src/VBox/HostDrivers/VBoxNetAdp/freebsd/Makefile
--- VirtualBox-4.1.10.orig/src/VBox/HostDrivers/VBoxNetAdp/freebsd/Makefile	2012-03-13 15:15:44.000000000 +0200
+++ VirtualBox-4.1.10/src/VBox/HostDrivers/VBoxNetAdp/freebsd/Makefile	2012-04-01 13:27:16.000000000 +0300
@@ -18,7 +18,7 @@
 
 KMOD = vboxnetadp
 
-CFLAGS += -DRT_OS_FREEBSD -DIN_RING0 -DIN_RT_R0 -DIN_SUP_R0 -DVBOX -DRT_WITH_VBOX -Iinclude -I. -Ir0drv -w -DVBOX_WITH_HARDENING
+CFLAGS += -DRT_OS_FREEBSD -DIN_RING0 -DIN_RT_R0 -DIN_SUP_R0 -DVBOX -DRT_WITH_VBOX -Iinclude -I. -Ir0drv -w -DVBOX_WITH_HARDENING -DVIMAGE
 
 .if (${MACHINE_ARCH} == "i386")
  CFLAGS += -DRT_ARCH_X86
diff -rpu VirtualBox-4.1.10.orig/src/VBox/HostDrivers/VBoxNetAdp/freebsd/VBoxNetAdp-freebsd.c VirtualBox-4.1.10/src/VBox/HostDrivers/VBoxNetAdp/freebsd/VBoxNetAdp-freebsd.c
--- VirtualBox-4.1.10.orig/src/VBox/HostDrivers/VBoxNetAdp/freebsd/VBoxNetAdp-freebsd.c	2012-03-13 15:15:44.000000000 +0200
+++ VirtualBox-4.1.10/src/VBox/HostDrivers/VBoxNetAdp/freebsd/VBoxNetAdp-freebsd.c	2012-04-01 13:23:58.000000000 +0300
@@ -68,6 +68,22 @@
 #define VBOXNETADP_OS_SPECFIC 1
 #include "../VBoxNetAdpInternal.h"
 
+#if defined(__FreeBSD_version) && __FreeBSD_version >= 800500
+# include <sys/jail.h>
+# include <net/vnet.h>
+
+# define VBOXCURVNET_SET(arg)           CURVNET_SET_QUIET(arg)
+# define VBOXCURVNET_SET_FROM_UCRED()   VBOXCURVNET_SET(CRED_TO_VNET(curthread->td_ucred))
+# define VBOXCURVNET_RESTORE()          CURVNET_RESTORE()
+
+#else /* !defined(__FreeBSD_version) || __FreeBSD_version < 800500 */
+
+# define VBOXCURVNET_SET(arg)
+# define VBOXCURVNET_SET_FROM_UCRED()
+# define VBOXCURVNET_RESTORE()
+
+#endif /* !defined(__FreeBSD_version) || __FreeBSD_version < 800500 */
+
 static int VBoxNetAdpFreeBSDCtrlioctl(struct cdev *, u_long, caddr_t, int flags,
     struct thread *);
 static struct cdevsw vboxnetadp_cdevsw =
@@ -260,6 +276,7 @@ int vboxNetAdpOsCreate(PVBOXNETADP pThis
 {
     struct ifnet *ifp;
 
+    VBOXCURVNET_SET_FROM_UCRED();
     ifp = if_alloc(IFT_ETHER);
     if (ifp == NULL)
         return VERR_NO_MEMORY;
@@ -279,6 +296,7 @@ int vboxNetAdpOsCreate(PVBOXNETADP pThis
 
     strncpy(pThis->szName, ifp->if_xname, VBOXNETADP_MAX_NAME_LEN);
     pThis->u.s.ifp = ifp;
+    VBOXCURVNET_RESTORE();
     return 0;
 }
 
@@ -286,7 +304,9 @@ void vboxNetAdpOsDestroy(PVBOXNETADP pTh
 {
     struct ifnet *ifp;
 
+    VBOXCURVNET_SET(ifp->if_vnet);
     ifp = pThis->u.s.ifp;
     ether_ifdetach(ifp);
     if_free(ifp);
+    VBOXCURVNET_RESTORE();
 }

--=-=-=--