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. 195b81b6b3SRodney W. Grimes * 4. 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 614ffd949eSMike Smith #include <machine/specialreg.h> 62b4adaf56SDavid E. O'Brien #include <machine/vmparam.h> 635b81b6b3SRodney W. Grimes 64c87801feSDavid Greenman #include <vm/vm.h> 6526f9a767SRodney W. Grimes #include <vm/pmap.h> 66efeaf95aSDavid Greenman #include <vm/vm_extern.h> 675b81b6b3SRodney W. Grimes 688ab2f5ecSMark Murray #include <machine/memdev.h> 694ffd949eSMike Smith 70f15a9cd2SJohn Baldwin /* 71f15a9cd2SJohn Baldwin * Used in /dev/mem drivers and elsewhere 72f15a9cd2SJohn Baldwin */ 73f15a9cd2SJohn Baldwin MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors"); 74f15a9cd2SJohn Baldwin 755afffbaaSMark Murray /* ARGSUSED */ 768ab2f5ecSMark Murray int 778ab2f5ecSMark Murray memrw(struct cdev *dev, struct uio *uio, int flags) 785b81b6b3SRodney W. Grimes { 795afffbaaSMark Murray int o; 80afa88623SPeter Wemm u_long c = 0, v; 815afffbaaSMark Murray struct iovec *iov; 825b81b6b3SRodney W. Grimes int error = 0; 835afffbaaSMark Murray vm_offset_t addr, eaddr; 845b81b6b3SRodney W. Grimes 850cddd8f0SMatthew Dillon GIANT_REQUIRED; 860cddd8f0SMatthew Dillon 875b81b6b3SRodney W. Grimes while (uio->uio_resid > 0 && error == 0) { 885b81b6b3SRodney W. Grimes iov = uio->uio_iov; 895b81b6b3SRodney W. Grimes if (iov->iov_len == 0) { 905b81b6b3SRodney W. Grimes uio->uio_iov++; 915b81b6b3SRodney W. Grimes uio->uio_iovcnt--; 925b81b6b3SRodney W. Grimes if (uio->uio_iovcnt < 0) 938ab2f5ecSMark Murray panic("memrw"); 945b81b6b3SRodney W. Grimes continue; 955b81b6b3SRodney W. Grimes } 968ab2f5ecSMark Murray if (minor(dev) == CDEV_MINOR_MEM) { 975b81b6b3SRodney W. Grimes v = uio->uio_offset; 98bfcd2ec7SHidetoshi Shimokawa kmemphys: 99bfcd2ec7SHidetoshi Shimokawa o = v & PAGE_MASK; 100bfcd2ec7SHidetoshi Shimokawa c = min(uio->uio_resid, (u_int)(PAGE_SIZE - o)); 101bfcd2ec7SHidetoshi Shimokawa error = uiomove((void *)PHYS_TO_DMAP(v), (int)c, uio); 1025b81b6b3SRodney W. Grimes continue; 1038ab2f5ecSMark Murray } 1048ab2f5ecSMark Murray else if (minor(dev) == CDEV_MINOR_KMEM) { 105bfcd2ec7SHidetoshi Shimokawa v = uio->uio_offset; 10697e11262SDavid Greenman 107bfcd2ec7SHidetoshi Shimokawa if (v >= DMAP_MIN_ADDRESS && v < DMAP_MAX_ADDRESS) { 108bfcd2ec7SHidetoshi Shimokawa v = DMAP_TO_PHYS(v); 109bfcd2ec7SHidetoshi Shimokawa goto kmemphys; 110bfcd2ec7SHidetoshi Shimokawa } 111bfcd2ec7SHidetoshi Shimokawa 112bfcd2ec7SHidetoshi Shimokawa c = iov->iov_len; 11321616ec3SPeter Wemm 11497e11262SDavid Greenman /* 1158ab2f5ecSMark Murray * Make sure that all of the pages are currently 1168ab2f5ecSMark Murray * resident so that we don't create any zero-fill 1178ab2f5ecSMark Murray * pages. 11897e11262SDavid Greenman */ 119bfcd2ec7SHidetoshi Shimokawa addr = trunc_page(v); 120bfcd2ec7SHidetoshi Shimokawa eaddr = round_page(v + c); 121d021fc11SPeter Wemm 1223c9a3c9cSPeter Wemm if (addr < (vm_offset_t)KERNBASE) 123e40ab3e9SMark Murray return (EFAULT); 12497e11262SDavid Greenman for (; addr < eaddr; addr += PAGE_SIZE) 125e40ab3e9SMark Murray if (pmap_extract(kernel_pmap, addr) == 0) 126e40ab3e9SMark Murray return (EFAULT); 12797e11262SDavid Greenman 128bfcd2ec7SHidetoshi Shimokawa if (!kernacc((caddr_t)(long)v, c, 12902c58685SPoul-Henning Kamp uio->uio_rw == UIO_READ ? 130e40ab3e9SMark Murray VM_PROT_READ : VM_PROT_WRITE)) 1315b81b6b3SRodney W. Grimes return (EFAULT); 132bfcd2ec7SHidetoshi Shimokawa 133bfcd2ec7SHidetoshi Shimokawa error = uiomove((caddr_t)(long)v, (int)c, uio); 1345b81b6b3SRodney W. Grimes continue; 13597e11262SDavid Greenman } 1368ab2f5ecSMark Murray /* else panic! */ 1375b81b6b3SRodney W. Grimes } 1385b81b6b3SRodney W. Grimes return (error); 1395b81b6b3SRodney W. Grimes } 140b513c262SDavid Greenman 1418ab2f5ecSMark Murray /* 1428ab2f5ecSMark Murray * allow user processes to MMAP some memory sections 1438ab2f5ecSMark Murray * instead of going through read/write 1448ab2f5ecSMark Murray */ 145ae17d056SMark Murray /* ARGSUSED */ 1468ab2f5ecSMark Murray int 147ae17d056SMark Murray memmmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, 148ae17d056SMark Murray int prot __unused) 149b513c262SDavid Greenman { 1508ab2f5ecSMark Murray if (minor(dev) == CDEV_MINOR_MEM) 15107159f9cSMaxime Henrion *paddr = offset; 1528ab2f5ecSMark Murray else if (minor(dev) == CDEV_MINOR_KMEM) 15307159f9cSMaxime Henrion *paddr = vtophys(offset); 1548ab2f5ecSMark Murray /* else panic! */ 15507159f9cSMaxime Henrion return (0); 1568567cb9fSMark Murray } 157b513c262SDavid Greenman 158316bbd5cSBruce Evans /* 1594ffd949eSMike Smith * Operations for changing memory attributes. 1604ffd949eSMike Smith * 1614ffd949eSMike Smith * This is basically just an ioctl shim for mem_range_attr_get 1624ffd949eSMike Smith * and mem_range_attr_set. 1634ffd949eSMike Smith */ 164ae17d056SMark Murray /* ARGSUSED */ 1658ab2f5ecSMark Murray int 166ae17d056SMark Murray memioctl(struct cdev *dev __unused, u_long cmd, caddr_t data, int flags, 1678ab2f5ecSMark Murray struct thread *td) 1684ffd949eSMike Smith { 1694ffd949eSMike Smith int nd, error = 0; 1704ffd949eSMike Smith struct mem_range_op *mo = (struct mem_range_op *)data; 1714ffd949eSMike Smith struct mem_range_desc *md; 1724ffd949eSMike Smith 1734ffd949eSMike Smith /* is this for us? */ 1744ffd949eSMike Smith if ((cmd != MEMRANGE_GET) && 1754ffd949eSMike Smith (cmd != MEMRANGE_SET)) 1764871de2fSBrian Feldman return (ENOTTY); 1774ffd949eSMike Smith 1784ffd949eSMike Smith /* any chance we can handle this? */ 1794ffd949eSMike Smith if (mem_range_softc.mr_op == NULL) 1804ffd949eSMike Smith return (EOPNOTSUPP); 1814ffd949eSMike Smith 1824ffd949eSMike Smith /* do we have any descriptors? */ 1834ffd949eSMike Smith if (mem_range_softc.mr_ndesc == 0) 1844ffd949eSMike Smith return (ENXIO); 1854ffd949eSMike Smith 1864ffd949eSMike Smith switch (cmd) { 1874ffd949eSMike Smith case MEMRANGE_GET: 1884ffd949eSMike Smith nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc); 1894ffd949eSMike Smith if (nd > 0) { 1904ffd949eSMike Smith md = (struct mem_range_desc *) 1914ffd949eSMike Smith malloc(nd * sizeof(struct mem_range_desc), 192a163d034SWarner Losh M_MEMDESC, M_WAITOK); 193ac3595b0SMike Smith error = mem_range_attr_get(md, &nd); 194ac3595b0SMike Smith if (!error) 1954ffd949eSMike Smith error = copyout(md, mo->mo_desc, 1964ffd949eSMike Smith nd * sizeof(struct mem_range_desc)); 1974ffd949eSMike Smith free(md, M_MEMDESC); 1984ffd949eSMike Smith } 199e40ab3e9SMark Murray else 200e40ab3e9SMark Murray nd = mem_range_softc.mr_ndesc; 2014ffd949eSMike Smith mo->mo_arg[0] = nd; 2024ffd949eSMike Smith break; 2034ffd949eSMike Smith 2044ffd949eSMike Smith case MEMRANGE_SET: 2054ffd949eSMike Smith md = (struct mem_range_desc *)malloc(sizeof(struct mem_range_desc), 206a163d034SWarner Losh M_MEMDESC, M_WAITOK); 2074ffd949eSMike Smith error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc)); 2084ffd949eSMike Smith /* clamp description string */ 2094ffd949eSMike Smith md->mr_owner[sizeof(md->mr_owner) - 1] = 0; 2104ffd949eSMike Smith if (error == 0) 2114ffd949eSMike Smith error = mem_range_attr_set(md, &mo->mo_arg[0]); 2124ffd949eSMike Smith free(md, M_MEMDESC); 2134ffd949eSMike Smith break; 2144ffd949eSMike Smith } 2154ffd949eSMike Smith return (error); 2164ffd949eSMike Smith } 2174ffd949eSMike Smith 2188ab2f5ecSMark Murray void 2198ab2f5ecSMark Murray dev_mem_md_init(void) 2204ffd949eSMike Smith { 2214ffd949eSMike Smith if (mem_range_softc.mr_op != NULL) 2224ffd949eSMike Smith mem_range_softc.mr_op->init(&mem_range_softc); 2235afffbaaSMark Murray } 224