xref: /onnv-gate/usr/src/uts/common/io/mem.c (revision 513:1b8c27adef4f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Memory special file
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include <sys/user.h>
360Sstevel@tonic-gate #include <sys/buf.h>
370Sstevel@tonic-gate #include <sys/systm.h>
380Sstevel@tonic-gate #include <sys/cred.h>
390Sstevel@tonic-gate #include <sys/vm.h>
400Sstevel@tonic-gate #include <sys/uio.h>
410Sstevel@tonic-gate #include <sys/mman.h>
420Sstevel@tonic-gate #include <sys/kmem.h>
430Sstevel@tonic-gate #include <vm/seg.h>
440Sstevel@tonic-gate #include <vm/page.h>
450Sstevel@tonic-gate #include <sys/stat.h>
460Sstevel@tonic-gate #include <sys/vmem.h>
470Sstevel@tonic-gate #include <sys/memlist.h>
480Sstevel@tonic-gate #include <sys/bootconf.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include <vm/seg_vn.h>
510Sstevel@tonic-gate #include <vm/seg_dev.h>
520Sstevel@tonic-gate #include <vm/seg_kmem.h>
530Sstevel@tonic-gate #include <vm/seg_kp.h>
540Sstevel@tonic-gate #include <vm/seg_kpm.h>
550Sstevel@tonic-gate #include <vm/hat.h>
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #include <sys/conf.h>
580Sstevel@tonic-gate #include <sys/mem.h>
590Sstevel@tonic-gate #include <sys/types.h>
600Sstevel@tonic-gate #include <sys/conf.h>
610Sstevel@tonic-gate #include <sys/param.h>
620Sstevel@tonic-gate #include <sys/systm.h>
630Sstevel@tonic-gate #include <sys/errno.h>
640Sstevel@tonic-gate #include <sys/modctl.h>
650Sstevel@tonic-gate #include <sys/memlist.h>
660Sstevel@tonic-gate #include <sys/ddi.h>
670Sstevel@tonic-gate #include <sys/sunddi.h>
680Sstevel@tonic-gate #include <sys/debug.h>
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #ifdef __sparc
710Sstevel@tonic-gate extern int cpu_get_mem_name(uint64_t, uint64_t *, uint64_t, char *, int, int *);
720Sstevel@tonic-gate extern int cpu_get_mem_info(uint64_t, uint64_t, uint64_t *, uint64_t *,
730Sstevel@tonic-gate     uint64_t *, int *, int *, int *);
740Sstevel@tonic-gate extern size_t cpu_get_name_bufsize(void);
750Sstevel@tonic-gate #endif
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * Turn a byte length into a pagecount.  The DDI btop takes a
790Sstevel@tonic-gate  * 32-bit size on 32-bit machines, this handles 64-bit sizes for
800Sstevel@tonic-gate  * large physical-memory 32-bit machines.
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate #define	BTOP(x)	((pgcnt_t)((x) >> _pageshift))
830Sstevel@tonic-gate 
840Sstevel@tonic-gate static kmutex_t mm_lock;
850Sstevel@tonic-gate static caddr_t mm_map;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate static dev_info_t *mm_dip;	/* private copy of devinfo pointer */
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static int mm_kmem_io_access;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate static int mm_kstat_update(kstat_t *ksp, int rw);
920Sstevel@tonic-gate static int mm_kstat_snapshot(kstat_t *ksp, void *buf, int rw);
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /*ARGSUSED1*/
950Sstevel@tonic-gate static int
960Sstevel@tonic-gate mm_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate 	int i;
990Sstevel@tonic-gate 	struct mem_minor {
1000Sstevel@tonic-gate 		char *name;
1010Sstevel@tonic-gate 		minor_t minor;
1020Sstevel@tonic-gate 		int privonly;
1030Sstevel@tonic-gate 		const char *rdpriv;
1040Sstevel@tonic-gate 		const char *wrpriv;
1050Sstevel@tonic-gate 		mode_t priv_mode;
1060Sstevel@tonic-gate 	} mm[] = {
1070Sstevel@tonic-gate 		{ "mem",	M_MEM,		0,	NULL,	"all",	0640 },
1080Sstevel@tonic-gate 		{ "kmem",	M_KMEM,		0,	NULL,	"all",	0640 },
1090Sstevel@tonic-gate 		{ "allkmem",	M_ALLKMEM,	0,	"all",	"all",	0600 },
1100Sstevel@tonic-gate 		{ "null",	M_NULL,	PRIVONLY_DEV,	NULL,	NULL,	0666 },
1110Sstevel@tonic-gate 		{ "zero",	M_ZERO, PRIVONLY_DEV,	NULL,	NULL,	0666 },
1120Sstevel@tonic-gate 	};
1130Sstevel@tonic-gate 	kstat_t *ksp;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	mutex_init(&mm_lock, NULL, MUTEX_DEFAULT, NULL);
1160Sstevel@tonic-gate 	mm_map = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	for (i = 0; i < (sizeof (mm) / sizeof (mm[0])); i++) {
1190Sstevel@tonic-gate 		if (ddi_create_priv_minor_node(devi, mm[i].name, S_IFCHR,
1200Sstevel@tonic-gate 		    mm[i].minor, DDI_PSEUDO, mm[i].privonly,
1210Sstevel@tonic-gate 		    mm[i].rdpriv, mm[i].wrpriv, mm[i].priv_mode) ==
1220Sstevel@tonic-gate 		    DDI_FAILURE) {
1230Sstevel@tonic-gate 			ddi_remove_minor_node(devi, NULL);
1240Sstevel@tonic-gate 			return (DDI_FAILURE);
1250Sstevel@tonic-gate 		}
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	mm_dip = devi;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	ksp = kstat_create("mm", 0, "phys_installed", "misc",
1310Sstevel@tonic-gate 	    KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VAR_SIZE | KSTAT_FLAG_VIRTUAL);
1320Sstevel@tonic-gate 	if (ksp != NULL) {
1330Sstevel@tonic-gate 		ksp->ks_update = mm_kstat_update;
1340Sstevel@tonic-gate 		ksp->ks_snapshot = mm_kstat_snapshot;
1350Sstevel@tonic-gate 		ksp->ks_lock = &mm_lock; /* XXX - not really needed */
1360Sstevel@tonic-gate 		kstat_install(ksp);
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	mm_kmem_io_access = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
1400Sstevel@tonic-gate 	    "kmem_io_access", 0);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	return (DDI_SUCCESS);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate /*ARGSUSED*/
1460Sstevel@tonic-gate static int
1470Sstevel@tonic-gate mm_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	register int error;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	switch (infocmd) {
1520Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
1530Sstevel@tonic-gate 		*result = (void *)mm_dip;
1540Sstevel@tonic-gate 		error = DDI_SUCCESS;
1550Sstevel@tonic-gate 		break;
1560Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
1570Sstevel@tonic-gate 		*result = (void *)0;
1580Sstevel@tonic-gate 		error = DDI_SUCCESS;
1590Sstevel@tonic-gate 		break;
1600Sstevel@tonic-gate 	default:
1610Sstevel@tonic-gate 		error = DDI_FAILURE;
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 	return (error);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate /*ARGSUSED1*/
1670Sstevel@tonic-gate static int
1680Sstevel@tonic-gate mmopen(dev_t *devp, int flag, int typ, struct cred *cred)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate 	switch (getminor(*devp)) {
1710Sstevel@tonic-gate 	case M_NULL:
1720Sstevel@tonic-gate 	case M_ZERO:
1730Sstevel@tonic-gate 	case M_MEM:
1740Sstevel@tonic-gate 	case M_KMEM:
1750Sstevel@tonic-gate 	case M_ALLKMEM:
1760Sstevel@tonic-gate 		/* standard devices */
1770Sstevel@tonic-gate 		break;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	default:
1800Sstevel@tonic-gate 		/* Unsupported or unknown type */
1810Sstevel@tonic-gate 		return (EINVAL);
1820Sstevel@tonic-gate 	}
1830Sstevel@tonic-gate 	return (0);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate struct pollhead	mm_pollhd;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate /*ARGSUSED*/
1890Sstevel@tonic-gate static int
1900Sstevel@tonic-gate mmchpoll(dev_t dev, short events, int anyyet, short *reventsp,
1910Sstevel@tonic-gate     struct pollhead **phpp)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate 	switch (getminor(dev)) {
1940Sstevel@tonic-gate 	case M_NULL:
1950Sstevel@tonic-gate 	case M_ZERO:
1960Sstevel@tonic-gate 	case M_MEM:
1970Sstevel@tonic-gate 	case M_KMEM:
1980Sstevel@tonic-gate 	case M_ALLKMEM:
1990Sstevel@tonic-gate 		*reventsp = events & (POLLIN | POLLOUT | POLLPRI | POLLRDNORM |
2000Sstevel@tonic-gate 			POLLWRNORM | POLLRDBAND | POLLWRBAND);
2010Sstevel@tonic-gate 		/*
2020Sstevel@tonic-gate 		 * A non NULL pollhead pointer should be returned in case
2030Sstevel@tonic-gate 		 * user polls for 0 events.
2040Sstevel@tonic-gate 		 */
2050Sstevel@tonic-gate 		*phpp = !anyyet && !*reventsp ?
2060Sstevel@tonic-gate 		    &mm_pollhd : (struct pollhead *)NULL;
2070Sstevel@tonic-gate 		return (0);
2080Sstevel@tonic-gate 	default:
2090Sstevel@tonic-gate 		/* no other devices currently support polling */
2100Sstevel@tonic-gate 		return (ENXIO);
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate static int
2150Sstevel@tonic-gate mmpropop(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int flags,
2160Sstevel@tonic-gate     char *name, caddr_t valuep, int *lengthp)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate 	/*
2190Sstevel@tonic-gate 	 * implement zero size to reduce overhead (avoid two failing
2200Sstevel@tonic-gate 	 * property lookups per stat).
2210Sstevel@tonic-gate 	 */
2220Sstevel@tonic-gate 	return (ddi_prop_op_size(dev, dip, prop_op,
2230Sstevel@tonic-gate 	    flags, name, valuep, lengthp, 0));
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate static int
2270Sstevel@tonic-gate mmio(struct uio *uio, enum uio_rw rw, pfn_t pfn, off_t pageoff, int allowio)
2280Sstevel@tonic-gate {
2290Sstevel@tonic-gate 	int error = 0;
2300Sstevel@tonic-gate 	size_t nbytes = MIN((size_t)(PAGESIZE - pageoff),
2310Sstevel@tonic-gate 	    (size_t)uio->uio_iov->iov_len);
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	mutex_enter(&mm_lock);
2340Sstevel@tonic-gate 	hat_devload(kas.a_hat, mm_map, PAGESIZE, pfn,
2350Sstevel@tonic-gate 	    (uint_t)(rw == UIO_READ ? PROT_READ : PROT_READ | PROT_WRITE),
2360Sstevel@tonic-gate 	    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	if (!pf_is_memory(pfn)) {
2390Sstevel@tonic-gate 		if (allowio) {
2400Sstevel@tonic-gate 			size_t c = uio->uio_iov->iov_len;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 			if (ddi_peekpokeio(NULL, uio, rw,
2430Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)uio->uio_loffset, c,
2440Sstevel@tonic-gate 			    sizeof (int32_t)) != DDI_SUCCESS)
2450Sstevel@tonic-gate 				error = EFAULT;
2460Sstevel@tonic-gate 		} else
2470Sstevel@tonic-gate 			error = EIO;
2480Sstevel@tonic-gate 	} else
2490Sstevel@tonic-gate 		error = uiomove(&mm_map[pageoff], nbytes, rw, uio);
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	hat_unload(kas.a_hat, mm_map, PAGESIZE, HAT_UNLOAD_UNLOCK);
2520Sstevel@tonic-gate 	mutex_exit(&mm_lock);
2530Sstevel@tonic-gate 	return (error);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate #ifdef	__sparc
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate #define	IS_KPM_VA(va)							\
2590Sstevel@tonic-gate 	(kpm_enable && (va) >= segkpm->s_base &&			\
2600Sstevel@tonic-gate 	(va) < (segkpm->s_base + segkpm->s_size))
2610Sstevel@tonic-gate #define	IS_KP_VA(va)							\
2620Sstevel@tonic-gate 	((va) >= segkp->s_base && (va) < segkp->s_base + segkp->s_size)
2630Sstevel@tonic-gate #define	NEED_LOCK_KVADDR(va)	(!IS_KPM_VA(va) && !IS_KP_VA(va))
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate #else	/* __i386, __amd64 */
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate #define	NEED_LOCK_KVADDR(va)	0
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate #endif	/* __sparc */
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate /*ARGSUSED3*/
2720Sstevel@tonic-gate static int
2730Sstevel@tonic-gate mmrw(dev_t dev, struct uio *uio, enum uio_rw rw, cred_t *cred)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	pfn_t v;
2760Sstevel@tonic-gate 	struct iovec *iov;
2770Sstevel@tonic-gate 	int error = 0;
2780Sstevel@tonic-gate 	size_t c;
2790Sstevel@tonic-gate 	ssize_t oresid = uio->uio_resid;
2800Sstevel@tonic-gate 	minor_t minor = getminor(dev);
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	while (uio->uio_resid > 0 && error == 0) {
2830Sstevel@tonic-gate 		iov = uio->uio_iov;
2840Sstevel@tonic-gate 		if (iov->iov_len == 0) {
2850Sstevel@tonic-gate 			uio->uio_iov++;
2860Sstevel@tonic-gate 			uio->uio_iovcnt--;
2870Sstevel@tonic-gate 			if (uio->uio_iovcnt < 0)
2880Sstevel@tonic-gate 				panic("mmrw");
2890Sstevel@tonic-gate 			continue;
2900Sstevel@tonic-gate 		}
2910Sstevel@tonic-gate 		switch (minor) {
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 		case M_MEM:
2940Sstevel@tonic-gate 			memlist_read_lock();
2950Sstevel@tonic-gate 			if (!address_in_memlist(phys_install,
2960Sstevel@tonic-gate 			    (uint64_t)uio->uio_loffset, 1)) {
2970Sstevel@tonic-gate 				memlist_read_unlock();
2980Sstevel@tonic-gate 				error = EFAULT;
2990Sstevel@tonic-gate 				break;
3000Sstevel@tonic-gate 			}
3010Sstevel@tonic-gate 			memlist_read_unlock();
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 			v = BTOP((u_offset_t)uio->uio_loffset);
3040Sstevel@tonic-gate 			error = mmio(uio, rw, v,
3050Sstevel@tonic-gate 			    uio->uio_loffset & PAGEOFFSET, 0);
3060Sstevel@tonic-gate 			break;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 		case M_KMEM:
3090Sstevel@tonic-gate 		case M_ALLKMEM:
3100Sstevel@tonic-gate 			{
3110Sstevel@tonic-gate 			page_t **ppp;
3120Sstevel@tonic-gate 			caddr_t vaddr = (caddr_t)uio->uio_offset;
3130Sstevel@tonic-gate 			int try_lock = NEED_LOCK_KVADDR(vaddr);
3140Sstevel@tonic-gate 			int locked = 0;
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 			/*
3170Sstevel@tonic-gate 			 * If vaddr does not map a valid page, as_pagelock()
3180Sstevel@tonic-gate 			 * will return failure. Hence we can't check the
3190Sstevel@tonic-gate 			 * return value and return EFAULT here as we'd like.
3200Sstevel@tonic-gate 			 * seg_kp and seg_kpm do not properly support
3210Sstevel@tonic-gate 			 * as_pagelock() for this context so we avoid it
3220Sstevel@tonic-gate 			 * using the try_lock set check above.  Some day when
3230Sstevel@tonic-gate 			 * the kernel page locking gets redesigned all this
3240Sstevel@tonic-gate 			 * muck can be cleaned up.
3250Sstevel@tonic-gate 			 */
3260Sstevel@tonic-gate 			if (try_lock)
3270Sstevel@tonic-gate 				locked = (as_pagelock(&kas, &ppp, vaddr,
3280Sstevel@tonic-gate 				    PAGESIZE, S_WRITE) == 0);
3290Sstevel@tonic-gate 
330*513Sjongkis 			v = hat_getpfnum(kas.a_hat,
331*513Sjongkis 			    (caddr_t)(uintptr_t)uio->uio_loffset);
3320Sstevel@tonic-gate 			if (v == PFN_INVALID) {
3330Sstevel@tonic-gate 				if (locked)
3340Sstevel@tonic-gate 					as_pageunlock(&kas, ppp, vaddr,
3350Sstevel@tonic-gate 					    PAGESIZE, S_WRITE);
3360Sstevel@tonic-gate 				error = EFAULT;
3370Sstevel@tonic-gate 				break;
3380Sstevel@tonic-gate 			}
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 			error = mmio(uio, rw, v, uio->uio_loffset & PAGEOFFSET,
3410Sstevel@tonic-gate 			    minor == M_ALLKMEM || mm_kmem_io_access);
3420Sstevel@tonic-gate 			if (locked)
3430Sstevel@tonic-gate 				as_pageunlock(&kas, ppp, vaddr, PAGESIZE,
3440Sstevel@tonic-gate 				    S_WRITE);
3450Sstevel@tonic-gate 			}
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 			break;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 		case M_ZERO:
3500Sstevel@tonic-gate 			if (rw == UIO_READ) {
3510Sstevel@tonic-gate 				label_t ljb;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 				if (on_fault(&ljb)) {
3540Sstevel@tonic-gate 					no_fault();
3550Sstevel@tonic-gate 					error = EFAULT;
3560Sstevel@tonic-gate 					break;
3570Sstevel@tonic-gate 				}
3580Sstevel@tonic-gate 				uzero(iov->iov_base, iov->iov_len);
3590Sstevel@tonic-gate 				no_fault();
3600Sstevel@tonic-gate 				uio->uio_resid -= iov->iov_len;
3610Sstevel@tonic-gate 				uio->uio_loffset += iov->iov_len;
3620Sstevel@tonic-gate 				break;
3630Sstevel@tonic-gate 			}
3640Sstevel@tonic-gate 			/* else it's a write, fall through to NULL case */
3650Sstevel@tonic-gate 			/*FALLTHROUGH*/
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 		case M_NULL:
3680Sstevel@tonic-gate 			if (rw == UIO_READ)
3690Sstevel@tonic-gate 				return (0);
3700Sstevel@tonic-gate 			c = iov->iov_len;
3710Sstevel@tonic-gate 			iov->iov_base += c;
3720Sstevel@tonic-gate 			iov->iov_len -= c;
3730Sstevel@tonic-gate 			uio->uio_loffset += c;
3740Sstevel@tonic-gate 			uio->uio_resid -= c;
3750Sstevel@tonic-gate 			break;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 		}
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 	return (uio->uio_resid == oresid ? error : 0);
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate static int
3830Sstevel@tonic-gate mmread(dev_t dev, struct uio *uio, cred_t *cred)
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate 	return (mmrw(dev, uio, UIO_READ, cred));
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate static int
3890Sstevel@tonic-gate mmwrite(dev_t dev, struct uio *uio, cred_t *cred)
3900Sstevel@tonic-gate {
3910Sstevel@tonic-gate 	return (mmrw(dev, uio, UIO_WRITE, cred));
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate /*
3950Sstevel@tonic-gate  * Private ioctl for libkvm to support kvm_physaddr().
3960Sstevel@tonic-gate  * Given an address space and a VA, compute the PA.
3970Sstevel@tonic-gate  */
3980Sstevel@tonic-gate static int
3990Sstevel@tonic-gate mmioctl_vtop(intptr_t data)
4000Sstevel@tonic-gate {
4010Sstevel@tonic-gate 	mem_vtop_t mem_vtop;
4020Sstevel@tonic-gate 	proc_t *p;
4030Sstevel@tonic-gate 	pfn_t pfn = (pfn_t)PFN_INVALID;
4040Sstevel@tonic-gate 	pid_t pid = 0;
4050Sstevel@tonic-gate 	struct as *as;
4060Sstevel@tonic-gate 	struct seg *seg;
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	if (copyin((void *)data, &mem_vtop, sizeof (mem_vtop_t)))
4090Sstevel@tonic-gate 		return (EFAULT);
4100Sstevel@tonic-gate 	if (mem_vtop.m_as == &kas) {
4110Sstevel@tonic-gate 		pfn = hat_getpfnum(kas.a_hat, mem_vtop.m_va);
4120Sstevel@tonic-gate 	} else if (mem_vtop.m_as == NULL) {
4130Sstevel@tonic-gate 		return (EIO);
4140Sstevel@tonic-gate 	} else {
4150Sstevel@tonic-gate 		mutex_enter(&pidlock);
4160Sstevel@tonic-gate 		for (p = practive; p != NULL; p = p->p_next) {
4170Sstevel@tonic-gate 			if (p->p_as == mem_vtop.m_as) {
4180Sstevel@tonic-gate 				pid = p->p_pid;
4190Sstevel@tonic-gate 				break;
4200Sstevel@tonic-gate 			}
4210Sstevel@tonic-gate 		}
4220Sstevel@tonic-gate 		mutex_exit(&pidlock);
4230Sstevel@tonic-gate 		if (p == NULL)
4240Sstevel@tonic-gate 			return (EIO);
4250Sstevel@tonic-gate 		p = sprlock(pid);
4260Sstevel@tonic-gate 		if (p == NULL)
4270Sstevel@tonic-gate 			return (EIO);
4280Sstevel@tonic-gate 		as = p->p_as;
4290Sstevel@tonic-gate 		if (as == mem_vtop.m_as) {
4300Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
4310Sstevel@tonic-gate 			AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
4320Sstevel@tonic-gate 			for (seg = AS_SEGFIRST(as); seg != NULL;
4330Sstevel@tonic-gate 			    seg = AS_SEGNEXT(as, seg))
4340Sstevel@tonic-gate 				if ((uintptr_t)mem_vtop.m_va -
4350Sstevel@tonic-gate 				    (uintptr_t)seg->s_base < seg->s_size)
4360Sstevel@tonic-gate 					break;
4370Sstevel@tonic-gate 			if (seg != NULL)
4380Sstevel@tonic-gate 				pfn = hat_getpfnum(as->a_hat, mem_vtop.m_va);
4390Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
4400Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
4410Sstevel@tonic-gate 		}
4420Sstevel@tonic-gate 		sprunlock(p);
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate 	mem_vtop.m_pfn = pfn;
4450Sstevel@tonic-gate 	if (pfn == PFN_INVALID)
4460Sstevel@tonic-gate 		return (EIO);
4470Sstevel@tonic-gate 	if (copyout(&mem_vtop, (void *)data, sizeof (mem_vtop_t)))
4480Sstevel@tonic-gate 		return (EFAULT);
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	return (0);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate /*
4540Sstevel@tonic-gate  * Given a PA, retire that page or check whether it has already been retired.
4550Sstevel@tonic-gate  */
4560Sstevel@tonic-gate static int
4570Sstevel@tonic-gate mmioctl_page_retire(int cmd, intptr_t data)
4580Sstevel@tonic-gate {
4590Sstevel@tonic-gate 	uint64_t pa;
4600Sstevel@tonic-gate 	pfn_t pfn;
4610Sstevel@tonic-gate 	page_t *pp;
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	if (copyin((void *)data, &pa, sizeof (uint64_t)))
4640Sstevel@tonic-gate 		return (EFAULT);
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	pfn = pa >> MMU_PAGESHIFT;
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	if (!pf_is_memory(pfn) || (pp = page_numtopp_nolock(pfn)) == NULL)
4690Sstevel@tonic-gate 		return (EINVAL);
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	/*
4720Sstevel@tonic-gate 	 * If we're checking, see if the page is retired; if not, confirm that
4730Sstevel@tonic-gate 	 * its status is at least set to be failing.  If neither, return EIO.
4740Sstevel@tonic-gate 	 */
4750Sstevel@tonic-gate 	if (cmd == MEM_PAGE_ISRETIRED) {
4760Sstevel@tonic-gate 		if (page_isretired(pp))
4770Sstevel@tonic-gate 			return (0);
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 		if (!page_isfailing(pp))
4800Sstevel@tonic-gate 			return (EIO);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 		return (EAGAIN);
4830Sstevel@tonic-gate 	}
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	/*
4860Sstevel@tonic-gate 	 * Try to retire the page. If the retire fails, it will be scheduled to
4870Sstevel@tonic-gate 	 * occur when the page is freed.  If this page is out of circulation
4880Sstevel@tonic-gate 	 * already, or is in the process of being retired, we fail.
4890Sstevel@tonic-gate 	 */
4900Sstevel@tonic-gate 	if (page_isretired(pp) || page_isfailing(pp))
4910Sstevel@tonic-gate 		return (EIO);
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	page_settoxic(pp, PAGE_IS_FAULTY);
4940Sstevel@tonic-gate 	return (page_retire(pp, PAGE_IS_FAILING) ? EAGAIN : 0);
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate #ifdef __sparc
4980Sstevel@tonic-gate /*
4990Sstevel@tonic-gate  * Given a syndrome, syndrome type, and address return the
5000Sstevel@tonic-gate  * associated memory name in the provided data buffer.
5010Sstevel@tonic-gate  */
5020Sstevel@tonic-gate static int
5030Sstevel@tonic-gate mmioctl_get_mem_name(intptr_t data)
5040Sstevel@tonic-gate {
5050Sstevel@tonic-gate 	mem_name_t mem_name;
5060Sstevel@tonic-gate #ifdef	_SYSCALL32
5070Sstevel@tonic-gate 	mem_name32_t mem_name32;
5080Sstevel@tonic-gate #endif
5090Sstevel@tonic-gate 	void *buf;
5100Sstevel@tonic-gate 	size_t bufsize;
5110Sstevel@tonic-gate 	int len, err;
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	if ((bufsize = cpu_get_name_bufsize()) == 0)
5140Sstevel@tonic-gate 		return (ENOTSUP);
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
5170Sstevel@tonic-gate 		if (copyin((void *)data, &mem_name, sizeof (mem_name_t)))
5180Sstevel@tonic-gate 			return (EFAULT);
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate #ifdef	_SYSCALL32
5210Sstevel@tonic-gate 	else {
5220Sstevel@tonic-gate 		if (copyin((void *)data, &mem_name32, sizeof (mem_name32_t)))
5230Sstevel@tonic-gate 			return (EFAULT);
5240Sstevel@tonic-gate 		mem_name.m_addr = mem_name32.m_addr;
5250Sstevel@tonic-gate 		mem_name.m_synd = mem_name32.m_synd;
5260Sstevel@tonic-gate 		mem_name.m_type[0] = mem_name32.m_type[0];
5270Sstevel@tonic-gate 		mem_name.m_type[1] = mem_name32.m_type[1];
528*513Sjongkis 		mem_name.m_name = (caddr_t)(uintptr_t)mem_name32.m_name;
5290Sstevel@tonic-gate 		mem_name.m_namelen = (size_t)mem_name32.m_namelen;
5300Sstevel@tonic-gate 	}
5310Sstevel@tonic-gate #endif	/* _SYSCALL32 */
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	buf = kmem_alloc(bufsize, KM_SLEEP);
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	/*
5360Sstevel@tonic-gate 	 * Call into cpu specific code to do the lookup.
5370Sstevel@tonic-gate 	 */
5380Sstevel@tonic-gate 	if ((err = cpu_get_mem_name(mem_name.m_synd, mem_name.m_type,
5390Sstevel@tonic-gate 	    mem_name.m_addr, buf, bufsize, &len)) != 0) {
5400Sstevel@tonic-gate 		kmem_free(buf, bufsize);
5410Sstevel@tonic-gate 		return (err);
5420Sstevel@tonic-gate 	}
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	if (len >= mem_name.m_namelen) {
5450Sstevel@tonic-gate 		kmem_free(buf, bufsize);
5460Sstevel@tonic-gate 		return (ENAMETOOLONG);
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	if (copyoutstr(buf, (char *)mem_name.m_name,
5500Sstevel@tonic-gate 	    mem_name.m_namelen, NULL) != 0) {
5510Sstevel@tonic-gate 		kmem_free(buf, bufsize);
5520Sstevel@tonic-gate 		return (EFAULT);
5530Sstevel@tonic-gate 	}
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	kmem_free(buf, bufsize);
5560Sstevel@tonic-gate 	return (0);
5570Sstevel@tonic-gate }
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate /*
5600Sstevel@tonic-gate  * Given a syndrome and address return information about the associated memory.
5610Sstevel@tonic-gate  */
5620Sstevel@tonic-gate static int
5630Sstevel@tonic-gate mmioctl_get_mem_info(intptr_t data)
5640Sstevel@tonic-gate {
5650Sstevel@tonic-gate 	mem_info_t mem_info;
5660Sstevel@tonic-gate 	int err;
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	if (copyin((void *)data, &mem_info, sizeof (mem_info_t)))
5690Sstevel@tonic-gate 		return (EFAULT);
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	if ((err = cpu_get_mem_info(mem_info.m_synd, mem_info.m_addr,
5720Sstevel@tonic-gate 	    &mem_info.m_mem_size, &mem_info.m_seg_size, &mem_info.m_bank_size,
5730Sstevel@tonic-gate 	    &mem_info.m_segments, &mem_info.m_banks, &mem_info.m_mcid)) != 0)
5740Sstevel@tonic-gate 		return (err);
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	if (copyout(&mem_info, (void *)data, sizeof (mem_info_t)) != 0)
5770Sstevel@tonic-gate 		return (EFAULT);
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	return (0);
5800Sstevel@tonic-gate }
5810Sstevel@tonic-gate #endif	/* __sparc */
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate /*
5840Sstevel@tonic-gate  * Private ioctls for
5850Sstevel@tonic-gate  *	libkvm to support kvm_physaddr().
5860Sstevel@tonic-gate  *	FMA support for page_retire() and memory attribute information.
5870Sstevel@tonic-gate  */
5880Sstevel@tonic-gate /*ARGSUSED*/
5890Sstevel@tonic-gate static int
5900Sstevel@tonic-gate mmioctl(dev_t dev, int cmd, intptr_t data, int flag, cred_t *cred, int *rvalp)
5910Sstevel@tonic-gate {
5920Sstevel@tonic-gate 	switch (cmd) {
5930Sstevel@tonic-gate 	case MEM_VTOP:
5940Sstevel@tonic-gate 		if (getminor(dev) != M_KMEM)
5950Sstevel@tonic-gate 			return (ENXIO);
5960Sstevel@tonic-gate 		return (mmioctl_vtop(data));
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	case MEM_PAGE_RETIRE:
5990Sstevel@tonic-gate 	case MEM_PAGE_ISRETIRED:
6000Sstevel@tonic-gate 		if (getminor(dev) != M_MEM)
6010Sstevel@tonic-gate 			return (ENXIO);
6020Sstevel@tonic-gate 		return (mmioctl_page_retire(cmd, data));
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	case MEM_NAME:
6050Sstevel@tonic-gate 		if (getminor(dev) != M_MEM)
6060Sstevel@tonic-gate 			return (ENXIO);
6070Sstevel@tonic-gate #ifdef __sparc
6080Sstevel@tonic-gate 		return (mmioctl_get_mem_name(data));
6090Sstevel@tonic-gate #else
6100Sstevel@tonic-gate 		return (ENOTSUP);
6110Sstevel@tonic-gate #endif
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	case MEM_INFO:
6140Sstevel@tonic-gate 		if (getminor(dev) != M_MEM)
6150Sstevel@tonic-gate 			return (ENXIO);
6160Sstevel@tonic-gate #ifdef __sparc
6170Sstevel@tonic-gate 		return (mmioctl_get_mem_info(data));
6180Sstevel@tonic-gate #else
6190Sstevel@tonic-gate 		return (ENOTSUP);
6200Sstevel@tonic-gate #endif
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate 	return (ENXIO);
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate /*ARGSUSED2*/
6260Sstevel@tonic-gate static int
6270Sstevel@tonic-gate mmmmap(dev_t dev, off_t off, int prot)
6280Sstevel@tonic-gate {
6290Sstevel@tonic-gate 	pfn_t pf;
6300Sstevel@tonic-gate 	struct memlist *pmem;
6310Sstevel@tonic-gate 	minor_t minor = getminor(dev);
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	switch (minor) {
6340Sstevel@tonic-gate 	case M_MEM:
6350Sstevel@tonic-gate 		pf = btop(off);
6360Sstevel@tonic-gate 		memlist_read_lock();
6370Sstevel@tonic-gate 		for (pmem = phys_install; pmem != NULL; pmem = pmem->next) {
6380Sstevel@tonic-gate 			if (pf >= BTOP(pmem->address) &&
6390Sstevel@tonic-gate 			    pf < BTOP(pmem->address + pmem->size)) {
6400Sstevel@tonic-gate 				memlist_read_unlock();
6410Sstevel@tonic-gate 				return (impl_obmem_pfnum(pf));
6420Sstevel@tonic-gate 			}
6430Sstevel@tonic-gate 		}
6440Sstevel@tonic-gate 		memlist_read_unlock();
6450Sstevel@tonic-gate 		break;
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	case M_KMEM:
6480Sstevel@tonic-gate 	case M_ALLKMEM:
6490Sstevel@tonic-gate 		/* no longer supported with KPR */
6500Sstevel@tonic-gate 		return (-1);
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	case M_ZERO:
6530Sstevel@tonic-gate 		/*
6540Sstevel@tonic-gate 		 * We shouldn't be mmap'ing to /dev/zero here as
6550Sstevel@tonic-gate 		 * mmsegmap() should have already converted
6560Sstevel@tonic-gate 		 * a mapping request for this device to a mapping
6570Sstevel@tonic-gate 		 * using seg_vn for anonymous memory.
6580Sstevel@tonic-gate 		 */
6590Sstevel@tonic-gate 		break;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	}
6620Sstevel@tonic-gate 	return (-1);
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate /*
6660Sstevel@tonic-gate  * This function is called when a memory device is mmap'ed.
6670Sstevel@tonic-gate  * Set up the mapping to the correct device driver.
6680Sstevel@tonic-gate  */
6690Sstevel@tonic-gate static int
6700Sstevel@tonic-gate mmsegmap(dev_t dev, off_t off, struct as *as, caddr_t *addrp, off_t len,
6710Sstevel@tonic-gate     uint_t prot, uint_t maxprot, uint_t flags, struct cred *cred)
6720Sstevel@tonic-gate {
6730Sstevel@tonic-gate 	struct segvn_crargs vn_a;
6740Sstevel@tonic-gate 	struct segdev_crargs dev_a;
6750Sstevel@tonic-gate 	int error;
6760Sstevel@tonic-gate 	minor_t minor;
6770Sstevel@tonic-gate 	off_t i;
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	minor = getminor(dev);
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	as_rangelock(as);
6820Sstevel@tonic-gate 	if ((flags & MAP_FIXED) == 0) {
6830Sstevel@tonic-gate 		/*
6840Sstevel@tonic-gate 		 * No need to worry about vac alignment on /dev/zero
6850Sstevel@tonic-gate 		 * since this is a "clone" object that doesn't yet exist.
6860Sstevel@tonic-gate 		 */
6870Sstevel@tonic-gate 		map_addr(addrp, len, (offset_t)off,
6880Sstevel@tonic-gate 				(minor == M_MEM) || (minor == M_KMEM), flags);
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 		if (*addrp == NULL) {
6910Sstevel@tonic-gate 			as_rangeunlock(as);
6920Sstevel@tonic-gate 			return (ENOMEM);
6930Sstevel@tonic-gate 		}
6940Sstevel@tonic-gate 	} else {
6950Sstevel@tonic-gate 		/*
6960Sstevel@tonic-gate 		 * User specified address -
6970Sstevel@tonic-gate 		 * Blow away any previous mappings.
6980Sstevel@tonic-gate 		 */
6990Sstevel@tonic-gate 		(void) as_unmap(as, *addrp, len);
7000Sstevel@tonic-gate 	}
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	switch (minor) {
7030Sstevel@tonic-gate 	case M_MEM:
7040Sstevel@tonic-gate 		/* /dev/mem cannot be mmap'ed with MAP_PRIVATE */
7050Sstevel@tonic-gate 		if ((flags & MAP_TYPE) != MAP_SHARED) {
7060Sstevel@tonic-gate 			as_rangeunlock(as);
7070Sstevel@tonic-gate 			return (EINVAL);
7080Sstevel@tonic-gate 		}
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 		/*
7110Sstevel@tonic-gate 		 * Check to ensure that the entire range is
7120Sstevel@tonic-gate 		 * legal and we are not trying to map in
7130Sstevel@tonic-gate 		 * more than the device will let us.
7140Sstevel@tonic-gate 		 */
7150Sstevel@tonic-gate 		for (i = 0; i < len; i += PAGESIZE) {
7160Sstevel@tonic-gate 			if (mmmmap(dev, off + i, maxprot) == -1) {
7170Sstevel@tonic-gate 				as_rangeunlock(as);
7180Sstevel@tonic-gate 				return (ENXIO);
7190Sstevel@tonic-gate 			}
7200Sstevel@tonic-gate 		}
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 		/*
7230Sstevel@tonic-gate 		 * Use seg_dev segment driver for /dev/mem mapping.
7240Sstevel@tonic-gate 		 */
7250Sstevel@tonic-gate 		dev_a.mapfunc = mmmmap;
7260Sstevel@tonic-gate 		dev_a.dev = dev;
7270Sstevel@tonic-gate 		dev_a.offset = off;
7280Sstevel@tonic-gate 		dev_a.type = (flags & MAP_TYPE);
7290Sstevel@tonic-gate 		dev_a.prot = (uchar_t)prot;
7300Sstevel@tonic-gate 		dev_a.maxprot = (uchar_t)maxprot;
7310Sstevel@tonic-gate 		dev_a.hat_attr = 0;
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 		/*
7340Sstevel@tonic-gate 		 * Make /dev/mem mappings non-consistent since we can't
7350Sstevel@tonic-gate 		 * alias pages that don't have page structs behind them,
7360Sstevel@tonic-gate 		 * such as kernel stack pages. If someone mmap()s a kernel
7370Sstevel@tonic-gate 		 * stack page and if we give him a tte with cv, a line from
7380Sstevel@tonic-gate 		 * that page can get into both pages of the spitfire d$.
7390Sstevel@tonic-gate 		 * But snoop from another processor will only invalidate
7400Sstevel@tonic-gate 		 * the first page. This later caused kernel (xc_attention)
7410Sstevel@tonic-gate 		 * to go into an infinite loop at pil 13 and no interrupts
7420Sstevel@tonic-gate 		 * could come in. See 1203630.
7430Sstevel@tonic-gate 		 *
7440Sstevel@tonic-gate 		 */
7450Sstevel@tonic-gate 		dev_a.hat_flags = HAT_LOAD_NOCONSIST;
7460Sstevel@tonic-gate 		dev_a.devmap_data = NULL;
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 		error = as_map(as, *addrp, len, segdev_create, &dev_a);
7490Sstevel@tonic-gate 		break;
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	case M_ZERO:
7520Sstevel@tonic-gate 		/*
7530Sstevel@tonic-gate 		 * Use seg_vn segment driver for /dev/zero mapping.
7540Sstevel@tonic-gate 		 * Passing in a NULL amp gives us the "cloning" effect.
7550Sstevel@tonic-gate 		 */
7560Sstevel@tonic-gate 		vn_a.vp = NULL;
7570Sstevel@tonic-gate 		vn_a.offset = 0;
7580Sstevel@tonic-gate 		vn_a.type = (flags & MAP_TYPE);
7590Sstevel@tonic-gate 		vn_a.prot = prot;
7600Sstevel@tonic-gate 		vn_a.maxprot = maxprot;
7610Sstevel@tonic-gate 		vn_a.flags = flags & ~MAP_TYPE;
7620Sstevel@tonic-gate 		vn_a.cred = cred;
7630Sstevel@tonic-gate 		vn_a.amp = NULL;
7640Sstevel@tonic-gate 		vn_a.szc = 0;
7650Sstevel@tonic-gate 		vn_a.lgrp_mem_policy_flags = 0;
7660Sstevel@tonic-gate 		error = as_map(as, *addrp, len, segvn_create, &vn_a);
7670Sstevel@tonic-gate 		break;
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	case M_KMEM:
7700Sstevel@tonic-gate 	case M_ALLKMEM:
7710Sstevel@tonic-gate 		/* No longer supported with KPR. */
7720Sstevel@tonic-gate 		error = ENXIO;
7730Sstevel@tonic-gate 		break;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	case M_NULL:
7760Sstevel@tonic-gate 		/*
7770Sstevel@tonic-gate 		 * Use seg_dev segment driver for /dev/null mapping.
7780Sstevel@tonic-gate 		 */
7790Sstevel@tonic-gate 		dev_a.mapfunc = mmmmap;
7800Sstevel@tonic-gate 		dev_a.dev = dev;
7810Sstevel@tonic-gate 		dev_a.offset = off;
7820Sstevel@tonic-gate 		dev_a.type = 0;		/* neither PRIVATE nor SHARED */
7830Sstevel@tonic-gate 		dev_a.prot = dev_a.maxprot = (uchar_t)PROT_NONE;
7840Sstevel@tonic-gate 		dev_a.hat_attr = 0;
7850Sstevel@tonic-gate 		dev_a.hat_flags = 0;
7860Sstevel@tonic-gate 		error = as_map(as, *addrp, len, segdev_create, &dev_a);
7870Sstevel@tonic-gate 		break;
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	default:
7900Sstevel@tonic-gate 		error = ENXIO;
7910Sstevel@tonic-gate 	}
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	as_rangeunlock(as);
7940Sstevel@tonic-gate 	return (error);
7950Sstevel@tonic-gate }
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate static struct cb_ops mm_cb_ops = {
7980Sstevel@tonic-gate 	mmopen,			/* open */
7990Sstevel@tonic-gate 	nulldev,		/* close */
8000Sstevel@tonic-gate 	nodev,			/* strategy */
8010Sstevel@tonic-gate 	nodev,			/* print */
8020Sstevel@tonic-gate 	nodev,			/* dump */
8030Sstevel@tonic-gate 	mmread,			/* read */
8040Sstevel@tonic-gate 	mmwrite,		/* write */
8050Sstevel@tonic-gate 	mmioctl,		/* ioctl */
8060Sstevel@tonic-gate 	nodev,			/* devmap */
8070Sstevel@tonic-gate 	mmmmap,			/* mmap */
8080Sstevel@tonic-gate 	mmsegmap,		/* segmap */
8090Sstevel@tonic-gate 	mmchpoll,		/* poll */
8100Sstevel@tonic-gate 	mmpropop,		/* prop_op */
8110Sstevel@tonic-gate 	0,			/* streamtab  */
8120Sstevel@tonic-gate 	D_NEW | D_MP | D_64BIT | D_U64BIT
8130Sstevel@tonic-gate };
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate static struct dev_ops mm_ops = {
8160Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
8170Sstevel@tonic-gate 	0,			/* refcnt  */
8180Sstevel@tonic-gate 	mm_info,		/* get_dev_info */
8190Sstevel@tonic-gate 	nulldev,		/* identify */
8200Sstevel@tonic-gate 	nulldev,		/* probe */
8210Sstevel@tonic-gate 	mm_attach,		/* attach */
8220Sstevel@tonic-gate 	nodev,			/* detach */
8230Sstevel@tonic-gate 	nodev,			/* reset */
8240Sstevel@tonic-gate 	&mm_cb_ops,		/* driver operations */
8250Sstevel@tonic-gate 	(struct bus_ops *)0	/* bus operations */
8260Sstevel@tonic-gate };
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate static struct modldrv modldrv = {
8290Sstevel@tonic-gate 	&mod_driverops, "memory driver %I%", &mm_ops,
8300Sstevel@tonic-gate };
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate static struct modlinkage modlinkage = {
8330Sstevel@tonic-gate 	MODREV_1, &modldrv, NULL
8340Sstevel@tonic-gate };
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate int
8370Sstevel@tonic-gate _init(void)
8380Sstevel@tonic-gate {
8390Sstevel@tonic-gate 	return (mod_install(&modlinkage));
8400Sstevel@tonic-gate }
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate int
8430Sstevel@tonic-gate _info(struct modinfo *modinfop)
8440Sstevel@tonic-gate {
8450Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
8460Sstevel@tonic-gate }
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate int
8490Sstevel@tonic-gate _fini(void)
8500Sstevel@tonic-gate {
8510Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
8520Sstevel@tonic-gate }
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate static int
8550Sstevel@tonic-gate mm_kstat_update(kstat_t *ksp, int rw)
8560Sstevel@tonic-gate {
8570Sstevel@tonic-gate 	struct memlist *pmem;
8580Sstevel@tonic-gate 	uint_t count;
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
8610Sstevel@tonic-gate 		return (EACCES);
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	count = 0;
8640Sstevel@tonic-gate 	memlist_read_lock();
8650Sstevel@tonic-gate 	for (pmem = phys_install; pmem != NULL; pmem = pmem->next) {
8660Sstevel@tonic-gate 		count++;
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate 	memlist_read_unlock();
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	ksp->ks_ndata = count;
8710Sstevel@tonic-gate 	ksp->ks_data_size = count * 2 * sizeof (uint64_t);
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	return (0);
8740Sstevel@tonic-gate }
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate static int
8770Sstevel@tonic-gate mm_kstat_snapshot(kstat_t *ksp, void *buf, int rw)
8780Sstevel@tonic-gate {
8790Sstevel@tonic-gate 	struct memlist *pmem;
8800Sstevel@tonic-gate 	struct memunit {
8810Sstevel@tonic-gate 		uint64_t address;
8820Sstevel@tonic-gate 		uint64_t size;
8830Sstevel@tonic-gate 	} *kspmem;
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
8860Sstevel@tonic-gate 		return (EACCES);
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	ksp->ks_snaptime = gethrtime();
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	kspmem = (struct memunit *)buf;
8910Sstevel@tonic-gate 	memlist_read_lock();
8920Sstevel@tonic-gate 	for (pmem = phys_install; pmem != NULL; pmem = pmem->next, kspmem++) {
8930Sstevel@tonic-gate 		if ((caddr_t)kspmem >= (caddr_t)buf + ksp->ks_data_size)
8940Sstevel@tonic-gate 			break;
8950Sstevel@tonic-gate 		kspmem->address = pmem->address;
8960Sstevel@tonic-gate 		kspmem->size = pmem->size;
8970Sstevel@tonic-gate 	}
8980Sstevel@tonic-gate 	memlist_read_unlock();
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	return (0);
9010Sstevel@tonic-gate }
902