merge yamt-uio_vmspace branch. trunk
authoryamt <yamt@NetBSD.org>
Wed, 01 Mar 2006 12:38:10 +0000
branchtrunk
changeset 143952 9cf9eb9ab26a
parent 143951 7179adc38f22
child 143953 cc22e7c1feb1
merge yamt-uio_vmspace branch. - use vmspace rather than proc or lwp where appropriate. the latter is more natural to specify an address space. (and less likely to be abused for random purposes.) - fix a swdmover race.
sys/arch/algor/algor/bus_dma.c
sys/arch/algor/include/bus.h
sys/arch/alpha/common/bus_dma.c
sys/arch/alpha/common/sgmap_typedep.c
sys/arch/arc/arc/bus_dma.c
sys/arch/arc/include/bus.h
sys/arch/arc/jazz/bus_dma_jazz.c
sys/arch/arc/jazz/jazzdmatlb.c
sys/arch/arc/jazz/jazzdmatlbvar.h
sys/arch/arm/arm32/bus_dma.c
sys/arch/arm/include/bus.h
sys/arch/atari/atari/bus.c
sys/arch/cobalt/cobalt/bus.c
sys/arch/cobalt/include/bus.h
sys/arch/evbppc/evbppc/bus_dma.c
sys/arch/ews4800mips/ews4800mips/bus_dma.c
sys/arch/hp700/hp700/mainbus.c
sys/arch/hpcmips/hpcmips/bus_dma.c
sys/arch/hpcsh/include/bus.h
sys/arch/i386/i386/process_machdep.c
sys/arch/m68k/m68k/bus_dma.c
sys/arch/macppc/macppc/bus_dma.c
sys/arch/mips/include/bus_dma.h
sys/arch/mips/mips/bus_dma.c
sys/arch/mipsco/mipsco/bus_dma.c
sys/arch/mvme68k/mvme68k/bus_dma.c
sys/arch/newsmips/include/bus.h
sys/arch/newsmips/newsmips/bus.c
sys/arch/playstation2/playstation2/bus_dma.c
sys/arch/pmax/include/bus.h
sys/arch/pmax/pmax/bus_dma.c
sys/arch/powerpc/marvell/bus_dma.c
sys/arch/powerpc/powerpc/bus_dma.c
sys/arch/powerpc/powerpc/process_machdep.c
sys/arch/sgimips/include/bus.h
sys/arch/sgimips/sgimips/bus.c
sys/arch/sh5/sh5/bus_dma.c
sys/arch/vax/vax/bus_dma.c
sys/arch/x86/x86/bus_dma.c
sys/coda/coda_vnops.c
sys/compat/common/vfs_syscalls_30.c
sys/compat/common/vfs_syscalls_43.c
sys/compat/darwin/darwin_iohidsystem.c
sys/compat/darwin/darwin_sysctl.c
sys/compat/ibcs2/ibcs2_misc.c
sys/compat/irix/irix_dirent.c
sys/compat/linux/common/linux_file64.c
sys/compat/linux/common/linux_misc.c
sys/compat/netbsd32/netbsd32_fs.c
sys/compat/netbsd32/netbsd32_socket.c
sys/compat/sunos/sunos_misc.c
sys/compat/sunos32/sunos32_misc.c
sys/compat/svr4/svr4_misc.c
sys/compat/svr4_32/svr4_32_misc.c
sys/dev/ata/wd.c
sys/dev/audiobell.c
sys/dev/dkwedge/dk.c
sys/dev/dmover/dmover_io.c
sys/dev/scsipi/scsipi_ioctl.c
sys/dev/usb/ugen.c
sys/dev/usb/urio.c
sys/dev/usb/usb.c
sys/dev/vnd.c
sys/fs/cd9660/cd9660_vnops.c
sys/fs/msdosfs/msdosfs_vnops.c
sys/fs/ntfs/ntfs_vnops.c
sys/fs/smbfs/smbfs_io.c
sys/fs/udf/udf_subr.c
sys/fs/union/union_subr.c
sys/kern/init_sysctl.c
sys/kern/kern_ktrace.c
sys/kern/kern_proc.c
sys/kern/kern_subr.c
sys/kern/kern_systrace.c
sys/kern/sys_generic.c
sys/kern/sys_pipe.c
sys/kern/sys_process.c
sys/kern/sys_socket.c
sys/kern/tty_tty.c
sys/kern/uipc_socket.c
sys/kern/uipc_syscalls.c
sys/kern/vfs_getcwd.c
sys/kern/vfs_lookup.c
sys/kern/vfs_syscalls.c
sys/kern/vfs_vnops.c
sys/kern/vfs_xattr.c
sys/miscfs/fdesc/fdesc_vnops.c
sys/miscfs/genfs/genfs_vnops.c
sys/miscfs/kernfs/kernfs_vnops.c
sys/miscfs/portal/portal_vnops.c
sys/miscfs/procfs/procfs_cmdline.c
sys/miscfs/procfs/procfs_subr.c
sys/miscfs/specfs/spec_vnops.c
sys/netsmb/smb_dev.c
sys/netsmb/smb_trantcp.c
sys/netsmb/subr_mchain.c
sys/nfs/nfs_bio.c
sys/nfs/nfs_boot.c
sys/nfs/nfs_serv.c
sys/nfs/nfs_socket.c
sys/nfs/nfs_subs.c
sys/nfs/nfs_vnops.c
sys/opencrypto/cryptodev.c
sys/sys/proc.h
sys/sys/systm.h
sys/sys/uio.h
sys/ufs/ext2fs/ext2fs_lookup.c
sys/ufs/ext2fs/ext2fs_readwrite.c
sys/ufs/ufs/ufs_extattr.c
sys/ufs/ufs/ufs_quota.c
sys/ufs/ufs/ufs_readwrite.c
sys/ufs/ufs/ufs_vnops.c
sys/uvm/uvm_extern.h
sys/uvm/uvm_map.c
--- a/sys/arch/algor/algor/bus_dma.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/algor/algor/bus_dma.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_dma.c,v 1.15 2005/12/11 12:16:08 christos Exp $	*/
+/*	$NetBSD: bus_dma.c,v 1.16 2006/03/01 12:38:10 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1997, 1998, 2001 The NetBSD Foundation, Inc.
@@ -39,7 +39,7 @@
 
 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
 
-__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.15 2005/12/11 12:16:08 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.16 2006/03/01 12:38:10 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -95,7 +95,7 @@
 	map->_dm_maxmaxsegsz = maxsegsz;
 	map->_dm_boundary = boundary;
 	map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
-	map->_dm_proc = NULL;
+	map->_dm_vmspace = NULL;
 	map->dm_maxsegsz = maxsegsz;
 	map->dm_mapsize = 0;		/* no valid mappings */
 	map->dm_nsegs = 0;
@@ -125,8 +125,8 @@
  */
 static int
 _bus_dmamap_load_buffer(bus_dma_tag_t t, bus_dmamap_t map,
-    void *buf, bus_size_t buflen, struct proc *p, int flags, paddr_t *lastaddrp,
-    int *segp, int first)
+    void *buf, bus_size_t buflen, struct vmspace *vm, int flags,
+    paddr_t *lastaddrp, int *segp, int first)
 {
 	bus_size_t sgsize;
 	bus_addr_t curaddr, lastaddr, baddr, bmask;
@@ -140,8 +140,8 @@
 		/*
 		 * Get the physical address for this segment.
 		 */
-		if (p != NULL)
-			(void) pmap_extract(p->p_vmspace->vm_map.pmap,
+		if (!VMSPACE_IS_KERNEL_P(vm))
+			(void) pmap_extract(vm_map_pmap(&vm->vm_map),
 			    vaddr, &curaddr);
 		else
 			curaddr = kvtophys(vaddr);
@@ -237,6 +237,7 @@
 {
 	paddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -248,13 +249,19 @@
 	if (buflen > map->_dm_size)
 		return (EINVAL);
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	seg = 0;
 	error = _bus_dmamap_load_buffer(t, map, buf, buflen,
-	    p, flags, &lastaddr, &seg, 1);
+	    vm, flags, &lastaddr, &seg, 1);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
 		map->dm_nsegs = seg + 1;
-		map->_dm_proc = p;
+		map->_dm_vmspace = vm;
 
 		/*
 		 * For linear buffers, we support marking the mapping
@@ -301,14 +308,14 @@
 	for (m = m0; m != NULL && error == 0; m = m->m_next) {
 		if (m->m_len == 0)
 			continue;
-		error = _bus_dmamap_load_buffer(t, map,
-		    m->m_data, m->m_len, NULL, flags, &lastaddr, &seg, first);
+		error = _bus_dmamap_load_buffer(t, map, m->m_data, m->m_len,
+	 	    vmspace_kernel(), flags, &lastaddr, &seg, first);
 		first = 0;
 	}
 	if (error == 0) {
 		map->dm_mapsize = m0->m_pkthdr.len;
 		map->dm_nsegs = seg + 1;
-		map->_dm_proc = NULL;	/* always kernel */
+		map->_dm_vmspace = vmspace_kernel();	/* always kernel */
 	}
 	return (error);
 }
@@ -323,7 +330,6 @@
 	paddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -337,15 +343,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_uio: "
-			    "USERSPACE but no proc");
-#endif
-	}
-
 	first = 1;
 	seg = 0;
 	error = 0;
@@ -357,8 +354,8 @@
 		minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
 		addr = (caddr_t)iov[i].iov_base;
 
-		error = _bus_dmamap_load_buffer(t, map,
-		    addr, minlen, p, flags, &lastaddr, &seg, first);
+		error = _bus_dmamap_load_buffer(t, map, addr, minlen,
+		    uio->uio_vmspace, flags, &lastaddr, &seg, first);
 		first = 0;
 
 		resid -= minlen;
@@ -366,7 +363,7 @@
 	if (error == 0) {
 		map->dm_mapsize = uio->uio_resid;
 		map->dm_nsegs = seg + 1;
-		map->_dm_proc = p;
+		map->_dm_vmspace = uio->uio_vmspace;
 	}
 	return (error);
 }
@@ -472,7 +469,8 @@
 	 *
 	 * This should be true the vast majority of the time.
 	 */
-	if (__predict_true(map->_dm_proc == NULL || map->_dm_proc == curproc))
+	if (__predict_true(VMSPACE_IS_KERNEL_P(map->_dm_vmspace) ||
+	    map->_dm_vmspace == curproc->p_vmspace))
 		useindex = 0;
 	else
 		useindex = 1;
--- a/sys/arch/algor/include/bus.h	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/algor/include/bus.h	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus.h,v 1.10 2005/12/11 12:16:08 christos Exp $	*/
+/*	$NetBSD: bus.h,v 1.11 2006/03/01 12:38:10 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1997, 1998, 2000, 2001 The NetBSD Foundation, Inc.
@@ -630,7 +630,7 @@
 	bus_size_t	_dm_maxmaxsegsz; /* fixed largest possible segment */
 	bus_size_t	_dm_boundary;	/* don't cross this */
 	int		_dm_flags;	/* misc. flags */
-	struct proc	*_dm_proc;	/* proc that owns the mapping */
+	struct vmspace	*_dm_vmspace;	/* vmspace that owns the mapping */
 
 	/*
 	 * Private cookie to be used by the DMA back-end.
--- a/sys/arch/alpha/common/bus_dma.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/alpha/common/bus_dma.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: bus_dma.c,v 1.61 2005/12/11 12:16:16 christos Exp $ */
+/* $NetBSD: bus_dma.c,v 1.62 2006/03/01 12:38:10 yamt Exp $ */
 
 /*-
  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
@@ -39,7 +39,7 @@
 
 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
 
-__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.61 2005/12/11 12:16:16 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.62 2006/03/01 12:38:10 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -56,7 +56,7 @@
 #include <machine/intr.h>
 
 int	_bus_dmamap_load_buffer_direct(bus_dma_tag_t,
-	    bus_dmamap_t, void *, bus_size_t, struct proc *, int,
+	    bus_dmamap_t, void *, bus_size_t, struct vmspace *, int,
 	    paddr_t *, int *, int);
 
 extern paddr_t avail_start, avail_end;	/* from pmap.c */
@@ -129,7 +129,7 @@
  */
 int
 _bus_dmamap_load_buffer_direct(bus_dma_tag_t t, bus_dmamap_t map,
-    void *buf, size_t buflen, struct proc *p, int flags, paddr_t *lastaddrp,
+    void *buf, size_t buflen, struct vmspace *vm, int flags, paddr_t *lastaddrp,
     int *segp, int first)
 {
 	bus_size_t sgsize;
@@ -144,9 +144,8 @@
 		/*
 		 * Get the physical address for this segment.
 		 */
-		if (p != NULL)
-			(void) pmap_extract(p->p_vmspace->vm_map.pmap,
-			    vaddr, &curaddr);
+		if (!VMSPACE_IS_KERNEL_P(vm))
+			(void) pmap_extract(vm->vm_map.pmap, vaddr, &curaddr);
 		else
 			curaddr = vtophys(vaddr);
 
@@ -236,6 +235,7 @@
 {
 	paddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -248,9 +248,14 @@
 	if (buflen > map->_dm_size)
 		return (EINVAL);
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
 	seg = 0;
 	error = _bus_dmamap_load_buffer_direct(t, map, buf, buflen,
-	    p, flags, &lastaddr, &seg, 1);
+	    vm, flags, &lastaddr, &seg, 1);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
 		map->dm_nsegs = seg + 1;
@@ -335,8 +340,8 @@
 
 		default:
 			error = _bus_dmamap_load_buffer_direct(t, map,
-			    m->m_data, m->m_len, NULL, flags, &lastaddr,
-			    &seg, first);
+			    m->m_data, m->m_len, vmspace_kernel(), flags,
+			    &lastaddr, &seg, first);
 		}
 		first = 0;
 	}
@@ -363,7 +368,7 @@
 	paddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
+	struct vmspace *vm;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -378,14 +383,7 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp->l_proc;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_uio_direct: "
-			    "USERSPACE but no proc");
-#endif
-	}
+	vm = uio->uio_vmspace;
 
 	first = 1;
 	seg = 0;
@@ -399,7 +397,7 @@
 		addr = (caddr_t)iov[i].iov_base;
 
 		error = _bus_dmamap_load_buffer_direct(t, map,
-		    addr, minlen, p, flags, &lastaddr, &seg, first);
+		    addr, minlen, vm, flags, &lastaddr, &seg, first);
 		first = 0;
 
 		resid -= minlen;
--- a/sys/arch/alpha/common/sgmap_typedep.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/alpha/common/sgmap_typedep.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: sgmap_typedep.c,v 1.32 2005/12/11 12:16:16 christos Exp $ */
+/* $NetBSD: sgmap_typedep.c,v 1.33 2006/03/01 12:38:10 yamt Exp $ */
 
 /*-
  * Copyright (c) 1997, 1998, 2001 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(1, "$NetBSD: sgmap_typedep.c,v 1.32 2005/12/11 12:16:16 christos Exp $");
+__KERNEL_RCSID(1, "$NetBSD: sgmap_typedep.c,v 1.33 2006/03/01 12:38:10 yamt Exp $");
 
 #include "opt_ddb.h"
 
@@ -50,7 +50,7 @@
 
 int			__C(SGMAP_TYPE,_load_buffer)(bus_dma_tag_t,
 			    bus_dmamap_t, void *buf, size_t buflen,
-			    struct proc *, int, int, struct alpha_sgmap *);
+			    struct vmspace *, int, int, struct alpha_sgmap *);
 
 void
 __C(SGMAP_TYPE,_init_spill_page_pte)(void)
@@ -63,7 +63,7 @@
 
 int
 __C(SGMAP_TYPE,_load_buffer)(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
-    size_t buflen, struct proc *p, int flags, int seg,
+    size_t buflen, struct vmspace *vm, int flags, int seg,
     struct alpha_sgmap *sgmap)
 {
 	vaddr_t endva, va = (vaddr_t)buf;
@@ -168,8 +168,8 @@
 	for (; va < endva; va += PAGE_SIZE, pteidx++,
 	     pte = &page_table[pteidx * SGMAP_PTE_SPACING]) {
 		/* Get the physical address for this segment. */
-		if (p != NULL)
-			(void) pmap_extract(p->p_vmspace->vm_map.pmap, va, &pa);
+		if (!VMSPACE_IS_KERNEL_P(vm))
+			(void) pmap_extract(vm->vm_map.pmap, va, &pa);
 		else
 			pa = vtophys(va);
 
@@ -203,6 +203,7 @@
     bus_size_t buflen, struct proc *p, int flags, struct alpha_sgmap *sgmap)
 {
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -219,8 +220,13 @@
 
 	map->_dm_flags |= flags & (BUS_DMA_READ|BUS_DMA_WRITE);
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
 	seg = 0;
-	error = __C(SGMAP_TYPE,_load_buffer)(t, map, buf, buflen, p,
+	error = __C(SGMAP_TYPE,_load_buffer)(t, map, buf, buflen, vm,
 	    flags, seg, sgmap);
 
 	alpha_mb();
@@ -278,7 +284,7 @@
 		if (m->m_len == 0)
 			continue;
 		error = __C(SGMAP_TYPE,_load_buffer)(t, map,
-		    m->m_data, m->m_len, NULL, flags, seg, sgmap);
+		    m->m_data, m->m_len, vmspace_kernel(), flags, seg, sgmap);
 		seg++;
 	}
 
@@ -313,7 +319,7 @@
     int flags, struct alpha_sgmap *sgmap)
 {
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
+	struct vmspace *vm;
 	struct iovec *iov;
 	caddr_t addr;
 	int i, seg, error;
@@ -333,14 +339,7 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp->l_proc;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic(__S(__C(SGMAP_TYPE,_load_uio))
-			    ": USERSPACE but no proc");
-#endif
-	}
+	vm = uio->uio_vmspace;
 
 	seg = 0;
 	error = 0;
@@ -354,7 +353,7 @@
 		addr = (caddr_t)iov[i].iov_base;
 
 		error = __C(SGMAP_TYPE,_load_buffer)(t, map,
-		    addr, minlen, p, flags, seg, sgmap);
+		    addr, minlen, vm, flags, seg, sgmap);
 
 		resid -= minlen;
 	}
--- a/sys/arch/arc/arc/bus_dma.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/arc/arc/bus_dma.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_dma.c,v 1.26 2005/12/11 12:16:37 christos Exp $	*/
+/*	$NetBSD: bus_dma.c,v 1.27 2006/03/01 12:38:10 yamt Exp $	*/
 /*	NetBSD: bus_dma.c,v 1.20 2000/01/10 03:24:36 simonb Exp 	*/
 
 /*-
@@ -39,7 +39,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.26 2005/12/11 12:16:37 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.27 2006/03/01 12:38:10 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -57,7 +57,7 @@
 paddr_t	kvtophys(vaddr_t);	/* XXX */
 
 static int	_bus_dmamap_load_buffer(bus_dma_tag_t, bus_dmamap_t,
-		    void *, bus_size_t, struct proc *, int, paddr_t *,
+		    void *, bus_size_t, struct vmspace *, int, paddr_t *,
 		    int *, int);
 
 extern paddr_t avail_start, avail_end;	/* from pmap.c */
@@ -119,7 +119,7 @@
 	map->_dm_maxmaxsegsz = maxsegsz;
 	map->_dm_boundary = boundary;
 	map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
-	map->_dm_proc = NULL;
+	map->_dm_vmspace = NULL;
 	map->dm_maxsegsz = maxsegsz;
 	map->dm_mapsize = 0;		/* no valid mappings */
 	map->dm_nsegs = 0;
@@ -147,7 +147,7 @@
  */
 static int
 _bus_dmamap_load_buffer(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
-    bus_size_t buflen, struct proc *p, int flags, paddr_t *lastaddrp,
+    bus_size_t buflen, struct vmspace *vm, int flags, paddr_t *lastaddrp,
     int *segp, int first)
 {
 	bus_size_t sgsize;
@@ -163,10 +163,10 @@
 		/*
 		 * Get the physical address for this segment.
 		 */
-		if (p != NULL) {
-			(void)pmap_extract(p->p_vmspace->vm_map.pmap,
+		if (!VMSPACE_IS_KERNEL_P(vm))
+			(void)pmap_extract(vm_map_pmap(&vm->vm_map),
 			    vaddr, &curaddr);
-		} else
+		else
 			curaddr = kvtophys(vaddr);
 
 		/*
@@ -241,6 +241,7 @@
 {
 	paddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -252,13 +253,19 @@
 	if (buflen > map->_dm_size)
 		return EINVAL;
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	seg = 0;
 	error = _bus_dmamap_load_buffer(t, map, buf, buflen,
-	    p, flags, &lastaddr, &seg, 1);
+	    vm, flags, &lastaddr, &seg, 1);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
 		map->dm_nsegs = seg + 1;
-		map->_dm_proc = p;
+		map->_dm_vmspace = vm;
 
 		/*
 		 * For linear buffers, we support marking the mapping
@@ -305,14 +312,14 @@
 	for (m = m0; m != NULL && error == 0; m = m->m_next) {
 		if (m->m_len == 0)
 			continue;
-		error = _bus_dmamap_load_buffer(t, map,
-		    m->m_data, m->m_len, NULL, flags, &lastaddr, &seg, first);
+		error = _bus_dmamap_load_buffer(t, map, m->m_data, m->m_len,
+		    vmspace_kernel(), flags, &lastaddr, &seg, first);
 		first = 0;
 	}
 	if (error == 0) {
 		map->dm_mapsize = m0->m_pkthdr.len;
 		map->dm_nsegs = seg + 1;
-		map->_dm_proc = NULL;	/* always kernel */
+		map->_dm_vmspace = vmspace_kernel();	/* always kernel */
 	}
 	return error;
 }
@@ -327,7 +334,6 @@
 	paddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -341,14 +347,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_uio: USERSPACE but no proc");
-#endif
-	}
-
 	first = 1;
 	seg = 0;
 	error = 0;
@@ -361,7 +359,7 @@
 		addr = (caddr_t)iov[i].iov_base;
 
 		error = _bus_dmamap_load_buffer(t, map, addr, minlen,
-		    p, flags, &lastaddr, &seg, first);
+		    uio->uio_vmspace, flags, &lastaddr, &seg, first);
 		first = 0;
 
 		resid -= minlen;
@@ -369,7 +367,7 @@
 	if (error == 0) {
 		map->dm_mapsize = uio->uio_resid;
 		map->dm_nsegs = seg + 1;
-		map->_dm_proc = p;
+		map->_dm_vmspace = uio->uio_vmspace;
 	}
 	return error;
 }
@@ -475,7 +473,8 @@
 	 *
 	 * This should be true the vast majority of the time.
 	 */
-	if (__predict_true(map->_dm_proc == NULL || map->_dm_proc == curproc))
+	if (__predict_true(VMSPACE_IS_KERNEL_P(map->_dm_vmspace) ||
+	    map->_dm_vmspace == curproc->p_vmspace))
 		useindex = 0;
 	else
 		useindex = 1;
--- a/sys/arch/arc/include/bus.h	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/arc/include/bus.h	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus.h,v 1.21 2006/02/16 20:17:13 perry Exp $	*/
+/*	$NetBSD: bus.h,v 1.22 2006/03/01 12:38:10 yamt Exp $	*/
 /*	NetBSD: bus.h,v 1.27 2000/03/15 16:44:50 drochner Exp 	*/
 /*	$OpenBSD: bus.h,v 1.15 1999/08/11 23:15:21 niklas Exp $	*/
 
@@ -813,7 +813,7 @@
 	bus_size_t	_dm_maxmaxsegsz; /* fixed largest possible segment */
 	bus_size_t	_dm_boundary;	/* don't cross this */
 	int		_dm_flags;	/* misc. flags */
-	struct proc	*_dm_proc;	/* proc that owns the mapping */
+	struct vmspace	*_dm_vmspace;	/* vmspace that owns the mapping */
 
 	/*
 	 * Private cookie to be used by the DMA back-end.
--- a/sys/arch/arc/jazz/bus_dma_jazz.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/arc/jazz/bus_dma_jazz.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_dma_jazz.c,v 1.12 2005/12/11 12:16:39 christos Exp $	*/
+/*	$NetBSD: bus_dma_jazz.c,v 1.13 2006/03/01 12:38:10 yamt Exp $	*/
 
 /*-
  * Copyright (C) 2003 Izumi Tsutsui.
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bus_dma_jazz.c,v 1.12 2005/12/11 12:16:39 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma_jazz.c,v 1.13 2006/03/01 12:38:10 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -49,7 +49,7 @@
 } *jazz_tlbmap_t;
 
 static int	jazz_bus_dmamap_alloc_sgmap(bus_dma_tag_t,
-		    bus_dma_segment_t *, int, bus_size_t, struct proc *, int);
+		    bus_dma_segment_t *, int, bus_size_t, int);
 static void	jazz_bus_dmamap_free_sgmap(bus_dma_tag_t,
 		    bus_dma_segment_t *, int);
 
@@ -88,7 +88,7 @@
 
 static int
 jazz_bus_dmamap_alloc_sgmap(bus_dma_tag_t t, bus_dma_segment_t *segs,
-    int nsegs, bus_size_t boundary, struct proc *p, int flags)
+    int nsegs, bus_size_t boundary, int flags)
 {
 	jazz_dma_pte_t *dmapte;
 	bus_addr_t addr;
@@ -208,10 +208,17 @@
 		/* just use pre-allocated DMA TLB for the buffer */
 		jazz_tlbmap_t tlbmap;
 		bus_size_t off;
+		struct vmspace *vm;
+
+		if (p != NULL) {
+			vm = p->p_vmspace;
+		} else {
+			vm = vmspace_kernel();
+		}
 
 		tlbmap = (jazz_tlbmap_t)map->_dm_cookie;
 		off = jazz_dma_page_offs(buf);
-		jazz_dmatlb_map_va(p, (vaddr_t)buf, buflen, tlbmap->ptebase);
+		jazz_dmatlb_map_va(vm, (vaddr_t)buf, buflen, tlbmap->ptebase);
 
 		map->dm_segs[0].ds_addr = tlbmap->vaddr + off;
 		map->dm_segs[0].ds_len = buflen;
@@ -226,7 +233,7 @@
 	if (error == 0) {
 		/* allocate DMA TLB for each dmamap segment */
 		error = jazz_bus_dmamap_alloc_sgmap(t, map->dm_segs,
-		    map->dm_nsegs, map->_dm_boundary, p, flags);
+		    map->dm_nsegs, map->_dm_boundary, flags);
 	}
 	return error;
 }
@@ -247,7 +254,7 @@
 	error = _bus_dmamap_load_mbuf(t, map, m0, flags);
 	if (error == 0) {
 		error = jazz_bus_dmamap_alloc_sgmap(t, map->dm_segs,
-		    map->dm_nsegs, map->_dm_boundary, NULL, flags);
+		    map->dm_nsegs, map->_dm_boundary, flags);
 	}
 	return error;
 }
@@ -268,9 +275,7 @@
 	error = jazz_bus_dmamap_load_uio(t, map, uio, flags);
 	if (error == 0) {
 		error = jazz_bus_dmamap_alloc_sgmap(t, map->dm_segs,
-		    map->dm_nsegs, map->_dm_boundary,
-		    uio->uio_segflg == UIO_USERSPACE ? uio->uio_lwp->l_proc :
-		    NULL, flags);
+		    map->dm_nsegs, map->_dm_boundary, flags);
 	}
 	return error;
 }
@@ -291,7 +296,7 @@
 	error = _bus_dmamap_load_raw(t, map, segs, nsegs, size, flags);
 	if (error == 0) {
 		error = jazz_bus_dmamap_alloc_sgmap(t, map->dm_segs,
-		    map->dm_nsegs, map->_dm_boundary, NULL, flags);
+		    map->dm_nsegs, map->_dm_boundary, flags);
 	}
 	return error;
 }
--- a/sys/arch/arc/jazz/jazzdmatlb.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/arc/jazz/jazzdmatlb.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: jazzdmatlb.c,v 1.13 2005/12/11 12:16:39 christos Exp $	*/
+/*	$NetBSD: jazzdmatlb.c,v 1.14 2006/03/01 12:38:10 yamt Exp $	*/
 /*	$OpenBSD: dma.c,v 1.5 1998/03/01 16:49:57 niklas Exp $	*/
 
 /*-
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: jazzdmatlb.c,v 1.13 2005/12/11 12:16:39 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: jazzdmatlb.c,v 1.14 2006/03/01 12:38:10 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -142,7 +142,7 @@
  *  the dma control structure.
  */
 void
-jazz_dmatlb_map_va(struct proc *p, vaddr_t va, vsize_t size,
+jazz_dmatlb_map_va(struct vmspace *vm, vaddr_t va, vsize_t size,
     jazz_dma_pte_t *dma_pte)
 {
 	paddr_t pa;
@@ -150,8 +150,8 @@
 	size = jazz_dma_page_round(size + jazz_dma_page_offs(va));
 	va &= JAZZ_DMA_PAGE_NUM;
 	while (size > 0) {
-		if (p != NULL)
-			(void)pmap_extract(p->p_vmspace->vm_map.pmap, va, &pa);
+		if (!VMSPACE_IS_KERNEL_P(vm))
+			(void)pmap_extract(vm_map_pmap(&vm->vm_map), va, &pa);
 		else
 			pa = kvtophys(va);
 
--- a/sys/arch/arc/jazz/jazzdmatlbvar.h	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/arc/jazz/jazzdmatlbvar.h	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: jazzdmatlbvar.h,v 1.3 2005/12/11 12:16:39 christos Exp $	*/
+/*	$NetBSD: jazzdmatlbvar.h,v 1.4 2006/03/01 12:38:10 yamt Exp $	*/
 
 /*-
  * Copyright (C) 2000 Shuichiro URATA.  All rights reserved.
@@ -30,7 +30,7 @@
 jazz_dma_pte_t *jazz_dmatlb_alloc(int npte, bus_size_t boundary,
 	    int flags, bus_addr_t *addr);
 void	jazz_dmatlb_free(bus_addr_t addr, int npte);
-void	jazz_dmatlb_map_va(struct proc *p, vaddr_t va, vsize_t size,
+void	jazz_dmatlb_map_va(struct vmspace *vm, vaddr_t va, vsize_t size,
 	    jazz_dma_pte_t *dma_pte);
 void	jazz_dmatlb_map_pa(paddr_t pa, psize_t size, jazz_dma_pte_t *dma_pte);
 void	jazz_dmatlb_flush(void);
--- a/sys/arch/arm/arm32/bus_dma.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/arm/arm32/bus_dma.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_dma.c,v 1.47 2005/12/24 20:06:47 perry Exp $	*/
+/*	$NetBSD: bus_dma.c,v 1.48 2006/03/01 12:38:10 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
@@ -40,7 +40,7 @@
 #define _ARM32_BUS_DMA_PRIVATE
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.47 2005/12/24 20:06:47 perry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.48 2006/03/01 12:38:10 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -63,7 +63,7 @@
 #include <arm/cpufunc.h>
 
 int	_bus_dmamap_load_buffer(bus_dma_tag_t, bus_dmamap_t, void *,
-	    bus_size_t, struct proc *, int);
+	    bus_size_t, struct vmspace *, int);
 struct arm32_dma_range *_bus_dma_inrange(struct arm32_dma_range *,
 	    int, bus_addr_t);
 
@@ -207,7 +207,7 @@
 	map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
 	map->_dm_origbuf = NULL;
 	map->_dm_buftype = ARM32_BUFTYPE_INVALID;
-	map->_dm_proc = NULL;
+	map->_dm_vmspace = vmspace_kernel();
 	map->dm_maxsegsz = maxsegsz;
 	map->dm_mapsize = 0;		/* no valid mappings */
 	map->dm_nsegs = 0;
@@ -239,7 +239,7 @@
 	map->dm_nsegs = 0;
 	map->_dm_origbuf = NULL;
 	map->_dm_buftype = ARM32_BUFTYPE_INVALID;
-	map->_dm_proc = NULL;
+	map->_dm_vmspace = NULL;
 
 	free(map, M_DMAMAP);
 }
@@ -253,6 +253,7 @@
     bus_size_t buflen, struct proc *p, int flags)
 {
 	int error;
+	struct vmspace *vm;
 
 #ifdef DEBUG_DMA
 	printf("dmamap_load: t=%p map=%p buf=%p len=%lx p=%p f=%d\n",
@@ -269,15 +270,21 @@
 	if (buflen > map->_dm_size)
 		return (EINVAL);
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	/* _bus_dmamap_load_buffer() clears this if we're not... */
 	map->_dm_flags |= ARM32_DMAMAP_COHERENT;
 
-	error = _bus_dmamap_load_buffer(t, map, buf, buflen, p, flags);
+	error = _bus_dmamap_load_buffer(t, map, buf, buflen, vm, flags);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
 		map->_dm_origbuf = buf;
 		map->_dm_buftype = ARM32_BUFTYPE_LINEAR;
-		map->_dm_proc = p;
+		map->_dm_vmspace = vm;
 	}
 #ifdef DEBUG_DMA
 	printf("dmamap_load: error=%d\n", error);
@@ -384,14 +391,14 @@
 
 		default:
 			error = _bus_dmamap_load_buffer(t, map, m->m_data,
-			    m->m_len, NULL, flags);
+			    m->m_len, vmspace_kernel(), flags);
 		}
 	}
 	if (error == 0) {
 		map->dm_mapsize = m0->m_pkthdr.len;
 		map->_dm_origbuf = m0;
 		map->_dm_buftype = ARM32_BUFTYPE_MBUF;
-		map->_dm_proc = NULL;	/* always kernel */
+		map->_dm_vmspace = vmspace_kernel();	/* always kernel */
 	}
 #ifdef DEBUG_DMA
 	printf("dmamap_load_mbuf: error=%d\n", error);
@@ -408,7 +415,6 @@
 {
 	int i, error;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -422,14 +428,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_uio: USERSPACE but no proc");
-#endif
-	}
-
 	/* _bus_dmamap_load_buffer() clears this if we're not... */
 	map->_dm_flags |= ARM32_DMAMAP_COHERENT;
 
@@ -443,7 +441,7 @@
 		addr = (caddr_t)iov[i].iov_base;
 
 		error = _bus_dmamap_load_buffer(t, map, addr, minlen,
-		    p, flags);
+		    uio->uio_vmspace, flags);
 
 		resid -= minlen;
 	}
@@ -451,7 +449,7 @@
 		map->dm_mapsize = uio->uio_resid;
 		map->_dm_origbuf = uio;
 		map->_dm_buftype = ARM32_BUFTYPE_UIO;
-		map->_dm_proc = p;
+		map->_dm_vmspace = uio->uio_vmspace;
 	}
 	return (error);
 }
@@ -488,7 +486,7 @@
 	map->dm_nsegs = 0;
 	map->_dm_origbuf = NULL;
 	map->_dm_buftype = ARM32_BUFTYPE_INVALID;
-	map->_dm_proc = NULL;
+	map->_dm_vmspace = NULL;
 }
 
 static inline void
@@ -702,8 +700,8 @@
 	 * vmspace has not been active since the last time a full
 	 * cache flush was performed, we don't need to do anything.
 	 */
-	if (__predict_false(map->_dm_proc != NULL &&
-	    map->_dm_proc->p_vmspace->vm_map.pmap->pm_cstate.cs_cache_d == 0))
+	if (__predict_false(!VMSPACE_IS_KERNEL_P(map->_dm_vmspace) &&
+	    vm_map_pmap(&map->_dm_vmspace->vm_map)->pm_cstate.cs_cache_d == 0))
 		return;
 
 	switch (map->_dm_buftype) {
@@ -950,7 +948,7 @@
  */
 int
 _bus_dmamap_load_buffer(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
-    bus_size_t buflen, struct proc *p, int flags)
+    bus_size_t buflen, struct vmspace *vm, int flags)
 {
 	bus_size_t sgsize;
 	bus_addr_t curaddr;
@@ -966,10 +964,7 @@
 	    buf, buflen, flags);
 #endif	/* DEBUG_DMA */
 
-	if (p != NULL)
-		pmap = p->p_vmspace->vm_map.pmap;
-	else
-		pmap = pmap_kernel();
+	pmap = vm_map_pmap(&vm->vm_map);
 
 	while (buflen > 0) {
 		/*
--- a/sys/arch/arm/include/bus.h	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/arm/include/bus.h	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus.h,v 1.15 2005/12/11 12:16:46 christos Exp $	*/
+/*	$NetBSD: bus.h,v 1.16 2006/03/01 12:38:11 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2001 The NetBSD Foundation, Inc.
@@ -919,7 +919,7 @@
 
 	void		*_dm_origbuf;	/* pointer to original buffer */
 	int		_dm_buftype;	/* type of buffer */
-	struct proc	*_dm_proc;	/* proc that owns the mapping */
+	struct vmspace	*_dm_vmspace;	/* vmspace that owns the mapping */
 
 	void		*_dm_cookie;	/* cookie for bus-specific functions */
 
--- a/sys/arch/atari/atari/bus.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/atari/atari/bus.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus.c,v 1.41 2005/12/11 12:16:54 christos Exp $	*/
+/*	$NetBSD: bus.c,v 1.42 2006/03/01 12:38:11 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bus.c,v 1.41 2005/12/11 12:16:54 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus.c,v 1.42 2006/03/01 12:38:11 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -59,7 +59,7 @@
 		bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags,
 		paddr_t low, paddr_t high));
 static int  _bus_dmamap_load_buffer __P((bus_dma_tag_t tag, bus_dmamap_t,
-		void *, bus_size_t, struct proc *, int, paddr_t *,
+		void *, bus_size_t, struct vmspace *, int, paddr_t *,
 		int *, int));
 static int  bus_mem_add_mapping __P((bus_space_tag_t t, bus_addr_t bpa,
 		bus_size_t size, int flags, bus_space_handle_t *bsph));
@@ -440,6 +440,7 @@
 {
 	paddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -451,8 +452,14 @@
 	if (buflen > map->_dm_size)
 		return (EINVAL);
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	seg = 0;
-	error = _bus_dmamap_load_buffer(t, map, buf, buflen, p, flags,
+	error = _bus_dmamap_load_buffer(t, map, buf, buflen, vm, flags,
 	    &lastaddr, &seg, 1);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
@@ -497,7 +504,7 @@
 		if (m->m_len == 0)
 			continue;
 		error = _bus_dmamap_load_buffer(t, map, m->m_data, m->m_len,
-		    NULL, flags, &lastaddr, &seg, first);
+		    vmspace_kernel(), flags, &lastaddr, &seg, first);
 		first = 0;
 	}
 	if (error == 0) {
@@ -520,7 +527,6 @@
 	paddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -534,14 +540,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_uio: USERSPACE but no proc");
-#endif
-	}
-
 	first = 1;
 	seg = 0;
 	error = 0;
@@ -554,7 +552,7 @@
 		addr = (caddr_t)iov[i].iov_base;
 
 		error = _bus_dmamap_load_buffer(t, map, addr, minlen,
-		    p, flags, &lastaddr, &seg, first);
+		    uio->uio_vmspace, flags, &lastaddr, &seg, first);
 		first = 0;
 
 		resid -= minlen;
@@ -822,12 +820,12 @@
  * first indicates if this is the first invocation of this function.
  */
 static int
-_bus_dmamap_load_buffer(t, map, buf, buflen, p, flags, lastaddrp, segp, first)
+_bus_dmamap_load_buffer(t, map, buf, buflen, vm, flags, lastaddrp, segp, first)
 	bus_dma_tag_t t;
 	bus_dmamap_t map;
 	void *buf;
 	bus_size_t buflen;
-	struct proc *p;
+	struct vmspace *vm;
 	int flags;
 	paddr_t *lastaddrp;
 	int *segp;
@@ -841,10 +839,7 @@
 
 	offset = t->_displacement;
 
-	if (p != NULL)
-		pmap = p->p_vmspace->vm_map.pmap;
-	else
-		pmap = pmap_kernel();
+	pmap = vm_map_pmap(&vm->vm_map);
 
 	lastaddr = *lastaddrp;
 	bmask = ~(map->_dm_boundary - 1);
--- a/sys/arch/cobalt/cobalt/bus.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/cobalt/cobalt/bus.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus.c,v 1.23 2005/12/11 12:17:05 christos Exp $	*/
+/*	$NetBSD: bus.c,v 1.24 2006/03/01 12:38:11 yamt Exp $	*/
 
 /*
  * Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bus.c,v 1.23 2005/12/11 12:17:05 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus.c,v 1.24 2006/03/01 12:38:11 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -56,7 +56,7 @@
 #include <mips/cache.h>
 
 static int	_bus_dmamap_load_buffer(bus_dmamap_t, void *, bus_size_t,
-				struct proc *, int, vaddr_t *, int *, int);
+				struct vmspace *, int, vaddr_t *, int *, int);
 
 struct cobalt_bus_dma_tag cobalt_default_bus_dma_tag = {
 	_bus_dmamap_create,
@@ -181,7 +181,7 @@
 	map->_dm_maxmaxsegsz = maxsegsz;
 	map->_dm_boundary = boundary;
 	map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
-	map->_dm_proc = NULL;
+	map->_dm_vmspace = NULL;
 	map->dm_maxsegsz = maxsegsz;
 	map->dm_mapsize = 0;		/* no valid mappings */
 	map->dm_nsegs = 0;
@@ -211,12 +211,12 @@
  * first indicates if this is the first invocation of this function.
  */
 int
-_bus_dmamap_load_buffer(map, buf, buflen, p, flags,
+_bus_dmamap_load_buffer(map, buf, buflen, vm, flags,
     lastaddrp, segp, first)
 	bus_dmamap_t map;
 	void *buf;
 	bus_size_t buflen;
-	struct proc *p;
+	struct vmspace *vm;
 	int flags;
 	vaddr_t *lastaddrp;
 	int *segp;
@@ -234,8 +234,8 @@
 		/*
 		 * Get the physical address for this segment.
 		 */
-		if (p != NULL)
-			(void) pmap_extract(p->p_vmspace->vm_map.pmap,
+		if (!VMSPACE_IS_KERNEL_P(vm))
+			(void) pmap_extract(vm_map_pmap(&vm->vm_map),
 			    vaddr, &curaddr);
 		else
 			curaddr = kvtophys(vaddr);
@@ -314,6 +314,7 @@
 {
 	vaddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -325,13 +326,19 @@
 	if (buflen > map->_dm_size)
 		return EINVAL;
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	seg = 0;
 	error = _bus_dmamap_load_buffer(map, buf, buflen,
-	    p, flags, &lastaddr, &seg, 1);
+	    vm, flags, &lastaddr, &seg, 1);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
 		map->dm_nsegs = seg + 1;
-		map->_dm_proc = p;
+		map->_dm_vmspace = vm;
 
 		/*
 		 * For linear buffers, we support marking the mapping
@@ -381,14 +388,14 @@
 	for (m = m0; m != NULL && error == 0; m = m->m_next) {
 		if (m->m_len == 0)
 			continue;
-		error = _bus_dmamap_load_buffer(map,
-		    m->m_data, m->m_len, NULL, flags, &lastaddr, &seg, first);
+		error = _bus_dmamap_load_buffer(map, m->m_data, m->m_len,
+		    vmspace_kernel(), flags, &lastaddr, &seg, first);
 		first = 0;
 	}
 	if (error == 0) {
 		map->dm_mapsize = m0->m_pkthdr.len;
 		map->dm_nsegs = seg + 1;
-		map->_dm_proc = NULL;	/* always kernel */
+		map->_dm_vmspace = vmspace_kernel();	/* always kernel */
 	}
 	return error;
 }
@@ -406,7 +413,6 @@
 	vaddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -420,14 +426,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_uio: USERSPACE but no proc");
-#endif
-	}
-
 	first = 1;
 	seg = 0;
 	error = 0;
@@ -440,7 +438,7 @@
 		addr = (caddr_t)iov[i].iov_base;
 
 		error = _bus_dmamap_load_buffer(map, addr, minlen,
-		    p, flags, &lastaddr, &seg, first);
+		    uio->uio_vmspace, flags, &lastaddr, &seg, first);
 		first = 0;
 
 		resid -= minlen;
@@ -448,7 +446,7 @@
 	if (error == 0) {
 		map->dm_mapsize = uio->uio_resid;
 		map->dm_nsegs = seg + 1;
-		map->_dm_proc = p;
+		map->_dm_vmspace = uio->uio_vmspace;
 	}
 	return error;
 }
@@ -487,7 +485,7 @@
 	map->dm_mapsize = 0;
 	map->dm_nsegs = 0;
 	map->_dm_flags &= ~COBALT_DMAMAP_COHERENT;
-	map->_dm_proc = NULL;
+	map->_dm_vmspace = NULL;
 }
 
 /*
@@ -566,7 +564,8 @@
 	 *
 	 * This should be true the vast majority of the time.
 	 */
-	if (__predict_true(map->_dm_proc == NULL || map->_dm_proc == curproc))
+	if (__predict_true(VMSPACE_IS_KERNEL_P(map->_dm_vmspace) ||
+	    map->_dm_vmspace == curproc->p_vmspace))
 		useindex = 0;
 	else
 		useindex = 1;
--- a/sys/arch/cobalt/include/bus.h	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/cobalt/include/bus.h	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus.h,v 1.14 2006/02/16 20:17:13 perry Exp $	*/
+/*	$NetBSD: bus.h,v 1.15 2006/03/01 12:38:11 yamt Exp $	*/
 
 /*
  * Copyright (c) 1996, 1997, 1998, 2001 The NetBSD Foundation, Inc.
@@ -602,7 +602,7 @@
 	bus_size_t	_dm_maxmaxsegsz; /* fixed largest possible segment */
 	bus_size_t	_dm_boundary;	/* don't cross this */
 	int		_dm_flags;	/* misc. flags */
-	struct proc	*_dm_proc;	/* proc that owns this mapping */
+	struct vmspace	*_dm_vmspace;	/* vmspace that owns this mapping */
 
 	/*
 	 * PUBLIC MEMBERS: these are used by machine-independent code.
--- a/sys/arch/evbppc/evbppc/bus_dma.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/evbppc/evbppc/bus_dma.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_dma.c,v 1.11 2005/12/24 22:45:35 perry Exp $	*/
+/*	$NetBSD: bus_dma.c,v 1.12 2006/03/01 12:38:11 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.11 2005/12/24 22:45:35 perry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.12 2006/03/01 12:38:11 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -55,7 +55,7 @@
 #include <machine/intr.h>
 
 int	_bus_dmamap_load_buffer(bus_dma_tag_t, bus_dmamap_t, void *,
-	    bus_size_t, struct proc *, int, paddr_t *, int *, int);
+	    bus_size_t, struct vmspace *, int, paddr_t *, int *, int);
 
 /*
  * Common function for DMA map creation.  May be called by bus-specific
@@ -123,7 +123,7 @@
  */
 int
 _bus_dmamap_load_buffer(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
-			bus_size_t buflen, struct proc *p, int flags,
+			bus_size_t buflen, struct vmspace *vm, int flags,
 			paddr_t *lastaddrp, int *segp, int first)
 {
 	bus_size_t sgsize;
@@ -138,8 +138,8 @@
 		/*
 		 * Get the physical address for this segment.
 		 */
-		if (p != NULL)
-			(void) pmap_extract(p->p_vmspace->vm_map.pmap,
+		if (!VMSPACE_IS_KERNEL(vm))
+			(void) pmap_extract(vm_map_pmap(&vm->vm_map),
 			    vaddr, (void *)&curaddr);
 		else
 			curaddr = vtophys(vaddr);
@@ -219,6 +219,7 @@
 {
 	paddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -230,8 +231,14 @@
 	if (buflen > map->_dm_size)
 		return (EINVAL);
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	seg = 0;
-	error = _bus_dmamap_load_buffer(t, map, buf, buflen, p, flags,
+	error = _bus_dmamap_load_buffer(t, map, buf, buflen, vm, flags,
 		&lastaddr, &seg, 1);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
@@ -273,7 +280,7 @@
 		if (m->m_len == 0)
 			continue;
 		error = _bus_dmamap_load_buffer(t, map, m->m_data, m->m_len,
-		    NULL, flags, &lastaddr, &seg, first);
+		    vmspace_kernel(), flags, &lastaddr, &seg, first);
 		first = 0;
 	}
 	if (error == 0) {
@@ -293,7 +300,6 @@
 	paddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -307,14 +313,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_uio: USERSPACE but no proc");
-#endif
-	}
-
 	first = 1;
 	seg = 0;
 	error = 0;
@@ -327,7 +325,7 @@
 		addr = (caddr_t)iov[i].iov_base;
 
 		error = _bus_dmamap_load_buffer(t, map, addr, minlen,
-		    p, flags, &lastaddr, &seg, first);
+		    uio->uio_vmspace, flags, &lastaddr, &seg, first);
 		first = 0;
 
 		resid -= minlen;
--- a/sys/arch/ews4800mips/ews4800mips/bus_dma.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/ews4800mips/ews4800mips/bus_dma.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_dma.c,v 1.1 2005/12/29 15:20:08 tsutsui Exp $	*/
+/*	$NetBSD: bus_dma.c,v 1.2 2006/03/01 12:38:11 yamt Exp $	*/
 
 /*
  * Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.1 2005/12/29 15:20:08 tsutsui Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.2 2006/03/01 12:38:11 yamt Exp $");
 
 /* #define	BUS_DMA_DEBUG */
 #include <sys/param.h>
@@ -56,7 +56,7 @@
 
 extern	paddr_t kvtophys(vaddr_t);		/* XXX */
 static int _bus_dmamap_load_buffer(bus_dmamap_t, void *, bus_size_t,
-    struct proc *, int, vaddr_t *, int *, int);
+    struct vmspace *, int, vaddr_t *, int *, int);
 
 struct ews4800mips_bus_dma_tag ews4800mips_default_bus_dma_tag = {
 	_bus_dmamap_create,
@@ -139,7 +139,7 @@
  */
 int
 _bus_dmamap_load_buffer(bus_dmamap_t map, void *buf, bus_size_t buflen,
-    struct proc *p, int flags, vaddr_t *lastaddrp, int *segp, int first)
+    struct vmspace *vm, int flags, vaddr_t *lastaddrp, int *segp, int first)
 {
 	bus_size_t sgsize;
 	bus_addr_t curaddr, lastaddr, baddr, bmask;
@@ -153,8 +153,8 @@
 		/*
 		 * Get the physical address for this segment.
 		 */
-		if (p != NULL)
-			(void) pmap_extract(p->p_vmspace->vm_map.pmap,
+		if (!VMSPACE_IS_KERNEL_P(vm))
+			(void) pmap_extract(vm_map_pmap(&vm->vm_map),
 			    vaddr, (paddr_t *)&curaddr);
 		else
 			curaddr = kvtophys(vaddr);
@@ -228,6 +228,7 @@
 {
 	vaddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -239,9 +240,15 @@
 	if (buflen > map->_dm_size)
 		return EINVAL;
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	seg = 0;
 	error = _bus_dmamap_load_buffer(map, buf, buflen,
-	    p, flags, &lastaddr, &seg, 1);
+	    vm, flags, &lastaddr, &seg, 1);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
 		map->dm_nsegs = seg + 1;
@@ -289,8 +296,8 @@
 	seg = 0;
 	error = 0;
 	for (m = m0; m != NULL && error == 0; m = m->m_next) {
-		error = _bus_dmamap_load_buffer(map,
-		    m->m_data, m->m_len, NULL, flags, &lastaddr, &seg, first);
+		error = _bus_dmamap_load_buffer(map, m->m_data, m->m_len,
+		    vmspace_kernel(), flags, &lastaddr, &seg, first);
 		first = 0;
 	}
 	if (error == 0) {
@@ -310,7 +317,6 @@
 	vaddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	void *addr;
 
@@ -324,14 +330,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_uio: USERSPACE but no proc");
-#endif
-	}
-
 	first = 1;
 	seg = 0;
 	error = 0;
@@ -344,7 +342,7 @@
 		addr = (void *)iov[i].iov_base;
 
 		error = _bus_dmamap_load_buffer(map, addr, minlen,
-		    p, flags, &lastaddr, &seg, first);
+		    uio->uio_vmspace, flags, &lastaddr, &seg, first);
 		first = 0;
 
 		resid -= minlen;
--- a/sys/arch/hp700/hp700/mainbus.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/hp700/hp700/mainbus.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: mainbus.c,v 1.31 2005/12/11 12:17:24 christos Exp $	*/
+/*	$NetBSD: mainbus.c,v 1.32 2006/03/01 12:38:11 yamt Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: mainbus.c,v 1.31 2005/12/11 12:17:24 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: mainbus.c,v 1.32 2006/03/01 12:38:11 yamt Exp $");
 
 #include "locators.h"
 #include "opt_power_switch.h"
@@ -178,7 +178,7 @@
 void mbus_dmamem_unmap(void *, caddr_t, size_t);
 paddr_t mbus_dmamem_mmap(void *, bus_dma_segment_t *, int, off_t, int, int);
 int _bus_dmamap_load_buffer(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
-    bus_size_t buflen, struct proc *p, int flags, paddr_t *lastaddrp, 
+    bus_size_t buflen, struct vmspace *vm, int flags, paddr_t *lastaddrp, 
     int *segp, int first);
 
 int
@@ -905,6 +905,7 @@
 {
 	vaddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -915,8 +916,14 @@
 	if (buflen > map->_dm_size)
 		return (EINVAL);
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	seg = 0;
-	error = _bus_dmamap_load_buffer(NULL, map, buf, buflen, p, flags,
+	error = _bus_dmamap_load_buffer(NULL, map, buf, buflen, vm, flags,
 	    &lastaddr, &seg, 1);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
@@ -957,7 +964,7 @@
 		if (m->m_len == 0)
 			continue;
 		error = _bus_dmamap_load_buffer(NULL, map, m->m_data, m->m_len,
-		    NULL, flags, &lastaddr, &seg, first);
+		    vmspace_kernel(), flags, &lastaddr, &seg, first);
 		first = 0;
 	}
 	if (error == 0) {
@@ -977,7 +984,6 @@
 	vaddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -990,14 +996,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_uio: USERSPACE but no lwp");
-#endif
-	}
-
 	first = 1;
 	seg = 0;
 	error = 0;
@@ -1010,7 +1008,7 @@
 		addr = (caddr_t)iov[i].iov_base;
 
 		error = _bus_dmamap_load_buffer(NULL, map, addr, minlen,
-		    p, flags, &lastaddr, &seg, first);
+		    uio->uio_vmspace, flags, &lastaddr, &seg, first);
 		first = 0;
 
 		resid -= minlen;
@@ -1381,7 +1379,7 @@
 
 int
 _bus_dmamap_load_buffer(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
-    bus_size_t buflen, struct proc *p, int flags, paddr_t *lastaddrp, 
+    bus_size_t buflen, struct vmspace *vm, int flags, paddr_t *lastaddrp, 
     int *segp, int first)
 {
 	bus_size_t sgsize;
@@ -1390,10 +1388,7 @@
 	int seg;
 	pmap_t pmap;
 
-	if (p != NULL)
-		pmap = p->p_vmspace->vm_map.pmap;
-	else
-		pmap = pmap_kernel();
+	pmap = vm_map_pmap(&vm->vm_map);
 
 	lastaddr = *lastaddrp;
 	bmask  = ~(map->_dm_boundary - 1);
--- a/sys/arch/hpcmips/hpcmips/bus_dma.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/hpcmips/hpcmips/bus_dma.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_dma.c,v 1.26 2005/12/11 12:17:33 christos Exp $	*/
+/*	$NetBSD: bus_dma.c,v 1.27 2006/03/01 12:38:11 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.26 2005/12/11 12:17:33 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.27 2006/03/01 12:38:11 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -52,7 +52,7 @@
 #include <machine/bus_dma_hpcmips.h>
 
 static int _hpcmips_bd_map_load_buffer(bus_dmamap_t, void *, bus_size_t,
-    struct proc *, int, vaddr_t *, int *, int);
+    struct vmspace *, int, vaddr_t *, int *, int);
 
 paddr_t	kvtophys(vaddr_t);	/* XXX */
 
@@ -146,7 +146,7 @@
  */
 static int
 _hpcmips_bd_map_load_buffer(bus_dmamap_t mapx, void *buf, bus_size_t buflen,
-    struct proc *p, int flags, vaddr_t *lastaddrp, int *segp, int first)
+    struct vmspace *vm, int flags, vaddr_t *lastaddrp, int *segp, int first)
 {
 	struct bus_dmamap_hpcmips *map = (struct bus_dmamap_hpcmips *)mapx;
 	bus_size_t sgsize;
@@ -161,8 +161,8 @@
 		/*
 		 * Get the physical address for this segment.
 		 */
-		if (p != NULL)
-			(void) pmap_extract(p->p_vmspace->vm_map.pmap,
+		if (!VMSPACE_IS_KERNEL_P(vm))
+			(void) pmap_extract(vm_map_pmap(&vm->vm_map),
 			    vaddr, &curaddr);
 		else
 			curaddr = kvtophys(vaddr);
@@ -237,6 +237,7 @@
 	struct bus_dmamap_hpcmips *map = (struct bus_dmamap_hpcmips *)mapx;
 	vaddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -248,9 +249,15 @@
 	if (buflen > map->_dm_size)
 		return (EINVAL);
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	seg = 0;
 	error = _hpcmips_bd_map_load_buffer(mapx, buf, buflen,
-	    p, flags, &lastaddr, &seg, 1);
+	    vm, flags, &lastaddr, &seg, 1);
 	if (error == 0) {
 		map->bdm.dm_mapsize = buflen;
 		map->bdm.dm_nsegs = seg + 1;
@@ -301,8 +308,8 @@
 	for (m = m0; m != NULL && error == 0; m = m->m_next) {
 		if (m->m_len == 0)
 			continue;
-		error = _hpcmips_bd_map_load_buffer(mapx,
-		    m->m_data, m->m_len, NULL, flags, &lastaddr, &seg, first);
+		error = _hpcmips_bd_map_load_buffer(mapx, m->m_data, m->m_len,
+		    vmspace_kernel(), flags, &lastaddr, &seg, first);
 		first = 0;
 	}
 	if (error == 0) {
@@ -323,7 +330,6 @@
 	vaddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -337,14 +343,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_hpcmips_bd_map_load_uio: USERSPACE but no proc");
-#endif
-	}
-
 	first = 1;
 	seg = 0;
 	error = 0;
@@ -357,7 +355,7 @@
 		addr = (caddr_t)iov[i].iov_base;
 
 		error = _hpcmips_bd_map_load_buffer(mapx, addr, minlen,
-		    p, flags, &lastaddr, &seg, first);
+		    uio->uio_vmspace, flags, &lastaddr, &seg, first);
 		first = 0;
 
 		resid -= minlen;
--- a/sys/arch/hpcsh/include/bus.h	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/hpcsh/include/bus.h	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus.h,v 1.11 2005/12/11 12:17:36 christos Exp $	*/
+/*	$NetBSD: bus.h,v 1.12 2006/03/01 12:38:11 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1997, 1998, 2000, 2001, 2002 The NetBSD Foundation, Inc.
@@ -730,7 +730,7 @@
 	bus_size_t	_dm_maxmaxsegsz; /* fixed largest possible segment */
 	bus_size_t	_dm_boundary;	/* don't cross this */
 	int		_dm_flags;	/* misc. flags */
-	struct proc	*_dm_proc;	/* proc that owns the mapping */
+	struct vmspace	*_dm_vmspace;	/* vmspace that owns the mapping */
 
 	/*
 	 * Private cookie to be used by the DMA back-end.
--- a/sys/arch/i386/i386/process_machdep.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/i386/i386/process_machdep.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: process_machdep.c,v 1.54 2005/12/24 20:07:10 perry Exp $	*/
+/*	$NetBSD: process_machdep.c,v 1.55 2006/03/01 12:38:11 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1998, 2000, 2001 The NetBSD Foundation, Inc.
@@ -59,7 +59,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.54 2005/12/24 20:07:10 perry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.55 2006/03/01 12:38:11 yamt Exp $");
 
 #include "opt_vm86.h"
 #include "npx.h"
@@ -476,16 +476,23 @@
 		if (!process_machdep_validxmmregs(lt->l_proc))
 			return (EINVAL);
 		else {
+			struct vmspace *vm;
+			int error;
+
+			error = proc_vmspace_getref(l->l_proc, &vm);
+			if (error) {
+				return error;
+			}
 			iov.iov_base = addr;
 			iov.iov_len = sizeof(struct xmmregs);
 			uio.uio_iov = &iov;
 			uio.uio_iovcnt = 1;
 			uio.uio_offset = 0;
 			uio.uio_resid = sizeof(struct xmmregs);
-			uio.uio_segflg = UIO_USERSPACE;
 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
-			uio.uio_lwp = l;
-			return (process_machdep_doxmmregs(l, lt, &uio));
+			error = process_machdep_doxmmregs(l, lt, &uio);
+			uvmspace_free(vm);
+			return error;
 		}
 	}
 
--- a/sys/arch/m68k/m68k/bus_dma.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/m68k/m68k/bus_dma.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: bus_dma.c,v 1.16 2005/12/11 12:17:59 christos Exp $ */
+/* $NetBSD: bus_dma.c,v 1.17 2006/03/01 12:38:11 yamt Exp $ */
 
 /*
  * This file was taken from from alpha/common/bus_dma.c
@@ -46,7 +46,7 @@
 
 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
 
-__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.16 2005/12/11 12:17:59 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.17 2006/03/01 12:38:11 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -65,7 +65,7 @@
 #include <m68k/cacheops.h>
 
 int	_bus_dmamap_load_buffer_direct_common __P((bus_dma_tag_t,
-	    bus_dmamap_t, void *, bus_size_t, struct proc *, int,
+	    bus_dmamap_t, void *, bus_size_t, struct vmspace *, int,
 	    paddr_t *, int *, int));
 
 /*
@@ -142,13 +142,13 @@
  * first indicates if this is the first invocation of this function.
  */
 int
-_bus_dmamap_load_buffer_direct_common(t, map, buf, buflen, p, flags,
+_bus_dmamap_load_buffer_direct_common(t, map, buf, buflen, vm, flags,
     lastaddrp, segp, first)
 	bus_dma_tag_t t;
 	bus_dmamap_t map;
 	void *buf;
 	bus_size_t buflen;
-	struct proc *p;
+	struct vmspace *vm;
 	int flags;
 	paddr_t *lastaddrp;
 	int *segp;
@@ -167,8 +167,8 @@
 		/*
 		 * Get the physical address for this segment.
 		 */
-		if (p != NULL)
-			rv = pmap_extract(p->p_vmspace->vm_map.pmap,
+		if (!VMSPACE_IS_KERNEL_P(vm))
+			rv = pmap_extract(vm_map_pmap(&vm->vm_map),
 			    vaddr, &curaddr);
 		else
 			rv = pmap_extract(pmap_kernel(), vaddr, &curaddr);
@@ -253,6 +253,7 @@
 {
 	paddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -264,9 +265,15 @@
 	if (buflen > map->_dm_size)
 		return (EINVAL);
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	seg = 0;
 	error = _bus_dmamap_load_buffer_direct_common(t, map, buf, buflen,
-	    p, flags, &lastaddr, &seg, 1);
+	    vm, flags, &lastaddr, &seg, 1);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
 		map->dm_nsegs = seg + 1;
@@ -310,7 +317,8 @@
 		if (m->m_len == 0)
 			continue;
 		error = _bus_dmamap_load_buffer_direct_common(t, map,
-		    m->m_data, m->m_len, NULL, flags, &lastaddr, &seg, first);
+		    m->m_data, m->m_len, vmspace_kernel(), flags, &lastaddr,
+		    &seg, first);
 		first = 0;
 	}
 	if (error == 0) {
@@ -333,7 +341,6 @@
 	paddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -347,14 +354,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp->l_proc;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_direct_common: USERSPACE but no proc");
-#endif
-	}
-
 	first = 1;
 	seg = 0;
 	error = 0;
@@ -367,7 +366,8 @@
 		addr = (caddr_t)iov[i].iov_base;
 
 		error = _bus_dmamap_load_buffer_direct_common(t, map,
-		    addr, minlen, p, flags, &lastaddr, &seg, first);
+		    addr, minlen, uio->uio_vmspace, flags, &lastaddr, &seg,
+		    first);
 		first = 0;
 
 		resid -= minlen;
--- a/sys/arch/macppc/macppc/bus_dma.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/macppc/macppc/bus_dma.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_dma.c,v 1.33 2005/12/11 12:18:06 christos Exp $	*/
+/*	$NetBSD: bus_dma.c,v 1.34 2006/03/01 12:38:11 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.33 2005/12/11 12:18:06 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.34 2006/03/01 12:38:11 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -55,7 +55,7 @@
 #include <machine/intr.h>
 
 int	_bus_dmamap_load_buffer __P((bus_dma_tag_t, bus_dmamap_t, void *,
-	    bus_size_t, struct proc *, int, paddr_t *, int *, int));
+	    bus_size_t, struct vmspace *vm, int, paddr_t *, int *, int));
 
 /*
  * Common function for DMA map creation.  May be called by bus-specific
@@ -129,12 +129,12 @@
  * first indicates if this is the first invocation of this function.
  */
 int
-_bus_dmamap_load_buffer(t, map, buf, buflen, p, flags, lastaddrp, segp, first)
+_bus_dmamap_load_buffer(t, map, buf, buflen, vm, flags, lastaddrp, segp, first)
 	bus_dma_tag_t t;
 	bus_dmamap_t map;
 	void *buf;
 	bus_size_t buflen;
-	struct proc *p;
+	struct vmspace *vm;
 	int flags;
 	paddr_t *lastaddrp;
 	int *segp;
@@ -152,8 +152,8 @@
 		/*
 		 * Get the physical address for this segment.
 		 */
-		if (p != NULL)
-			(void) pmap_extract(p->p_vmspace->vm_map.pmap,
+		if (!VMSPACE_IS_KERNEL_P(vm))
+			(void) pmap_extract(vm_map_pmap(&vm->vm_map),
 			    vaddr, (void *)&curaddr);
 		else
 			curaddr = vtophys(vaddr);
@@ -238,6 +238,7 @@
 {
 	paddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -249,8 +250,14 @@
 	if (buflen > map->_dm_size)
 		return (EINVAL);
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	seg = 0;
-	error = _bus_dmamap_load_buffer(t, map, buf, buflen, p, flags,
+	error = _bus_dmamap_load_buffer(t, map, buf, buflen, vm, flags,
 		&lastaddr, &seg, 1);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
@@ -295,7 +302,7 @@
 		if (m->m_len == 0)
 			continue;
 		error = _bus_dmamap_load_buffer(t, map, m->m_data, m->m_len,
-		    NULL, flags, &lastaddr, &seg, first);
+		    vmspace_kernel(), flags, &lastaddr, &seg, first);
 		first = 0;
 	}
 	if (error == 0) {
@@ -318,7 +325,6 @@
 	paddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -332,14 +338,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_uio: USERSPACE but no proc");
-#endif
-	}
-
 	first = 1;
 	seg = 0;
 	error = 0;
@@ -352,7 +350,7 @@
 		addr = (caddr_t)iov[i].iov_base;
 
 		error = _bus_dmamap_load_buffer(t, map, addr, minlen,
-		    p, flags, &lastaddr, &seg, first);
+		    uio->uio_vmspace, flags, &lastaddr, &seg, first);
 		first = 0;
 
 		resid -= minlen;
--- a/sys/arch/mips/include/bus_dma.h	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/mips/include/bus_dma.h	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: bus_dma.h,v 1.5 2005/12/11 12:18:09 christos Exp $ */
+/* $NetBSD: bus_dma.h,v 1.6 2006/03/01 12:38:11 yamt Exp $ */
 
 /*-
  * Copyright (c) 1997, 1998, 2000, 2001 The NetBSD Foundation, Inc.
@@ -210,7 +210,7 @@
 	bus_size_t	_dm_maxmaxsegsz; /* fixed largest possible segment */
 	bus_size_t	_dm_boundary;	/* don't cross this */
 	int		_dm_flags;	/* misc. flags */
-	struct proc	*_dm_proc;	/* proc that owns the mapping */
+	struct vmspace	*_dm_vmspace;	/* vmspace that owns the mapping */
 
 	/*
 	 * Private cookie to be used by the DMA back-end.
--- a/sys/arch/mips/mips/bus_dma.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/mips/mips/bus_dma.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_dma.c,v 1.16 2005/12/11 12:18:09 christos Exp $	*/
+/*	$NetBSD: bus_dma.c,v 1.17 2006/03/01 12:38:11 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1997, 1998, 2001 The NetBSD Foundation, Inc.
@@ -39,7 +39,7 @@
 
 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
 
-__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.16 2005/12/11 12:18:09 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.17 2006/03/01 12:38:11 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -95,7 +95,7 @@
 	map->_dm_maxmaxsegsz = maxsegsz;
 	map->_dm_boundary = boundary;
 	map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
-	map->_dm_proc = NULL;
+	map->_dm_vmspace = NULL;
 	map->dm_maxsegsz = maxsegsz;
 	map->dm_mapsize = 0;		/* no valid mappings */
 	map->dm_nsegs = 0;
@@ -125,8 +125,8 @@
  */
 static int
 _bus_dmamap_load_buffer(bus_dma_tag_t t, bus_dmamap_t map,
-    void *buf, bus_size_t buflen, struct proc *p, int flags, paddr_t *lastaddrp,
-    int *segp, int first)
+    void *buf, bus_size_t buflen, struct vmspace *vm, int flags,
+    paddr_t *lastaddrp, int *segp, int first)
 {
 	bus_size_t sgsize;
 	bus_size_t bmask;
@@ -141,9 +141,9 @@
 		/*
 		 * Get the physical address for this segment.
 		 */
-		if (p != NULL)
-			(void) pmap_extract(p->p_vmspace->vm_map.pmap,
-			    vaddr, &curaddr);
+		if (!VMSPACE_IS_KERNEL_P(vm))
+			(void) pmap_extract(vm_map_pmap(&vm->vm_map), vaddr,
+			    &curaddr);
 		else
 			curaddr = kvtophys(vaddr);
 
@@ -238,6 +238,7 @@
 {
 	paddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -249,13 +250,19 @@
 	if (buflen > map->_dm_size)
 		return (EINVAL);
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	seg = 0;
 	error = _bus_dmamap_load_buffer(t, map, buf, buflen,
-	    p, flags, &lastaddr, &seg, 1);
+	    vm, flags, &lastaddr, &seg, 1);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
 		map->dm_nsegs = seg + 1;
-		map->_dm_proc = p;
+		map->_dm_vmspace = vm;
 
 		/*
 		 * For linear buffers, we support marking the mapping
@@ -302,14 +309,14 @@
 	for (m = m0; m != NULL && error == 0; m = m->m_next) {
 		if (m->m_len == 0)
 			continue;
-		error = _bus_dmamap_load_buffer(t, map,
-		    m->m_data, m->m_len, NULL, flags, &lastaddr, &seg, first);
+		error = _bus_dmamap_load_buffer(t, map, m->m_data, m->m_len,
+		    vmspace_kernel(), flags, &lastaddr, &seg, first);
 		first = 0;
 	}
 	if (error == 0) {
 		map->dm_mapsize = m0->m_pkthdr.len;
 		map->dm_nsegs = seg + 1;
-		map->_dm_proc = NULL;	/* always kernel */
+		map->_dm_vmspace = vmspace_kernel();	/* always kernel */
 	}
 	return (error);
 }
@@ -324,7 +331,6 @@
 	paddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -338,14 +344,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_uio: USERSPACE but no proc");
-#endif
-	}
-
 	first = 1;
 	seg = 0;
 	error = 0;
@@ -357,8 +355,8 @@
 		minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
 		addr = (caddr_t)iov[i].iov_base;
 
-		error = _bus_dmamap_load_buffer(t, map,
-		    addr, minlen, p, flags, &lastaddr, &seg, first);
+		error = _bus_dmamap_load_buffer(t, map, addr, minlen,
+		    uio->uio_vmspace, flags, &lastaddr, &seg, first);
 		first = 0;
 
 		resid -= minlen;
@@ -366,7 +364,7 @@
 	if (error == 0) {
 		map->dm_mapsize = uio->uio_resid;
 		map->dm_nsegs = seg + 1;
-		map->_dm_proc = p;
+		map->_dm_vmspace = uio->uio_vmspace;
 	}
 	return (error);
 }
@@ -472,8 +470,8 @@
 	 *
 	 * This should be true the vast majority of the time.
 	 */
-	if (__predict_true(map->_dm_proc == NULL || 
-		map->_dm_proc == curlwp->l_proc))
+	if (__predict_true(VMSPACE_IS_KERNEL_P(map->_dm_vmspace) ||
+	    map->_dm_vmspace == curproc->p_vmspace))
 		useindex = 0;
 	else
 		useindex = 1;
--- a/sys/arch/mipsco/mipsco/bus_dma.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/mipsco/mipsco/bus_dma.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_dma.c,v 1.18 2005/12/11 12:18:13 christos Exp $	*/
+/*	$NetBSD: bus_dma.c,v 1.19 2006/03/01 12:38:11 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.18 2005/12/11 12:18:13 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.19 2006/03/01 12:38:11 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -56,7 +56,7 @@
 paddr_t	kvtophys __P((vaddr_t));	/* XXX */
 
 static int	_bus_dmamap_load_buffer __P((bus_dma_tag_t, bus_dmamap_t,
-		    void *, bus_size_t, struct proc *, int, paddr_t *,
+		    void *, bus_size_t, struct vmspace *, int, paddr_t *,
 		    int *, int));
 
 extern paddr_t avail_start, avail_end;	/* from pmap.c */
@@ -153,12 +153,12 @@
  * first indicates if this is the first invocation of this function.
  */
 static int
-_bus_dmamap_load_buffer(t, map, buf, buflen, p, flags, lastaddrp, segp, first)
+_bus_dmamap_load_buffer(t, map, buf, buflen, vm, flags, lastaddrp, segp, first)
 	bus_dma_tag_t t;
 	bus_dmamap_t map;
 	void *buf;
 	bus_size_t buflen;
-	struct proc *p;
+	struct vmspace *vm;
 	int flags;
 	paddr_t *lastaddrp;
 	int *segp;
@@ -177,10 +177,10 @@
 		/*
 		 * Get the physical address for this segment.
 		 */
-		if (p != NULL) {
-			(void) pmap_extract(p->p_vmspace->vm_map.pmap,
+		if (!VMSPACE_IS_KERNEL_P(vm))
+			(void) pmap_extract(vm_map_pmap(&vm->vm_map),
 			    vaddr, &curaddr);
-		} else
+		else
 			curaddr = kvtophys(vaddr);
 
 		/*
@@ -260,6 +260,7 @@
 {
 	paddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -271,9 +272,15 @@
 	if (buflen > map->_dm_size)
 		return (EINVAL);
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	seg = 0;
 	error = _bus_dmamap_load_buffer(t, map, buf, buflen,
-	    p, flags, &lastaddr, &seg, 1);
+	    vm, flags, &lastaddr, &seg, 1);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
 		map->dm_nsegs = seg + 1;
@@ -326,8 +333,8 @@
 	for (m = m0; m != NULL && error == 0; m = m->m_next) {
 		if (m->m_len == 0)
 			continue;
-		error = _bus_dmamap_load_buffer(t, map,
-		    m->m_data, m->m_len, NULL, flags, &lastaddr, &seg, first);
+		error = _bus_dmamap_load_buffer(t, map, m->m_data, m->m_len,
+		    vmspace_kernel(), flags, &lastaddr, &seg, first);
 		first = 0;
 	}
 	if (error == 0) {
@@ -350,7 +357,6 @@
 	paddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -364,14 +370,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_uio: USERSPACE but no proc");
-#endif
-	}
-
 	first = 1;
 	seg = 0;
 	error = 0;
@@ -384,7 +382,7 @@
 		addr = (caddr_t)iov[i].iov_base;
 
 		error = _bus_dmamap_load_buffer(t, map, addr, minlen,
-		    p, flags, &lastaddr, &seg, first);
+		    uio->uio_vmspace, flags, &lastaddr, &seg, first);
 		first = 0;
 
 		resid -= minlen;
--- a/sys/arch/mvme68k/mvme68k/bus_dma.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/mvme68k/mvme68k/bus_dma.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: bus_dma.c,v 1.30 2005/12/11 12:18:17 christos Exp $	*/
+/* $NetBSD: bus_dma.c,v 1.31 2006/03/01 12:38:11 yamt Exp $	*/
 
 /*
  * This file was taken from from next68k/dev/bus_dma.c, which was originally
@@ -46,7 +46,7 @@
 
 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
 
-__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.30 2005/12/11 12:18:17 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.31 2006/03/01 12:38:11 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -68,7 +68,7 @@
 extern	phys_ram_seg_t mem_clusters[];
 
 int	_bus_dmamap_load_buffer_direct_common __P((bus_dma_tag_t,
-	    bus_dmamap_t, void *, bus_size_t, struct proc *, int,
+	    bus_dmamap_t, void *, bus_size_t, struct vmspace *, int,
 	    paddr_t *, int *, int));
 
 /*
@@ -142,13 +142,13 @@
  * first indicates if this is the first invocation of this function.
  */
 int
-_bus_dmamap_load_buffer_direct_common(t, map, buf, buflen, p, flags,
+_bus_dmamap_load_buffer_direct_common(t, map, buf, buflen, vm, flags,
     lastaddrp, segp, first)
 	bus_dma_tag_t t;
 	bus_dmamap_t map;
 	void *buf;
 	bus_size_t buflen;
-	struct proc *p;
+	struct vmspace *vm;
 	int flags;
 	paddr_t *lastaddrp;
 	int *segp;
@@ -166,17 +166,9 @@
 		/*
 		 * Get the physical address for this segment.
 		 */
-		if (p != NULL) {
-			(void) pmap_extract(p->p_vmspace->vm_map.pmap,
-			    vaddr, &curaddr);
-			cacheable =
-			    _pmap_page_is_cacheable(p->p_vmspace->vm_map.pmap,
-				vaddr);
-		} else {
-			(void) pmap_extract(pmap_kernel(),vaddr, &curaddr);
-			cacheable =
-			    _pmap_page_is_cacheable(pmap_kernel(), vaddr);
-		}
+		(void) pmap_extract(vm_map_pmap(&vm->vm_map), vaddr, &curaddr);
+		cacheable = _pmap_page_is_cacheable(vm_map_pmap(&vm->vm_map),
+		    vaddr);
 
 		if (cacheable)
 			coherent = 0;
@@ -268,6 +260,7 @@
 {
 	paddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -279,9 +272,15 @@
 	if (buflen > map->_dm_size)
 		return (EINVAL);
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	seg = 0;
 	error = _bus_dmamap_load_buffer_direct_common(t, map, buf, buflen,
-	    p, flags, &lastaddr, &seg, 1);
+	    vm, flags, &lastaddr, &seg, 1);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
 		map->dm_nsegs = seg + 1;
@@ -325,7 +324,8 @@
 		if (m->m_len == 0)
 			continue;
 		error = _bus_dmamap_load_buffer_direct_common(t, map,
-		    m->m_data, m->m_len, NULL, flags, &lastaddr, &seg, first);
+		    m->m_data, m->m_len, vmspace_kernel(), flags, &lastaddr,
+		    &seg, first);
 		first = 0;
 	}
 	if (error == 0) {
@@ -348,7 +348,6 @@
 	paddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -362,14 +361,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp->l_proc;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_direct_common: USERSPACE but no proc");
-#endif
-	}
-
 	first = 1;
 	seg = 0;
 	error = 0;
@@ -382,7 +373,8 @@
 		addr = (caddr_t)iov[i].iov_base;
 
 		error = _bus_dmamap_load_buffer_direct_common(t, map,
-		    addr, minlen, p, flags, &lastaddr, &seg, first);
+		    addr, minlen, uio->uio_vmspace, flags, &lastaddr, &seg,
+		    first);
 		first = 0;
 
 		resid -= minlen;
--- a/sys/arch/newsmips/include/bus.h	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/newsmips/include/bus.h	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus.h,v 1.14 2006/02/16 20:17:14 perry Exp $	*/
+/*	$NetBSD: bus.h,v 1.15 2006/03/01 12:38:11 yamt Exp $	*/
 
 /*
  * Copyright (c) 1996, 1997, 1998, 2001 The NetBSD Foundation, Inc.
@@ -614,7 +614,7 @@
 	int		_dm_flags;	/* misc. flags */
 	int		_dm_maptbl;	/* DMA mapping table index */
 	int		_dm_maptblcnt;	/* number of DMA mapping table */
-	struct proc	*_dm_proc;	/* proc that owns the mapping */
+	struct vmspace	*_dm_vmspace;	/* vmspace that owns the mapping */
 
 	/*
 	 * PUBLIC MEMBERS: these are used by machine-independent code.
--- a/sys/arch/newsmips/newsmips/bus.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/newsmips/newsmips/bus.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus.c,v 1.22 2005/12/11 12:18:25 christos Exp $	*/
+/*	$NetBSD: bus.c,v 1.23 2006/03/01 12:38:11 yamt Exp $	*/
 
 /*
  * Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bus.c,v 1.22 2005/12/11 12:18:25 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus.c,v 1.23 2006/03/01 12:38:11 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -57,7 +57,7 @@
 #include <mips/cache.h>
 
 static int	_bus_dmamap_load_buffer(bus_dmamap_t, void *, bus_size_t,
-				struct proc *, int, vaddr_t *, int *, int);
+				struct vmspace *, int, vaddr_t *, int *, int);
 
 struct newsmips_bus_dma_tag newsmips_default_bus_dma_tag = {
 	_bus_dmamap_create,
@@ -173,7 +173,7 @@
 	map->_dm_maxmaxsegsz = maxsegsz;
 	map->_dm_boundary = boundary;
 	map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
-	map->_dm_proc = NULL;
+	map->_dm_vmspace = NULL;
 	map->dm_maxsegsz = maxsegsz;
 	map->dm_mapsize = 0;		/* no valid mappings */
 	map->dm_nsegs = 0;
@@ -203,7 +203,7 @@
  */
 int
 _bus_dmamap_load_buffer(bus_dmamap_t map, void *buf, bus_size_t buflen,
-    struct proc *p, int flags, vaddr_t *lastaddrp, int *segp, int first)
+    struct vmspace *vm, int flags, vaddr_t *lastaddrp, int *segp, int first)
 {
 	bus_size_t sgsize;
 	bus_addr_t curaddr, lastaddr, baddr, bmask;
@@ -217,8 +217,8 @@
 		/*
 		 * Get the physical address for this segment.
 		 */
-		if (p != NULL)
-			(void) pmap_extract(p->p_vmspace->vm_map.pmap,
+		if (!VMSPACE_IS_KERNEL_P(vm))
+			(void) pmap_extract(vm_map_pmap(&vm->vm_map),
 			    vaddr, &curaddr);
 		else
 			curaddr = kvtophys(vaddr);
@@ -292,6 +292,7 @@
 {
 	vaddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -303,13 +304,19 @@
 	if (buflen > map->_dm_size)
 		return EINVAL;
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	seg = 0;
 	error = _bus_dmamap_load_buffer(map, buf, buflen,
-	    p, flags, &lastaddr, &seg, 1);
+	    vm, flags, &lastaddr, &seg, 1);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
 		map->dm_nsegs = seg + 1;
-		map->_dm_proc = p;
+		map->_dm_vmspace = vm;
 
 		/*
 		 * For linear buffers, we support marking the mapping
@@ -356,14 +363,14 @@
 	for (m = m0; m != NULL && error == 0; m = m->m_next) {
 		if (m->m_len == 0)
 			continue;
-		error = _bus_dmamap_load_buffer(map,
-		    m->m_data, m->m_len, NULL, flags, &lastaddr, &seg, first);
+		error = _bus_dmamap_load_buffer(map, m->m_data, m->m_len,
+		    vmspace_kernel(), flags, &lastaddr, &seg, first);
 		first = 0;
 	}
 	if (error == 0) {
 		map->dm_mapsize = m0->m_pkthdr.len;
 		map->dm_nsegs = seg + 1;
-		map->_dm_proc = NULL;	/* always kernel */
+		map->_dm_vmspace = vmspace_kernel();	/* always kernel */
 	}
 	return error;
 }
@@ -378,7 +385,6 @@
 	vaddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -392,14 +398,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_uio: USERSPACE but no proc");
-#endif
-	}
-
 	first = 1;
 	seg = 0;
 	error = 0;
@@ -412,7 +410,7 @@
 		addr = (caddr_t)iov[i].iov_base;
 
 		error = _bus_dmamap_load_buffer(map, addr, minlen,
-		    p, flags, &lastaddr, &seg, first);
+		    uio->uio_vmspace, flags, &lastaddr, &seg, first);
 		first = 0;
 
 		resid -= minlen;
@@ -420,7 +418,7 @@
 	if (error == 0) {
 		map->dm_mapsize = uio->uio_resid;
 		map->dm_nsegs = seg + 1;
-		map->_dm_proc = p;
+		map->_dm_vmspace = uio->uio_vmspace;
 	}
 	return error;
 }
@@ -451,7 +449,7 @@
 	map->dm_mapsize = 0;
 	map->dm_nsegs = 0;
 	map->_dm_flags &= ~NEWSMIPS_DMAMAP_COHERENT;
-	map->_dm_proc = NULL;
+	map->_dm_vmspace = NULL;
 }
 
 #ifdef MIPS1
@@ -630,7 +628,8 @@
 	 *
 	 * This should be true the vast majority of the time.
 	 */
-	if (__predict_true(map->_dm_proc == NULL || map->_dm_proc == curproc))
+	if (__predict_true(VMSPACE_IS_KERNEL_P(map->_dm_vmspace) ||
+	    map->_dm_vmspace == curproc->p_vmspace))
 		useindex = 0;
 	else
 		useindex = 1;
--- a/sys/arch/playstation2/playstation2/bus_dma.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/playstation2/playstation2/bus_dma.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_dma.c,v 1.13 2005/12/11 12:18:36 christos Exp $	*/
+/*	$NetBSD: bus_dma.c,v 1.14 2006/03/01 12:38:11 yamt Exp $	*/
 
 /*
  * Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.13 2005/12/11 12:18:36 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.14 2006/03/01 12:38:11 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/malloc.h>
@@ -56,7 +56,7 @@
 
 extern	paddr_t kvtophys(vaddr_t);		/* XXX */
 static int _bus_dmamap_load_buffer(bus_dmamap_t, void *, bus_size_t,
-    struct proc *, int, vaddr_t *, int *, int);
+    struct vmspace *, int, vaddr_t *, int *, int);
 
 struct playstation2_bus_dma_tag playstation2_default_bus_dma_tag = {
 	_bus_dmamap_create,
@@ -139,7 +139,7 @@
  */
 int
 _bus_dmamap_load_buffer(bus_dmamap_t map, void *buf, bus_size_t buflen,
-    struct proc *p, int flags, vaddr_t *lastaddrp, int *segp, int first)
+    struct vmspace *vm, int flags, vaddr_t *lastaddrp, int *segp, int first)
 {
 	bus_size_t sgsize;
 	bus_addr_t curaddr, lastaddr, baddr, bmask;
@@ -153,8 +153,8 @@
 		/*
 		 * Get the physical address for this segment.
 		 */
-		if (p != NULL)
-			(void) pmap_extract(p->p_vmspace->vm_map.pmap,
+		if (!VMSPACE_IS_KERNEL_P(vm))
+			(void) pmap_extract(vm_map_pmap(&vm->vm_map),
 			    vaddr, (paddr_t *)&curaddr);
 		else
 			curaddr = kvtophys(vaddr);
@@ -228,6 +228,7 @@
 {
 	vaddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -239,9 +240,15 @@
 	if (buflen > map->_dm_size)
 		return EINVAL;
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	seg = 0;
 	error = _bus_dmamap_load_buffer(map, buf, buflen,
-	    p, flags, &lastaddr, &seg, 1);
+	    vm, flags, &lastaddr, &seg, 1);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
 		map->dm_nsegs = seg + 1;
@@ -291,8 +298,8 @@
 	for (m = m0; m != NULL && error == 0; m = m->m_next) {
 		if (m->m_len == 0)
 			continue;
-		error = _bus_dmamap_load_buffer(map,
-		    m->m_data, m->m_len, NULL, flags, &lastaddr, &seg, first);
+		error = _bus_dmamap_load_buffer(map, m->m_data, m->m_len,
+		    vmspace_kernel(), flags, &lastaddr, &seg, first);
 		first = 0;
 	}
 	if (error == 0) {
@@ -312,7 +319,6 @@
 	vaddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -326,14 +332,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_uio: USERSPACE but no proc");
-#endif
-	}
-
 	first = 1;
 	seg = 0;
 	error = 0;
@@ -346,7 +344,7 @@
 		addr = (caddr_t)iov[i].iov_base;
 
 		error = _bus_dmamap_load_buffer(map, addr, minlen,
-		    p, flags, &lastaddr, &seg, first);
+		    uio->uio_vmspace, flags, &lastaddr, &seg, first);
 		first = 0;
 
 		resid -= minlen;
--- a/sys/arch/pmax/include/bus.h	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/pmax/include/bus.h	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus.h,v 1.23 2006/02/16 20:17:14 perry Exp $	*/
+/*	$NetBSD: bus.h,v 1.24 2006/03/01 12:38:11 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2001 The NetBSD Foundation, Inc.
@@ -615,7 +615,7 @@
 	bus_size_t	_dm_maxmaxsegsz; /* fixed largest possible segment */
 	bus_size_t	_dm_boundary;	/* don't cross this */
 	int		_dm_flags;	/* misc. flags */
-	struct proc	*_dm_proc;	/* proc that owns the mapping */
+	struct vmspace	*_dm_vmspace;	/* vmspace that owns the mapping */
 
 	/*
 	 * PUBLIC MEMBERS: these are used by machine-independent code.
--- a/sys/arch/pmax/pmax/bus_dma.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/pmax/pmax/bus_dma.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_dma.c,v 1.44 2005/12/11 12:18:39 christos Exp $	*/
+/*	$NetBSD: bus_dma.c,v 1.45 2006/03/01 12:38:11 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.44 2005/12/11 12:18:39 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.45 2006/03/01 12:38:11 yamt Exp $");
 
 #include "opt_cputype.h"
 
@@ -55,7 +55,7 @@
 #include <mips/cache.h>
 
 static int	_bus_dmamap_load_buffer __P((bus_dmamap_t,
-		    void *, bus_size_t, struct proc *, int, vaddr_t *,
+		    void *, bus_size_t, struct vmspace *, int, vaddr_t *,
 		    int *, int));
 
 paddr_t	kvtophys __P((vaddr_t));	/* XXX */
@@ -135,7 +135,7 @@
 	map->_dm_maxmaxsegsz = maxsegsz;
 	map->_dm_boundary = boundary;
 	map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
-	map->_dm_proc = NULL;
+	map->_dm_vmspace = NULL;
 	map->dm_maxsegsz = maxsegsz;
 	map->dm_mapsize = 0;		/* no valid mappings */
 	map->dm_nsegs = 0;
@@ -164,12 +164,12 @@
  * first indicates if this is the first invocation of this function.
  */
 static int
-_bus_dmamap_load_buffer(map, buf, buflen, p, flags,
+_bus_dmamap_load_buffer(map, buf, buflen, vm, flags,
     lastaddrp, segp, first)
 	bus_dmamap_t map;
 	void *buf;
 	bus_size_t buflen;
-	struct proc *p;
+	struct vmspace *vm;
 	int flags;
 	vaddr_t *lastaddrp;
 	int *segp;
@@ -187,8 +187,8 @@
 		/*
 		 * Get the physical address for this segment.
 		 */
-		if (p != NULL)
-			(void) pmap_extract(p->p_vmspace->vm_map.pmap,
+		if (!VMSPACE_IS_KERNEL_P(vm))
+			(void) pmap_extract(vm_map_pmap(&vm->vm_map),
 			    vaddr, &curaddr);
 		else
 			curaddr = kvtophys(vaddr);
@@ -267,6 +267,7 @@
 {
 	vaddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -278,13 +279,19 @@
 	if (buflen > map->_dm_size)
 		return (EINVAL);
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	seg = 0;
 	error = _bus_dmamap_load_buffer(map, buf, buflen,
-	    p, flags, &lastaddr, &seg, 1);
+	    vm, flags, &lastaddr, &seg, 1);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
 		map->dm_nsegs = seg + 1;
-		map->_dm_proc = p;
+		map->_dm_vmspace = vm;
 
 		/*
 		 * For linear buffers, we support marking the mapping
@@ -334,14 +341,14 @@
 	for (m = m0; m != NULL && error == 0; m = m->m_next) {
 		if (m->m_len == 0)
 			continue;
-		error = _bus_dmamap_load_buffer(map,
-		    m->m_data, m->m_len, NULL, flags, &lastaddr, &seg, first);
+		error = _bus_dmamap_load_buffer(map, m->m_data, m->m_len,
+		    vmspace_kernel(), flags, &lastaddr, &seg, first);
 		first = 0;
 	}
 	if (error == 0) {
 		map->dm_mapsize = m0->m_pkthdr.len;
 		map->dm_nsegs = seg + 1;
-		map->_dm_proc = NULL;	/* always kernel */
+		map->_dm_vmspace = vmspace_kernel();	/* always kernel */
 	}
 	return (error);
 }
@@ -359,7 +366,6 @@
 	vaddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -373,14 +379,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_uio: USERSPACE but no proc");
-#endif
-	}
-
 	first = 1;
 	seg = 0;
 	error = 0;
@@ -393,7 +391,7 @@
 		addr = iov[i].iov_base;
 
 		error = _bus_dmamap_load_buffer(map, addr, minlen,
-		    p, flags, &lastaddr, &seg, first);
+		    uio->uio_vmspace, flags, &lastaddr, &seg, first);
 		first = 0;
 
 		resid -= minlen;
@@ -401,7 +399,7 @@
 	if (error == 0) {
 		map->dm_mapsize = uio->uio_resid;
 		map->dm_nsegs = seg + 1;
-		map->_dm_proc = p;
+		map->_dm_vmspace = uio->uio_vmspace;
 	}
 	return (error);
 }
@@ -440,7 +438,7 @@
 	map->dm_mapsize = 0;
 	map->dm_nsegs = 0;
 	map->_dm_flags &= ~PMAX_DMAMAP_COHERENT;
-	map->_dm_proc = NULL;
+	map->_dm_vmspace = NULL;
 }
 
 #ifdef MIPS1
@@ -627,7 +625,8 @@
 	 *
 	 * This should be true the vast majority of the time.
 	 */
-	if (__predict_true(map->_dm_proc == NULL || map->_dm_proc == curproc))
+	if (__predict_true(VMSPACE_IS_KERNEL_P(map->_dm_vmspace) ||
+	    map->_dm_vmspace == curproc->p_vmspace))
 		useindex = 0;
 	else
 		useindex = 1;
--- a/sys/arch/powerpc/marvell/bus_dma.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/powerpc/marvell/bus_dma.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_dma.c,v 1.11 2005/12/24 20:07:28 perry Exp $	*/
+/*	$NetBSD: bus_dma.c,v 1.12 2006/03/01 12:38:11 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.11 2005/12/24 20:07:28 perry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.12 2006/03/01 12:38:11 yamt Exp $");
 
 #define DEBUG 1
 
@@ -133,7 +133,7 @@
 }
 
 int	_bus_dmamap_load_buffer __P((bus_dma_tag_t, bus_dmamap_t, void *,
-	    bus_size_t, struct proc *, int, paddr_t *, int *, int));
+	    bus_size_t, struct vmspace *, int, paddr_t *, int *, int));
 
 /*
  * Common function for DMA map creation.  May be called by bus-specific
@@ -207,12 +207,12 @@
  * first indicates if this is the first invocation of this function.
  */
 int
-_bus_dmamap_load_buffer(t, map, buf, buflen, p, flags, lastaddrp, segp, first)
+_bus_dmamap_load_buffer(t, map, buf, buflen, vm, flags, lastaddrp, segp, first)
 	bus_dma_tag_t t;
 	bus_dmamap_t map;
 	void *buf;
 	bus_size_t buflen;
-	struct proc *p;
+	struct vmspace *vm;
 	int flags;
 	paddr_t *lastaddrp;
 	int *segp;
@@ -230,9 +230,8 @@
 		/*
 		 * Get the physical address for this segment.
 		 */
-		KASSERT(p == NULL);	/* we "never" use the p!=NULL case */
-		if (p != NULL)
-			(void) pmap_extract(p->p_vmspace->vm_map.pmap,
+		if (!VMSPACE_IS_KERNEL(vm))
+			(void) pmap_extract(vm_map_pmap(&vm->vm_map),
 			    vaddr, (paddr_t *)&curaddr);
 		else
 			curaddr = vtophys(vaddr);
@@ -327,6 +326,7 @@
 {
 	paddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -338,8 +338,14 @@
 	if (buflen > map->_dm_size)
 		return (EINVAL);
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	seg = 0;
-	error = _bus_dmamap_load_buffer(t, map, buf, buflen, p, flags,
+	error = _bus_dmamap_load_buffer(t, map, buf, buflen, vm, flags,
 		&lastaddr, &seg, 1);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
@@ -384,7 +390,7 @@
 		if (m->m_len == 0)
 			continue;
 		error = _bus_dmamap_load_buffer(t, map, m->m_data, m->m_len,
-		    NULL, flags, &lastaddr, &seg, first);
+		    vmspace_kernel(), flags, &lastaddr, &seg, first);
 		first = 0;
 	}
 	if (error == 0) {
@@ -407,7 +413,6 @@
 	paddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -421,14 +426,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_uio: USERSPACE but no proc");
-#endif
-	}
-
 	first = 1;
 	seg = 0;
 	error = 0;
@@ -441,7 +438,7 @@
 		addr = (caddr_t)iov[i].iov_base;
 
 		error = _bus_dmamap_load_buffer(t, map, addr, minlen,
-		    p, flags, &lastaddr, &seg, first);
+		    uio->uio_vmspace, flags, &lastaddr, &seg, first);
 		first = 0;
 
 		resid -= minlen;
--- a/sys/arch/powerpc/powerpc/bus_dma.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/powerpc/powerpc/bus_dma.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_dma.c,v 1.25 2005/12/24 20:07:28 perry Exp $	*/
+/*	$NetBSD: bus_dma.c,v 1.26 2006/03/01 12:38:12 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.25 2005/12/24 20:07:28 perry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.26 2006/03/01 12:38:12 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -55,7 +55,7 @@
 #include <machine/intr.h>
 
 int	_bus_dmamap_load_buffer (bus_dma_tag_t, bus_dmamap_t, void *,
-	    bus_size_t, struct proc *, int, paddr_t *, int *, int);
+	    bus_size_t, struct vmspace *, int, paddr_t *, int *, int);
 
 static inline void
 dcbst(paddr_t pa, long len, int dcache_line_size)
@@ -153,12 +153,12 @@
  * first indicates if this is the first invocation of this function.
  */
 int
-_bus_dmamap_load_buffer(t, map, buf, buflen, p, flags, lastaddrp, segp, first)
+_bus_dmamap_load_buffer(t, map, buf, buflen, vm, flags, lastaddrp, segp, first)
 	bus_dma_tag_t t;
 	bus_dmamap_t map;
 	void *buf;
 	bus_size_t buflen;
-	struct proc *p;
+	struct vmspace *vm;
 	int flags;
 	paddr_t *lastaddrp;
 	int *segp;
@@ -176,8 +176,8 @@
 		/*
 		 * Get the physical address for this segment.
 		 */
-		if (p != NULL)
-			(void) pmap_extract(p->p_vmspace->vm_map.pmap,
+		if (VMSPACE_IS_KERNEL_P(vm))
+			(void) pmap_extract(vm_map_pmap(&vm->vm_map),
 			    vaddr, (void *)&curaddr);
 		else
 			curaddr = vtophys(vaddr);
@@ -264,6 +264,7 @@
 {
 	paddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -275,8 +276,14 @@
 	if (buflen > map->_dm_size)
 		return (EINVAL);
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	seg = 0;
-	error = _bus_dmamap_load_buffer(t, map, buf, buflen, p, flags,
+	error = _bus_dmamap_load_buffer(t, map, buf, buflen, vm, flags,
 		&lastaddr, &seg, 1);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
@@ -350,7 +357,7 @@
 		}
 #endif
 		error = _bus_dmamap_load_buffer(t, map, m->m_data,
-		    m->m_len, NULL, flags, &lastaddr, &seg, first);
+		    m->m_len, vmspace_kernel(), flags, &lastaddr, &seg, first);
 	}
 	if (error == 0) {
 		map->dm_mapsize = m0->m_pkthdr.len;
@@ -372,7 +379,6 @@
 	paddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -386,14 +392,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_uio: USERSPACE but no proc");
-#endif
-	}
-
 	first = 1;
 	seg = 0;
 	error = 0;
@@ -406,7 +404,7 @@
 		addr = (caddr_t)iov[i].iov_base;
 
 		error = _bus_dmamap_load_buffer(t, map, addr, minlen,
-		    p, flags, &lastaddr, &seg, first);
+		    uio->uio_vmspace, flags, &lastaddr, &seg, first);
 		first = 0;
 
 		resid -= minlen;
--- a/sys/arch/powerpc/powerpc/process_machdep.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/powerpc/powerpc/process_machdep.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: process_machdep.c,v 1.20 2005/12/11 12:18:46 christos Exp $	*/
+/*	$NetBSD: process_machdep.c,v 1.21 2006/03/01 12:38:12 yamt Exp $	*/
 
 /*
  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.20 2005/12/11 12:18:46 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.21 2006/03/01 12:38:12 yamt Exp $");
 
 #include "opt_altivec.h"
 
@@ -201,9 +201,8 @@
 		uio.uio_iovcnt = 1;
 		uio.uio_offset = 0;
 		uio.uio_resid = sizeof(struct vreg);
-		uio.uio_segflg = UIO_USERSPACE;
 		uio.uio_rw = write ? UIO_WRITE : UIO_READ;
-		uio.uio_lwp = l;
+		uio.uio_vmspace = l->l_proc->p_vmspace;
 		return process_machdep_dovecregs(l, lt, &uio);
 	}
 
--- a/sys/arch/sgimips/include/bus.h	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/sgimips/include/bus.h	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus.h,v 1.17 2006/02/16 20:17:14 perry Exp $	*/
+/*	$NetBSD: bus.h,v 1.18 2006/03/01 12:38:12 yamt Exp $	*/
 
 /*
  * Copyright (c) 1996, 1997, 1998, 2001 The NetBSD Foundation, Inc.
@@ -609,7 +609,7 @@
 	bus_size_t	_dm_maxmaxsegsz; /* fixed largest possible segment */
 	bus_size_t	_dm_boundary;	/* don't cross this */
 	int		_dm_flags;	/* misc. flags */
-	struct proc	*_dm_proc;	/* proc that owns the mapping */
+	struct vmspace	*_dm_vmspace;	/* vmspace that owns the mapping */
 
 	/*
 	 * PUBLIC MEMBERS: these are used by machine-independent code.
--- a/sys/arch/sgimips/sgimips/bus.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/sgimips/sgimips/bus.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus.c,v 1.41 2005/12/11 12:18:58 christos Exp $	*/
+/*	$NetBSD: bus.c,v 1.42 2006/03/01 12:38:12 yamt Exp $	*/
 
 /*
  * Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bus.c,v 1.41 2005/12/11 12:18:58 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus.c,v 1.42 2006/03/01 12:38:12 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -64,7 +64,7 @@
 #include <sgimips/mace/macereg.h>
 
 static int	_bus_dmamap_load_buffer(bus_dmamap_t, void *, bus_size_t,
-				struct proc *, int, vaddr_t *, int *, int);
+				struct vmspace *, int, vaddr_t *, int *, int);
 
 struct sgimips_bus_dma_tag sgimips_default_bus_dma_tag = {
 	_bus_dmamap_create,
@@ -387,7 +387,7 @@
 	map->_dm_maxmaxsegsz = maxsegsz;
 	map->_dm_boundary = boundary;
 	map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
-	map->_dm_proc = NULL;
+	map->_dm_vmspace = NULL;
 	map->dm_maxsegsz = maxsegsz;
 	map->dm_mapsize = 0;		/* no valid mappings */
 	map->dm_nsegs = 0;
@@ -416,7 +416,7 @@
  */
 int
 _bus_dmamap_load_buffer(bus_dmamap_t map, void *buf, bus_size_t buflen,
-			struct proc *p, int flags, vaddr_t *lastaddrp,
+			struct vmspace *vm, int flags, vaddr_t *lastaddrp,
 			int *segp, int first)
 {
 	bus_size_t sgsize;
@@ -431,8 +431,8 @@
 		/*
 		 * Get the physical address for this segment.
 		 */
-		if (p != NULL)
-			(void) pmap_extract(p->p_vmspace->vm_map.pmap,
+		if (!VMSPACE_IS_KERNEL_P(vm))
+			(void) pmap_extract(vm_map_pmap(&vm->vm_map),
 			    vaddr, &curaddr);
 		else
 			curaddr = kvtophys(vaddr);
@@ -506,6 +506,7 @@
 {
 	vaddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -517,13 +518,19 @@
 	if (buflen > map->_dm_size)
 		return EINVAL;
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	seg = 0;
 	error = _bus_dmamap_load_buffer(map, buf, buflen,
-	    p, flags, &lastaddr, &seg, 1);
+	    vm, flags, &lastaddr, &seg, 1);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
 		map->dm_nsegs = seg + 1;
-		map->_dm_proc = p;
+		map->_dm_vmspace = vm;
 
 		/*
 		 * For linear buffers, we support marking the mapping
@@ -570,14 +577,14 @@
 	for (m = m0; m != NULL && error == 0; m = m->m_next) {
 		if (m->m_len == 0)
 			continue;
-		error = _bus_dmamap_load_buffer(map,
-		    m->m_data, m->m_len, NULL, flags, &lastaddr, &seg, first);
+		error = _bus_dmamap_load_buffer(map, m->m_data, m->m_len,
+		    vmspace_kernel(), flags, &lastaddr, &seg, first);
 		first = 0;
 	}
 	if (error == 0) {
 		map->dm_mapsize = m0->m_pkthdr.len;
 		map->dm_nsegs = seg + 1;
-		map->_dm_proc = NULL;	/* always kernel */
+		map->_dm_vmspace = vmspace_kernel();	/* always kernel */
 	}
 	return error;
 }
@@ -592,7 +599,6 @@
 	vaddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -606,14 +612,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_uio: USERSPACE but no proc");
-#endif
-	}
-
 	first = 1;
 	seg = 0;
 	error = 0;
@@ -626,7 +624,7 @@
 		addr = (caddr_t)iov[i].iov_base;
 
 		error = _bus_dmamap_load_buffer(map, addr, minlen,
-		    p, flags, &lastaddr, &seg, first);
+		    uio->uio_vmspace, flags, &lastaddr, &seg, first);
 		first = 0;
 
 		resid -= minlen;
@@ -634,7 +632,7 @@
 	if (error == 0) {
 		map->dm_mapsize = uio->uio_resid;
 		map->dm_nsegs = seg + 1;
-		map->_dm_proc = p;
+		map->_dm_vmspace = uio->uio_vmspace;
 	}
 	return error;
 }
@@ -844,8 +842,8 @@
 	 *
 	 * This should be true the vast majority of the time.
 	 */
-	if (__predict_true(map->_dm_proc == NULL ||
-		map->_dm_proc == curlwp->l_proc))
+	if (__predict_true(VMSPACE_IS_KERNEL_P(map->_dm_vmspace) ||
+		map->_dm_vmspace == curproc->p_vmspace))
 		useindex = 0;
 	else
 		useindex = 1;
--- a/sys/arch/sh5/sh5/bus_dma.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/sh5/sh5/bus_dma.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_dma.c,v 1.16 2005/12/11 12:19:02 christos Exp $	*/
+/*	$NetBSD: bus_dma.c,v 1.17 2006/03/01 12:38:12 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
@@ -39,7 +39,7 @@
 
 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
 
-__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.16 2005/12/11 12:19:02 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.17 2006/03/01 12:38:12 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -79,7 +79,7 @@
 		off_t, int, int);
 
 static int _bus_dmamap_load_buffer_direct_common(void *,
-    bus_dmamap_t, void *, bus_size_t, struct proc *, int,
+    bus_dmamap_t, void *, bus_size_t, struct vmspace *, int,
     paddr_t *, int *, int);
 
 static void _bus_dmamap_sync_helper(vaddr_t, paddr_t, vsize_t, int);
@@ -170,7 +170,7 @@
 /*ARGSUSED*/
 static int
 _bus_dmamap_load_buffer_direct_common(void *cookie, bus_dmamap_t map,
-    void *buf, bus_size_t buflen, struct proc *p, int flags,
+    void *buf, bus_size_t buflen, struct vmspace *vm, int flags,
     paddr_t *lastaddrp, int *segp, int first)
 {
 	bus_size_t sgsize;
@@ -183,7 +183,7 @@
 	lastaddr = *lastaddrp;
 	bmask = ~(map->_dm_boundary - 1);
 
-	pm = p ? p->p_vmspace->vm_map.pmap : pmap_kernel();
+	pm = vm_map_pmap(&vm->vm_map);
 
 	for (seg = *segp; buflen > 0 ; ) {
 		/*
@@ -290,6 +290,7 @@
 {
 	paddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 	/*
 	 * Make sure that on error condition we return "no valid mappings".
@@ -304,10 +305,16 @@
 	if (buflen == 0)
 		return (0);
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	seg = 0;
 	map->_dm_flags |= BUS_DMA_COHERENT;
 	error = _bus_dmamap_load_buffer_direct_common(cookie, map, buf, buflen,
-	    p, flags, &lastaddr, &seg, 1);
+	    vm, flags, &lastaddr, &seg, 1);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
 		map->dm_nsegs = seg + 1;
@@ -350,7 +357,8 @@
 		if (m->m_len == 0)
 			continue;
 		error = _bus_dmamap_load_buffer_direct_common(cookie, map,
-		    m->m_data, m->m_len, NULL, flags, &lastaddr, &seg, first);
+		    m->m_data, m->m_len, vmspace_kernel(), flags, &lastaddr,
+		    &seg, first);
 		first = 0;
 	}
 	if (error == 0) {
@@ -371,7 +379,6 @@
 	paddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -385,14 +392,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp->l_proc;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_uio_direct: USERSPACE but no proc");
-#endif
-	}
-
 	first = 1;
 	seg = 0;
 	error = 0;
@@ -409,7 +408,8 @@
 			continue;
 
 		error = _bus_dmamap_load_buffer_direct_common(cookie, map,
-		    addr, minlen, p, flags, &lastaddr, &seg, first);
+		    addr, minlen, uio->uio_vmspace, flags, &lastaddr, &seg,
+		    first);
 		first = 0;
 
 		resid -= minlen;
--- a/sys/arch/vax/vax/bus_dma.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/vax/vax/bus_dma.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_dma.c,v 1.25 2005/12/11 12:19:36 christos Exp $	*/
+/*	$NetBSD: bus_dma.c,v 1.26 2006/03/01 12:38:12 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
@@ -42,7 +42,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.25 2005/12/11 12:19:36 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.26 2006/03/01 12:38:12 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -69,7 +69,7 @@
 extern  vaddr_t virtual_avail;
 
 int	_bus_dmamap_load_buffer __P((bus_dma_tag_t, bus_dmamap_t, void *,
-	    bus_size_t, struct proc *, int, vaddr_t *, int *, int));
+	    bus_size_t, struct vmspace *, int, vaddr_t *, int *, int));
 int	_bus_dma_inrange __P((bus_dma_segment_t *, int, bus_addr_t));
 int	_bus_dmamem_alloc_range __P((bus_dma_tag_t, bus_size_t, bus_size_t,
 	    bus_size_t, bus_dma_segment_t*, int, int *, int, vaddr_t, vaddr_t));
@@ -167,6 +167,7 @@
 {
 	vaddr_t lastaddr;
 	int seg, error;
+	struct vmspace *vm;
 
 #ifdef DEBUG_DMA
 	printf("dmamap_load: t=%p map=%p buf=%p len=%lx p=%p f=%d\n",
@@ -183,8 +184,14 @@
 	if (buflen > map->_dm_size)
 		return (EINVAL);
 
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+
 	seg = 0;
-	error = _bus_dmamap_load_buffer(t, map, buf, buflen, p, flags,
+	error = _bus_dmamap_load_buffer(t, map, buf, buflen, vm, flags,
 	    &lastaddr, &seg, 1);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
@@ -237,7 +244,7 @@
 		if (m->m_len == 0)
 			continue;
 		error = _bus_dmamap_load_buffer(t, map, m->m_data, m->m_len,
-		    NULL, flags, &lastaddr, &seg, first);
+		    vmspace_kernel(), flags, &lastaddr, &seg, first);
 		first = 0;
 	}
 	if (error == 0) {
@@ -263,7 +270,6 @@
 	vaddr_t lastaddr;
 	int seg, i, error, first;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
 	struct iovec *iov;
 	caddr_t addr;
 
@@ -277,14 +283,6 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_uio: USERSPACE but no proc");
-#endif
-	}
-
 	first = 1;
 	seg = 0;
 	error = 0;
@@ -297,7 +295,7 @@
 		addr = (caddr_t)iov[i].iov_base;
 
 		error = _bus_dmamap_load_buffer(t, map, addr, minlen,
-		    p, flags, &lastaddr, &seg, first);
+		    uio->uio_vmspace, flags, &lastaddr, &seg, first);
 		first = 0;
 
 		resid -= minlen;
@@ -566,12 +564,12 @@
  * first indicates if this is the first invocation of this function.
  */
 int
-_bus_dmamap_load_buffer(t, map, buf, buflen, p, flags, lastaddrp, segp, first)
+_bus_dmamap_load_buffer(t, map, buf, buflen, vm, flags, lastaddrp, segp, first)
 	bus_dma_tag_t t;
 	bus_dmamap_t map;
 	void *buf;
 	bus_size_t buflen;
-	struct proc *p;
+	struct vmspace *vm;
 	int flags;
 	vaddr_t *lastaddrp;
 	int *segp;
@@ -588,10 +586,7 @@
 	    buf, buflen, flags, first);
 #endif	/* DEBUG_DMA */
 
-	if (p != NULL)
-		pmap = p->p_vmspace->vm_map.pmap;
-	else
-		pmap = pmap_kernel();
+	pmap = vm_map_pmap(&vm->vm_map);
 
 	lastaddr = *lastaddrp;
 	bmask  = ~(map->_dm_boundary - 1);
--- a/sys/arch/x86/x86/bus_dma.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/arch/x86/x86/bus_dma.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: bus_dma.c,v 1.28 2006/01/14 23:49:59 christos Exp $	*/
+/*	$NetBSD: bus_dma.c,v 1.29 2006/03/01 12:38:12 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.28 2006/01/14 23:49:59 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.29 2006/03/01 12:38:12 yamt Exp $");
 
 /*
  * The following is included because _bus_dma_uiomove is derived from
@@ -145,7 +145,7 @@
 	    bus_size_t size, int flags);
 static void _bus_dma_free_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map);
 static int _bus_dmamap_load_buffer(bus_dma_tag_t t, bus_dmamap_t map,
-	    void *buf, bus_size_t buflen, struct proc *p, int flags);
+	    void *buf, bus_size_t buflen, struct vmspace *vm, int flags);
 static inline int _bus_dmamap_load_busaddr(bus_dma_tag_t, bus_dmamap_t,
     bus_addr_t, int);
 
@@ -327,6 +327,7 @@
 {
 	struct x86_bus_dma_cookie *cookie = map->_dm_cookie;
 	int error;
+	struct vmspace *vm;
 
 	STAT_INCR(loads);
 
@@ -340,7 +341,12 @@
 	if (buflen > map->_dm_size)
 		return EINVAL;
 
-	error = _bus_dmamap_load_buffer(t, map, buf, buflen, p, flags);
+	if (p != NULL) {
+		vm = p->p_vmspace;
+	} else {
+		vm = vmspace_kernel();
+	}
+	error = _bus_dmamap_load_buffer(t, map, buf, buflen, vm, flags);
 	if (error == 0) {
 		map->dm_mapsize = buflen;
 		return 0;
@@ -535,7 +541,7 @@
 
 		default:
 			error = _bus_dmamap_load_buffer(t, map, m->m_data,
-			    m->m_len, NULL, flags);
+			    m->m_len, vmspace_kernel(), flags);
 		}
 	}
 	if (error == 0) {
@@ -591,7 +597,7 @@
 {
 	int i, error;
 	bus_size_t minlen, resid;
-	struct proc *p = NULL;
+	struct vmspace *vm;
 	struct iovec *iov;
 	caddr_t addr;
 	struct x86_bus_dma_cookie *cookie = map->_dm_cookie;
@@ -606,13 +612,7 @@
 	resid = uio->uio_resid;
 	iov = uio->uio_iov;
 
-	if (uio->uio_segflg == UIO_USERSPACE) {
-		p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
-#ifdef DIAGNOSTIC
-		if (p == NULL)
-			panic("_bus_dmamap_load_uio: USERSPACE but no proc");
-#endif
-	}
+	vm = uio->uio_vmspace;
 
 	error = 0;
 	for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) {
@@ -624,7 +624,7 @@
 		addr = (caddr_t)iov[i].iov_base;
 
 		error = _bus_dmamap_load_buffer(t, map, addr, minlen,
-		    p, flags);
+		    vm, flags);
 
 		resid -= minlen;
 	}
@@ -940,13 +940,13 @@
 {
 	struct iovec *iov;
 	int error;
-	struct proc *p;
+	struct vmspace *vm;
 	char *cp;
 	size_t resid, cnt;
 	int i;
 
 	iov = uio->uio_iov;
-	p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
+	vm = uio->uio_vmspace;
 	cp = buf;
 	resid = n;
 
@@ -956,34 +956,18 @@
 			continue;
 		cnt = MIN(resid, iov->iov_len);
 
-		if (uio->uio_segflg == UIO_USERSPACE) {
-			if (curlwp != NULL &&
-			    curlwp->l_cpu->ci_schedstate.spc_flags &
-			      SPCF_SHOULDYIELD)
-				preempt(1);
-			if (p == curproc) {
-				if (direction == UIO_READ)
-					error = copyout(cp, iov->iov_base, cnt);
-				else
-					error = copyin(iov->iov_base, cp, cnt);
-			} else {
-				if (direction == UIO_READ)
-					error = copyout_proc(p, cp,
-					    iov->iov_base, cnt);
-				else
-					error = copyin_proc(p, iov->iov_base,
-					    cp, cnt);
-			}
-			if (error)
-				return (error);
+		if (!VMSPACE_IS_KERNEL_P(vm) &&
+		    (curlwp->l_cpu->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
+		    != 0) {
+			preempt(1);
+		}
+		if (direction == UIO_READ) {
+			error = copyout_vmspace(vm, cp, iov->iov_base, cnt);
 		} else {
-			if (direction == UIO_READ)
-				error = kcopy(cp, iov->iov_base, cnt);
-			else
-				error = kcopy(iov->iov_base, cp, cnt);
-			if (error)
-				return (error);
+			error = copyin_vmspace(vm, iov->iov_base, cp, cnt);
 		}
+		if (error)
+			return (error);
 		cp += cnt;
 		resid -= cnt;
 	}
@@ -1161,15 +1145,15 @@
  */
 static int
 _bus_dmamap_load_buffer(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
-    bus_size_t buflen, struct proc *p, int flags)
+    bus_size_t buflen, struct vmspace *vm, int flags)
 {
 	bus_size_t sgsize;
 	bus_addr_t curaddr;
 	vaddr_t vaddr = (vaddr_t)buf;
 	pmap_t pmap;
 
-	if (p != NULL)
-		pmap = p->p_vmspace->vm_map.pmap;
+	if (vm != NULL)
+		pmap = vm_map_pmap(&vm->vm_map);
 	else
 		pmap = pmap_kernel();
 
--- a/sys/coda/coda_vnops.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/coda/coda_vnops.c	Wed Mar 01 12:38:10 2006 +0000
@@ -6,7 +6,7 @@
 rmdir
 symlink
 */
-/*	$NetBSD: coda_vnops.c,v 1.46 2006/01/12 14:57:06 gdt Exp $	*/
+/*	$NetBSD: coda_vnops.c,v 1.47 2006/03/01 12:38:12 yamt Exp $	*/
 
 /*
  *
@@ -54,7 +54,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: coda_vnops.c,v 1.46 2006/01/12 14:57:06 gdt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: coda_vnops.c,v 1.47 2006/03/01 12:38:12 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -359,7 +359,7 @@
 
     ENTRY;
     return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_READ,
-		    ap->a_ioflag, ap->a_cred, ap->a_uio->uio_lwp));
+		    ap->a_ioflag, ap->a_cred, curlwp));
 }
 
 int
@@ -369,7 +369,7 @@
 
     ENTRY;
     return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_WRITE,
-		    ap->a_ioflag, ap->a_cred, ap->a_uio->uio_lwp));
+		    ap->a_ioflag, ap->a_cred, curlwp));
 }
 
 int
@@ -387,10 +387,10 @@
 
     MARK_ENTRY(CODA_RDWR_STATS);
 
-    CODADEBUG(CODA_RDWR, myprintf(("coda_rdwr(%d, %p, %lu, %lld, %d)\n", rw,
+    CODADEBUG(CODA_RDWR, myprintf(("coda_rdwr(%d, %p, %lu, %lld)\n", rw,
 			      uiop->uio_iov->iov_base,
 			      (unsigned long) uiop->uio_resid,
-			      (long long) uiop->uio_offset, uiop->uio_segflg)); )
+			      (long long) uiop->uio_offset)); )
 
     /* Check for rdwr of control object. */
     if (IS_CTL_VP(vp)) {
@@ -712,8 +712,8 @@
     struct cnode *cp = VTOC(vp);
     struct uio *uiop = ap->a_uio;
     struct ucred *cred = ap->a_cred;
-    struct lwp *l = ap->a_uio->uio_lwp;
 /* locals */
+    struct lwp *l = curlwp;
     int error;
     char *str;
     int len;
@@ -1649,14 +1649,14 @@
     int *eofflag = ap->a_eofflag;
     off_t **cookies = ap->a_cookies;
     int *ncookies = ap->a_ncookies;
-    struct lwp *l = ap->a_uio->uio_lwp;
 /* upcall decl */
 /* locals */
+    struct lwp *l = curlwp;
     int error = 0;
 
     MARK_ENTRY(CODA_READDIR_STATS);
 
-    CODADEBUG(CODA_READDIR, myprintf(("coda_readdir(%p, %lu, %lld, %d)\n", uiop->uio_iov->iov_base, (unsigned long) uiop->uio_resid, (long long) uiop->uio_offset, uiop->uio_segflg)); )
+    CODADEBUG(CODA_READDIR, myprintf(("coda_readdir(%p, %lu, %lld)\n", uiop->uio_iov->iov_base, (unsigned long) uiop->uio_resid, (long long) uiop->uio_offset)); )
 
     /* Check for readdir of control object. */
     if (IS_CTL_VP(vp)) {
--- a/sys/compat/common/vfs_syscalls_30.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/compat/common/vfs_syscalls_30.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_syscalls_30.c,v 1.6 2005/12/11 12:19:56 christos Exp $	*/
+/*	$NetBSD: vfs_syscalls_30.c,v 1.7 2006/03/01 12:38:12 yamt Exp $	*/
 
 /*-
  * Copyright (c) 2005 The NetBSD Foundation, Inc.
@@ -36,7 +36,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_30.c,v 1.6 2005/12/11 12:19:56 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_30.c,v 1.7 2006/03/01 12:38:12 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -231,10 +231,9 @@
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_SYSSPACE;
-	auio.uio_lwp = NULL;
 	auio.uio_resid = buflen;
 	auio.uio_offset = off;
+	UIO_SETUP_SYSSPACE(&auio);
 	/*
          * First we read into the malloc'ed buffer, then
          * we massage it into user space, one record at a time.
--- a/sys/compat/common/vfs_syscalls_43.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/compat/common/vfs_syscalls_43.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_syscalls_43.c,v 1.31 2005/12/11 12:19:56 christos Exp $	*/
+/*	$NetBSD: vfs_syscalls_43.c,v 1.32 2006/03/01 12:38:12 yamt Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_43.c,v 1.31 2005/12/11 12:19:56 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_43.c,v 1.32 2006/03/01 12:38:12 yamt Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "fs_union.h"
@@ -385,9 +385,9 @@
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_USERSPACE;
-	auio.uio_lwp = l;
 	auio.uio_resid = count;
+	KASSERT(l == curlwp);
+	auio.uio_vmspace = l->l_proc->p_vmspace;
 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
 	loff = auio.uio_offset = fp->f_offset;
 #	if (BYTE_ORDER != LITTLE_ENDIAN)
@@ -400,10 +400,10 @@
 	{
 		kuio = auio;
 		kuio.uio_iov = &kiov;
-		kuio.uio_segflg = UIO_SYSSPACE;
 		kiov.iov_len = count;
 		dirbuf = malloc(count, M_TEMP, M_WAITOK);
 		kiov.iov_base = dirbuf;
+		UIO_SETUP_SYSSPACE(&kuio);
 		error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
 			    (off_t **)0, (int *)0);
 		fp->f_offset = kuio.uio_offset;
--- a/sys/compat/darwin/darwin_iohidsystem.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/compat/darwin/darwin_iohidsystem.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: darwin_iohidsystem.c,v 1.31 2006/02/25 02:28:57 wiz Exp $ */
+/*	$NetBSD: darwin_iohidsystem.c,v 1.32 2006/03/01 12:38:12 yamt Exp $ */
 
 /*-
  * Copyright (c) 2003 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: darwin_iohidsystem.c,v 1.31 2006/02/25 02:28:57 wiz Exp $");
+__KERNEL_RCSID(0, "$NetBSD: darwin_iohidsystem.c,v 1.32 2006/03/01 12:38:12 yamt Exp $");
 
 #include "ioconf.h"
 #include "wsmux.h"
@@ -477,9 +477,8 @@
 		aiov.iov_len = sizeof(wsevt);
 		auio.uio_resid = sizeof(wsevt);
 		auio.uio_offset = 0;
-		auio.uio_segflg = UIO_SYSSPACE;
 		auio.uio_rw = UIO_READ;
-		auio.uio_lwp = NULL;
+		UIO_SETUP_SYSSPACE(&auio);
 
 		if ((error = (wsmux->d_read)(dev, &auio, 0)) != 0) {
 #ifdef DEBUG_DARWIN
--- a/sys/compat/darwin/darwin_sysctl.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/compat/darwin/darwin_sysctl.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: darwin_sysctl.c,v 1.38 2005/12/11 12:19:56 christos Exp $ */
+/*	$NetBSD: darwin_sysctl.c,v 1.39 2006/03/01 12:38:12 yamt Exp $ */
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: darwin_sysctl.c,v 1.38 2005/12/11 12:19:56 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: darwin_sysctl.c,v 1.39 2006/03/01 12:38:12 yamt Exp $");
 
 #include <sys/types.h>
 #include <sys/param.h>
@@ -989,9 +989,8 @@
 	auio.uio_iovcnt = 1;
 	auio.uio_offset = (vaddr_t)p->p_psstr;
 	auio.uio_resid = sizeof(pss);
-	auio.uio_segflg = UIO_SYSSPACE;
 	auio.uio_rw = UIO_READ;
-	auio.uio_lwp = NULL;
+	UIO_SETUP_SYSSPACE(&auio);
 	if ((error = uvm_io(&p->p_vmspace->vm_map, &auio)) != 0)
 		goto done;
 
@@ -1010,9 +1009,8 @@
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_resid = sizeof(argv);
-	auio.uio_segflg = UIO_SYSSPACE;
 	auio.uio_rw = UIO_READ;
-	auio.uio_lwp = NULL;
+	UIO_SETUP_SYSSPACE(&auio);
 	if ((error = uvm_io(&p->p_vmspace->vm_map, &auio)) != 0)
 		goto done;
 
@@ -1032,9 +1030,8 @@
 		auio.uio_offset = argv + len;
 		xlen = PAGE_SIZE - ((argv + len) & PAGE_MASK);
 		auio.uio_resid = xlen;
-		auio.uio_segflg = UIO_SYSSPACE;
 		auio.uio_rw = UIO_READ;
-		auio.uio_lwp = NULL;
+		UIO_SETUP_SYSSPACE(&auio);
 		error = uvm_io(&p->p_vmspace->vm_map, &auio);
 		if (error)
 			goto done;
@@ -1094,9 +1091,8 @@
 		auio.uio_offset = argv + len;
 		xlen = PAGE_SIZE - ((argv + len) & PAGE_MASK);
 		auio.uio_resid = xlen;
-		auio.uio_segflg = UIO_SYSSPACE;
 		auio.uio_rw = UIO_READ;
-		auio.uio_lwp = NULL;
+		UIO_SETUP_SYSSPACE(&auio);
 		error = uvm_io(&p->p_vmspace->vm_map, &auio);
 		if (error)
 			goto done;
--- a/sys/compat/ibcs2/ibcs2_misc.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/compat/ibcs2/ibcs2_misc.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: ibcs2_misc.c,v 1.76 2005/12/11 12:20:02 christos Exp $	*/
+/*	$NetBSD: ibcs2_misc.c,v 1.77 2006/03/01 12:38:12 yamt Exp $	*/
 
 /*
  * Copyright (c) 1992, 1993
@@ -95,7 +95,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ibcs2_misc.c,v 1.76 2005/12/11 12:20:02 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ibcs2_misc.c,v 1.77 2006/03/01 12:38:12 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -441,10 +441,9 @@
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_SYSSPACE;
-	auio.uio_lwp = NULL;
 	auio.uio_resid = buflen;
 	auio.uio_offset = off;
+	UIO_SETUP_SYSSPACE(&auio);
 	/*
 	 * First we read into the malloc'ed buffer, then
 	 * we massage it into user space, one record at a time.
@@ -580,10 +579,9 @@
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_SYSSPACE;
-	auio.uio_lwp = NULL;
 	auio.uio_resid = buflen;
 	auio.uio_offset = off;
+	UIO_SETUP_SYSSPACE(&auio);
 	/*
 	 * First we read into the malloc'ed buffer, then
 	 * we massage it into user space, one record at a time.
--- a/sys/compat/irix/irix_dirent.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/compat/irix/irix_dirent.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: irix_dirent.c,v 1.15 2005/12/11 12:20:12 christos Exp $ */
+/*	$NetBSD: irix_dirent.c,v 1.16 2006/03/01 12:38:12 yamt Exp $ */
 
 /*-
  * Copyright (c) 1994, 2001 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: irix_dirent.c,v 1.15 2005/12/11 12:20:12 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: irix_dirent.c,v 1.16 2006/03/01 12:38:12 yamt Exp $");
 
 #include <sys/types.h>
 #include <sys/signal.h>
@@ -123,10 +123,9 @@
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_SYSSPACE;
-	auio.uio_lwp = NULL;
 	auio.uio_resid = buflen;
 	auio.uio_offset = off;
+	UIO_SETUP_SYSSPACE(&auio);
 	/*
          * First we read into the malloc'ed buffer, then
          * we massage it into user space, one record at a time.
@@ -283,10 +282,9 @@
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_SYSSPACE;
-	auio.uio_lwp = NULL;
 	auio.uio_resid = buflen;
 	auio.uio_offset = off;
+	UIO_SETUP_SYSSPACE(&auio);
 	/*
          * First we read into the malloc'ed buffer, then
          * we massage it into user space, one record at a time.
--- a/sys/compat/linux/common/linux_file64.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/compat/linux/common/linux_file64.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: linux_file64.c,v 1.31 2006/02/09 19:18:56 manu Exp $	*/
+/*	$NetBSD: linux_file64.c,v 1.32 2006/03/01 12:38:12 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1995, 1998, 2000 The NetBSD Foundation, Inc.
@@ -41,7 +41,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: linux_file64.c,v 1.31 2006/02/09 19:18:56 manu Exp $");
+__KERNEL_RCSID(0, "$NetBSD: linux_file64.c,v 1.32 2006/03/01 12:38:12 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -457,10 +457,9 @@
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_SYSSPACE;
-	auio.uio_lwp = NULL;
 	auio.uio_resid = buflen;
 	auio.uio_offset = off;
+	UIO_SETUP_SYSSPACE(&auio);
 	/*
          * First we read into the malloc'ed buffer, then
          * we massage it into user space, one record at a time.
--- a/sys/compat/linux/common/linux_misc.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/compat/linux/common/linux_misc.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: linux_misc.c,v 1.150 2006/02/09 19:18:56 manu Exp $	*/
+/*	$NetBSD: linux_misc.c,v 1.151 2006/03/01 12:38:12 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1995, 1998, 1999 The NetBSD Foundation, Inc.
@@ -64,7 +64,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: linux_misc.c,v 1.150 2006/02/09 19:18:56 manu Exp $");
+__KERNEL_RCSID(0, "$NetBSD: linux_misc.c,v 1.151 2006/03/01 12:38:12 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -875,10 +875,9 @@
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_SYSSPACE;
-	auio.uio_lwp = NULL;
 	auio.uio_resid = buflen;
 	auio.uio_offset = off;
+	UIO_SETUP_SYSSPACE(&auio);
 	/*
          * First we read into the malloc'ed buffer, then
          * we massage it into user space, one record at a time.
--- a/sys/compat/netbsd32/netbsd32_fs.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/compat/netbsd32/netbsd32_fs.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_fs.c,v 1.23 2005/12/11 12:20:22 christos Exp $	*/
+/*	$NetBSD: netbsd32_fs.c,v 1.24 2006/03/01 12:38:12 yamt Exp $	*/
 
 /*
  * Copyright (c) 1998, 2001 Matthew R. Green
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_fs.c,v 1.23 2005/12/11 12:20:22 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_fs.c,v 1.24 2006/03/01 12:38:12 yamt Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_ktrace.h"
@@ -137,8 +137,7 @@
 	auio.uio_iov = iov;
 	auio.uio_iovcnt = iovcnt;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_USERSPACE;
-	auio.uio_lwp = l;
+	auio.uio_vmspace = l->l_proc->p_vmspace;
 	error = netbsd32_to_iovecin(iovp, iov, iovcnt);
 	if (error)
 		goto done;
@@ -260,8 +259,7 @@
 	auio.uio_iov = iov;
 	auio.uio_iovcnt = iovcnt;
 	auio.uio_rw = UIO_WRITE;
-	auio.uio_segflg = UIO_USERSPACE;
-	auio.uio_lwp = l;
+	auio.uio_vmspace = l->l_proc->p_vmspace;
 	error = netbsd32_to_iovecin(iovp, iov, iovcnt);
 	if (error)
 		goto done;
--- a/sys/compat/netbsd32/netbsd32_socket.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/compat/netbsd32/netbsd32_socket.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_socket.c,v 1.16 2005/12/11 12:20:22 christos Exp $	*/
+/*	$NetBSD: netbsd32_socket.c,v 1.17 2006/03/01 12:38:12 yamt Exp $	*/
 
 /*
  * Copyright (c) 1998, 2001 Matthew R. Green
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_socket.c,v 1.16 2005/12/11 12:20:22 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_socket.c,v 1.17 2006/03/01 12:38:12 yamt Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_ktrace.h"
@@ -133,9 +133,8 @@
 		return (error);
 	auio.uio_iov = iov;
 	auio.uio_iovcnt = mp->msg_iovlen;
-	auio.uio_segflg = UIO_USERSPACE;
 	auio.uio_rw = UIO_READ;
-	auio.uio_lwp = l;
+	auio.uio_vmspace = l->l_proc->p_vmspace;
 	auio.uio_offset = 0;			/* XXX */
 	auio.uio_resid = 0;
 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
--- a/sys/compat/sunos/sunos_misc.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/compat/sunos/sunos_misc.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: sunos_misc.c,v 1.135 2005/12/24 23:41:33 perry Exp $	*/
+/*	$NetBSD: sunos_misc.c,v 1.136 2006/03/01 12:38:12 yamt Exp $	*/
 
 /*
  * Copyright (c) 1992, 1993
@@ -50,7 +50,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sunos_misc.c,v 1.135 2005/12/24 23:41:33 perry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sunos_misc.c,v 1.136 2006/03/01 12:38:12 yamt Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_nfsserver.h"
@@ -525,10 +525,9 @@
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_SYSSPACE;
-	auio.uio_lwp = NULL;
 	auio.uio_resid = buflen;
 	auio.uio_offset = off;
+	UIO_SETUP_SYSSPACE(&auio);
 	/*
 	 * First we read into the malloc'ed buffer, then
 	 * we massage it into user space, one record at a time.
--- a/sys/compat/sunos32/sunos32_misc.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/compat/sunos32/sunos32_misc.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: sunos32_misc.c,v 1.35 2005/12/24 23:41:33 perry Exp $	*/
+/*	$NetBSD: sunos32_misc.c,v 1.36 2006/03/01 12:38:12 yamt Exp $	*/
 /* from :NetBSD: sunos_misc.c,v 1.107 2000/12/01 19:25:10 jdolecek Exp	*/
 
 /*
@@ -79,7 +79,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sunos32_misc.c,v 1.35 2005/12/24 23:41:33 perry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sunos32_misc.c,v 1.36 2006/03/01 12:38:12 yamt Exp $");
 
 #define COMPAT_SUNOS 1
 
@@ -752,10 +752,9 @@
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_SYSSPACE;
-	auio.uio_lwp = NULL;
 	auio.uio_resid = buflen;
 	auio.uio_offset = off;
+	UIO_SETUP_SYSSPACE(&auio);
 	/*
 	 * First we read into the malloc'ed buffer, then
 	 * we massage it into user space, one record at a time.
--- a/sys/compat/svr4/svr4_misc.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/compat/svr4/svr4_misc.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: svr4_misc.c,v 1.115 2005/12/24 20:45:08 perry Exp $	 */
+/*	$NetBSD: svr4_misc.c,v 1.116 2006/03/01 12:38:12 yamt Exp $	 */
 
 /*-
  * Copyright (c) 1994 The NetBSD Foundation, Inc.
@@ -44,7 +44,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: svr4_misc.c,v 1.115 2005/12/24 20:45:08 perry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: svr4_misc.c,v 1.116 2006/03/01 12:38:12 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -299,10 +299,9 @@
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_SYSSPACE;
-	auio.uio_lwp = NULL;
 	auio.uio_resid = buflen;
 	auio.uio_offset = off;
+	UIO_SETUP_SYSSPACE(&auio);
 	/*
          * First we read into the malloc'ed buffer, then
          * we massage it into user space, one record at a time.
@@ -425,10 +424,9 @@
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_SYSSPACE;
-	auio.uio_lwp = NULL;
 	auio.uio_resid = buflen;
 	auio.uio_offset = off;
+	UIO_SETUP_SYSSPACE(&auio);
 	/*
          * First we read into the malloc'ed buffer, then
          * we massage it into user space, one record at a time.
--- a/sys/compat/svr4_32/svr4_32_misc.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/compat/svr4_32/svr4_32_misc.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: svr4_32_misc.c,v 1.34 2005/12/24 20:45:09 perry Exp $	 */
+/*	$NetBSD: svr4_32_misc.c,v 1.35 2006/03/01 12:38:12 yamt Exp $	 */
 
 /*-
  * Copyright (c) 1994 The NetBSD Foundation, Inc.
@@ -44,7 +44,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: svr4_32_misc.c,v 1.34 2005/12/24 20:45:09 perry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: svr4_32_misc.c,v 1.35 2006/03/01 12:38:12 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -303,10 +303,9 @@
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_SYSSPACE;
-	auio.uio_lwp = NULL;
 	auio.uio_resid = buflen;
 	auio.uio_offset = off;
+	UIO_SETUP_SYSSPACE(&auio);
 	/*
          * First we read into the malloc'ed buffer, then
          * we massage it into user space, one record at a time.
@@ -429,10 +428,9 @@
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_SYSSPACE;
-	auio.uio_lwp = NULL;
 	auio.uio_resid = buflen;
 	auio.uio_offset = off;
+	UIO_SETUP_SYSSPACE(&auio);
 	/*
          * First we read into the malloc'ed buffer, then
          * we massage it into user space, one record at a time.
--- a/sys/dev/ata/wd.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/dev/ata/wd.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: wd.c,v 1.319 2006/02/20 16:50:36 thorpej Exp $ */
+/*	$NetBSD: wd.c,v 1.320 2006/03/01 12:38:13 yamt Exp $ */
 
 /*
  * Copyright (c) 1998, 2001 Manuel Bouyer.  All rights reserved.
@@ -66,7 +66,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: wd.c,v 1.319 2006/02/20 16:50:36 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: wd.c,v 1.320 2006/03/01 12:38:13 yamt Exp $");
 
 #ifndef ATADEBUG
 #define ATADEBUG
@@ -1340,10 +1340,9 @@
 		auio.uio_iov = &aiov;
 		auio.uio_iovcnt = 1;
 		auio.uio_resid = fop->df_count;
-		auio.uio_segflg = 0;
 		auio.uio_offset =
 			fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
-		auio.uio_lwp = l;
+		auio.uio_vmspace = l->l_proc->p_vmspace;
 		error = physio(wdformat, NULL, dev, B_WRITE, minphys,
 		    &auio);
 		fop->df_count -= auio.uio_resid;
@@ -1385,10 +1384,9 @@
 			wi->wi_uio.uio_iovcnt = 1;
 			wi->wi_uio.uio_resid = atareq->datalen;
 			wi->wi_uio.uio_offset = 0;
-			wi->wi_uio.uio_segflg = UIO_USERSPACE;
 			wi->wi_uio.uio_rw =
 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
-			wi->wi_uio.uio_lwp = l;
+			wi->wi_uio.uio_vmspace = l->l_proc->p_vmspace;
 			error1 = physio(wdioctlstrategy, &wi->wi_bp, dev,
 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
 			    minphys, &wi->wi_uio);
--- a/sys/dev/audiobell.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/dev/audiobell.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: audiobell.c,v 1.2 2005/12/11 12:20:53 christos Exp $	*/
+/*	$NetBSD: audiobell.c,v 1.3 2006/03/01 12:38:13 yamt Exp $	*/
 
 /*
  * Copyright (c) 1999 Richard Earnshaw
@@ -31,7 +31,7 @@
  */
 
 #include <sys/types.h>
-__KERNEL_RCSID(0, "$NetBSD: audiobell.c,v 1.2 2005/12/11 12:20:53 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audiobell.c,v 1.3 2006/03/01 12:38:13 yamt Exp $");
 
 #include <sys/audioio.h>
 #include <sys/conf.h>
@@ -158,9 +158,8 @@
 	auio.uio_iovcnt = 1;
 	auio.uio_offset = 0;
 	auio.uio_resid = period * 8;
-	auio.uio_segflg = UIO_SYSSPACE;
 	auio.uio_rw = UIO_WRITE;
-	auio.uio_lwp = NULL;
+	UIO_SETUP_SYSSPACE(&auio);
 
 	audiowrite(AUDIO_DEVICE | audio->dv_unit, &auio, 0);
 
--- a/sys/dev/dkwedge/dk.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/dev/dkwedge/dk.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.11 2006/01/04 10:13:05 yamt Exp $	*/
+/*	$NetBSD: dk.c,v 1.12 2006/03/01 12:38:13 yamt Exp $	*/
 
 /*-
  * Copyright (c) 2004 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.11 2006/01/04 10:13:05 yamt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.12 2006/03/01 12:38:13 yamt Exp $");
 
 #include "opt_dkwedge.h"
 
@@ -588,6 +588,7 @@
 	struct iovec iov;
 	struct dkwedge_softc *sc;
 	struct dkwedge_info dkw;
+	struct vmspace *vm;
 	int error = 0;
 
 	iov.iov_base = dkwl->dkwl_buf;
@@ -597,9 +598,16 @@
 	uio.uio_iovcnt = 1;
 	uio.uio_offset = 0;
 	uio.uio_resid = dkwl->dkwl_bufsize;
-	uio.uio_segflg = l != NULL ? UIO_USERSPACE : UIO_SYSSPACE;
 	uio.uio_rw = UIO_READ;
-	uio.uio_lwp = l;
+	if (l == NULL) {
+		UIO_SETUP_SYSSPACE(&uio);
+	} else {
+		error = proc_vmspace_getref(l->l_proc, &vm);
+		if (error) {
+			return error;
+		}
+		uio.uio_vmspace = vm;
+	}
 
 	dkwl->dkwl_ncopied = 0;
 
@@ -627,6 +635,10 @@
 	dkwl->dkwl_nwedges = pdk->dk_nwedges;
 	(void) lockmgr(&pdk->dk_openlock, LK_RELEASE, NULL);
 
+	if (l != NULL) {
+		uvmspace_free(vm);
+	}
+
 	return (error);
 }
 
--- a/sys/dev/dmover/dmover_io.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/dev/dmover/dmover_io.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: dmover_io.c,v 1.20 2006/01/17 12:10:51 yamt Exp $	*/
+/*	$NetBSD: dmover_io.c,v 1.21 2006/03/01 12:38:13 yamt Exp $	*/
 
 /*
  * Copyright (c) 2002, 2003 Wasabi Systems, Inc.
@@ -55,7 +55,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: dmover_io.c,v 1.20 2006/01/17 12:10:51 yamt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dmover_io.c,v 1.21 2006/03/01 12:38:13 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/queue.h>
@@ -70,16 +70,26 @@
 #include <sys/filio.h>
 #include <sys/select.h>
 #include <sys/systm.h>
+#include <sys/workqueue.h>
+#include <sys/once.h>
+
+#include <uvm/uvm_extern.h>
 
 #include <dev/dmover/dmovervar.h>
 #include <dev/dmover/dmover_io.h>
 
 struct dmio_usrreq_state {
-	TAILQ_ENTRY(dmio_usrreq_state) dus_q;
+	union {
+		struct work u_work;
+		TAILQ_ENTRY(dmio_usrreq_state) u_q;
+	} dus_u;
+#define	dus_q		dus_u.u_q
+#define	dus_work	dus_u.u_work
 	struct uio dus_uio_out;
 	struct uio *dus_uio_in;
 	struct dmover_request *dus_req;
 	uint32_t dus_id;
+	struct vmspace *dus_vmspace;
 };
 
 struct dmio_state {
@@ -92,6 +102,11 @@
 	struct simplelock ds_slock;
 };
 
+static ONCE_DECL(dmio_cleaner_control);
+static struct workqueue *dmio_cleaner;
+static int dmio_cleaner_init(void);
+static void dmio_usrreq_fini1(struct work *wk, void *);
+
 #define	DMIO_STATE_SEL		0x0001
 #define	DMIO_STATE_DEAD		0x0002
 #define	DMIO_STATE_LARVAL	0x0004
@@ -128,6 +143,19 @@
 }
 
 /*
+ * dmio_cleaner_init:
+ *
+ *	Create cleaner thread.
+ */
+static int
+dmio_cleaner_init(void)
+{
+
+	return workqueue_create(&dmio_cleaner, "dmioclean", dmio_usrreq_fini1,
+	    NULL, PWAIT, 0 /* IPL_SOFTCLOCK */, 0);
+}
+
+/*
  * dmio_usrreq_init:
  *
  *	Build a request structure.
@@ -147,6 +175,16 @@
 
 	/* XXX How should malloc interact w/ FNONBLOCK? */
 
+	error = RUN_ONCE(&dmio_cleaner_control, dmio_cleaner_init);
+	if (error) {
+		return error;
+	}
+
+	error = proc_vmspace_getref(curproc, &dus->dus_vmspace);
+	if (error) {
+		return error;
+	}
+
 	if (req->req_outbuf.dmbuf_iovcnt != 0) {
 		if (req->req_outbuf.dmbuf_iovcnt > IOV_MAX)
 			return (EINVAL);
@@ -170,8 +208,8 @@
 		uio_out->uio_iovcnt = req->req_outbuf.dmbuf_iovcnt;
 		uio_out->uio_resid = len;
 		uio_out->uio_rw = UIO_READ;
-		uio_out->uio_segflg = UIO_USERSPACE;
-		uio_out->uio_lwp = curlwp;
+		uio_out->uio_vmspace = dus->dus_vmspace;
+
 		dreq->dreq_outbuf_type = DMOVER_BUF_UIO;
 		dreq->dreq_outbuf.dmbuf_uio = uio_out;
 	} else {
@@ -236,8 +274,7 @@
 		uio_in->uio_iovcnt = inbuf.dmbuf_iovcnt;
 		uio_in->uio_resid = len;
 		uio_in->uio_rw = UIO_WRITE;
-		uio_in->uio_segflg = UIO_USERSPACE;
-		uio_in->uio_lwp = curlwp;
+		uio_in->uio_vmspace = dus->dus_vmspace;
 
 		dreq->dreq_inbuf[i].dmbuf_uio = uio_in;
 	}
@@ -254,6 +291,7 @@
 	free(dus->dus_uio_in, M_TEMP);
 	if (uio_out != NULL)
 		free(uio_out->uio_iov, M_TEMP);
+	uvmspace_free(dus->dus_vmspace);
 	return (error);
 }
 
@@ -273,19 +311,29 @@
 	if (uio_out->uio_iov != NULL)
 		free(uio_out->uio_iov, M_TEMP);
 
-	if (dses->dses_ninputs == 0) {
-		pool_put(&dmio_usrreq_state_pool, dus);
-		return;
+	if (dses->dses_ninputs) {
+		for (i = 0; i < dses->dses_ninputs; i++) {
+			uio_in = &dus->dus_uio_in[i];
+			free(uio_in->uio_iov, M_TEMP);
+		}
+		free(dus->dus_uio_in, M_TEMP);
 	}
 
-	for (i = 0; i < dses->dses_ninputs; i++) {
-		uio_in = &dus->dus_uio_in[i];
-		free(uio_in->uio_iov, M_TEMP);
-	}
+	workqueue_enqueue(dmio_cleaner, &dus->dus_work);
+}
 
-	free(dus->dus_uio_in, M_TEMP);
+static void
+dmio_usrreq_fini1(struct work *wk, void *dummy)
+{
+	struct dmio_usrreq_state *dus = (void *)wk;
+	int s;
 
+	KASSERT(wk == &dus->dus_work);
+
+	uvmspace_free(dus->dus_vmspace);
+	s = splsoftclock();
 	pool_put(&dmio_usrreq_state_pool, dus);
+	splx(s);
 }
 
 /*
--- a/sys/dev/scsipi/scsipi_ioctl.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/dev/scsipi/scsipi_ioctl.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: scsipi_ioctl.c,v 1.57 2005/12/14 21:55:47 reinoud Exp $	*/
+/*	$NetBSD: scsipi_ioctl.c,v 1.58 2006/03/01 12:38:13 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
@@ -44,7 +44,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: scsipi_ioctl.c,v 1.57 2005/12/14 21:55:47 reinoud Exp $");
+__KERNEL_RCSID(0, "$NetBSD: scsipi_ioctl.c,v 1.58 2006/03/01 12:38:13 yamt Exp $");
 
 #include "opt_compat_freebsd.h"
 #include "opt_compat_netbsd.h"
@@ -337,10 +337,13 @@
 			si->si_uio.uio_iovcnt = 1;
 			si->si_uio.uio_resid = len;
 			si->si_uio.uio_offset = 0;
-			si->si_uio.uio_segflg = (flag & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE;
 			si->si_uio.uio_rw =
 			    (screq->flags & SCCMD_READ) ? UIO_READ : UIO_WRITE;
-			si->si_uio.uio_lwp = l;
+			if ((flag & FKIOCTL) == 0) {
+				si->si_uio.uio_vmspace = l->l_proc->p_vmspace;
+			} else {
+				UIO_SETUP_SYSSPACE(&si->si_uio);
+			}
 			error = physio(scsistrategy, &si->si_bp, dev,
 			    (screq->flags & SCCMD_READ) ? B_READ : B_WRITE,
 			    periph->periph_channel->chan_adapter->adapt_minphys,
--- a/sys/dev/usb/ugen.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/dev/usb/ugen.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: ugen.c,v 1.78 2005/12/11 12:24:01 christos Exp $	*/
+/*	$NetBSD: ugen.c,v 1.79 2006/03/01 12:38:13 yamt Exp $	*/
 
 /*
  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
@@ -39,7 +39,7 @@
 
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ugen.c,v 1.78 2005/12/11 12:24:01 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ugen.c,v 1.79 2006/03/01 12:38:13 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -1225,9 +1225,8 @@
 		uio.uio_iovcnt = 1;
 		uio.uio_resid = len;
 		uio.uio_offset = 0;
-		uio.uio_segflg = UIO_USERSPACE;
 		uio.uio_rw = UIO_READ;
-		uio.uio_lwp = l;
+		uio.uio_vmspace = l->l_proc->p_vmspace;
 		error = uiomove((void *)cdesc, len, &uio);
 		free(cdesc, M_TEMP);
 		return (error);
@@ -1271,11 +1270,10 @@
 			uio.uio_iovcnt = 1;
 			uio.uio_resid = len;
 			uio.uio_offset = 0;
-			uio.uio_segflg = UIO_USERSPACE;
 			uio.uio_rw =
 				ur->ucr_request.bmRequestType & UT_READ ?
 				UIO_READ : UIO_WRITE;
-			uio.uio_lwp = l;
+			uio.uio_vmspace = l->l_proc->p_vmspace;
 			ptr = malloc(len, M_TEMP, M_WAITOK);
 			if (uio.uio_rw == UIO_WRITE) {
 				error = uiomove(ptr, len, &uio);
--- a/sys/dev/usb/urio.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/dev/usb/urio.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: urio.c,v 1.21 2005/12/11 12:24:01 christos Exp $	*/
+/*	$NetBSD: urio.c,v 1.22 2006/03/01 12:38:13 yamt Exp $	*/
 
 /*
  * Copyright (c) 2000 The NetBSD Foundation, Inc.
@@ -43,7 +43,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: urio.c,v 1.21 2005/12/11 12:24:01 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: urio.c,v 1.22 2006/03/01 12:38:13 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -548,10 +548,9 @@
 		uio.uio_iovcnt = 1;
 		uio.uio_resid = len;
 		uio.uio_offset = 0;
-		uio.uio_segflg = UIO_USERSPACE;
 		uio.uio_rw = req.bmRequestType & UT_READ ?
 			     UIO_READ : UIO_WRITE;
-		uio.uio_lwp = l;
+		uio.uio_vmspace = l->l_proc->p_vmspace;
 		ptr = malloc(len, M_TEMP, M_WAITOK);
 		if (uio.uio_rw == UIO_WRITE) {
 			error = uiomove(ptr, len, &uio);
--- a/sys/dev/usb/usb.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/dev/usb/usb.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: usb.c,v 1.84 2005/12/11 12:24:01 christos Exp $	*/
+/*	$NetBSD: usb.c,v 1.85 2006/03/01 12:38:13 yamt Exp $	*/
 
 /*
  * Copyright (c) 1998, 2002 The NetBSD Foundation, Inc.
@@ -44,7 +44,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.84 2005/12/11 12:24:01 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.85 2006/03/01 12:38:13 yamt Exp $");
 
 #include "ohci.h"
 #include "uhci.h"
@@ -518,11 +518,10 @@
 			uio.uio_iovcnt = 1;
 			uio.uio_resid = len;
 			uio.uio_offset = 0;
-			uio.uio_segflg = UIO_USERSPACE;
 			uio.uio_rw =
 				ur->ucr_request.bmRequestType & UT_READ ?
 				UIO_READ : UIO_WRITE;
-			uio.uio_lwp = l;
+			uio.uio_vmspace = l->l_proc->p_vmspace;
 			ptr = malloc(len, M_TEMP, M_WAITOK);
 			if (uio.uio_rw == UIO_WRITE) {
 				error = uiomove(ptr, len, &uio);
--- a/sys/dev/vnd.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/dev/vnd.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: vnd.c,v 1.140 2006/02/04 13:40:38 yamt Exp $	*/
+/*	$NetBSD: vnd.c,v 1.141 2006/03/01 12:38:13 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
@@ -133,7 +133,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vnd.c,v 1.140 2006/02/04 13:40:38 yamt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vnd.c,v 1.141 2006/03/01 12:38:13 yamt Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "fs_nfs.h"
@@ -1308,8 +1308,8 @@
 	auio.uio_iovcnt = 1;
 	auio.uio_offset = 0;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_SYSSPACE;
 	auio.uio_resid = aiov.iov_len;
+	UIO_SETUP_SYSSPACE(&auio);
 	vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
 	error = VOP_READ(vnd->sc_vp, &auio, 0, vnd->sc_cred);
 	if (error == 0) {
@@ -1607,7 +1607,7 @@
 
 	/* set up constants for data move */
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_SYSSPACE;
+	UIO_SETUP_SYSSPACE(&auio);
 
 	/* read, and transfer the data */
 	addr = bp->b_data;
--- a/sys/fs/cd9660/cd9660_vnops.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/fs/cd9660/cd9660_vnops.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: cd9660_vnops.c,v 1.19 2005/12/11 12:24:25 christos Exp $	*/
+/*	$NetBSD: cd9660_vnops.c,v 1.20 2006/03/01 12:38:13 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1994
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: cd9660_vnops.c,v 1.19 2005/12/11 12:24:25 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cd9660_vnops.c,v 1.20 2006/03/01 12:38:13 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -218,9 +218,8 @@
 		auio.uio_iovcnt = 1;
 		auio.uio_offset = 0;
 		auio.uio_rw = UIO_READ;
-		auio.uio_segflg = UIO_SYSSPACE;
-		auio.uio_lwp = NULL;
 		auio.uio_resid = MAXPATHLEN;
+		UIO_SETUP_SYSSPACE(&auio);
 		rdlnk.a_uio = &auio;
 		rdlnk.a_vp = ap->a_vp;
 		rdlnk.a_cred = ap->a_cred;
@@ -633,6 +632,7 @@
 	u_short	symlen;
 	int	error;
 	char	*symname;
+	boolean_t use_pnbuf;
 
 	ip  = VTOI(ap->a_vp);
 	imp = ip->i_mnt;
@@ -672,19 +672,21 @@
 	 * Now get a buffer
 	 * Abuse a namei buffer for now.
 	 */
-	if (uio->uio_segflg == UIO_SYSSPACE &&
-	    uio->uio_iov->iov_len >= MAXPATHLEN)
+	use_pnbuf = !VMSPACE_IS_KERNEL_P(uio->uio_vmspace) ||
+	    uio->uio_iov->iov_len < MAXPATHLEN;
+	if (use_pnbuf) {
+		symname = PNBUF_GET();
+	} else {
 		symname = uio->uio_iov->iov_base;
-	else
-		symname = PNBUF_GET();
+	}
 
 	/*
 	 * Ok, we just gathering a symbolic name in SL record.
 	 */
 	if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
-		if (uio->uio_segflg != UIO_SYSSPACE ||
-		    uio->uio_iov->iov_len < MAXPATHLEN)
+		if (use_pnbuf) {
 			PNBUF_PUT(symname);
+		}
 		brelse(bp);
 		return (EINVAL);
 	}
@@ -696,8 +698,7 @@
 	/*
 	 * return with the symbolic name to caller's.
 	 */
-	if (uio->uio_segflg != UIO_SYSSPACE ||
-	    uio->uio_iov->iov_len < MAXPATHLEN) {
+	if (use_pnbuf) {
 		error = uiomove(symname, symlen, uio);
 		PNBUF_PUT(symname);
 		return (error);
--- a/sys/fs/msdosfs/msdosfs_vnops.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/fs/msdosfs/msdosfs_vnops.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: msdosfs_vnops.c,v 1.25 2006/01/14 23:49:59 christos Exp $	*/
+/*	$NetBSD: msdosfs_vnops.c,v 1.26 2006/03/01 12:38:13 yamt Exp $	*/
 
 /*-
  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
@@ -48,7 +48,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: msdosfs_vnops.c,v 1.25 2006/01/14 23:49:59 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: msdosfs_vnops.c,v 1.26 2006/03/01 12:38:13 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -561,7 +561,7 @@
 	vsize_t bytelen;
 	off_t oldoff;
 	struct uio *uio = ap->a_uio;
-	struct proc *p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
+	struct proc *p = curproc;
 	struct vnode *vp = ap->a_vp;
 	struct denode *dep = VTODE(vp);
 	struct msdosfsmount *pmp = dep->de_pmp;
@@ -599,8 +599,7 @@
 	/*
 	 * If they've exceeded their filesize limit, tell them about it.
 	 */
-	if (p &&
-	    ((uio->uio_offset + uio->uio_resid) >
+	if (((uio->uio_offset + uio->uio_resid) >
 	    p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
 		psignal(p, SIGXFSZ);
 		return (EFBIG);
--- a/sys/fs/ntfs/ntfs_vnops.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/fs/ntfs/ntfs_vnops.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: ntfs_vnops.c,v 1.27 2005/12/11 12:24:29 christos Exp $	*/
+/*	$NetBSD: ntfs_vnops.c,v 1.28 2006/03/01 12:38:21 yamt Exp $	*/
 
 /*
  * Copyright (c) 1992, 1993
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ntfs_vnops.c,v 1.27 2005/12/11 12:24:29 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ntfs_vnops.c,v 1.28 2006/03/01 12:38:21 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -155,9 +155,9 @@
 	u_int64_t toread;
 	int error;
 
-	dprintf(("ntfs_read: ino: %llu, off: %qd resid: %qd, segflg: %d\n",
+	dprintf(("ntfs_read: ino: %llu, off: %qd resid: %qd\n",
 	    (unsigned long long)ip->i_number, (long long)uio->uio_offset,
-	    (long long)uio->uio_resid, uio->uio_segflg));
+	    (long long)uio->uio_resid));
 
 	dprintf(("ntfs_read: filesize: %qu",(long long)fp->f_size));
 
@@ -421,9 +421,9 @@
 	size_t written;
 	int error;
 
-	dprintf(("ntfs_write: ino: %llu, off: %qd resid: %qd, segflg: %d\n",
+	dprintf(("ntfs_write: ino: %llu, off: %qd resid: %qd\n",
 	    (unsigned long long)ip->i_number, (long long)uio->uio_offset,
-	    (long long)uio->uio_resid, uio->uio_segflg));
+	    (long long)uio->uio_resid));
 	dprintf(("ntfs_write: filesize: %qu",(long long)fp->f_size));
 
 	if (uio->uio_resid + uio->uio_offset > fp->f_size) {
@@ -697,7 +697,8 @@
 #endif
 
 		dprintf(("ntfs_readdir: %d cookies\n",ncookies));
-		if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
+		if (!VMSPACE_IS_KERNEL_P(uio->uio_vmspace) ||
+		    uio->uio_iovcnt != 1)
 			panic("ntfs_readdir: unexpected uio from NFS server");
 		dpStart = (struct dirent *)
 		     ((caddr_t)uio->uio_iov->iov_base -
--- a/sys/fs/smbfs/smbfs_io.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/fs/smbfs/smbfs_io.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: smbfs_io.c,v 1.21 2006/01/12 13:36:17 yamt Exp $	*/
+/*	$NetBSD: smbfs_io.c,v 1.22 2006/03/01 12:38:21 yamt Exp $	*/
 
 /*
  * Copyright (c) 2000-2001, Boris Popov
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: smbfs_io.c,v 1.21 2006/01/12 13:36:17 yamt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: smbfs_io.c,v 1.22 2006/03/01 12:38:21 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -311,7 +311,7 @@
 
 	uiop->uio_iov = &io;
 	uiop->uio_iovcnt = 1;
-	uiop->uio_segflg = UIO_SYSSPACE;
+	UIO_SETUP_SYSSPACE(uiop);
 
 	smb_makescred(&scred, l, cr);
 
--- a/sys/fs/udf/udf_subr.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/fs/udf/udf_subr.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: udf_subr.c,v 1.4 2006/02/04 23:21:43 reinoud Exp $ */
+/* $NetBSD: udf_subr.c,v 1.5 2006/03/01 12:38:21 yamt Exp $ */
 
 /*
  * Copyright (c) 2006 Reinoud Zandijk
@@ -36,7 +36,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: udf_subr.c,v 1.4 2006/02/04 23:21:43 reinoud Exp $");
+__RCSID("$NetBSD: udf_subr.c,v 1.5 2006/03/01 12:38:21 yamt Exp $");
 #endif /* not lint */
 
 
@@ -2401,7 +2401,7 @@
 	dir_uio.uio_rw     = UIO_READ;	/* read into this space */
 	dir_uio.uio_iovcnt = 1;
 	dir_uio.uio_iov    = &dir_iovec;
-	dir_uio.uio_segflg = UIO_SYSSPACE;
+	UIO_SETUP_SYSSPACE(&dir_uio);
 	dir_iovec.iov_base = fid;
 	dir_iovec.iov_len  = lb_size;
 	dir_uio.uio_offset = *offset;
--- a/sys/fs/union/union_subr.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/fs/union/union_subr.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: union_subr.c,v 1.15 2005/12/11 12:24:29 christos Exp $	*/
+/*	$NetBSD: union_subr.c,v 1.16 2006/03/01 12:38:21 yamt Exp $	*/
 
 /*
  * Copyright (c) 1994
@@ -72,7 +72,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: union_subr.c,v 1.15 2005/12/11 12:24:29 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: union_subr.c,v 1.16 2006/03/01 12:38:21 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -637,9 +637,8 @@
 	 * give up at the first sign of trouble.
 	 */
 
-	uio.uio_lwp = NULL;
-	uio.uio_segflg = UIO_SYSSPACE;
 	uio.uio_offset = 0;
+	UIO_SETUP_SYSSPACE(&uio);
 
 	VOP_UNLOCK(fvp, 0);			/* XXX */
 	VOP_LEASE(fvp, l, cred, LEASE_READ);
--- a/sys/kern/init_sysctl.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/kern/init_sysctl.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: init_sysctl.c,v 1.62 2006/02/04 12:09:50 yamt Exp $ */
+/*	$NetBSD: init_sysctl.c,v 1.63 2006/03/01 12:38:21 yamt Exp $ */
 
 /*-
  * Copyright (c) 2003 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: init_sysctl.c,v 1.62 2006/02/04 12:09:50 yamt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: init_sysctl.c,v 1.63 2006/03/01 12:38:21 yamt Exp $");
 
 #include "opt_sysv.h"
 #include "opt_multiprocessor.h"
@@ -2440,9 +2440,8 @@
 	auio.uio_iovcnt = 1;
 	auio.uio_offset = psstr_addr;
 	auio.uio_resid = sizeof(pss);
-	auio.uio_segflg = UIO_SYSSPACE;
 	auio.uio_rw = UIO_READ;
-	auio.uio_lwp = NULL;
+	UIO_SETUP_SYSSPACE(&auio);
 	error = uvm_io(&vmspace->vm_map, &auio);
 	if (error)
 		goto done;
@@ -2472,9 +2471,8 @@
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_resid = sizeof(argv);
-	auio.uio_segflg = UIO_SYSSPACE;
 	auio.uio_rw = UIO_READ;
-	auio.uio_lwp = NULL;
+	UIO_SETUP_SYSSPACE(&auio);
 	error = uvm_io(&vmspace->vm_map, &auio);
 	if (error)
 		goto done;
@@ -2494,9 +2492,8 @@
 		auio.uio_offset = argv + len;
 		xlen = PAGE_SIZE - ((argv + len) & PAGE_MASK);
 		auio.uio_resid = xlen;
-		auio.uio_segflg = UIO_SYSSPACE;
 		auio.uio_rw = UIO_READ;
-		auio.uio_lwp = NULL;
+		UIO_SETUP_SYSSPACE(&auio);
 		error = uvm_io(&vmspace->vm_map, &auio);
 		if (error)
 			goto done;
--- a/sys/kern/kern_ktrace.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/kern/kern_ktrace.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_ktrace.c,v 1.100 2005/12/24 19:12:23 perry Exp $	*/
+/*	$NetBSD: kern_ktrace.c,v 1.101 2006/03/01 12:38:21 yamt Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_ktrace.c,v 1.100 2005/12/24 19:12:23 perry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_ktrace.c,v 1.101 2006/03/01 12:38:21 yamt Exp $");
 
 #include "opt_ktrace.h"
 #include "opt_compat_mach.h"
@@ -1123,11 +1123,10 @@
 next:
 	auio.uio_iov = iov = &aiov[0];
 	auio.uio_offset = 0;
-	auio.uio_segflg = UIO_SYSSPACE;
 	auio.uio_rw = UIO_WRITE;
 	auio.uio_resid = 0;
 	auio.uio_iovcnt = 0;
-	auio.uio_lwp = curlwp;
+	UIO_SETUP_SYSSPACE(&auio);
 	do {
 		kth = &kte->kte_kth;
 
--- a/sys/kern/kern_proc.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/kern/kern_proc.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_proc.c,v 1.85 2005/12/26 18:45:27 perry Exp $	*/
+/*	$NetBSD: kern_proc.c,v 1.86 2006/03/01 12:38:21 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1999 The NetBSD Foundation, Inc.
@@ -69,7 +69,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.85 2005/12/26 18:45:27 perry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.86 2006/03/01 12:38:21 yamt Exp $");
 
 #include "opt_kstack.h"
 
@@ -1231,3 +1231,20 @@
 
 	return ret;
 }
+
+int
+proc_vmspace_getref(struct proc *p, struct vmspace **vm)
+{
+
+	/* XXXCDC: how should locking work here? */
+
+	if ((p->p_flag & P_WEXIT) != 0 ||
+	    (p->p_vmspace->vm_refcnt < 1)) { /* XXX */
+		return EFAULT;
+	}
+
+	uvmspace_addref(p->p_vmspace);
+	*vm = p->p_vmspace;
+
+	return 0;
+}
--- a/sys/kern/kern_subr.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/kern/kern_subr.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_subr.c,v 1.125 2006/02/27 03:04:28 thorpej Exp $	*/
+/*	$NetBSD: kern_subr.c,v 1.126 2006/03/01 12:38:21 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
@@ -86,7 +86,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_subr.c,v 1.125 2006/02/27 03:04:28 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_subr.c,v 1.126 2006/03/01 12:38:21 yamt Exp $");
 
 #include "opt_ddb.h"
 #include "opt_md.h"
@@ -131,15 +131,21 @@
 
 MALLOC_DEFINE(M_IOV, "iov", "large iov's");
 
+void
+uio_setup_sysspace(struct uio *uio)
+{
+
+	uio->uio_vmspace = vmspace_kernel();
+}
+
 int
 uiomove(void *buf, size_t n, struct uio *uio)
 {
+	struct vmspace *vm = uio->uio_vmspace;
 	struct iovec *iov;
 	u_int cnt;
 	int error = 0;
 	char *cp = buf;
-	struct lwp *l;
-	struct proc *p;
 	int hold_count;
 
 	hold_count = KERNEL_LOCK_RELEASE_ALL();
@@ -164,30 +170,20 @@
 		}
 		if (cnt > n)
 			cnt = n;
-		switch (uio->uio_segflg) {
-
-		case UIO_USERSPACE:
-			l = uio->uio_lwp;
-			p = l ? l->l_proc : NULL;
-
+		if (!VMSPACE_IS_KERNEL_P(vm)) {
 			if (curcpu()->ci_schedstate.spc_flags &
 			    SPCF_SHOULDYIELD)
 				preempt(1);
-			if (uio->uio_rw == UIO_READ)
-				error = copyout_proc(p, cp, iov->iov_base, cnt);
-			else
-				error = copyin_proc(p, iov->iov_base, cp, cnt);
-			if (error)
-				goto out;
-			break;
+		}
 
-		case UIO_SYSSPACE:
-			if (uio->uio_rw == UIO_READ)
-				error = kcopy(cp, iov->iov_base, cnt);
-			else
-				error = kcopy(iov->iov_base, cp, cnt);
-			if (error)
-				goto out;
+		if (uio->uio_rw == UIO_READ) {
+			error = copyout_vmspace(vm, cp, iov->iov_base,
+			    cnt);
+		} else {
+			error = copyin_vmspace(vm, iov->iov_base, cp,
+			    cnt);
+		}
+		if (error) {
 			break;
 		}
 		iov->iov_base = (caddr_t)iov->iov_base + cnt;
@@ -198,7 +194,6 @@
 		KDASSERT(cnt <= n);
 		n -= cnt;
 	}
-out:
 	KERNEL_LOCK_ACQUIRE_COUNT(hold_count);
 	return (error);
 }
@@ -239,16 +234,11 @@
 		uio->uio_iov++;
 		goto again;
 	}
-	switch (uio->uio_segflg) {
-
-	case UIO_USERSPACE:
+	if (!VMSPACE_IS_KERNEL_P(uio->uio_vmspace)) {
 		if (subyte(iov->iov_base, c) < 0)
 			return (EFAULT);
-		break;
-
-	case UIO_SYSSPACE:
+	} else {
 		*(char *)iov->iov_base = c;
-		break;
 	}
 	iov->iov_base = (caddr_t)iov->iov_base + 1;
 	iov->iov_len--;
@@ -258,10 +248,43 @@
 }
 
 /*
- * Like copyin(), but operates on an arbitrary process.
+ * Like copyin(), but operates on an arbitrary vmspace.
  */
 int
-copyin_proc(struct proc *p, const void *uaddr, void *kaddr, size_t len)
+copyin_vmspace(struct vmspace *vm, const void *uaddr, void *kaddr, size_t len)
+{
+	struct iovec iov;
+	struct uio uio;
+	int error;
+
+	if (len == 0)
+		return (0);
+
+	if (VMSPACE_IS_KERNEL_P(vm)) {
+		return kcopy(uaddr, kaddr, len);
+	}
+	if (__predict_true(vm == curproc->p_vmspace)) {
+		return copyin(uaddr, kaddr, len);
+	}
+
+	iov.iov_base = kaddr;
+	iov.iov_len = len;
+	uio.uio_iov = &iov;
+	uio.uio_iovcnt = 1;
+	uio.uio_offset = (off_t)(intptr_t)uaddr;
+	uio.uio_resid = len;
+	uio.uio_rw = UIO_READ;
+	uio.uio_vmspace = vm;
+	error = uvm_io(&vm->vm_map, &uio);
+
+	return (error);
+}
+
+/*
+ * Like copyout(), but operates on an arbitrary vmspace.
+ */
+int
+copyout_vmspace(struct vmspace *vm, const void *kaddr, void *uaddr, size_t len)
 {
 	struct iovec iov;
 	struct uio uio;
@@ -270,63 +293,62 @@
 	if (len == 0)
 		return (0);
 
-	if (__predict_true(p == curproc))
-		return copyin(uaddr, kaddr, len);
+	if (VMSPACE_IS_KERNEL_P(vm)) {
+		return kcopy(kaddr, uaddr, len);
+	}
+	if (__predict_true(vm == curproc->p_vmspace)) {
+		return copyout(kaddr, uaddr, len);
+	}
 
-	iov.iov_base = kaddr;
+	iov.iov_base = __UNCONST(kaddr); /* XXXUNCONST cast away const */
 	iov.iov_len = len;
 	uio.uio_iov = &iov;
 	uio.uio_iovcnt = 1;
 	uio.uio_offset = (off_t)(intptr_t)uaddr;
 	uio.uio_resid = len;
-	uio.uio_segflg = UIO_SYSSPACE;
-	uio.uio_rw = UIO_READ;
-	uio.uio_lwp = NULL;
-
-	/* XXXCDC: how should locking work here? */
-	if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
-		return (EFAULT);
-	p->p_vmspace->vm_refcnt++;	/* XXX */
-	error = uvm_io(&p->p_vmspace->vm_map, &uio);
-	uvmspace_free(p->p_vmspace);
+	uio.uio_rw = UIO_WRITE;
+	uio.uio_vmspace = vm;
+	error = uvm_io(&vm->vm_map, &uio);
 
 	return (error);
 }
 
 /*
+ * Like copyin(), but operates on an arbitrary process.
+ */
+int
+copyin_proc(struct proc *p, const void *uaddr, void *kaddr, size_t len)
+{
+	struct vmspace *vm;
+	int error;
+
+	error = proc_vmspace_getref(p, &vm);
+	if (error) {
+		return error;
+	}
+	error = copyin_vmspace(vm, uaddr, kaddr, len);
+	uvmspace_free(vm);
+
+	return error;
+}
+
+/*
  * Like copyout(), but operates on an arbitrary process.
  */
 int
 copyout_proc(struct proc *p, const void *kaddr, void *uaddr, size_t len)
 {
-	struct iovec iov;
-	struct uio uio;
+	struct vmspace *vm;
 	int error;
 
-	if (len == 0)
-		return (0);
-
-	if (__predict_true(p == curproc))
-		return copyout(kaddr, uaddr, len);
+	error = proc_vmspace_getref(p, &vm);
+	if (error) {
+		return error;
+	}
+	error = copyin_vmspace(vm, kaddr, uaddr, len);
+	uvmspace_free(vm);
 
-	iov.iov_base = __UNCONST(kaddr); /* XXXUNCONST cast away const */
-	iov.iov_len = len;
-	uio.uio_iov = &iov;
-	uio.uio_iovcnt = 1;
-	uio.uio_offset = (off_t)(intptr_t)uaddr;
-	uio.uio_resid = len;
-	uio.uio_segflg = UIO_SYSSPACE;
-	uio.uio_rw = UIO_WRITE;
-	uio.uio_lwp = NULL;
-
-	/* XXXCDC: how should locking work here? */
-	if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
-		return (EFAULT);
-	p->p_vmspace->vm_refcnt++;	/* XXX */
-	error = uvm_io(&p->p_vmspace->vm_map, &uio);
-	uvmspace_free(p->p_vmspace);
-
-	return (error);
+	return error;
 }
 
 /*
--- a/sys/kern/kern_systrace.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/kern/kern_systrace.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_systrace.c,v 1.50 2005/12/27 00:26:58 chs Exp $	*/
+/*	$NetBSD: kern_systrace.c,v 1.51 2006/03/01 12:38:21 yamt Exp $	*/
 
 /*
  * Copyright 2002, 2003 Niels Provos <provos@citi.umich.edu>
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_systrace.c,v 1.50 2005/12/27 00:26:58 chs Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_systrace.c,v 1.51 2006/03/01 12:38:21 yamt Exp $");
 
 #include "opt_systrace.h"
 
@@ -1180,8 +1180,7 @@
 	uio.uio_iovcnt = 1;
 	uio.uio_offset = (off_t)(unsigned long)io->strio_offs;
 	uio.uio_resid = io->strio_len;
-	uio.uio_segflg = UIO_USERSPACE;
-	uio.uio_lwp = l;
+	uio.uio_vmspace = l->l_proc->p_vmspace;
 
 #ifdef __NetBSD__
 	error = process_domem(l, proc_representative_lwp(t), &uio);
--- a/sys/kern/sys_generic.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/kern/sys_generic.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_generic.c,v 1.84 2005/12/11 12:24:30 christos Exp $	*/
+/*	$NetBSD: sys_generic.c,v 1.85 2006/03/01 12:38:21 yamt Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_generic.c,v 1.84 2005/12/11 12:24:30 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_generic.c,v 1.85 2006/03/01 12:38:21 yamt Exp $");
 
 #include "opt_ktrace.h"
 
@@ -62,6 +62,8 @@
 #include <sys/sa.h>
 #include <sys/syscallargs.h>
 
+#include <uvm/uvm_extern.h>
+
 int selscan(struct lwp *, fd_mask *, fd_mask *, int, register_t *);
 int pollscan(struct lwp *, struct pollfd *, int, register_t *);
 
@@ -109,13 +111,18 @@
 	struct iovec aiov;
 	struct uio auio;
 	struct proc *p;
+	struct vmspace *vm;
 	size_t cnt;
 	int error;
 #ifdef KTRACE
 	struct iovec	ktriov = {0};
 #endif
 	p = l->l_proc;
-	error = 0;
+
+	error = proc_vmspace_getref(p, &vm);
+	if (error) {
+		goto out;
+	}
 
 	aiov.iov_base = (caddr_t)buf;
 	aiov.iov_len = nbyte;
@@ -123,8 +130,7 @@
 	auio.uio_iovcnt = 1;
 	auio.uio_resid = nbyte;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_USERSPACE;
-	auio.uio_lwp = l;
+	auio.uio_vmspace = vm;
 
 	/*
 	 * Reads return ssize_t because -1 is returned on error.  Therefore
@@ -157,6 +163,7 @@
 	*retval = cnt;
  out:
 	FILE_UNUSE(fp, l);
+	uvmspace_free(vm);
 	return (error);
 }
 
@@ -202,6 +209,7 @@
 	struct proc *p;
 	struct uio	auio;
 	struct iovec	*iov, *needfree, aiov[UIO_SMALLIOV];
+	struct vmspace	*vm;
 	int		i, error;
 	size_t		cnt;
 	u_int		iovlen;
@@ -210,7 +218,11 @@
 #endif
 
 	p = l->l_proc;
-	error = 0;
+	error = proc_vmspace_getref(p, &vm);
+	if (error) {
+		goto out;
+	}
+
 #ifdef KTRACE
 	ktriov = NULL;
 #endif
@@ -234,8 +246,7 @@
 	auio.uio_iov = iov;
 	auio.uio_iovcnt = iovcnt;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_USERSPACE;
-	auio.uio_lwp = l;
+	auio.uio_vmspace = vm;
 	error = copyin(iovp, iov, iovlen);
 	if (error)
 		goto done;
@@ -282,6 +293,7 @@
 		free(needfree, M_IOV);
  out:
 	FILE_UNUSE(fp, l);
+	uvmspace_free(vm);
 	return (error);
 }
 
@@ -327,6 +339,7 @@
 	struct iovec aiov;
 	struct uio auio;
 	struct proc *p;
+	struct vmspace *vm;
 	size_t cnt;
 	int error;
 #ifdef KTRACE
@@ -334,15 +347,17 @@
 #endif
 
 	p = l->l_proc;
-	error = 0;
+	error = proc_vmspace_getref(p, &vm);
+	if (error) {
+		goto out;
+	}
 	aiov.iov_base = __UNCONST(buf);		/* XXXUNCONST kills const */
 	aiov.iov_len = nbyte;
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_resid = nbyte;
 	auio.uio_rw = UIO_WRITE;
-	auio.uio_segflg = UIO_USERSPACE;
-	auio.uio_lwp = l;
+	auio.uio_vmspace = vm;
 
 	/*
 	 * Writes return ssize_t because -1 is returned on error.  Therefore
@@ -378,6 +393,7 @@
 	*retval = cnt;
  out:
 	FILE_UNUSE(fp, l);
+	uvmspace_free(vm);
 	return (error);
 }
 
@@ -423,6 +439,7 @@
 	struct proc	*p;
 	struct uio	auio;
 	struct iovec	*iov, *needfree, aiov[UIO_SMALLIOV];
+	struct vmspace	*vm;
 	int		i, error;
 	size_t		cnt;
 	u_int		iovlen;
@@ -431,7 +448,10 @@
 #endif
 
 	p = l->l_proc;
-	error = 0;
+	error = proc_vmspace_getref(p, &vm);
+	if (error) {
+		goto out;
+	}
 #ifdef KTRACE
 	ktriov = NULL;
 #endif
@@ -455,8 +475,7 @@
 	auio.uio_iov = iov;
 	auio.uio_iovcnt = iovcnt;
 	auio.uio_rw = UIO_WRITE;
-	auio.uio_segflg = UIO_USERSPACE;
-	auio.uio_lwp = l;
+	auio.uio_vmspace = vm;
 	error = copyin(iovp, iov, iovlen);
 	if (error)
 		goto done;
@@ -506,6 +525,7 @@
 		free(needfree, M_IOV);
  out:
 	FILE_UNUSE(fp, l);
+	uvmspace_free(vm);
 	return (error);
 }
 
--- a/sys/kern/sys_pipe.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/kern/sys_pipe.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_pipe.c,v 1.70 2005/12/24 19:12:23 perry Exp $	*/
+/*	$NetBSD: sys_pipe.c,v 1.71 2006/03/01 12:38:21 yamt Exp $	*/
 
 /*-
  * Copyright (c) 2003 The NetBSD Foundation, Inc.
@@ -83,7 +83,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.70 2005/12/24 19:12:23 perry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.71 2006/03/01 12:38:21 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -688,7 +688,7 @@
 
 	/* Loan the write buffer memory from writer process */
 	pgs = wpipe->pipe_map.pgs;
-	error = uvm_loan(&uio->uio_lwp->l_proc->p_vmspace->vm_map, base, blen,
+	error = uvm_loan(&uio->uio_vmspace->vm_map, base, blen,
 			 pgs, UVM_LOAN_TOPAGE);
 	if (error) {
 		pipe_loan_free(wpipe);
--- a/sys/kern/sys_process.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/kern/sys_process.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_process.c,v 1.97 2005/12/11 12:24:30 christos Exp $	*/
+/*	$NetBSD: sys_process.c,v 1.98 2006/03/01 12:38:21 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -89,7 +89,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_process.c,v 1.97 2005/12/11 12:24:30 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_process.c,v 1.98 2006/03/01 12:38:21 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -133,6 +133,7 @@
 	struct iovec iov;
 	struct ptrace_io_desc piod;
 	struct ptrace_lwpinfo pl;
+	struct vmspace *vm;
 	int s, error, write, tmp, size;
 	char *path;
 
@@ -311,9 +312,8 @@
 		uio.uio_iovcnt = 1;
 		uio.uio_offset = (off_t)(unsigned long)SCARG(uap, addr);
 		uio.uio_resid = sizeof(tmp);
-		uio.uio_segflg = UIO_SYSSPACE;
 		uio.uio_rw = write ? UIO_WRITE : UIO_READ;
-		uio.uio_lwp = NULL;
+		UIO_SETUP_SYSSPACE(&uio);
 		error = process_domem(l, lt, &uio);
 		if (!write)
 			*retval = tmp;
@@ -329,8 +329,11 @@
 		uio.uio_iovcnt = 1;
 		uio.uio_offset = (off_t)(unsigned long)piod.piod_offs;
 		uio.uio_resid = piod.piod_len;
-		uio.uio_segflg = UIO_USERSPACE;
-		uio.uio_lwp = l;
+		error = proc_vmspace_getref(l->l_proc, &vm);
+		if (error) {
+			return error;
+		}
+		uio.uio_vmspace = vm;
 		switch (piod.piod_op) {
 		case PIOD_READ_D:
 		case PIOD_READ_I:
@@ -346,6 +349,7 @@
 		error = process_domem(l, lt, &uio);
 		piod.piod_len -= uio.uio_resid;
 		(void) copyout(&piod, SCARG(uap, addr), sizeof(piod));
+		uvmspace_free(vm);
 		return (error);
 
 	case  PT_DUMPCORE:
@@ -526,16 +530,21 @@
 		if (!process_validregs(proc_representative_lwp(t)))
 			return (EINVAL);
 		else {
+			error = proc_vmspace_getref(l->l_proc, &vm);
+			if (error) {
+				return error;
+			}
 			iov.iov_base = SCARG(uap, addr);
 			iov.iov_len = sizeof(struct reg);
 			uio.uio_iov = &iov;
 			uio.uio_iovcnt = 1;
 			uio.uio_offset = 0;
 			uio.uio_resid = sizeof(struct reg);
-			uio.uio_segflg = UIO_USERSPACE;
 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
-			uio.uio_lwp = l;
-			return (process_doregs(l, lt, &uio));
+			uio.uio_vmspace = vm;
+			error = process_doregs(l, lt, &uio);
+			uvmspace_free(vm);
+			return error;
 		}
 #endif
 
@@ -559,16 +568,21 @@
 		if (!process_validfpregs(proc_representative_lwp(t)))
 			return (EINVAL);
 		else {
+			error = proc_vmspace_getref(l->l_proc, &vm);
+			if (error) {
+				return error;
+			}
 			iov.iov_base = SCARG(uap, addr);
 			iov.iov_len = sizeof(struct fpreg);
 			uio.uio_iov = &iov;
 			uio.uio_iovcnt = 1;
 			uio.uio_offset = 0;
 			uio.uio_resid = sizeof(struct fpreg);
-			uio.uio_segflg = UIO_USERSPACE;
 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
-			uio.uio_lwp = l;
-			return (process_dofpregs(l, lt, &uio));
+			uio.uio_vmspace = vm;
+			error = process_dofpregs(l, lt, &uio);
+			uvmspace_free(vm);
+			return error;
 		}
 #endif
 
--- a/sys/kern/sys_socket.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/kern/sys_socket.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_socket.c,v 1.45 2005/12/11 12:24:30 christos Exp $	*/
+/*	$NetBSD: sys_socket.c,v 1.46 2006/03/01 12:38:21 yamt Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1990, 1993
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_socket.c,v 1.45 2005/12/11 12:24:30 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_socket.c,v 1.46 2006/03/01 12:38:21 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -70,8 +70,8 @@
     int flags)
 {
 	struct socket *so = (struct socket *) fp->f_data;
-	return ((*so->so_send)(so, (struct mbuf *)0,
-		uio, (struct mbuf *)0, (struct mbuf *)0, 0, uio->uio_lwp));
+	return (*so->so_send)(so, (struct mbuf *)0,
+		uio, (struct mbuf *)0, (struct mbuf *)0, 0, curlwp);
 }
 
 int
--- a/sys/kern/tty_tty.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/kern/tty_tty.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: tty_tty.c,v 1.26 2005/12/11 12:24:30 christos Exp $	*/
+/*	$NetBSD: tty_tty.c,v 1.27 2006/03/01 12:38:21 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1982, 1986, 1991, 1993, 1995
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: tty_tty.c,v 1.26 2005/12/11 12:24:30 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tty_tty.c,v 1.27 2006/03/01 12:38:21 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -81,7 +81,7 @@
 static int
 cttyread(dev_t dev, struct uio *uio, int flag)
 {
-	struct vnode *ttyvp = cttyvp(uio->uio_lwp->l_proc);
+	struct vnode *ttyvp = cttyvp(curproc);
 	int error;
 
 	if (ttyvp == NULL)
@@ -96,7 +96,7 @@
 static int
 cttywrite(dev_t dev, struct uio *uio, int flag)
 {
-	struct vnode *ttyvp = cttyvp(uio->uio_lwp->l_proc);
+	struct vnode *ttyvp = cttyvp(curproc);
 	struct mount *mp;
 	int error;
 
--- a/sys/kern/uipc_socket.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/kern/uipc_socket.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: uipc_socket.c,v 1.115 2005/12/27 00:00:29 yamt Exp $	*/
+/*	$NetBSD: uipc_socket.c,v 1.116 2006/03/01 12:38:21 yamt Exp $	*/
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -68,7 +68,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.115 2005/12/27 00:00:29 yamt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.116 2006/03/01 12:38:21 yamt Exp $");
 
 #include "opt_sock_counters.h"
 #include "opt_sosend_loan.h"
@@ -390,7 +390,7 @@
 	vaddr_t lva, va;
 	int npgs, i, error;
 
-	if (uio->uio_segflg != UIO_USERSPACE)
+	if (VMSPACE_IS_KERNEL_P(uio->uio_vmspace))
 		return (0);
 
 	if (iov->iov_len < (size_t) space)
@@ -405,13 +405,12 @@
 
 	/* XXX KDASSERT */
 	KASSERT(npgs <= M_EXT_MAXPAGES);
-	KASSERT(uio->uio_lwp != NULL);
 
 	lva = sokvaalloc(len, so);
 	if (lva == 0)
 		return 0;
 
-	error = uvm_loan(&uio->uio_lwp->l_proc->p_vmspace->vm_map, sva, len,
+	error = uvm_loan(&uio->uio_vmspace->vm_map, sva, len,
 	    m->m_ext.ext_pgs, UVM_LOAN_TOPAGE);
 	if (error) {
 		sokvafree(lva, len);
@@ -934,7 +933,7 @@
 soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio,
 	struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
 {
-	struct lwp *l;
+	struct lwp *l = curlwp;
 	struct mbuf	*m, **mp;
 	int		flags, len, error, s, offset, moff, type, orig_resid;
 	const struct protosw	*pr;
@@ -945,7 +944,6 @@
 	mp = mp0;
 	type = 0;
 	orig_resid = uio->uio_resid;
-	l = uio->uio_lwp;
 
 	if (paddr)
 		*paddr = 0;
--- a/sys/kern/uipc_syscalls.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/kern/uipc_syscalls.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: uipc_syscalls.c,v 1.96 2005/12/26 18:45:27 perry Exp $	*/
+/*	$NetBSD: uipc_syscalls.c,v 1.97 2006/03/01 12:38:21 yamt Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1990, 1993
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.96 2005/12/26 18:45:27 perry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.97 2006/03/01 12:38:21 yamt Exp $");
 
 #include "opt_ktrace.h"
 #include "opt_pipe.h"
@@ -481,11 +481,11 @@
 	so = (struct socket *)fp->f_data;
 	auio.uio_iov = mp->msg_iov;
 	auio.uio_iovcnt = mp->msg_iovlen;
-	auio.uio_segflg = UIO_USERSPACE;
 	auio.uio_rw = UIO_WRITE;
-	auio.uio_lwp = l;
 	auio.uio_offset = 0;			/* XXX */
 	auio.uio_resid = 0;
+	KASSERT(l == curlwp);
+	auio.uio_vmspace = l->l_proc->p_vmspace;
 	iov = mp->msg_iov;
 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
 #if 0
@@ -688,11 +688,11 @@
 	so = (struct socket *)fp->f_data;
 	auio.uio_iov = mp->msg_iov;
 	auio.uio_iovcnt = mp->msg_iovlen;
-	auio.uio_segflg = UIO_USERSPACE;
 	auio.uio_rw = UIO_READ;
-	auio.uio_lwp = l;
 	auio.uio_offset = 0;			/* XXX */
 	auio.uio_resid = 0;
+	KASSERT(l == curlwp);
+	auio.uio_vmspace = l->l_proc->p_vmspace;
 	iov = mp->msg_iov;
 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
 #if 0
--- a/sys/kern/vfs_getcwd.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/kern/vfs_getcwd.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: vfs_getcwd.c,v 1.29 2005/12/11 12:24:30 christos Exp $ */
+/* $NetBSD: vfs_getcwd.c,v 1.30 2006/03/01 12:38:21 yamt Exp $ */
 
 /*-
  * Copyright (c) 1999 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vfs_getcwd.c,v 1.29 2005/12/11 12:24:30 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_getcwd.c,v 1.30 2006/03/01 12:38:21 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -181,9 +181,8 @@
 		uio.uio_iovcnt = 1;
 		uio.uio_offset = off;
 		uio.uio_resid = dirbuflen;
-		uio.uio_segflg = UIO_SYSSPACE;
 		uio.uio_rw = UIO_READ;
-		uio.uio_lwp = NULL;
+		UIO_SETUP_SYSSPACE(&uio);
 
 		eofflag = 0;
 
--- a/sys/kern/vfs_lookup.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/kern/vfs_lookup.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_lookup.c,v 1.67 2006/02/12 01:32:06 chs Exp $	*/
+/*	$NetBSD: vfs_lookup.c,v 1.68 2006/03/01 12:38:21 yamt Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vfs_lookup.c,v 1.67 2006/02/12 01:32:06 chs Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_lookup.c,v 1.68 2006/03/01 12:38:21 yamt Exp $");
 
 #include "opt_ktrace.h"
 #include "opt_systrace.h"
@@ -320,9 +320,8 @@
 		auio.uio_iovcnt = 1;
 		auio.uio_offset = 0;
 		auio.uio_rw = UIO_READ;
-		auio.uio_segflg = UIO_SYSSPACE;
-		auio.uio_lwp = NULL;
 		auio.uio_resid = MAXPATHLEN;
+		UIO_SETUP_SYSSPACE(&auio);
 		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
 		if (error) {
 		badlink:
--- a/sys/kern/vfs_syscalls.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/kern/vfs_syscalls.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_syscalls.c,v 1.237 2006/02/12 01:32:06 chs Exp $	*/
+/*	$NetBSD: vfs_syscalls.c,v 1.238 2006/03/01 12:38:21 yamt Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.237 2006/02/12 01:32:06 chs Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.238 2006/03/01 12:38:21 yamt Exp $");
 
 #include "opt_compat_netbsd.h"
 #include "opt_compat_43.h"
@@ -2251,8 +2251,8 @@
 		auio.uio_iovcnt = 1;
 		auio.uio_offset = 0;
 		auio.uio_rw = UIO_READ;
-		auio.uio_segflg = UIO_USERSPACE;
-		auio.uio_lwp = l;
+		KASSERT(l == curlwp);
+		auio.uio_vmspace = l->l_proc->p_vmspace;
 		auio.uio_resid = SCARG(uap, count);
 		error = VOP_READLINK(vp, &auio, p->p_ucred);
 	}
--- a/sys/kern/vfs_vnops.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/kern/vfs_vnops.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_vnops.c,v 1.105 2006/02/04 11:58:08 yamt Exp $	*/
+/*	$NetBSD: vfs_vnops.c,v 1.106 2006/03/01 12:38:21 yamt Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.105 2006/02/04 11:58:08 yamt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.106 2006/03/01 12:38:21 yamt Exp $");
 
 #include "opt_verified_exec.h"
 
@@ -371,9 +371,13 @@
 	aiov.iov_len = len;
 	auio.uio_resid = len;
 	auio.uio_offset = offset;
-	auio.uio_segflg = segflg;
 	auio.uio_rw = rw;
-	auio.uio_lwp = l;
+	if (segflg == UIO_SYSSPACE) {
+		UIO_SETUP_SYSSPACE(&auio);
+	} else {
+		KASSERT(l == curlwp);
+		auio.uio_vmspace = l->l_proc->p_vmspace;
+	}
 	if (rw == UIO_READ) {
 		error = VOP_READ(vp, &auio, ioflg, cred);
 	} else {
@@ -409,8 +413,12 @@
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = segflg;
-	auio.uio_lwp = l;
+	if (segflg == UIO_SYSSPACE) {
+		UIO_SETUP_SYSSPACE(&auio);
+	} else {
+		KASSERT(l == curlwp);
+		auio.uio_vmspace = l->l_proc->p_vmspace;
+	}
 	auio.uio_resid = count;
 	vn_lock(vp, LK_SHARED | LK_RETRY);
 	auio.uio_offset = fp->f_offset;
@@ -456,8 +464,9 @@
 {
 	struct vnode *vp = (struct vnode *)fp->f_data;
 	int count, error, ioflag;
+	struct lwp *l = curlwp;
 
-	VOP_LEASE(vp, uio->uio_lwp, cred, LEASE_READ);
+	VOP_LEASE(vp, l, cred, LEASE_READ);
 	ioflag = IO_ADV_ENCODE(fp->f_advice);
 	if (fp->f_flag & FNONBLOCK)
 		ioflag |= IO_NDELAY;
@@ -485,6 +494,7 @@
 	struct vnode *vp = (struct vnode *)fp->f_data;
 	struct mount *mp;
 	int count, error, ioflag = IO_UNIT;
+	struct lwp *l = curlwp;
 
 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
 		ioflag |= IO_APPEND;
@@ -501,7 +511,7 @@
 	if (vp->v_type != VCHR &&
 	    (error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
 		return (error);
-	VOP_LEASE(vp, uio->uio_lwp, cred, LEASE_WRITE);
+	VOP_LEASE(vp, l, cred, LEASE_WRITE);
 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
 	uio->uio_offset = *offset;
 	count = uio->uio_resid;
@@ -844,10 +854,9 @@
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_SYSSPACE;
-	auio.uio_lwp = l;
 	auio.uio_offset = 0;
 	auio.uio_resid = *buflen;
+	UIO_SETUP_SYSSPACE(&auio);
 
 	if ((ioflg & IO_NODELOCKED) == 0)
 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
@@ -882,10 +891,9 @@
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_rw = UIO_WRITE;
-	auio.uio_segflg = UIO_SYSSPACE;
-	auio.uio_lwp = l;
 	auio.uio_offset = 0;
 	auio.uio_resid = buflen;
+	UIO_SETUP_SYSSPACE(&auio);
 
 	if ((ioflg & IO_NODELOCKED) == 0) {
 		if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
--- a/sys/kern/vfs_xattr.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/kern/vfs_xattr.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_xattr.c,v 1.4 2005/12/11 12:24:30 christos Exp $	*/
+/*	$NetBSD: vfs_xattr.c,v 1.5 2006/03/01 12:38:21 yamt Exp $	*/
 
 /*-
  * Copyright (c) 2005 The NetBSD Foundation, Inc.
@@ -75,7 +75,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vfs_xattr.c,v 1.4 2005/12/11 12:24:30 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_xattr.c,v 1.5 2006/03/01 12:38:21 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -245,8 +245,8 @@
 	}
 	auio.uio_resid = nbytes;
 	auio.uio_rw = UIO_WRITE;
-	auio.uio_segflg = UIO_USERSPACE;
-	auio.uio_lwp = l;
+	KASSERT(l == curlwp);
+	auio.uio_vmspace = l->l_proc->p_vmspace;
 	cnt = nbytes;
 
 	error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio,
@@ -297,8 +297,8 @@
 		}
 		auio.uio_resid = nbytes;
 		auio.uio_rw = UIO_READ;
-		auio.uio_segflg = UIO_USERSPACE;
-		auio.uio_lwp = l;
+		KASSERT(l == curlwp);
+		auio.uio_vmspace = l->l_proc->p_vmspace;
 		auiop = &auio;
 		cnt = nbytes;
 	} else
@@ -378,8 +378,8 @@
 		}
 		auio.uio_resid = nbytes;
 		auio.uio_rw = UIO_READ;
-		auio.uio_segflg = UIO_USERSPACE;
-		auio.uio_lwp = l;
+		KASSERT(l == curlwp);
+		auio.uio_vmspace = l->l_proc->p_vmspace;
 		auiop = &auio;
 		cnt = nbytes;
 	} else
--- a/sys/miscfs/fdesc/fdesc_vnops.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/miscfs/fdesc/fdesc_vnops.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: fdesc_vnops.c,v 1.89 2005/12/11 12:24:50 christos Exp $	*/
+/*	$NetBSD: fdesc_vnops.c,v 1.90 2006/03/01 12:38:21 yamt Exp $	*/
 
 /*
  * Copyright (c) 1992, 1993
@@ -41,7 +41,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: fdesc_vnops.c,v 1.89 2005/12/11 12:24:50 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fdesc_vnops.c,v 1.90 2006/03/01 12:38:21 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -698,7 +698,7 @@
 		break;
 	}
 
-	fdp = uio->uio_lwp ? uio->uio_lwp->l_proc->p_fd : NULL;
+	fdp = curproc->p_fd;
 
 	if (uio->uio_resid < UIO_MX)
 		return EINVAL;
@@ -732,8 +732,7 @@
 		    i < nfdesc_targets; ft++, i++) {
 			switch (ft->ft_fileno) {
 			case FD_CTTY:
-				if (uio->uio_lwp == NULL ||
-				    cttyvp(uio->uio_lwp->l_proc) == NULL)
+				if (cttyvp(curproc) == NULL)
 					continue;
 				break;
 
--- a/sys/miscfs/genfs/genfs_vnops.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/miscfs/genfs/genfs_vnops.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: genfs_vnops.c,v 1.121 2006/01/16 19:45:00 reinoud Exp $	*/
+/*	$NetBSD: genfs_vnops.c,v 1.122 2006/03/01 12:38:21 yamt Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: genfs_vnops.c,v 1.121 2006/01/16 19:45:00 reinoud Exp $");
+__KERNEL_RCSID(0, "$NetBSD: genfs_vnops.c,v 1.122 2006/03/01 12:38:21 yamt Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_nfsserver.h"
@@ -1620,10 +1620,9 @@
 		uio.uio_iov = &iov;
 		uio.uio_iovcnt = 1;
 		uio.uio_offset = origoffset + (i << PAGE_SHIFT);
-		uio.uio_segflg = UIO_SYSSPACE;
 		uio.uio_rw = UIO_READ;
 		uio.uio_resid = PAGE_SIZE;
-		uio.uio_lwp = NULL;
+		UIO_SETUP_SYSSPACE(&uio);
 		/* XXX vn_lock */
 		error = VOP_READ(vp, &uio, 0, cred);
 		if (error) {
@@ -1674,10 +1673,9 @@
 	uio.uio_iov = &iov;
 	uio.uio_iovcnt = 1;
 	uio.uio_offset = offset;
-	uio.uio_segflg = UIO_SYSSPACE;
 	uio.uio_rw = UIO_WRITE;
 	uio.uio_resid = npages << PAGE_SHIFT;
-	uio.uio_lwp = NULL;
+	UIO_SETUP_SYSSPACE(&uio);
 	/* XXX vn_lock */
 	error = VOP_WRITE(vp, &uio, 0, cred);
 
--- a/sys/miscfs/kernfs/kernfs_vnops.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/miscfs/kernfs/kernfs_vnops.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: kernfs_vnops.c,v 1.116 2005/12/24 20:45:09 perry Exp $	*/
+/*	$NetBSD: kernfs_vnops.c,v 1.117 2006/03/01 12:38:32 yamt Exp $	*/
 
 /*
  * Copyright (c) 1992, 1993
@@ -39,7 +39,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kernfs_vnops.c,v 1.116 2005/12/24 20:45:09 perry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kernfs_vnops.c,v 1.117 2006/03/01 12:38:32 yamt Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ipsec.h"
@@ -1017,10 +1017,11 @@
 		return error;
 	if (kt->kt_tag == KFSdevice) {
 		struct vattr va;
-		if ((error = VOP_GETATTR(vp, &va, ap->a_cred,
-		    ap->a_uio->uio_segflg == UIO_USERSPACE ?
-		    ap->a_uio->uio_lwp : &lwp0)) != 0)
-			return (error);
+
+		error = VOP_GETATTR(vp, &va, ap->a_cred, curlwp);
+		if (error != 0) {
+			return error;
+		}
 		d->d_fileno = va.va_fileid;
 	} else {
 		kfs = VTOKERN(vp);
--- a/sys/miscfs/portal/portal_vnops.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/miscfs/portal/portal_vnops.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: portal_vnops.c,v 1.62 2005/12/11 12:24:51 christos Exp $	*/
+/*	$NetBSD: portal_vnops.c,v 1.63 2006/03/01 12:38:32 yamt Exp $	*/
 
 /*
  * Copyright (c) 1992, 1993
@@ -40,7 +40,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: portal_vnops.c,v 1.62 2005/12/11 12:24:51 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: portal_vnops.c,v 1.63 2006/03/01 12:38:32 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -404,10 +404,9 @@
 	auio.uio_iov = aiov;
 	auio.uio_iovcnt = 2;
 	auio.uio_rw = UIO_WRITE;
-	auio.uio_segflg = UIO_SYSSPACE;
-	auio.uio_lwp = l;
 	auio.uio_offset = 0;
 	auio.uio_resid = aiov[0].iov_len + aiov[1].iov_len;
+	UIO_SETUP_SYSSPACE(&auio);
 
 	error = (*so->so_send)(so, (struct mbuf *) 0, &auio,
 			(struct mbuf *) 0, (struct mbuf *) 0, 0, l);
--- a/sys/miscfs/procfs/procfs_cmdline.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/miscfs/procfs/procfs_cmdline.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: procfs_cmdline.c,v 1.20 2005/12/11 12:24:51 christos Exp $	*/
+/*	$NetBSD: procfs_cmdline.c,v 1.21 2006/03/01 12:38:32 yamt Exp $	*/
 
 /*
  * Copyright (c) 1999 Jaromir Dolecek <dolecek@ics.muni.cz>
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: procfs_cmdline.c,v 1.20 2005/12/11 12:24:51 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: procfs_cmdline.c,v 1.21 2006/03/01 12:38:32 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -116,9 +116,8 @@
 	auio.uio_iovcnt = 1;
 	auio.uio_offset = (vaddr_t)p->p_psstr;
 	auio.uio_resid = sizeof(pss);
-	auio.uio_segflg = UIO_SYSSPACE;
 	auio.uio_rw = UIO_READ;
-	auio.uio_lwp = NULL;
+	UIO_SETUP_SYSSPACE(&auio);
 	error = uvm_io(&p->p_vmspace->vm_map, &auio);
 	if (error)
 		goto bad;
@@ -132,9 +131,8 @@
 	auio.uio_iovcnt = 1;
 	auio.uio_offset = (vaddr_t)pss.ps_argvstr;
 	auio.uio_resid = sizeof(argv);
-	auio.uio_segflg = UIO_SYSSPACE;
 	auio.uio_rw = UIO_READ;
-	auio.uio_lwp = NULL;
+	UIO_SETUP_SYSSPACE(&auio);
 	error = uvm_io(&p->p_vmspace->vm_map, &auio);
 	if (error)
 		goto bad;
@@ -155,9 +153,8 @@
 		auio.uio_offset = argv + len;
 		xlen = PAGE_SIZE - ((argv + len) & PAGE_MASK);
 		auio.uio_resid = xlen;
-		auio.uio_segflg = UIO_SYSSPACE;
 		auio.uio_rw = UIO_READ;
-		auio.uio_lwp = NULL;
+		UIO_SETUP_SYSSPACE(&auio);
 		error = uvm_io(&p->p_vmspace->vm_map, &auio);
 		if (error)
 			goto bad;
--- a/sys/miscfs/procfs/procfs_subr.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/miscfs/procfs/procfs_subr.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: procfs_subr.c,v 1.67 2005/12/11 12:24:51 christos Exp $	*/
+/*	$NetBSD: procfs_subr.c,v 1.68 2006/03/01 12:38:32 yamt Exp $	*/
 
 /*
  * Copyright (c) 1993
@@ -73,7 +73,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: procfs_subr.c,v 1.67 2005/12/11 12:24:51 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: procfs_subr.c,v 1.68 2006/03/01 12:38:32 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -314,7 +314,7 @@
 	if (uio->uio_rw == UIO_WRITE && p == initproc && securelevel > -1)
 		return EPERM;
 
-	curl = uio->uio_lwp;
+	curl = curlwp;
 
 	/* XXX NJWLWP
 	 * The entire procfs interface needs work to be useful to
--- a/sys/miscfs/specfs/spec_vnops.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/miscfs/specfs/spec_vnops.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: spec_vnops.c,v 1.85 2005/12/11 12:24:51 christos Exp $	*/
+/*	$NetBSD: spec_vnops.c,v 1.86 2006/03/01 12:38:32 yamt Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: spec_vnops.c,v 1.85 2005/12/11 12:24:51 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: spec_vnops.c,v 1.86 2006/03/01 12:38:32 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/proc.h>
@@ -278,7 +278,7 @@
 	} */ *ap = v;
 	struct vnode *vp = ap->a_vp;
 	struct uio *uio = ap->a_uio;
- 	struct lwp *l = uio->uio_lwp;
+ 	struct lwp *l = curlwp;
 	struct buf *bp;
 	const struct bdevsw *bdev;
 	const struct cdevsw *cdev;
@@ -291,7 +291,8 @@
 #ifdef DIAGNOSTIC
 	if (uio->uio_rw != UIO_READ)
 		panic("spec_read mode");
-	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_lwp != curlwp)
+	if (&uio->uio_vmspace->vm_map != kernel_map &&
+	    uio->uio_vmspace != curproc->p_vmspace)
 		panic("spec_read proc");
 #endif
 	if (uio->uio_resid == 0)
@@ -360,7 +361,7 @@
 	} */ *ap = v;
 	struct vnode *vp = ap->a_vp;
 	struct uio *uio = ap->a_uio;
-	struct lwp *l = uio->uio_lwp;
+	struct lwp *l = curlwp;
 	struct buf *bp;
 	const struct bdevsw *bdev;
 	const struct cdevsw *cdev;
@@ -373,7 +374,8 @@
 #ifdef DIAGNOSTIC
 	if (uio->uio_rw != UIO_WRITE)
 		panic("spec_write mode");
-	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_lwp != curlwp)
+	if (&uio->uio_vmspace->vm_map != kernel_map &&
+	    uio->uio_vmspace != curproc->p_vmspace)
 		panic("spec_write proc");
 #endif
 
--- a/sys/netsmb/smb_dev.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/netsmb/smb_dev.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: smb_dev.c,v 1.21 2005/12/11 12:25:16 christos Exp $	*/
+/*	$NetBSD: smb_dev.c,v 1.22 2006/03/01 12:38:32 yamt Exp $	*/
 
 /*
  * Copyright (c) 2000-2001 Boris Popov
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: smb_dev.c,v 1.21 2005/12/11 12:25:16 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: smb_dev.c,v 1.22 2006/03/01 12:38:32 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
@@ -389,9 +389,8 @@
 		auio.uio_iovcnt = 1;
 		auio.uio_offset = rwrq->ioc_offset;
 		auio.uio_resid = rwrq->ioc_cnt;
-		auio.uio_segflg = UIO_USERSPACE;
 		auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE;
-		auio.uio_lwp = l;
+		auio.uio_vmspace = l->l_proc->p_vmspace;
 		if (cmd == SMBIOC_READ)
 			error = smb_read(ssp, rwrq->ioc_fh, &auio, &scred);
 		else
--- a/sys/netsmb/smb_trantcp.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/netsmb/smb_trantcp.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: smb_trantcp.c,v 1.20 2005/12/24 20:45:09 perry Exp $	*/
+/*	$NetBSD: smb_trantcp.c,v 1.21 2006/03/01 12:38:32 yamt Exp $	*/
 
 /*
  * Copyright (c) 2000-2001 Boris Popov
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: smb_trantcp.c,v 1.20 2005/12/24 20:45:09 perry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: smb_trantcp.c,v 1.21 2006/03/01 12:38:32 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -406,11 +406,10 @@
 	aio.iov_len = sizeof(len);
 	auio.uio_iov = &aio;
 	auio.uio_iovcnt = 1;
-	auio.uio_segflg = UIO_SYSSPACE;
 	auio.uio_rw = UIO_READ;
 	auio.uio_offset = 0;
 	auio.uio_resid = sizeof(len);
-	auio.uio_lwp = NULL;
+	UIO_SETUP_SYSSPACE(&auio);
 #ifndef __NetBSD__
 	error = so->so_proto->pr_usrreqs->pru_soreceive
 	    (so, (struct sockaddr **)NULL, &auio,
@@ -493,7 +492,7 @@
 			rcvflg = MSG_WAITALL;
 			bzero(&auio, sizeof(auio));
 			auio.uio_resid = min(resid, NB_SORECEIVE_CHUNK);
-			auio.uio_lwp = l;
+			/* not need to setup uio_vmspace */
 			resid -= auio.uio_resid;
 			/*
 			 * Spin until we have collected everything in
--- a/sys/netsmb/subr_mchain.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/netsmb/subr_mchain.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_mchain.c,v 1.12 2005/12/11 12:25:16 christos Exp $	*/
+/*	$NetBSD: subr_mchain.c,v 1.13 2006/03/01 12:38:32 yamt Exp $	*/
 
 /*
  * Copyright (c) 2000, 2001 Boris Popov
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_mchain.c,v 1.12 2005/12/11 12:25:16 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_mchain.c,v 1.13 2006/03/01 12:38:32 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -43,6 +43,8 @@
 #include <sys/mbuf.h>
 #include <sys/uio.h>
 
+#include <uvm/uvm_extern.h>
+
 #include <netsmb/mchain.h>
 
 #define MBERROR(format, args...) printf("%s(%d): "format, __func__ , \
@@ -329,7 +331,7 @@
 	long left;
 	int mtype, error;
 
-	mtype = (uiop->uio_segflg == UIO_SYSSPACE) ? MB_MSYSTEM : MB_MUSER;
+	mtype = VMSPACE_IS_KERNEL_P(uiop->uio_vmspace) ? MB_MSYSTEM : MB_MUSER;
 
 	while (size > 0 && uiop->uio_resid) {
 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
@@ -582,7 +584,7 @@
 	long left;
 	int mtype, error;
 
-	mtype = (uiop->uio_segflg == UIO_SYSSPACE) ? MB_MSYSTEM : MB_MUSER;
+	mtype = VMSPACE_IS_KERNEL_P(uiop->uio_vmspace) ? MB_MSYSTEM : MB_MUSER;
 	while (size > 0 && uiop->uio_resid) {
 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
 			return EFBIG;
--- a/sys/nfs/nfs_bio.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/nfs/nfs_bio.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: nfs_bio.c,v 1.141 2006/01/14 08:57:40 yamt Exp $	*/
+/*	$NetBSD: nfs_bio.c,v 1.142 2006/03/01 12:38:32 yamt Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nfs_bio.c,v 1.141 2006/01/14 08:57:40 yamt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nfs_bio.c,v 1.142 2006/03/01 12:38:32 yamt Exp $");
 
 #include "opt_nfs.h"
 #include "opt_ddb.h"
@@ -85,7 +85,6 @@
 {
 	struct nfsnode *np = VTONFS(vp);
 	struct buf *bp = NULL, *rabp;
-	struct lwp *l = uio->uio_lwp;
 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
 	struct nfsdircache *ndp = NULL, *nndp = NULL;
 	caddr_t baddr;
@@ -94,6 +93,7 @@
 	struct dirent *dp, *pdp, *edp, *ep;
 	off_t curoff = 0;
 	int advice;
+	struct lwp *l = curlwp;
 
 #ifdef DIAGNOSTIC
 	if (uio->uio_rw != UIO_READ)
@@ -498,7 +498,7 @@
 		struct ucred *a_cred;
 	} */ *ap = v;
 	struct uio *uio = ap->a_uio;
-	struct lwp *l = uio->uio_lwp;
+	struct lwp *l = curlwp;
 	struct vnode *vp = ap->a_vp;
 	struct nfsnode *np = VTONFS(vp);
 	struct ucred *cred = ap->a_cred;
@@ -957,6 +957,7 @@
 			memset((char *)bp->b_data + diff, 0, len);
 			uiop->uio_resid = 0;
 		}
+#if 0
 		if (uiop->uio_lwp && (vp->v_flag & VTEXT) &&
 		    (((nmp->nm_flag & NFSMNT_NQNFS) &&
 		      NQNFS_CKINVALID(vp, np, ND_READ) &&
@@ -968,6 +969,7 @@
 			uiop->uio_lwp->l_proc->p_holdcnt++;
 #endif
 		}
+#endif
 		break;
 	case VLNK:
 		KASSERT(uiop->uio_offset == (off_t)0);
@@ -1271,9 +1273,8 @@
 
 	uiop->uio_iov = &io;
 	uiop->uio_iovcnt = 1;
-	uiop->uio_segflg = UIO_SYSSPACE;
-	uiop->uio_lwp = NULL;
 	uiop->uio_offset = (((off_t)bp->b_blkno) << DEV_BSHIFT);
+	UIO_SETUP_SYSSPACE(uiop);
 	io.iov_base = bp->b_data;
 	io.iov_len = uiop->uio_resid = bp->b_bcount;
 
--- a/sys/nfs/nfs_boot.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/nfs/nfs_boot.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: nfs_boot.c,v 1.62 2005/12/11 12:25:16 christos Exp $	*/
+/*	$NetBSD: nfs_boot.c,v 1.63 2006/03/01 12:38:32 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1995, 1997 The NetBSD Foundation, Inc.
@@ -42,7 +42,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nfs_boot.c,v 1.62 2005/12/11 12:25:16 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nfs_boot.c,v 1.63 2006/03/01 12:38:32 yamt Exp $");
 
 #include "opt_nfs.h"
 #include "opt_nfs_boot.h"
@@ -459,7 +459,6 @@
 			m = NULL;
 		}
 		uio.uio_resid = 1 << 16; /* ??? */
-		uio.uio_lwp = lwp;
 		rcvflg = 0;
 		error = (*so->so_receive)(so, &from, &uio, &m, NULL, &rcvflg);
 		if (error == EWOULDBLOCK) {
--- a/sys/nfs/nfs_serv.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/nfs/nfs_serv.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: nfs_serv.c,v 1.100 2006/01/03 11:41:50 yamt Exp $	*/
+/*	$NetBSD: nfs_serv.c,v 1.101 2006/03/01 12:38:32 yamt Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -55,7 +55,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nfs_serv.c,v 1.100 2006/01/03 11:41:50 yamt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nfs_serv.c,v 1.101 2006/03/01 12:38:32 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -527,8 +527,7 @@
 	uiop->uio_offset = 0;
 	uiop->uio_resid = len;
 	uiop->uio_rw = UIO_READ;
-	uiop->uio_segflg = UIO_SYSSPACE;
-	uiop->uio_lwp = NULL;
+	UIO_SETUP_SYSSPACE(uiop);
 	error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
 	if (error) {
@@ -760,7 +759,7 @@
 			uiop->uio_offset = off;
 			uiop->uio_resid = cnt;
 			uiop->uio_rw = UIO_READ;
-			uiop->uio_segflg = UIO_SYSSPACE;
+			UIO_SETUP_SYSSPACE(uiop);
 			error = VOP_READ(vp, uiop, IO_NODELOCKED, cred);
 			free((caddr_t)iv2, M_TEMP);
 		}
@@ -949,9 +948,8 @@
 			ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
 		uiop->uio_resid = len;
 		uiop->uio_rw = UIO_WRITE;
-		uiop->uio_segflg = UIO_SYSSPACE;
-		uiop->uio_lwp = NULL;
 		uiop->uio_offset = off;
+		UIO_SETUP_SYSSPACE(uiop);
 		error = VOP_WRITE(vp, uiop, ioflags, cred);
 		nfsstats.srvvop_writes++;
 		free(iv, M_TEMP);
@@ -1188,10 +1186,9 @@
 		else
 		    ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
 		uiop->uio_rw = UIO_WRITE;
-		uiop->uio_segflg = UIO_SYSSPACE;
-		uiop->uio_lwp = NULL;
 		uiop->uio_offset = nfsd->nd_off;
 		uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
+		UIO_SETUP_SYSSPACE(uiop);
 		if (uiop->uio_resid > 0) {
 		    mp = mrep;
 		    i = 0;
@@ -2227,9 +2224,8 @@
 	io.uio_offset = 0;
 	io.uio_iov = &iv;
 	io.uio_iovcnt = 1;
-	io.uio_segflg = UIO_SYSSPACE;
 	io.uio_rw = UIO_READ;
-	io.uio_lwp = NULL;
+	UIO_SETUP_SYSSPACE(&io);
 	nfsm_mtouio(&io, len2);
 	if (!v3) {
 		nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
@@ -2651,9 +2647,8 @@
 	io.uio_iovcnt = 1;
 	io.uio_offset = (off_t)off;
 	io.uio_resid = fullsiz;
-	io.uio_segflg = UIO_SYSSPACE;
 	io.uio_rw = UIO_READ;
-	io.uio_lwp = NULL;
+	UIO_SETUP_SYSSPACE(&io);
 	eofflag = 0;
 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
 
@@ -2910,9 +2905,8 @@
 	io.uio_iovcnt = 1;
 	io.uio_offset = (off_t)off;
 	io.uio_resid = fullsiz;
-	io.uio_segflg = UIO_SYSSPACE;
 	io.uio_rw = UIO_READ;
-	io.uio_lwp = NULL;
+	UIO_SETUP_SYSSPACE(&io);
 	eofflag = 0;
 
 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
--- a/sys/nfs/nfs_socket.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/nfs/nfs_socket.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: nfs_socket.c,v 1.122 2006/01/03 12:30:01 yamt Exp $	*/
+/*	$NetBSD: nfs_socket.c,v 1.123 2006/03/01 12:38:32 yamt Exp $	*/
 
 /*
  * Copyright (c) 1989, 1991, 1993, 1995
@@ -39,7 +39,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nfs_socket.c,v 1.122 2006/01/03 12:30:01 yamt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nfs_socket.c,v 1.123 2006/03/01 12:38:32 yamt Exp $");
 
 #include "fs_nfs.h"
 #include "opt_nfs.h"
@@ -630,11 +630,10 @@
 			aio.iov_len = sizeof(u_int32_t);
 			auio.uio_iov = &aio;
 			auio.uio_iovcnt = 1;
-			auio.uio_segflg = UIO_SYSSPACE;
 			auio.uio_rw = UIO_READ;
 			auio.uio_offset = 0;
 			auio.uio_resid = sizeof(u_int32_t);
-			auio.uio_lwp = NULL;
+			UIO_SETUP_SYSSPACE(&auio);
 			do {
 			   rcvflg = MSG_WAITALL;
 			   error = (*so->so_receive)(so, (struct mbuf **)0, &auio,
@@ -708,7 +707,7 @@
 			 * on.
 			 */
 			auio.uio_resid = len = 100000000; /* Anything Big */
-			auio.uio_lwp = l;
+			/* not need to setup uio_vmspace */
 			do {
 			    rcvflg = 0;
 			    error =  (*so->so_receive)(so, (struct mbuf **)0,
@@ -752,7 +751,7 @@
 		else
 			getnam = aname;
 		auio.uio_resid = len = 1000000;
-		auio.uio_lwp = l;
+		/* not need to setup uio_vmspace */
 		do {
 			rcvflg = 0;
 			error =  (*so->so_receive)(so, getnam, &auio, mp,
@@ -2042,7 +2041,7 @@
 			uio.uio_offset = 0;
 			uio.uio_iov = &iov;
 			uio.uio_iovcnt = 1;
-			uio.uio_segflg = UIO_SYSSPACE;
+			UIO_SETUP_SYSSPACE(&uio);
 			iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4];
 			iov.iov_len = RPCAUTH_MAXSIZ - 4;
 			nfsm_mtouio(&uio, uio.uio_resid);
@@ -2237,8 +2236,6 @@
 		goto dorecs;
 	}
 #endif
-	/* XXX: was NULL, soreceive() requires non-NULL uio->uio_lwp */
-	auio.uio_lwp = curlwp;	/* XXX curlwp */
 	simple_lock(&slp->ns_lock);
 	slp->ns_flag &= ~SLP_NEEDQ;
 	simple_unlock(&slp->ns_lock);
@@ -2257,6 +2254,7 @@
 		 * Do soreceive().
 		 */
 		auio.uio_resid = 1000000000;
+		/* not need to setup uio_vmspace */
 		flags = MSG_DONTWAIT;
 		error = (*so->so_receive)(so, &nam, &auio, &mp, NULL, &flags);
 		if (error || mp == NULL) {
@@ -2291,6 +2289,7 @@
 	} else {
 		do {
 			auio.uio_resid = 1000000000;
+			/* not need to setup uio_vmspace */
 			flags = MSG_DONTWAIT;
 			error = (*so->so_receive)(so, &nam, &auio, &mp, NULL,
 			    &flags);
--- a/sys/nfs/nfs_subs.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/nfs/nfs_subs.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: nfs_subs.c,v 1.157 2006/01/16 21:45:38 yamt Exp $	*/
+/*	$NetBSD: nfs_subs.c,v 1.158 2006/03/01 12:38:32 yamt Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -70,7 +70,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nfs_subs.c,v 1.157 2006/01/16 21:45:38 yamt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nfs_subs.c,v 1.158 2006/03/01 12:38:32 yamt Exp $");
 
 #include "fs_nfs.h"
 #include "opt_nfs.h"
@@ -825,19 +825,11 @@
 				len = mp->m_len;
 			}
 			xfer = (left > len) ? len : left;
-#ifdef notdef
-			/* Not Yet.. */
-			if (uiop->uio_iov->iov_op != NULL)
-				(*(uiop->uio_iov->iov_op))
-				(mbufcp, uiocp, xfer);
-			else
-#endif
-			if (uiop->uio_segflg == UIO_SYSSPACE)
-				memcpy(uiocp, mbufcp, xfer);
-			else
-				if ((error = copyout_proc(uiop->uio_lwp->l_proc,
-				    mbufcp, uiocp, xfer)) != 0)
-					return error;
+			error = copyout_vmspace(uiop->uio_vmspace, mbufcp,
+			    uiocp, xfer);
+			if (error) {
+				return error;
+			}
 			left -= xfer;
 			len -= xfer;
 			mbufcp += xfer;
@@ -882,6 +874,7 @@
 	int xfer, left, mlen;
 	int uiosiz, clflg, rem;
 	char *cp;
+	int error;
 
 #ifdef DIAGNOSTIC
 	if (uiop->uio_iovcnt != 1)
@@ -914,18 +907,11 @@
 			}
 			xfer = (left > mlen) ? mlen : left;
 			cp = mtod(mp, caddr_t) + mp->m_len;
-#ifdef notdef
-			/* Not Yet.. */
-			if (uiop->uio_iov->iov_op != NULL)
-				(*(uiop->uio_iov->iov_op))(uiocp, cp, xfer);
-			else
-#endif
-			if (uiop->uio_segflg == UIO_SYSSPACE)
-				(void)memcpy(cp, uiocp, xfer);
-			else
-				/*XXX: Check error */
-				(void)copyin_proc(uiop->uio_lwp->l_proc, uiocp,
-				    cp, xfer);
+			error = copyin_vmspace(uiop->uio_vmspace, uiocp, cp,
+			    xfer);
+			if (error) {
+				/* XXX */
+			}
 			mp->m_len += xfer;
 			left -= xfer;
 			uiocp += xfer;
@@ -2047,10 +2033,9 @@
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_SYSSPACE;
-	auio.uio_lwp = NULL;
 	auio.uio_resid = NFS_DIRFRAGSIZ;
 	auio.uio_offset = 0;
+	UIO_SETUP_SYSSPACE(&auio);
 
 	error = VOP_READDIR(vp, &auio, cred, &eof, &cookies, &nc);
 
@@ -2293,9 +2278,8 @@
 		auio.uio_iovcnt = 1;
 		auio.uio_offset = 0;
 		auio.uio_rw = UIO_READ;
-		auio.uio_segflg = UIO_SYSSPACE;
-		auio.uio_lwp = NULL;
 		auio.uio_resid = MAXPATHLEN;
+		UIO_SETUP_SYSSPACE(&auio);
 		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
 		if (error) {
 		badlink:
--- a/sys/nfs/nfs_vnops.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/nfs/nfs_vnops.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: nfs_vnops.c,v 1.230 2005/12/11 12:25:17 christos Exp $	*/
+/*	$NetBSD: nfs_vnops.c,v 1.231 2006/03/01 12:38:32 yamt Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -39,7 +39,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nfs_vnops.c,v 1.230 2005/12/11 12:25:17 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nfs_vnops.c,v 1.231 2006/03/01 12:38:32 yamt Exp $");
 
 #include "opt_inet.h"
 #include "opt_nfs.h"
@@ -1195,7 +1195,7 @@
 	nfsstats.rpccnt[NFSPROC_READLINK]++;
 	nfsm_reqhead(np, NFSPROC_READLINK, NFSX_FH(v3));
 	nfsm_fhtom(np, v3);
-	nfsm_request(np, NFSPROC_READLINK, uiop->uio_lwp, cred);
+	nfsm_request(np, NFSPROC_READLINK, curlwp, cred);
 #ifndef NFS_V2_ONLY
 	if (v3)
 		nfsm_postop_attr(vp, attrflag, 0);
@@ -1271,7 +1271,7 @@
 			*tl++ = txdr_unsigned(len);
 			*tl = 0;
 		}
-		nfsm_request(np, NFSPROC_READ, uiop->uio_lwp, np->n_rcred);
+		nfsm_request(np, NFSPROC_READ, curlwp, np->n_rcred);
 #ifndef NFS_V2_ONLY
 		if (v3) {
 			nfsm_postop_attr(vp, attrflag, NAC_NOTRUNC);
@@ -1444,7 +1444,7 @@
 		} else {
 			nfsm_uiotom(uiop, len);
 		}
-		nfsm_request(np, NFSPROC_WRITE, uiop->uio_lwp, np->n_wcred);
+		nfsm_request(np, NFSPROC_WRITE, curlwp, np->n_wcred);
 #ifndef NFS_V2_ONLY
 		if (v3) {
 			wccflag = NFSV3_WCCCHK;
@@ -2492,7 +2492,8 @@
 		 * load the directory block into system space, so we can
 		 * just look at it directly.
 		 */
-		if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
+		if (!VMSPACE_IS_KERNEL_P(uio->uio_vmspace) ||
+		    uio->uio_iovcnt != 1)
 			panic("nfs_readdir: lost in space");
 		for (nc = 0; ncookies-- &&
 		     base < (char *)uio->uio_iov->iov_base; nc++){
@@ -2592,7 +2593,7 @@
 			*tl++ = txdr_unsigned(uiop->uio_offset);
 		}
 		*tl = txdr_unsigned(nmp->nm_readdirsize);
-		nfsm_request(dnp, NFSPROC_READDIR, uiop->uio_lwp, cred);
+		nfsm_request(dnp, NFSPROC_READDIR, curlwp, cred);
 		nrpcs++;
 #ifndef NFS_V2_ONLY
 		if (v3) {
@@ -2800,7 +2801,7 @@
 		*tl++ = dnp->n_cookieverf.nfsuquad[1];
 		*tl++ = txdr_unsigned(nmp->nm_readdirsize);
 		*tl = txdr_unsigned(nmp->nm_rsize);
-		nfsm_request(dnp, NFSPROC_READDIRPLUS, uiop->uio_lwp, cred);
+		nfsm_request(dnp, NFSPROC_READDIRPLUS, curlwp, cred);
 		nfsm_postop_attr(vp, attrflag, 0);
 		if (error) {
 			m_freem(mrep);
--- a/sys/opencrypto/cryptodev.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/opencrypto/cryptodev.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.c,v 1.16 2005/12/11 12:25:20 christos Exp $ */
+/*	$NetBSD: cryptodev.c,v 1.17 2006/03/01 12:38:32 yamt Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.16 2005/12/11 12:25:20 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.17 2006/03/01 12:38:32 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -341,10 +341,9 @@
 	bzero(&cse->uio, sizeof(cse->uio));
 	cse->uio.uio_iovcnt = 1;
 	cse->uio.uio_resid = 0;
-	cse->uio.uio_segflg = UIO_SYSSPACE;
 	cse->uio.uio_rw = UIO_WRITE;
-	cse->uio.uio_lwp = NULL;
 	cse->uio.uio_iov = cse->iovec;
+	UIO_SETUP_SYSSPACE(&cse->uio);
 	bzero(&cse->iovec, sizeof(cse->iovec));
 	cse->uio.uio_iov[0].iov_len = cop->len;
 	cse->uio.uio_iov[0].iov_base = malloc(cop->len, M_XDATA, M_WAITOK);
--- a/sys/sys/proc.h	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/sys/proc.h	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: proc.h,v 1.212 2006/02/16 20:17:20 perry Exp $	*/
+/*	$NetBSD: proc.h,v 1.213 2006/03/01 12:38:32 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1986, 1989, 1991, 1993
@@ -402,6 +402,7 @@
 
 extern struct proc	proc0;		/* Process slot for swapper */
 extern int		nprocs, maxproc; /* Current and max number of procs */
+#define	vmspace_kernel()	(proc0.p_vmspace)
 
 /* Process list lock; see kern_proc.c for locking protocol details */
 extern struct lock	proclist_lock;
@@ -487,6 +488,8 @@
 void	proclist_unlock_write(int);
 void	p_sugid(struct proc *);
 
+int	proc_vmspace_getref(struct proc *, struct vmspace **);
+
 int	proclist_foreach_call(struct proclist *,
     int (*)(struct proc *, void *arg), void *);
 static __inline struct proc *_proclist_skipmarker(struct proc *);
--- a/sys/sys/systm.h	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/sys/systm.h	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: systm.h,v 1.183 2005/12/26 18:41:36 perry Exp $	*/
+/*	$NetBSD: systm.h,v 1.184 2006/03/01 12:38:32 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1982, 1988, 1991, 1993
@@ -83,6 +83,7 @@
 struct tty;
 struct uio;
 struct vnode;
+struct vmspace;
 
 extern int securelevel;		/* system security level */
 extern const char *panicstr;	/* panic message */
@@ -247,6 +248,8 @@
 
 int	copyin_proc(struct proc *, const void *, void *, size_t);
 int	copyout_proc(struct proc *, const void *, void *, size_t);
+int	copyin_vmspace(struct vmspace *, const void *, void *, size_t);
+int	copyout_vmspace(struct vmspace *, const void *, void *, size_t);
 
 int	ioctl_copyin(int ioctlflags, const void *src, void *dst, size_t len);
 int	ioctl_copyout(int ioctlflags, const void *src, void *dst, size_t len);
--- a/sys/sys/uio.h	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/sys/uio.h	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: uio.h,v 1.33 2005/12/11 12:25:21 christos Exp $	*/
+/*	$NetBSD: uio.h,v 1.34 2006/03/01 12:38:32 yamt Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1993, 1994
@@ -81,10 +81,10 @@
 	int	uio_iovcnt;	/* number of iovecs in array */
 	off_t	uio_offset;	/* offset into file this uio corresponds to */
 	size_t	uio_resid;	/* residual i/o count */
-	enum	uio_seg uio_segflg; /* see above */
 	enum	uio_rw uio_rw;	/* see above */
-	struct	lwp *uio_lwp;	/* LWP if UIO_USERSPACE */
+	struct	vmspace *uio_vmspace;
 };
+#define	UIO_SETUP_SYSSPACE(uio)	uio_setup_sysspace(uio)
 
 #endif /* __UIO_EXPOSE */
 
@@ -101,6 +101,8 @@
 MALLOC_DECLARE(M_IOV);
 
 #define UIO_SMALLIOV	8		/* 8 on stack, else malloc */
+
+void uio_setup_sysspace(struct uio *);
 #endif
 
 #ifndef	_KERNEL
--- a/sys/ufs/ext2fs/ext2fs_lookup.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/ufs/ext2fs/ext2fs_lookup.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: ext2fs_lookup.c,v 1.39 2005/12/11 12:25:25 christos Exp $	*/
+/*	$NetBSD: ext2fs_lookup.c,v 1.40 2006/03/01 12:38:33 yamt Exp $	*/
 
 /*
  * Modified for NetBSD 1.2E
@@ -48,7 +48,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ext2fs_lookup.c,v 1.39 2005/12/11 12:25:25 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ext2fs_lookup.c,v 1.40 2006/03/01 12:38:33 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -162,9 +162,9 @@
 	auio = *uio;
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
-	auio.uio_segflg = UIO_SYSSPACE;
 	aiov.iov_len = e2fs_count;
 	auio.uio_resid = e2fs_count;
+	UIO_SETUP_SYSSPACE(&auio);
 	MALLOC(dirbuf, caddr_t, e2fs_count, M_TEMP, M_WAITOK);
 	if (ap->a_ncookies) {
 		nc = ncookies = e2fs_count / 16;
@@ -818,8 +818,7 @@
 		auio.uio_iov = &aiov;
 		auio.uio_iovcnt = 1;
 		auio.uio_rw = UIO_WRITE;
-		auio.uio_segflg = UIO_SYSSPACE;
-		auio.uio_lwp = NULL;
+		UIO_SETUP_SYSSPACE(&auio);
 		error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred);
 		if (dirblksiz > dvp->v_mount->mnt_stat.f_bsize)
 			/* XXX should grow with balloc() */
--- a/sys/ufs/ext2fs/ext2fs_readwrite.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/ufs/ext2fs/ext2fs_readwrite.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: ext2fs_readwrite.c,v 1.41 2006/01/14 23:49:59 christos Exp $	*/
+/*	$NetBSD: ext2fs_readwrite.c,v 1.42 2006/03/01 12:38:33 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1993
@@ -65,7 +65,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ext2fs_readwrite.c,v 1.41 2006/01/14 23:49:59 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ext2fs_readwrite.c,v 1.42 2006/03/01 12:38:33 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -281,7 +281,7 @@
 	 * Maybe this should be above the vnode op call, but so long as
 	 * file servers have no limits, I don't think it matters.
 	 */
-	p = uio->uio_lwp ? uio->uio_lwp->l_proc : NULL;
+	p = curproc;
 	if (vp->v_type == VREG && p &&
 	    uio->uio_offset + uio->uio_resid >
 	    p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
@@ -399,7 +399,7 @@
 		VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
 	if (error) {
 		(void) ext2fs_truncate(vp, osize, ioflag & IO_SYNC, ap->a_cred,
-		    uio->uio_lwp == NULL ? NULL : uio->uio_lwp->l_proc);
+		    p);
 		uio->uio_offset -= resid - uio->uio_resid;
 		uio->uio_resid = resid;
 	} else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
--- a/sys/ufs/ufs/ufs_extattr.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/ufs/ufs/ufs_extattr.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: ufs_extattr.c,v 1.6 2005/12/23 23:20:00 rpaulo Exp $	*/
+/*	$NetBSD: ufs_extattr.c,v 1.7 2006/03/01 12:38:33 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1999-2002 Robert N. M. Watson
@@ -48,7 +48,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: ufs_extattr.c,v 1.6 2005/12/23 23:20:00 rpaulo Exp $");
+__RCSID("$NetBSD: ufs_extattr.c,v 1.7 2006/03/01 12:38:33 yamt Exp $");
 
 #include "opt_ffs.h"
 
@@ -377,9 +377,8 @@
 	auio.uio_iov = &aiov;
 	auio.uio_iovcnt = 1;
 	auio.uio_rw = UIO_READ;
-	auio.uio_segflg = UIO_SYSSPACE;
-	auio.uio_lwp = l;
 	auio.uio_offset = 0;
+	UIO_SETUP_SYSSPACE(&auio);
 
 	vargs.a_desc = NULL;
 	vargs.a_vp = dvp;
@@ -633,9 +632,8 @@
 	aiov.iov_len = sizeof(struct ufs_extattr_fileheader);
 	auio.uio_resid = sizeof(struct ufs_extattr_fileheader);
 	auio.uio_offset = (off_t) 0;
-	auio.uio_segflg = UIO_SYSSPACE;
 	auio.uio_rw = UIO_READ;
-	auio.uio_lwp = l;
+	UIO_SETUP_SYSSPACE(&auio);
 
 	VOP_LEASE(backing_vnode, l, l->l_proc->p_ucred, LEASE_WRITE);
 	vn_lock(backing_vnode, LK_SHARED | LK_RETRY);
@@ -891,10 +889,9 @@
 	local_aio.uio_iov = &local_aiov;
 	local_aio.uio_iovcnt = 1;
 	local_aio.uio_rw = UIO_READ;
-	local_aio.uio_segflg = UIO_SYSSPACE;
-	local_aio.uio_lwp = l;
 	local_aio.uio_offset = base_offset;
 	local_aio.uio_resid = sizeof(struct ufs_extattr_header);
+	UIO_SETUP_SYSSPACE(&local_aio);
 
 	/*
 	 * Acquire locks.
@@ -1113,10 +1110,9 @@
 	local_aio.uio_iov = &local_aiov;
 	local_aio.uio_iovcnt = 1;
 	local_aio.uio_rw = UIO_WRITE;
-	local_aio.uio_segflg = UIO_SYSSPACE;
-	local_aio.uio_lwp = l;
 	local_aio.uio_offset = base_offset;
 	local_aio.uio_resid = sizeof(struct ufs_extattr_header);
+	UIO_SETUP_SYSSPACE(&local_aio);
 
 	/*
 	 * Acquire locks.
@@ -1216,10 +1212,9 @@
 	local_aio.uio_iov = &local_aiov;
 	local_aio.uio_iovcnt = 1;
 	local_aio.uio_rw = UIO_READ;
-	local_aio.uio_segflg = UIO_SYSSPACE;
-	local_aio.uio_lwp = l;
 	local_aio.uio_offset = base_offset;
 	local_aio.uio_resid = sizeof(struct ufs_extattr_header);
+	UIO_SETUP_SYSSPACE(&local_aio);
 
 	VOP_LEASE(attribute->uele_backing_vnode, l, cred, LEASE_WRITE);
 
@@ -1272,10 +1267,9 @@
 	local_aio.uio_iov = &local_aiov;
 	local_aio.uio_iovcnt = 1;
 	local_aio.uio_rw = UIO_WRITE;
-	local_aio.uio_segflg = UIO_SYSSPACE;
-	local_aio.uio_lwp = l;
 	local_aio.uio_offset = base_offset;
 	local_aio.uio_resid = sizeof(struct ufs_extattr_header);
+	UIO_SETUP_SYSSPACE(&local_aio);
 
 	ioflag = IO_NODELOCKED;
 	if (ufs_extattr_sync)
--- a/sys/ufs/ufs/ufs_quota.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/ufs/ufs/ufs_quota.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: ufs_quota.c,v 1.37 2005/12/27 04:06:46 chs Exp $	*/
+/*	$NetBSD: ufs_quota.c,v 1.38 2006/03/01 12:38:33 yamt Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1990, 1993, 1995
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ufs_quota.c,v 1.37 2005/12/27 04:06:46 chs Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ufs_quota.c,v 1.38 2006/03/01 12:38:33 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
@@ -764,9 +764,8 @@
 	aiov.iov_len = sizeof (struct dqblk);
 	auio.uio_resid = sizeof (struct dqblk);
 	auio.uio_offset = (off_t)(id * sizeof (struct dqblk));
-	auio.uio_segflg = UIO_SYSSPACE;
 	auio.uio_rw = UIO_READ;
-	auio.uio_lwp = NULL;
+	UIO_SETUP_SYSSPACE(&auio);
 	error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
 	if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
 		memset((caddr_t)&dq->dq_dqb, 0, sizeof(struct dqblk));
@@ -870,9 +869,8 @@
 	aiov.iov_len = sizeof (struct dqblk);
 	auio.uio_resid = sizeof (struct dqblk);
 	auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct dqblk));
-	auio.uio_segflg = UIO_SYSSPACE;
 	auio.uio_rw = UIO_WRITE;
-	auio.uio_lwp = NULL;
+	UIO_SETUP_SYSSPACE(&auio);
 	error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
 	if (auio.uio_resid && error == 0)
 		error = EIO;
--- a/sys/ufs/ufs/ufs_readwrite.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/ufs/ufs/ufs_readwrite.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: ufs_readwrite.c,v 1.66 2005/12/11 12:25:28 christos Exp $	*/
+/*	$NetBSD: ufs_readwrite.c,v 1.67 2006/03/01 12:38:33 yamt Exp $	*/
 
 /*-
  * Copyright (c) 1993
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(1, "$NetBSD: ufs_readwrite.c,v 1.66 2005/12/11 12:25:28 christos Exp $");
+__KERNEL_RCSID(1, "$NetBSD: ufs_readwrite.c,v 1.67 2006/03/01 12:38:33 yamt Exp $");
 
 #ifdef LFS_READWRITE
 #define	BLKSIZE(a, b, c)	blksize(a, b, c)
@@ -262,7 +262,7 @@
 	 * Maybe this should be above the vnode op call, but so long as
 	 * file servers have no limits, I don't think it matters.
 	 */
-	l = uio->uio_lwp;
+	l = curlwp;
 	if (vp->v_type == VREG && l &&
 	    uio->uio_offset + uio->uio_resid >
 	    l->l_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
@@ -490,7 +490,7 @@
 		VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
 	if (error) {
 		(void) UFS_TRUNCATE(vp, osize, ioflag & IO_SYNC, ap->a_cred,
-		    uio->uio_lwp);
+		    curlwp);
 		uio->uio_offset -= resid - uio->uio_resid;
 		uio->uio_resid = resid;
 	} else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
--- a/sys/ufs/ufs/ufs_vnops.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/ufs/ufs/ufs_vnops.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: ufs_vnops.c,v 1.138 2005/12/11 12:25:28 christos Exp $	*/
+/*	$NetBSD: ufs_vnops.c,v 1.139 2006/03/01 12:38:33 yamt Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1993, 1995
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ufs_vnops.c,v 1.138 2005/12/11 12:25:28 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ufs_vnops.c,v 1.139 2006/03/01 12:38:33 yamt Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_ffs.h"
@@ -1597,7 +1597,7 @@
 	auio.uio_iovcnt = 1;
 	auio.uio_offset = uio->uio_offset;
 	auio.uio_resid = rcount;
-	auio.uio_segflg = UIO_SYSSPACE;
+	UIO_SETUP_SYSSPACE(&auio);
 	auio.uio_rw = UIO_READ;
 	cdbuf = malloc(rcount, M_TEMP, M_WAITOK);
 	aiov.iov_base = cdbuf;
--- a/sys/uvm/uvm_extern.h	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/uvm/uvm_extern.h	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: uvm_extern.h,v 1.110 2006/02/10 00:53:04 simonb Exp $	*/
+/*	$NetBSD: uvm_extern.h,v 1.111 2006/03/01 12:38:44 yamt Exp $	*/
 
 /*
  *
@@ -511,6 +511,7 @@
 	caddr_t vm_maxsaddr;	/* user VA at max stack growth */
 	caddr_t vm_minsaddr;	/* user VA at top of stack */
 };
+#define	VMSPACE_IS_KERNEL_P(vm)	VM_MAP_IS_KERNEL(&(vm)->vm_map)
 
 #ifdef _KERNEL
 
@@ -639,6 +640,7 @@
 			    vaddr_t, vaddr_t);
 void			uvmspace_exec(struct lwp *, vaddr_t, vaddr_t);
 struct vmspace		*uvmspace_fork(struct vmspace *);
+void			uvmspace_addref(struct vmspace *);
 void			uvmspace_free(struct vmspace *);
 void			uvmspace_share(struct proc *, struct proc *);
 void			uvmspace_unshare(struct lwp *);
--- a/sys/uvm/uvm_map.c	Wed Mar 01 12:23:47 2006 +0000
+++ b/sys/uvm/uvm_map.c	Wed Mar 01 12:38:10 2006 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: uvm_map.c,v 1.214 2006/02/22 22:20:56 bjh21 Exp $	*/
+/*	$NetBSD: uvm_map.c,v 1.215 2006/03/01 12:38:44 yamt Exp $	*/
 
 /*
  * Copyright (c) 1997 Charles D. Cranor and Washington University.
@@ -71,7 +71,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: uvm_map.c,v 1.214 2006/02/22 22:20:56 bjh21 Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uvm_map.c,v 1.215 2006/03/01 12:38:44 yamt Exp $");
 
 #include "opt_ddb.h"
 #include "opt_uvmhist.h"
@@ -3652,12 +3652,9 @@
 void
 uvmspace_share(struct proc *p1, struct proc *p2)
 {
-	struct simplelock *slock = &p1->p_vmspace->vm_map.ref_lock;
-
+
+	uvmspace_addref(p1->p_vmspace);
 	p2->p_vmspace = p1->p_vmspace;
-	simple_lock(slock);
-	p1->p_vmspace->vm_refcnt++;
-	simple_unlock(slock);
 }
 
 /*
@@ -3767,6 +3764,23 @@
 }
 
 /*
+ * uvmspace_addref: add a referece to a vmspace.
+ */
+
+void
+uvmspace_addref(struct vmspace *vm)
+{
+	struct vm_map *map = &vm->vm_map;
+
+	KASSERT((map->flags & VM_MAP_DYING) == 0);
+
+	simple_lock(&map->ref_lock);
+	KASSERT(vm->vm_refcnt > 0);
+	vm->vm_refcnt++;
+	simple_unlock(&map->ref_lock);
+}
+
+/*
  * uvmspace_free: free a vmspace data structure
  */