xref: /freebsd/sys/amd64/amd64/mem.c (revision d82e286489da73321a47e329d98a98817b0438b6)
1 /*-
2  * Copyright (c) 1988 University of Utah.
3  * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * the Systems Programming Group of the University of Utah Computer
8  * Science Department, and code derived from software contributed to
9  * Berkeley by William Jolitz.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *	from: Utah $Hdr: mem.c 1.13 89/10/08$
40  *	from: @(#)mem.c	7.2 (Berkeley) 5/9/91
41  *	$Id: mem.c,v 1.16 1995/10/29 11:37:56 bde Exp $
42  */
43 
44 /*
45  * Memory special file
46  */
47 
48 #include <sys/param.h>
49 #include <sys/conf.h>
50 #include <sys/buf.h>
51 #include <sys/systm.h>
52 #include <sys/uio.h>
53 #include <sys/malloc.h>
54 #include <sys/proc.h>
55 
56 #include <machine/cpu.h>
57 #include <machine/random.h>
58 #include <machine/psl.h>
59 
60 #include <vm/vm.h>
61 #include <vm/vm_param.h>
62 #include <vm/lock.h>
63 #include <vm/vm_prot.h>
64 #include <vm/pmap.h>
65 
66 #ifdef	DEVFS
67 #include <sys/devfsext.h>
68 #include "sys/kernel.h"
69 int mmopen();
70 
71 void memdev_init(void *data) /* data not used */
72 {
73   void * x;
74 /*            path	name		devsw   minor	type   uid gid perm*/
75    x=dev_add("/misc",	"mem",		mmopen, 0,	DV_CHR, 0,  2, 0640);
76    x=dev_add("/misc",	"kmem",		mmopen, 1,	DV_CHR, 0,  2, 0640);
77    x=dev_add("/misc",	"null",		mmopen, 2,	DV_CHR, 0,  0, 0666);
78 #ifdef DEVRANDOM
79    x=dev_add("/misc",	"random",	mmopen, 3,	DV_CHR, 0,  0, 0666);
80    x=dev_add("/misc",	"urandom",	mmopen, 4,	DV_CHR, 0,  0, 0666);
81 #endif
82    x=dev_add("/misc",	"zero",		mmopen, 12,	DV_CHR, 0,  0, 0666);
83    x=dev_add("/misc",	"io",		mmopen, 14,	DV_CHR, 0,  2, 0640);
84 }
85 SYSINIT(memdev,SI_SUB_DEVFS, SI_ORDER_ANY, memdev_init, NULL)
86 #endif /*DEVFS*/
87 
88 
89 
90 extern        char *ptvmmap;            /* poor name! */
91 /*ARGSUSED*/
92 int
93 mmclose(dev, flags, fmt, p)
94 	dev_t dev;
95 	int flags;
96 	int fmt;
97 	struct proc *p;
98 {
99 	struct trapframe *fp;
100 
101 	switch (minor(dev)) {
102 	case 14:
103 		fp = (struct trapframe *)curproc->p_md.md_regs;
104 		fp->tf_eflags &= ~PSL_IOPL;
105 		break;
106 	default:
107 		break;
108 	}
109 	return(0);
110 }
111 /*ARGSUSED*/
112 int
113 mmopen(dev, flags, fmt, p)
114 	dev_t dev;
115 	int flags;
116 	int fmt;
117 	struct proc *p;
118 {
119 	struct trapframe *fp;
120 
121 	switch (minor(dev)) {
122 	case 14:
123 		fp = (struct trapframe *)curproc->p_md.md_regs;
124 		fp->tf_eflags |= PSL_IOPL;
125 		break;
126 	default:
127 		break;
128 	}
129 	return(0);
130 }
131 /*ARGSUSED*/
132 int
133 mmrw(dev, uio, flags)
134 	dev_t dev;
135 	struct uio *uio;
136 	int flags;
137 {
138 	register int o;
139 	register u_int c, v;
140 #ifdef DEVRANDOM
141 	u_int poolsize;
142 #endif
143 	register struct iovec *iov;
144 	int error = 0;
145 	caddr_t buf = NULL;
146 
147 	while (uio->uio_resid > 0 && error == 0) {
148 		iov = uio->uio_iov;
149 		if (iov->iov_len == 0) {
150 			uio->uio_iov++;
151 			uio->uio_iovcnt--;
152 			if (uio->uio_iovcnt < 0)
153 				panic("mmrw");
154 			continue;
155 		}
156 		switch (minor(dev)) {
157 
158 /* minor device 0 is physical memory */
159 		case 0:
160 			v = uio->uio_offset;
161 			pmap_enter(kernel_pmap, (vm_offset_t)ptvmmap, v,
162 				uio->uio_rw == UIO_READ ? VM_PROT_READ : VM_PROT_WRITE,
163 				TRUE);
164 			o = (int)uio->uio_offset & PGOFSET;
165 			c = (u_int)(NBPG - ((int)iov->iov_base & PGOFSET));
166 			c = min(c, (u_int)(NBPG - o));
167 			c = min(c, (u_int)iov->iov_len);
168 			error = uiomove((caddr_t)&ptvmmap[o], (int)c, uio);
169 			pmap_remove(kernel_pmap, (vm_offset_t)ptvmmap,
170 				    (vm_offset_t)&ptvmmap[NBPG]);
171 			continue;
172 
173 /* minor device 1 is kernel memory */
174 		case 1: {
175 			vm_offset_t addr, eaddr;
176 			c = iov->iov_len;
177 
178 			/*
179 			 * Make sure that all of the pages are currently resident so
180 			 * that we don't create any zero-fill pages.
181 			 */
182 			addr = trunc_page(uio->uio_offset);
183 			eaddr = round_page(uio->uio_offset + c);
184 			for (; addr < eaddr; addr += PAGE_SIZE)
185 				if (pmap_extract(kernel_pmap, addr) == 0)
186 					return EFAULT;
187 
188 			if (!kernacc((caddr_t)(int)uio->uio_offset, c,
189 			    uio->uio_rw == UIO_READ ? B_READ : B_WRITE))
190 				return(EFAULT);
191 			error = uiomove((caddr_t)(int)uio->uio_offset, (int)c, uio);
192 			continue;
193 		}
194 
195 /* minor device 2 is EOF/RATHOLE */
196 		case 2:
197 			if (uio->uio_rw == UIO_READ)
198 				return (0);
199 			c = iov->iov_len;
200 			break;
201 
202 #ifdef DEVRANDOM
203 /* minor device 3 (/dev/random) is source of filth on read, rathole on write */
204 		case 3:
205 			if (uio->uio_rw == UIO_WRITE) {
206 				c = iov->iov_len;
207 				break;
208 			}
209 			if (buf == NULL)
210 				buf = (caddr_t)
211 				    malloc(CLBYTES, M_TEMP, M_WAITOK);
212 			c = min(iov->iov_len, CLBYTES);
213 			poolsize = read_random(buf, c);
214 			if (poolsize == 0) {
215 				if (buf)
216 					free(buf, M_TEMP);
217 				return (0);
218 			}
219 			c = min(c, poolsize);
220 			error = uiomove(buf, (int)c, uio);
221 			continue;
222 
223 /* minor device 4 (/dev/urandom) is source of muck on read, rathole on write */
224 		case 4:
225 			if (uio->uio_rw == UIO_WRITE) {
226 				c = iov->iov_len;
227 				break;
228 			}
229 			if (buf == NULL)
230 				buf = (caddr_t)
231 				    malloc(CLBYTES, M_TEMP, M_WAITOK);
232 			c = min(iov->iov_len, CLBYTES);
233 			poolsize = read_random_unlimited(buf, c);
234 			c = min(c, poolsize);
235 			error = uiomove(buf, (int)c, uio);
236 			continue;
237 #endif
238 
239 /* minor device 12 (/dev/zero) is source of nulls on read, rathole on write */
240 		case 12:
241 			if (uio->uio_rw == UIO_WRITE) {
242 				c = iov->iov_len;
243 				break;
244 			}
245 			if (buf == NULL) {
246 				buf = (caddr_t)
247 				    malloc(CLBYTES, M_TEMP, M_WAITOK);
248 				bzero(buf, CLBYTES);
249 			}
250 			c = min(iov->iov_len, CLBYTES);
251 			error = uiomove(buf, (int)c, uio);
252 			continue;
253 
254 #ifdef notyet
255 /* 386 I/O address space (/dev/ioport[bwl]) is a read/write access to seperate
256    i/o device address bus, different than memory bus. Semantics here are
257    very different than ordinary read/write, as if iov_len is a multiple
258    an implied string move from a single port will be done. Note that lseek
259    must be used to set the port number reliably. */
260 		case 14:
261 			if (iov->iov_len == 1) {
262 				u_char tmp;
263 				tmp = inb(uio->uio_offset);
264 				error = uiomove (&tmp, iov->iov_len, uio);
265 			} else {
266 				if (!useracc((caddr_t)iov->iov_base,
267 					iov->iov_len, uio->uio_rw))
268 					return (EFAULT);
269 				insb(uio->uio_offset, iov->iov_base,
270 					iov->iov_len);
271 			}
272 			break;
273 		case 15:
274 			if (iov->iov_len == sizeof (short)) {
275 				u_short tmp;
276 				tmp = inw(uio->uio_offset);
277 				error = uiomove (&tmp, iov->iov_len, uio);
278 			} else {
279 				if (!useracc((caddr_t)iov->iov_base,
280 					iov->iov_len, uio->uio_rw))
281 					return (EFAULT);
282 				insw(uio->uio_offset, iov->iov_base,
283 					iov->iov_len/ sizeof (short));
284 			}
285 			break;
286 		case 16:
287 			if (iov->iov_len == sizeof (long)) {
288 				u_long tmp;
289 				tmp = inl(uio->uio_offset);
290 				error = uiomove (&tmp, iov->iov_len, uio);
291 			} else {
292 				if (!useracc((caddr_t)iov->iov_base,
293 					iov->iov_len, uio->uio_rw))
294 					return (EFAULT);
295 				insl(uio->uio_offset, iov->iov_base,
296 					iov->iov_len/ sizeof (long));
297 			}
298 			break;
299 #endif
300 
301 		default:
302 			return (ENXIO);
303 		}
304 		if (error)
305 			break;
306 		iov->iov_base += c;
307 		iov->iov_len -= c;
308 		uio->uio_offset += c;
309 		uio->uio_resid -= c;
310 	}
311 	if (buf)
312 		free(buf, M_TEMP);
313 	return (error);
314 }
315 
316 
317 
318 
319 /*******************************************************\
320 * allow user processes to MMAP some memory sections	*
321 * instead of going through read/write			*
322 \*******************************************************/
323 int memmmap(dev_t dev, int offset, int nprot)
324 {
325 	switch (minor(dev))
326 	{
327 
328 /* minor device 0 is physical memory */
329 	case 0:
330         	return i386_btop(offset);
331 
332 /* minor device 1 is kernel memory */
333 	case 1:
334         	return i386_btop(vtophys(offset));
335 
336 	default:
337 		return -1;
338 	}
339 }
340 
341 /*
342  * Allow userland to select which interrupts will be used in the muck
343  * gathering business.
344  */
345 int
346 mmioctl(dev, cmd, cmdarg, flags, p)
347 	dev_t dev;
348 	int cmd;
349 	caddr_t cmdarg;
350 	int flags;
351 	struct proc *p;
352 {
353 #ifdef DEVRANDOM
354 	int error;
355 
356 	if (minor(dev) != 3 && minor(dev) != 4)
357 		return (ENODEV);
358 
359 	if (*(u_int16_t *)cmdarg >= 16)
360 		return (EINVAL);
361 
362 	/* Only root can do this */
363 	error = suser(p->p_ucred, &p->p_acflag);
364 	if (error != 0) {
365 		return (error);
366 	}
367 
368 	switch (cmd){
369 
370 		case MEM_SETIRQ:
371 			interrupt_allowed |= 1 << *(u_int16_t *)cmdarg;
372 			break;
373 
374 		case MEM_CLEARIRQ:
375 			interrupt_allowed &= ~(1 << *(u_int16_t *)cmdarg);
376 			break;
377 
378 		case MEM_RETURNIRQ:
379 			*(u_int16_t *)cmdarg = interrupt_allowed;
380 			break;
381 
382 		default:
383 			return (ENOTTY);
384 	}
385 	return (0);
386 #else
387 	return (ENODEV);
388 #endif /* DEVRANDOM */
389 }
390