1/*	$OpenBSD: mem.c,v 1.6 2017/12/14 03:30:43 guenther 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/filio.h>
46#include <sys/systm.h>
47#include <sys/uio.h>
48#include <sys/malloc.h>
49
50#include <machine/conf.h>
51
52#include <uvm/uvm_extern.h>
53
54caddr_t zpage;
55extern vaddr_t first_addr, last_addr;
56extern caddr_t kernelstart;
57extern void *etext;
58
59int
60mmopen(dev_t dev, int flag, int mode, struct proc *p)
61{
62	extern int allowkmem;
63
64	switch (minor(dev)) {
65	case 0:
66	case 1:
67		if (securelevel <= 0 || allowkmem)
68			break;
69		return (EPERM);
70	case 2:
71	case 12:
72		break;
73	default:
74		return (ENXIO);
75	}
76	return (0);
77}
78
79int
80mmclose(dev_t dev, int flag, int mode, struct proc *p)
81{
82
83	return (0);
84}
85
86int
87mmrw(dev_t dev, struct uio *uio, int flags)
88{
89	vaddr_t v;
90	size_t c;
91	struct iovec *iov;
92	int error = 0;
93
94	while (uio->uio_resid > 0 && error == 0) {
95		iov = uio->uio_iov;
96		if (iov->iov_len == 0) {
97			uio->uio_iov++;
98			uio->uio_iovcnt--;
99			if (uio->uio_iovcnt < 0)
100				panic("mmrw");
101			continue;
102		}
103		switch (minor(dev)) {
104
105		/* minor device 0 is physical memory */
106		case 0:
107			v = uio->uio_offset;
108			error = uiomove((caddr_t)v, uio->uio_resid, uio);
109			continue;
110
111		/* minor device 1 is kernel memory */
112		case 1:
113			v = uio->uio_offset;
114			c = ulmin(iov->iov_len, MAXPHYS);
115			if (v >= (vaddr_t)&kernelstart &&
116			    v < (vaddr_t)first_addr) {
117				if (v < (vaddr_t)etext &&
118				    uio->uio_rw == UIO_WRITE)
119					return (EFAULT);
120			} else if (!uvm_kernacc((caddr_t)v, c,
121			    uio->uio_rw == UIO_READ ? B_READ : B_WRITE))
122				return (EFAULT);
123			error = uiomove((caddr_t)v, c, uio);
124			continue;
125
126		/* minor device 2 is /dev/null */
127		case 2:
128			if (uio->uio_rw == UIO_WRITE)
129				uio->uio_resid = 0;
130			return (0);
131
132		/* minor device 12 is /dev/zero */
133		case 12:
134			if (uio->uio_rw == UIO_WRITE) {
135				c = iov->iov_len;
136				break;
137			}
138			if (zpage == NULL)
139				zpage = malloc(PAGE_SIZE, M_TEMP,
140				    M_WAITOK | M_ZERO);
141			c = ulmin(iov->iov_len, PAGE_SIZE);
142			error = uiomove(zpage, c, uio);
143			continue;
144
145		default:
146			return (ENXIO);
147		}
148		if (error)
149			break;
150		iov->iov_base += c;
151		iov->iov_len -= c;
152		uio->uio_offset += c;
153		uio->uio_resid -= c;
154	}
155
156	return (error);
157}
158
159paddr_t
160mmmmap(dev_t dev, off_t off, int prot)
161{
162	return (-1);
163}
164
165int
166mmioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
167{
168        switch (cmd) {
169        case FIONBIO:
170        case FIOASYNC:
171                /* handled by fd layer */
172                return 0;
173        }
174
175	return (EOPNOTSUPP);
176}
177