xref: /freebsd/sys/amd64/amd64/mem.c (revision 0ea3482342b4d7d6e71f3007ce4dafe445c639fd)
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.18 1995/11/04 13:52:30 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 
70 static void memdev_init __P((void *));
71 SYSINIT(memdev,SI_SUB_DEVFS, SI_ORDER_ANY, memdev_init, NULL)
72 
73 static void
74 memdev_init(dummy)
75 	void *dummy;
76 {
77   void * x;
78 /*            path	name		devsw   minor	type   uid gid perm*/
79    x=dev_add("/misc",	"mem",		mmopen, 0,	DV_CHR, 0,  2, 0640);
80    x=dev_add("/misc",	"kmem",		mmopen, 1,	DV_CHR, 0,  2, 0640);
81    x=dev_add("/misc",	"null",		mmopen, 2,	DV_CHR, 0,  0, 0666);
82    x=dev_add("/misc",	"random",	mmopen, 3,	DV_CHR, 0,  0, 0666);
83    x=dev_add("/misc",	"urandom",	mmopen, 4,	DV_CHR, 0,  0, 0666);
84    x=dev_add("/misc",	"zero",		mmopen, 12,	DV_CHR, 0,  0, 0666);
85    x=dev_add("/misc",	"io",		mmopen, 14,	DV_CHR, 0,  2, 0640);
86 }
87 #endif /* DEVFS */
88 
89 extern        char *ptvmmap;            /* poor name! */
90 
91 int
92 mmclose(dev, flags, fmt, p)
93 	dev_t dev;
94 	int flags;
95 	int fmt;
96 	struct proc *p;
97 {
98 	struct trapframe *fp;
99 
100 	switch (minor(dev)) {
101 	case 14:
102 		fp = (struct trapframe *)curproc->p_md.md_regs;
103 		fp->tf_eflags &= ~PSL_IOPL;
104 		break;
105 	default:
106 		break;
107 	}
108 	return(0);
109 }
110 
111 int
112 mmopen(dev, flags, fmt, p)
113 	dev_t dev;
114 	int flags;
115 	int fmt;
116 	struct proc *p;
117 {
118 	struct trapframe *fp;
119 
120 	switch (minor(dev)) {
121 	case 14:
122 		fp = (struct trapframe *)curproc->p_md.md_regs;
123 		fp->tf_eflags |= PSL_IOPL;
124 		break;
125 	default:
126 		break;
127 	}
128 	return(0);
129 }
130 
131 int
132 mmrw(dev, uio, flags)
133 	dev_t dev;
134 	struct uio *uio;
135 	int flags;
136 {
137 	register int o;
138 	register u_int c, v;
139 	u_int poolsize;
140 	register struct iovec *iov;
141 	int error = 0;
142 	caddr_t buf = NULL;
143 
144 	while (uio->uio_resid > 0 && error == 0) {
145 		iov = uio->uio_iov;
146 		if (iov->iov_len == 0) {
147 			uio->uio_iov++;
148 			uio->uio_iovcnt--;
149 			if (uio->uio_iovcnt < 0)
150 				panic("mmrw");
151 			continue;
152 		}
153 		switch (minor(dev)) {
154 
155 /* minor device 0 is physical memory */
156 		case 0:
157 			v = uio->uio_offset;
158 			pmap_enter(kernel_pmap, (vm_offset_t)ptvmmap, v,
159 				uio->uio_rw == UIO_READ ? VM_PROT_READ : VM_PROT_WRITE,
160 				TRUE);
161 			o = (int)uio->uio_offset & PGOFSET;
162 			c = (u_int)(NBPG - ((int)iov->iov_base & PGOFSET));
163 			c = min(c, (u_int)(NBPG - o));
164 			c = min(c, (u_int)iov->iov_len);
165 			error = uiomove((caddr_t)&ptvmmap[o], (int)c, uio);
166 			pmap_remove(kernel_pmap, (vm_offset_t)ptvmmap,
167 				    (vm_offset_t)&ptvmmap[NBPG]);
168 			continue;
169 
170 /* minor device 1 is kernel memory */
171 		case 1: {
172 			vm_offset_t addr, eaddr;
173 			c = iov->iov_len;
174 
175 			/*
176 			 * Make sure that all of the pages are currently resident so
177 			 * that we don't create any zero-fill pages.
178 			 */
179 			addr = trunc_page(uio->uio_offset);
180 			eaddr = round_page(uio->uio_offset + c);
181 			for (; addr < eaddr; addr += PAGE_SIZE)
182 				if (pmap_extract(kernel_pmap, addr) == 0)
183 					return EFAULT;
184 
185 			if (!kernacc((caddr_t)(int)uio->uio_offset, c,
186 			    uio->uio_rw == UIO_READ ? B_READ : B_WRITE))
187 				return(EFAULT);
188 			error = uiomove((caddr_t)(int)uio->uio_offset, (int)c, uio);
189 			continue;
190 		}
191 
192 /* minor device 2 is EOF/RATHOLE */
193 		case 2:
194 			if (uio->uio_rw == UIO_READ)
195 				return (0);
196 			c = iov->iov_len;
197 			break;
198 
199 /* minor device 3 (/dev/random) is source of filth on read, rathole on write */
200 		case 3:
201 			if (uio->uio_rw == UIO_WRITE) {
202 				c = iov->iov_len;
203 				break;
204 			}
205 			if (buf == NULL)
206 				buf = (caddr_t)
207 				    malloc(CLBYTES, M_TEMP, M_WAITOK);
208 			c = min(iov->iov_len, CLBYTES);
209 			poolsize = read_random(buf, c);
210 			if (poolsize == 0) {
211 				if (buf)
212 					free(buf, M_TEMP);
213 				return (0);
214 			}
215 			c = min(c, poolsize);
216 			error = uiomove(buf, (int)c, uio);
217 			continue;
218 
219 /* minor device 4 (/dev/urandom) is source of muck on read, rathole on write */
220 		case 4:
221 			if (uio->uio_rw == UIO_WRITE) {
222 				c = iov->iov_len;
223 				break;
224 			}
225 			if (buf == NULL)
226 				buf = (caddr_t)
227 				    malloc(CLBYTES, M_TEMP, M_WAITOK);
228 			c = min(iov->iov_len, CLBYTES);
229 			poolsize = read_random_unlimited(buf, c);
230 			c = min(c, poolsize);
231 			error = uiomove(buf, (int)c, uio);
232 			continue;
233 
234 /* minor device 12 (/dev/zero) is source of nulls on read, rathole on write */
235 		case 12:
236 			if (uio->uio_rw == UIO_WRITE) {
237 				c = iov->iov_len;
238 				break;
239 			}
240 			if (buf == NULL) {
241 				buf = (caddr_t)
242 				    malloc(CLBYTES, M_TEMP, M_WAITOK);
243 				bzero(buf, CLBYTES);
244 			}
245 			c = min(iov->iov_len, CLBYTES);
246 			error = uiomove(buf, (int)c, uio);
247 			continue;
248 
249 #ifdef notyet
250 /* 386 I/O address space (/dev/ioport[bwl]) is a read/write access to seperate
251    i/o device address bus, different than memory bus. Semantics here are
252    very different than ordinary read/write, as if iov_len is a multiple
253    an implied string move from a single port will be done. Note that lseek
254    must be used to set the port number reliably. */
255 		case 14:
256 			if (iov->iov_len == 1) {
257 				u_char tmp;
258 				tmp = inb(uio->uio_offset);
259 				error = uiomove (&tmp, iov->iov_len, uio);
260 			} else {
261 				if (!useracc((caddr_t)iov->iov_base,
262 					iov->iov_len, uio->uio_rw))
263 					return (EFAULT);
264 				insb(uio->uio_offset, iov->iov_base,
265 					iov->iov_len);
266 			}
267 			break;
268 		case 15:
269 			if (iov->iov_len == sizeof (short)) {
270 				u_short tmp;
271 				tmp = inw(uio->uio_offset);
272 				error = uiomove (&tmp, iov->iov_len, uio);
273 			} else {
274 				if (!useracc((caddr_t)iov->iov_base,
275 					iov->iov_len, uio->uio_rw))
276 					return (EFAULT);
277 				insw(uio->uio_offset, iov->iov_base,
278 					iov->iov_len/ sizeof (short));
279 			}
280 			break;
281 		case 16:
282 			if (iov->iov_len == sizeof (long)) {
283 				u_long tmp;
284 				tmp = inl(uio->uio_offset);
285 				error = uiomove (&tmp, iov->iov_len, uio);
286 			} else {
287 				if (!useracc((caddr_t)iov->iov_base,
288 					iov->iov_len, uio->uio_rw))
289 					return (EFAULT);
290 				insl(uio->uio_offset, iov->iov_base,
291 					iov->iov_len/ sizeof (long));
292 			}
293 			break;
294 #endif
295 
296 		default:
297 			return (ENXIO);
298 		}
299 		if (error)
300 			break;
301 		iov->iov_base += c;
302 		iov->iov_len -= c;
303 		uio->uio_offset += c;
304 		uio->uio_resid -= c;
305 	}
306 	if (buf)
307 		free(buf, M_TEMP);
308 	return (error);
309 }
310 
311 
312 
313 
314 /*******************************************************\
315 * allow user processes to MMAP some memory sections	*
316 * instead of going through read/write			*
317 \*******************************************************/
318 int memmmap(dev_t dev, int offset, int nprot)
319 {
320 	switch (minor(dev))
321 	{
322 
323 /* minor device 0 is physical memory */
324 	case 0:
325         	return i386_btop(offset);
326 
327 /* minor device 1 is kernel memory */
328 	case 1:
329         	return i386_btop(vtophys(offset));
330 
331 	default:
332 		return -1;
333 	}
334 }
335 
336 /*
337  * Allow userland to select which interrupts will be used in the muck
338  * gathering business.
339  */
340 int
341 mmioctl(dev, cmd, cmdarg, flags, p)
342 	dev_t dev;
343 	int cmd;
344 	caddr_t cmdarg;
345 	int flags;
346 	struct proc *p;
347 {
348 	int error;
349 
350 	if (minor(dev) != 3 && minor(dev) != 4)
351 		return (ENODEV);
352 
353 	if (*(u_int16_t *)cmdarg >= 16)
354 		return (EINVAL);
355 
356 	/* Only root can do this */
357 	error = suser(p->p_ucred, &p->p_acflag);
358 	if (error != 0) {
359 		return (error);
360 	}
361 
362 	switch (cmd){
363 
364 		case MEM_SETIRQ:
365 			interrupt_allowed |= 1 << *(u_int16_t *)cmdarg;
366 			break;
367 
368 		case MEM_CLEARIRQ:
369 			interrupt_allowed &= ~(1 << *(u_int16_t *)cmdarg);
370 			break;
371 
372 		case MEM_RETURNIRQ:
373 			*(u_int16_t *)cmdarg = interrupt_allowed;
374 			break;
375 
376 		default:
377 			return (ENOTTY);
378 	}
379 	return (0);
380 }
381