Module Name:    src
Committed By:   jmcneill
Date:           Sat Sep  3 18:42:13 UTC 2011

Modified Files:
        src/sys/arch/usermode/conf: Makefile.usermode files.usermode
        src/sys/arch/usermode/include: thunk.h
        src/sys/arch/usermode/usermode: pmap.c thunk.c

Log Message:
Let NetBSD/usermode build & link on a Linux host:
 - Need to add options CPU_HOST=i386 or CPU_HOST=amd64 for membar & atomic_ops
 - Don't use MAP_* and PROT_* with thunk API since the kernel and host
   might not be the same; add THUNK_MAP_* and THUNK_PROT_* and translate them
 - Add thunk_posix_memalign
 - allocate mem_uvm with thunk_posix_memalign instead of thunk_malloc
 - Fix thunk_mmap callers to always pass either THUNK_MAP_PRIVATE or
   THUNK_MAP_SHARED
 - mkstemp on Linux requires exactly 6 "X" characters at the end of the
   template string, so add an X


To generate a diff of this commit:
cvs rdiff -u -r1.17 -r1.18 src/sys/arch/usermode/conf/Makefile.usermode
cvs rdiff -u -r1.8 -r1.9 src/sys/arch/usermode/conf/files.usermode
cvs rdiff -u -r1.24 -r1.25 src/sys/arch/usermode/include/thunk.h
cvs rdiff -u -r1.52 -r1.53 src/sys/arch/usermode/usermode/pmap.c
cvs rdiff -u -r1.27 -r1.28 src/sys/arch/usermode/usermode/thunk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/usermode/conf/Makefile.usermode
diff -u src/sys/arch/usermode/conf/Makefile.usermode:1.17 src/sys/arch/usermode/conf/Makefile.usermode:1.18
--- src/sys/arch/usermode/conf/Makefile.usermode:1.17	Sat Sep  3 15:00:27 2011
+++ src/sys/arch/usermode/conf/Makefile.usermode	Sat Sep  3 18:42:13 2011
@@ -1,6 +1,11 @@
-# $NetBSD: Makefile.usermode,v 1.17 2011/09/03 15:00:27 jmcneill Exp $
+# $NetBSD: Makefile.usermode,v 1.18 2011/09/03 18:42:13 jmcneill Exp $
 
+OPT_CPU_HOST=			%CPU_HOST%
+.if !empty(OPT_CPU_HOST)
+MACHINE_ARCH=			${OPT_CPU_HOST}
+.else
 MACHINE_ARCH=			usermode
+.endif
 USETOOLS?=			no
 NEED_OWN_INSTALL_TARGET?=	no
 .include <bsd.own.mk>
@@ -15,12 +20,17 @@
 ## (2) compile settings
 ##
 USERMODE_HOSTOS!=uname -s
+USERMODE_HOSTMACHINE!=uname -m
 USERMODE_LIBS=	-lrt
+USERMODE_CPPFLAGS=-U_KERNEL -I/usr/include
+USERMODE_CPPFLAGS+=${CWARNFLAGS} ${NOGCCERROR:D:U-Werror}
+
 .if ${USERMODE_HOSTOS} == "Linux"
 USERMODE_LIBS+=	-lpthread
+CPPFLAGS+=-Dstrtoul=netbsd_strtoul
+CPPFLAGS+=-Dstrtoll=netbsd_strtoll
+CPPFLAGS+=-Dstrtoull=netbsd_strtoull
 .endif
-USERMODE_CPPFLAGS=-U_KERNEL -I/usr/include
-USERMODE_CPPFLAGS+=${CWARNFLAGS} ${NOGCCERROR:D:U-Werror}
 
 DEFCOPTS=	-fno-omit-frame-pointer
 CPPFLAGS+=	-Dusermode -D__NetBSD__ -Wno-unused-but-set-variable
@@ -42,8 +52,8 @@
 ##
 ## (3) libkern and compat
 ##
-KERN_AS=	obj
-COMPAT_AS=	obj
+KERN_AS=	library
+COMPAT_AS=	library
 
 ##
 ## (4) local objects, compile rules, and dependencies

Index: src/sys/arch/usermode/conf/files.usermode
diff -u src/sys/arch/usermode/conf/files.usermode:1.8 src/sys/arch/usermode/conf/files.usermode:1.9
--- src/sys/arch/usermode/conf/files.usermode:1.8	Sat Sep  3 12:28:45 2011
+++ src/sys/arch/usermode/conf/files.usermode	Sat Sep  3 18:42:13 2011
@@ -1,4 +1,4 @@
-# $NetBSD: files.usermode,v 1.8 2011/09/03 12:28:45 jmcneill Exp $
+# $NetBSD: files.usermode,v 1.9 2011/09/03 18:42:13 jmcneill Exp $
 
 maxpartitions 8
 maxusers 8 16 64
@@ -6,6 +6,7 @@
 defparam opt_memsize.h				MEMSIZE
 defflag opt_sdl.h				SDL
 defflag opt_cpu.h				CPU_DEBUG
+defparam opt_cpu.h				CPU_HOST
 defflag opt_urkelvisor.h			URKELVISOR
 
 define	thunkbus { }

Index: src/sys/arch/usermode/include/thunk.h
diff -u src/sys/arch/usermode/include/thunk.h:1.24 src/sys/arch/usermode/include/thunk.h:1.25
--- src/sys/arch/usermode/include/thunk.h:1.24	Sat Sep  3 15:00:28 2011
+++ src/sys/arch/usermode/include/thunk.h	Sat Sep  3 18:42:13 2011
@@ -1,4 +1,4 @@
-/* $NetBSD: thunk.h,v 1.24 2011/09/03 15:00:28 jmcneill Exp $ */
+/* $NetBSD: thunk.h,v 1.25 2011/09/03 18:42:13 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2011 Jared D. McNeill <jmcne...@invisible.ca>
@@ -35,7 +35,6 @@
 #include <sys/fcntl.h>
 #include <sys/ucontext.h>
 #include <sys/signal.h>
-#include <sys/mman.h>
 
 struct thunk_timeval {
 	int64_t tv_sec;
@@ -57,6 +56,17 @@
 	int32_t c_ospeed;
 };
 
+#define THUNK_MAP_ANON		0x0001
+#define THUNK_MAP_FIXED		0x0002
+#define THUNK_MAP_FILE		0x0004
+#define THUNK_MAP_SHARED	0x0010
+#define THUNK_MAP_PRIVATE	0x0020
+
+#define THUNK_PROT_NONE		0x00
+#define THUNK_PROT_READ		0x01
+#define THUNK_PROT_WRITE	0x02
+#define THUNK_PROT_EXEC		0x04
+
 struct aiocb;
 
 int	thunk_setitimer(int, const struct thunk_itimerval *, struct thunk_itimerval *);
@@ -108,6 +118,7 @@
 void *	thunk_mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset);
 int	thunk_munmap(void *addr, size_t len);
 int	thunk_mprotect(void *addr, size_t len, int prot);
+int	thunk_posix_memalign(void **, size_t, size_t);
 
 char *	thunk_getenv(const char *);
 

Index: src/sys/arch/usermode/usermode/pmap.c
diff -u src/sys/arch/usermode/usermode/pmap.c:1.52 src/sys/arch/usermode/usermode/pmap.c:1.53
--- src/sys/arch/usermode/usermode/pmap.c:1.52	Sat Sep  3 12:33:03 2011
+++ src/sys/arch/usermode/usermode/pmap.c	Sat Sep  3 18:42:13 2011
@@ -1,4 +1,4 @@
-/* $NetBSD: pmap.c,v 1.52 2011/09/03 12:33:03 jmcneill Exp $ */
+/* $NetBSD: pmap.c,v 1.53 2011/09/03 18:42:13 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2011 Reinoud Zandijk <rein...@netbsd.org>
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.52 2011/09/03 12:33:03 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.53 2011/09/03 18:42:13 jmcneill Exp $");
 
 #include "opt_memsize.h"
 #include "opt_kmempages.h"
@@ -145,7 +145,9 @@
 
 	/* claim memory with 2 pages more */
 	uvm_len = kmem_len + barrier_len + user_len + barrier_len + 2*PAGE_SIZE;
-	mem_uvm = thunk_malloc(uvm_len);
+	err = thunk_posix_memalign(&mem_uvm, PAGE_SIZE, uvm_len);
+	if (err)
+		panic("pmap_bootstrap: couldn't allocate uvm memory");
 	/* make page aligned */
 	mpos = round_page((vaddr_t) mem_uvm);// + PAGE_SIZE;
 	if (!((void *) mpos >= mem_uvm))
@@ -172,11 +174,11 @@
 	/* protect complete UVM area (---) */
 	addr = thunk_mmap((void*) mem_uvm,
 		uvm_len,
-		PROT_NONE,
-		MAP_ANON | MAP_FIXED,
+		THUNK_PROT_NONE,
+		THUNK_MAP_ANON | THUNK_MAP_FIXED | THUNK_MAP_PRIVATE,
 		-1, 0);
 	if (addr != (void *) mem_uvm)
-		panic("pmap_bootstrap: uvm space protection barrier failed\n");
+		panic("pmap_bootstrap: uvm space protection barrier failed (%p)\n", (void *)addr);
 #endif
 
 	aprint_debug("\nMemory summary\n");
@@ -191,7 +193,7 @@
 	aprint_debug("Creating memory mapped backend\n");
 
 	/* create memory file since mmap/maccess only can be on files */
-	strlcpy(mem_name, "/tmp/netbsd.XXXXX", sizeof(mem_name));
+	strlcpy(mem_name, "/tmp/netbsd.XXXXXX", sizeof(mem_name));
 	mem_fh = thunk_mkstemp(mem_name);
 	if (mem_fh < 0)
 		panic("pmap_bootstrap: can't create memory file\n");
@@ -208,7 +210,7 @@
 	/* protect the current kernel section */
 	/* XXX kernel stack? */
 	err = thunk_mprotect((void *) kmem_k_start, kmem_k_end - kmem_k_start,
-		PROT_READ | PROT_EXEC);
+		THUNK_PROT_READ | THUNK_PROT_EXEC);
 	assert(err == 0);
 
 	/* set up pv_table; bootstrap problem! */
@@ -227,8 +229,8 @@
 	pv_fpos = fpos;
 	pv_table = (struct pv_entry *) kmem_ext_cur_start;
 	addr = thunk_mmap(pv_table, pv_table_size,
-		PROT_READ | PROT_WRITE,
-		MAP_FILE | MAP_FIXED,
+		THUNK_PROT_READ | THUNK_PROT_WRITE,
+		THUNK_MAP_FILE | THUNK_MAP_FIXED | THUNK_MAP_SHARED,
 		mem_fh, pv_fpos);
 	if (addr != (void *) kmem_ext_start)
 		panic("pmap_bootstrap: can't map in pv table\n");
@@ -255,8 +257,8 @@
 
 	pm_fpos = fpos;
 	addr = thunk_mmap(pmap->pm_entries, pm_entries_size,
-		PROT_READ | PROT_WRITE,
-		MAP_FILE | MAP_FIXED,
+		THUNK_PROT_READ | THUNK_PROT_WRITE,
+		THUNK_MAP_FILE | THUNK_MAP_FIXED | THUNK_MAP_SHARED,
 		mem_fh, pm_fpos);
 	if (addr != (void *) pmap->pm_entries)
 		panic("pmap_bootstrap: can't map in pmap entries\n");
@@ -481,11 +483,11 @@
 	/* determine pmap access type (mmap doesnt need to be 1:1 on VM_PROT_) */
 	prot = pv->pv_prot;
 	cur_prot = VM_PROT_NONE;
-	if (pv->pv_mmap_ppl & PROT_READ)
+	if (pv->pv_mmap_ppl & THUNK_PROT_READ)
 		cur_prot |= VM_PROT_READ;
-	if (pv->pv_mmap_ppl & PROT_WRITE)
+	if (pv->pv_mmap_ppl & THUNK_PROT_WRITE)
 		cur_prot |= VM_PROT_WRITE;
-	if (pv->pv_mmap_ppl & PROT_EXEC)
+	if (pv->pv_mmap_ppl & THUNK_PROT_EXEC)
 		cur_prot |= VM_PROT_EXECUTE;
 
 	diff = prot & (prot ^ cur_prot);
@@ -543,7 +545,7 @@
 	void *addr;
 
 	addr = thunk_mmap((void *) va, PAGE_SIZE, pv->pv_mmap_ppl,
-		MAP_FILE | MAP_FIXED | MAP_SHARED,
+		THUNK_MAP_FILE | THUNK_MAP_FIXED | THUNK_MAP_SHARED,
 		mem_fh, pa);
 	aprint_debug("page_activate: (va %p, pa %p, ppl %d) -> %p\n",
 		(void *) va, (void *) pa, pv->pv_mmap_ppl, (void *) addr);
@@ -558,8 +560,8 @@
 	vaddr_t va = pv->pv_lpn * PAGE_SIZE + VM_MIN_ADDRESS; /* L->V */
 	void *addr;
 
-	addr = thunk_mmap((void *) va, PAGE_SIZE, PROT_NONE,
-		MAP_FILE | MAP_FIXED | MAP_SHARED,
+	addr = thunk_mmap((void *) va, PAGE_SIZE, THUNK_PROT_NONE,
+		THUNK_MAP_FILE | THUNK_MAP_FIXED | THUNK_MAP_SHARED,
 		mem_fh, pa);
 	aprint_debug("page_deactivate: (va %p, pa %p, ppl %d) -> %p\n",
 		(void *) va, (void *) pa, pv->pv_mmap_ppl, (void *) addr);
@@ -577,23 +579,23 @@
 	pflags = pv_table[pv->pv_ppn].pv_pflags;
 	vflags = pv_table[pv->pv_ppn].pv_vflags;
 
-	KASSERT(PROT_READ == VM_PROT_READ);
-	KASSERT(PROT_WRITE == VM_PROT_WRITE);
-	KASSERT(PROT_EXEC == VM_PROT_EXECUTE);
+	KASSERT(THUNK_PROT_READ == VM_PROT_READ);
+	KASSERT(THUNK_PROT_WRITE == VM_PROT_WRITE);
+	KASSERT(THUNK_PROT_EXEC == VM_PROT_EXECUTE);
 
 	/* create referenced/modified emulation */
 	if ((pv->pv_prot & VM_PROT_WRITE) &&
 	    (pflags & PV_REFERENCED) && (pflags & PV_MODIFIED))
-		mmap_ppl = PROT_READ | PROT_WRITE;
+		mmap_ppl = THUNK_PROT_READ | THUNK_PROT_WRITE;
 	else if ((pv->pv_prot & (VM_PROT_READ | VM_PROT_EXECUTE)) &&
 		 (pflags & PV_REFERENCED))
-		mmap_ppl = PROT_READ;
+		mmap_ppl = THUNK_PROT_READ;
 	else
-		mmap_ppl = PROT_NONE;
+		mmap_ppl = THUNK_PROT_NONE;
 
 	/* unmanaged pages are special; they dont track r/m */
 	if (vflags & PV_UNMANAGED)
-		mmap_ppl = PROT_READ | PROT_WRITE;
+		mmap_ppl = THUNK_PROT_READ | THUNK_PROT_WRITE;
 
 	pv->pv_mmap_ppl = mmap_ppl;
 }

Index: src/sys/arch/usermode/usermode/thunk.c
diff -u src/sys/arch/usermode/usermode/thunk.c:1.27 src/sys/arch/usermode/usermode/thunk.c:1.28
--- src/sys/arch/usermode/usermode/thunk.c:1.27	Sat Sep  3 15:00:28 2011
+++ src/sys/arch/usermode/usermode/thunk.c	Sat Sep  3 18:42:13 2011
@@ -1,4 +1,4 @@
-/* $NetBSD: thunk.c,v 1.27 2011/09/03 15:00:28 jmcneill Exp $ */
+/* $NetBSD: thunk.c,v 1.28 2011/09/03 18:42:13 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2011 Jared D. McNeill <jmcne...@invisible.ca>
@@ -28,10 +28,11 @@
 
 #include <sys/cdefs.h>
 #ifdef __NetBSD__
-__RCSID("$NetBSD: thunk.c,v 1.27 2011/09/03 15:00:28 jmcneill Exp $");
+__RCSID("$NetBSD: thunk.c,v 1.28 2011/09/03 18:42:13 jmcneill Exp $");
 #endif
 
 #include <sys/types.h>
+#include <sys/mman.h>
 
 #include <aio.h>
 #include <assert.h>
@@ -53,6 +54,10 @@
 #define __arraycount(x)	(sizeof((x)) / sizeof((x)[0]))
 #endif
 
+#ifndef MAP_ANON
+#define MAP_ANON MAP_ANONYMOUS
+#endif
+
 static void
 thunk_to_timeval(const struct thunk_timeval *ttv, struct timeval *tv)
 {
@@ -111,6 +116,40 @@
 	tt->c_ospeed= t->c_ospeed;
 }
 
+static int
+thunk_to_native_prot(int prot)
+{
+	int nprot = PROT_NONE;
+
+	if (prot & THUNK_PROT_READ)
+		nprot |= PROT_READ;
+	if (prot & THUNK_PROT_WRITE)
+		nprot |= PROT_WRITE;
+	if (prot & THUNK_PROT_EXEC)
+		nprot |= PROT_EXEC;
+
+	return nprot;
+}
+
+static int
+thunk_to_native_mapflags(int flags)
+{
+	int nflags = 0;
+
+	if (flags & THUNK_MAP_ANON)
+		nflags |= MAP_ANON;
+	if (flags & THUNK_MAP_FIXED)
+		nflags |= MAP_FIXED;
+	if (flags & THUNK_MAP_FILE)
+		nflags |= MAP_FILE;
+	if (flags & THUNK_MAP_SHARED)
+		nflags |= MAP_SHARED;
+	if (flags & THUNK_MAP_PRIVATE)
+		nflags |= MAP_PRIVATE;
+
+	return nflags;
+}
+
 int
 thunk_setitimer(int which, const struct thunk_itimerval *value,
     struct thunk_itimerval *ovalue)
@@ -392,7 +431,16 @@
 void *
 thunk_mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
 {
-	return mmap(addr, len, prot, flags, fd, offset);
+	int nflags, nprot;
+	void *a;
+
+	nprot = thunk_to_native_prot(prot);
+	nflags = thunk_to_native_mapflags(flags);
+
+	a = mmap(addr, len, nprot, nflags, fd, offset);
+	if (a == (void *)-1)
+		perror("mmap");
+	return a;
 }
 
 int
@@ -404,7 +452,17 @@
 int
 thunk_mprotect(void *addr, size_t len, int prot)
 {
-	return mprotect(addr, len, prot);
+	int nprot;
+
+	nprot = thunk_to_native_prot(prot);
+
+	return mprotect(addr, len, nprot);
+}
+
+int
+thunk_posix_memalign(void **ptr, size_t alignment, size_t size)
+{
+	return posix_memalign(ptr, alignment, size);
 }
 
 char *

Reply via email to