xref: /onnv-gate/usr/src/uts/common/io/mem.c (revision 11474:857f9db4ef05)
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
51463Sayznaga  * Common Development and Distribution License (the "License").
61463Sayznaga  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*11474SJonathan.Adams@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Memory special file
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/user.h>
330Sstevel@tonic-gate #include <sys/buf.h>
340Sstevel@tonic-gate #include <sys/systm.h>
350Sstevel@tonic-gate #include <sys/cred.h>
360Sstevel@tonic-gate #include <sys/vm.h>
370Sstevel@tonic-gate #include <sys/uio.h>
380Sstevel@tonic-gate #include <sys/mman.h>
390Sstevel@tonic-gate #include <sys/kmem.h>
400Sstevel@tonic-gate #include <vm/seg.h>
410Sstevel@tonic-gate #include <vm/page.h>
420Sstevel@tonic-gate #include <sys/stat.h>
430Sstevel@tonic-gate #include <sys/vmem.h>
440Sstevel@tonic-gate #include <sys/memlist.h>
450Sstevel@tonic-gate #include <sys/bootconf.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include <vm/seg_vn.h>
480Sstevel@tonic-gate #include <vm/seg_dev.h>
490Sstevel@tonic-gate #include <vm/seg_kmem.h>
500Sstevel@tonic-gate #include <vm/seg_kp.h>
510Sstevel@tonic-gate #include <vm/seg_kpm.h>
520Sstevel@tonic-gate #include <vm/hat.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #include <sys/conf.h>
550Sstevel@tonic-gate #include <sys/mem.h>
560Sstevel@tonic-gate #include <sys/types.h>
570Sstevel@tonic-gate #include <sys/conf.h>
580Sstevel@tonic-gate #include <sys/param.h>
590Sstevel@tonic-gate #include <sys/systm.h>
600Sstevel@tonic-gate #include <sys/errno.h>
610Sstevel@tonic-gate #include <sys/modctl.h>
620Sstevel@tonic-gate #include <sys/memlist.h>
630Sstevel@tonic-gate #include <sys/ddi.h>
640Sstevel@tonic-gate #include <sys/sunddi.h>
650Sstevel@tonic-gate #include <sys/debug.h>
661186Sayznaga #include <sys/fm/protocol.h>
670Sstevel@tonic-gate 
681414Scindi #if defined(__sparc)
690Sstevel@tonic-gate extern int cpu_get_mem_name(uint64_t, uint64_t *, uint64_t, char *, int, int *);
700Sstevel@tonic-gate extern int cpu_get_mem_info(uint64_t, uint64_t, uint64_t *, uint64_t *,
710Sstevel@tonic-gate     uint64_t *, int *, int *, int *);
720Sstevel@tonic-gate extern size_t cpu_get_name_bufsize(void);
731186Sayznaga extern int cpu_get_mem_sid(char *, char *, int, int *);
741186Sayznaga extern int cpu_get_mem_addr(char *, char *, uint64_t, uint64_t *);
753446Smrj #elif defined(__x86)
761414Scindi #include <sys/cpu_module.h>
771186Sayznaga #endif	/* __sparc */
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * Turn a byte length into a pagecount.  The DDI btop takes a
810Sstevel@tonic-gate  * 32-bit size on 32-bit machines, this handles 64-bit sizes for
820Sstevel@tonic-gate  * large physical-memory 32-bit machines.
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate #define	BTOP(x)	((pgcnt_t)((x) >> _pageshift))
850Sstevel@tonic-gate 
860Sstevel@tonic-gate static kmutex_t mm_lock;
870Sstevel@tonic-gate static caddr_t mm_map;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static dev_info_t *mm_dip;	/* private copy of devinfo pointer */
900Sstevel@tonic-gate 
910Sstevel@tonic-gate static int mm_kmem_io_access;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate static int mm_kstat_update(kstat_t *ksp, int rw);
940Sstevel@tonic-gate static int mm_kstat_snapshot(kstat_t *ksp, void *buf, int rw);
950Sstevel@tonic-gate 
961186Sayznaga static int mm_read_mem_name(intptr_t data, mem_name_t *mem_name);
971186Sayznaga 
980Sstevel@tonic-gate /*ARGSUSED1*/
990Sstevel@tonic-gate static int
mm_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)1000Sstevel@tonic-gate mm_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	int i;
1030Sstevel@tonic-gate 	struct mem_minor {
1040Sstevel@tonic-gate 		char *name;
1050Sstevel@tonic-gate 		minor_t minor;
1060Sstevel@tonic-gate 		int privonly;
1070Sstevel@tonic-gate 		const char *rdpriv;
1080Sstevel@tonic-gate 		const char *wrpriv;
1090Sstevel@tonic-gate 		mode_t priv_mode;
1100Sstevel@tonic-gate 	} mm[] = {
1110Sstevel@tonic-gate 		{ "mem",	M_MEM,		0,	NULL,	"all",	0640 },
1120Sstevel@tonic-gate 		{ "kmem",	M_KMEM,		0,	NULL,	"all",	0640 },
1130Sstevel@tonic-gate 		{ "allkmem",	M_ALLKMEM,	0,	"all",	"all",	0600 },
1140Sstevel@tonic-gate 		{ "null",	M_NULL,	PRIVONLY_DEV,	NULL,	NULL,	0666 },
1150Sstevel@tonic-gate 		{ "zero",	M_ZERO, PRIVONLY_DEV,	NULL,	NULL,	0666 },
1160Sstevel@tonic-gate 	};
1170Sstevel@tonic-gate 	kstat_t *ksp;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	mutex_init(&mm_lock, NULL, MUTEX_DEFAULT, NULL);
1200Sstevel@tonic-gate 	mm_map = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	for (i = 0; i < (sizeof (mm) / sizeof (mm[0])); i++) {
1230Sstevel@tonic-gate 		if (ddi_create_priv_minor_node(devi, mm[i].name, S_IFCHR,
1240Sstevel@tonic-gate 		    mm[i].minor, DDI_PSEUDO, mm[i].privonly,
1250Sstevel@tonic-gate 		    mm[i].rdpriv, mm[i].wrpriv, mm[i].priv_mode) ==
1260Sstevel@tonic-gate 		    DDI_FAILURE) {
1270Sstevel@tonic-gate 			ddi_remove_minor_node(devi, NULL);
1280Sstevel@tonic-gate 			return (DDI_FAILURE);
1290Sstevel@tonic-gate 		}
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	mm_dip = devi;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	ksp = kstat_create("mm", 0, "phys_installed", "misc",
1350Sstevel@tonic-gate 	    KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VAR_SIZE | KSTAT_FLAG_VIRTUAL);
1360Sstevel@tonic-gate 	if (ksp != NULL) {
1370Sstevel@tonic-gate 		ksp->ks_update = mm_kstat_update;
1380Sstevel@tonic-gate 		ksp->ks_snapshot = mm_kstat_snapshot;
1390Sstevel@tonic-gate 		ksp->ks_lock = &mm_lock; /* XXX - not really needed */
1400Sstevel@tonic-gate 		kstat_install(ksp);
1410Sstevel@tonic-gate 	}
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	mm_kmem_io_access = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
1440Sstevel@tonic-gate 	    "kmem_io_access", 0);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	return (DDI_SUCCESS);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate /*ARGSUSED*/
1500Sstevel@tonic-gate static int
mm_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)1510Sstevel@tonic-gate mm_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	register int error;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	switch (infocmd) {
1560Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
1570Sstevel@tonic-gate 		*result = (void *)mm_dip;
1580Sstevel@tonic-gate 		error = DDI_SUCCESS;
1590Sstevel@tonic-gate 		break;
1600Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
1610Sstevel@tonic-gate 		*result = (void *)0;
1620Sstevel@tonic-gate 		error = DDI_SUCCESS;
1630Sstevel@tonic-gate 		break;
1640Sstevel@tonic-gate 	default:
1650Sstevel@tonic-gate 		error = DDI_FAILURE;
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 	return (error);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate /*ARGSUSED1*/
1710Sstevel@tonic-gate static int
mmopen(dev_t * devp,int flag,int typ,struct cred * cred)1720Sstevel@tonic-gate mmopen(dev_t *devp, int flag, int typ, struct cred *cred)
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate 	switch (getminor(*devp)) {
1750Sstevel@tonic-gate 	case M_NULL:
1760Sstevel@tonic-gate 	case M_ZERO:
1770Sstevel@tonic-gate 	case M_MEM:
1780Sstevel@tonic-gate 	case M_KMEM:
1790Sstevel@tonic-gate 	case M_ALLKMEM:
1800Sstevel@tonic-gate 		/* standard devices */
1810Sstevel@tonic-gate 		break;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	default:
1840Sstevel@tonic-gate 		/* Unsupported or unknown type */
1850Sstevel@tonic-gate 		return (EINVAL);
1860Sstevel@tonic-gate 	}
1876731Scth 	/* must be character device */
1886731Scth 	if (typ != OTYP_CHR)
1896731Scth 		return (EINVAL);
1900Sstevel@tonic-gate 	return (0);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate struct pollhead	mm_pollhd;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate /*ARGSUSED*/
1960Sstevel@tonic-gate static int
mmchpoll(dev_t dev,short events,int anyyet,short * reventsp,struct pollhead ** phpp)1970Sstevel@tonic-gate mmchpoll(dev_t dev, short events, int anyyet, short *reventsp,
1980Sstevel@tonic-gate     struct pollhead **phpp)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate 	switch (getminor(dev)) {
2010Sstevel@tonic-gate 	case M_NULL:
2020Sstevel@tonic-gate 	case M_ZERO:
2030Sstevel@tonic-gate 	case M_MEM:
2040Sstevel@tonic-gate 	case M_KMEM:
2050Sstevel@tonic-gate 	case M_ALLKMEM:
2060Sstevel@tonic-gate 		*reventsp = events & (POLLIN | POLLOUT | POLLPRI | POLLRDNORM |
2074374Smb91622 		    POLLWRNORM | POLLRDBAND | POLLWRBAND);
2080Sstevel@tonic-gate 		/*
2090Sstevel@tonic-gate 		 * A non NULL pollhead pointer should be returned in case
2100Sstevel@tonic-gate 		 * user polls for 0 events.
2110Sstevel@tonic-gate 		 */
2120Sstevel@tonic-gate 		*phpp = !anyyet && !*reventsp ?
2130Sstevel@tonic-gate 		    &mm_pollhd : (struct pollhead *)NULL;
2140Sstevel@tonic-gate 		return (0);
2150Sstevel@tonic-gate 	default:
2160Sstevel@tonic-gate 		/* no other devices currently support polling */
2170Sstevel@tonic-gate 		return (ENXIO);
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate static int
mmpropop(dev_t dev,dev_info_t * dip,ddi_prop_op_t prop_op,int flags,char * name,caddr_t valuep,int * lengthp)2220Sstevel@tonic-gate mmpropop(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int flags,
2230Sstevel@tonic-gate     char *name, caddr_t valuep, int *lengthp)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate 	/*
2260Sstevel@tonic-gate 	 * implement zero size to reduce overhead (avoid two failing
2270Sstevel@tonic-gate 	 * property lookups per stat).
2280Sstevel@tonic-gate 	 */
2290Sstevel@tonic-gate 	return (ddi_prop_op_size(dev, dip, prop_op,
2300Sstevel@tonic-gate 	    flags, name, valuep, lengthp, 0));
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate static int
mmio(struct uio * uio,enum uio_rw rw,pfn_t pfn,off_t pageoff,int allowio,page_t * pp)2349894SPavel.Tatashin@Sun.COM mmio(struct uio *uio, enum uio_rw rw, pfn_t pfn, off_t pageoff, int allowio,
2359894SPavel.Tatashin@Sun.COM     page_t *pp)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate 	int error = 0;
2389894SPavel.Tatashin@Sun.COM 	int devload = 0;
2399894SPavel.Tatashin@Sun.COM 	int is_memory = pf_is_memory(pfn);
2400Sstevel@tonic-gate 	size_t nbytes = MIN((size_t)(PAGESIZE - pageoff),
2410Sstevel@tonic-gate 	    (size_t)uio->uio_iov->iov_len);
2429894SPavel.Tatashin@Sun.COM 	caddr_t va = NULL;
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	mutex_enter(&mm_lock);
2459894SPavel.Tatashin@Sun.COM 
2469894SPavel.Tatashin@Sun.COM 	if (is_memory && kpm_enable) {
2479894SPavel.Tatashin@Sun.COM 		if (pp)
2489894SPavel.Tatashin@Sun.COM 			va = hat_kpm_mapin(pp, NULL);
2499894SPavel.Tatashin@Sun.COM 		else
2509894SPavel.Tatashin@Sun.COM 			va = hat_kpm_mapin_pfn(pfn);
2519894SPavel.Tatashin@Sun.COM 	}
2520Sstevel@tonic-gate 
2539894SPavel.Tatashin@Sun.COM 	if (va == NULL) {
2549894SPavel.Tatashin@Sun.COM 		hat_devload(kas.a_hat, mm_map, PAGESIZE, pfn,
2559894SPavel.Tatashin@Sun.COM 		    (uint_t)(rw == UIO_READ ? PROT_READ : PROT_READ|PROT_WRITE),
2569894SPavel.Tatashin@Sun.COM 		    HAT_LOAD_NOCONSIST|HAT_LOAD_LOCK);
2579894SPavel.Tatashin@Sun.COM 		va = mm_map;
2589894SPavel.Tatashin@Sun.COM 		devload = 1;
2599894SPavel.Tatashin@Sun.COM 	}
2609894SPavel.Tatashin@Sun.COM 
2619894SPavel.Tatashin@Sun.COM 	if (!is_memory) {
2620Sstevel@tonic-gate 		if (allowio) {
2630Sstevel@tonic-gate 			size_t c = uio->uio_iov->iov_len;
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 			if (ddi_peekpokeio(NULL, uio, rw,
2660Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)uio->uio_loffset, c,
2670Sstevel@tonic-gate 			    sizeof (int32_t)) != DDI_SUCCESS)
2680Sstevel@tonic-gate 				error = EFAULT;
2690Sstevel@tonic-gate 		} else
2700Sstevel@tonic-gate 			error = EIO;
27110271SJason.Beloro@Sun.COM 	} else
2729894SPavel.Tatashin@Sun.COM 		error = uiomove(va + pageoff, nbytes, rw, uio);
2730Sstevel@tonic-gate 
2749894SPavel.Tatashin@Sun.COM 	if (devload)
2759894SPavel.Tatashin@Sun.COM 		hat_unload(kas.a_hat, mm_map, PAGESIZE, HAT_UNLOAD_UNLOCK);
2769894SPavel.Tatashin@Sun.COM 	else if (pp)
2779894SPavel.Tatashin@Sun.COM 		hat_kpm_mapout(pp, NULL, va);
2789894SPavel.Tatashin@Sun.COM 	else
2799894SPavel.Tatashin@Sun.COM 		hat_kpm_mapout_pfn(pfn);
2809894SPavel.Tatashin@Sun.COM 
2810Sstevel@tonic-gate 	mutex_exit(&mm_lock);
2820Sstevel@tonic-gate 	return (error);
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate 
285670Selowe static int
mmpagelock(struct as * as,caddr_t va)286670Selowe mmpagelock(struct as *as, caddr_t va)
287670Selowe {
288670Selowe 	struct seg *seg;
289670Selowe 	int i;
290670Selowe 
291670Selowe 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
292670Selowe 	seg = as_segat(as, va);
293670Selowe 	i = (seg != NULL)? SEGOP_CAPABLE(seg, S_CAPABILITY_NOMINFLT) : 0;
294670Selowe 	AS_LOCK_EXIT(as, &as->a_lock);
295670Selowe 
296670Selowe 	return (i);
297670Selowe }
298670Selowe 
2995084Sjohnlev #ifdef	__sparc
3005084Sjohnlev 
301670Selowe #define	NEED_LOCK_KVADDR(kva)	mmpagelock(&kas, kva)
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate #else	/* __i386, __amd64 */
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate #define	NEED_LOCK_KVADDR(va)	0
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate #endif	/* __sparc */
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate /*ARGSUSED3*/
3100Sstevel@tonic-gate static int
mmrw(dev_t dev,struct uio * uio,enum uio_rw rw,cred_t * cred)3110Sstevel@tonic-gate mmrw(dev_t dev, struct uio *uio, enum uio_rw rw, cred_t *cred)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate 	pfn_t v;
3140Sstevel@tonic-gate 	struct iovec *iov;
3150Sstevel@tonic-gate 	int error = 0;
3160Sstevel@tonic-gate 	size_t c;
3170Sstevel@tonic-gate 	ssize_t oresid = uio->uio_resid;
3180Sstevel@tonic-gate 	minor_t minor = getminor(dev);
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	while (uio->uio_resid > 0 && error == 0) {
3210Sstevel@tonic-gate 		iov = uio->uio_iov;
3220Sstevel@tonic-gate 		if (iov->iov_len == 0) {
3230Sstevel@tonic-gate 			uio->uio_iov++;
3240Sstevel@tonic-gate 			uio->uio_iovcnt--;
3250Sstevel@tonic-gate 			if (uio->uio_iovcnt < 0)
3260Sstevel@tonic-gate 				panic("mmrw");
3270Sstevel@tonic-gate 			continue;
3280Sstevel@tonic-gate 		}
3290Sstevel@tonic-gate 		switch (minor) {
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 		case M_MEM:
3320Sstevel@tonic-gate 			memlist_read_lock();
3330Sstevel@tonic-gate 			if (!address_in_memlist(phys_install,
3340Sstevel@tonic-gate 			    (uint64_t)uio->uio_loffset, 1)) {
3350Sstevel@tonic-gate 				memlist_read_unlock();
3360Sstevel@tonic-gate 				error = EFAULT;
3370Sstevel@tonic-gate 				break;
3380Sstevel@tonic-gate 			}
3390Sstevel@tonic-gate 			memlist_read_unlock();
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 			v = BTOP((u_offset_t)uio->uio_loffset);
3420Sstevel@tonic-gate 			error = mmio(uio, rw, v,
3439894SPavel.Tatashin@Sun.COM 			    uio->uio_loffset & PAGEOFFSET, 0, NULL);
3440Sstevel@tonic-gate 			break;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 		case M_KMEM:
3470Sstevel@tonic-gate 		case M_ALLKMEM:
3480Sstevel@tonic-gate 			{
3499894SPavel.Tatashin@Sun.COM 			page_t **ppp = NULL;
3500Sstevel@tonic-gate 			caddr_t vaddr = (caddr_t)uio->uio_offset;
3510Sstevel@tonic-gate 			int try_lock = NEED_LOCK_KVADDR(vaddr);
3520Sstevel@tonic-gate 			int locked = 0;
3530Sstevel@tonic-gate 
3545084Sjohnlev 			if ((error = plat_mem_do_mmio(uio, rw)) != ENOTSUP)
3553446Smrj 				break;
3563446Smrj 
3570Sstevel@tonic-gate 			/*
3580Sstevel@tonic-gate 			 * If vaddr does not map a valid page, as_pagelock()
3590Sstevel@tonic-gate 			 * will return failure. Hence we can't check the
3600Sstevel@tonic-gate 			 * return value and return EFAULT here as we'd like.
3610Sstevel@tonic-gate 			 * seg_kp and seg_kpm do not properly support
3620Sstevel@tonic-gate 			 * as_pagelock() for this context so we avoid it
3630Sstevel@tonic-gate 			 * using the try_lock set check above.  Some day when
3640Sstevel@tonic-gate 			 * the kernel page locking gets redesigned all this
3650Sstevel@tonic-gate 			 * muck can be cleaned up.
3660Sstevel@tonic-gate 			 */
3670Sstevel@tonic-gate 			if (try_lock)
3680Sstevel@tonic-gate 				locked = (as_pagelock(&kas, &ppp, vaddr,
3690Sstevel@tonic-gate 				    PAGESIZE, S_WRITE) == 0);
3700Sstevel@tonic-gate 
371513Sjongkis 			v = hat_getpfnum(kas.a_hat,
372513Sjongkis 			    (caddr_t)(uintptr_t)uio->uio_loffset);
3730Sstevel@tonic-gate 			if (v == PFN_INVALID) {
3740Sstevel@tonic-gate 				if (locked)
3750Sstevel@tonic-gate 					as_pageunlock(&kas, ppp, vaddr,
3760Sstevel@tonic-gate 					    PAGESIZE, S_WRITE);
3770Sstevel@tonic-gate 				error = EFAULT;
3780Sstevel@tonic-gate 				break;
3790Sstevel@tonic-gate 			}
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 			error = mmio(uio, rw, v, uio->uio_loffset & PAGEOFFSET,
3829894SPavel.Tatashin@Sun.COM 			    minor == M_ALLKMEM || mm_kmem_io_access,
3839894SPavel.Tatashin@Sun.COM 			    (locked && ppp) ? *ppp : NULL);
3840Sstevel@tonic-gate 			if (locked)
3850Sstevel@tonic-gate 				as_pageunlock(&kas, ppp, vaddr, PAGESIZE,
3860Sstevel@tonic-gate 				    S_WRITE);
3870Sstevel@tonic-gate 			}
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 			break;
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 		case M_ZERO:
3920Sstevel@tonic-gate 			if (rw == UIO_READ) {
3930Sstevel@tonic-gate 				label_t ljb;
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 				if (on_fault(&ljb)) {
3960Sstevel@tonic-gate 					no_fault();
3970Sstevel@tonic-gate 					error = EFAULT;
3980Sstevel@tonic-gate 					break;
3990Sstevel@tonic-gate 				}
4000Sstevel@tonic-gate 				uzero(iov->iov_base, iov->iov_len);
4010Sstevel@tonic-gate 				no_fault();
4020Sstevel@tonic-gate 				uio->uio_resid -= iov->iov_len;
4030Sstevel@tonic-gate 				uio->uio_loffset += iov->iov_len;
4040Sstevel@tonic-gate 				break;
4050Sstevel@tonic-gate 			}
4060Sstevel@tonic-gate 			/* else it's a write, fall through to NULL case */
4070Sstevel@tonic-gate 			/*FALLTHROUGH*/
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 		case M_NULL:
4100Sstevel@tonic-gate 			if (rw == UIO_READ)
4110Sstevel@tonic-gate 				return (0);
4120Sstevel@tonic-gate 			c = iov->iov_len;
4130Sstevel@tonic-gate 			iov->iov_base += c;
4140Sstevel@tonic-gate 			iov->iov_len -= c;
4150Sstevel@tonic-gate 			uio->uio_loffset += c;
4160Sstevel@tonic-gate 			uio->uio_resid -= c;
4170Sstevel@tonic-gate 			break;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 		}
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 	return (uio->uio_resid == oresid ? error : 0);
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate static int
mmread(dev_t dev,struct uio * uio,cred_t * cred)4250Sstevel@tonic-gate mmread(dev_t dev, struct uio *uio, cred_t *cred)
4260Sstevel@tonic-gate {
4270Sstevel@tonic-gate 	return (mmrw(dev, uio, UIO_READ, cred));
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate static int
mmwrite(dev_t dev,struct uio * uio,cred_t * cred)4310Sstevel@tonic-gate mmwrite(dev_t dev, struct uio *uio, cred_t *cred)
4320Sstevel@tonic-gate {
4330Sstevel@tonic-gate 	return (mmrw(dev, uio, UIO_WRITE, cred));
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate /*
4370Sstevel@tonic-gate  * Private ioctl for libkvm to support kvm_physaddr().
4380Sstevel@tonic-gate  * Given an address space and a VA, compute the PA.
4390Sstevel@tonic-gate  */
4400Sstevel@tonic-gate static int
mmioctl_vtop(intptr_t data)4410Sstevel@tonic-gate mmioctl_vtop(intptr_t data)
4420Sstevel@tonic-gate {
4431414Scindi #ifdef _SYSCALL32
4441414Scindi 	mem_vtop32_t vtop32;
4451414Scindi #endif
4460Sstevel@tonic-gate 	mem_vtop_t mem_vtop;
4470Sstevel@tonic-gate 	proc_t *p;
4480Sstevel@tonic-gate 	pfn_t pfn = (pfn_t)PFN_INVALID;
4490Sstevel@tonic-gate 	pid_t pid = 0;
4500Sstevel@tonic-gate 	struct as *as;
4510Sstevel@tonic-gate 	struct seg *seg;
4520Sstevel@tonic-gate 
4531414Scindi 	if (get_udatamodel() == DATAMODEL_NATIVE) {
4541414Scindi 		if (copyin((void *)data, &mem_vtop, sizeof (mem_vtop_t)))
4551414Scindi 			return (EFAULT);
4561414Scindi 	}
4571414Scindi #ifdef _SYSCALL32
4581414Scindi 	else {
4591414Scindi 		if (copyin((void *)data, &vtop32, sizeof (mem_vtop32_t)))
4601414Scindi 			return (EFAULT);
4611717Swesolows 		mem_vtop.m_as = (struct as *)(uintptr_t)vtop32.m_as;
4621717Swesolows 		mem_vtop.m_va = (void *)(uintptr_t)vtop32.m_va;
4631414Scindi 
4641414Scindi 		if (mem_vtop.m_as != NULL)
4651414Scindi 			return (EINVAL);
4661414Scindi 	}
4671414Scindi #endif
4681414Scindi 
4690Sstevel@tonic-gate 	if (mem_vtop.m_as == &kas) {
4700Sstevel@tonic-gate 		pfn = hat_getpfnum(kas.a_hat, mem_vtop.m_va);
4710Sstevel@tonic-gate 	} else {
4721414Scindi 		if (mem_vtop.m_as == NULL) {
4731414Scindi 			/*
4741414Scindi 			 * Assume the calling process's address space if the
4751414Scindi 			 * caller didn't specify one.
4761414Scindi 			 */
4771414Scindi 			p = curthread->t_procp;
4781414Scindi 			if (p == NULL)
4791414Scindi 				return (EIO);
4801414Scindi 			mem_vtop.m_as = p->p_as;
4811414Scindi 		}
4821414Scindi 
4830Sstevel@tonic-gate 		mutex_enter(&pidlock);
4840Sstevel@tonic-gate 		for (p = practive; p != NULL; p = p->p_next) {
4850Sstevel@tonic-gate 			if (p->p_as == mem_vtop.m_as) {
4860Sstevel@tonic-gate 				pid = p->p_pid;
4870Sstevel@tonic-gate 				break;
4880Sstevel@tonic-gate 			}
4890Sstevel@tonic-gate 		}
4900Sstevel@tonic-gate 		mutex_exit(&pidlock);
4910Sstevel@tonic-gate 		if (p == NULL)
4920Sstevel@tonic-gate 			return (EIO);
4930Sstevel@tonic-gate 		p = sprlock(pid);
4940Sstevel@tonic-gate 		if (p == NULL)
4950Sstevel@tonic-gate 			return (EIO);
4960Sstevel@tonic-gate 		as = p->p_as;
4970Sstevel@tonic-gate 		if (as == mem_vtop.m_as) {
4980Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
4990Sstevel@tonic-gate 			AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
5000Sstevel@tonic-gate 			for (seg = AS_SEGFIRST(as); seg != NULL;
5010Sstevel@tonic-gate 			    seg = AS_SEGNEXT(as, seg))
5020Sstevel@tonic-gate 				if ((uintptr_t)mem_vtop.m_va -
5030Sstevel@tonic-gate 				    (uintptr_t)seg->s_base < seg->s_size)
5040Sstevel@tonic-gate 					break;
5050Sstevel@tonic-gate 			if (seg != NULL)
5060Sstevel@tonic-gate 				pfn = hat_getpfnum(as->a_hat, mem_vtop.m_va);
5070Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
5080Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
5090Sstevel@tonic-gate 		}
5100Sstevel@tonic-gate 		sprunlock(p);
5110Sstevel@tonic-gate 	}
5120Sstevel@tonic-gate 	mem_vtop.m_pfn = pfn;
5130Sstevel@tonic-gate 	if (pfn == PFN_INVALID)
5140Sstevel@tonic-gate 		return (EIO);
5151414Scindi 
5161414Scindi 	if (get_udatamodel() == DATAMODEL_NATIVE) {
5171414Scindi 		if (copyout(&mem_vtop, (void *)data, sizeof (mem_vtop_t)))
5181414Scindi 			return (EFAULT);
5191414Scindi 	}
5201414Scindi #ifdef _SYSCALL32
5211414Scindi 	else {
5221414Scindi 		vtop32.m_pfn = mem_vtop.m_pfn;
5231414Scindi 		if (copyout(&vtop32, (void *)data, sizeof (mem_vtop32_t)))
5241414Scindi 			return (EFAULT);
5251414Scindi 	}
5261414Scindi #endif
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	return (0);
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate /*
532917Selowe  * Given a PA, execute the given page retire command on it.
5330Sstevel@tonic-gate  */
5340Sstevel@tonic-gate static int
mmioctl_page_retire(int cmd,intptr_t data)5350Sstevel@tonic-gate mmioctl_page_retire(int cmd, intptr_t data)
5360Sstevel@tonic-gate {
537917Selowe 	extern int page_retire_test(void);
5380Sstevel@tonic-gate 	uint64_t pa;
5390Sstevel@tonic-gate 
540917Selowe 	if (copyin((void *)data, &pa, sizeof (uint64_t))) {
541917Selowe 		return (EFAULT);
5420Sstevel@tonic-gate 	}
5430Sstevel@tonic-gate 
544917Selowe 	switch (cmd) {
545917Selowe 	case MEM_PAGE_ISRETIRED:
546917Selowe 		return (page_retire_check(pa, NULL));
547917Selowe 
548917Selowe 	case MEM_PAGE_UNRETIRE:
549917Selowe 		return (page_unretire(pa));
550917Selowe 
551917Selowe 	case MEM_PAGE_RETIRE:
552917Selowe 		return (page_retire(pa, PR_FMA));
553917Selowe 
554917Selowe 	case MEM_PAGE_RETIRE_MCE:
555917Selowe 		return (page_retire(pa, PR_MCE));
556917Selowe 
557917Selowe 	case MEM_PAGE_RETIRE_UE:
558917Selowe 		return (page_retire(pa, PR_UE));
5590Sstevel@tonic-gate 
560917Selowe 	case MEM_PAGE_GETERRORS:
561917Selowe 		{
562917Selowe 			uint64_t page_errors;
563917Selowe 			int rc = page_retire_check(pa, &page_errors);
564917Selowe 			if (copyout(&page_errors, (void *)data,
565917Selowe 			    sizeof (uint64_t))) {
566917Selowe 				return (EFAULT);
567917Selowe 			}
568917Selowe 			return (rc);
569917Selowe 		}
570917Selowe 
571917Selowe 	case MEM_PAGE_RETIRE_TEST:
572917Selowe 		return (page_retire_test());
573917Selowe 
574917Selowe 	}
575917Selowe 
576917Selowe 	return (EINVAL);
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate #ifdef __sparc
5800Sstevel@tonic-gate /*
5810Sstevel@tonic-gate  * Given a syndrome, syndrome type, and address return the
5820Sstevel@tonic-gate  * associated memory name in the provided data buffer.
5830Sstevel@tonic-gate  */
5840Sstevel@tonic-gate static int
mmioctl_get_mem_name(intptr_t data)5850Sstevel@tonic-gate mmioctl_get_mem_name(intptr_t data)
5860Sstevel@tonic-gate {
5870Sstevel@tonic-gate 	mem_name_t mem_name;
5880Sstevel@tonic-gate 	void *buf;
5890Sstevel@tonic-gate 	size_t bufsize;
5900Sstevel@tonic-gate 	int len, err;
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	if ((bufsize = cpu_get_name_bufsize()) == 0)
5930Sstevel@tonic-gate 		return (ENOTSUP);
5940Sstevel@tonic-gate 
5951186Sayznaga 	if ((err = mm_read_mem_name(data, &mem_name)) < 0)
5961186Sayznaga 		return (err);
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	buf = kmem_alloc(bufsize, KM_SLEEP);
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	/*
6010Sstevel@tonic-gate 	 * Call into cpu specific code to do the lookup.
6020Sstevel@tonic-gate 	 */
6030Sstevel@tonic-gate 	if ((err = cpu_get_mem_name(mem_name.m_synd, mem_name.m_type,
6040Sstevel@tonic-gate 	    mem_name.m_addr, buf, bufsize, &len)) != 0) {
6050Sstevel@tonic-gate 		kmem_free(buf, bufsize);
6060Sstevel@tonic-gate 		return (err);
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	if (len >= mem_name.m_namelen) {
6100Sstevel@tonic-gate 		kmem_free(buf, bufsize);
6116803Spothier 		return (ENOSPC);
6120Sstevel@tonic-gate 	}
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	if (copyoutstr(buf, (char *)mem_name.m_name,
6150Sstevel@tonic-gate 	    mem_name.m_namelen, NULL) != 0) {
6160Sstevel@tonic-gate 		kmem_free(buf, bufsize);
6170Sstevel@tonic-gate 		return (EFAULT);
6180Sstevel@tonic-gate 	}
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	kmem_free(buf, bufsize);
6210Sstevel@tonic-gate 	return (0);
6220Sstevel@tonic-gate }
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate /*
6250Sstevel@tonic-gate  * Given a syndrome and address return information about the associated memory.
6260Sstevel@tonic-gate  */
6270Sstevel@tonic-gate static int
mmioctl_get_mem_info(intptr_t data)6280Sstevel@tonic-gate mmioctl_get_mem_info(intptr_t data)
6290Sstevel@tonic-gate {
6300Sstevel@tonic-gate 	mem_info_t mem_info;
6310Sstevel@tonic-gate 	int err;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	if (copyin((void *)data, &mem_info, sizeof (mem_info_t)))
6340Sstevel@tonic-gate 		return (EFAULT);
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	if ((err = cpu_get_mem_info(mem_info.m_synd, mem_info.m_addr,
6370Sstevel@tonic-gate 	    &mem_info.m_mem_size, &mem_info.m_seg_size, &mem_info.m_bank_size,
6380Sstevel@tonic-gate 	    &mem_info.m_segments, &mem_info.m_banks, &mem_info.m_mcid)) != 0)
6390Sstevel@tonic-gate 		return (err);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	if (copyout(&mem_info, (void *)data, sizeof (mem_info_t)) != 0)
6420Sstevel@tonic-gate 		return (EFAULT);
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	return (0);
6450Sstevel@tonic-gate }
6461186Sayznaga 
6471186Sayznaga /*
6481186Sayznaga  * Given a memory name, return its associated serial id
6491186Sayznaga  */
6501186Sayznaga static int
mmioctl_get_mem_sid(intptr_t data)6511186Sayznaga mmioctl_get_mem_sid(intptr_t data)
6521186Sayznaga {
6531186Sayznaga 	mem_name_t mem_name;
6541186Sayznaga 	void *buf;
6551186Sayznaga 	void *name;
6561186Sayznaga 	size_t	name_len;
6571186Sayznaga 	size_t bufsize;
6581186Sayznaga 	int len, err;
6591186Sayznaga 
6601186Sayznaga 	if ((bufsize = cpu_get_name_bufsize()) == 0)
6611186Sayznaga 		return (ENOTSUP);
6621186Sayznaga 
6631186Sayznaga 	if ((err = mm_read_mem_name(data, &mem_name)) < 0)
6641186Sayznaga 		return (err);
6651186Sayznaga 
6661186Sayznaga 	buf = kmem_alloc(bufsize, KM_SLEEP);
6671186Sayznaga 
6681186Sayznaga 	if (mem_name.m_namelen > 1024)
6691186Sayznaga 		mem_name.m_namelen = 1024; /* cap at 1024 bytes */
6701186Sayznaga 
6711186Sayznaga 	name = kmem_alloc(mem_name.m_namelen, KM_SLEEP);
6721186Sayznaga 
6731186Sayznaga 	if ((err = copyinstr((char *)mem_name.m_name, (char *)name,
6741186Sayznaga 	    mem_name.m_namelen, &name_len)) != 0) {
6751186Sayznaga 		kmem_free(buf, bufsize);
6761186Sayznaga 		kmem_free(name, mem_name.m_namelen);
6771186Sayznaga 		return (err);
6781186Sayznaga 	}
6791186Sayznaga 
6801186Sayznaga 	/*
6811186Sayznaga 	 * Call into cpu specific code to do the lookup.
6821186Sayznaga 	 */
6831186Sayznaga 	if ((err = cpu_get_mem_sid(name, buf, bufsize, &len)) != 0) {
6841186Sayznaga 		kmem_free(buf, bufsize);
6851186Sayznaga 		kmem_free(name, mem_name.m_namelen);
6861186Sayznaga 		return (err);
6871186Sayznaga 	}
6881186Sayznaga 
6891186Sayznaga 	if (len > mem_name.m_sidlen) {
6901186Sayznaga 		kmem_free(buf, bufsize);
6911186Sayznaga 		kmem_free(name, mem_name.m_namelen);
6921186Sayznaga 		return (ENAMETOOLONG);
6931186Sayznaga 	}
6941186Sayznaga 
6951186Sayznaga 	if (copyoutstr(buf, (char *)mem_name.m_sid,
6961186Sayznaga 	    mem_name.m_sidlen, NULL) != 0) {
6971186Sayznaga 		kmem_free(buf, bufsize);
6981186Sayznaga 		kmem_free(name, mem_name.m_namelen);
6991186Sayznaga 		return (EFAULT);
7001186Sayznaga 	}
7011186Sayznaga 
7021186Sayznaga 	kmem_free(buf, bufsize);
7031186Sayznaga 	kmem_free(name, mem_name.m_namelen);
7041186Sayznaga 	return (0);
7051186Sayznaga }
7060Sstevel@tonic-gate #endif	/* __sparc */
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate /*
7090Sstevel@tonic-gate  * Private ioctls for
7100Sstevel@tonic-gate  *	libkvm to support kvm_physaddr().
7110Sstevel@tonic-gate  *	FMA support for page_retire() and memory attribute information.
7120Sstevel@tonic-gate  */
7130Sstevel@tonic-gate /*ARGSUSED*/
7140Sstevel@tonic-gate static int
mmioctl(dev_t dev,int cmd,intptr_t data,int flag,cred_t * cred,int * rvalp)7150Sstevel@tonic-gate mmioctl(dev_t dev, int cmd, intptr_t data, int flag, cred_t *cred, int *rvalp)
7160Sstevel@tonic-gate {
7171283Sayznaga 	if ((cmd == MEM_VTOP && getminor(dev) != M_KMEM) ||
7181283Sayznaga 	    (cmd != MEM_VTOP && getminor(dev) != M_MEM))
7191186Sayznaga 		return (ENXIO);
7201186Sayznaga 
7210Sstevel@tonic-gate 	switch (cmd) {
7220Sstevel@tonic-gate 	case MEM_VTOP:
7230Sstevel@tonic-gate 		return (mmioctl_vtop(data));
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	case MEM_PAGE_RETIRE:
7260Sstevel@tonic-gate 	case MEM_PAGE_ISRETIRED:
727917Selowe 	case MEM_PAGE_UNRETIRE:
728917Selowe 	case MEM_PAGE_RETIRE_MCE:
729917Selowe 	case MEM_PAGE_RETIRE_UE:
730917Selowe 	case MEM_PAGE_GETERRORS:
731917Selowe 	case MEM_PAGE_RETIRE_TEST:
7320Sstevel@tonic-gate 		return (mmioctl_page_retire(cmd, data));
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate #ifdef __sparc
7351186Sayznaga 	case MEM_NAME:
7360Sstevel@tonic-gate 		return (mmioctl_get_mem_name(data));
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	case MEM_INFO:
7390Sstevel@tonic-gate 		return (mmioctl_get_mem_info(data));
7401186Sayznaga 
7411186Sayznaga 	case MEM_SID:
7421186Sayznaga 		return (mmioctl_get_mem_sid(data));
7430Sstevel@tonic-gate #else
7441186Sayznaga 	case MEM_NAME:
7451186Sayznaga 	case MEM_INFO:
7461186Sayznaga 	case MEM_SID:
7470Sstevel@tonic-gate 		return (ENOTSUP);
7481186Sayznaga #endif	/* __sparc */
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 	return (ENXIO);
7510Sstevel@tonic-gate }
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate /*ARGSUSED2*/
7540Sstevel@tonic-gate static int
mmmmap(dev_t dev,off_t off,int prot)7550Sstevel@tonic-gate mmmmap(dev_t dev, off_t off, int prot)
7560Sstevel@tonic-gate {
7570Sstevel@tonic-gate 	pfn_t pf;
7580Sstevel@tonic-gate 	struct memlist *pmem;
7590Sstevel@tonic-gate 	minor_t minor = getminor(dev);
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	switch (minor) {
7620Sstevel@tonic-gate 	case M_MEM:
7630Sstevel@tonic-gate 		pf = btop(off);
7640Sstevel@tonic-gate 		memlist_read_lock();
765*11474SJonathan.Adams@Sun.COM 		for (pmem = phys_install; pmem != NULL; pmem = pmem->ml_next) {
766*11474SJonathan.Adams@Sun.COM 			if (pf >= BTOP(pmem->ml_address) &&
767*11474SJonathan.Adams@Sun.COM 			    pf < BTOP(pmem->ml_address + pmem->ml_size)) {
7680Sstevel@tonic-gate 				memlist_read_unlock();
7690Sstevel@tonic-gate 				return (impl_obmem_pfnum(pf));
7700Sstevel@tonic-gate 			}
7710Sstevel@tonic-gate 		}
7720Sstevel@tonic-gate 		memlist_read_unlock();
7730Sstevel@tonic-gate 		break;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	case M_KMEM:
7760Sstevel@tonic-gate 	case M_ALLKMEM:
7770Sstevel@tonic-gate 		/* no longer supported with KPR */
7780Sstevel@tonic-gate 		return (-1);
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	case M_ZERO:
7810Sstevel@tonic-gate 		/*
7820Sstevel@tonic-gate 		 * We shouldn't be mmap'ing to /dev/zero here as
7830Sstevel@tonic-gate 		 * mmsegmap() should have already converted
7840Sstevel@tonic-gate 		 * a mapping request for this device to a mapping
7850Sstevel@tonic-gate 		 * using seg_vn for anonymous memory.
7860Sstevel@tonic-gate 		 */
7870Sstevel@tonic-gate 		break;
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	}
7900Sstevel@tonic-gate 	return (-1);
7910Sstevel@tonic-gate }
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate /*
7940Sstevel@tonic-gate  * This function is called when a memory device is mmap'ed.
7950Sstevel@tonic-gate  * Set up the mapping to the correct device driver.
7960Sstevel@tonic-gate  */
7970Sstevel@tonic-gate static int
mmsegmap(dev_t dev,off_t off,struct as * as,caddr_t * addrp,off_t len,uint_t prot,uint_t maxprot,uint_t flags,struct cred * cred)7980Sstevel@tonic-gate mmsegmap(dev_t dev, off_t off, struct as *as, caddr_t *addrp, off_t len,
7990Sstevel@tonic-gate     uint_t prot, uint_t maxprot, uint_t flags, struct cred *cred)
8000Sstevel@tonic-gate {
8010Sstevel@tonic-gate 	struct segvn_crargs vn_a;
8020Sstevel@tonic-gate 	struct segdev_crargs dev_a;
8030Sstevel@tonic-gate 	int error;
8040Sstevel@tonic-gate 	minor_t minor;
8050Sstevel@tonic-gate 	off_t i;
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	minor = getminor(dev);
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	as_rangelock(as);
8106036Smec 	/*
8116036Smec 	 * No need to worry about vac alignment on /dev/zero
8126036Smec 	 * since this is a "clone" object that doesn't yet exist.
8136036Smec 	 */
8146036Smec 	error = choose_addr(as, addrp, len, off,
8156036Smec 	    (minor == M_MEM) || (minor == M_KMEM), flags);
8166036Smec 	if (error != 0) {
8176036Smec 		as_rangeunlock(as);
8186036Smec 		return (error);
8190Sstevel@tonic-gate 	}
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	switch (minor) {
8220Sstevel@tonic-gate 	case M_MEM:
8230Sstevel@tonic-gate 		/* /dev/mem cannot be mmap'ed with MAP_PRIVATE */
8240Sstevel@tonic-gate 		if ((flags & MAP_TYPE) != MAP_SHARED) {
8250Sstevel@tonic-gate 			as_rangeunlock(as);
8260Sstevel@tonic-gate 			return (EINVAL);
8270Sstevel@tonic-gate 		}
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 		/*
8300Sstevel@tonic-gate 		 * Check to ensure that the entire range is
8310Sstevel@tonic-gate 		 * legal and we are not trying to map in
8320Sstevel@tonic-gate 		 * more than the device will let us.
8330Sstevel@tonic-gate 		 */
8340Sstevel@tonic-gate 		for (i = 0; i < len; i += PAGESIZE) {
8350Sstevel@tonic-gate 			if (mmmmap(dev, off + i, maxprot) == -1) {
8360Sstevel@tonic-gate 				as_rangeunlock(as);
8370Sstevel@tonic-gate 				return (ENXIO);
8380Sstevel@tonic-gate 			}
8390Sstevel@tonic-gate 		}
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 		/*
8420Sstevel@tonic-gate 		 * Use seg_dev segment driver for /dev/mem mapping.
8430Sstevel@tonic-gate 		 */
8440Sstevel@tonic-gate 		dev_a.mapfunc = mmmmap;
8450Sstevel@tonic-gate 		dev_a.dev = dev;
8460Sstevel@tonic-gate 		dev_a.offset = off;
8470Sstevel@tonic-gate 		dev_a.type = (flags & MAP_TYPE);
8480Sstevel@tonic-gate 		dev_a.prot = (uchar_t)prot;
8490Sstevel@tonic-gate 		dev_a.maxprot = (uchar_t)maxprot;
8500Sstevel@tonic-gate 		dev_a.hat_attr = 0;
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 		/*
8530Sstevel@tonic-gate 		 * Make /dev/mem mappings non-consistent since we can't
8540Sstevel@tonic-gate 		 * alias pages that don't have page structs behind them,
8550Sstevel@tonic-gate 		 * such as kernel stack pages. If someone mmap()s a kernel
8560Sstevel@tonic-gate 		 * stack page and if we give him a tte with cv, a line from
8570Sstevel@tonic-gate 		 * that page can get into both pages of the spitfire d$.
8580Sstevel@tonic-gate 		 * But snoop from another processor will only invalidate
8590Sstevel@tonic-gate 		 * the first page. This later caused kernel (xc_attention)
8600Sstevel@tonic-gate 		 * to go into an infinite loop at pil 13 and no interrupts
8610Sstevel@tonic-gate 		 * could come in. See 1203630.
8620Sstevel@tonic-gate 		 *
8630Sstevel@tonic-gate 		 */
8640Sstevel@tonic-gate 		dev_a.hat_flags = HAT_LOAD_NOCONSIST;
8650Sstevel@tonic-gate 		dev_a.devmap_data = NULL;
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 		error = as_map(as, *addrp, len, segdev_create, &dev_a);
8680Sstevel@tonic-gate 		break;
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	case M_ZERO:
8710Sstevel@tonic-gate 		/*
8720Sstevel@tonic-gate 		 * Use seg_vn segment driver for /dev/zero mapping.
8730Sstevel@tonic-gate 		 * Passing in a NULL amp gives us the "cloning" effect.
8740Sstevel@tonic-gate 		 */
8750Sstevel@tonic-gate 		vn_a.vp = NULL;
8760Sstevel@tonic-gate 		vn_a.offset = 0;
8770Sstevel@tonic-gate 		vn_a.type = (flags & MAP_TYPE);
8780Sstevel@tonic-gate 		vn_a.prot = prot;
8790Sstevel@tonic-gate 		vn_a.maxprot = maxprot;
8800Sstevel@tonic-gate 		vn_a.flags = flags & ~MAP_TYPE;
8810Sstevel@tonic-gate 		vn_a.cred = cred;
8820Sstevel@tonic-gate 		vn_a.amp = NULL;
8830Sstevel@tonic-gate 		vn_a.szc = 0;
8840Sstevel@tonic-gate 		vn_a.lgrp_mem_policy_flags = 0;
8850Sstevel@tonic-gate 		error = as_map(as, *addrp, len, segvn_create, &vn_a);
8860Sstevel@tonic-gate 		break;
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	case M_KMEM:
8890Sstevel@tonic-gate 	case M_ALLKMEM:
8900Sstevel@tonic-gate 		/* No longer supported with KPR. */
8910Sstevel@tonic-gate 		error = ENXIO;
8920Sstevel@tonic-gate 		break;
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	case M_NULL:
8950Sstevel@tonic-gate 		/*
8960Sstevel@tonic-gate 		 * Use seg_dev segment driver for /dev/null mapping.
8970Sstevel@tonic-gate 		 */
8980Sstevel@tonic-gate 		dev_a.mapfunc = mmmmap;
8990Sstevel@tonic-gate 		dev_a.dev = dev;
9000Sstevel@tonic-gate 		dev_a.offset = off;
9010Sstevel@tonic-gate 		dev_a.type = 0;		/* neither PRIVATE nor SHARED */
9020Sstevel@tonic-gate 		dev_a.prot = dev_a.maxprot = (uchar_t)PROT_NONE;
9030Sstevel@tonic-gate 		dev_a.hat_attr = 0;
9040Sstevel@tonic-gate 		dev_a.hat_flags = 0;
9050Sstevel@tonic-gate 		error = as_map(as, *addrp, len, segdev_create, &dev_a);
9060Sstevel@tonic-gate 		break;
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	default:
9090Sstevel@tonic-gate 		error = ENXIO;
9100Sstevel@tonic-gate 	}
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	as_rangeunlock(as);
9130Sstevel@tonic-gate 	return (error);
9140Sstevel@tonic-gate }
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate static struct cb_ops mm_cb_ops = {
9170Sstevel@tonic-gate 	mmopen,			/* open */
9180Sstevel@tonic-gate 	nulldev,		/* close */
9190Sstevel@tonic-gate 	nodev,			/* strategy */
9200Sstevel@tonic-gate 	nodev,			/* print */
9210Sstevel@tonic-gate 	nodev,			/* dump */
9220Sstevel@tonic-gate 	mmread,			/* read */
9230Sstevel@tonic-gate 	mmwrite,		/* write */
9240Sstevel@tonic-gate 	mmioctl,		/* ioctl */
9250Sstevel@tonic-gate 	nodev,			/* devmap */
9260Sstevel@tonic-gate 	mmmmap,			/* mmap */
9270Sstevel@tonic-gate 	mmsegmap,		/* segmap */
9280Sstevel@tonic-gate 	mmchpoll,		/* poll */
9290Sstevel@tonic-gate 	mmpropop,		/* prop_op */
9300Sstevel@tonic-gate 	0,			/* streamtab  */
9310Sstevel@tonic-gate 	D_NEW | D_MP | D_64BIT | D_U64BIT
9320Sstevel@tonic-gate };
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate static struct dev_ops mm_ops = {
9350Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
9360Sstevel@tonic-gate 	0,			/* refcnt  */
9370Sstevel@tonic-gate 	mm_info,		/* get_dev_info */
9380Sstevel@tonic-gate 	nulldev,		/* identify */
9390Sstevel@tonic-gate 	nulldev,		/* probe */
9400Sstevel@tonic-gate 	mm_attach,		/* attach */
9410Sstevel@tonic-gate 	nodev,			/* detach */
9420Sstevel@tonic-gate 	nodev,			/* reset */
9430Sstevel@tonic-gate 	&mm_cb_ops,		/* driver operations */
9447656SSherry.Moore@Sun.COM 	(struct bus_ops *)0,	/* bus operations */
9457656SSherry.Moore@Sun.COM 	NULL,			/* power */
9467656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
9470Sstevel@tonic-gate };
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate static struct modldrv modldrv = {
9507532SSean.Ye@Sun.COM 	&mod_driverops, "memory driver", &mm_ops,
9510Sstevel@tonic-gate };
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate static struct modlinkage modlinkage = {
9540Sstevel@tonic-gate 	MODREV_1, &modldrv, NULL
9550Sstevel@tonic-gate };
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate int
_init(void)9580Sstevel@tonic-gate _init(void)
9590Sstevel@tonic-gate {
9600Sstevel@tonic-gate 	return (mod_install(&modlinkage));
9610Sstevel@tonic-gate }
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate int
_info(struct modinfo * modinfop)9640Sstevel@tonic-gate _info(struct modinfo *modinfop)
9650Sstevel@tonic-gate {
9660Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
9670Sstevel@tonic-gate }
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate int
_fini(void)9700Sstevel@tonic-gate _fini(void)
9710Sstevel@tonic-gate {
9720Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
9730Sstevel@tonic-gate }
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate static int
mm_kstat_update(kstat_t * ksp,int rw)9760Sstevel@tonic-gate mm_kstat_update(kstat_t *ksp, int rw)
9770Sstevel@tonic-gate {
9780Sstevel@tonic-gate 	struct memlist *pmem;
9790Sstevel@tonic-gate 	uint_t count;
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
9820Sstevel@tonic-gate 		return (EACCES);
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 	count = 0;
9850Sstevel@tonic-gate 	memlist_read_lock();
986*11474SJonathan.Adams@Sun.COM 	for (pmem = phys_install; pmem != NULL; pmem = pmem->ml_next) {
9870Sstevel@tonic-gate 		count++;
9880Sstevel@tonic-gate 	}
9890Sstevel@tonic-gate 	memlist_read_unlock();
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	ksp->ks_ndata = count;
9920Sstevel@tonic-gate 	ksp->ks_data_size = count * 2 * sizeof (uint64_t);
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	return (0);
9950Sstevel@tonic-gate }
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate static int
mm_kstat_snapshot(kstat_t * ksp,void * buf,int rw)9980Sstevel@tonic-gate mm_kstat_snapshot(kstat_t *ksp, void *buf, int rw)
9990Sstevel@tonic-gate {
10000Sstevel@tonic-gate 	struct memlist *pmem;
10010Sstevel@tonic-gate 	struct memunit {
10020Sstevel@tonic-gate 		uint64_t address;
10030Sstevel@tonic-gate 		uint64_t size;
10040Sstevel@tonic-gate 	} *kspmem;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
10070Sstevel@tonic-gate 		return (EACCES);
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	ksp->ks_snaptime = gethrtime();
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	kspmem = (struct memunit *)buf;
10120Sstevel@tonic-gate 	memlist_read_lock();
1013*11474SJonathan.Adams@Sun.COM 	for (pmem = phys_install; pmem != NULL;
1014*11474SJonathan.Adams@Sun.COM 	    pmem = pmem->ml_next, kspmem++) {
10150Sstevel@tonic-gate 		if ((caddr_t)kspmem >= (caddr_t)buf + ksp->ks_data_size)
10160Sstevel@tonic-gate 			break;
1017*11474SJonathan.Adams@Sun.COM 		kspmem->address = pmem->ml_address;
1018*11474SJonathan.Adams@Sun.COM 		kspmem->size = pmem->ml_size;
10190Sstevel@tonic-gate 	}
10200Sstevel@tonic-gate 	memlist_read_unlock();
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	return (0);
10230Sstevel@tonic-gate }
10241186Sayznaga 
10251186Sayznaga /*
10261186Sayznaga  * Read a mem_name_t from user-space and store it in the mem_name_t
10271186Sayznaga  * pointed to by the mem_name argument.
10281186Sayznaga  */
10291186Sayznaga static int
mm_read_mem_name(intptr_t data,mem_name_t * mem_name)10301186Sayznaga mm_read_mem_name(intptr_t data, mem_name_t *mem_name)
10311186Sayznaga {
10321186Sayznaga 	if (get_udatamodel() == DATAMODEL_NATIVE) {
10331186Sayznaga 		if (copyin((void *)data, mem_name, sizeof (mem_name_t)))
10341186Sayznaga 			return (EFAULT);
10351186Sayznaga 	}
10361186Sayznaga #ifdef	_SYSCALL32
10371186Sayznaga 	else {
10381186Sayznaga 		mem_name32_t mem_name32;
10391186Sayznaga 
10401186Sayznaga 		if (copyin((void *)data, &mem_name32, sizeof (mem_name32_t)))
10411186Sayznaga 			return (EFAULT);
10421186Sayznaga 		mem_name->m_addr = mem_name32.m_addr;
10431186Sayznaga 		mem_name->m_synd = mem_name32.m_synd;
10441186Sayznaga 		mem_name->m_type[0] = mem_name32.m_type[0];
10451186Sayznaga 		mem_name->m_type[1] = mem_name32.m_type[1];
10461283Sayznaga 		mem_name->m_name = (caddr_t)(uintptr_t)mem_name32.m_name;
10471186Sayznaga 		mem_name->m_namelen = (size_t)mem_name32.m_namelen;
10481283Sayznaga 		mem_name->m_sid = (caddr_t)(uintptr_t)mem_name32.m_sid;
10491186Sayznaga 		mem_name->m_sidlen = (size_t)mem_name32.m_sidlen;
10501186Sayznaga 	}
10511186Sayznaga #endif	/* _SYSCALL32 */
10521186Sayznaga 
10531186Sayznaga 	return (0);
10541186Sayznaga }
1055