mem.c revision 1.4
1/*	$OpenBSD: mem.c,v 1.4 2016/08/01 15:58:22 tedu Exp $ */
2
3/*
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1982, 1986, 1990, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	@(#)mem.c	8.3 (Berkeley) 1/12/94
37 */
38
39/*
40 * Memory special file
41 */
42
43#include <sys/param.h>
44#include <sys/buf.h>
45#include <sys/systm.h>
46#include <sys/uio.h>
47#include <sys/malloc.h>
48
49#include <machine/conf.h>
50
51#include <uvm/uvm_extern.h>
52
53caddr_t zpage;
54extern vaddr_t first_addr, last_addr;
55extern caddr_t kernelstart;
56extern void *etext;
57
58int
59mmopen(dev_t dev, int flag, int mode, struct proc *p)
60{
61
62	switch (minor(dev)) {
63		case 0:
64		case 1:
65		case 2:
66		case 12:
67			return (0);
68		default:
69			return (ENXIO);
70	}
71}
72
73int
74mmclose(dev_t dev, int flag, int mode, struct proc *p)
75{
76
77	return (0);
78}
79
80int
81mmrw(dev_t dev, struct uio *uio, int flags)
82{
83	vaddr_t v;
84	size_t c;
85	struct iovec *iov;
86	int error = 0;
87
88	while (uio->uio_resid > 0 && error == 0) {
89		iov = uio->uio_iov;
90		if (iov->iov_len == 0) {
91			uio->uio_iov++;
92			uio->uio_iovcnt--;
93			if (uio->uio_iovcnt < 0)
94				panic("mmrw");
95			continue;
96		}
97		switch (minor(dev)) {
98
99		/* minor device 0 is physical memory */
100		case 0:
101			v = uio->uio_offset;
102			error = uiomove((caddr_t)v, uio->uio_resid, uio);
103			continue;
104
105		/* minor device 1 is kernel memory */
106		case 1:
107			v = uio->uio_offset;
108			c = ulmin(iov->iov_len, MAXPHYS);
109			if (v >= (vaddr_t)&kernelstart &&
110			    v < (vaddr_t)first_addr) {
111				if (v < (vaddr_t)etext &&
112				    uio->uio_rw == UIO_WRITE)
113					return (EFAULT);
114			} else if (!uvm_kernacc((caddr_t)v, c,
115			    uio->uio_rw == UIO_READ ? B_READ : B_WRITE))
116				return (EFAULT);
117			error = uiomove((caddr_t)v, c, uio);
118			continue;
119
120		/* minor device 2 is /dev/null */
121		case 2:
122			if (uio->uio_rw == UIO_WRITE)
123				uio->uio_resid = 0;
124			return (0);
125
126		/* minor device 12 is /dev/zero */
127		case 12:
128			if (uio->uio_rw == UIO_WRITE) {
129				c = iov->iov_len;
130				break;
131			}
132			if (zpage == NULL)
133				zpage = malloc(PAGE_SIZE, M_TEMP,
134				    M_WAITOK | M_ZERO);
135			c = ulmin(iov->iov_len, PAGE_SIZE);
136			error = uiomove(zpage, c, uio);
137			continue;
138
139		default:
140			return (ENXIO);
141		}
142		if (error)
143			break;
144		iov->iov_base += c;
145		iov->iov_len -= c;
146		uio->uio_offset += c;
147		uio->uio_resid -= c;
148	}
149
150	return (error);
151}
152
153paddr_t
154mmmmap(dev_t dev, off_t off, int prot)
155{
156	return (-1);
157}
158
159int
160mmioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
161{
162	return (EOPNOTSUPP);
163}
164