xref: /onnv-gate/usr/src/uts/common/io/mem.c (revision 1189:5d0c54864c1c)
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>
691186Sayznaga #include <sys/fm/protocol.h>
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #ifdef __sparc
720Sstevel@tonic-gate extern int cpu_get_mem_name(uint64_t, uint64_t *, uint64_t, char *, int, int *);
730Sstevel@tonic-gate extern int cpu_get_mem_info(uint64_t, uint64_t, uint64_t *, uint64_t *,
740Sstevel@tonic-gate     uint64_t *, int *, int *, int *);
750Sstevel@tonic-gate extern size_t cpu_get_name_bufsize(void);
761186Sayznaga extern int cpu_get_mem_sid(char *, char *, int, int *);
771186Sayznaga extern int cpu_get_mem_addr(char *, char *, uint64_t, uint64_t *);
781186Sayznaga #endif	/* __sparc */
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate  * Turn a byte length into a pagecount.  The DDI btop takes a
820Sstevel@tonic-gate  * 32-bit size on 32-bit machines, this handles 64-bit sizes for
830Sstevel@tonic-gate  * large physical-memory 32-bit machines.
840Sstevel@tonic-gate  */
850Sstevel@tonic-gate #define	BTOP(x)	((pgcnt_t)((x) >> _pageshift))
860Sstevel@tonic-gate 
870Sstevel@tonic-gate static kmutex_t mm_lock;
880Sstevel@tonic-gate static caddr_t mm_map;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate static dev_info_t *mm_dip;	/* private copy of devinfo pointer */
910Sstevel@tonic-gate 
920Sstevel@tonic-gate static int mm_kmem_io_access;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate static int mm_kstat_update(kstat_t *ksp, int rw);
950Sstevel@tonic-gate static int mm_kstat_snapshot(kstat_t *ksp, void *buf, int rw);
960Sstevel@tonic-gate 
971186Sayznaga static int mm_read_mem_name(intptr_t data, mem_name_t *mem_name);
981186Sayznaga static int mm_read_mem_page(intptr_t data, mem_page_t *mpage);
991186Sayznaga static int mm_get_mem_fmri(mem_page_t *mpage, nvlist_t **nvl);
1001186Sayznaga static int mm_get_paddr(nvlist_t *nvl, uint64_t *paddr);
1011186Sayznaga 
1020Sstevel@tonic-gate /*ARGSUSED1*/
1030Sstevel@tonic-gate static int
1040Sstevel@tonic-gate mm_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate 	int i;
1070Sstevel@tonic-gate 	struct mem_minor {
1080Sstevel@tonic-gate 		char *name;
1090Sstevel@tonic-gate 		minor_t minor;
1100Sstevel@tonic-gate 		int privonly;
1110Sstevel@tonic-gate 		const char *rdpriv;
1120Sstevel@tonic-gate 		const char *wrpriv;
1130Sstevel@tonic-gate 		mode_t priv_mode;
1140Sstevel@tonic-gate 	} mm[] = {
1150Sstevel@tonic-gate 		{ "mem",	M_MEM,		0,	NULL,	"all",	0640 },
1160Sstevel@tonic-gate 		{ "kmem",	M_KMEM,		0,	NULL,	"all",	0640 },
1170Sstevel@tonic-gate 		{ "allkmem",	M_ALLKMEM,	0,	"all",	"all",	0600 },
1180Sstevel@tonic-gate 		{ "null",	M_NULL,	PRIVONLY_DEV,	NULL,	NULL,	0666 },
1190Sstevel@tonic-gate 		{ "zero",	M_ZERO, PRIVONLY_DEV,	NULL,	NULL,	0666 },
1200Sstevel@tonic-gate 	};
1210Sstevel@tonic-gate 	kstat_t *ksp;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	mutex_init(&mm_lock, NULL, MUTEX_DEFAULT, NULL);
1240Sstevel@tonic-gate 	mm_map = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	for (i = 0; i < (sizeof (mm) / sizeof (mm[0])); i++) {
1270Sstevel@tonic-gate 		if (ddi_create_priv_minor_node(devi, mm[i].name, S_IFCHR,
1280Sstevel@tonic-gate 		    mm[i].minor, DDI_PSEUDO, mm[i].privonly,
1290Sstevel@tonic-gate 		    mm[i].rdpriv, mm[i].wrpriv, mm[i].priv_mode) ==
1300Sstevel@tonic-gate 		    DDI_FAILURE) {
1310Sstevel@tonic-gate 			ddi_remove_minor_node(devi, NULL);
1320Sstevel@tonic-gate 			return (DDI_FAILURE);
1330Sstevel@tonic-gate 		}
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	mm_dip = devi;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	ksp = kstat_create("mm", 0, "phys_installed", "misc",
1390Sstevel@tonic-gate 	    KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VAR_SIZE | KSTAT_FLAG_VIRTUAL);
1400Sstevel@tonic-gate 	if (ksp != NULL) {
1410Sstevel@tonic-gate 		ksp->ks_update = mm_kstat_update;
1420Sstevel@tonic-gate 		ksp->ks_snapshot = mm_kstat_snapshot;
1430Sstevel@tonic-gate 		ksp->ks_lock = &mm_lock; /* XXX - not really needed */
1440Sstevel@tonic-gate 		kstat_install(ksp);
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	mm_kmem_io_access = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
1480Sstevel@tonic-gate 	    "kmem_io_access", 0);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	return (DDI_SUCCESS);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate /*ARGSUSED*/
1540Sstevel@tonic-gate static int
1550Sstevel@tonic-gate mm_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	register int error;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	switch (infocmd) {
1600Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
1610Sstevel@tonic-gate 		*result = (void *)mm_dip;
1620Sstevel@tonic-gate 		error = DDI_SUCCESS;
1630Sstevel@tonic-gate 		break;
1640Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
1650Sstevel@tonic-gate 		*result = (void *)0;
1660Sstevel@tonic-gate 		error = DDI_SUCCESS;
1670Sstevel@tonic-gate 		break;
1680Sstevel@tonic-gate 	default:
1690Sstevel@tonic-gate 		error = DDI_FAILURE;
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 	return (error);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate /*ARGSUSED1*/
1750Sstevel@tonic-gate static int
1760Sstevel@tonic-gate mmopen(dev_t *devp, int flag, int typ, struct cred *cred)
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate 	switch (getminor(*devp)) {
1790Sstevel@tonic-gate 	case M_NULL:
1800Sstevel@tonic-gate 	case M_ZERO:
1810Sstevel@tonic-gate 	case M_MEM:
1820Sstevel@tonic-gate 	case M_KMEM:
1830Sstevel@tonic-gate 	case M_ALLKMEM:
1840Sstevel@tonic-gate 		/* standard devices */
1850Sstevel@tonic-gate 		break;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	default:
1880Sstevel@tonic-gate 		/* Unsupported or unknown type */
1890Sstevel@tonic-gate 		return (EINVAL);
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate 	return (0);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate struct pollhead	mm_pollhd;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate /*ARGSUSED*/
1970Sstevel@tonic-gate static int
1980Sstevel@tonic-gate mmchpoll(dev_t dev, short events, int anyyet, short *reventsp,
1990Sstevel@tonic-gate     struct pollhead **phpp)
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate 	switch (getminor(dev)) {
2020Sstevel@tonic-gate 	case M_NULL:
2030Sstevel@tonic-gate 	case M_ZERO:
2040Sstevel@tonic-gate 	case M_MEM:
2050Sstevel@tonic-gate 	case M_KMEM:
2060Sstevel@tonic-gate 	case M_ALLKMEM:
2070Sstevel@tonic-gate 		*reventsp = events & (POLLIN | POLLOUT | POLLPRI | POLLRDNORM |
2080Sstevel@tonic-gate 			POLLWRNORM | POLLRDBAND | POLLWRBAND);
2090Sstevel@tonic-gate 		/*
2100Sstevel@tonic-gate 		 * A non NULL pollhead pointer should be returned in case
2110Sstevel@tonic-gate 		 * user polls for 0 events.
2120Sstevel@tonic-gate 		 */
2130Sstevel@tonic-gate 		*phpp = !anyyet && !*reventsp ?
2140Sstevel@tonic-gate 		    &mm_pollhd : (struct pollhead *)NULL;
2150Sstevel@tonic-gate 		return (0);
2160Sstevel@tonic-gate 	default:
2170Sstevel@tonic-gate 		/* no other devices currently support polling */
2180Sstevel@tonic-gate 		return (ENXIO);
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate static int
2230Sstevel@tonic-gate mmpropop(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int flags,
2240Sstevel@tonic-gate     char *name, caddr_t valuep, int *lengthp)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	/*
2270Sstevel@tonic-gate 	 * implement zero size to reduce overhead (avoid two failing
2280Sstevel@tonic-gate 	 * property lookups per stat).
2290Sstevel@tonic-gate 	 */
2300Sstevel@tonic-gate 	return (ddi_prop_op_size(dev, dip, prop_op,
2310Sstevel@tonic-gate 	    flags, name, valuep, lengthp, 0));
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate static int
2350Sstevel@tonic-gate mmio(struct uio *uio, enum uio_rw rw, pfn_t pfn, off_t pageoff, int allowio)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate 	int error = 0;
2380Sstevel@tonic-gate 	size_t nbytes = MIN((size_t)(PAGESIZE - pageoff),
2390Sstevel@tonic-gate 	    (size_t)uio->uio_iov->iov_len);
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	mutex_enter(&mm_lock);
2420Sstevel@tonic-gate 	hat_devload(kas.a_hat, mm_map, PAGESIZE, pfn,
2430Sstevel@tonic-gate 	    (uint_t)(rw == UIO_READ ? PROT_READ : PROT_READ | PROT_WRITE),
2440Sstevel@tonic-gate 	    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	if (!pf_is_memory(pfn)) {
2470Sstevel@tonic-gate 		if (allowio) {
2480Sstevel@tonic-gate 			size_t c = uio->uio_iov->iov_len;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 			if (ddi_peekpokeio(NULL, uio, rw,
2510Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)uio->uio_loffset, c,
2520Sstevel@tonic-gate 			    sizeof (int32_t)) != DDI_SUCCESS)
2530Sstevel@tonic-gate 				error = EFAULT;
2540Sstevel@tonic-gate 		} else
2550Sstevel@tonic-gate 			error = EIO;
2560Sstevel@tonic-gate 	} else
2570Sstevel@tonic-gate 		error = uiomove(&mm_map[pageoff], nbytes, rw, uio);
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	hat_unload(kas.a_hat, mm_map, PAGESIZE, HAT_UNLOAD_UNLOCK);
2600Sstevel@tonic-gate 	mutex_exit(&mm_lock);
2610Sstevel@tonic-gate 	return (error);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate #ifdef	__sparc
2650Sstevel@tonic-gate 
266670Selowe static int
267670Selowe mmpagelock(struct as *as, caddr_t va)
268670Selowe {
269670Selowe 	struct seg *seg;
270670Selowe 	int i;
271670Selowe 
272670Selowe 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
273670Selowe 	seg = as_segat(as, va);
274670Selowe 	i = (seg != NULL)? SEGOP_CAPABLE(seg, S_CAPABILITY_NOMINFLT) : 0;
275670Selowe 	AS_LOCK_EXIT(as, &as->a_lock);
276670Selowe 
277670Selowe 	return (i);
278670Selowe }
279670Selowe 
280670Selowe #define	NEED_LOCK_KVADDR(kva)	mmpagelock(&kas, kva)
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate #else	/* __i386, __amd64 */
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate #define	NEED_LOCK_KVADDR(va)	0
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate #endif	/* __sparc */
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate /*ARGSUSED3*/
2890Sstevel@tonic-gate static int
2900Sstevel@tonic-gate mmrw(dev_t dev, struct uio *uio, enum uio_rw rw, cred_t *cred)
2910Sstevel@tonic-gate {
2920Sstevel@tonic-gate 	pfn_t v;
2930Sstevel@tonic-gate 	struct iovec *iov;
2940Sstevel@tonic-gate 	int error = 0;
2950Sstevel@tonic-gate 	size_t c;
2960Sstevel@tonic-gate 	ssize_t oresid = uio->uio_resid;
2970Sstevel@tonic-gate 	minor_t minor = getminor(dev);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	while (uio->uio_resid > 0 && error == 0) {
3000Sstevel@tonic-gate 		iov = uio->uio_iov;
3010Sstevel@tonic-gate 		if (iov->iov_len == 0) {
3020Sstevel@tonic-gate 			uio->uio_iov++;
3030Sstevel@tonic-gate 			uio->uio_iovcnt--;
3040Sstevel@tonic-gate 			if (uio->uio_iovcnt < 0)
3050Sstevel@tonic-gate 				panic("mmrw");
3060Sstevel@tonic-gate 			continue;
3070Sstevel@tonic-gate 		}
3080Sstevel@tonic-gate 		switch (minor) {
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 		case M_MEM:
3110Sstevel@tonic-gate 			memlist_read_lock();
3120Sstevel@tonic-gate 			if (!address_in_memlist(phys_install,
3130Sstevel@tonic-gate 			    (uint64_t)uio->uio_loffset, 1)) {
3140Sstevel@tonic-gate 				memlist_read_unlock();
3150Sstevel@tonic-gate 				error = EFAULT;
3160Sstevel@tonic-gate 				break;
3170Sstevel@tonic-gate 			}
3180Sstevel@tonic-gate 			memlist_read_unlock();
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 			v = BTOP((u_offset_t)uio->uio_loffset);
3210Sstevel@tonic-gate 			error = mmio(uio, rw, v,
3220Sstevel@tonic-gate 			    uio->uio_loffset & PAGEOFFSET, 0);
3230Sstevel@tonic-gate 			break;
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 		case M_KMEM:
3260Sstevel@tonic-gate 		case M_ALLKMEM:
3270Sstevel@tonic-gate 			{
3280Sstevel@tonic-gate 			page_t **ppp;
3290Sstevel@tonic-gate 			caddr_t vaddr = (caddr_t)uio->uio_offset;
3300Sstevel@tonic-gate 			int try_lock = NEED_LOCK_KVADDR(vaddr);
3310Sstevel@tonic-gate 			int locked = 0;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 			/*
3340Sstevel@tonic-gate 			 * If vaddr does not map a valid page, as_pagelock()
3350Sstevel@tonic-gate 			 * will return failure. Hence we can't check the
3360Sstevel@tonic-gate 			 * return value and return EFAULT here as we'd like.
3370Sstevel@tonic-gate 			 * seg_kp and seg_kpm do not properly support
3380Sstevel@tonic-gate 			 * as_pagelock() for this context so we avoid it
3390Sstevel@tonic-gate 			 * using the try_lock set check above.  Some day when
3400Sstevel@tonic-gate 			 * the kernel page locking gets redesigned all this
3410Sstevel@tonic-gate 			 * muck can be cleaned up.
3420Sstevel@tonic-gate 			 */
3430Sstevel@tonic-gate 			if (try_lock)
3440Sstevel@tonic-gate 				locked = (as_pagelock(&kas, &ppp, vaddr,
3450Sstevel@tonic-gate 				    PAGESIZE, S_WRITE) == 0);
3460Sstevel@tonic-gate 
347513Sjongkis 			v = hat_getpfnum(kas.a_hat,
348513Sjongkis 			    (caddr_t)(uintptr_t)uio->uio_loffset);
3490Sstevel@tonic-gate 			if (v == PFN_INVALID) {
3500Sstevel@tonic-gate 				if (locked)
3510Sstevel@tonic-gate 					as_pageunlock(&kas, ppp, vaddr,
3520Sstevel@tonic-gate 					    PAGESIZE, S_WRITE);
3530Sstevel@tonic-gate 				error = EFAULT;
3540Sstevel@tonic-gate 				break;
3550Sstevel@tonic-gate 			}
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 			error = mmio(uio, rw, v, uio->uio_loffset & PAGEOFFSET,
3580Sstevel@tonic-gate 			    minor == M_ALLKMEM || mm_kmem_io_access);
3590Sstevel@tonic-gate 			if (locked)
3600Sstevel@tonic-gate 				as_pageunlock(&kas, ppp, vaddr, PAGESIZE,
3610Sstevel@tonic-gate 				    S_WRITE);
3620Sstevel@tonic-gate 			}
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 			break;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 		case M_ZERO:
3670Sstevel@tonic-gate 			if (rw == UIO_READ) {
3680Sstevel@tonic-gate 				label_t ljb;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 				if (on_fault(&ljb)) {
3710Sstevel@tonic-gate 					no_fault();
3720Sstevel@tonic-gate 					error = EFAULT;
3730Sstevel@tonic-gate 					break;
3740Sstevel@tonic-gate 				}
3750Sstevel@tonic-gate 				uzero(iov->iov_base, iov->iov_len);
3760Sstevel@tonic-gate 				no_fault();
3770Sstevel@tonic-gate 				uio->uio_resid -= iov->iov_len;
3780Sstevel@tonic-gate 				uio->uio_loffset += iov->iov_len;
3790Sstevel@tonic-gate 				break;
3800Sstevel@tonic-gate 			}
3810Sstevel@tonic-gate 			/* else it's a write, fall through to NULL case */
3820Sstevel@tonic-gate 			/*FALLTHROUGH*/
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 		case M_NULL:
3850Sstevel@tonic-gate 			if (rw == UIO_READ)
3860Sstevel@tonic-gate 				return (0);
3870Sstevel@tonic-gate 			c = iov->iov_len;
3880Sstevel@tonic-gate 			iov->iov_base += c;
3890Sstevel@tonic-gate 			iov->iov_len -= c;
3900Sstevel@tonic-gate 			uio->uio_loffset += c;
3910Sstevel@tonic-gate 			uio->uio_resid -= c;
3920Sstevel@tonic-gate 			break;
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 		}
3950Sstevel@tonic-gate 	}
3960Sstevel@tonic-gate 	return (uio->uio_resid == oresid ? error : 0);
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate static int
4000Sstevel@tonic-gate mmread(dev_t dev, struct uio *uio, cred_t *cred)
4010Sstevel@tonic-gate {
4020Sstevel@tonic-gate 	return (mmrw(dev, uio, UIO_READ, cred));
4030Sstevel@tonic-gate }
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate static int
4060Sstevel@tonic-gate mmwrite(dev_t dev, struct uio *uio, cred_t *cred)
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate 	return (mmrw(dev, uio, UIO_WRITE, cred));
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate /*
4120Sstevel@tonic-gate  * Private ioctl for libkvm to support kvm_physaddr().
4130Sstevel@tonic-gate  * Given an address space and a VA, compute the PA.
4140Sstevel@tonic-gate  */
4150Sstevel@tonic-gate static int
4160Sstevel@tonic-gate mmioctl_vtop(intptr_t data)
4170Sstevel@tonic-gate {
4180Sstevel@tonic-gate 	mem_vtop_t mem_vtop;
4190Sstevel@tonic-gate 	proc_t *p;
4200Sstevel@tonic-gate 	pfn_t pfn = (pfn_t)PFN_INVALID;
4210Sstevel@tonic-gate 	pid_t pid = 0;
4220Sstevel@tonic-gate 	struct as *as;
4230Sstevel@tonic-gate 	struct seg *seg;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	if (copyin((void *)data, &mem_vtop, sizeof (mem_vtop_t)))
4260Sstevel@tonic-gate 		return (EFAULT);
4270Sstevel@tonic-gate 	if (mem_vtop.m_as == &kas) {
4280Sstevel@tonic-gate 		pfn = hat_getpfnum(kas.a_hat, mem_vtop.m_va);
4290Sstevel@tonic-gate 	} else if (mem_vtop.m_as == NULL) {
4300Sstevel@tonic-gate 		return (EIO);
4310Sstevel@tonic-gate 	} else {
4320Sstevel@tonic-gate 		mutex_enter(&pidlock);
4330Sstevel@tonic-gate 		for (p = practive; p != NULL; p = p->p_next) {
4340Sstevel@tonic-gate 			if (p->p_as == mem_vtop.m_as) {
4350Sstevel@tonic-gate 				pid = p->p_pid;
4360Sstevel@tonic-gate 				break;
4370Sstevel@tonic-gate 			}
4380Sstevel@tonic-gate 		}
4390Sstevel@tonic-gate 		mutex_exit(&pidlock);
4400Sstevel@tonic-gate 		if (p == NULL)
4410Sstevel@tonic-gate 			return (EIO);
4420Sstevel@tonic-gate 		p = sprlock(pid);
4430Sstevel@tonic-gate 		if (p == NULL)
4440Sstevel@tonic-gate 			return (EIO);
4450Sstevel@tonic-gate 		as = p->p_as;
4460Sstevel@tonic-gate 		if (as == mem_vtop.m_as) {
4470Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
4480Sstevel@tonic-gate 			AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
4490Sstevel@tonic-gate 			for (seg = AS_SEGFIRST(as); seg != NULL;
4500Sstevel@tonic-gate 			    seg = AS_SEGNEXT(as, seg))
4510Sstevel@tonic-gate 				if ((uintptr_t)mem_vtop.m_va -
4520Sstevel@tonic-gate 				    (uintptr_t)seg->s_base < seg->s_size)
4530Sstevel@tonic-gate 					break;
4540Sstevel@tonic-gate 			if (seg != NULL)
4550Sstevel@tonic-gate 				pfn = hat_getpfnum(as->a_hat, mem_vtop.m_va);
4560Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
4570Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
4580Sstevel@tonic-gate 		}
4590Sstevel@tonic-gate 		sprunlock(p);
4600Sstevel@tonic-gate 	}
4610Sstevel@tonic-gate 	mem_vtop.m_pfn = pfn;
4620Sstevel@tonic-gate 	if (pfn == PFN_INVALID)
4630Sstevel@tonic-gate 		return (EIO);
4640Sstevel@tonic-gate 	if (copyout(&mem_vtop, (void *)data, sizeof (mem_vtop_t)))
4650Sstevel@tonic-gate 		return (EFAULT);
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	return (0);
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate /*
471917Selowe  * Given a PA, execute the given page retire command on it.
4720Sstevel@tonic-gate  */
4730Sstevel@tonic-gate static int
4740Sstevel@tonic-gate mmioctl_page_retire(int cmd, intptr_t data)
4750Sstevel@tonic-gate {
476917Selowe 	extern int page_retire_test(void);
4770Sstevel@tonic-gate 	uint64_t pa;
4780Sstevel@tonic-gate 
479917Selowe 	if (copyin((void *)data, &pa, sizeof (uint64_t))) {
480917Selowe 		return (EFAULT);
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate 
483917Selowe 	switch (cmd) {
484917Selowe 	case MEM_PAGE_ISRETIRED:
485917Selowe 		return (page_retire_check(pa, NULL));
486917Selowe 
487917Selowe 	case MEM_PAGE_UNRETIRE:
488917Selowe 		return (page_unretire(pa));
489917Selowe 
490917Selowe 	case MEM_PAGE_RETIRE:
491917Selowe 		return (page_retire(pa, PR_FMA));
492917Selowe 
493917Selowe 	case MEM_PAGE_RETIRE_MCE:
494917Selowe 		return (page_retire(pa, PR_MCE));
495917Selowe 
496917Selowe 	case MEM_PAGE_RETIRE_UE:
497917Selowe 		return (page_retire(pa, PR_UE));
4980Sstevel@tonic-gate 
499917Selowe 	case MEM_PAGE_GETERRORS:
500917Selowe 		{
501917Selowe 			uint64_t page_errors;
502917Selowe 			int rc = page_retire_check(pa, &page_errors);
503917Selowe 			if (copyout(&page_errors, (void *)data,
504917Selowe 			    sizeof (uint64_t))) {
505917Selowe 				return (EFAULT);
506917Selowe 			}
507917Selowe 			return (rc);
508917Selowe 		}
509917Selowe 
510917Selowe 	case MEM_PAGE_RETIRE_TEST:
511917Selowe 		return (page_retire_test());
512917Selowe 
513917Selowe 	}
514917Selowe 
515917Selowe 	return (EINVAL);
5160Sstevel@tonic-gate }
5170Sstevel@tonic-gate 
5181186Sayznaga /*
5191186Sayznaga  * Given a mem-scheme FMRI for a page, execute the given page retire
5201186Sayznaga  * command on it.
5211186Sayznaga  */
5221186Sayznaga static int
5231186Sayznaga mmioctl_page_fmri_retire(int cmd, intptr_t data)
5241186Sayznaga {
5251186Sayznaga 	mem_page_t mpage;
5261186Sayznaga 	uint64_t pa;
5271186Sayznaga 	nvlist_t *nvl;
5281186Sayznaga 	int err;
5291186Sayznaga 
5301186Sayznaga 	if ((err = mm_read_mem_page(data, &mpage)) < 0)
5311186Sayznaga 		return (err);
5321186Sayznaga 
5331186Sayznaga 	if ((err = mm_get_mem_fmri(&mpage, &nvl)) < 0)
5341186Sayznaga 		return (err);
5351186Sayznaga 
5361186Sayznaga 	if ((err = mm_get_paddr(nvl, &pa)) < 0) {
5371186Sayznaga 		nvlist_free(nvl);
5381186Sayznaga 		return (err);
5391186Sayznaga 	}
5401186Sayznaga 
5411186Sayznaga 	nvlist_free(nvl);
5421186Sayznaga 
5431186Sayznaga 	switch (cmd) {
5441186Sayznaga 	case MEM_PAGE_FMRI_ISRETIRED:
5451186Sayznaga 		return (page_retire_check(pa, NULL));
5461186Sayznaga 
5471186Sayznaga 	case MEM_PAGE_FMRI_RETIRE:
5481186Sayznaga 		return (page_retire(pa, PR_FMA));
5491186Sayznaga 	}
5501186Sayznaga 
5511186Sayznaga 	return (EINVAL);
5521186Sayznaga }
5531186Sayznaga 
5540Sstevel@tonic-gate #ifdef __sparc
5550Sstevel@tonic-gate /*
5560Sstevel@tonic-gate  * Given a syndrome, syndrome type, and address return the
5570Sstevel@tonic-gate  * associated memory name in the provided data buffer.
5580Sstevel@tonic-gate  */
5590Sstevel@tonic-gate static int
5600Sstevel@tonic-gate mmioctl_get_mem_name(intptr_t data)
5610Sstevel@tonic-gate {
5620Sstevel@tonic-gate 	mem_name_t mem_name;
5630Sstevel@tonic-gate 	void *buf;
5640Sstevel@tonic-gate 	size_t bufsize;
5650Sstevel@tonic-gate 	int len, err;
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	if ((bufsize = cpu_get_name_bufsize()) == 0)
5680Sstevel@tonic-gate 		return (ENOTSUP);
5690Sstevel@tonic-gate 
5701186Sayznaga 	if ((err = mm_read_mem_name(data, &mem_name)) < 0)
5711186Sayznaga 		return (err);
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	buf = kmem_alloc(bufsize, KM_SLEEP);
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	/*
5760Sstevel@tonic-gate 	 * Call into cpu specific code to do the lookup.
5770Sstevel@tonic-gate 	 */
5780Sstevel@tonic-gate 	if ((err = cpu_get_mem_name(mem_name.m_synd, mem_name.m_type,
5790Sstevel@tonic-gate 	    mem_name.m_addr, buf, bufsize, &len)) != 0) {
5800Sstevel@tonic-gate 		kmem_free(buf, bufsize);
5810Sstevel@tonic-gate 		return (err);
5820Sstevel@tonic-gate 	}
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	if (len >= mem_name.m_namelen) {
5850Sstevel@tonic-gate 		kmem_free(buf, bufsize);
5860Sstevel@tonic-gate 		return (ENAMETOOLONG);
5870Sstevel@tonic-gate 	}
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	if (copyoutstr(buf, (char *)mem_name.m_name,
5900Sstevel@tonic-gate 	    mem_name.m_namelen, NULL) != 0) {
5910Sstevel@tonic-gate 		kmem_free(buf, bufsize);
5920Sstevel@tonic-gate 		return (EFAULT);
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	kmem_free(buf, bufsize);
5960Sstevel@tonic-gate 	return (0);
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate /*
6000Sstevel@tonic-gate  * Given a syndrome and address return information about the associated memory.
6010Sstevel@tonic-gate  */
6020Sstevel@tonic-gate static int
6030Sstevel@tonic-gate mmioctl_get_mem_info(intptr_t data)
6040Sstevel@tonic-gate {
6050Sstevel@tonic-gate 	mem_info_t mem_info;
6060Sstevel@tonic-gate 	int err;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	if (copyin((void *)data, &mem_info, sizeof (mem_info_t)))
6090Sstevel@tonic-gate 		return (EFAULT);
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	if ((err = cpu_get_mem_info(mem_info.m_synd, mem_info.m_addr,
6120Sstevel@tonic-gate 	    &mem_info.m_mem_size, &mem_info.m_seg_size, &mem_info.m_bank_size,
6130Sstevel@tonic-gate 	    &mem_info.m_segments, &mem_info.m_banks, &mem_info.m_mcid)) != 0)
6140Sstevel@tonic-gate 		return (err);
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	if (copyout(&mem_info, (void *)data, sizeof (mem_info_t)) != 0)
6170Sstevel@tonic-gate 		return (EFAULT);
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	return (0);
6200Sstevel@tonic-gate }
6211186Sayznaga 
6221186Sayznaga /*
6231186Sayznaga  * Given a memory name, return its associated serial id
6241186Sayznaga  */
6251186Sayznaga static int
6261186Sayznaga mmioctl_get_mem_sid(intptr_t data)
6271186Sayznaga {
6281186Sayznaga 	mem_name_t mem_name;
6291186Sayznaga 	void *buf;
6301186Sayznaga 	void *name;
6311186Sayznaga 	size_t	name_len;
6321186Sayznaga 	size_t bufsize;
6331186Sayznaga 	int len, err;
6341186Sayznaga 
6351186Sayznaga 	if ((bufsize = cpu_get_name_bufsize()) == 0)
6361186Sayznaga 		return (ENOTSUP);
6371186Sayznaga 
6381186Sayznaga 	if ((err = mm_read_mem_name(data, &mem_name)) < 0)
6391186Sayznaga 		return (err);
6401186Sayznaga 
6411186Sayznaga 	buf = kmem_alloc(bufsize, KM_SLEEP);
6421186Sayznaga 
6431186Sayznaga 	if (mem_name.m_namelen > 1024)
6441186Sayznaga 		mem_name.m_namelen = 1024; /* cap at 1024 bytes */
6451186Sayznaga 
6461186Sayznaga 	name = kmem_alloc(mem_name.m_namelen, KM_SLEEP);
6471186Sayznaga 
6481186Sayznaga 	if ((err = copyinstr((char *)mem_name.m_name, (char *)name,
6491186Sayznaga 	    mem_name.m_namelen, &name_len)) != 0) {
6501186Sayznaga 		kmem_free(buf, bufsize);
6511186Sayznaga 		kmem_free(name, mem_name.m_namelen);
6521186Sayznaga 		return (err);
6531186Sayznaga 	}
6541186Sayznaga 
6551186Sayznaga 	/*
6561186Sayznaga 	 * Call into cpu specific code to do the lookup.
6571186Sayznaga 	 */
6581186Sayznaga 	if ((err = cpu_get_mem_sid(name, buf, bufsize, &len)) != 0) {
6591186Sayznaga 		kmem_free(buf, bufsize);
6601186Sayznaga 		kmem_free(name, mem_name.m_namelen);
6611186Sayznaga 		return (err);
6621186Sayznaga 	}
6631186Sayznaga 
6641186Sayznaga 	if (len > mem_name.m_sidlen) {
6651186Sayznaga 		kmem_free(buf, bufsize);
6661186Sayznaga 		kmem_free(name, mem_name.m_namelen);
6671186Sayznaga 		return (ENAMETOOLONG);
6681186Sayznaga 	}
6691186Sayznaga 
6701186Sayznaga 	if (copyoutstr(buf, (char *)mem_name.m_sid,
6711186Sayznaga 	    mem_name.m_sidlen, NULL) != 0) {
6721186Sayznaga 		kmem_free(buf, bufsize);
6731186Sayznaga 		kmem_free(name, mem_name.m_namelen);
6741186Sayznaga 		return (EFAULT);
6751186Sayznaga 	}
6761186Sayznaga 
6771186Sayznaga 	kmem_free(buf, bufsize);
6781186Sayznaga 	kmem_free(name, mem_name.m_namelen);
6791186Sayznaga 	return (0);
6801186Sayznaga }
6810Sstevel@tonic-gate #endif	/* __sparc */
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate /*
6840Sstevel@tonic-gate  * Private ioctls for
6850Sstevel@tonic-gate  *	libkvm to support kvm_physaddr().
6860Sstevel@tonic-gate  *	FMA support for page_retire() and memory attribute information.
6870Sstevel@tonic-gate  */
6880Sstevel@tonic-gate /*ARGSUSED*/
6890Sstevel@tonic-gate static int
6900Sstevel@tonic-gate mmioctl(dev_t dev, int cmd, intptr_t data, int flag, cred_t *cred, int *rvalp)
6910Sstevel@tonic-gate {
6921186Sayznaga 	if (cmd == MEM_VTOP && (getminor(dev) != M_KMEM))
6931186Sayznaga 		return (ENXIO);
6941186Sayznaga 	else if (getminor(dev) != M_MEM)
6951186Sayznaga 		return (ENXIO);
6961186Sayznaga 
6970Sstevel@tonic-gate 	switch (cmd) {
6980Sstevel@tonic-gate 	case MEM_VTOP:
6990Sstevel@tonic-gate 		return (mmioctl_vtop(data));
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	case MEM_PAGE_RETIRE:
7020Sstevel@tonic-gate 	case MEM_PAGE_ISRETIRED:
703917Selowe 	case MEM_PAGE_UNRETIRE:
704917Selowe 	case MEM_PAGE_RETIRE_MCE:
705917Selowe 	case MEM_PAGE_RETIRE_UE:
706917Selowe 	case MEM_PAGE_GETERRORS:
707917Selowe 	case MEM_PAGE_RETIRE_TEST:
7080Sstevel@tonic-gate 		return (mmioctl_page_retire(cmd, data));
7090Sstevel@tonic-gate 
7101186Sayznaga 	case MEM_PAGE_FMRI_RETIRE:
7111186Sayznaga 	case MEM_PAGE_FMRI_ISRETIRED:
7121186Sayznaga 		return (mmioctl_page_fmri_retire(cmd, data));
7131186Sayznaga 
7140Sstevel@tonic-gate #ifdef __sparc
7151186Sayznaga 	case MEM_NAME:
7160Sstevel@tonic-gate 		return (mmioctl_get_mem_name(data));
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	case MEM_INFO:
7190Sstevel@tonic-gate 		return (mmioctl_get_mem_info(data));
7201186Sayznaga 
7211186Sayznaga 	case MEM_SID:
7221186Sayznaga 		return (mmioctl_get_mem_sid(data));
7230Sstevel@tonic-gate #else
7241186Sayznaga 	case MEM_NAME:
7251186Sayznaga 	case MEM_INFO:
7261186Sayznaga 	case MEM_SID:
7270Sstevel@tonic-gate 		return (ENOTSUP);
7281186Sayznaga #endif	/* __sparc */
7290Sstevel@tonic-gate 	}
7300Sstevel@tonic-gate 	return (ENXIO);
7310Sstevel@tonic-gate }
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate /*ARGSUSED2*/
7340Sstevel@tonic-gate static int
7350Sstevel@tonic-gate mmmmap(dev_t dev, off_t off, int prot)
7360Sstevel@tonic-gate {
7370Sstevel@tonic-gate 	pfn_t pf;
7380Sstevel@tonic-gate 	struct memlist *pmem;
7390Sstevel@tonic-gate 	minor_t minor = getminor(dev);
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	switch (minor) {
7420Sstevel@tonic-gate 	case M_MEM:
7430Sstevel@tonic-gate 		pf = btop(off);
7440Sstevel@tonic-gate 		memlist_read_lock();
7450Sstevel@tonic-gate 		for (pmem = phys_install; pmem != NULL; pmem = pmem->next) {
7460Sstevel@tonic-gate 			if (pf >= BTOP(pmem->address) &&
7470Sstevel@tonic-gate 			    pf < BTOP(pmem->address + pmem->size)) {
7480Sstevel@tonic-gate 				memlist_read_unlock();
7490Sstevel@tonic-gate 				return (impl_obmem_pfnum(pf));
7500Sstevel@tonic-gate 			}
7510Sstevel@tonic-gate 		}
7520Sstevel@tonic-gate 		memlist_read_unlock();
7530Sstevel@tonic-gate 		break;
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	case M_KMEM:
7560Sstevel@tonic-gate 	case M_ALLKMEM:
7570Sstevel@tonic-gate 		/* no longer supported with KPR */
7580Sstevel@tonic-gate 		return (-1);
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	case M_ZERO:
7610Sstevel@tonic-gate 		/*
7620Sstevel@tonic-gate 		 * We shouldn't be mmap'ing to /dev/zero here as
7630Sstevel@tonic-gate 		 * mmsegmap() should have already converted
7640Sstevel@tonic-gate 		 * a mapping request for this device to a mapping
7650Sstevel@tonic-gate 		 * using seg_vn for anonymous memory.
7660Sstevel@tonic-gate 		 */
7670Sstevel@tonic-gate 		break;
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 	return (-1);
7710Sstevel@tonic-gate }
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate /*
7740Sstevel@tonic-gate  * This function is called when a memory device is mmap'ed.
7750Sstevel@tonic-gate  * Set up the mapping to the correct device driver.
7760Sstevel@tonic-gate  */
7770Sstevel@tonic-gate static int
7780Sstevel@tonic-gate mmsegmap(dev_t dev, off_t off, struct as *as, caddr_t *addrp, off_t len,
7790Sstevel@tonic-gate     uint_t prot, uint_t maxprot, uint_t flags, struct cred *cred)
7800Sstevel@tonic-gate {
7810Sstevel@tonic-gate 	struct segvn_crargs vn_a;
7820Sstevel@tonic-gate 	struct segdev_crargs dev_a;
7830Sstevel@tonic-gate 	int error;
7840Sstevel@tonic-gate 	minor_t minor;
7850Sstevel@tonic-gate 	off_t i;
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	minor = getminor(dev);
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	as_rangelock(as);
7900Sstevel@tonic-gate 	if ((flags & MAP_FIXED) == 0) {
7910Sstevel@tonic-gate 		/*
7920Sstevel@tonic-gate 		 * No need to worry about vac alignment on /dev/zero
7930Sstevel@tonic-gate 		 * since this is a "clone" object that doesn't yet exist.
7940Sstevel@tonic-gate 		 */
7950Sstevel@tonic-gate 		map_addr(addrp, len, (offset_t)off,
7960Sstevel@tonic-gate 				(minor == M_MEM) || (minor == M_KMEM), flags);
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 		if (*addrp == NULL) {
7990Sstevel@tonic-gate 			as_rangeunlock(as);
8000Sstevel@tonic-gate 			return (ENOMEM);
8010Sstevel@tonic-gate 		}
8020Sstevel@tonic-gate 	} else {
8030Sstevel@tonic-gate 		/*
8040Sstevel@tonic-gate 		 * User specified address -
8050Sstevel@tonic-gate 		 * Blow away any previous mappings.
8060Sstevel@tonic-gate 		 */
8070Sstevel@tonic-gate 		(void) as_unmap(as, *addrp, len);
8080Sstevel@tonic-gate 	}
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	switch (minor) {
8110Sstevel@tonic-gate 	case M_MEM:
8120Sstevel@tonic-gate 		/* /dev/mem cannot be mmap'ed with MAP_PRIVATE */
8130Sstevel@tonic-gate 		if ((flags & MAP_TYPE) != MAP_SHARED) {
8140Sstevel@tonic-gate 			as_rangeunlock(as);
8150Sstevel@tonic-gate 			return (EINVAL);
8160Sstevel@tonic-gate 		}
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 		/*
8190Sstevel@tonic-gate 		 * Check to ensure that the entire range is
8200Sstevel@tonic-gate 		 * legal and we are not trying to map in
8210Sstevel@tonic-gate 		 * more than the device will let us.
8220Sstevel@tonic-gate 		 */
8230Sstevel@tonic-gate 		for (i = 0; i < len; i += PAGESIZE) {
8240Sstevel@tonic-gate 			if (mmmmap(dev, off + i, maxprot) == -1) {
8250Sstevel@tonic-gate 				as_rangeunlock(as);
8260Sstevel@tonic-gate 				return (ENXIO);
8270Sstevel@tonic-gate 			}
8280Sstevel@tonic-gate 		}
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 		/*
8310Sstevel@tonic-gate 		 * Use seg_dev segment driver for /dev/mem mapping.
8320Sstevel@tonic-gate 		 */
8330Sstevel@tonic-gate 		dev_a.mapfunc = mmmmap;
8340Sstevel@tonic-gate 		dev_a.dev = dev;
8350Sstevel@tonic-gate 		dev_a.offset = off;
8360Sstevel@tonic-gate 		dev_a.type = (flags & MAP_TYPE);
8370Sstevel@tonic-gate 		dev_a.prot = (uchar_t)prot;
8380Sstevel@tonic-gate 		dev_a.maxprot = (uchar_t)maxprot;
8390Sstevel@tonic-gate 		dev_a.hat_attr = 0;
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 		/*
8420Sstevel@tonic-gate 		 * Make /dev/mem mappings non-consistent since we can't
8430Sstevel@tonic-gate 		 * alias pages that don't have page structs behind them,
8440Sstevel@tonic-gate 		 * such as kernel stack pages. If someone mmap()s a kernel
8450Sstevel@tonic-gate 		 * stack page and if we give him a tte with cv, a line from
8460Sstevel@tonic-gate 		 * that page can get into both pages of the spitfire d$.
8470Sstevel@tonic-gate 		 * But snoop from another processor will only invalidate
8480Sstevel@tonic-gate 		 * the first page. This later caused kernel (xc_attention)
8490Sstevel@tonic-gate 		 * to go into an infinite loop at pil 13 and no interrupts
8500Sstevel@tonic-gate 		 * could come in. See 1203630.
8510Sstevel@tonic-gate 		 *
8520Sstevel@tonic-gate 		 */
8530Sstevel@tonic-gate 		dev_a.hat_flags = HAT_LOAD_NOCONSIST;
8540Sstevel@tonic-gate 		dev_a.devmap_data = NULL;
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 		error = as_map(as, *addrp, len, segdev_create, &dev_a);
8570Sstevel@tonic-gate 		break;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	case M_ZERO:
8600Sstevel@tonic-gate 		/*
8610Sstevel@tonic-gate 		 * Use seg_vn segment driver for /dev/zero mapping.
8620Sstevel@tonic-gate 		 * Passing in a NULL amp gives us the "cloning" effect.
8630Sstevel@tonic-gate 		 */
8640Sstevel@tonic-gate 		vn_a.vp = NULL;
8650Sstevel@tonic-gate 		vn_a.offset = 0;
8660Sstevel@tonic-gate 		vn_a.type = (flags & MAP_TYPE);
8670Sstevel@tonic-gate 		vn_a.prot = prot;
8680Sstevel@tonic-gate 		vn_a.maxprot = maxprot;
8690Sstevel@tonic-gate 		vn_a.flags = flags & ~MAP_TYPE;
8700Sstevel@tonic-gate 		vn_a.cred = cred;
8710Sstevel@tonic-gate 		vn_a.amp = NULL;
8720Sstevel@tonic-gate 		vn_a.szc = 0;
8730Sstevel@tonic-gate 		vn_a.lgrp_mem_policy_flags = 0;
8740Sstevel@tonic-gate 		error = as_map(as, *addrp, len, segvn_create, &vn_a);
8750Sstevel@tonic-gate 		break;
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	case M_KMEM:
8780Sstevel@tonic-gate 	case M_ALLKMEM:
8790Sstevel@tonic-gate 		/* No longer supported with KPR. */
8800Sstevel@tonic-gate 		error = ENXIO;
8810Sstevel@tonic-gate 		break;
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	case M_NULL:
8840Sstevel@tonic-gate 		/*
8850Sstevel@tonic-gate 		 * Use seg_dev segment driver for /dev/null mapping.
8860Sstevel@tonic-gate 		 */
8870Sstevel@tonic-gate 		dev_a.mapfunc = mmmmap;
8880Sstevel@tonic-gate 		dev_a.dev = dev;
8890Sstevel@tonic-gate 		dev_a.offset = off;
8900Sstevel@tonic-gate 		dev_a.type = 0;		/* neither PRIVATE nor SHARED */
8910Sstevel@tonic-gate 		dev_a.prot = dev_a.maxprot = (uchar_t)PROT_NONE;
8920Sstevel@tonic-gate 		dev_a.hat_attr = 0;
8930Sstevel@tonic-gate 		dev_a.hat_flags = 0;
8940Sstevel@tonic-gate 		error = as_map(as, *addrp, len, segdev_create, &dev_a);
8950Sstevel@tonic-gate 		break;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	default:
8980Sstevel@tonic-gate 		error = ENXIO;
8990Sstevel@tonic-gate 	}
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	as_rangeunlock(as);
9020Sstevel@tonic-gate 	return (error);
9030Sstevel@tonic-gate }
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate static struct cb_ops mm_cb_ops = {
9060Sstevel@tonic-gate 	mmopen,			/* open */
9070Sstevel@tonic-gate 	nulldev,		/* close */
9080Sstevel@tonic-gate 	nodev,			/* strategy */
9090Sstevel@tonic-gate 	nodev,			/* print */
9100Sstevel@tonic-gate 	nodev,			/* dump */
9110Sstevel@tonic-gate 	mmread,			/* read */
9120Sstevel@tonic-gate 	mmwrite,		/* write */
9130Sstevel@tonic-gate 	mmioctl,		/* ioctl */
9140Sstevel@tonic-gate 	nodev,			/* devmap */
9150Sstevel@tonic-gate 	mmmmap,			/* mmap */
9160Sstevel@tonic-gate 	mmsegmap,		/* segmap */
9170Sstevel@tonic-gate 	mmchpoll,		/* poll */
9180Sstevel@tonic-gate 	mmpropop,		/* prop_op */
9190Sstevel@tonic-gate 	0,			/* streamtab  */
9200Sstevel@tonic-gate 	D_NEW | D_MP | D_64BIT | D_U64BIT
9210Sstevel@tonic-gate };
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate static struct dev_ops mm_ops = {
9240Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
9250Sstevel@tonic-gate 	0,			/* refcnt  */
9260Sstevel@tonic-gate 	mm_info,		/* get_dev_info */
9270Sstevel@tonic-gate 	nulldev,		/* identify */
9280Sstevel@tonic-gate 	nulldev,		/* probe */
9290Sstevel@tonic-gate 	mm_attach,		/* attach */
9300Sstevel@tonic-gate 	nodev,			/* detach */
9310Sstevel@tonic-gate 	nodev,			/* reset */
9320Sstevel@tonic-gate 	&mm_cb_ops,		/* driver operations */
9330Sstevel@tonic-gate 	(struct bus_ops *)0	/* bus operations */
9340Sstevel@tonic-gate };
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate static struct modldrv modldrv = {
9370Sstevel@tonic-gate 	&mod_driverops, "memory driver %I%", &mm_ops,
9380Sstevel@tonic-gate };
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate static struct modlinkage modlinkage = {
9410Sstevel@tonic-gate 	MODREV_1, &modldrv, NULL
9420Sstevel@tonic-gate };
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate int
9450Sstevel@tonic-gate _init(void)
9460Sstevel@tonic-gate {
9470Sstevel@tonic-gate 	return (mod_install(&modlinkage));
9480Sstevel@tonic-gate }
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate int
9510Sstevel@tonic-gate _info(struct modinfo *modinfop)
9520Sstevel@tonic-gate {
9530Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
9540Sstevel@tonic-gate }
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate int
9570Sstevel@tonic-gate _fini(void)
9580Sstevel@tonic-gate {
9590Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
9600Sstevel@tonic-gate }
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate static int
9630Sstevel@tonic-gate mm_kstat_update(kstat_t *ksp, int rw)
9640Sstevel@tonic-gate {
9650Sstevel@tonic-gate 	struct memlist *pmem;
9660Sstevel@tonic-gate 	uint_t count;
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
9690Sstevel@tonic-gate 		return (EACCES);
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	count = 0;
9720Sstevel@tonic-gate 	memlist_read_lock();
9730Sstevel@tonic-gate 	for (pmem = phys_install; pmem != NULL; pmem = pmem->next) {
9740Sstevel@tonic-gate 		count++;
9750Sstevel@tonic-gate 	}
9760Sstevel@tonic-gate 	memlist_read_unlock();
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate 	ksp->ks_ndata = count;
9790Sstevel@tonic-gate 	ksp->ks_data_size = count * 2 * sizeof (uint64_t);
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	return (0);
9820Sstevel@tonic-gate }
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate static int
9850Sstevel@tonic-gate mm_kstat_snapshot(kstat_t *ksp, void *buf, int rw)
9860Sstevel@tonic-gate {
9870Sstevel@tonic-gate 	struct memlist *pmem;
9880Sstevel@tonic-gate 	struct memunit {
9890Sstevel@tonic-gate 		uint64_t address;
9900Sstevel@tonic-gate 		uint64_t size;
9910Sstevel@tonic-gate 	} *kspmem;
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
9940Sstevel@tonic-gate 		return (EACCES);
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	ksp->ks_snaptime = gethrtime();
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	kspmem = (struct memunit *)buf;
9990Sstevel@tonic-gate 	memlist_read_lock();
10000Sstevel@tonic-gate 	for (pmem = phys_install; pmem != NULL; pmem = pmem->next, kspmem++) {
10010Sstevel@tonic-gate 		if ((caddr_t)kspmem >= (caddr_t)buf + ksp->ks_data_size)
10020Sstevel@tonic-gate 			break;
10030Sstevel@tonic-gate 		kspmem->address = pmem->address;
10040Sstevel@tonic-gate 		kspmem->size = pmem->size;
10050Sstevel@tonic-gate 	}
10060Sstevel@tonic-gate 	memlist_read_unlock();
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	return (0);
10090Sstevel@tonic-gate }
10101186Sayznaga 
10111186Sayznaga /*
10121186Sayznaga  * Read a mem_name_t from user-space and store it in the mem_name_t
10131186Sayznaga  * pointed to by the mem_name argument.
10141186Sayznaga  */
10151186Sayznaga static int
10161186Sayznaga mm_read_mem_name(intptr_t data, mem_name_t *mem_name)
10171186Sayznaga {
10181186Sayznaga 	if (get_udatamodel() == DATAMODEL_NATIVE) {
10191186Sayznaga 		if (copyin((void *)data, mem_name, sizeof (mem_name_t)))
10201186Sayznaga 			return (EFAULT);
10211186Sayznaga 	}
10221186Sayznaga #ifdef	_SYSCALL32
10231186Sayznaga 	else {
10241186Sayznaga 		mem_name32_t mem_name32;
10251186Sayznaga 
10261186Sayznaga 		if (copyin((void *)data, &mem_name32, sizeof (mem_name32_t)))
10271186Sayznaga 			return (EFAULT);
10281186Sayznaga 		mem_name->m_addr = mem_name32.m_addr;
10291186Sayznaga 		mem_name->m_synd = mem_name32.m_synd;
10301186Sayznaga 		mem_name->m_type[0] = mem_name32.m_type[0];
10311186Sayznaga 		mem_name->m_type[1] = mem_name32.m_type[1];
10321186Sayznaga 		mem_name->m_name = (caddr_t)mem_name32.m_name;
10331186Sayznaga 		mem_name->m_namelen = (size_t)mem_name32.m_namelen;
10341186Sayznaga 		mem_name->m_sid = (caddr_t)mem_name32.m_sid;
10351186Sayznaga 		mem_name->m_sidlen = (size_t)mem_name32.m_sidlen;
10361186Sayznaga 	}
10371186Sayznaga #endif	/* _SYSCALL32 */
10381186Sayznaga 
10391186Sayznaga 	return (0);
10401186Sayznaga }
10411186Sayznaga 
10421186Sayznaga /*
10431186Sayznaga  * Read a mem_page_t from user-space and store it in the mem_page_t
10441186Sayznaga  * pointed to by the mpage argument.
10451186Sayznaga  */
10461186Sayznaga static int
10471186Sayznaga mm_read_mem_page(intptr_t data, mem_page_t *mpage)
10481186Sayznaga {
10491186Sayznaga 	if (get_udatamodel() == DATAMODEL_NATIVE) {
10501186Sayznaga 		if (copyin((void *)data, mpage, sizeof (mem_page_t)) != 0)
10511186Sayznaga 			return (EFAULT);
10521186Sayznaga 	}
10531186Sayznaga #ifdef _SYSCALL32
10541186Sayznaga 	else {
10551186Sayznaga 		mem_page32_t	mpage32;
10561186Sayznaga 
10571186Sayznaga 		if (copyin((void *)data, &mpage32, sizeof (mem_page32_t)) != 0)
10581186Sayznaga 			return (EFAULT);
10591186Sayznaga 
10601186Sayznaga 		mpage->m_fmri = (caddr_t)(uintptr_t)mpage32.m_fmri;
10611186Sayznaga 		mpage->m_fmrisz = mpage32.m_fmrisz;
10621186Sayznaga 	}
10631186Sayznaga #endif	/* _SYSCALL32 */
10641186Sayznaga 
10651186Sayznaga 	return (0);
10661186Sayznaga }
10671186Sayznaga 
10681186Sayznaga /*
10691186Sayznaga  * Expand an FMRI from a mem_page_t.
10701186Sayznaga  */
10711186Sayznaga static int
10721186Sayznaga mm_get_mem_fmri(mem_page_t *mpage, nvlist_t **nvl)
10731186Sayznaga {
10741186Sayznaga 	char *buf;
10751186Sayznaga 	int err;
10761186Sayznaga 
10771186Sayznaga 	if (mpage->m_fmri == NULL || mpage->m_fmrisz > MEM_FMRI_MAX_BUFSIZE)
10781186Sayznaga 		return (EINVAL);
10791186Sayznaga 
10801186Sayznaga 	buf = kmem_alloc(mpage->m_fmrisz, KM_SLEEP);
10811186Sayznaga 	if (copyin(mpage->m_fmri, buf, mpage->m_fmrisz) != 0) {
10821186Sayznaga 		kmem_free(buf, mpage->m_fmrisz);
10831186Sayznaga 		return (EFAULT);
10841186Sayznaga 	}
10851186Sayznaga 
10861186Sayznaga 	err = nvlist_unpack(buf, mpage->m_fmrisz, nvl, KM_SLEEP);
10871186Sayznaga 	kmem_free(buf, mpage->m_fmrisz);
10881186Sayznaga 
10891186Sayznaga 	return (err);
10901186Sayznaga }
10911186Sayznaga 
10921186Sayznaga static int
10931186Sayznaga mm_get_paddr(nvlist_t *nvl, uint64_t *paddr)
10941186Sayznaga {
10951186Sayznaga 	uint8_t version;
1096*1189Sayznaga 	uint64_t pa;
10971186Sayznaga 	char *scheme;
1098*1189Sayznaga #ifdef __sparc
1099*1189Sayznaga 	uint64_t offset;
11001186Sayznaga 	char *unum;
11011186Sayznaga 	char **serids;
11021186Sayznaga 	uint_t nserids;
11031186Sayznaga 	int err;
1104*1189Sayznaga #endif
11051186Sayznaga 
11061186Sayznaga 	/* Verify FMRI scheme name and version number */
11071186Sayznaga 	if ((nvlist_lookup_string(nvl, FM_FMRI_SCHEME, &scheme) != 0) ||
11081186Sayznaga 	    (strcmp(scheme, FM_FMRI_SCHEME_MEM) != 0) ||
11091186Sayznaga 	    (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0) ||
11101186Sayznaga 	    version > FM_MEM_SCHEME_VERSION) {
11111186Sayznaga 		return (EINVAL);
11121186Sayznaga 	}
11131186Sayznaga 
11141186Sayznaga 	/*
11151186Sayznaga 	 * There are two ways a physical address can be  obtained from a mem
11161186Sayznaga 	 * scheme FMRI.  One way is to use the "offset" and  "serial"
11171186Sayznaga 	 * members, if they are present, together with the "unum" member to
11181186Sayznaga 	 * calculate a physical address.  This is the preferred way since
11191186Sayznaga 	 * it is independent of possible changes to the programming of
11201186Sayznaga 	 * underlying hardware registers that may change the physical address.
11211186Sayznaga 	 * If the "offset" member is not present, then the address is
11221186Sayznaga 	 * retrieved from the "physaddr" member.
11231186Sayznaga 	 */
11241187Sayznaga #ifdef __sparc
11251186Sayznaga 	if (nvlist_lookup_uint64(nvl, FM_FMRI_MEM_OFFSET, &offset) != 0) {
11261186Sayznaga 		if (nvlist_lookup_uint64(nvl, FM_FMRI_MEM_PHYSADDR, &pa) !=
11271186Sayznaga 		    0) {
11281186Sayznaga 			return (EINVAL);
11291186Sayznaga 		}
11301186Sayznaga 	} else if (nvlist_lookup_string(nvl, FM_FMRI_MEM_UNUM, &unum) != 0 ||
11311186Sayznaga 	    nvlist_lookup_string_array(nvl, FM_FMRI_MEM_SERIAL_ID, &serids,
11321186Sayznaga 	    &nserids) != 0) {
11331186Sayznaga 		return (EINVAL);
11341186Sayznaga 	} else {
11351186Sayznaga 		if ((err = cpu_get_mem_addr(unum, serids[0], offset, &pa)) != 0)
11361186Sayznaga 			return (err);
11371186Sayznaga 	}
11381187Sayznaga #else /* __i386, __amd64 */
11391187Sayznaga 	if (nvlist_lookup_uint64(nvl, FM_FMRI_MEM_PHYSADDR, &pa) != 0)
11401187Sayznaga 		return (EINVAL);
11411187Sayznaga #endif /* __sparc */
11421186Sayznaga 
11431186Sayznaga 	*paddr = pa;
11441186Sayznaga 	return (0);
11451186Sayznaga }
1146