xref: /freebsd/sys/amd64/amd64/mem.c (revision ae17d056a13db4832428001e2328e2bc26f2980d)
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 
70b4adaf56SDavid E. O'Brien /*
71b4adaf56SDavid E. O'Brien  * Used in /dev/mem drivers and elsewhere
72b4adaf56SDavid E. O'Brien  */
73b4adaf56SDavid E. O'Brien MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors");
744ffd949eSMike Smith struct mem_range_softc mem_range_softc;
754ffd949eSMike Smith 
76ae17d056SMark Murray void
77ae17d056SMark Murray mem_range_AP_init(void)
78ae17d056SMark Murray {
79ae17d056SMark Murray 	if (mem_range_softc.mr_op && mem_range_softc.mr_op->initAP)
80ae17d056SMark Murray 		mem_range_softc.mr_op->initAP(&mem_range_softc);
81ae17d056SMark Murray }
82ae17d056SMark Murray 
835afffbaaSMark Murray /* ARGSUSED */
848ab2f5ecSMark Murray int
858ab2f5ecSMark Murray memrw(struct cdev *dev, struct uio *uio, int flags)
865b81b6b3SRodney W. Grimes {
875afffbaaSMark Murray 	int o;
88afa88623SPeter Wemm 	u_long c = 0, v;
895afffbaaSMark Murray 	struct iovec *iov;
905b81b6b3SRodney W. Grimes 	int error = 0;
915afffbaaSMark Murray 	vm_offset_t addr, eaddr;
925b81b6b3SRodney W. Grimes 
930cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
940cddd8f0SMatthew Dillon 
955b81b6b3SRodney W. Grimes 	while (uio->uio_resid > 0 && error == 0) {
965b81b6b3SRodney W. Grimes 		iov = uio->uio_iov;
975b81b6b3SRodney W. Grimes 		if (iov->iov_len == 0) {
985b81b6b3SRodney W. Grimes 			uio->uio_iov++;
995b81b6b3SRodney W. Grimes 			uio->uio_iovcnt--;
1005b81b6b3SRodney W. Grimes 			if (uio->uio_iovcnt < 0)
1018ab2f5ecSMark Murray 				panic("memrw");
1025b81b6b3SRodney W. Grimes 			continue;
1035b81b6b3SRodney W. Grimes 		}
1048ab2f5ecSMark Murray 		if (minor(dev) == CDEV_MINOR_MEM) {
1055b81b6b3SRodney W. Grimes 			v = uio->uio_offset;
106bfcd2ec7SHidetoshi Shimokawa kmemphys:
107bfcd2ec7SHidetoshi Shimokawa 			o = v & PAGE_MASK;
108bfcd2ec7SHidetoshi Shimokawa 			c = min(uio->uio_resid, (u_int)(PAGE_SIZE - o));
109bfcd2ec7SHidetoshi Shimokawa 			error = uiomove((void *)PHYS_TO_DMAP(v), (int)c, uio);
1105b81b6b3SRodney W. Grimes 			continue;
1118ab2f5ecSMark Murray 		}
1128ab2f5ecSMark Murray 		else if (minor(dev) == CDEV_MINOR_KMEM) {
113bfcd2ec7SHidetoshi Shimokawa 			v = uio->uio_offset;
11497e11262SDavid Greenman 
115bfcd2ec7SHidetoshi Shimokawa 			if (v >= DMAP_MIN_ADDRESS && v < DMAP_MAX_ADDRESS) {
116bfcd2ec7SHidetoshi Shimokawa 				v = DMAP_TO_PHYS(v);
117bfcd2ec7SHidetoshi Shimokawa 				goto kmemphys;
118bfcd2ec7SHidetoshi Shimokawa 			}
119bfcd2ec7SHidetoshi Shimokawa 
120bfcd2ec7SHidetoshi Shimokawa 			c = iov->iov_len;
12121616ec3SPeter Wemm 
12297e11262SDavid Greenman 			/*
1238ab2f5ecSMark Murray 			 * Make sure that all of the pages are currently
1248ab2f5ecSMark Murray 			 * resident so that we don't create any zero-fill
1258ab2f5ecSMark Murray 			 * pages.
12697e11262SDavid Greenman 			 */
127bfcd2ec7SHidetoshi Shimokawa 			addr = trunc_page(v);
128bfcd2ec7SHidetoshi Shimokawa 			eaddr = round_page(v + c);
129d021fc11SPeter Wemm 
1303c9a3c9cSPeter Wemm 			if (addr < (vm_offset_t)KERNBASE)
131e40ab3e9SMark Murray 				return (EFAULT);
13297e11262SDavid Greenman 			for (; addr < eaddr; addr += PAGE_SIZE)
133e40ab3e9SMark Murray 				if (pmap_extract(kernel_pmap, addr) == 0)
134e40ab3e9SMark Murray 					return (EFAULT);
13597e11262SDavid Greenman 
136bfcd2ec7SHidetoshi Shimokawa 			if (!kernacc((caddr_t)(long)v, c,
13702c58685SPoul-Henning Kamp 			    uio->uio_rw == UIO_READ ?
138e40ab3e9SMark Murray 			    VM_PROT_READ : VM_PROT_WRITE))
1395b81b6b3SRodney W. Grimes 				return (EFAULT);
140bfcd2ec7SHidetoshi Shimokawa 
141bfcd2ec7SHidetoshi Shimokawa 			error = uiomove((caddr_t)(long)v, (int)c, uio);
1425b81b6b3SRodney W. Grimes 			continue;
14397e11262SDavid Greenman 		}
1448ab2f5ecSMark Murray 		/* else panic! */
1455b81b6b3SRodney W. Grimes 	}
1465b81b6b3SRodney W. Grimes 	return (error);
1475b81b6b3SRodney W. Grimes }
148b513c262SDavid Greenman 
1498ab2f5ecSMark Murray /*
1508ab2f5ecSMark Murray  * allow user processes to MMAP some memory sections
1518ab2f5ecSMark Murray  * instead of going through read/write
1528ab2f5ecSMark Murray  */
153ae17d056SMark Murray /* ARGSUSED */
1548ab2f5ecSMark Murray int
155ae17d056SMark Murray memmmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr,
156ae17d056SMark Murray     int prot __unused)
157b513c262SDavid Greenman {
1588ab2f5ecSMark Murray 	if (minor(dev) == CDEV_MINOR_MEM)
15907159f9cSMaxime Henrion 		*paddr = offset;
1608ab2f5ecSMark Murray 	else if (minor(dev) == CDEV_MINOR_KMEM)
16107159f9cSMaxime Henrion         	*paddr = vtophys(offset);
1628ab2f5ecSMark Murray 	/* else panic! */
16307159f9cSMaxime Henrion 	return (0);
1648567cb9fSMark Murray }
165b513c262SDavid Greenman 
166316bbd5cSBruce Evans /*
1674ffd949eSMike Smith  * Operations for changing memory attributes.
1684ffd949eSMike Smith  *
1694ffd949eSMike Smith  * This is basically just an ioctl shim for mem_range_attr_get
1704ffd949eSMike Smith  * and mem_range_attr_set.
1714ffd949eSMike Smith  */
172ae17d056SMark Murray /* ARGSUSED */
1738ab2f5ecSMark Murray int
174ae17d056SMark Murray memioctl(struct cdev *dev __unused, u_long cmd, caddr_t data, int flags,
1758ab2f5ecSMark Murray     struct thread *td)
1764ffd949eSMike Smith {
1774ffd949eSMike Smith 	int nd, error = 0;
1784ffd949eSMike Smith 	struct mem_range_op *mo = (struct mem_range_op *)data;
1794ffd949eSMike Smith 	struct mem_range_desc *md;
1804ffd949eSMike Smith 
1814ffd949eSMike Smith 	/* is this for us? */
1824ffd949eSMike Smith 	if ((cmd != MEMRANGE_GET) &&
1834ffd949eSMike Smith 	    (cmd != MEMRANGE_SET))
1844871de2fSBrian Feldman 		return (ENOTTY);
1854ffd949eSMike Smith 
1864ffd949eSMike Smith 	/* any chance we can handle this? */
1874ffd949eSMike Smith 	if (mem_range_softc.mr_op == NULL)
1884ffd949eSMike Smith 		return (EOPNOTSUPP);
1894ffd949eSMike Smith 
1904ffd949eSMike Smith 	/* do we have any descriptors? */
1914ffd949eSMike Smith 	if (mem_range_softc.mr_ndesc == 0)
1924ffd949eSMike Smith 		return (ENXIO);
1934ffd949eSMike Smith 
1944ffd949eSMike Smith 	switch (cmd) {
1954ffd949eSMike Smith 	case MEMRANGE_GET:
1964ffd949eSMike Smith 		nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc);
1974ffd949eSMike Smith 		if (nd > 0) {
1984ffd949eSMike Smith 			md = (struct mem_range_desc *)
1994ffd949eSMike Smith 				malloc(nd * sizeof(struct mem_range_desc),
200a163d034SWarner Losh 				       M_MEMDESC, M_WAITOK);
201ac3595b0SMike Smith 			error = mem_range_attr_get(md, &nd);
202ac3595b0SMike Smith 			if (!error)
2034ffd949eSMike Smith 				error = copyout(md, mo->mo_desc,
2044ffd949eSMike Smith 					nd * sizeof(struct mem_range_desc));
2054ffd949eSMike Smith 			free(md, M_MEMDESC);
2064ffd949eSMike Smith 		}
207e40ab3e9SMark Murray 		else
208e40ab3e9SMark Murray 			nd = mem_range_softc.mr_ndesc;
2094ffd949eSMike Smith 		mo->mo_arg[0] = nd;
2104ffd949eSMike Smith 		break;
2114ffd949eSMike Smith 
2124ffd949eSMike Smith 	case MEMRANGE_SET:
2134ffd949eSMike Smith 		md = (struct mem_range_desc *)malloc(sizeof(struct mem_range_desc),
214a163d034SWarner Losh 						    M_MEMDESC, M_WAITOK);
2154ffd949eSMike Smith 		error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc));
2164ffd949eSMike Smith 		/* clamp description string */
2174ffd949eSMike Smith 		md->mr_owner[sizeof(md->mr_owner) - 1] = 0;
2184ffd949eSMike Smith 		if (error == 0)
2194ffd949eSMike Smith 			error = mem_range_attr_set(md, &mo->mo_arg[0]);
2204ffd949eSMike Smith 		free(md, M_MEMDESC);
2214ffd949eSMike Smith 		break;
2224ffd949eSMike Smith 	}
2234ffd949eSMike Smith 	return (error);
2244ffd949eSMike Smith }
2254ffd949eSMike Smith 
2264ffd949eSMike Smith /*
2274ffd949eSMike Smith  * Implementation-neutral, kernel-callable functions for manipulating
2284ffd949eSMike Smith  * memory range attributes.
2294ffd949eSMike Smith  */
230ac3595b0SMike Smith int
2315afffbaaSMark Murray mem_range_attr_get(struct mem_range_desc *mrd, int *arg)
2324ffd949eSMike Smith {
2334af396a5SMike Smith 	/* can we handle this? */
2344af396a5SMike Smith 	if (mem_range_softc.mr_op == NULL)
2354af396a5SMike Smith 		return (EOPNOTSUPP);
2364af396a5SMike Smith 
237e40ab3e9SMark Murray 	if (*arg == 0)
2384ffd949eSMike Smith 		*arg = mem_range_softc.mr_ndesc;
239e40ab3e9SMark Murray 	else
2405afffbaaSMark Murray 		bcopy(mem_range_softc.mr_desc, mrd,
2415afffbaaSMark Murray 			(*arg) * sizeof(struct mem_range_desc));
242ac3595b0SMike Smith 	return (0);
2434ffd949eSMike Smith }
2444ffd949eSMike Smith 
2454ffd949eSMike Smith int
2465afffbaaSMark Murray mem_range_attr_set(struct mem_range_desc *mrd, int *arg)
2474ffd949eSMike Smith {
2484af396a5SMike Smith 	/* can we handle this? */
2494af396a5SMike Smith 	if (mem_range_softc.mr_op == NULL)
2504af396a5SMike Smith 		return (EOPNOTSUPP);
2514af396a5SMike Smith 
2524ffd949eSMike Smith 	return (mem_range_softc.mr_op->set(&mem_range_softc, mrd, arg));
2534ffd949eSMike Smith }
2544ffd949eSMike Smith 
2558ab2f5ecSMark Murray void
2568ab2f5ecSMark Murray dev_mem_md_init(void)
2574ffd949eSMike Smith {
2584ffd949eSMike Smith 	if (mem_range_softc.mr_op != NULL)
2594ffd949eSMike Smith 		mem_range_softc.mr_op->init(&mem_range_softc);
2605afffbaaSMark Murray }
261