Subject: Re: Fix for NetBSD Dom0 boot problem
To: Manuel Bouyer <bouyer@antioche.eu.org>
From: Christoph Egger <Christoph_Egger@gmx.de>
List: port-xen
Date: 12/06/2007 09:08:17
--Boundary-00=_x36VHd0hoHSu2UG
Content-Type: text/plain;
  charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On Wednesday 05 December 2007 23:11:31 Manuel Bouyer wrote:
> On Wed, Dec 05, 2007 at 06:23:46PM +0100, Christoph Egger wrote:
> > Hi Manuel,
> >
> > Please update pkgsrc's xenkernel3/patches/patch-ba:
> >
> > hg clone http://xenbits.xensource.com/staging/xen-unstable.hg/
> > hg diff -p -r16534 -r16535 >netbsd_dom0_bootfix.diff
> >
> > The content of netbsd_dom0_bootfix.diff should match this:
> > http://xenbits.xensource.com/staging/xen-unstable.hg/rev/c67d024fdd2d
> >
> > Add this diff to above patch-ba.
> >
> >
> > This fixes a NetBSD Dom0 Xen-amd64 boot problem for me, which I have
> > with Xen 3.1.x and Xen-unstable. The Xen crash output is below. The crash
> > happened right before NetBSD loaded the symbol table.
> >
> > The bug was a trampoline mapping leaking into Dom0s address space.
> > And the bug was hit when the Dom0 freed the pagetable.
> >
> > Linux Dom0 uses the builder-provided pagetable as its kernel idle
> > pagetable. So it never frees the pagetable and so never hit the bad path.
> >
> > Oh, I have no idea how you got NetBSD Dom0 booting without that fix.
>
> Yes, I did it see that problem too. It crashes when it tries to unpin the
> L4 page table provided by Xen. I debugged it and came with patch-cy in
> xenkernel3. I now have commited the Xen offical fix, and removed patch-cy
> which should have never been there in the first place (the same file
> patched twice ...) Thanks !

You're welcome.

Oh, I see you updated pkgsrc to Xen 3.1.2. There will also be a Xen 3.1.3.
But I don't know what comes first: 3.1.3 or 3.2.0.
Anyway, whenever 3.2.0 is out, I highly recommend you to package that one,
since this is of much more value for *BSD.

Now that you fetched xen-unstable in order to get the diff for patch-ba,
you can give it a try and give me feedback. :-)
Just run 'gmake' on the top-level directory and enjoy the result.

Apply attached patch to make tools/blktap building, but not functional
on *BSD. I don't submit this one since it has not the quality, but it's a 
start. Applying this on Xen 3.1.2 makes tools/blktap NOT building because
I already got two patches upstream which remove linux specific dependencies
in other files (i.e. #include <linux/fs.h> without actually using it.)

> BTW, I posted this to xen-devel@, but it seems it never went to the list
> (I can't see it in the archives), and got no bounce either. Do you know if
> this list is restricted in some way ?

I am subscribed there but never saw your mail. I am not aware of any
restrictions.

Christoph

--Boundary-00=_x36VHd0hoHSu2UG
Content-Type: text/x-diff;
  charset="iso-8859-1";
  name="tools_blktap.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="tools_blktap.diff"

diff -r 8ba08f2244b2 tools/blktap/drivers/block-aio.c
--- a/tools/blktap/drivers/block-aio.c	Wed Dec 05 17:56:13 2007 +0000
+++ b/tools/blktap/drivers/block-aio.c	Thu Dec 06 11:05:44 2007 +0100
@@ -41,11 +41,18 @@
 #include <sys/statvfs.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
+#if defined(__linux__) || defined(__Linux__)
 #include <linux/fs.h>
+#endif
 #include "tapdisk.h"
 #include "tapaio.h"
 
 #define MAX_AIO_REQS (MAX_REQUESTS * MAX_SEGMENTS_PER_REQ)
+
+/* *BSD has no O_LARGEFILE */
+#ifndef O_LARGEFILE
+#define O_LARGEFILE	0
+#endif
 
 struct pending_aio {
 	td_callback_t cb;
@@ -88,6 +95,7 @@ static int get_image_info(struct td_stat
 	if (S_ISBLK(stat.st_mode)) {
 		/*Accessing block device directly*/
 		s->size = 0;
+#if defined(BLKGETSIZE)
 		if (ioctl(fd,BLKGETSIZE,&s->size)!=0) {
 			DPRINTF("ERR: BLKGETSIZE failed, couldn't stat image");
 			return -EINVAL;
@@ -99,6 +107,7 @@ static int get_image_info(struct td_stat
 			(long long unsigned)s->size);
 
 		/*Get the sector size*/
+#endif
 #if defined(BLKSSZGET)
 		{
 			int arg;
diff -r 8ba08f2244b2 tools/blktap/drivers/block-qcow.c
--- a/tools/blktap/drivers/block-qcow.c	Wed Dec 05 17:56:13 2007 +0000
+++ b/tools/blktap/drivers/block-qcow.c	Thu Dec 06 11:05:44 2007 +0100
@@ -21,6 +21,9 @@
  * furnished to do so, subject to the following conditions:
  */
 
+/* Linux/i386 needs this */
+#define _FILE_OFFSET_BITS	64
+
 #include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
@@ -29,7 +32,9 @@
 #include <sys/statvfs.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
+#if defined(__linux__) || defined(__Linux__)
 #include <linux/fs.h>
+#endif
 #include <string.h>
 #include <zlib.h>
 #include <inttypes.h>
@@ -39,6 +44,11 @@
 #include "aes.h"
 #include "tapdisk.h"
 #include "tapaio.h"
+
+/* *BSD has no O_LARGEFILE */
+#ifndef O_LARGEFILE
+#define O_LARGEFILE	0
+#endif
 
 #if 1
 #define ASSERT(_p) \
@@ -284,11 +294,13 @@ static int get_filesize(char *filename, 
 		fd = open(filename, O_RDONLY);
 		if (fd < 0)
 			return -1;
+#if defined(BLKGETSIZE)
 		if (ioctl(fd,BLKGETSIZE,size)!=0) {
 			printf("Unable to get Block device size\n");
 			close(fd);
 			return -1;
 		}
+#endif
 		close(fd);
 	} else *size = (st->st_size >> SECTOR_SHIFT);	
 	return 0;
@@ -990,8 +1002,8 @@ int tdqcow_open (struct disk_driver *dd,
 	if (!final_cluster)
 		s->fd_end = s->l1_table_offset + l1_table_size;
 	else {
-		s->fd_end = lseek64(fd, 0, SEEK_END);
-		if (s->fd_end == (off64_t)-1)
+		s->fd_end = lseek(fd, 0, SEEK_END);
+		if (s->fd_end == (off_t)-1)
 			goto fail;
 	}
 
@@ -1230,7 +1242,7 @@ int qcow_create(const char *filename, ui
 	DPRINTF("Qcow_create: size %llu\n",(long long unsigned)total_size);
 
 	fd = open(filename, 
-		  O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 
+		  O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
 		  0644);
 	if (fd < 0)
 		return -1;
diff -r 8ba08f2244b2 tools/blktap/drivers/block-ram.c
--- a/tools/blktap/drivers/block-ram.c	Wed Dec 05 17:56:13 2007 +0000
+++ b/tools/blktap/drivers/block-ram.c	Thu Dec 06 11:05:44 2007 +0100
@@ -37,11 +37,18 @@
 #include <sys/statvfs.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
+#if defined(__linux__) || defined(__Linux__)
 #include <linux/fs.h>
+#endif
 #include <string.h>
 #include "tapdisk.h"
 
 #define MAX_DISK_SIZE 1024000 /*500MB disk limit*/
+
+/* *BSD has no O_LARGEFILE */
+#ifndef O_LARGEFILE
+#define O_LARGEFILE	0
+#endif
 
 char *img;
 long int   disksector_size;
@@ -72,6 +79,7 @@ static int get_image_info(struct td_stat
 	if (S_ISBLK(stat.st_mode)) {
 		/*Accessing block device directly*/
 		s->size = 0;
+#if defined(BLKGETSIZE)
 		if (ioctl(fd,BLKGETSIZE,&s->size)!=0) {
 			DPRINTF("ERR: BLKGETSIZE failed, couldn't stat image");
 			return -EINVAL;
@@ -82,6 +90,7 @@ static int get_image_info(struct td_stat
 			(long long unsigned)(s->size << SECTOR_SHIFT),
 			(long long unsigned)s->size);
 
+#endif
 		/*Get the sector size*/
 #if defined(BLKSSZGET)
 		{
diff -r 8ba08f2244b2 tools/blktap/drivers/block-sync.c
--- a/tools/blktap/drivers/block-sync.c	Wed Dec 05 17:56:13 2007 +0000
+++ b/tools/blktap/drivers/block-sync.c	Thu Dec 06 11:05:44 2007 +0100
@@ -37,8 +37,15 @@
 #include <sys/statvfs.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
+#if defined(__linux__) || defined(__Linux__)
 #include <linux/fs.h>
+#endif
 #include "tapdisk.h"
+
+/* *BSD has no O_LARGEFILE */
+#ifndef O_LARGEFILE
+#define O_LARGEFILE	0
+#endif
 
 struct tdsync_state {
 	int fd;
@@ -63,6 +70,7 @@ static int get_image_info(struct td_stat
 	if (S_ISBLK(stat.st_mode)) {
 		/*Accessing block device directly*/
 		s->size = 0;
+#if defined(BLKGETSIZE)
 		if (ioctl(fd,BLKGETSIZE,&s->size)!=0) {
 			DPRINTF("ERR: BLKGETSIZE failed, couldn't stat image");
 			return -EINVAL;
@@ -73,6 +81,7 @@ static int get_image_info(struct td_stat
 			(long long unsigned)(s->size << SECTOR_SHIFT),
 			(long long unsigned)s->size);
 
+#endif
 		/*Get the sector size*/
 #if defined(BLKSSZGET)
 		{
diff -r 8ba08f2244b2 tools/blktap/drivers/block-vmdk.c
--- a/tools/blktap/drivers/block-vmdk.c	Wed Dec 05 17:56:13 2007 +0000
+++ b/tools/blktap/drivers/block-vmdk.c	Thu Dec 06 11:05:44 2007 +0100
@@ -42,10 +42,17 @@
 #include <sys/statvfs.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
+#if defined(__linux__) || defined(__Linux__)
 #include <linux/fs.h>
+#endif
 #include <string.h>
 #include "tapdisk.h"
 #include "bswap.h"
+
+/* *BSD has no O_LARGEFILE */
+#ifndef O_LARGEFILE
+#define O_LARGEFILE	0
+#endif
 
 #define safer_free(_x)       \
   do {                       \
diff -r 8ba08f2244b2 tools/blktap/drivers/img2qcow.c
--- a/tools/blktap/drivers/img2qcow.c	Wed Dec 05 17:56:13 2007 +0000
+++ b/tools/blktap/drivers/img2qcow.c	Thu Dec 06 11:05:44 2007 +0100
@@ -37,7 +37,9 @@
 #include <sys/statvfs.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
+#if defined(__linux__)
 #include <linux/fs.h>
+#endif
 #include <string.h>
 #include "tapdisk.h"
 
@@ -46,6 +48,12 @@
 #else
 #define DFPRINTF(_f, _a...) ((void)0)
 #endif
+
+/* *BSD has no O_LARGEFILE */
+#ifndef O_LARGEFILE
+#define O_LARGEFILE	0
+#endif
+
 
 #define TAPDISK 1
 #define BLOCK_PROCESSSZ 4096
@@ -110,11 +118,13 @@ static int get_image_info(struct td_stat
 	if (S_ISBLK(stat.st_mode)) {
 		/*Accessing block device directly*/
 		s->size = 0;
+#if defined(BLKGETSIZE)
 		if (ioctl(fd,BLKGETSIZE,&s->size)!=0) {
 			DFPRINTF("ERR: BLKGETSIZE failed, "
 				 "couldn't stat image");
 			return -EINVAL;
 		}
+#endif
 
 		DFPRINTF("Image size: \n\tpre sector_shift  [%llu]\n\tpost "
 			"sector_shift [%llu]\n",
diff -r 8ba08f2244b2 tools/blktap/drivers/qcow2raw.c
--- a/tools/blktap/drivers/qcow2raw.c	Wed Dec 05 17:56:13 2007 +0000
+++ b/tools/blktap/drivers/qcow2raw.c	Thu Dec 06 11:05:44 2007 +0100
@@ -37,7 +37,9 @@
 #include <sys/statvfs.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
+#if defined(__linux__)
 #include <linux/fs.h>
+#endif
 #include <string.h>
 #include "tapdisk.h"
 
@@ -45,6 +47,12 @@
 #define DFPRINTF(_f, _a...) fprintf ( stderr, _f , ## _a )
 #else
 #define DFPRINTF(_f, _a...) ((void)0)
+#endif
+
+
+/* *BSD has no O_LARGEFILE */
+#ifndef O_LARGEFILE
+#define O_LARGEFILE 0
 #endif
 
 #define TAPDISK 1
@@ -227,6 +235,7 @@ int main(int argc, char *argv[])
 		}
 
 		if (S_ISBLK(finfo.st_mode)) {
+#if defined(BLKGETSIZE)
 			if(ioctl(fd,BLKGETSIZE,&size)!=0) {
 				DFPRINTF("ERROR: BLKGETSIZE failed, "
 					"couldn't stat image [%s]\n", 
@@ -234,6 +243,9 @@ int main(int argc, char *argv[])
 				close(fd);
 				exit(-1);
 			}
+#else
+			size = 0;
+#endif
 			if (size < ddqcow.td_state->size<<9) {
 				DFPRINTF("ERROR: Not enough space on device "
 					"%s (%lu bytes available, %llu bytes required\n",

--Boundary-00=_x36VHd0hoHSu2UG--