17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * Memory special file 317c478bd9Sstevel@tonic-gate */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include <sys/types.h> 347c478bd9Sstevel@tonic-gate #include <sys/param.h> 357c478bd9Sstevel@tonic-gate #include <sys/user.h> 367c478bd9Sstevel@tonic-gate #include <sys/buf.h> 377c478bd9Sstevel@tonic-gate #include <sys/systm.h> 387c478bd9Sstevel@tonic-gate #include <sys/cred.h> 397c478bd9Sstevel@tonic-gate #include <sys/vm.h> 407c478bd9Sstevel@tonic-gate #include <sys/uio.h> 417c478bd9Sstevel@tonic-gate #include <sys/mman.h> 427c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 437c478bd9Sstevel@tonic-gate #include <vm/seg.h> 447c478bd9Sstevel@tonic-gate #include <vm/page.h> 457c478bd9Sstevel@tonic-gate #include <sys/stat.h> 467c478bd9Sstevel@tonic-gate #include <sys/vmem.h> 477c478bd9Sstevel@tonic-gate #include <sys/memlist.h> 487c478bd9Sstevel@tonic-gate #include <sys/bootconf.h> 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h> 517c478bd9Sstevel@tonic-gate #include <vm/seg_dev.h> 527c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h> 537c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h> 547c478bd9Sstevel@tonic-gate #include <vm/seg_kpm.h> 557c478bd9Sstevel@tonic-gate #include <vm/hat.h> 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #include <sys/conf.h> 587c478bd9Sstevel@tonic-gate #include <sys/mem.h> 597c478bd9Sstevel@tonic-gate #include <sys/types.h> 607c478bd9Sstevel@tonic-gate #include <sys/conf.h> 617c478bd9Sstevel@tonic-gate #include <sys/param.h> 627c478bd9Sstevel@tonic-gate #include <sys/systm.h> 637c478bd9Sstevel@tonic-gate #include <sys/errno.h> 647c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 657c478bd9Sstevel@tonic-gate #include <sys/memlist.h> 667c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 677c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 687c478bd9Sstevel@tonic-gate #include <sys/debug.h> 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate #ifdef __sparc 717c478bd9Sstevel@tonic-gate extern int cpu_get_mem_name(uint64_t, uint64_t *, uint64_t, char *, int, int *); 727c478bd9Sstevel@tonic-gate extern int cpu_get_mem_info(uint64_t, uint64_t, uint64_t *, uint64_t *, 737c478bd9Sstevel@tonic-gate uint64_t *, int *, int *, int *); 747c478bd9Sstevel@tonic-gate extern size_t cpu_get_name_bufsize(void); 757c478bd9Sstevel@tonic-gate #endif 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate /* 787c478bd9Sstevel@tonic-gate * Turn a byte length into a pagecount. The DDI btop takes a 797c478bd9Sstevel@tonic-gate * 32-bit size on 32-bit machines, this handles 64-bit sizes for 807c478bd9Sstevel@tonic-gate * large physical-memory 32-bit machines. 817c478bd9Sstevel@tonic-gate */ 827c478bd9Sstevel@tonic-gate #define BTOP(x) ((pgcnt_t)((x) >> _pageshift)) 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate static kmutex_t mm_lock; 857c478bd9Sstevel@tonic-gate static caddr_t mm_map; 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate static dev_info_t *mm_dip; /* private copy of devinfo pointer */ 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate static int mm_kmem_io_access; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate static int mm_kstat_update(kstat_t *ksp, int rw); 927c478bd9Sstevel@tonic-gate static int mm_kstat_snapshot(kstat_t *ksp, void *buf, int rw); 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate /*ARGSUSED1*/ 957c478bd9Sstevel@tonic-gate static int 967c478bd9Sstevel@tonic-gate mm_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 977c478bd9Sstevel@tonic-gate { 987c478bd9Sstevel@tonic-gate int i; 997c478bd9Sstevel@tonic-gate struct mem_minor { 1007c478bd9Sstevel@tonic-gate char *name; 1017c478bd9Sstevel@tonic-gate minor_t minor; 1027c478bd9Sstevel@tonic-gate int privonly; 1037c478bd9Sstevel@tonic-gate const char *rdpriv; 1047c478bd9Sstevel@tonic-gate const char *wrpriv; 1057c478bd9Sstevel@tonic-gate mode_t priv_mode; 1067c478bd9Sstevel@tonic-gate } mm[] = { 1077c478bd9Sstevel@tonic-gate { "mem", M_MEM, 0, NULL, "all", 0640 }, 1087c478bd9Sstevel@tonic-gate { "kmem", M_KMEM, 0, NULL, "all", 0640 }, 1097c478bd9Sstevel@tonic-gate { "allkmem", M_ALLKMEM, 0, "all", "all", 0600 }, 1107c478bd9Sstevel@tonic-gate { "null", M_NULL, PRIVONLY_DEV, NULL, NULL, 0666 }, 1117c478bd9Sstevel@tonic-gate { "zero", M_ZERO, PRIVONLY_DEV, NULL, NULL, 0666 }, 1127c478bd9Sstevel@tonic-gate }; 1137c478bd9Sstevel@tonic-gate kstat_t *ksp; 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate mutex_init(&mm_lock, NULL, MUTEX_DEFAULT, NULL); 1167c478bd9Sstevel@tonic-gate mm_map = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP); 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate for (i = 0; i < (sizeof (mm) / sizeof (mm[0])); i++) { 1197c478bd9Sstevel@tonic-gate if (ddi_create_priv_minor_node(devi, mm[i].name, S_IFCHR, 1207c478bd9Sstevel@tonic-gate mm[i].minor, DDI_PSEUDO, mm[i].privonly, 1217c478bd9Sstevel@tonic-gate mm[i].rdpriv, mm[i].wrpriv, mm[i].priv_mode) == 1227c478bd9Sstevel@tonic-gate DDI_FAILURE) { 1237c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 1247c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate mm_dip = devi; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate ksp = kstat_create("mm", 0, "phys_installed", "misc", 1317c478bd9Sstevel@tonic-gate KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VAR_SIZE | KSTAT_FLAG_VIRTUAL); 1327c478bd9Sstevel@tonic-gate if (ksp != NULL) { 1337c478bd9Sstevel@tonic-gate ksp->ks_update = mm_kstat_update; 1347c478bd9Sstevel@tonic-gate ksp->ks_snapshot = mm_kstat_snapshot; 1357c478bd9Sstevel@tonic-gate ksp->ks_lock = &mm_lock; /* XXX - not really needed */ 1367c478bd9Sstevel@tonic-gate kstat_install(ksp); 1377c478bd9Sstevel@tonic-gate } 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate mm_kmem_io_access = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 1407c478bd9Sstevel@tonic-gate "kmem_io_access", 0); 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 1437c478bd9Sstevel@tonic-gate } 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1467c478bd9Sstevel@tonic-gate static int 1477c478bd9Sstevel@tonic-gate mm_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 1487c478bd9Sstevel@tonic-gate { 1497c478bd9Sstevel@tonic-gate register int error; 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate switch (infocmd) { 1527c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 1537c478bd9Sstevel@tonic-gate *result = (void *)mm_dip; 1547c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 1557c478bd9Sstevel@tonic-gate break; 1567c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 1577c478bd9Sstevel@tonic-gate *result = (void *)0; 1587c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 1597c478bd9Sstevel@tonic-gate break; 1607c478bd9Sstevel@tonic-gate default: 1617c478bd9Sstevel@tonic-gate error = DDI_FAILURE; 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate return (error); 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate /*ARGSUSED1*/ 1677c478bd9Sstevel@tonic-gate static int 1687c478bd9Sstevel@tonic-gate mmopen(dev_t *devp, int flag, int typ, struct cred *cred) 1697c478bd9Sstevel@tonic-gate { 1707c478bd9Sstevel@tonic-gate switch (getminor(*devp)) { 1717c478bd9Sstevel@tonic-gate case M_NULL: 1727c478bd9Sstevel@tonic-gate case M_ZERO: 1737c478bd9Sstevel@tonic-gate case M_MEM: 1747c478bd9Sstevel@tonic-gate case M_KMEM: 1757c478bd9Sstevel@tonic-gate case M_ALLKMEM: 1767c478bd9Sstevel@tonic-gate /* standard devices */ 1777c478bd9Sstevel@tonic-gate break; 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate default: 1807c478bd9Sstevel@tonic-gate /* Unsupported or unknown type */ 1817c478bd9Sstevel@tonic-gate return (EINVAL); 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate return (0); 1847c478bd9Sstevel@tonic-gate } 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate struct pollhead mm_pollhd; 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1897c478bd9Sstevel@tonic-gate static int 1907c478bd9Sstevel@tonic-gate mmchpoll(dev_t dev, short events, int anyyet, short *reventsp, 1917c478bd9Sstevel@tonic-gate struct pollhead **phpp) 1927c478bd9Sstevel@tonic-gate { 1937c478bd9Sstevel@tonic-gate switch (getminor(dev)) { 1947c478bd9Sstevel@tonic-gate case M_NULL: 1957c478bd9Sstevel@tonic-gate case M_ZERO: 1967c478bd9Sstevel@tonic-gate case M_MEM: 1977c478bd9Sstevel@tonic-gate case M_KMEM: 1987c478bd9Sstevel@tonic-gate case M_ALLKMEM: 1997c478bd9Sstevel@tonic-gate *reventsp = events & (POLLIN | POLLOUT | POLLPRI | POLLRDNORM | 2007c478bd9Sstevel@tonic-gate POLLWRNORM | POLLRDBAND | POLLWRBAND); 2017c478bd9Sstevel@tonic-gate /* 2027c478bd9Sstevel@tonic-gate * A non NULL pollhead pointer should be returned in case 2037c478bd9Sstevel@tonic-gate * user polls for 0 events. 2047c478bd9Sstevel@tonic-gate */ 2057c478bd9Sstevel@tonic-gate *phpp = !anyyet && !*reventsp ? 2067c478bd9Sstevel@tonic-gate &mm_pollhd : (struct pollhead *)NULL; 2077c478bd9Sstevel@tonic-gate return (0); 2087c478bd9Sstevel@tonic-gate default: 2097c478bd9Sstevel@tonic-gate /* no other devices currently support polling */ 2107c478bd9Sstevel@tonic-gate return (ENXIO); 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate static int 2157c478bd9Sstevel@tonic-gate mmpropop(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int flags, 2167c478bd9Sstevel@tonic-gate char *name, caddr_t valuep, int *lengthp) 2177c478bd9Sstevel@tonic-gate { 2187c478bd9Sstevel@tonic-gate /* 2197c478bd9Sstevel@tonic-gate * implement zero size to reduce overhead (avoid two failing 2207c478bd9Sstevel@tonic-gate * property lookups per stat). 2217c478bd9Sstevel@tonic-gate */ 2227c478bd9Sstevel@tonic-gate return (ddi_prop_op_size(dev, dip, prop_op, 2237c478bd9Sstevel@tonic-gate flags, name, valuep, lengthp, 0)); 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate static int 2277c478bd9Sstevel@tonic-gate mmio(struct uio *uio, enum uio_rw rw, pfn_t pfn, off_t pageoff, int allowio) 2287c478bd9Sstevel@tonic-gate { 2297c478bd9Sstevel@tonic-gate int error = 0; 2307c478bd9Sstevel@tonic-gate size_t nbytes = MIN((size_t)(PAGESIZE - pageoff), 2317c478bd9Sstevel@tonic-gate (size_t)uio->uio_iov->iov_len); 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate mutex_enter(&mm_lock); 2347c478bd9Sstevel@tonic-gate hat_devload(kas.a_hat, mm_map, PAGESIZE, pfn, 2357c478bd9Sstevel@tonic-gate (uint_t)(rw == UIO_READ ? PROT_READ : PROT_READ | PROT_WRITE), 2367c478bd9Sstevel@tonic-gate HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK); 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate if (!pf_is_memory(pfn)) { 2397c478bd9Sstevel@tonic-gate if (allowio) { 2407c478bd9Sstevel@tonic-gate size_t c = uio->uio_iov->iov_len; 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate if (ddi_peekpokeio(NULL, uio, rw, 2437c478bd9Sstevel@tonic-gate (caddr_t)(uintptr_t)uio->uio_loffset, c, 2447c478bd9Sstevel@tonic-gate sizeof (int32_t)) != DDI_SUCCESS) 2457c478bd9Sstevel@tonic-gate error = EFAULT; 2467c478bd9Sstevel@tonic-gate } else 2477c478bd9Sstevel@tonic-gate error = EIO; 2487c478bd9Sstevel@tonic-gate } else 2497c478bd9Sstevel@tonic-gate error = uiomove(&mm_map[pageoff], nbytes, rw, uio); 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate hat_unload(kas.a_hat, mm_map, PAGESIZE, HAT_UNLOAD_UNLOCK); 2527c478bd9Sstevel@tonic-gate mutex_exit(&mm_lock); 2537c478bd9Sstevel@tonic-gate return (error); 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate #ifdef __sparc 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate #define IS_KPM_VA(va) \ 2597c478bd9Sstevel@tonic-gate (kpm_enable && (va) >= segkpm->s_base && \ 2607c478bd9Sstevel@tonic-gate (va) < (segkpm->s_base + segkpm->s_size)) 2617c478bd9Sstevel@tonic-gate #define IS_KP_VA(va) \ 2627c478bd9Sstevel@tonic-gate ((va) >= segkp->s_base && (va) < segkp->s_base + segkp->s_size) 2637c478bd9Sstevel@tonic-gate #define NEED_LOCK_KVADDR(va) (!IS_KPM_VA(va) && !IS_KP_VA(va)) 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate #else /* __i386, __amd64 */ 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate #define NEED_LOCK_KVADDR(va) 0 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate #endif /* __sparc */ 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate /*ARGSUSED3*/ 2727c478bd9Sstevel@tonic-gate static int 2737c478bd9Sstevel@tonic-gate mmrw(dev_t dev, struct uio *uio, enum uio_rw rw, cred_t *cred) 2747c478bd9Sstevel@tonic-gate { 2757c478bd9Sstevel@tonic-gate pfn_t v; 2767c478bd9Sstevel@tonic-gate struct iovec *iov; 2777c478bd9Sstevel@tonic-gate int error = 0; 2787c478bd9Sstevel@tonic-gate size_t c; 2797c478bd9Sstevel@tonic-gate ssize_t oresid = uio->uio_resid; 2807c478bd9Sstevel@tonic-gate minor_t minor = getminor(dev); 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate while (uio->uio_resid > 0 && error == 0) { 2837c478bd9Sstevel@tonic-gate iov = uio->uio_iov; 2847c478bd9Sstevel@tonic-gate if (iov->iov_len == 0) { 2857c478bd9Sstevel@tonic-gate uio->uio_iov++; 2867c478bd9Sstevel@tonic-gate uio->uio_iovcnt--; 2877c478bd9Sstevel@tonic-gate if (uio->uio_iovcnt < 0) 2887c478bd9Sstevel@tonic-gate panic("mmrw"); 2897c478bd9Sstevel@tonic-gate continue; 2907c478bd9Sstevel@tonic-gate } 2917c478bd9Sstevel@tonic-gate switch (minor) { 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate case M_MEM: 2947c478bd9Sstevel@tonic-gate memlist_read_lock(); 2957c478bd9Sstevel@tonic-gate if (!address_in_memlist(phys_install, 2967c478bd9Sstevel@tonic-gate (uint64_t)uio->uio_loffset, 1)) { 2977c478bd9Sstevel@tonic-gate memlist_read_unlock(); 2987c478bd9Sstevel@tonic-gate error = EFAULT; 2997c478bd9Sstevel@tonic-gate break; 3007c478bd9Sstevel@tonic-gate } 3017c478bd9Sstevel@tonic-gate memlist_read_unlock(); 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate v = BTOP((u_offset_t)uio->uio_loffset); 3047c478bd9Sstevel@tonic-gate error = mmio(uio, rw, v, 3057c478bd9Sstevel@tonic-gate uio->uio_loffset & PAGEOFFSET, 0); 3067c478bd9Sstevel@tonic-gate break; 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate case M_KMEM: 3097c478bd9Sstevel@tonic-gate case M_ALLKMEM: 3107c478bd9Sstevel@tonic-gate { 3117c478bd9Sstevel@tonic-gate page_t **ppp; 3127c478bd9Sstevel@tonic-gate caddr_t vaddr = (caddr_t)uio->uio_offset; 3137c478bd9Sstevel@tonic-gate int try_lock = NEED_LOCK_KVADDR(vaddr); 3147c478bd9Sstevel@tonic-gate int locked = 0; 3157c478bd9Sstevel@tonic-gate 3167c478bd9Sstevel@tonic-gate /* 3177c478bd9Sstevel@tonic-gate * If vaddr does not map a valid page, as_pagelock() 3187c478bd9Sstevel@tonic-gate * will return failure. Hence we can't check the 3197c478bd9Sstevel@tonic-gate * return value and return EFAULT here as we'd like. 3207c478bd9Sstevel@tonic-gate * seg_kp and seg_kpm do not properly support 3217c478bd9Sstevel@tonic-gate * as_pagelock() for this context so we avoid it 3227c478bd9Sstevel@tonic-gate * using the try_lock set check above. Some day when 3237c478bd9Sstevel@tonic-gate * the kernel page locking gets redesigned all this 3247c478bd9Sstevel@tonic-gate * muck can be cleaned up. 3257c478bd9Sstevel@tonic-gate */ 3267c478bd9Sstevel@tonic-gate if (try_lock) 3277c478bd9Sstevel@tonic-gate locked = (as_pagelock(&kas, &ppp, vaddr, 3287c478bd9Sstevel@tonic-gate PAGESIZE, S_WRITE) == 0); 3297c478bd9Sstevel@tonic-gate 330*e42d2a1cSjongkis v = hat_getpfnum(kas.a_hat, 331*e42d2a1cSjongkis (caddr_t)(uintptr_t)uio->uio_loffset); 3327c478bd9Sstevel@tonic-gate if (v == PFN_INVALID) { 3337c478bd9Sstevel@tonic-gate if (locked) 3347c478bd9Sstevel@tonic-gate as_pageunlock(&kas, ppp, vaddr, 3357c478bd9Sstevel@tonic-gate PAGESIZE, S_WRITE); 3367c478bd9Sstevel@tonic-gate error = EFAULT; 3377c478bd9Sstevel@tonic-gate break; 3387c478bd9Sstevel@tonic-gate } 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate error = mmio(uio, rw, v, uio->uio_loffset & PAGEOFFSET, 3417c478bd9Sstevel@tonic-gate minor == M_ALLKMEM || mm_kmem_io_access); 3427c478bd9Sstevel@tonic-gate if (locked) 3437c478bd9Sstevel@tonic-gate as_pageunlock(&kas, ppp, vaddr, PAGESIZE, 3447c478bd9Sstevel@tonic-gate S_WRITE); 3457c478bd9Sstevel@tonic-gate } 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate break; 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate case M_ZERO: 3507c478bd9Sstevel@tonic-gate if (rw == UIO_READ) { 3517c478bd9Sstevel@tonic-gate label_t ljb; 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate if (on_fault(&ljb)) { 3547c478bd9Sstevel@tonic-gate no_fault(); 3557c478bd9Sstevel@tonic-gate error = EFAULT; 3567c478bd9Sstevel@tonic-gate break; 3577c478bd9Sstevel@tonic-gate } 3587c478bd9Sstevel@tonic-gate uzero(iov->iov_base, iov->iov_len); 3597c478bd9Sstevel@tonic-gate no_fault(); 3607c478bd9Sstevel@tonic-gate uio->uio_resid -= iov->iov_len; 3617c478bd9Sstevel@tonic-gate uio->uio_loffset += iov->iov_len; 3627c478bd9Sstevel@tonic-gate break; 3637c478bd9Sstevel@tonic-gate } 3647c478bd9Sstevel@tonic-gate /* else it's a write, fall through to NULL case */ 3657c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/ 3667c478bd9Sstevel@tonic-gate 3677c478bd9Sstevel@tonic-gate case M_NULL: 3687c478bd9Sstevel@tonic-gate if (rw == UIO_READ) 3697c478bd9Sstevel@tonic-gate return (0); 3707c478bd9Sstevel@tonic-gate c = iov->iov_len; 3717c478bd9Sstevel@tonic-gate iov->iov_base += c; 3727c478bd9Sstevel@tonic-gate iov->iov_len -= c; 3737c478bd9Sstevel@tonic-gate uio->uio_loffset += c; 3747c478bd9Sstevel@tonic-gate uio->uio_resid -= c; 3757c478bd9Sstevel@tonic-gate break; 3767c478bd9Sstevel@tonic-gate 3777c478bd9Sstevel@tonic-gate } 3787c478bd9Sstevel@tonic-gate } 3797c478bd9Sstevel@tonic-gate return (uio->uio_resid == oresid ? error : 0); 3807c478bd9Sstevel@tonic-gate } 3817c478bd9Sstevel@tonic-gate 3827c478bd9Sstevel@tonic-gate static int 3837c478bd9Sstevel@tonic-gate mmread(dev_t dev, struct uio *uio, cred_t *cred) 3847c478bd9Sstevel@tonic-gate { 3857c478bd9Sstevel@tonic-gate return (mmrw(dev, uio, UIO_READ, cred)); 3867c478bd9Sstevel@tonic-gate } 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate static int 3897c478bd9Sstevel@tonic-gate mmwrite(dev_t dev, struct uio *uio, cred_t *cred) 3907c478bd9Sstevel@tonic-gate { 3917c478bd9Sstevel@tonic-gate return (mmrw(dev, uio, UIO_WRITE, cred)); 3927c478bd9Sstevel@tonic-gate } 3937c478bd9Sstevel@tonic-gate 3947c478bd9Sstevel@tonic-gate /* 3957c478bd9Sstevel@tonic-gate * Private ioctl for libkvm to support kvm_physaddr(). 3967c478bd9Sstevel@tonic-gate * Given an address space and a VA, compute the PA. 3977c478bd9Sstevel@tonic-gate */ 3987c478bd9Sstevel@tonic-gate static int 3997c478bd9Sstevel@tonic-gate mmioctl_vtop(intptr_t data) 4007c478bd9Sstevel@tonic-gate { 4017c478bd9Sstevel@tonic-gate mem_vtop_t mem_vtop; 4027c478bd9Sstevel@tonic-gate proc_t *p; 4037c478bd9Sstevel@tonic-gate pfn_t pfn = (pfn_t)PFN_INVALID; 4047c478bd9Sstevel@tonic-gate pid_t pid = 0; 4057c478bd9Sstevel@tonic-gate struct as *as; 4067c478bd9Sstevel@tonic-gate struct seg *seg; 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate if (copyin((void *)data, &mem_vtop, sizeof (mem_vtop_t))) 4097c478bd9Sstevel@tonic-gate return (EFAULT); 4107c478bd9Sstevel@tonic-gate if (mem_vtop.m_as == &kas) { 4117c478bd9Sstevel@tonic-gate pfn = hat_getpfnum(kas.a_hat, mem_vtop.m_va); 4127c478bd9Sstevel@tonic-gate } else if (mem_vtop.m_as == NULL) { 4137c478bd9Sstevel@tonic-gate return (EIO); 4147c478bd9Sstevel@tonic-gate } else { 4157c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 4167c478bd9Sstevel@tonic-gate for (p = practive; p != NULL; p = p->p_next) { 4177c478bd9Sstevel@tonic-gate if (p->p_as == mem_vtop.m_as) { 4187c478bd9Sstevel@tonic-gate pid = p->p_pid; 4197c478bd9Sstevel@tonic-gate break; 4207c478bd9Sstevel@tonic-gate } 4217c478bd9Sstevel@tonic-gate } 4227c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 4237c478bd9Sstevel@tonic-gate if (p == NULL) 4247c478bd9Sstevel@tonic-gate return (EIO); 4257c478bd9Sstevel@tonic-gate p = sprlock(pid); 4267c478bd9Sstevel@tonic-gate if (p == NULL) 4277c478bd9Sstevel@tonic-gate return (EIO); 4287c478bd9Sstevel@tonic-gate as = p->p_as; 4297c478bd9Sstevel@tonic-gate if (as == mem_vtop.m_as) { 4307c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 4317c478bd9Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 4327c478bd9Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; 4337c478bd9Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg)) 4347c478bd9Sstevel@tonic-gate if ((uintptr_t)mem_vtop.m_va - 4357c478bd9Sstevel@tonic-gate (uintptr_t)seg->s_base < seg->s_size) 4367c478bd9Sstevel@tonic-gate break; 4377c478bd9Sstevel@tonic-gate if (seg != NULL) 4387c478bd9Sstevel@tonic-gate pfn = hat_getpfnum(as->a_hat, mem_vtop.m_va); 4397c478bd9Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 4407c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 4417c478bd9Sstevel@tonic-gate } 4427c478bd9Sstevel@tonic-gate sprunlock(p); 4437c478bd9Sstevel@tonic-gate } 4447c478bd9Sstevel@tonic-gate mem_vtop.m_pfn = pfn; 4457c478bd9Sstevel@tonic-gate if (pfn == PFN_INVALID) 4467c478bd9Sstevel@tonic-gate return (EIO); 4477c478bd9Sstevel@tonic-gate if (copyout(&mem_vtop, (void *)data, sizeof (mem_vtop_t))) 4487c478bd9Sstevel@tonic-gate return (EFAULT); 4497c478bd9Sstevel@tonic-gate 4507c478bd9Sstevel@tonic-gate return (0); 4517c478bd9Sstevel@tonic-gate } 4527c478bd9Sstevel@tonic-gate 4537c478bd9Sstevel@tonic-gate /* 4547c478bd9Sstevel@tonic-gate * Given a PA, retire that page or check whether it has already been retired. 4557c478bd9Sstevel@tonic-gate */ 4567c478bd9Sstevel@tonic-gate static int 4577c478bd9Sstevel@tonic-gate mmioctl_page_retire(int cmd, intptr_t data) 4587c478bd9Sstevel@tonic-gate { 4597c478bd9Sstevel@tonic-gate uint64_t pa; 4607c478bd9Sstevel@tonic-gate pfn_t pfn; 4617c478bd9Sstevel@tonic-gate page_t *pp; 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate if (copyin((void *)data, &pa, sizeof (uint64_t))) 4647c478bd9Sstevel@tonic-gate return (EFAULT); 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate pfn = pa >> MMU_PAGESHIFT; 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate if (!pf_is_memory(pfn) || (pp = page_numtopp_nolock(pfn)) == NULL) 4697c478bd9Sstevel@tonic-gate return (EINVAL); 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate /* 4727c478bd9Sstevel@tonic-gate * If we're checking, see if the page is retired; if not, confirm that 4737c478bd9Sstevel@tonic-gate * its status is at least set to be failing. If neither, return EIO. 4747c478bd9Sstevel@tonic-gate */ 4757c478bd9Sstevel@tonic-gate if (cmd == MEM_PAGE_ISRETIRED) { 4767c478bd9Sstevel@tonic-gate if (page_isretired(pp)) 4777c478bd9Sstevel@tonic-gate return (0); 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate if (!page_isfailing(pp)) 4807c478bd9Sstevel@tonic-gate return (EIO); 4817c478bd9Sstevel@tonic-gate 4827c478bd9Sstevel@tonic-gate return (EAGAIN); 4837c478bd9Sstevel@tonic-gate } 4847c478bd9Sstevel@tonic-gate 4857c478bd9Sstevel@tonic-gate /* 4867c478bd9Sstevel@tonic-gate * Try to retire the page. If the retire fails, it will be scheduled to 4877c478bd9Sstevel@tonic-gate * occur when the page is freed. If this page is out of circulation 4887c478bd9Sstevel@tonic-gate * already, or is in the process of being retired, we fail. 4897c478bd9Sstevel@tonic-gate */ 4907c478bd9Sstevel@tonic-gate if (page_isretired(pp) || page_isfailing(pp)) 4917c478bd9Sstevel@tonic-gate return (EIO); 4927c478bd9Sstevel@tonic-gate 4937c478bd9Sstevel@tonic-gate page_settoxic(pp, PAGE_IS_FAULTY); 4947c478bd9Sstevel@tonic-gate return (page_retire(pp, PAGE_IS_FAILING) ? EAGAIN : 0); 4957c478bd9Sstevel@tonic-gate } 4967c478bd9Sstevel@tonic-gate 4977c478bd9Sstevel@tonic-gate #ifdef __sparc 4987c478bd9Sstevel@tonic-gate /* 4997c478bd9Sstevel@tonic-gate * Given a syndrome, syndrome type, and address return the 5007c478bd9Sstevel@tonic-gate * associated memory name in the provided data buffer. 5017c478bd9Sstevel@tonic-gate */ 5027c478bd9Sstevel@tonic-gate static int 5037c478bd9Sstevel@tonic-gate mmioctl_get_mem_name(intptr_t data) 5047c478bd9Sstevel@tonic-gate { 5057c478bd9Sstevel@tonic-gate mem_name_t mem_name; 5067c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32 5077c478bd9Sstevel@tonic-gate mem_name32_t mem_name32; 5087c478bd9Sstevel@tonic-gate #endif 5097c478bd9Sstevel@tonic-gate void *buf; 5107c478bd9Sstevel@tonic-gate size_t bufsize; 5117c478bd9Sstevel@tonic-gate int len, err; 5127c478bd9Sstevel@tonic-gate 5137c478bd9Sstevel@tonic-gate if ((bufsize = cpu_get_name_bufsize()) == 0) 5147c478bd9Sstevel@tonic-gate return (ENOTSUP); 5157c478bd9Sstevel@tonic-gate 5167c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 5177c478bd9Sstevel@tonic-gate if (copyin((void *)data, &mem_name, sizeof (mem_name_t))) 5187c478bd9Sstevel@tonic-gate return (EFAULT); 5197c478bd9Sstevel@tonic-gate } 5207c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32 5217c478bd9Sstevel@tonic-gate else { 5227c478bd9Sstevel@tonic-gate if (copyin((void *)data, &mem_name32, sizeof (mem_name32_t))) 5237c478bd9Sstevel@tonic-gate return (EFAULT); 5247c478bd9Sstevel@tonic-gate mem_name.m_addr = mem_name32.m_addr; 5257c478bd9Sstevel@tonic-gate mem_name.m_synd = mem_name32.m_synd; 5267c478bd9Sstevel@tonic-gate mem_name.m_type[0] = mem_name32.m_type[0]; 5277c478bd9Sstevel@tonic-gate mem_name.m_type[1] = mem_name32.m_type[1]; 528*e42d2a1cSjongkis mem_name.m_name = (caddr_t)(uintptr_t)mem_name32.m_name; 5297c478bd9Sstevel@tonic-gate mem_name.m_namelen = (size_t)mem_name32.m_namelen; 5307c478bd9Sstevel@tonic-gate } 5317c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32 */ 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate buf = kmem_alloc(bufsize, KM_SLEEP); 5347c478bd9Sstevel@tonic-gate 5357c478bd9Sstevel@tonic-gate /* 5367c478bd9Sstevel@tonic-gate * Call into cpu specific code to do the lookup. 5377c478bd9Sstevel@tonic-gate */ 5387c478bd9Sstevel@tonic-gate if ((err = cpu_get_mem_name(mem_name.m_synd, mem_name.m_type, 5397c478bd9Sstevel@tonic-gate mem_name.m_addr, buf, bufsize, &len)) != 0) { 5407c478bd9Sstevel@tonic-gate kmem_free(buf, bufsize); 5417c478bd9Sstevel@tonic-gate return (err); 5427c478bd9Sstevel@tonic-gate } 5437c478bd9Sstevel@tonic-gate 5447c478bd9Sstevel@tonic-gate if (len >= mem_name.m_namelen) { 5457c478bd9Sstevel@tonic-gate kmem_free(buf, bufsize); 5467c478bd9Sstevel@tonic-gate return (ENAMETOOLONG); 5477c478bd9Sstevel@tonic-gate } 5487c478bd9Sstevel@tonic-gate 5497c478bd9Sstevel@tonic-gate if (copyoutstr(buf, (char *)mem_name.m_name, 5507c478bd9Sstevel@tonic-gate mem_name.m_namelen, NULL) != 0) { 5517c478bd9Sstevel@tonic-gate kmem_free(buf, bufsize); 5527c478bd9Sstevel@tonic-gate return (EFAULT); 5537c478bd9Sstevel@tonic-gate } 5547c478bd9Sstevel@tonic-gate 5557c478bd9Sstevel@tonic-gate kmem_free(buf, bufsize); 5567c478bd9Sstevel@tonic-gate return (0); 5577c478bd9Sstevel@tonic-gate } 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate /* 5607c478bd9Sstevel@tonic-gate * Given a syndrome and address return information about the associated memory. 5617c478bd9Sstevel@tonic-gate */ 5627c478bd9Sstevel@tonic-gate static int 5637c478bd9Sstevel@tonic-gate mmioctl_get_mem_info(intptr_t data) 5647c478bd9Sstevel@tonic-gate { 5657c478bd9Sstevel@tonic-gate mem_info_t mem_info; 5667c478bd9Sstevel@tonic-gate int err; 5677c478bd9Sstevel@tonic-gate 5687c478bd9Sstevel@tonic-gate if (copyin((void *)data, &mem_info, sizeof (mem_info_t))) 5697c478bd9Sstevel@tonic-gate return (EFAULT); 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate if ((err = cpu_get_mem_info(mem_info.m_synd, mem_info.m_addr, 5727c478bd9Sstevel@tonic-gate &mem_info.m_mem_size, &mem_info.m_seg_size, &mem_info.m_bank_size, 5737c478bd9Sstevel@tonic-gate &mem_info.m_segments, &mem_info.m_banks, &mem_info.m_mcid)) != 0) 5747c478bd9Sstevel@tonic-gate return (err); 5757c478bd9Sstevel@tonic-gate 5767c478bd9Sstevel@tonic-gate if (copyout(&mem_info, (void *)data, sizeof (mem_info_t)) != 0) 5777c478bd9Sstevel@tonic-gate return (EFAULT); 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate return (0); 5807c478bd9Sstevel@tonic-gate } 5817c478bd9Sstevel@tonic-gate #endif /* __sparc */ 5827c478bd9Sstevel@tonic-gate 5837c478bd9Sstevel@tonic-gate /* 5847c478bd9Sstevel@tonic-gate * Private ioctls for 5857c478bd9Sstevel@tonic-gate * libkvm to support kvm_physaddr(). 5867c478bd9Sstevel@tonic-gate * FMA support for page_retire() and memory attribute information. 5877c478bd9Sstevel@tonic-gate */ 5887c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 5897c478bd9Sstevel@tonic-gate static int 5907c478bd9Sstevel@tonic-gate mmioctl(dev_t dev, int cmd, intptr_t data, int flag, cred_t *cred, int *rvalp) 5917c478bd9Sstevel@tonic-gate { 5927c478bd9Sstevel@tonic-gate switch (cmd) { 5937c478bd9Sstevel@tonic-gate case MEM_VTOP: 5947c478bd9Sstevel@tonic-gate if (getminor(dev) != M_KMEM) 5957c478bd9Sstevel@tonic-gate return (ENXIO); 5967c478bd9Sstevel@tonic-gate return (mmioctl_vtop(data)); 5977c478bd9Sstevel@tonic-gate 5987c478bd9Sstevel@tonic-gate case MEM_PAGE_RETIRE: 5997c478bd9Sstevel@tonic-gate case MEM_PAGE_ISRETIRED: 6007c478bd9Sstevel@tonic-gate if (getminor(dev) != M_MEM) 6017c478bd9Sstevel@tonic-gate return (ENXIO); 6027c478bd9Sstevel@tonic-gate return (mmioctl_page_retire(cmd, data)); 6037c478bd9Sstevel@tonic-gate 6047c478bd9Sstevel@tonic-gate case MEM_NAME: 6057c478bd9Sstevel@tonic-gate if (getminor(dev) != M_MEM) 6067c478bd9Sstevel@tonic-gate return (ENXIO); 6077c478bd9Sstevel@tonic-gate #ifdef __sparc 6087c478bd9Sstevel@tonic-gate return (mmioctl_get_mem_name(data)); 6097c478bd9Sstevel@tonic-gate #else 6107c478bd9Sstevel@tonic-gate return (ENOTSUP); 6117c478bd9Sstevel@tonic-gate #endif 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate case MEM_INFO: 6147c478bd9Sstevel@tonic-gate if (getminor(dev) != M_MEM) 6157c478bd9Sstevel@tonic-gate return (ENXIO); 6167c478bd9Sstevel@tonic-gate #ifdef __sparc 6177c478bd9Sstevel@tonic-gate return (mmioctl_get_mem_info(data)); 6187c478bd9Sstevel@tonic-gate #else 6197c478bd9Sstevel@tonic-gate return (ENOTSUP); 6207c478bd9Sstevel@tonic-gate #endif 6217c478bd9Sstevel@tonic-gate } 6227c478bd9Sstevel@tonic-gate return (ENXIO); 6237c478bd9Sstevel@tonic-gate } 6247c478bd9Sstevel@tonic-gate 6257c478bd9Sstevel@tonic-gate /*ARGSUSED2*/ 6267c478bd9Sstevel@tonic-gate static int 6277c478bd9Sstevel@tonic-gate mmmmap(dev_t dev, off_t off, int prot) 6287c478bd9Sstevel@tonic-gate { 6297c478bd9Sstevel@tonic-gate pfn_t pf; 6307c478bd9Sstevel@tonic-gate struct memlist *pmem; 6317c478bd9Sstevel@tonic-gate minor_t minor = getminor(dev); 6327c478bd9Sstevel@tonic-gate 6337c478bd9Sstevel@tonic-gate switch (minor) { 6347c478bd9Sstevel@tonic-gate case M_MEM: 6357c478bd9Sstevel@tonic-gate pf = btop(off); 6367c478bd9Sstevel@tonic-gate memlist_read_lock(); 6377c478bd9Sstevel@tonic-gate for (pmem = phys_install; pmem != NULL; pmem = pmem->next) { 6387c478bd9Sstevel@tonic-gate if (pf >= BTOP(pmem->address) && 6397c478bd9Sstevel@tonic-gate pf < BTOP(pmem->address + pmem->size)) { 6407c478bd9Sstevel@tonic-gate memlist_read_unlock(); 6417c478bd9Sstevel@tonic-gate return (impl_obmem_pfnum(pf)); 6427c478bd9Sstevel@tonic-gate } 6437c478bd9Sstevel@tonic-gate } 6447c478bd9Sstevel@tonic-gate memlist_read_unlock(); 6457c478bd9Sstevel@tonic-gate break; 6467c478bd9Sstevel@tonic-gate 6477c478bd9Sstevel@tonic-gate case M_KMEM: 6487c478bd9Sstevel@tonic-gate case M_ALLKMEM: 6497c478bd9Sstevel@tonic-gate /* no longer supported with KPR */ 6507c478bd9Sstevel@tonic-gate return (-1); 6517c478bd9Sstevel@tonic-gate 6527c478bd9Sstevel@tonic-gate case M_ZERO: 6537c478bd9Sstevel@tonic-gate /* 6547c478bd9Sstevel@tonic-gate * We shouldn't be mmap'ing to /dev/zero here as 6557c478bd9Sstevel@tonic-gate * mmsegmap() should have already converted 6567c478bd9Sstevel@tonic-gate * a mapping request for this device to a mapping 6577c478bd9Sstevel@tonic-gate * using seg_vn for anonymous memory. 6587c478bd9Sstevel@tonic-gate */ 6597c478bd9Sstevel@tonic-gate break; 6607c478bd9Sstevel@tonic-gate 6617c478bd9Sstevel@tonic-gate } 6627c478bd9Sstevel@tonic-gate return (-1); 6637c478bd9Sstevel@tonic-gate } 6647c478bd9Sstevel@tonic-gate 6657c478bd9Sstevel@tonic-gate /* 6667c478bd9Sstevel@tonic-gate * This function is called when a memory device is mmap'ed. 6677c478bd9Sstevel@tonic-gate * Set up the mapping to the correct device driver. 6687c478bd9Sstevel@tonic-gate */ 6697c478bd9Sstevel@tonic-gate static int 6707c478bd9Sstevel@tonic-gate mmsegmap(dev_t dev, off_t off, struct as *as, caddr_t *addrp, off_t len, 6717c478bd9Sstevel@tonic-gate uint_t prot, uint_t maxprot, uint_t flags, struct cred *cred) 6727c478bd9Sstevel@tonic-gate { 6737c478bd9Sstevel@tonic-gate struct segvn_crargs vn_a; 6747c478bd9Sstevel@tonic-gate struct segdev_crargs dev_a; 6757c478bd9Sstevel@tonic-gate int error; 6767c478bd9Sstevel@tonic-gate minor_t minor; 6777c478bd9Sstevel@tonic-gate off_t i; 6787c478bd9Sstevel@tonic-gate 6797c478bd9Sstevel@tonic-gate minor = getminor(dev); 6807c478bd9Sstevel@tonic-gate 6817c478bd9Sstevel@tonic-gate as_rangelock(as); 6827c478bd9Sstevel@tonic-gate if ((flags & MAP_FIXED) == 0) { 6837c478bd9Sstevel@tonic-gate /* 6847c478bd9Sstevel@tonic-gate * No need to worry about vac alignment on /dev/zero 6857c478bd9Sstevel@tonic-gate * since this is a "clone" object that doesn't yet exist. 6867c478bd9Sstevel@tonic-gate */ 6877c478bd9Sstevel@tonic-gate map_addr(addrp, len, (offset_t)off, 6887c478bd9Sstevel@tonic-gate (minor == M_MEM) || (minor == M_KMEM), flags); 6897c478bd9Sstevel@tonic-gate 6907c478bd9Sstevel@tonic-gate if (*addrp == NULL) { 6917c478bd9Sstevel@tonic-gate as_rangeunlock(as); 6927c478bd9Sstevel@tonic-gate return (ENOMEM); 6937c478bd9Sstevel@tonic-gate } 6947c478bd9Sstevel@tonic-gate } else { 6957c478bd9Sstevel@tonic-gate /* 6967c478bd9Sstevel@tonic-gate * User specified address - 6977c478bd9Sstevel@tonic-gate * Blow away any previous mappings. 6987c478bd9Sstevel@tonic-gate */ 6997c478bd9Sstevel@tonic-gate (void) as_unmap(as, *addrp, len); 7007c478bd9Sstevel@tonic-gate } 7017c478bd9Sstevel@tonic-gate 7027c478bd9Sstevel@tonic-gate switch (minor) { 7037c478bd9Sstevel@tonic-gate case M_MEM: 7047c478bd9Sstevel@tonic-gate /* /dev/mem cannot be mmap'ed with MAP_PRIVATE */ 7057c478bd9Sstevel@tonic-gate if ((flags & MAP_TYPE) != MAP_SHARED) { 7067c478bd9Sstevel@tonic-gate as_rangeunlock(as); 7077c478bd9Sstevel@tonic-gate return (EINVAL); 7087c478bd9Sstevel@tonic-gate } 7097c478bd9Sstevel@tonic-gate 7107c478bd9Sstevel@tonic-gate /* 7117c478bd9Sstevel@tonic-gate * Check to ensure that the entire range is 7127c478bd9Sstevel@tonic-gate * legal and we are not trying to map in 7137c478bd9Sstevel@tonic-gate * more than the device will let us. 7147c478bd9Sstevel@tonic-gate */ 7157c478bd9Sstevel@tonic-gate for (i = 0; i < len; i += PAGESIZE) { 7167c478bd9Sstevel@tonic-gate if (mmmmap(dev, off + i, maxprot) == -1) { 7177c478bd9Sstevel@tonic-gate as_rangeunlock(as); 7187c478bd9Sstevel@tonic-gate return (ENXIO); 7197c478bd9Sstevel@tonic-gate } 7207c478bd9Sstevel@tonic-gate } 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate /* 7237c478bd9Sstevel@tonic-gate * Use seg_dev segment driver for /dev/mem mapping. 7247c478bd9Sstevel@tonic-gate */ 7257c478bd9Sstevel@tonic-gate dev_a.mapfunc = mmmmap; 7267c478bd9Sstevel@tonic-gate dev_a.dev = dev; 7277c478bd9Sstevel@tonic-gate dev_a.offset = off; 7287c478bd9Sstevel@tonic-gate dev_a.type = (flags & MAP_TYPE); 7297c478bd9Sstevel@tonic-gate dev_a.prot = (uchar_t)prot; 7307c478bd9Sstevel@tonic-gate dev_a.maxprot = (uchar_t)maxprot; 7317c478bd9Sstevel@tonic-gate dev_a.hat_attr = 0; 7327c478bd9Sstevel@tonic-gate 7337c478bd9Sstevel@tonic-gate /* 7347c478bd9Sstevel@tonic-gate * Make /dev/mem mappings non-consistent since we can't 7357c478bd9Sstevel@tonic-gate * alias pages that don't have page structs behind them, 7367c478bd9Sstevel@tonic-gate * such as kernel stack pages. If someone mmap()s a kernel 7377c478bd9Sstevel@tonic-gate * stack page and if we give him a tte with cv, a line from 7387c478bd9Sstevel@tonic-gate * that page can get into both pages of the spitfire d$. 7397c478bd9Sstevel@tonic-gate * But snoop from another processor will only invalidate 7407c478bd9Sstevel@tonic-gate * the first page. This later caused kernel (xc_attention) 7417c478bd9Sstevel@tonic-gate * to go into an infinite loop at pil 13 and no interrupts 7427c478bd9Sstevel@tonic-gate * could come in. See 1203630. 7437c478bd9Sstevel@tonic-gate * 7447c478bd9Sstevel@tonic-gate */ 7457c478bd9Sstevel@tonic-gate dev_a.hat_flags = HAT_LOAD_NOCONSIST; 7467c478bd9Sstevel@tonic-gate dev_a.devmap_data = NULL; 7477c478bd9Sstevel@tonic-gate 7487c478bd9Sstevel@tonic-gate error = as_map(as, *addrp, len, segdev_create, &dev_a); 7497c478bd9Sstevel@tonic-gate break; 7507c478bd9Sstevel@tonic-gate 7517c478bd9Sstevel@tonic-gate case M_ZERO: 7527c478bd9Sstevel@tonic-gate /* 7537c478bd9Sstevel@tonic-gate * Use seg_vn segment driver for /dev/zero mapping. 7547c478bd9Sstevel@tonic-gate * Passing in a NULL amp gives us the "cloning" effect. 7557c478bd9Sstevel@tonic-gate */ 7567c478bd9Sstevel@tonic-gate vn_a.vp = NULL; 7577c478bd9Sstevel@tonic-gate vn_a.offset = 0; 7587c478bd9Sstevel@tonic-gate vn_a.type = (flags & MAP_TYPE); 7597c478bd9Sstevel@tonic-gate vn_a.prot = prot; 7607c478bd9Sstevel@tonic-gate vn_a.maxprot = maxprot; 7617c478bd9Sstevel@tonic-gate vn_a.flags = flags & ~MAP_TYPE; 7627c478bd9Sstevel@tonic-gate vn_a.cred = cred; 7637c478bd9Sstevel@tonic-gate vn_a.amp = NULL; 7647c478bd9Sstevel@tonic-gate vn_a.szc = 0; 7657c478bd9Sstevel@tonic-gate vn_a.lgrp_mem_policy_flags = 0; 7667c478bd9Sstevel@tonic-gate error = as_map(as, *addrp, len, segvn_create, &vn_a); 7677c478bd9Sstevel@tonic-gate break; 7687c478bd9Sstevel@tonic-gate 7697c478bd9Sstevel@tonic-gate case M_KMEM: 7707c478bd9Sstevel@tonic-gate case M_ALLKMEM: 7717c478bd9Sstevel@tonic-gate /* No longer supported with KPR. */ 7727c478bd9Sstevel@tonic-gate error = ENXIO; 7737c478bd9Sstevel@tonic-gate break; 7747c478bd9Sstevel@tonic-gate 7757c478bd9Sstevel@tonic-gate case M_NULL: 7767c478bd9Sstevel@tonic-gate /* 7777c478bd9Sstevel@tonic-gate * Use seg_dev segment driver for /dev/null mapping. 7787c478bd9Sstevel@tonic-gate */ 7797c478bd9Sstevel@tonic-gate dev_a.mapfunc = mmmmap; 7807c478bd9Sstevel@tonic-gate dev_a.dev = dev; 7817c478bd9Sstevel@tonic-gate dev_a.offset = off; 7827c478bd9Sstevel@tonic-gate dev_a.type = 0; /* neither PRIVATE nor SHARED */ 7837c478bd9Sstevel@tonic-gate dev_a.prot = dev_a.maxprot = (uchar_t)PROT_NONE; 7847c478bd9Sstevel@tonic-gate dev_a.hat_attr = 0; 7857c478bd9Sstevel@tonic-gate dev_a.hat_flags = 0; 7867c478bd9Sstevel@tonic-gate error = as_map(as, *addrp, len, segdev_create, &dev_a); 7877c478bd9Sstevel@tonic-gate break; 7887c478bd9Sstevel@tonic-gate 7897c478bd9Sstevel@tonic-gate default: 7907c478bd9Sstevel@tonic-gate error = ENXIO; 7917c478bd9Sstevel@tonic-gate } 7927c478bd9Sstevel@tonic-gate 7937c478bd9Sstevel@tonic-gate as_rangeunlock(as); 7947c478bd9Sstevel@tonic-gate return (error); 7957c478bd9Sstevel@tonic-gate } 7967c478bd9Sstevel@tonic-gate 7977c478bd9Sstevel@tonic-gate static struct cb_ops mm_cb_ops = { 7987c478bd9Sstevel@tonic-gate mmopen, /* open */ 7997c478bd9Sstevel@tonic-gate nulldev, /* close */ 8007c478bd9Sstevel@tonic-gate nodev, /* strategy */ 8017c478bd9Sstevel@tonic-gate nodev, /* print */ 8027c478bd9Sstevel@tonic-gate nodev, /* dump */ 8037c478bd9Sstevel@tonic-gate mmread, /* read */ 8047c478bd9Sstevel@tonic-gate mmwrite, /* write */ 8057c478bd9Sstevel@tonic-gate mmioctl, /* ioctl */ 8067c478bd9Sstevel@tonic-gate nodev, /* devmap */ 8077c478bd9Sstevel@tonic-gate mmmmap, /* mmap */ 8087c478bd9Sstevel@tonic-gate mmsegmap, /* segmap */ 8097c478bd9Sstevel@tonic-gate mmchpoll, /* poll */ 8107c478bd9Sstevel@tonic-gate mmpropop, /* prop_op */ 8117c478bd9Sstevel@tonic-gate 0, /* streamtab */ 8127c478bd9Sstevel@tonic-gate D_NEW | D_MP | D_64BIT | D_U64BIT 8137c478bd9Sstevel@tonic-gate }; 8147c478bd9Sstevel@tonic-gate 8157c478bd9Sstevel@tonic-gate static struct dev_ops mm_ops = { 8167c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev, */ 8177c478bd9Sstevel@tonic-gate 0, /* refcnt */ 8187c478bd9Sstevel@tonic-gate mm_info, /* get_dev_info */ 8197c478bd9Sstevel@tonic-gate nulldev, /* identify */ 8207c478bd9Sstevel@tonic-gate nulldev, /* probe */ 8217c478bd9Sstevel@tonic-gate mm_attach, /* attach */ 8227c478bd9Sstevel@tonic-gate nodev, /* detach */ 8237c478bd9Sstevel@tonic-gate nodev, /* reset */ 8247c478bd9Sstevel@tonic-gate &mm_cb_ops, /* driver operations */ 8257c478bd9Sstevel@tonic-gate (struct bus_ops *)0 /* bus operations */ 8267c478bd9Sstevel@tonic-gate }; 8277c478bd9Sstevel@tonic-gate 8287c478bd9Sstevel@tonic-gate static struct modldrv modldrv = { 8297c478bd9Sstevel@tonic-gate &mod_driverops, "memory driver %I%", &mm_ops, 8307c478bd9Sstevel@tonic-gate }; 8317c478bd9Sstevel@tonic-gate 8327c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 8337c478bd9Sstevel@tonic-gate MODREV_1, &modldrv, NULL 8347c478bd9Sstevel@tonic-gate }; 8357c478bd9Sstevel@tonic-gate 8367c478bd9Sstevel@tonic-gate int 8377c478bd9Sstevel@tonic-gate _init(void) 8387c478bd9Sstevel@tonic-gate { 8397c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage)); 8407c478bd9Sstevel@tonic-gate } 8417c478bd9Sstevel@tonic-gate 8427c478bd9Sstevel@tonic-gate int 8437c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 8447c478bd9Sstevel@tonic-gate { 8457c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 8467c478bd9Sstevel@tonic-gate } 8477c478bd9Sstevel@tonic-gate 8487c478bd9Sstevel@tonic-gate int 8497c478bd9Sstevel@tonic-gate _fini(void) 8507c478bd9Sstevel@tonic-gate { 8517c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage)); 8527c478bd9Sstevel@tonic-gate } 8537c478bd9Sstevel@tonic-gate 8547c478bd9Sstevel@tonic-gate static int 8557c478bd9Sstevel@tonic-gate mm_kstat_update(kstat_t *ksp, int rw) 8567c478bd9Sstevel@tonic-gate { 8577c478bd9Sstevel@tonic-gate struct memlist *pmem; 8587c478bd9Sstevel@tonic-gate uint_t count; 8597c478bd9Sstevel@tonic-gate 8607c478bd9Sstevel@tonic-gate if (rw == KSTAT_WRITE) 8617c478bd9Sstevel@tonic-gate return (EACCES); 8627c478bd9Sstevel@tonic-gate 8637c478bd9Sstevel@tonic-gate count = 0; 8647c478bd9Sstevel@tonic-gate memlist_read_lock(); 8657c478bd9Sstevel@tonic-gate for (pmem = phys_install; pmem != NULL; pmem = pmem->next) { 8667c478bd9Sstevel@tonic-gate count++; 8677c478bd9Sstevel@tonic-gate } 8687c478bd9Sstevel@tonic-gate memlist_read_unlock(); 8697c478bd9Sstevel@tonic-gate 8707c478bd9Sstevel@tonic-gate ksp->ks_ndata = count; 8717c478bd9Sstevel@tonic-gate ksp->ks_data_size = count * 2 * sizeof (uint64_t); 8727c478bd9Sstevel@tonic-gate 8737c478bd9Sstevel@tonic-gate return (0); 8747c478bd9Sstevel@tonic-gate } 8757c478bd9Sstevel@tonic-gate 8767c478bd9Sstevel@tonic-gate static int 8777c478bd9Sstevel@tonic-gate mm_kstat_snapshot(kstat_t *ksp, void *buf, int rw) 8787c478bd9Sstevel@tonic-gate { 8797c478bd9Sstevel@tonic-gate struct memlist *pmem; 8807c478bd9Sstevel@tonic-gate struct memunit { 8817c478bd9Sstevel@tonic-gate uint64_t address; 8827c478bd9Sstevel@tonic-gate uint64_t size; 8837c478bd9Sstevel@tonic-gate } *kspmem; 8847c478bd9Sstevel@tonic-gate 8857c478bd9Sstevel@tonic-gate if (rw == KSTAT_WRITE) 8867c478bd9Sstevel@tonic-gate return (EACCES); 8877c478bd9Sstevel@tonic-gate 8887c478bd9Sstevel@tonic-gate ksp->ks_snaptime = gethrtime(); 8897c478bd9Sstevel@tonic-gate 8907c478bd9Sstevel@tonic-gate kspmem = (struct memunit *)buf; 8917c478bd9Sstevel@tonic-gate memlist_read_lock(); 8927c478bd9Sstevel@tonic-gate for (pmem = phys_install; pmem != NULL; pmem = pmem->next, kspmem++) { 8937c478bd9Sstevel@tonic-gate if ((caddr_t)kspmem >= (caddr_t)buf + ksp->ks_data_size) 8947c478bd9Sstevel@tonic-gate break; 8957c478bd9Sstevel@tonic-gate kspmem->address = pmem->address; 8967c478bd9Sstevel@tonic-gate kspmem->size = pmem->size; 8977c478bd9Sstevel@tonic-gate } 8987c478bd9Sstevel@tonic-gate memlist_read_unlock(); 8997c478bd9Sstevel@tonic-gate 9007c478bd9Sstevel@tonic-gate return (0); 9017c478bd9Sstevel@tonic-gate } 902