xref: /onnv-gate/usr/src/uts/common/io/mem.c (revision 917:a3d1e8247fa0)
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 
258670Selowe static int
259670Selowe mmpagelock(struct as *as, caddr_t va)
260670Selowe {
261670Selowe 	struct seg *seg;
262670Selowe 	int i;
263670Selowe 
264670Selowe 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
265670Selowe 	seg = as_segat(as, va);
266670Selowe 	i = (seg != NULL)? SEGOP_CAPABLE(seg, S_CAPABILITY_NOMINFLT) : 0;
267670Selowe 	AS_LOCK_EXIT(as, &as->a_lock);
268670Selowe 
269670Selowe 	return (i);
270670Selowe }
271670Selowe 
272670Selowe #define	NEED_LOCK_KVADDR(kva)	mmpagelock(&kas, kva)
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate #else	/* __i386, __amd64 */
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate #define	NEED_LOCK_KVADDR(va)	0
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate #endif	/* __sparc */
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate /*ARGSUSED3*/
2810Sstevel@tonic-gate static int
2820Sstevel@tonic-gate mmrw(dev_t dev, struct uio *uio, enum uio_rw rw, cred_t *cred)
2830Sstevel@tonic-gate {
2840Sstevel@tonic-gate 	pfn_t v;
2850Sstevel@tonic-gate 	struct iovec *iov;
2860Sstevel@tonic-gate 	int error = 0;
2870Sstevel@tonic-gate 	size_t c;
2880Sstevel@tonic-gate 	ssize_t oresid = uio->uio_resid;
2890Sstevel@tonic-gate 	minor_t minor = getminor(dev);
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	while (uio->uio_resid > 0 && error == 0) {
2920Sstevel@tonic-gate 		iov = uio->uio_iov;
2930Sstevel@tonic-gate 		if (iov->iov_len == 0) {
2940Sstevel@tonic-gate 			uio->uio_iov++;
2950Sstevel@tonic-gate 			uio->uio_iovcnt--;
2960Sstevel@tonic-gate 			if (uio->uio_iovcnt < 0)
2970Sstevel@tonic-gate 				panic("mmrw");
2980Sstevel@tonic-gate 			continue;
2990Sstevel@tonic-gate 		}
3000Sstevel@tonic-gate 		switch (minor) {
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 		case M_MEM:
3030Sstevel@tonic-gate 			memlist_read_lock();
3040Sstevel@tonic-gate 			if (!address_in_memlist(phys_install,
3050Sstevel@tonic-gate 			    (uint64_t)uio->uio_loffset, 1)) {
3060Sstevel@tonic-gate 				memlist_read_unlock();
3070Sstevel@tonic-gate 				error = EFAULT;
3080Sstevel@tonic-gate 				break;
3090Sstevel@tonic-gate 			}
3100Sstevel@tonic-gate 			memlist_read_unlock();
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 			v = BTOP((u_offset_t)uio->uio_loffset);
3130Sstevel@tonic-gate 			error = mmio(uio, rw, v,
3140Sstevel@tonic-gate 			    uio->uio_loffset & PAGEOFFSET, 0);
3150Sstevel@tonic-gate 			break;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 		case M_KMEM:
3180Sstevel@tonic-gate 		case M_ALLKMEM:
3190Sstevel@tonic-gate 			{
3200Sstevel@tonic-gate 			page_t **ppp;
3210Sstevel@tonic-gate 			caddr_t vaddr = (caddr_t)uio->uio_offset;
3220Sstevel@tonic-gate 			int try_lock = NEED_LOCK_KVADDR(vaddr);
3230Sstevel@tonic-gate 			int locked = 0;
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 			/*
3260Sstevel@tonic-gate 			 * If vaddr does not map a valid page, as_pagelock()
3270Sstevel@tonic-gate 			 * will return failure. Hence we can't check the
3280Sstevel@tonic-gate 			 * return value and return EFAULT here as we'd like.
3290Sstevel@tonic-gate 			 * seg_kp and seg_kpm do not properly support
3300Sstevel@tonic-gate 			 * as_pagelock() for this context so we avoid it
3310Sstevel@tonic-gate 			 * using the try_lock set check above.  Some day when
3320Sstevel@tonic-gate 			 * the kernel page locking gets redesigned all this
3330Sstevel@tonic-gate 			 * muck can be cleaned up.
3340Sstevel@tonic-gate 			 */
3350Sstevel@tonic-gate 			if (try_lock)
3360Sstevel@tonic-gate 				locked = (as_pagelock(&kas, &ppp, vaddr,
3370Sstevel@tonic-gate 				    PAGESIZE, S_WRITE) == 0);
3380Sstevel@tonic-gate 
339513Sjongkis 			v = hat_getpfnum(kas.a_hat,
340513Sjongkis 			    (caddr_t)(uintptr_t)uio->uio_loffset);
3410Sstevel@tonic-gate 			if (v == PFN_INVALID) {
3420Sstevel@tonic-gate 				if (locked)
3430Sstevel@tonic-gate 					as_pageunlock(&kas, ppp, vaddr,
3440Sstevel@tonic-gate 					    PAGESIZE, S_WRITE);
3450Sstevel@tonic-gate 				error = EFAULT;
3460Sstevel@tonic-gate 				break;
3470Sstevel@tonic-gate 			}
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 			error = mmio(uio, rw, v, uio->uio_loffset & PAGEOFFSET,
3500Sstevel@tonic-gate 			    minor == M_ALLKMEM || mm_kmem_io_access);
3510Sstevel@tonic-gate 			if (locked)
3520Sstevel@tonic-gate 				as_pageunlock(&kas, ppp, vaddr, PAGESIZE,
3530Sstevel@tonic-gate 				    S_WRITE);
3540Sstevel@tonic-gate 			}
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 			break;
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 		case M_ZERO:
3590Sstevel@tonic-gate 			if (rw == UIO_READ) {
3600Sstevel@tonic-gate 				label_t ljb;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 				if (on_fault(&ljb)) {
3630Sstevel@tonic-gate 					no_fault();
3640Sstevel@tonic-gate 					error = EFAULT;
3650Sstevel@tonic-gate 					break;
3660Sstevel@tonic-gate 				}
3670Sstevel@tonic-gate 				uzero(iov->iov_base, iov->iov_len);
3680Sstevel@tonic-gate 				no_fault();
3690Sstevel@tonic-gate 				uio->uio_resid -= iov->iov_len;
3700Sstevel@tonic-gate 				uio->uio_loffset += iov->iov_len;
3710Sstevel@tonic-gate 				break;
3720Sstevel@tonic-gate 			}
3730Sstevel@tonic-gate 			/* else it's a write, fall through to NULL case */
3740Sstevel@tonic-gate 			/*FALLTHROUGH*/
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 		case M_NULL:
3770Sstevel@tonic-gate 			if (rw == UIO_READ)
3780Sstevel@tonic-gate 				return (0);
3790Sstevel@tonic-gate 			c = iov->iov_len;
3800Sstevel@tonic-gate 			iov->iov_base += c;
3810Sstevel@tonic-gate 			iov->iov_len -= c;
3820Sstevel@tonic-gate 			uio->uio_loffset += c;
3830Sstevel@tonic-gate 			uio->uio_resid -= c;
3840Sstevel@tonic-gate 			break;
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 		}
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate 	return (uio->uio_resid == oresid ? error : 0);
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate static int
3920Sstevel@tonic-gate mmread(dev_t dev, struct uio *uio, cred_t *cred)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	return (mmrw(dev, uio, UIO_READ, cred));
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate static int
3980Sstevel@tonic-gate mmwrite(dev_t dev, struct uio *uio, cred_t *cred)
3990Sstevel@tonic-gate {
4000Sstevel@tonic-gate 	return (mmrw(dev, uio, UIO_WRITE, cred));
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate /*
4040Sstevel@tonic-gate  * Private ioctl for libkvm to support kvm_physaddr().
4050Sstevel@tonic-gate  * Given an address space and a VA, compute the PA.
4060Sstevel@tonic-gate  */
4070Sstevel@tonic-gate static int
4080Sstevel@tonic-gate mmioctl_vtop(intptr_t data)
4090Sstevel@tonic-gate {
4100Sstevel@tonic-gate 	mem_vtop_t mem_vtop;
4110Sstevel@tonic-gate 	proc_t *p;
4120Sstevel@tonic-gate 	pfn_t pfn = (pfn_t)PFN_INVALID;
4130Sstevel@tonic-gate 	pid_t pid = 0;
4140Sstevel@tonic-gate 	struct as *as;
4150Sstevel@tonic-gate 	struct seg *seg;
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	if (copyin((void *)data, &mem_vtop, sizeof (mem_vtop_t)))
4180Sstevel@tonic-gate 		return (EFAULT);
4190Sstevel@tonic-gate 	if (mem_vtop.m_as == &kas) {
4200Sstevel@tonic-gate 		pfn = hat_getpfnum(kas.a_hat, mem_vtop.m_va);
4210Sstevel@tonic-gate 	} else if (mem_vtop.m_as == NULL) {
4220Sstevel@tonic-gate 		return (EIO);
4230Sstevel@tonic-gate 	} else {
4240Sstevel@tonic-gate 		mutex_enter(&pidlock);
4250Sstevel@tonic-gate 		for (p = practive; p != NULL; p = p->p_next) {
4260Sstevel@tonic-gate 			if (p->p_as == mem_vtop.m_as) {
4270Sstevel@tonic-gate 				pid = p->p_pid;
4280Sstevel@tonic-gate 				break;
4290Sstevel@tonic-gate 			}
4300Sstevel@tonic-gate 		}
4310Sstevel@tonic-gate 		mutex_exit(&pidlock);
4320Sstevel@tonic-gate 		if (p == NULL)
4330Sstevel@tonic-gate 			return (EIO);
4340Sstevel@tonic-gate 		p = sprlock(pid);
4350Sstevel@tonic-gate 		if (p == NULL)
4360Sstevel@tonic-gate 			return (EIO);
4370Sstevel@tonic-gate 		as = p->p_as;
4380Sstevel@tonic-gate 		if (as == mem_vtop.m_as) {
4390Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
4400Sstevel@tonic-gate 			AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
4410Sstevel@tonic-gate 			for (seg = AS_SEGFIRST(as); seg != NULL;
4420Sstevel@tonic-gate 			    seg = AS_SEGNEXT(as, seg))
4430Sstevel@tonic-gate 				if ((uintptr_t)mem_vtop.m_va -
4440Sstevel@tonic-gate 				    (uintptr_t)seg->s_base < seg->s_size)
4450Sstevel@tonic-gate 					break;
4460Sstevel@tonic-gate 			if (seg != NULL)
4470Sstevel@tonic-gate 				pfn = hat_getpfnum(as->a_hat, mem_vtop.m_va);
4480Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
4490Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
4500Sstevel@tonic-gate 		}
4510Sstevel@tonic-gate 		sprunlock(p);
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate 	mem_vtop.m_pfn = pfn;
4540Sstevel@tonic-gate 	if (pfn == PFN_INVALID)
4550Sstevel@tonic-gate 		return (EIO);
4560Sstevel@tonic-gate 	if (copyout(&mem_vtop, (void *)data, sizeof (mem_vtop_t)))
4570Sstevel@tonic-gate 		return (EFAULT);
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	return (0);
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate /*
463*917Selowe  * Given a PA, execute the given page retire command on it.
4640Sstevel@tonic-gate  */
4650Sstevel@tonic-gate static int
4660Sstevel@tonic-gate mmioctl_page_retire(int cmd, intptr_t data)
4670Sstevel@tonic-gate {
468*917Selowe 	extern int page_retire_test(void);
4690Sstevel@tonic-gate 	uint64_t pa;
4700Sstevel@tonic-gate 
471*917Selowe 	if (copyin((void *)data, &pa, sizeof (uint64_t))) {
472*917Selowe 		return (EFAULT);
4730Sstevel@tonic-gate 	}
4740Sstevel@tonic-gate 
475*917Selowe 	switch (cmd) {
476*917Selowe 	case MEM_PAGE_ISRETIRED:
477*917Selowe 		return (page_retire_check(pa, NULL));
478*917Selowe 
479*917Selowe 	case MEM_PAGE_UNRETIRE:
480*917Selowe 		return (page_unretire(pa));
481*917Selowe 
482*917Selowe 	case MEM_PAGE_RETIRE:
483*917Selowe 		return (page_retire(pa, PR_FMA));
484*917Selowe 
485*917Selowe 	case MEM_PAGE_RETIRE_MCE:
486*917Selowe 		return (page_retire(pa, PR_MCE));
487*917Selowe 
488*917Selowe 	case MEM_PAGE_RETIRE_UE:
489*917Selowe 		return (page_retire(pa, PR_UE));
4900Sstevel@tonic-gate 
491*917Selowe 	case MEM_PAGE_GETERRORS:
492*917Selowe 		{
493*917Selowe 			uint64_t page_errors;
494*917Selowe 			int rc = page_retire_check(pa, &page_errors);
495*917Selowe 			if (copyout(&page_errors, (void *)data,
496*917Selowe 			    sizeof (uint64_t))) {
497*917Selowe 				return (EFAULT);
498*917Selowe 			}
499*917Selowe 			return (rc);
500*917Selowe 		}
501*917Selowe 
502*917Selowe 	case MEM_PAGE_RETIRE_TEST:
503*917Selowe 		return (page_retire_test());
504*917Selowe 
505*917Selowe 	}
506*917Selowe 
507*917Selowe 	return (EINVAL);
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate #ifdef __sparc
5110Sstevel@tonic-gate /*
5120Sstevel@tonic-gate  * Given a syndrome, syndrome type, and address return the
5130Sstevel@tonic-gate  * associated memory name in the provided data buffer.
5140Sstevel@tonic-gate  */
5150Sstevel@tonic-gate static int
5160Sstevel@tonic-gate mmioctl_get_mem_name(intptr_t data)
5170Sstevel@tonic-gate {
5180Sstevel@tonic-gate 	mem_name_t mem_name;
5190Sstevel@tonic-gate #ifdef	_SYSCALL32
5200Sstevel@tonic-gate 	mem_name32_t mem_name32;
5210Sstevel@tonic-gate #endif
5220Sstevel@tonic-gate 	void *buf;
5230Sstevel@tonic-gate 	size_t bufsize;
5240Sstevel@tonic-gate 	int len, err;
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	if ((bufsize = cpu_get_name_bufsize()) == 0)
5270Sstevel@tonic-gate 		return (ENOTSUP);
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
5300Sstevel@tonic-gate 		if (copyin((void *)data, &mem_name, sizeof (mem_name_t)))
5310Sstevel@tonic-gate 			return (EFAULT);
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate #ifdef	_SYSCALL32
5340Sstevel@tonic-gate 	else {
5350Sstevel@tonic-gate 		if (copyin((void *)data, &mem_name32, sizeof (mem_name32_t)))
5360Sstevel@tonic-gate 			return (EFAULT);
5370Sstevel@tonic-gate 		mem_name.m_addr = mem_name32.m_addr;
5380Sstevel@tonic-gate 		mem_name.m_synd = mem_name32.m_synd;
5390Sstevel@tonic-gate 		mem_name.m_type[0] = mem_name32.m_type[0];
5400Sstevel@tonic-gate 		mem_name.m_type[1] = mem_name32.m_type[1];
541513Sjongkis 		mem_name.m_name = (caddr_t)(uintptr_t)mem_name32.m_name;
5420Sstevel@tonic-gate 		mem_name.m_namelen = (size_t)mem_name32.m_namelen;
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate #endif	/* _SYSCALL32 */
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	buf = kmem_alloc(bufsize, KM_SLEEP);
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	/*
5490Sstevel@tonic-gate 	 * Call into cpu specific code to do the lookup.
5500Sstevel@tonic-gate 	 */
5510Sstevel@tonic-gate 	if ((err = cpu_get_mem_name(mem_name.m_synd, mem_name.m_type,
5520Sstevel@tonic-gate 	    mem_name.m_addr, buf, bufsize, &len)) != 0) {
5530Sstevel@tonic-gate 		kmem_free(buf, bufsize);
5540Sstevel@tonic-gate 		return (err);
5550Sstevel@tonic-gate 	}
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	if (len >= mem_name.m_namelen) {
5580Sstevel@tonic-gate 		kmem_free(buf, bufsize);
5590Sstevel@tonic-gate 		return (ENAMETOOLONG);
5600Sstevel@tonic-gate 	}
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	if (copyoutstr(buf, (char *)mem_name.m_name,
5630Sstevel@tonic-gate 	    mem_name.m_namelen, NULL) != 0) {
5640Sstevel@tonic-gate 		kmem_free(buf, bufsize);
5650Sstevel@tonic-gate 		return (EFAULT);
5660Sstevel@tonic-gate 	}
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	kmem_free(buf, bufsize);
5690Sstevel@tonic-gate 	return (0);
5700Sstevel@tonic-gate }
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate /*
5730Sstevel@tonic-gate  * Given a syndrome and address return information about the associated memory.
5740Sstevel@tonic-gate  */
5750Sstevel@tonic-gate static int
5760Sstevel@tonic-gate mmioctl_get_mem_info(intptr_t data)
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate 	mem_info_t mem_info;
5790Sstevel@tonic-gate 	int err;
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	if (copyin((void *)data, &mem_info, sizeof (mem_info_t)))
5820Sstevel@tonic-gate 		return (EFAULT);
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	if ((err = cpu_get_mem_info(mem_info.m_synd, mem_info.m_addr,
5850Sstevel@tonic-gate 	    &mem_info.m_mem_size, &mem_info.m_seg_size, &mem_info.m_bank_size,
5860Sstevel@tonic-gate 	    &mem_info.m_segments, &mem_info.m_banks, &mem_info.m_mcid)) != 0)
5870Sstevel@tonic-gate 		return (err);
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	if (copyout(&mem_info, (void *)data, sizeof (mem_info_t)) != 0)
5900Sstevel@tonic-gate 		return (EFAULT);
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	return (0);
5930Sstevel@tonic-gate }
5940Sstevel@tonic-gate #endif	/* __sparc */
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate /*
5970Sstevel@tonic-gate  * Private ioctls for
5980Sstevel@tonic-gate  *	libkvm to support kvm_physaddr().
5990Sstevel@tonic-gate  *	FMA support for page_retire() and memory attribute information.
6000Sstevel@tonic-gate  */
6010Sstevel@tonic-gate /*ARGSUSED*/
6020Sstevel@tonic-gate static int
6030Sstevel@tonic-gate mmioctl(dev_t dev, int cmd, intptr_t data, int flag, cred_t *cred, int *rvalp)
6040Sstevel@tonic-gate {
6050Sstevel@tonic-gate 	switch (cmd) {
6060Sstevel@tonic-gate 	case MEM_VTOP:
6070Sstevel@tonic-gate 		if (getminor(dev) != M_KMEM)
6080Sstevel@tonic-gate 			return (ENXIO);
6090Sstevel@tonic-gate 		return (mmioctl_vtop(data));
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	case MEM_PAGE_RETIRE:
6120Sstevel@tonic-gate 	case MEM_PAGE_ISRETIRED:
613*917Selowe 	case MEM_PAGE_UNRETIRE:
614*917Selowe 	case MEM_PAGE_RETIRE_MCE:
615*917Selowe 	case MEM_PAGE_RETIRE_UE:
616*917Selowe 	case MEM_PAGE_GETERRORS:
617*917Selowe 	case MEM_PAGE_RETIRE_TEST:
6180Sstevel@tonic-gate 		if (getminor(dev) != M_MEM)
6190Sstevel@tonic-gate 			return (ENXIO);
6200Sstevel@tonic-gate 		return (mmioctl_page_retire(cmd, data));
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	case MEM_NAME:
6230Sstevel@tonic-gate 		if (getminor(dev) != M_MEM)
6240Sstevel@tonic-gate 			return (ENXIO);
6250Sstevel@tonic-gate #ifdef __sparc
6260Sstevel@tonic-gate 		return (mmioctl_get_mem_name(data));
6270Sstevel@tonic-gate #else
6280Sstevel@tonic-gate 		return (ENOTSUP);
6290Sstevel@tonic-gate #endif
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	case MEM_INFO:
6320Sstevel@tonic-gate 		if (getminor(dev) != M_MEM)
6330Sstevel@tonic-gate 			return (ENXIO);
6340Sstevel@tonic-gate #ifdef __sparc
6350Sstevel@tonic-gate 		return (mmioctl_get_mem_info(data));
6360Sstevel@tonic-gate #else
6370Sstevel@tonic-gate 		return (ENOTSUP);
6380Sstevel@tonic-gate #endif
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate 	return (ENXIO);
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate /*ARGSUSED2*/
6440Sstevel@tonic-gate static int
6450Sstevel@tonic-gate mmmmap(dev_t dev, off_t off, int prot)
6460Sstevel@tonic-gate {
6470Sstevel@tonic-gate 	pfn_t pf;
6480Sstevel@tonic-gate 	struct memlist *pmem;
6490Sstevel@tonic-gate 	minor_t minor = getminor(dev);
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	switch (minor) {
6520Sstevel@tonic-gate 	case M_MEM:
6530Sstevel@tonic-gate 		pf = btop(off);
6540Sstevel@tonic-gate 		memlist_read_lock();
6550Sstevel@tonic-gate 		for (pmem = phys_install; pmem != NULL; pmem = pmem->next) {
6560Sstevel@tonic-gate 			if (pf >= BTOP(pmem->address) &&
6570Sstevel@tonic-gate 			    pf < BTOP(pmem->address + pmem->size)) {
6580Sstevel@tonic-gate 				memlist_read_unlock();
6590Sstevel@tonic-gate 				return (impl_obmem_pfnum(pf));
6600Sstevel@tonic-gate 			}
6610Sstevel@tonic-gate 		}
6620Sstevel@tonic-gate 		memlist_read_unlock();
6630Sstevel@tonic-gate 		break;
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	case M_KMEM:
6660Sstevel@tonic-gate 	case M_ALLKMEM:
6670Sstevel@tonic-gate 		/* no longer supported with KPR */
6680Sstevel@tonic-gate 		return (-1);
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	case M_ZERO:
6710Sstevel@tonic-gate 		/*
6720Sstevel@tonic-gate 		 * We shouldn't be mmap'ing to /dev/zero here as
6730Sstevel@tonic-gate 		 * mmsegmap() should have already converted
6740Sstevel@tonic-gate 		 * a mapping request for this device to a mapping
6750Sstevel@tonic-gate 		 * using seg_vn for anonymous memory.
6760Sstevel@tonic-gate 		 */
6770Sstevel@tonic-gate 		break;
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	}
6800Sstevel@tonic-gate 	return (-1);
6810Sstevel@tonic-gate }
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate /*
6840Sstevel@tonic-gate  * This function is called when a memory device is mmap'ed.
6850Sstevel@tonic-gate  * Set up the mapping to the correct device driver.
6860Sstevel@tonic-gate  */
6870Sstevel@tonic-gate static int
6880Sstevel@tonic-gate mmsegmap(dev_t dev, off_t off, struct as *as, caddr_t *addrp, off_t len,
6890Sstevel@tonic-gate     uint_t prot, uint_t maxprot, uint_t flags, struct cred *cred)
6900Sstevel@tonic-gate {
6910Sstevel@tonic-gate 	struct segvn_crargs vn_a;
6920Sstevel@tonic-gate 	struct segdev_crargs dev_a;
6930Sstevel@tonic-gate 	int error;
6940Sstevel@tonic-gate 	minor_t minor;
6950Sstevel@tonic-gate 	off_t i;
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	minor = getminor(dev);
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	as_rangelock(as);
7000Sstevel@tonic-gate 	if ((flags & MAP_FIXED) == 0) {
7010Sstevel@tonic-gate 		/*
7020Sstevel@tonic-gate 		 * No need to worry about vac alignment on /dev/zero
7030Sstevel@tonic-gate 		 * since this is a "clone" object that doesn't yet exist.
7040Sstevel@tonic-gate 		 */
7050Sstevel@tonic-gate 		map_addr(addrp, len, (offset_t)off,
7060Sstevel@tonic-gate 				(minor == M_MEM) || (minor == M_KMEM), flags);
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 		if (*addrp == NULL) {
7090Sstevel@tonic-gate 			as_rangeunlock(as);
7100Sstevel@tonic-gate 			return (ENOMEM);
7110Sstevel@tonic-gate 		}
7120Sstevel@tonic-gate 	} else {
7130Sstevel@tonic-gate 		/*
7140Sstevel@tonic-gate 		 * User specified address -
7150Sstevel@tonic-gate 		 * Blow away any previous mappings.
7160Sstevel@tonic-gate 		 */
7170Sstevel@tonic-gate 		(void) as_unmap(as, *addrp, len);
7180Sstevel@tonic-gate 	}
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	switch (minor) {
7210Sstevel@tonic-gate 	case M_MEM:
7220Sstevel@tonic-gate 		/* /dev/mem cannot be mmap'ed with MAP_PRIVATE */
7230Sstevel@tonic-gate 		if ((flags & MAP_TYPE) != MAP_SHARED) {
7240Sstevel@tonic-gate 			as_rangeunlock(as);
7250Sstevel@tonic-gate 			return (EINVAL);
7260Sstevel@tonic-gate 		}
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 		/*
7290Sstevel@tonic-gate 		 * Check to ensure that the entire range is
7300Sstevel@tonic-gate 		 * legal and we are not trying to map in
7310Sstevel@tonic-gate 		 * more than the device will let us.
7320Sstevel@tonic-gate 		 */
7330Sstevel@tonic-gate 		for (i = 0; i < len; i += PAGESIZE) {
7340Sstevel@tonic-gate 			if (mmmmap(dev, off + i, maxprot) == -1) {
7350Sstevel@tonic-gate 				as_rangeunlock(as);
7360Sstevel@tonic-gate 				return (ENXIO);
7370Sstevel@tonic-gate 			}
7380Sstevel@tonic-gate 		}
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 		/*
7410Sstevel@tonic-gate 		 * Use seg_dev segment driver for /dev/mem mapping.
7420Sstevel@tonic-gate 		 */
7430Sstevel@tonic-gate 		dev_a.mapfunc = mmmmap;
7440Sstevel@tonic-gate 		dev_a.dev = dev;
7450Sstevel@tonic-gate 		dev_a.offset = off;
7460Sstevel@tonic-gate 		dev_a.type = (flags & MAP_TYPE);
7470Sstevel@tonic-gate 		dev_a.prot = (uchar_t)prot;
7480Sstevel@tonic-gate 		dev_a.maxprot = (uchar_t)maxprot;
7490Sstevel@tonic-gate 		dev_a.hat_attr = 0;
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 		/*
7520Sstevel@tonic-gate 		 * Make /dev/mem mappings non-consistent since we can't
7530Sstevel@tonic-gate 		 * alias pages that don't have page structs behind them,
7540Sstevel@tonic-gate 		 * such as kernel stack pages. If someone mmap()s a kernel
7550Sstevel@tonic-gate 		 * stack page and if we give him a tte with cv, a line from
7560Sstevel@tonic-gate 		 * that page can get into both pages of the spitfire d$.
7570Sstevel@tonic-gate 		 * But snoop from another processor will only invalidate
7580Sstevel@tonic-gate 		 * the first page. This later caused kernel (xc_attention)
7590Sstevel@tonic-gate 		 * to go into an infinite loop at pil 13 and no interrupts
7600Sstevel@tonic-gate 		 * could come in. See 1203630.
7610Sstevel@tonic-gate 		 *
7620Sstevel@tonic-gate 		 */
7630Sstevel@tonic-gate 		dev_a.hat_flags = HAT_LOAD_NOCONSIST;
7640Sstevel@tonic-gate 		dev_a.devmap_data = NULL;
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 		error = as_map(as, *addrp, len, segdev_create, &dev_a);
7670Sstevel@tonic-gate 		break;
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	case M_ZERO:
7700Sstevel@tonic-gate 		/*
7710Sstevel@tonic-gate 		 * Use seg_vn segment driver for /dev/zero mapping.
7720Sstevel@tonic-gate 		 * Passing in a NULL amp gives us the "cloning" effect.
7730Sstevel@tonic-gate 		 */
7740Sstevel@tonic-gate 		vn_a.vp = NULL;
7750Sstevel@tonic-gate 		vn_a.offset = 0;
7760Sstevel@tonic-gate 		vn_a.type = (flags & MAP_TYPE);
7770Sstevel@tonic-gate 		vn_a.prot = prot;
7780Sstevel@tonic-gate 		vn_a.maxprot = maxprot;
7790Sstevel@tonic-gate 		vn_a.flags = flags & ~MAP_TYPE;
7800Sstevel@tonic-gate 		vn_a.cred = cred;
7810Sstevel@tonic-gate 		vn_a.amp = NULL;
7820Sstevel@tonic-gate 		vn_a.szc = 0;
7830Sstevel@tonic-gate 		vn_a.lgrp_mem_policy_flags = 0;
7840Sstevel@tonic-gate 		error = as_map(as, *addrp, len, segvn_create, &vn_a);
7850Sstevel@tonic-gate 		break;
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	case M_KMEM:
7880Sstevel@tonic-gate 	case M_ALLKMEM:
7890Sstevel@tonic-gate 		/* No longer supported with KPR. */
7900Sstevel@tonic-gate 		error = ENXIO;
7910Sstevel@tonic-gate 		break;
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	case M_NULL:
7940Sstevel@tonic-gate 		/*
7950Sstevel@tonic-gate 		 * Use seg_dev segment driver for /dev/null mapping.
7960Sstevel@tonic-gate 		 */
7970Sstevel@tonic-gate 		dev_a.mapfunc = mmmmap;
7980Sstevel@tonic-gate 		dev_a.dev = dev;
7990Sstevel@tonic-gate 		dev_a.offset = off;
8000Sstevel@tonic-gate 		dev_a.type = 0;		/* neither PRIVATE nor SHARED */
8010Sstevel@tonic-gate 		dev_a.prot = dev_a.maxprot = (uchar_t)PROT_NONE;
8020Sstevel@tonic-gate 		dev_a.hat_attr = 0;
8030Sstevel@tonic-gate 		dev_a.hat_flags = 0;
8040Sstevel@tonic-gate 		error = as_map(as, *addrp, len, segdev_create, &dev_a);
8050Sstevel@tonic-gate 		break;
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	default:
8080Sstevel@tonic-gate 		error = ENXIO;
8090Sstevel@tonic-gate 	}
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	as_rangeunlock(as);
8120Sstevel@tonic-gate 	return (error);
8130Sstevel@tonic-gate }
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate static struct cb_ops mm_cb_ops = {
8160Sstevel@tonic-gate 	mmopen,			/* open */
8170Sstevel@tonic-gate 	nulldev,		/* close */
8180Sstevel@tonic-gate 	nodev,			/* strategy */
8190Sstevel@tonic-gate 	nodev,			/* print */
8200Sstevel@tonic-gate 	nodev,			/* dump */
8210Sstevel@tonic-gate 	mmread,			/* read */
8220Sstevel@tonic-gate 	mmwrite,		/* write */
8230Sstevel@tonic-gate 	mmioctl,		/* ioctl */
8240Sstevel@tonic-gate 	nodev,			/* devmap */
8250Sstevel@tonic-gate 	mmmmap,			/* mmap */
8260Sstevel@tonic-gate 	mmsegmap,		/* segmap */
8270Sstevel@tonic-gate 	mmchpoll,		/* poll */
8280Sstevel@tonic-gate 	mmpropop,		/* prop_op */
8290Sstevel@tonic-gate 	0,			/* streamtab  */
8300Sstevel@tonic-gate 	D_NEW | D_MP | D_64BIT | D_U64BIT
8310Sstevel@tonic-gate };
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate static struct dev_ops mm_ops = {
8340Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
8350Sstevel@tonic-gate 	0,			/* refcnt  */
8360Sstevel@tonic-gate 	mm_info,		/* get_dev_info */
8370Sstevel@tonic-gate 	nulldev,		/* identify */
8380Sstevel@tonic-gate 	nulldev,		/* probe */
8390Sstevel@tonic-gate 	mm_attach,		/* attach */
8400Sstevel@tonic-gate 	nodev,			/* detach */
8410Sstevel@tonic-gate 	nodev,			/* reset */
8420Sstevel@tonic-gate 	&mm_cb_ops,		/* driver operations */
8430Sstevel@tonic-gate 	(struct bus_ops *)0	/* bus operations */
8440Sstevel@tonic-gate };
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate static struct modldrv modldrv = {
8470Sstevel@tonic-gate 	&mod_driverops, "memory driver %I%", &mm_ops,
8480Sstevel@tonic-gate };
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate static struct modlinkage modlinkage = {
8510Sstevel@tonic-gate 	MODREV_1, &modldrv, NULL
8520Sstevel@tonic-gate };
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate int
8550Sstevel@tonic-gate _init(void)
8560Sstevel@tonic-gate {
8570Sstevel@tonic-gate 	return (mod_install(&modlinkage));
8580Sstevel@tonic-gate }
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate int
8610Sstevel@tonic-gate _info(struct modinfo *modinfop)
8620Sstevel@tonic-gate {
8630Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
8640Sstevel@tonic-gate }
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate int
8670Sstevel@tonic-gate _fini(void)
8680Sstevel@tonic-gate {
8690Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
8700Sstevel@tonic-gate }
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate static int
8730Sstevel@tonic-gate mm_kstat_update(kstat_t *ksp, int rw)
8740Sstevel@tonic-gate {
8750Sstevel@tonic-gate 	struct memlist *pmem;
8760Sstevel@tonic-gate 	uint_t count;
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
8790Sstevel@tonic-gate 		return (EACCES);
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	count = 0;
8820Sstevel@tonic-gate 	memlist_read_lock();
8830Sstevel@tonic-gate 	for (pmem = phys_install; pmem != NULL; pmem = pmem->next) {
8840Sstevel@tonic-gate 		count++;
8850Sstevel@tonic-gate 	}
8860Sstevel@tonic-gate 	memlist_read_unlock();
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	ksp->ks_ndata = count;
8890Sstevel@tonic-gate 	ksp->ks_data_size = count * 2 * sizeof (uint64_t);
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	return (0);
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate static int
8950Sstevel@tonic-gate mm_kstat_snapshot(kstat_t *ksp, void *buf, int rw)
8960Sstevel@tonic-gate {
8970Sstevel@tonic-gate 	struct memlist *pmem;
8980Sstevel@tonic-gate 	struct memunit {
8990Sstevel@tonic-gate 		uint64_t address;
9000Sstevel@tonic-gate 		uint64_t size;
9010Sstevel@tonic-gate 	} *kspmem;
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
9040Sstevel@tonic-gate 		return (EACCES);
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	ksp->ks_snaptime = gethrtime();
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	kspmem = (struct memunit *)buf;
9090Sstevel@tonic-gate 	memlist_read_lock();
9100Sstevel@tonic-gate 	for (pmem = phys_install; pmem != NULL; pmem = pmem->next, kspmem++) {
9110Sstevel@tonic-gate 		if ((caddr_t)kspmem >= (caddr_t)buf + ksp->ks_data_size)
9120Sstevel@tonic-gate 			break;
9130Sstevel@tonic-gate 		kspmem->address = pmem->address;
9140Sstevel@tonic-gate 		kspmem->size = pmem->size;
9150Sstevel@tonic-gate 	}
9160Sstevel@tonic-gate 	memlist_read_unlock();
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	return (0);
9190Sstevel@tonic-gate }
920