15b81b6b3SRodney W. Grimes /*- 25b81b6b3SRodney W. Grimes * Copyright (c) 1988 University of Utah. 35b81b6b3SRodney W. Grimes * Copyright (c) 1982, 1986, 1990 The Regents of the University of California. 45b81b6b3SRodney W. Grimes * All rights reserved. 55b81b6b3SRodney W. Grimes * 65b81b6b3SRodney W. Grimes * This code is derived from software contributed to Berkeley by 75b81b6b3SRodney W. Grimes * the Systems Programming Group of the University of Utah Computer 85b81b6b3SRodney W. Grimes * Science Department, and code derived from software contributed to 95b81b6b3SRodney W. Grimes * Berkeley by William Jolitz. 105b81b6b3SRodney W. Grimes * 115b81b6b3SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 125b81b6b3SRodney W. Grimes * modification, are permitted provided that the following conditions 135b81b6b3SRodney W. Grimes * are met: 145b81b6b3SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 155b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 165b81b6b3SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 175b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 185b81b6b3SRodney W. Grimes * documentation and/or other materials provided with the distribution. 19*fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 205b81b6b3SRodney W. Grimes * may be used to endorse or promote products derived from this software 215b81b6b3SRodney W. Grimes * without specific prior written permission. 225b81b6b3SRodney W. Grimes * 235b81b6b3SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 245b81b6b3SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 255b81b6b3SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 265b81b6b3SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 275b81b6b3SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 285b81b6b3SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 295b81b6b3SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 305b81b6b3SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 315b81b6b3SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 325b81b6b3SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 335b81b6b3SRodney W. Grimes * SUCH DAMAGE. 345b81b6b3SRodney W. Grimes * 3521616ec3SPeter Wemm * from: Utah $Hdr: mem.c 1.13 89/10/08$ 3621616ec3SPeter Wemm * from: @(#)mem.c 7.2 (Berkeley) 5/9/91 375b81b6b3SRodney W. Grimes */ 385b81b6b3SRodney W. Grimes 3956ae44c5SDavid E. O'Brien #include <sys/cdefs.h> 4056ae44c5SDavid E. O'Brien __FBSDID("$FreeBSD$"); 4156ae44c5SDavid E. O'Brien 425b81b6b3SRodney W. Grimes /* 435b81b6b3SRodney W. Grimes * Memory special file 445b81b6b3SRodney W. Grimes */ 455b81b6b3SRodney W. Grimes 4626f9a767SRodney W. Grimes #include <sys/param.h> 4726f9a767SRodney W. Grimes #include <sys/conf.h> 48265cdeddSBruce Evans #include <sys/fcntl.h> 494ffd949eSMike Smith #include <sys/ioccom.h> 50b4adaf56SDavid E. O'Brien #include <sys/kernel.h> 5123955314SAlfred Perlstein #include <sys/lock.h> 5226f9a767SRodney W. Grimes #include <sys/malloc.h> 534ffd949eSMike Smith #include <sys/memrange.h> 54fd360128SPoul-Henning Kamp #include <sys/module.h> 559dceb26bSJohn Baldwin #include <sys/mutex.h> 5626f9a767SRodney W. Grimes #include <sys/proc.h> 57bb25f0ddSBruce Evans #include <sys/signalvar.h> 58d4af7a50SDavid E. O'Brien #include <sys/systm.h> 59d4af7a50SDavid E. O'Brien #include <sys/uio.h> 605b81b6b3SRodney W. Grimes 6122bb3201SKonstantin Belousov #include <machine/md_var.h> 624ffd949eSMike Smith #include <machine/specialreg.h> 63b4adaf56SDavid E. O'Brien #include <machine/vmparam.h> 645b81b6b3SRodney W. Grimes 65c87801feSDavid Greenman #include <vm/vm.h> 6626f9a767SRodney W. Grimes #include <vm/pmap.h> 67efeaf95aSDavid Greenman #include <vm/vm_extern.h> 685b81b6b3SRodney W. Grimes 698ab2f5ecSMark Murray #include <machine/memdev.h> 704ffd949eSMike Smith 71f15a9cd2SJohn Baldwin /* 72f15a9cd2SJohn Baldwin * Used in /dev/mem drivers and elsewhere 73f15a9cd2SJohn Baldwin */ 74f15a9cd2SJohn Baldwin MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors"); 75f15a9cd2SJohn Baldwin 765afffbaaSMark Murray /* ARGSUSED */ 778ab2f5ecSMark Murray int 788ab2f5ecSMark Murray memrw(struct cdev *dev, struct uio *uio, int flags) 795b81b6b3SRodney W. Grimes { 805afffbaaSMark Murray struct iovec *iov; 8122bb3201SKonstantin Belousov void *p; 8222bb3201SKonstantin Belousov ssize_t orig_resid; 8322bb3201SKonstantin Belousov u_long v, vd; 8422bb3201SKonstantin Belousov u_int c; 8518cc2ff0SKonstantin Belousov int error; 865b81b6b3SRodney W. Grimes 8752f3c44eSKonstantin Belousov error = 0; 8822bb3201SKonstantin Belousov orig_resid = uio->uio_resid; 895b81b6b3SRodney W. Grimes while (uio->uio_resid > 0 && error == 0) { 905b81b6b3SRodney W. Grimes iov = uio->uio_iov; 915b81b6b3SRodney W. Grimes if (iov->iov_len == 0) { 925b81b6b3SRodney W. Grimes uio->uio_iov++; 935b81b6b3SRodney W. Grimes uio->uio_iovcnt--; 945b81b6b3SRodney W. Grimes if (uio->uio_iovcnt < 0) 958ab2f5ecSMark Murray panic("memrw"); 965b81b6b3SRodney W. Grimes continue; 975b81b6b3SRodney W. Grimes } 985b81b6b3SRodney W. Grimes v = uio->uio_offset; 9922bb3201SKonstantin Belousov c = ulmin(iov->iov_len, PAGE_SIZE - (u_int)(v & PAGE_MASK)); 10097e11262SDavid Greenman 10122bb3201SKonstantin Belousov switch (dev2unit(dev)) { 10222bb3201SKonstantin Belousov case CDEV_MINOR_KMEM: 10397e11262SDavid Greenman /* 10422bb3201SKonstantin Belousov * Since c is clamped to be less or equal than 10522bb3201SKonstantin Belousov * PAGE_SIZE, the uiomove() call does not 10622bb3201SKonstantin Belousov * access past the end of the direct map. 10797e11262SDavid Greenman */ 10822bb3201SKonstantin Belousov if (v >= DMAP_MIN_ADDRESS && 10922bb3201SKonstantin Belousov v < DMAP_MIN_ADDRESS + dmaplimit) { 11022bb3201SKonstantin Belousov error = uiomove((void *)v, c, uio); 11122bb3201SKonstantin Belousov break; 11222bb3201SKonstantin Belousov } 113d021fc11SPeter Wemm 11422bb3201SKonstantin Belousov if (!kernacc((void *)v, c, uio->uio_rw == UIO_READ ? 11552f3c44eSKonstantin Belousov VM_PROT_READ : VM_PROT_WRITE)) { 11652f3c44eSKonstantin Belousov error = EFAULT; 11722bb3201SKonstantin Belousov break; 11852f3c44eSKonstantin Belousov } 119bfcd2ec7SHidetoshi Shimokawa 12022bb3201SKonstantin Belousov /* 12122bb3201SKonstantin Belousov * If the extracted address is not accessible 12222bb3201SKonstantin Belousov * through the direct map, then we make a 12322bb3201SKonstantin Belousov * private (uncached) mapping because we can't 12422bb3201SKonstantin Belousov * depend on the existing kernel mapping 12522bb3201SKonstantin Belousov * remaining valid until the completion of 12622bb3201SKonstantin Belousov * uiomove(). 12722bb3201SKonstantin Belousov * 12822bb3201SKonstantin Belousov * XXX We cannot provide access to the 12922bb3201SKonstantin Belousov * physical page 0 mapped into KVA. 13022bb3201SKonstantin Belousov */ 13122bb3201SKonstantin Belousov v = pmap_extract(kernel_pmap, v); 13222bb3201SKonstantin Belousov if (v == 0) { 13322bb3201SKonstantin Belousov error = EFAULT; 13422bb3201SKonstantin Belousov break; 13597e11262SDavid Greenman } 13622bb3201SKonstantin Belousov /* FALLTHROUGH */ 13722bb3201SKonstantin Belousov case CDEV_MINOR_MEM: 13822bb3201SKonstantin Belousov if (v < dmaplimit) { 13922bb3201SKonstantin Belousov vd = PHYS_TO_DMAP(v); 14022bb3201SKonstantin Belousov error = uiomove((void *)vd, c, uio); 14122bb3201SKonstantin Belousov break; 1425b81b6b3SRodney W. Grimes } 14316dcd773SJohn Baldwin if (v > cpu_getmaxphyaddr()) { 14422bb3201SKonstantin Belousov error = EFAULT; 14522bb3201SKonstantin Belousov break; 14622bb3201SKonstantin Belousov } 14722bb3201SKonstantin Belousov p = pmap_mapdev(v, PAGE_SIZE); 14822bb3201SKonstantin Belousov error = uiomove(p, c, uio); 14922bb3201SKonstantin Belousov pmap_unmapdev((vm_offset_t)p, PAGE_SIZE); 15022bb3201SKonstantin Belousov break; 15122bb3201SKonstantin Belousov } 15222bb3201SKonstantin Belousov } 15322bb3201SKonstantin Belousov /* 15422bb3201SKonstantin Belousov * Don't return error if any byte was written. Read and write 15522bb3201SKonstantin Belousov * can return error only if no i/o was performed. 15622bb3201SKonstantin Belousov */ 15722bb3201SKonstantin Belousov if (uio->uio_resid != orig_resid) 15822bb3201SKonstantin Belousov error = 0; 1595b81b6b3SRodney W. Grimes return (error); 1605b81b6b3SRodney W. Grimes } 161b513c262SDavid Greenman 1628ab2f5ecSMark Murray /* 1638ab2f5ecSMark Murray * allow user processes to MMAP some memory sections 1648ab2f5ecSMark Murray * instead of going through read/write 1658ab2f5ecSMark Murray */ 166ae17d056SMark Murray /* ARGSUSED */ 1678ab2f5ecSMark Murray int 168cfd7baceSRobert Noland memmmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr, 169cfd7baceSRobert Noland int prot __unused, vm_memattr_t *memattr __unused) 170b513c262SDavid Greenman { 171ad1d96caSJohn Baldwin if (dev2unit(dev) == CDEV_MINOR_MEM) { 17216dcd773SJohn Baldwin if (offset > cpu_getmaxphyaddr()) 173ad1d96caSJohn Baldwin return (-1); 17407159f9cSMaxime Henrion *paddr = offset; 17507159f9cSMaxime Henrion return (0); 1768567cb9fSMark Murray } 17731dc1e96SJohn Baldwin return (-1); 17831dc1e96SJohn Baldwin } 179b513c262SDavid Greenman 180316bbd5cSBruce Evans /* 1814ffd949eSMike Smith * Operations for changing memory attributes. 1824ffd949eSMike Smith * 1834ffd949eSMike Smith * This is basically just an ioctl shim for mem_range_attr_get 1844ffd949eSMike Smith * and mem_range_attr_set. 1854ffd949eSMike Smith */ 186ae17d056SMark Murray /* ARGSUSED */ 1878ab2f5ecSMark Murray int 188ae17d056SMark Murray memioctl(struct cdev *dev __unused, u_long cmd, caddr_t data, int flags, 1898ab2f5ecSMark Murray struct thread *td) 1904ffd949eSMike Smith { 1914ffd949eSMike Smith int nd, error = 0; 1924ffd949eSMike Smith struct mem_range_op *mo = (struct mem_range_op *)data; 1934ffd949eSMike Smith struct mem_range_desc *md; 1944ffd949eSMike Smith 1954ffd949eSMike Smith /* is this for us? */ 1964ffd949eSMike Smith if ((cmd != MEMRANGE_GET) && 1974ffd949eSMike Smith (cmd != MEMRANGE_SET)) 1984871de2fSBrian Feldman return (ENOTTY); 1994ffd949eSMike Smith 2004ffd949eSMike Smith /* any chance we can handle this? */ 2014ffd949eSMike Smith if (mem_range_softc.mr_op == NULL) 2024ffd949eSMike Smith return (EOPNOTSUPP); 2034ffd949eSMike Smith 2044ffd949eSMike Smith /* do we have any descriptors? */ 2054ffd949eSMike Smith if (mem_range_softc.mr_ndesc == 0) 2064ffd949eSMike Smith return (ENXIO); 2074ffd949eSMike Smith 2084ffd949eSMike Smith switch (cmd) { 2094ffd949eSMike Smith case MEMRANGE_GET: 2104ffd949eSMike Smith nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc); 2114ffd949eSMike Smith if (nd > 0) { 2124ffd949eSMike Smith md = (struct mem_range_desc *) 2134ffd949eSMike Smith malloc(nd * sizeof(struct mem_range_desc), 214a163d034SWarner Losh M_MEMDESC, M_WAITOK); 215ac3595b0SMike Smith error = mem_range_attr_get(md, &nd); 216ac3595b0SMike Smith if (!error) 2174ffd949eSMike Smith error = copyout(md, mo->mo_desc, 2184ffd949eSMike Smith nd * sizeof(struct mem_range_desc)); 2194ffd949eSMike Smith free(md, M_MEMDESC); 2204ffd949eSMike Smith } 221e40ab3e9SMark Murray else 222e40ab3e9SMark Murray nd = mem_range_softc.mr_ndesc; 2234ffd949eSMike Smith mo->mo_arg[0] = nd; 2244ffd949eSMike Smith break; 2254ffd949eSMike Smith 2264ffd949eSMike Smith case MEMRANGE_SET: 2274ffd949eSMike Smith md = (struct mem_range_desc *)malloc(sizeof(struct mem_range_desc), 228a163d034SWarner Losh M_MEMDESC, M_WAITOK); 2294ffd949eSMike Smith error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc)); 2304ffd949eSMike Smith /* clamp description string */ 2314ffd949eSMike Smith md->mr_owner[sizeof(md->mr_owner) - 1] = 0; 2324ffd949eSMike Smith if (error == 0) 2334ffd949eSMike Smith error = mem_range_attr_set(md, &mo->mo_arg[0]); 2344ffd949eSMike Smith free(md, M_MEMDESC); 2354ffd949eSMike Smith break; 2364ffd949eSMike Smith } 2374ffd949eSMike Smith return (error); 2384ffd949eSMike Smith } 239