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*3446Smrj * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * Memory special file 300Sstevel@tonic-gate */ 310Sstevel@tonic-gate 320Sstevel@tonic-gate #include <sys/types.h> 330Sstevel@tonic-gate #include <sys/param.h> 340Sstevel@tonic-gate #include <sys/user.h> 350Sstevel@tonic-gate #include <sys/buf.h> 360Sstevel@tonic-gate #include <sys/systm.h> 370Sstevel@tonic-gate #include <sys/cred.h> 380Sstevel@tonic-gate #include <sys/vm.h> 390Sstevel@tonic-gate #include <sys/uio.h> 400Sstevel@tonic-gate #include <sys/mman.h> 410Sstevel@tonic-gate #include <sys/kmem.h> 420Sstevel@tonic-gate #include <vm/seg.h> 430Sstevel@tonic-gate #include <vm/page.h> 440Sstevel@tonic-gate #include <sys/stat.h> 450Sstevel@tonic-gate #include <sys/vmem.h> 460Sstevel@tonic-gate #include <sys/memlist.h> 470Sstevel@tonic-gate #include <sys/bootconf.h> 480Sstevel@tonic-gate 490Sstevel@tonic-gate #include <vm/seg_vn.h> 500Sstevel@tonic-gate #include <vm/seg_dev.h> 510Sstevel@tonic-gate #include <vm/seg_kmem.h> 520Sstevel@tonic-gate #include <vm/seg_kp.h> 530Sstevel@tonic-gate #include <vm/seg_kpm.h> 540Sstevel@tonic-gate #include <vm/hat.h> 550Sstevel@tonic-gate 560Sstevel@tonic-gate #include <sys/conf.h> 570Sstevel@tonic-gate #include <sys/mem.h> 580Sstevel@tonic-gate #include <sys/types.h> 590Sstevel@tonic-gate #include <sys/conf.h> 600Sstevel@tonic-gate #include <sys/param.h> 610Sstevel@tonic-gate #include <sys/systm.h> 620Sstevel@tonic-gate #include <sys/errno.h> 630Sstevel@tonic-gate #include <sys/modctl.h> 640Sstevel@tonic-gate #include <sys/memlist.h> 650Sstevel@tonic-gate #include <sys/ddi.h> 660Sstevel@tonic-gate #include <sys/sunddi.h> 670Sstevel@tonic-gate #include <sys/debug.h> 681186Sayznaga #include <sys/fm/protocol.h> 690Sstevel@tonic-gate 701414Scindi #if defined(__sparc) 710Sstevel@tonic-gate extern int cpu_get_mem_name(uint64_t, uint64_t *, uint64_t, char *, int, int *); 720Sstevel@tonic-gate extern int cpu_get_mem_info(uint64_t, uint64_t, uint64_t *, uint64_t *, 730Sstevel@tonic-gate uint64_t *, int *, int *, int *); 740Sstevel@tonic-gate extern size_t cpu_get_name_bufsize(void); 751186Sayznaga extern int cpu_get_mem_sid(char *, char *, int, int *); 761186Sayznaga extern int cpu_get_mem_addr(char *, char *, uint64_t, uint64_t *); 77*3446Smrj #elif defined(__x86) 781414Scindi #include <sys/cpu_module.h> 791186Sayznaga #endif /* __sparc */ 800Sstevel@tonic-gate 810Sstevel@tonic-gate /* 820Sstevel@tonic-gate * Turn a byte length into a pagecount. The DDI btop takes a 830Sstevel@tonic-gate * 32-bit size on 32-bit machines, this handles 64-bit sizes for 840Sstevel@tonic-gate * large physical-memory 32-bit machines. 850Sstevel@tonic-gate */ 860Sstevel@tonic-gate #define BTOP(x) ((pgcnt_t)((x) >> _pageshift)) 870Sstevel@tonic-gate 880Sstevel@tonic-gate static kmutex_t mm_lock; 890Sstevel@tonic-gate static caddr_t mm_map; 900Sstevel@tonic-gate 910Sstevel@tonic-gate static dev_info_t *mm_dip; /* private copy of devinfo pointer */ 920Sstevel@tonic-gate 930Sstevel@tonic-gate static int mm_kmem_io_access; 940Sstevel@tonic-gate 950Sstevel@tonic-gate static int mm_kstat_update(kstat_t *ksp, int rw); 960Sstevel@tonic-gate static int mm_kstat_snapshot(kstat_t *ksp, void *buf, int rw); 970Sstevel@tonic-gate 981186Sayznaga static int mm_read_mem_name(intptr_t data, mem_name_t *mem_name); 991186Sayznaga static int mm_read_mem_page(intptr_t data, mem_page_t *mpage); 1001186Sayznaga static int mm_get_mem_fmri(mem_page_t *mpage, nvlist_t **nvl); 1011186Sayznaga static int mm_get_paddr(nvlist_t *nvl, uint64_t *paddr); 1021186Sayznaga 1030Sstevel@tonic-gate /*ARGSUSED1*/ 1040Sstevel@tonic-gate static int 1050Sstevel@tonic-gate mm_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 1060Sstevel@tonic-gate { 1070Sstevel@tonic-gate int i; 1080Sstevel@tonic-gate struct mem_minor { 1090Sstevel@tonic-gate char *name; 1100Sstevel@tonic-gate minor_t minor; 1110Sstevel@tonic-gate int privonly; 1120Sstevel@tonic-gate const char *rdpriv; 1130Sstevel@tonic-gate const char *wrpriv; 1140Sstevel@tonic-gate mode_t priv_mode; 1150Sstevel@tonic-gate } mm[] = { 1160Sstevel@tonic-gate { "mem", M_MEM, 0, NULL, "all", 0640 }, 1170Sstevel@tonic-gate { "kmem", M_KMEM, 0, NULL, "all", 0640 }, 1180Sstevel@tonic-gate { "allkmem", M_ALLKMEM, 0, "all", "all", 0600 }, 1190Sstevel@tonic-gate { "null", M_NULL, PRIVONLY_DEV, NULL, NULL, 0666 }, 1200Sstevel@tonic-gate { "zero", M_ZERO, PRIVONLY_DEV, NULL, NULL, 0666 }, 1210Sstevel@tonic-gate }; 1220Sstevel@tonic-gate kstat_t *ksp; 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate mutex_init(&mm_lock, NULL, MUTEX_DEFAULT, NULL); 1250Sstevel@tonic-gate mm_map = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP); 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate for (i = 0; i < (sizeof (mm) / sizeof (mm[0])); i++) { 1280Sstevel@tonic-gate if (ddi_create_priv_minor_node(devi, mm[i].name, S_IFCHR, 1290Sstevel@tonic-gate mm[i].minor, DDI_PSEUDO, mm[i].privonly, 1300Sstevel@tonic-gate mm[i].rdpriv, mm[i].wrpriv, mm[i].priv_mode) == 1310Sstevel@tonic-gate DDI_FAILURE) { 1320Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 1330Sstevel@tonic-gate return (DDI_FAILURE); 1340Sstevel@tonic-gate } 1350Sstevel@tonic-gate } 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate mm_dip = devi; 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate ksp = kstat_create("mm", 0, "phys_installed", "misc", 1400Sstevel@tonic-gate KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VAR_SIZE | KSTAT_FLAG_VIRTUAL); 1410Sstevel@tonic-gate if (ksp != NULL) { 1420Sstevel@tonic-gate ksp->ks_update = mm_kstat_update; 1430Sstevel@tonic-gate ksp->ks_snapshot = mm_kstat_snapshot; 1440Sstevel@tonic-gate ksp->ks_lock = &mm_lock; /* XXX - not really needed */ 1450Sstevel@tonic-gate kstat_install(ksp); 1460Sstevel@tonic-gate } 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate mm_kmem_io_access = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 1490Sstevel@tonic-gate "kmem_io_access", 0); 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate return (DDI_SUCCESS); 1520Sstevel@tonic-gate } 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate /*ARGSUSED*/ 1550Sstevel@tonic-gate static int 1560Sstevel@tonic-gate mm_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 1570Sstevel@tonic-gate { 1580Sstevel@tonic-gate register int error; 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate switch (infocmd) { 1610Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 1620Sstevel@tonic-gate *result = (void *)mm_dip; 1630Sstevel@tonic-gate error = DDI_SUCCESS; 1640Sstevel@tonic-gate break; 1650Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 1660Sstevel@tonic-gate *result = (void *)0; 1670Sstevel@tonic-gate error = DDI_SUCCESS; 1680Sstevel@tonic-gate break; 1690Sstevel@tonic-gate default: 1700Sstevel@tonic-gate error = DDI_FAILURE; 1710Sstevel@tonic-gate } 1720Sstevel@tonic-gate return (error); 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate /*ARGSUSED1*/ 1760Sstevel@tonic-gate static int 1770Sstevel@tonic-gate mmopen(dev_t *devp, int flag, int typ, struct cred *cred) 1780Sstevel@tonic-gate { 1790Sstevel@tonic-gate switch (getminor(*devp)) { 1800Sstevel@tonic-gate case M_NULL: 1810Sstevel@tonic-gate case M_ZERO: 1820Sstevel@tonic-gate case M_MEM: 1830Sstevel@tonic-gate case M_KMEM: 1840Sstevel@tonic-gate case M_ALLKMEM: 1850Sstevel@tonic-gate /* standard devices */ 1860Sstevel@tonic-gate break; 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate default: 1890Sstevel@tonic-gate /* Unsupported or unknown type */ 1900Sstevel@tonic-gate return (EINVAL); 1910Sstevel@tonic-gate } 1920Sstevel@tonic-gate return (0); 1930Sstevel@tonic-gate } 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate struct pollhead mm_pollhd; 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate /*ARGSUSED*/ 1980Sstevel@tonic-gate static int 1990Sstevel@tonic-gate mmchpoll(dev_t dev, short events, int anyyet, short *reventsp, 2000Sstevel@tonic-gate struct pollhead **phpp) 2010Sstevel@tonic-gate { 2020Sstevel@tonic-gate switch (getminor(dev)) { 2030Sstevel@tonic-gate case M_NULL: 2040Sstevel@tonic-gate case M_ZERO: 2050Sstevel@tonic-gate case M_MEM: 2060Sstevel@tonic-gate case M_KMEM: 2070Sstevel@tonic-gate case M_ALLKMEM: 2080Sstevel@tonic-gate *reventsp = events & (POLLIN | POLLOUT | POLLPRI | POLLRDNORM | 2090Sstevel@tonic-gate POLLWRNORM | POLLRDBAND | POLLWRBAND); 2100Sstevel@tonic-gate /* 2110Sstevel@tonic-gate * A non NULL pollhead pointer should be returned in case 2120Sstevel@tonic-gate * user polls for 0 events. 2130Sstevel@tonic-gate */ 2140Sstevel@tonic-gate *phpp = !anyyet && !*reventsp ? 2150Sstevel@tonic-gate &mm_pollhd : (struct pollhead *)NULL; 2160Sstevel@tonic-gate return (0); 2170Sstevel@tonic-gate default: 2180Sstevel@tonic-gate /* no other devices currently support polling */ 2190Sstevel@tonic-gate return (ENXIO); 2200Sstevel@tonic-gate } 2210Sstevel@tonic-gate } 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate static int 2240Sstevel@tonic-gate mmpropop(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int flags, 2250Sstevel@tonic-gate char *name, caddr_t valuep, int *lengthp) 2260Sstevel@tonic-gate { 2270Sstevel@tonic-gate /* 2280Sstevel@tonic-gate * implement zero size to reduce overhead (avoid two failing 2290Sstevel@tonic-gate * property lookups per stat). 2300Sstevel@tonic-gate */ 2310Sstevel@tonic-gate return (ddi_prop_op_size(dev, dip, prop_op, 2320Sstevel@tonic-gate flags, name, valuep, lengthp, 0)); 2330Sstevel@tonic-gate } 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate static int 2360Sstevel@tonic-gate mmio(struct uio *uio, enum uio_rw rw, pfn_t pfn, off_t pageoff, int allowio) 2370Sstevel@tonic-gate { 2380Sstevel@tonic-gate int error = 0; 2390Sstevel@tonic-gate size_t nbytes = MIN((size_t)(PAGESIZE - pageoff), 2400Sstevel@tonic-gate (size_t)uio->uio_iov->iov_len); 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate mutex_enter(&mm_lock); 2430Sstevel@tonic-gate hat_devload(kas.a_hat, mm_map, PAGESIZE, pfn, 2440Sstevel@tonic-gate (uint_t)(rw == UIO_READ ? PROT_READ : PROT_READ | PROT_WRITE), 2450Sstevel@tonic-gate HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK); 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate if (!pf_is_memory(pfn)) { 2480Sstevel@tonic-gate if (allowio) { 2490Sstevel@tonic-gate size_t c = uio->uio_iov->iov_len; 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate if (ddi_peekpokeio(NULL, uio, rw, 2520Sstevel@tonic-gate (caddr_t)(uintptr_t)uio->uio_loffset, c, 2530Sstevel@tonic-gate sizeof (int32_t)) != DDI_SUCCESS) 2540Sstevel@tonic-gate error = EFAULT; 2550Sstevel@tonic-gate } else 2560Sstevel@tonic-gate error = EIO; 2570Sstevel@tonic-gate } else 2580Sstevel@tonic-gate error = uiomove(&mm_map[pageoff], nbytes, rw, uio); 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate hat_unload(kas.a_hat, mm_map, PAGESIZE, HAT_UNLOAD_UNLOCK); 2610Sstevel@tonic-gate mutex_exit(&mm_lock); 2620Sstevel@tonic-gate return (error); 2630Sstevel@tonic-gate } 2640Sstevel@tonic-gate 265*3446Smrj /* 266*3446Smrj * Some platforms have permanently-mapped areas without PFNs, so we check 267*3446Smrj * specially here. 268*3446Smrj */ 269*3446Smrj static int 270*3446Smrj mmplatio(struct uio *uio, enum uio_rw rw) 271*3446Smrj { 272*3446Smrj uintptr_t pageaddr = (uintptr_t)uio->uio_loffset & PAGEMASK; 273*3446Smrj off_t pageoff = uio->uio_loffset & PAGEOFFSET; 274*3446Smrj size_t nbytes = MIN((size_t)(PAGESIZE - pageoff), 275*3446Smrj (size_t)uio->uio_iov->iov_len); 276*3446Smrj 277*3446Smrj if (!plat_mem_valid_page(pageaddr, rw)) 278*3446Smrj return (ENOTSUP); 279*3446Smrj 280*3446Smrj return (uiomove((void *)(pageaddr + pageoff), nbytes, rw, uio)); 281*3446Smrj } 282*3446Smrj 2830Sstevel@tonic-gate #ifdef __sparc 2840Sstevel@tonic-gate 285670Selowe static int 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 299670Selowe #define NEED_LOCK_KVADDR(kva) mmpagelock(&kas, kva) 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate #else /* __i386, __amd64 */ 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate #define NEED_LOCK_KVADDR(va) 0 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate #endif /* __sparc */ 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate /*ARGSUSED3*/ 3080Sstevel@tonic-gate static int 3090Sstevel@tonic-gate mmrw(dev_t dev, struct uio *uio, enum uio_rw rw, cred_t *cred) 3100Sstevel@tonic-gate { 3110Sstevel@tonic-gate pfn_t v; 3120Sstevel@tonic-gate struct iovec *iov; 3130Sstevel@tonic-gate int error = 0; 3140Sstevel@tonic-gate size_t c; 3150Sstevel@tonic-gate ssize_t oresid = uio->uio_resid; 3160Sstevel@tonic-gate minor_t minor = getminor(dev); 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate while (uio->uio_resid > 0 && error == 0) { 3190Sstevel@tonic-gate iov = uio->uio_iov; 3200Sstevel@tonic-gate if (iov->iov_len == 0) { 3210Sstevel@tonic-gate uio->uio_iov++; 3220Sstevel@tonic-gate uio->uio_iovcnt--; 3230Sstevel@tonic-gate if (uio->uio_iovcnt < 0) 3240Sstevel@tonic-gate panic("mmrw"); 3250Sstevel@tonic-gate continue; 3260Sstevel@tonic-gate } 3270Sstevel@tonic-gate switch (minor) { 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate case M_MEM: 3300Sstevel@tonic-gate memlist_read_lock(); 3310Sstevel@tonic-gate if (!address_in_memlist(phys_install, 3320Sstevel@tonic-gate (uint64_t)uio->uio_loffset, 1)) { 3330Sstevel@tonic-gate memlist_read_unlock(); 3340Sstevel@tonic-gate error = EFAULT; 3350Sstevel@tonic-gate break; 3360Sstevel@tonic-gate } 3370Sstevel@tonic-gate memlist_read_unlock(); 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate v = BTOP((u_offset_t)uio->uio_loffset); 3400Sstevel@tonic-gate error = mmio(uio, rw, v, 3410Sstevel@tonic-gate uio->uio_loffset & PAGEOFFSET, 0); 3420Sstevel@tonic-gate break; 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate case M_KMEM: 3450Sstevel@tonic-gate case M_ALLKMEM: 3460Sstevel@tonic-gate { 3470Sstevel@tonic-gate page_t **ppp; 3480Sstevel@tonic-gate caddr_t vaddr = (caddr_t)uio->uio_offset; 3490Sstevel@tonic-gate int try_lock = NEED_LOCK_KVADDR(vaddr); 3500Sstevel@tonic-gate int locked = 0; 3510Sstevel@tonic-gate 352*3446Smrj if ((error = mmplatio(uio, rw)) != ENOTSUP) 353*3446Smrj break; 354*3446Smrj 3550Sstevel@tonic-gate /* 3560Sstevel@tonic-gate * If vaddr does not map a valid page, as_pagelock() 3570Sstevel@tonic-gate * will return failure. Hence we can't check the 3580Sstevel@tonic-gate * return value and return EFAULT here as we'd like. 3590Sstevel@tonic-gate * seg_kp and seg_kpm do not properly support 3600Sstevel@tonic-gate * as_pagelock() for this context so we avoid it 3610Sstevel@tonic-gate * using the try_lock set check above. Some day when 3620Sstevel@tonic-gate * the kernel page locking gets redesigned all this 3630Sstevel@tonic-gate * muck can be cleaned up. 3640Sstevel@tonic-gate */ 3650Sstevel@tonic-gate if (try_lock) 3660Sstevel@tonic-gate locked = (as_pagelock(&kas, &ppp, vaddr, 3670Sstevel@tonic-gate PAGESIZE, S_WRITE) == 0); 3680Sstevel@tonic-gate 369513Sjongkis v = hat_getpfnum(kas.a_hat, 370513Sjongkis (caddr_t)(uintptr_t)uio->uio_loffset); 3710Sstevel@tonic-gate if (v == PFN_INVALID) { 3720Sstevel@tonic-gate if (locked) 3730Sstevel@tonic-gate as_pageunlock(&kas, ppp, vaddr, 3740Sstevel@tonic-gate PAGESIZE, S_WRITE); 3750Sstevel@tonic-gate error = EFAULT; 3760Sstevel@tonic-gate break; 3770Sstevel@tonic-gate } 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate error = mmio(uio, rw, v, uio->uio_loffset & PAGEOFFSET, 3800Sstevel@tonic-gate minor == M_ALLKMEM || mm_kmem_io_access); 3810Sstevel@tonic-gate if (locked) 3820Sstevel@tonic-gate as_pageunlock(&kas, ppp, vaddr, PAGESIZE, 3830Sstevel@tonic-gate S_WRITE); 3840Sstevel@tonic-gate } 3850Sstevel@tonic-gate 3860Sstevel@tonic-gate break; 3870Sstevel@tonic-gate 3880Sstevel@tonic-gate case M_ZERO: 3890Sstevel@tonic-gate if (rw == UIO_READ) { 3900Sstevel@tonic-gate label_t ljb; 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate if (on_fault(&ljb)) { 3930Sstevel@tonic-gate no_fault(); 3940Sstevel@tonic-gate error = EFAULT; 3950Sstevel@tonic-gate break; 3960Sstevel@tonic-gate } 3970Sstevel@tonic-gate uzero(iov->iov_base, iov->iov_len); 3980Sstevel@tonic-gate no_fault(); 3990Sstevel@tonic-gate uio->uio_resid -= iov->iov_len; 4000Sstevel@tonic-gate uio->uio_loffset += iov->iov_len; 4010Sstevel@tonic-gate break; 4020Sstevel@tonic-gate } 4030Sstevel@tonic-gate /* else it's a write, fall through to NULL case */ 4040Sstevel@tonic-gate /*FALLTHROUGH*/ 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate case M_NULL: 4070Sstevel@tonic-gate if (rw == UIO_READ) 4080Sstevel@tonic-gate return (0); 4090Sstevel@tonic-gate c = iov->iov_len; 4100Sstevel@tonic-gate iov->iov_base += c; 4110Sstevel@tonic-gate iov->iov_len -= c; 4120Sstevel@tonic-gate uio->uio_loffset += c; 4130Sstevel@tonic-gate uio->uio_resid -= c; 4140Sstevel@tonic-gate break; 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate } 4170Sstevel@tonic-gate } 4180Sstevel@tonic-gate return (uio->uio_resid == oresid ? error : 0); 4190Sstevel@tonic-gate } 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate static int 4220Sstevel@tonic-gate mmread(dev_t dev, struct uio *uio, cred_t *cred) 4230Sstevel@tonic-gate { 4240Sstevel@tonic-gate return (mmrw(dev, uio, UIO_READ, cred)); 4250Sstevel@tonic-gate } 4260Sstevel@tonic-gate 4270Sstevel@tonic-gate static int 4280Sstevel@tonic-gate mmwrite(dev_t dev, struct uio *uio, cred_t *cred) 4290Sstevel@tonic-gate { 4300Sstevel@tonic-gate return (mmrw(dev, uio, UIO_WRITE, cred)); 4310Sstevel@tonic-gate } 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate /* 4340Sstevel@tonic-gate * Private ioctl for libkvm to support kvm_physaddr(). 4350Sstevel@tonic-gate * Given an address space and a VA, compute the PA. 4360Sstevel@tonic-gate */ 4370Sstevel@tonic-gate static int 4380Sstevel@tonic-gate mmioctl_vtop(intptr_t data) 4390Sstevel@tonic-gate { 4401414Scindi #ifdef _SYSCALL32 4411414Scindi mem_vtop32_t vtop32; 4421414Scindi #endif 4430Sstevel@tonic-gate mem_vtop_t mem_vtop; 4440Sstevel@tonic-gate proc_t *p; 4450Sstevel@tonic-gate pfn_t pfn = (pfn_t)PFN_INVALID; 4460Sstevel@tonic-gate pid_t pid = 0; 4470Sstevel@tonic-gate struct as *as; 4480Sstevel@tonic-gate struct seg *seg; 4490Sstevel@tonic-gate 4501414Scindi if (get_udatamodel() == DATAMODEL_NATIVE) { 4511414Scindi if (copyin((void *)data, &mem_vtop, sizeof (mem_vtop_t))) 4521414Scindi return (EFAULT); 4531414Scindi } 4541414Scindi #ifdef _SYSCALL32 4551414Scindi else { 4561414Scindi if (copyin((void *)data, &vtop32, sizeof (mem_vtop32_t))) 4571414Scindi return (EFAULT); 4581717Swesolows mem_vtop.m_as = (struct as *)(uintptr_t)vtop32.m_as; 4591717Swesolows mem_vtop.m_va = (void *)(uintptr_t)vtop32.m_va; 4601414Scindi 4611414Scindi if (mem_vtop.m_as != NULL) 4621414Scindi return (EINVAL); 4631414Scindi } 4641414Scindi #endif 4651414Scindi 4660Sstevel@tonic-gate if (mem_vtop.m_as == &kas) { 4670Sstevel@tonic-gate pfn = hat_getpfnum(kas.a_hat, mem_vtop.m_va); 4680Sstevel@tonic-gate } else { 4691414Scindi if (mem_vtop.m_as == NULL) { 4701414Scindi /* 4711414Scindi * Assume the calling process's address space if the 4721414Scindi * caller didn't specify one. 4731414Scindi */ 4741414Scindi p = curthread->t_procp; 4751414Scindi if (p == NULL) 4761414Scindi return (EIO); 4771414Scindi mem_vtop.m_as = p->p_as; 4781414Scindi } 4791414Scindi 4800Sstevel@tonic-gate mutex_enter(&pidlock); 4810Sstevel@tonic-gate for (p = practive; p != NULL; p = p->p_next) { 4820Sstevel@tonic-gate if (p->p_as == mem_vtop.m_as) { 4830Sstevel@tonic-gate pid = p->p_pid; 4840Sstevel@tonic-gate break; 4850Sstevel@tonic-gate } 4860Sstevel@tonic-gate } 4870Sstevel@tonic-gate mutex_exit(&pidlock); 4880Sstevel@tonic-gate if (p == NULL) 4890Sstevel@tonic-gate return (EIO); 4900Sstevel@tonic-gate p = sprlock(pid); 4910Sstevel@tonic-gate if (p == NULL) 4920Sstevel@tonic-gate return (EIO); 4930Sstevel@tonic-gate as = p->p_as; 4940Sstevel@tonic-gate if (as == mem_vtop.m_as) { 4950Sstevel@tonic-gate mutex_exit(&p->p_lock); 4960Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 4970Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; 4980Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg)) 4990Sstevel@tonic-gate if ((uintptr_t)mem_vtop.m_va - 5000Sstevel@tonic-gate (uintptr_t)seg->s_base < seg->s_size) 5010Sstevel@tonic-gate break; 5020Sstevel@tonic-gate if (seg != NULL) 5030Sstevel@tonic-gate pfn = hat_getpfnum(as->a_hat, mem_vtop.m_va); 5040Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 5050Sstevel@tonic-gate mutex_enter(&p->p_lock); 5060Sstevel@tonic-gate } 5070Sstevel@tonic-gate sprunlock(p); 5080Sstevel@tonic-gate } 5090Sstevel@tonic-gate mem_vtop.m_pfn = pfn; 5100Sstevel@tonic-gate if (pfn == PFN_INVALID) 5110Sstevel@tonic-gate return (EIO); 5121414Scindi 5131414Scindi if (get_udatamodel() == DATAMODEL_NATIVE) { 5141414Scindi if (copyout(&mem_vtop, (void *)data, sizeof (mem_vtop_t))) 5151414Scindi return (EFAULT); 5161414Scindi } 5171414Scindi #ifdef _SYSCALL32 5181414Scindi else { 5191414Scindi vtop32.m_pfn = mem_vtop.m_pfn; 5201414Scindi if (copyout(&vtop32, (void *)data, sizeof (mem_vtop32_t))) 5211414Scindi return (EFAULT); 5221414Scindi } 5231414Scindi #endif 5240Sstevel@tonic-gate 5250Sstevel@tonic-gate return (0); 5260Sstevel@tonic-gate } 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate /* 529917Selowe * Given a PA, execute the given page retire command on it. 5300Sstevel@tonic-gate */ 5310Sstevel@tonic-gate static int 5320Sstevel@tonic-gate mmioctl_page_retire(int cmd, intptr_t data) 5330Sstevel@tonic-gate { 534917Selowe extern int page_retire_test(void); 5350Sstevel@tonic-gate uint64_t pa; 5360Sstevel@tonic-gate 537917Selowe if (copyin((void *)data, &pa, sizeof (uint64_t))) { 538917Selowe return (EFAULT); 5390Sstevel@tonic-gate } 5400Sstevel@tonic-gate 541917Selowe switch (cmd) { 542917Selowe case MEM_PAGE_ISRETIRED: 543917Selowe return (page_retire_check(pa, NULL)); 544917Selowe 545917Selowe case MEM_PAGE_UNRETIRE: 546917Selowe return (page_unretire(pa)); 547917Selowe 548917Selowe case MEM_PAGE_RETIRE: 549917Selowe return (page_retire(pa, PR_FMA)); 550917Selowe 551917Selowe case MEM_PAGE_RETIRE_MCE: 552917Selowe return (page_retire(pa, PR_MCE)); 553917Selowe 554917Selowe case MEM_PAGE_RETIRE_UE: 555917Selowe return (page_retire(pa, PR_UE)); 5560Sstevel@tonic-gate 557917Selowe case MEM_PAGE_GETERRORS: 558917Selowe { 559917Selowe uint64_t page_errors; 560917Selowe int rc = page_retire_check(pa, &page_errors); 561917Selowe if (copyout(&page_errors, (void *)data, 562917Selowe sizeof (uint64_t))) { 563917Selowe return (EFAULT); 564917Selowe } 565917Selowe return (rc); 566917Selowe } 567917Selowe 568917Selowe case MEM_PAGE_RETIRE_TEST: 569917Selowe return (page_retire_test()); 570917Selowe 571917Selowe } 572917Selowe 573917Selowe return (EINVAL); 5740Sstevel@tonic-gate } 5750Sstevel@tonic-gate 5761186Sayznaga /* 5771186Sayznaga * Given a mem-scheme FMRI for a page, execute the given page retire 5781186Sayznaga * command on it. 5791186Sayznaga */ 5801186Sayznaga static int 5811186Sayznaga mmioctl_page_fmri_retire(int cmd, intptr_t data) 5821186Sayznaga { 5831186Sayznaga mem_page_t mpage; 5841186Sayznaga uint64_t pa; 5851186Sayznaga nvlist_t *nvl; 5861186Sayznaga int err; 5871186Sayznaga 5881186Sayznaga if ((err = mm_read_mem_page(data, &mpage)) < 0) 5891186Sayznaga return (err); 5901186Sayznaga 5911186Sayznaga if ((err = mm_get_mem_fmri(&mpage, &nvl)) < 0) 5921186Sayznaga return (err); 5931186Sayznaga 5941414Scindi if ((err = mm_get_paddr(nvl, &pa)) != 0) { 5951186Sayznaga nvlist_free(nvl); 5961186Sayznaga return (err); 5971186Sayznaga } 5981186Sayznaga 5991186Sayznaga nvlist_free(nvl); 6001186Sayznaga 6011186Sayznaga switch (cmd) { 6021186Sayznaga case MEM_PAGE_FMRI_ISRETIRED: 6031186Sayznaga return (page_retire_check(pa, NULL)); 6041186Sayznaga 6051186Sayznaga case MEM_PAGE_FMRI_RETIRE: 6061186Sayznaga return (page_retire(pa, PR_FMA)); 6071186Sayznaga } 6081186Sayznaga 6091186Sayznaga return (EINVAL); 6101186Sayznaga } 6111186Sayznaga 6120Sstevel@tonic-gate #ifdef __sparc 6130Sstevel@tonic-gate /* 6140Sstevel@tonic-gate * Given a syndrome, syndrome type, and address return the 6150Sstevel@tonic-gate * associated memory name in the provided data buffer. 6160Sstevel@tonic-gate */ 6170Sstevel@tonic-gate static int 6180Sstevel@tonic-gate mmioctl_get_mem_name(intptr_t data) 6190Sstevel@tonic-gate { 6200Sstevel@tonic-gate mem_name_t mem_name; 6210Sstevel@tonic-gate void *buf; 6220Sstevel@tonic-gate size_t bufsize; 6230Sstevel@tonic-gate int len, err; 6240Sstevel@tonic-gate 6250Sstevel@tonic-gate if ((bufsize = cpu_get_name_bufsize()) == 0) 6260Sstevel@tonic-gate return (ENOTSUP); 6270Sstevel@tonic-gate 6281186Sayznaga if ((err = mm_read_mem_name(data, &mem_name)) < 0) 6291186Sayznaga return (err); 6300Sstevel@tonic-gate 6310Sstevel@tonic-gate buf = kmem_alloc(bufsize, KM_SLEEP); 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate /* 6340Sstevel@tonic-gate * Call into cpu specific code to do the lookup. 6350Sstevel@tonic-gate */ 6360Sstevel@tonic-gate if ((err = cpu_get_mem_name(mem_name.m_synd, mem_name.m_type, 6370Sstevel@tonic-gate mem_name.m_addr, buf, bufsize, &len)) != 0) { 6380Sstevel@tonic-gate kmem_free(buf, bufsize); 6390Sstevel@tonic-gate return (err); 6400Sstevel@tonic-gate } 6410Sstevel@tonic-gate 6420Sstevel@tonic-gate if (len >= mem_name.m_namelen) { 6430Sstevel@tonic-gate kmem_free(buf, bufsize); 6440Sstevel@tonic-gate return (ENAMETOOLONG); 6450Sstevel@tonic-gate } 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate if (copyoutstr(buf, (char *)mem_name.m_name, 6480Sstevel@tonic-gate mem_name.m_namelen, NULL) != 0) { 6490Sstevel@tonic-gate kmem_free(buf, bufsize); 6500Sstevel@tonic-gate return (EFAULT); 6510Sstevel@tonic-gate } 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate kmem_free(buf, bufsize); 6540Sstevel@tonic-gate return (0); 6550Sstevel@tonic-gate } 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate /* 6580Sstevel@tonic-gate * Given a syndrome and address return information about the associated memory. 6590Sstevel@tonic-gate */ 6600Sstevel@tonic-gate static int 6610Sstevel@tonic-gate mmioctl_get_mem_info(intptr_t data) 6620Sstevel@tonic-gate { 6630Sstevel@tonic-gate mem_info_t mem_info; 6640Sstevel@tonic-gate int err; 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate if (copyin((void *)data, &mem_info, sizeof (mem_info_t))) 6670Sstevel@tonic-gate return (EFAULT); 6680Sstevel@tonic-gate 6690Sstevel@tonic-gate if ((err = cpu_get_mem_info(mem_info.m_synd, mem_info.m_addr, 6700Sstevel@tonic-gate &mem_info.m_mem_size, &mem_info.m_seg_size, &mem_info.m_bank_size, 6710Sstevel@tonic-gate &mem_info.m_segments, &mem_info.m_banks, &mem_info.m_mcid)) != 0) 6720Sstevel@tonic-gate return (err); 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate if (copyout(&mem_info, (void *)data, sizeof (mem_info_t)) != 0) 6750Sstevel@tonic-gate return (EFAULT); 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate return (0); 6780Sstevel@tonic-gate } 6791186Sayznaga 6801186Sayznaga /* 6811186Sayznaga * Given a memory name, return its associated serial id 6821186Sayznaga */ 6831186Sayznaga static int 6841186Sayznaga mmioctl_get_mem_sid(intptr_t data) 6851186Sayznaga { 6861186Sayznaga mem_name_t mem_name; 6871186Sayznaga void *buf; 6881186Sayznaga void *name; 6891186Sayznaga size_t name_len; 6901186Sayznaga size_t bufsize; 6911186Sayznaga int len, err; 6921186Sayznaga 6931186Sayznaga if ((bufsize = cpu_get_name_bufsize()) == 0) 6941186Sayznaga return (ENOTSUP); 6951186Sayznaga 6961186Sayznaga if ((err = mm_read_mem_name(data, &mem_name)) < 0) 6971186Sayznaga return (err); 6981186Sayznaga 6991186Sayznaga buf = kmem_alloc(bufsize, KM_SLEEP); 7001186Sayznaga 7011186Sayznaga if (mem_name.m_namelen > 1024) 7021186Sayznaga mem_name.m_namelen = 1024; /* cap at 1024 bytes */ 7031186Sayznaga 7041186Sayznaga name = kmem_alloc(mem_name.m_namelen, KM_SLEEP); 7051186Sayznaga 7061186Sayznaga if ((err = copyinstr((char *)mem_name.m_name, (char *)name, 7071186Sayznaga mem_name.m_namelen, &name_len)) != 0) { 7081186Sayznaga kmem_free(buf, bufsize); 7091186Sayznaga kmem_free(name, mem_name.m_namelen); 7101186Sayznaga return (err); 7111186Sayznaga } 7121186Sayznaga 7131186Sayznaga /* 7141186Sayznaga * Call into cpu specific code to do the lookup. 7151186Sayznaga */ 7161186Sayznaga if ((err = cpu_get_mem_sid(name, buf, bufsize, &len)) != 0) { 7171186Sayznaga kmem_free(buf, bufsize); 7181186Sayznaga kmem_free(name, mem_name.m_namelen); 7191186Sayznaga return (err); 7201186Sayznaga } 7211186Sayznaga 7221186Sayznaga if (len > mem_name.m_sidlen) { 7231186Sayznaga kmem_free(buf, bufsize); 7241186Sayznaga kmem_free(name, mem_name.m_namelen); 7251186Sayznaga return (ENAMETOOLONG); 7261186Sayznaga } 7271186Sayznaga 7281186Sayznaga if (copyoutstr(buf, (char *)mem_name.m_sid, 7291186Sayznaga mem_name.m_sidlen, NULL) != 0) { 7301186Sayznaga kmem_free(buf, bufsize); 7311186Sayznaga kmem_free(name, mem_name.m_namelen); 7321186Sayznaga return (EFAULT); 7331186Sayznaga } 7341186Sayznaga 7351186Sayznaga kmem_free(buf, bufsize); 7361186Sayznaga kmem_free(name, mem_name.m_namelen); 7371186Sayznaga return (0); 7381186Sayznaga } 7390Sstevel@tonic-gate #endif /* __sparc */ 7400Sstevel@tonic-gate 7410Sstevel@tonic-gate /* 7420Sstevel@tonic-gate * Private ioctls for 7430Sstevel@tonic-gate * libkvm to support kvm_physaddr(). 7440Sstevel@tonic-gate * FMA support for page_retire() and memory attribute information. 7450Sstevel@tonic-gate */ 7460Sstevel@tonic-gate /*ARGSUSED*/ 7470Sstevel@tonic-gate static int 7480Sstevel@tonic-gate mmioctl(dev_t dev, int cmd, intptr_t data, int flag, cred_t *cred, int *rvalp) 7490Sstevel@tonic-gate { 7501283Sayznaga if ((cmd == MEM_VTOP && getminor(dev) != M_KMEM) || 7511283Sayznaga (cmd != MEM_VTOP && getminor(dev) != M_MEM)) 7521186Sayznaga return (ENXIO); 7531186Sayznaga 7540Sstevel@tonic-gate switch (cmd) { 7550Sstevel@tonic-gate case MEM_VTOP: 7560Sstevel@tonic-gate return (mmioctl_vtop(data)); 7570Sstevel@tonic-gate 7580Sstevel@tonic-gate case MEM_PAGE_RETIRE: 7590Sstevel@tonic-gate case MEM_PAGE_ISRETIRED: 760917Selowe case MEM_PAGE_UNRETIRE: 761917Selowe case MEM_PAGE_RETIRE_MCE: 762917Selowe case MEM_PAGE_RETIRE_UE: 763917Selowe case MEM_PAGE_GETERRORS: 764917Selowe case MEM_PAGE_RETIRE_TEST: 7650Sstevel@tonic-gate return (mmioctl_page_retire(cmd, data)); 7660Sstevel@tonic-gate 7671186Sayznaga case MEM_PAGE_FMRI_RETIRE: 7681186Sayznaga case MEM_PAGE_FMRI_ISRETIRED: 7691186Sayznaga return (mmioctl_page_fmri_retire(cmd, data)); 7701186Sayznaga 7710Sstevel@tonic-gate #ifdef __sparc 7721186Sayznaga case MEM_NAME: 7730Sstevel@tonic-gate return (mmioctl_get_mem_name(data)); 7740Sstevel@tonic-gate 7750Sstevel@tonic-gate case MEM_INFO: 7760Sstevel@tonic-gate return (mmioctl_get_mem_info(data)); 7771186Sayznaga 7781186Sayznaga case MEM_SID: 7791186Sayznaga return (mmioctl_get_mem_sid(data)); 7800Sstevel@tonic-gate #else 7811186Sayznaga case MEM_NAME: 7821186Sayznaga case MEM_INFO: 7831186Sayznaga case MEM_SID: 7840Sstevel@tonic-gate return (ENOTSUP); 7851186Sayznaga #endif /* __sparc */ 7860Sstevel@tonic-gate } 7870Sstevel@tonic-gate return (ENXIO); 7880Sstevel@tonic-gate } 7890Sstevel@tonic-gate 7900Sstevel@tonic-gate /*ARGSUSED2*/ 7910Sstevel@tonic-gate static int 7920Sstevel@tonic-gate mmmmap(dev_t dev, off_t off, int prot) 7930Sstevel@tonic-gate { 7940Sstevel@tonic-gate pfn_t pf; 7950Sstevel@tonic-gate struct memlist *pmem; 7960Sstevel@tonic-gate minor_t minor = getminor(dev); 7970Sstevel@tonic-gate 7980Sstevel@tonic-gate switch (minor) { 7990Sstevel@tonic-gate case M_MEM: 8000Sstevel@tonic-gate pf = btop(off); 8010Sstevel@tonic-gate memlist_read_lock(); 8020Sstevel@tonic-gate for (pmem = phys_install; pmem != NULL; pmem = pmem->next) { 8030Sstevel@tonic-gate if (pf >= BTOP(pmem->address) && 8040Sstevel@tonic-gate pf < BTOP(pmem->address + pmem->size)) { 8050Sstevel@tonic-gate memlist_read_unlock(); 8060Sstevel@tonic-gate return (impl_obmem_pfnum(pf)); 8070Sstevel@tonic-gate } 8080Sstevel@tonic-gate } 8090Sstevel@tonic-gate memlist_read_unlock(); 8100Sstevel@tonic-gate break; 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate case M_KMEM: 8130Sstevel@tonic-gate case M_ALLKMEM: 8140Sstevel@tonic-gate /* no longer supported with KPR */ 8150Sstevel@tonic-gate return (-1); 8160Sstevel@tonic-gate 8170Sstevel@tonic-gate case M_ZERO: 8180Sstevel@tonic-gate /* 8190Sstevel@tonic-gate * We shouldn't be mmap'ing to /dev/zero here as 8200Sstevel@tonic-gate * mmsegmap() should have already converted 8210Sstevel@tonic-gate * a mapping request for this device to a mapping 8220Sstevel@tonic-gate * using seg_vn for anonymous memory. 8230Sstevel@tonic-gate */ 8240Sstevel@tonic-gate break; 8250Sstevel@tonic-gate 8260Sstevel@tonic-gate } 8270Sstevel@tonic-gate return (-1); 8280Sstevel@tonic-gate } 8290Sstevel@tonic-gate 8300Sstevel@tonic-gate /* 8310Sstevel@tonic-gate * This function is called when a memory device is mmap'ed. 8320Sstevel@tonic-gate * Set up the mapping to the correct device driver. 8330Sstevel@tonic-gate */ 8340Sstevel@tonic-gate static int 8350Sstevel@tonic-gate mmsegmap(dev_t dev, off_t off, struct as *as, caddr_t *addrp, off_t len, 8360Sstevel@tonic-gate uint_t prot, uint_t maxprot, uint_t flags, struct cred *cred) 8370Sstevel@tonic-gate { 8380Sstevel@tonic-gate struct segvn_crargs vn_a; 8390Sstevel@tonic-gate struct segdev_crargs dev_a; 8400Sstevel@tonic-gate int error; 8410Sstevel@tonic-gate minor_t minor; 8420Sstevel@tonic-gate off_t i; 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate minor = getminor(dev); 8450Sstevel@tonic-gate 8460Sstevel@tonic-gate as_rangelock(as); 8470Sstevel@tonic-gate if ((flags & MAP_FIXED) == 0) { 8480Sstevel@tonic-gate /* 8490Sstevel@tonic-gate * No need to worry about vac alignment on /dev/zero 8500Sstevel@tonic-gate * since this is a "clone" object that doesn't yet exist. 8510Sstevel@tonic-gate */ 8520Sstevel@tonic-gate map_addr(addrp, len, (offset_t)off, 8530Sstevel@tonic-gate (minor == M_MEM) || (minor == M_KMEM), flags); 8540Sstevel@tonic-gate 8550Sstevel@tonic-gate if (*addrp == NULL) { 8560Sstevel@tonic-gate as_rangeunlock(as); 8570Sstevel@tonic-gate return (ENOMEM); 8580Sstevel@tonic-gate } 8590Sstevel@tonic-gate } else { 8600Sstevel@tonic-gate /* 8610Sstevel@tonic-gate * User specified address - 8620Sstevel@tonic-gate * Blow away any previous mappings. 8630Sstevel@tonic-gate */ 8640Sstevel@tonic-gate (void) as_unmap(as, *addrp, len); 8650Sstevel@tonic-gate } 8660Sstevel@tonic-gate 8670Sstevel@tonic-gate switch (minor) { 8680Sstevel@tonic-gate case M_MEM: 8690Sstevel@tonic-gate /* /dev/mem cannot be mmap'ed with MAP_PRIVATE */ 8700Sstevel@tonic-gate if ((flags & MAP_TYPE) != MAP_SHARED) { 8710Sstevel@tonic-gate as_rangeunlock(as); 8720Sstevel@tonic-gate return (EINVAL); 8730Sstevel@tonic-gate } 8740Sstevel@tonic-gate 8750Sstevel@tonic-gate /* 8760Sstevel@tonic-gate * Check to ensure that the entire range is 8770Sstevel@tonic-gate * legal and we are not trying to map in 8780Sstevel@tonic-gate * more than the device will let us. 8790Sstevel@tonic-gate */ 8800Sstevel@tonic-gate for (i = 0; i < len; i += PAGESIZE) { 8810Sstevel@tonic-gate if (mmmmap(dev, off + i, maxprot) == -1) { 8820Sstevel@tonic-gate as_rangeunlock(as); 8830Sstevel@tonic-gate return (ENXIO); 8840Sstevel@tonic-gate } 8850Sstevel@tonic-gate } 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate /* 8880Sstevel@tonic-gate * Use seg_dev segment driver for /dev/mem mapping. 8890Sstevel@tonic-gate */ 8900Sstevel@tonic-gate dev_a.mapfunc = mmmmap; 8910Sstevel@tonic-gate dev_a.dev = dev; 8920Sstevel@tonic-gate dev_a.offset = off; 8930Sstevel@tonic-gate dev_a.type = (flags & MAP_TYPE); 8940Sstevel@tonic-gate dev_a.prot = (uchar_t)prot; 8950Sstevel@tonic-gate dev_a.maxprot = (uchar_t)maxprot; 8960Sstevel@tonic-gate dev_a.hat_attr = 0; 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate /* 8990Sstevel@tonic-gate * Make /dev/mem mappings non-consistent since we can't 9000Sstevel@tonic-gate * alias pages that don't have page structs behind them, 9010Sstevel@tonic-gate * such as kernel stack pages. If someone mmap()s a kernel 9020Sstevel@tonic-gate * stack page and if we give him a tte with cv, a line from 9030Sstevel@tonic-gate * that page can get into both pages of the spitfire d$. 9040Sstevel@tonic-gate * But snoop from another processor will only invalidate 9050Sstevel@tonic-gate * the first page. This later caused kernel (xc_attention) 9060Sstevel@tonic-gate * to go into an infinite loop at pil 13 and no interrupts 9070Sstevel@tonic-gate * could come in. See 1203630. 9080Sstevel@tonic-gate * 9090Sstevel@tonic-gate */ 9100Sstevel@tonic-gate dev_a.hat_flags = HAT_LOAD_NOCONSIST; 9110Sstevel@tonic-gate dev_a.devmap_data = NULL; 9120Sstevel@tonic-gate 9130Sstevel@tonic-gate error = as_map(as, *addrp, len, segdev_create, &dev_a); 9140Sstevel@tonic-gate break; 9150Sstevel@tonic-gate 9160Sstevel@tonic-gate case M_ZERO: 9170Sstevel@tonic-gate /* 9180Sstevel@tonic-gate * Use seg_vn segment driver for /dev/zero mapping. 9190Sstevel@tonic-gate * Passing in a NULL amp gives us the "cloning" effect. 9200Sstevel@tonic-gate */ 9210Sstevel@tonic-gate vn_a.vp = NULL; 9220Sstevel@tonic-gate vn_a.offset = 0; 9230Sstevel@tonic-gate vn_a.type = (flags & MAP_TYPE); 9240Sstevel@tonic-gate vn_a.prot = prot; 9250Sstevel@tonic-gate vn_a.maxprot = maxprot; 9260Sstevel@tonic-gate vn_a.flags = flags & ~MAP_TYPE; 9270Sstevel@tonic-gate vn_a.cred = cred; 9280Sstevel@tonic-gate vn_a.amp = NULL; 9290Sstevel@tonic-gate vn_a.szc = 0; 9300Sstevel@tonic-gate vn_a.lgrp_mem_policy_flags = 0; 9310Sstevel@tonic-gate error = as_map(as, *addrp, len, segvn_create, &vn_a); 9320Sstevel@tonic-gate break; 9330Sstevel@tonic-gate 9340Sstevel@tonic-gate case M_KMEM: 9350Sstevel@tonic-gate case M_ALLKMEM: 9360Sstevel@tonic-gate /* No longer supported with KPR. */ 9370Sstevel@tonic-gate error = ENXIO; 9380Sstevel@tonic-gate break; 9390Sstevel@tonic-gate 9400Sstevel@tonic-gate case M_NULL: 9410Sstevel@tonic-gate /* 9420Sstevel@tonic-gate * Use seg_dev segment driver for /dev/null mapping. 9430Sstevel@tonic-gate */ 9440Sstevel@tonic-gate dev_a.mapfunc = mmmmap; 9450Sstevel@tonic-gate dev_a.dev = dev; 9460Sstevel@tonic-gate dev_a.offset = off; 9470Sstevel@tonic-gate dev_a.type = 0; /* neither PRIVATE nor SHARED */ 9480Sstevel@tonic-gate dev_a.prot = dev_a.maxprot = (uchar_t)PROT_NONE; 9490Sstevel@tonic-gate dev_a.hat_attr = 0; 9500Sstevel@tonic-gate dev_a.hat_flags = 0; 9510Sstevel@tonic-gate error = as_map(as, *addrp, len, segdev_create, &dev_a); 9520Sstevel@tonic-gate break; 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate default: 9550Sstevel@tonic-gate error = ENXIO; 9560Sstevel@tonic-gate } 9570Sstevel@tonic-gate 9580Sstevel@tonic-gate as_rangeunlock(as); 9590Sstevel@tonic-gate return (error); 9600Sstevel@tonic-gate } 9610Sstevel@tonic-gate 9620Sstevel@tonic-gate static struct cb_ops mm_cb_ops = { 9630Sstevel@tonic-gate mmopen, /* open */ 9640Sstevel@tonic-gate nulldev, /* close */ 9650Sstevel@tonic-gate nodev, /* strategy */ 9660Sstevel@tonic-gate nodev, /* print */ 9670Sstevel@tonic-gate nodev, /* dump */ 9680Sstevel@tonic-gate mmread, /* read */ 9690Sstevel@tonic-gate mmwrite, /* write */ 9700Sstevel@tonic-gate mmioctl, /* ioctl */ 9710Sstevel@tonic-gate nodev, /* devmap */ 9720Sstevel@tonic-gate mmmmap, /* mmap */ 9730Sstevel@tonic-gate mmsegmap, /* segmap */ 9740Sstevel@tonic-gate mmchpoll, /* poll */ 9750Sstevel@tonic-gate mmpropop, /* prop_op */ 9760Sstevel@tonic-gate 0, /* streamtab */ 9770Sstevel@tonic-gate D_NEW | D_MP | D_64BIT | D_U64BIT 9780Sstevel@tonic-gate }; 9790Sstevel@tonic-gate 9800Sstevel@tonic-gate static struct dev_ops mm_ops = { 9810Sstevel@tonic-gate DEVO_REV, /* devo_rev, */ 9820Sstevel@tonic-gate 0, /* refcnt */ 9830Sstevel@tonic-gate mm_info, /* get_dev_info */ 9840Sstevel@tonic-gate nulldev, /* identify */ 9850Sstevel@tonic-gate nulldev, /* probe */ 9860Sstevel@tonic-gate mm_attach, /* attach */ 9870Sstevel@tonic-gate nodev, /* detach */ 9880Sstevel@tonic-gate nodev, /* reset */ 9890Sstevel@tonic-gate &mm_cb_ops, /* driver operations */ 9900Sstevel@tonic-gate (struct bus_ops *)0 /* bus operations */ 9910Sstevel@tonic-gate }; 9920Sstevel@tonic-gate 9930Sstevel@tonic-gate static struct modldrv modldrv = { 9940Sstevel@tonic-gate &mod_driverops, "memory driver %I%", &mm_ops, 9950Sstevel@tonic-gate }; 9960Sstevel@tonic-gate 9970Sstevel@tonic-gate static struct modlinkage modlinkage = { 9980Sstevel@tonic-gate MODREV_1, &modldrv, NULL 9990Sstevel@tonic-gate }; 10000Sstevel@tonic-gate 10010Sstevel@tonic-gate int 10020Sstevel@tonic-gate _init(void) 10030Sstevel@tonic-gate { 10040Sstevel@tonic-gate return (mod_install(&modlinkage)); 10050Sstevel@tonic-gate } 10060Sstevel@tonic-gate 10070Sstevel@tonic-gate int 10080Sstevel@tonic-gate _info(struct modinfo *modinfop) 10090Sstevel@tonic-gate { 10100Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 10110Sstevel@tonic-gate } 10120Sstevel@tonic-gate 10130Sstevel@tonic-gate int 10140Sstevel@tonic-gate _fini(void) 10150Sstevel@tonic-gate { 10160Sstevel@tonic-gate return (mod_remove(&modlinkage)); 10170Sstevel@tonic-gate } 10180Sstevel@tonic-gate 10190Sstevel@tonic-gate static int 10200Sstevel@tonic-gate mm_kstat_update(kstat_t *ksp, int rw) 10210Sstevel@tonic-gate { 10220Sstevel@tonic-gate struct memlist *pmem; 10230Sstevel@tonic-gate uint_t count; 10240Sstevel@tonic-gate 10250Sstevel@tonic-gate if (rw == KSTAT_WRITE) 10260Sstevel@tonic-gate return (EACCES); 10270Sstevel@tonic-gate 10280Sstevel@tonic-gate count = 0; 10290Sstevel@tonic-gate memlist_read_lock(); 10300Sstevel@tonic-gate for (pmem = phys_install; pmem != NULL; pmem = pmem->next) { 10310Sstevel@tonic-gate count++; 10320Sstevel@tonic-gate } 10330Sstevel@tonic-gate memlist_read_unlock(); 10340Sstevel@tonic-gate 10350Sstevel@tonic-gate ksp->ks_ndata = count; 10360Sstevel@tonic-gate ksp->ks_data_size = count * 2 * sizeof (uint64_t); 10370Sstevel@tonic-gate 10380Sstevel@tonic-gate return (0); 10390Sstevel@tonic-gate } 10400Sstevel@tonic-gate 10410Sstevel@tonic-gate static int 10420Sstevel@tonic-gate mm_kstat_snapshot(kstat_t *ksp, void *buf, int rw) 10430Sstevel@tonic-gate { 10440Sstevel@tonic-gate struct memlist *pmem; 10450Sstevel@tonic-gate struct memunit { 10460Sstevel@tonic-gate uint64_t address; 10470Sstevel@tonic-gate uint64_t size; 10480Sstevel@tonic-gate } *kspmem; 10490Sstevel@tonic-gate 10500Sstevel@tonic-gate if (rw == KSTAT_WRITE) 10510Sstevel@tonic-gate return (EACCES); 10520Sstevel@tonic-gate 10530Sstevel@tonic-gate ksp->ks_snaptime = gethrtime(); 10540Sstevel@tonic-gate 10550Sstevel@tonic-gate kspmem = (struct memunit *)buf; 10560Sstevel@tonic-gate memlist_read_lock(); 10570Sstevel@tonic-gate for (pmem = phys_install; pmem != NULL; pmem = pmem->next, kspmem++) { 10580Sstevel@tonic-gate if ((caddr_t)kspmem >= (caddr_t)buf + ksp->ks_data_size) 10590Sstevel@tonic-gate break; 10600Sstevel@tonic-gate kspmem->address = pmem->address; 10610Sstevel@tonic-gate kspmem->size = pmem->size; 10620Sstevel@tonic-gate } 10630Sstevel@tonic-gate memlist_read_unlock(); 10640Sstevel@tonic-gate 10650Sstevel@tonic-gate return (0); 10660Sstevel@tonic-gate } 10671186Sayznaga 10681186Sayznaga /* 10691186Sayznaga * Read a mem_name_t from user-space and store it in the mem_name_t 10701186Sayznaga * pointed to by the mem_name argument. 10711186Sayznaga */ 10721186Sayznaga static int 10731186Sayznaga mm_read_mem_name(intptr_t data, mem_name_t *mem_name) 10741186Sayznaga { 10751186Sayznaga if (get_udatamodel() == DATAMODEL_NATIVE) { 10761186Sayznaga if (copyin((void *)data, mem_name, sizeof (mem_name_t))) 10771186Sayznaga return (EFAULT); 10781186Sayznaga } 10791186Sayznaga #ifdef _SYSCALL32 10801186Sayznaga else { 10811186Sayznaga mem_name32_t mem_name32; 10821186Sayznaga 10831186Sayznaga if (copyin((void *)data, &mem_name32, sizeof (mem_name32_t))) 10841186Sayznaga return (EFAULT); 10851186Sayznaga mem_name->m_addr = mem_name32.m_addr; 10861186Sayznaga mem_name->m_synd = mem_name32.m_synd; 10871186Sayznaga mem_name->m_type[0] = mem_name32.m_type[0]; 10881186Sayznaga mem_name->m_type[1] = mem_name32.m_type[1]; 10891283Sayznaga mem_name->m_name = (caddr_t)(uintptr_t)mem_name32.m_name; 10901186Sayznaga mem_name->m_namelen = (size_t)mem_name32.m_namelen; 10911283Sayznaga mem_name->m_sid = (caddr_t)(uintptr_t)mem_name32.m_sid; 10921186Sayznaga mem_name->m_sidlen = (size_t)mem_name32.m_sidlen; 10931186Sayznaga } 10941186Sayznaga #endif /* _SYSCALL32 */ 10951186Sayznaga 10961186Sayznaga return (0); 10971186Sayznaga } 10981186Sayznaga 10991186Sayznaga /* 11001186Sayznaga * Read a mem_page_t from user-space and store it in the mem_page_t 11011186Sayznaga * pointed to by the mpage argument. 11021186Sayznaga */ 11031186Sayznaga static int 11041186Sayznaga mm_read_mem_page(intptr_t data, mem_page_t *mpage) 11051186Sayznaga { 11061186Sayznaga if (get_udatamodel() == DATAMODEL_NATIVE) { 11071186Sayznaga if (copyin((void *)data, mpage, sizeof (mem_page_t)) != 0) 11081186Sayznaga return (EFAULT); 11091186Sayznaga } 11101186Sayznaga #ifdef _SYSCALL32 11111186Sayznaga else { 11121186Sayznaga mem_page32_t mpage32; 11131186Sayznaga 11141186Sayznaga if (copyin((void *)data, &mpage32, sizeof (mem_page32_t)) != 0) 11151186Sayznaga return (EFAULT); 11161186Sayznaga 11171186Sayznaga mpage->m_fmri = (caddr_t)(uintptr_t)mpage32.m_fmri; 11181186Sayznaga mpage->m_fmrisz = mpage32.m_fmrisz; 11191186Sayznaga } 11201186Sayznaga #endif /* _SYSCALL32 */ 11211186Sayznaga 11221186Sayznaga return (0); 11231186Sayznaga } 11241186Sayznaga 11251186Sayznaga /* 11261186Sayznaga * Expand an FMRI from a mem_page_t. 11271186Sayznaga */ 11281186Sayznaga static int 11291186Sayznaga mm_get_mem_fmri(mem_page_t *mpage, nvlist_t **nvl) 11301186Sayznaga { 11311186Sayznaga char *buf; 11321186Sayznaga int err; 11331186Sayznaga 11341186Sayznaga if (mpage->m_fmri == NULL || mpage->m_fmrisz > MEM_FMRI_MAX_BUFSIZE) 11351186Sayznaga return (EINVAL); 11361186Sayznaga 11371186Sayznaga buf = kmem_alloc(mpage->m_fmrisz, KM_SLEEP); 11381186Sayznaga if (copyin(mpage->m_fmri, buf, mpage->m_fmrisz) != 0) { 11391186Sayznaga kmem_free(buf, mpage->m_fmrisz); 11401186Sayznaga return (EFAULT); 11411186Sayznaga } 11421186Sayznaga 11431186Sayznaga err = nvlist_unpack(buf, mpage->m_fmrisz, nvl, KM_SLEEP); 11441186Sayznaga kmem_free(buf, mpage->m_fmrisz); 11451186Sayznaga 11461186Sayznaga return (err); 11471186Sayznaga } 11481186Sayznaga 11491186Sayznaga static int 11501186Sayznaga mm_get_paddr(nvlist_t *nvl, uint64_t *paddr) 11511186Sayznaga { 11521186Sayznaga uint8_t version; 11531189Sayznaga uint64_t pa; 11541186Sayznaga char *scheme; 11551189Sayznaga #ifdef __sparc 11561189Sayznaga uint64_t offset; 11571186Sayznaga char *unum; 11581186Sayznaga char **serids; 11591186Sayznaga uint_t nserids; 11601186Sayznaga int err; 11611189Sayznaga #endif 11621186Sayznaga 11631186Sayznaga /* Verify FMRI scheme name and version number */ 11641186Sayznaga if ((nvlist_lookup_string(nvl, FM_FMRI_SCHEME, &scheme) != 0) || 11651186Sayznaga (strcmp(scheme, FM_FMRI_SCHEME_MEM) != 0) || 11661186Sayznaga (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0) || 11671186Sayznaga version > FM_MEM_SCHEME_VERSION) { 11681186Sayznaga return (EINVAL); 11691186Sayznaga } 11701186Sayznaga 11711186Sayznaga /* 11721186Sayznaga * There are two ways a physical address can be obtained from a mem 11731186Sayznaga * scheme FMRI. One way is to use the "offset" and "serial" 11741186Sayznaga * members, if they are present, together with the "unum" member to 11751186Sayznaga * calculate a physical address. This is the preferred way since 11761186Sayznaga * it is independent of possible changes to the programming of 11771186Sayznaga * underlying hardware registers that may change the physical address. 11781186Sayznaga * If the "offset" member is not present, then the address is 11791186Sayznaga * retrieved from the "physaddr" member. 11801186Sayznaga */ 11811414Scindi #if defined(__sparc) 11821186Sayznaga if (nvlist_lookup_uint64(nvl, FM_FMRI_MEM_OFFSET, &offset) != 0) { 11831186Sayznaga if (nvlist_lookup_uint64(nvl, FM_FMRI_MEM_PHYSADDR, &pa) != 11841186Sayznaga 0) { 11851186Sayznaga return (EINVAL); 11861186Sayznaga } 11871186Sayznaga } else if (nvlist_lookup_string(nvl, FM_FMRI_MEM_UNUM, &unum) != 0 || 11881186Sayznaga nvlist_lookup_string_array(nvl, FM_FMRI_MEM_SERIAL_ID, &serids, 11891186Sayznaga &nserids) != 0) { 11901186Sayznaga return (EINVAL); 11911186Sayznaga } else { 11921463Sayznaga err = cpu_get_mem_addr(unum, serids[0], offset, &pa); 11931463Sayznaga if (err != 0) { 11941463Sayznaga if (err == ENOTSUP) { 11951463Sayznaga /* Fall back to physaddr */ 11961463Sayznaga if (nvlist_lookup_uint64(nvl, 11971463Sayznaga FM_FMRI_MEM_PHYSADDR, &pa) != 0) 11981463Sayznaga return (EINVAL); 11991463Sayznaga } else 12001463Sayznaga return (err); 12011463Sayznaga } 12021186Sayznaga } 1203*3446Smrj #elif defined(__x86) 12041414Scindi if (cmi_mc_unumtopa(NULL, nvl, &pa) == 0) 12051187Sayznaga return (EINVAL); 12061414Scindi #else 12071414Scindi #error "port me" 12081187Sayznaga #endif /* __sparc */ 12091186Sayznaga 12101186Sayznaga *paddr = pa; 12111186Sayznaga return (0); 12121186Sayznaga } 1213