xref: /freebsd/sys/amd64/amd64/mem.c (revision b513c26294324c9c81aa94c85d2e8dc2d3ffd009)
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  * 3. All advertising materials mentioning features or use of this software
205b81b6b3SRodney W. Grimes  *    must display the following acknowledgement:
215b81b6b3SRodney W. Grimes  *	This product includes software developed by the University of
225b81b6b3SRodney W. Grimes  *	California, Berkeley and its contributors.
235b81b6b3SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
245b81b6b3SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
255b81b6b3SRodney W. Grimes  *    without specific prior written permission.
265b81b6b3SRodney W. Grimes  *
275b81b6b3SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
285b81b6b3SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
295b81b6b3SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
305b81b6b3SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
315b81b6b3SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
325b81b6b3SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
335b81b6b3SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
345b81b6b3SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
355b81b6b3SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
365b81b6b3SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
375b81b6b3SRodney W. Grimes  * SUCH DAMAGE.
385b81b6b3SRodney W. Grimes  *
395b81b6b3SRodney W. Grimes  *	from: Utah $Hdr: mem.c 1.13 89/10/08$
4047cacd38SRodney W. Grimes  *	from: @(#)mem.c	7.2 (Berkeley) 5/9/91
41b513c262SDavid Greenman  *	$Id: mem.c,v 1.3 1993/10/16 14:15:06 rgrimes Exp $
425b81b6b3SRodney W. Grimes  */
435b81b6b3SRodney W. Grimes 
445b81b6b3SRodney W. Grimes /*
455b81b6b3SRodney W. Grimes  * Memory special file
465b81b6b3SRodney W. Grimes  */
475b81b6b3SRodney W. Grimes 
485b81b6b3SRodney W. Grimes #include "param.h"
495b81b6b3SRodney W. Grimes #include "conf.h"
505b81b6b3SRodney W. Grimes #include "buf.h"
515b81b6b3SRodney W. Grimes #include "systm.h"
525b81b6b3SRodney W. Grimes #include "uio.h"
535b81b6b3SRodney W. Grimes #include "malloc.h"
5478d172caSRodney W. Grimes #include "proc.h"
555b81b6b3SRodney W. Grimes 
565b81b6b3SRodney W. Grimes #include "machine/cpu.h"
5778d172caSRodney W. Grimes #include "machine/psl.h"
585b81b6b3SRodney W. Grimes 
595b81b6b3SRodney W. Grimes #include "vm/vm_param.h"
605b81b6b3SRodney W. Grimes #include "vm/lock.h"
615b81b6b3SRodney W. Grimes #include "vm/vm_statistics.h"
625b81b6b3SRodney W. Grimes #include "vm/pmap.h"
635b81b6b3SRodney W. Grimes #include "vm/vm_prot.h"
645b81b6b3SRodney W. Grimes 
655b81b6b3SRodney W. Grimes extern        char *vmmap;            /* poor name! */
665b81b6b3SRodney W. Grimes /*ARGSUSED*/
6778d172caSRodney W. Grimes mmclose(dev, uio, flags)
6878d172caSRodney W. Grimes 	dev_t dev;
6978d172caSRodney W. Grimes 	struct uio *uio;
7078d172caSRodney W. Grimes 	int flags;
7178d172caSRodney W. Grimes {
7278d172caSRodney W. Grimes 	struct syscframe *fp;
7378d172caSRodney W. Grimes 
7478d172caSRodney W. Grimes 	switch (minor(dev)) {
7578d172caSRodney W. Grimes 	case 14:
7678d172caSRodney W. Grimes 		fp = (struct syscframe *)curproc->p_regs;
7778d172caSRodney W. Grimes 		fp->sf_eflags &= ~PSL_IOPL;
7878d172caSRodney W. Grimes 		break;
7978d172caSRodney W. Grimes 	default:
8078d172caSRodney W. Grimes 		break;
8178d172caSRodney W. Grimes 	}
8278d172caSRodney W. Grimes 	return(0);
8378d172caSRodney W. Grimes }
8478d172caSRodney W. Grimes /*ARGSUSED*/
8578d172caSRodney W. Grimes mmopen(dev, uio, flags)
8678d172caSRodney W. Grimes 	dev_t dev;
8778d172caSRodney W. Grimes 	struct uio *uio;
8878d172caSRodney W. Grimes 	int flags;
8978d172caSRodney W. Grimes {
9078d172caSRodney W. Grimes 	struct syscframe *fp;
9178d172caSRodney W. Grimes 
9278d172caSRodney W. Grimes 	switch (minor(dev)) {
9378d172caSRodney W. Grimes 	case 14:
9478d172caSRodney W. Grimes 		fp = (struct syscframe *)curproc->p_regs;
9578d172caSRodney W. Grimes 		fp->sf_eflags |= PSL_IOPL;
9678d172caSRodney W. Grimes 		break;
9778d172caSRodney W. Grimes 	default:
9878d172caSRodney W. Grimes 		break;
9978d172caSRodney W. Grimes 	}
10078d172caSRodney W. Grimes 	return(0);
10178d172caSRodney W. Grimes }
10278d172caSRodney W. Grimes /*ARGSUSED*/
1035b81b6b3SRodney W. Grimes mmrw(dev, uio, flags)
1045b81b6b3SRodney W. Grimes 	dev_t dev;
1055b81b6b3SRodney W. Grimes 	struct uio *uio;
1065b81b6b3SRodney W. Grimes 	int flags;
1075b81b6b3SRodney W. Grimes {
1085b81b6b3SRodney W. Grimes 	register int o;
1095b81b6b3SRodney W. Grimes 	register u_int c, v;
1105b81b6b3SRodney W. Grimes 	register struct iovec *iov;
1115b81b6b3SRodney W. Grimes 	int error = 0;
1125b81b6b3SRodney W. Grimes 	caddr_t zbuf = NULL;
1135b81b6b3SRodney W. Grimes 
1145b81b6b3SRodney W. Grimes 	while (uio->uio_resid > 0 && error == 0) {
1155b81b6b3SRodney W. Grimes 		iov = uio->uio_iov;
1165b81b6b3SRodney W. Grimes 		if (iov->iov_len == 0) {
1175b81b6b3SRodney W. Grimes 			uio->uio_iov++;
1185b81b6b3SRodney W. Grimes 			uio->uio_iovcnt--;
1195b81b6b3SRodney W. Grimes 			if (uio->uio_iovcnt < 0)
1205b81b6b3SRodney W. Grimes 				panic("mmrw");
1215b81b6b3SRodney W. Grimes 			continue;
1225b81b6b3SRodney W. Grimes 		}
1235b81b6b3SRodney W. Grimes 		switch (minor(dev)) {
1245b81b6b3SRodney W. Grimes 
1255b81b6b3SRodney W. Grimes /* minor device 0 is physical memory */
1265b81b6b3SRodney W. Grimes 		case 0:
1275b81b6b3SRodney W. Grimes 			v = uio->uio_offset;
1285b81b6b3SRodney W. Grimes 			pmap_enter(pmap_kernel(), vmmap, v,
1295b81b6b3SRodney W. Grimes 				uio->uio_rw == UIO_READ ? VM_PROT_READ : VM_PROT_WRITE,
1305b81b6b3SRodney W. Grimes 				TRUE);
1315b81b6b3SRodney W. Grimes 			o = (int)uio->uio_offset & PGOFSET;
1325b81b6b3SRodney W. Grimes 			c = (u_int)(NBPG - ((int)iov->iov_base & PGOFSET));
1335b81b6b3SRodney W. Grimes 			c = MIN(c, (u_int)(NBPG - o));
1345b81b6b3SRodney W. Grimes 			c = MIN(c, (u_int)iov->iov_len);
1355b81b6b3SRodney W. Grimes 			error = uiomove((caddr_t)&vmmap[o], (int)c, uio);
1365b81b6b3SRodney W. Grimes 			pmap_remove(pmap_kernel(), vmmap, &vmmap[NBPG]);
1375b81b6b3SRodney W. Grimes 			continue;
1385b81b6b3SRodney W. Grimes 
1395b81b6b3SRodney W. Grimes /* minor device 1 is kernel memory */
1405b81b6b3SRodney W. Grimes 		case 1:
1415b81b6b3SRodney W. Grimes 			c = iov->iov_len;
1425b81b6b3SRodney W. Grimes 			if (!kernacc((caddr_t)uio->uio_offset, c,
1435b81b6b3SRodney W. Grimes 			    uio->uio_rw == UIO_READ ? B_READ : B_WRITE))
1445b81b6b3SRodney W. Grimes 				return(EFAULT);
1455b81b6b3SRodney W. Grimes 			error = uiomove((caddr_t)uio->uio_offset, (int)c, uio);
1465b81b6b3SRodney W. Grimes 			continue;
1475b81b6b3SRodney W. Grimes 
1485b81b6b3SRodney W. Grimes /* minor device 2 is EOF/RATHOLE */
1495b81b6b3SRodney W. Grimes 		case 2:
1505b81b6b3SRodney W. Grimes 			if (uio->uio_rw == UIO_READ)
1515b81b6b3SRodney W. Grimes 				return (0);
1525b81b6b3SRodney W. Grimes 			c = iov->iov_len;
1535b81b6b3SRodney W. Grimes 			break;
1545b81b6b3SRodney W. Grimes 
1555b81b6b3SRodney W. Grimes /* minor device 12 (/dev/zero) is source of nulls on read, rathole on write */
1565b81b6b3SRodney W. Grimes 		case 12:
1575b81b6b3SRodney W. Grimes 			if (uio->uio_rw == UIO_WRITE) {
1585b81b6b3SRodney W. Grimes 				c = iov->iov_len;
1595b81b6b3SRodney W. Grimes 				break;
1605b81b6b3SRodney W. Grimes 			}
1615b81b6b3SRodney W. Grimes 			if (zbuf == NULL) {
1625b81b6b3SRodney W. Grimes 				zbuf = (caddr_t)
1635b81b6b3SRodney W. Grimes 				    malloc(CLBYTES, M_TEMP, M_WAITOK);
1645b81b6b3SRodney W. Grimes 				bzero(zbuf, CLBYTES);
1655b81b6b3SRodney W. Grimes 			}
1665b81b6b3SRodney W. Grimes 			c = MIN(iov->iov_len, CLBYTES);
1675b81b6b3SRodney W. Grimes 			error = uiomove(zbuf, (int)c, uio);
1685b81b6b3SRodney W. Grimes 			continue;
1695b81b6b3SRodney W. Grimes 
1705b81b6b3SRodney W. Grimes #ifdef notyet
1715b81b6b3SRodney W. Grimes /* 386 I/O address space (/dev/ioport[bwl]) is a read/write access to seperate
1725b81b6b3SRodney W. Grimes    i/o device address bus, different than memory bus. Semantics here are
1735b81b6b3SRodney W. Grimes    very different than ordinary read/write, as if iov_len is a multiple
1745b81b6b3SRodney W. Grimes    an implied string move from a single port will be done. Note that lseek
1755b81b6b3SRodney W. Grimes    must be used to set the port number reliably. */
1765b81b6b3SRodney W. Grimes 		case 14:
1775b81b6b3SRodney W. Grimes 			if (iov->iov_len == 1) {
1785b81b6b3SRodney W. Grimes 				u_char tmp;
1795b81b6b3SRodney W. Grimes 				tmp = inb(uio->uio_offset);
1805b81b6b3SRodney W. Grimes 				error = uiomove (&tmp, iov->iov_len, uio);
1815b81b6b3SRodney W. Grimes 			} else {
1825b81b6b3SRodney W. Grimes 				if (!useracc((caddr_t)iov->iov_base,
1835b81b6b3SRodney W. Grimes 					iov->iov_len, uio->uio_rw))
1845b81b6b3SRodney W. Grimes 					return (EFAULT);
1855b81b6b3SRodney W. Grimes 				insb(uio->uio_offset, iov->iov_base,
1865b81b6b3SRodney W. Grimes 					iov->iov_len);
1875b81b6b3SRodney W. Grimes 			}
1885b81b6b3SRodney W. Grimes 			break;
1895b81b6b3SRodney W. Grimes 		case 15:
1905b81b6b3SRodney W. Grimes 			if (iov->iov_len == sizeof (short)) {
1915b81b6b3SRodney W. Grimes 				u_short tmp;
1925b81b6b3SRodney W. Grimes 				tmp = inw(uio->uio_offset);
1935b81b6b3SRodney W. Grimes 				error = uiomove (&tmp, iov->iov_len, uio);
1945b81b6b3SRodney W. Grimes 			} else {
1955b81b6b3SRodney W. Grimes 				if (!useracc((caddr_t)iov->iov_base,
1965b81b6b3SRodney W. Grimes 					iov->iov_len, uio->uio_rw))
1975b81b6b3SRodney W. Grimes 					return (EFAULT);
1985b81b6b3SRodney W. Grimes 				insw(uio->uio_offset, iov->iov_base,
1995b81b6b3SRodney W. Grimes 					iov->iov_len/ sizeof (short));
2005b81b6b3SRodney W. Grimes 			}
2015b81b6b3SRodney W. Grimes 			break;
2025b81b6b3SRodney W. Grimes 		case 16:
2035b81b6b3SRodney W. Grimes 			if (iov->iov_len == sizeof (long)) {
2045b81b6b3SRodney W. Grimes 				u_long tmp;
2055b81b6b3SRodney W. Grimes 				tmp = inl(uio->uio_offset);
2065b81b6b3SRodney W. Grimes 				error = uiomove (&tmp, iov->iov_len, uio);
2075b81b6b3SRodney W. Grimes 			} else {
2085b81b6b3SRodney W. Grimes 				if (!useracc((caddr_t)iov->iov_base,
2095b81b6b3SRodney W. Grimes 					iov->iov_len, uio->uio_rw))
2105b81b6b3SRodney W. Grimes 					return (EFAULT);
2115b81b6b3SRodney W. Grimes 				insl(uio->uio_offset, iov->iov_base,
2125b81b6b3SRodney W. Grimes 					iov->iov_len/ sizeof (long));
2135b81b6b3SRodney W. Grimes 			}
2145b81b6b3SRodney W. Grimes 			break;
2155b81b6b3SRodney W. Grimes #endif
2165b81b6b3SRodney W. Grimes 
2175b81b6b3SRodney W. Grimes 		default:
2185b81b6b3SRodney W. Grimes 			return (ENXIO);
2195b81b6b3SRodney W. Grimes 		}
2205b81b6b3SRodney W. Grimes 		if (error)
2215b81b6b3SRodney W. Grimes 			break;
2225b81b6b3SRodney W. Grimes 		iov->iov_base += c;
2235b81b6b3SRodney W. Grimes 		iov->iov_len -= c;
2245b81b6b3SRodney W. Grimes 		uio->uio_offset += c;
2255b81b6b3SRodney W. Grimes 		uio->uio_resid -= c;
2265b81b6b3SRodney W. Grimes 	}
2275b81b6b3SRodney W. Grimes 	if (zbuf)
2285b81b6b3SRodney W. Grimes 		free(zbuf, M_TEMP);
2295b81b6b3SRodney W. Grimes 	return (error);
2305b81b6b3SRodney W. Grimes }
231b513c262SDavid Greenman 
232b513c262SDavid Greenman 
233b513c262SDavid Greenman 
234b513c262SDavid Greenman 
235b513c262SDavid Greenman /*******************************************************\
236b513c262SDavid Greenman * allow user processes to MMAP some memory sections	*
237b513c262SDavid Greenman * instead of going through read/write			*
238b513c262SDavid Greenman \*******************************************************/
239b513c262SDavid Greenman int memmmap(dev_t dev, int offset, int nprot)
240b513c262SDavid Greenman {
241b513c262SDavid Greenman 	switch (minor(dev))
242b513c262SDavid Greenman 	{
243b513c262SDavid Greenman 
244b513c262SDavid Greenman /* minor device 0 is physical memory */
245b513c262SDavid Greenman 	case 0:
246b513c262SDavid Greenman         	return i386_btop(offset);
247b513c262SDavid Greenman 
248b513c262SDavid Greenman /* minor device 1 is kernel memory */
249b513c262SDavid Greenman 	case 1:
250b513c262SDavid Greenman         	return i386_btop(vtophys(offset));
251b513c262SDavid Greenman 
252b513c262SDavid Greenman 	default:
253b513c262SDavid Greenman 		return -1;
254b513c262SDavid Greenman 	}
255b513c262SDavid Greenman }
256b513c262SDavid Greenman 
257