xref: /freebsd/sys/amd64/amd64/mem.c (revision 5129159789cc9d7bc514e4546b88e3427695002d)
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  * $FreeBSD$
42  */
43 
44 /*
45  * Memory special file
46  */
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/conf.h>
51 #include <sys/buf.h>
52 #include <sys/kernel.h>
53 #include <sys/uio.h>
54 #include <sys/ioccom.h>
55 #include <sys/malloc.h>
56 #include <sys/memrange.h>
57 #include <sys/proc.h>
58 #include <sys/signalvar.h>
59 
60 #include <machine/frame.h>
61 #include <machine/random.h>
62 #include <machine/psl.h>
63 #include <machine/specialreg.h>
64 #include <i386/isa/intr_machdep.h>
65 
66 #include <vm/vm.h>
67 #include <vm/pmap.h>
68 #include <vm/vm_extern.h>
69 
70 
71 static	d_open_t	mmopen;
72 static	d_close_t	mmclose;
73 static	d_read_t	mmrw;
74 static	d_ioctl_t	mmioctl;
75 static	d_mmap_t	memmmap;
76 static	d_poll_t	mmpoll;
77 
78 #define CDEV_MAJOR 2
79 static struct cdevsw mem_cdevsw = {
80 	/* open */	mmopen,
81 	/* close */	mmclose,
82 	/* read */	mmrw,
83 	/* write */	mmrw,
84 	/* ioctl */	mmioctl,
85 	/* poll */	mmpoll,
86 	/* mmap */	memmmap,
87 	/* strategy */	nostrategy,
88 	/* name */	"mem",
89 	/* maj */	CDEV_MAJOR,
90 	/* dump */	nodump,
91 	/* psize */	nopsize,
92 	/* flags */	D_MEM,
93 	/* bmaj */	-1
94 };
95 
96 static struct random_softc random_softc[16];
97 static caddr_t	zbuf;
98 
99 MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors");
100 static int mem_ioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
101 static int random_ioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
102 
103 struct mem_range_softc mem_range_softc;
104 
105 
106 static int
107 mmclose(dev, flags, fmt, p)
108 	dev_t dev;
109 	int flags;
110 	int fmt;
111 	struct proc *p;
112 {
113 	switch (minor(dev)) {
114 	case 14:
115 		curproc->p_md.md_regs->tf_eflags &= ~PSL_IOPL;
116 		break;
117 	default:
118 		break;
119 	}
120 	return (0);
121 }
122 
123 static int
124 mmopen(dev, flags, fmt, p)
125 	dev_t dev;
126 	int flags;
127 	int fmt;
128 	struct proc *p;
129 {
130 	int error;
131 
132 	switch (minor(dev)) {
133 	case 0:
134 	case 1:
135 		if (securelevel >= 1)
136 			return (EPERM);
137 		break;
138 	case 14:
139 		error = suser(p);
140 		if (error != 0)
141 			return (error);
142 		if (securelevel > 0)
143 			return (EPERM);
144 		curproc->p_md.md_regs->tf_eflags |= PSL_IOPL;
145 		break;
146 	default:
147 		break;
148 	}
149 	return (0);
150 }
151 
152 static int
153 mmrw(dev, uio, flags)
154 	dev_t dev;
155 	struct uio *uio;
156 	int flags;
157 {
158 	register int o;
159 	register u_int c, v;
160 	u_int poolsize;
161 	register struct iovec *iov;
162 	int error = 0;
163 	caddr_t buf = NULL;
164 
165 	while (uio->uio_resid > 0 && error == 0) {
166 		iov = uio->uio_iov;
167 		if (iov->iov_len == 0) {
168 			uio->uio_iov++;
169 			uio->uio_iovcnt--;
170 			if (uio->uio_iovcnt < 0)
171 				panic("mmrw");
172 			continue;
173 		}
174 		switch (minor(dev)) {
175 
176 /* minor device 0 is physical memory */
177 		case 0:
178 			v = uio->uio_offset;
179 			pmap_enter(kernel_pmap, (vm_offset_t)ptvmmap, v,
180 				uio->uio_rw == UIO_READ ? VM_PROT_READ : VM_PROT_WRITE,
181 				TRUE);
182 			o = (int)uio->uio_offset & PAGE_MASK;
183 			c = (u_int)(PAGE_SIZE - ((int)iov->iov_base & PAGE_MASK));
184 			c = min(c, (u_int)(PAGE_SIZE - o));
185 			c = min(c, (u_int)iov->iov_len);
186 			error = uiomove((caddr_t)&ptvmmap[o], (int)c, uio);
187 			pmap_remove(kernel_pmap, (vm_offset_t)ptvmmap,
188 				    (vm_offset_t)&ptvmmap[PAGE_SIZE]);
189 			continue;
190 
191 /* minor device 1 is kernel memory */
192 		case 1: {
193 			vm_offset_t addr, eaddr;
194 			c = iov->iov_len;
195 
196 			/*
197 			 * Make sure that all of the pages are currently resident so
198 			 * that we don't create any zero-fill pages.
199 			 */
200 			addr = trunc_page(uio->uio_offset);
201 			eaddr = round_page(uio->uio_offset + c);
202 
203 			if (addr < (vm_offset_t)VADDR(PTDPTDI, 0))
204 				return EFAULT;
205 			if (eaddr >= (vm_offset_t)VADDR(APTDPTDI, 0))
206 				return EFAULT;
207 			for (; addr < eaddr; addr += PAGE_SIZE)
208 				if (pmap_extract(kernel_pmap, addr) == 0)
209 					return EFAULT;
210 
211 			if (!kernacc((caddr_t)(int)uio->uio_offset, c,
212 			    uio->uio_rw == UIO_READ ?
213 			    VM_PROT_READ : VM_PROT_WRITE))
214 				return (EFAULT);
215 			error = uiomove((caddr_t)(int)uio->uio_offset, (int)c, uio);
216 			continue;
217 		}
218 
219 /* minor device 2 is EOF/RATHOLE */
220 		case 2:
221 			if (uio->uio_rw == UIO_READ)
222 				return (0);
223 			c = iov->iov_len;
224 			break;
225 
226 /* minor device 3 (/dev/random) is source of filth on read, rathole on write */
227 		case 3:
228 			if (uio->uio_rw == UIO_WRITE) {
229 				c = iov->iov_len;
230 				break;
231 			}
232 			if (buf == NULL)
233 				buf = (caddr_t)
234 				    malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
235 			c = min(iov->iov_len, PAGE_SIZE);
236 			poolsize = read_random(buf, c);
237 			if (poolsize == 0) {
238 				if (buf)
239 					free(buf, M_TEMP);
240 				return (0);
241 			}
242 			c = min(c, poolsize);
243 			error = uiomove(buf, (int)c, uio);
244 			continue;
245 
246 /* minor device 4 (/dev/urandom) is source of muck on read, rathole on write */
247 		case 4:
248 			if (uio->uio_rw == UIO_WRITE) {
249 				c = iov->iov_len;
250 				break;
251 			}
252 			if (CURSIG(curproc) != 0) {
253 				/*
254 				 * Use tsleep() to get the error code right.
255 				 * It should return immediately.
256 				 */
257 				error = tsleep(&random_softc[0],
258 				    PZERO | PCATCH, "urand", 1);
259 				if (error != 0 && error != EWOULDBLOCK)
260 					continue;
261 			}
262 			if (buf == NULL)
263 				buf = (caddr_t)
264 				    malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
265 			c = min(iov->iov_len, PAGE_SIZE);
266 			poolsize = read_random_unlimited(buf, c);
267 			c = min(c, poolsize);
268 			error = uiomove(buf, (int)c, uio);
269 			continue;
270 
271 /* minor device 12 (/dev/zero) is source of nulls on read, rathole on write */
272 		case 12:
273 			if (uio->uio_rw == UIO_WRITE) {
274 				c = iov->iov_len;
275 				break;
276 			}
277 			if (zbuf == NULL) {
278 				zbuf = (caddr_t)
279 				    malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
280 				bzero(zbuf, PAGE_SIZE);
281 			}
282 			c = min(iov->iov_len, PAGE_SIZE);
283 			error = uiomove(zbuf, (int)c, uio);
284 			continue;
285 
286 		default:
287 			return (ENXIO);
288 		}
289 		if (error)
290 			break;
291 		iov->iov_base += c;
292 		iov->iov_len -= c;
293 		uio->uio_offset += c;
294 		uio->uio_resid -= c;
295 	}
296 	if (buf)
297 		free(buf, M_TEMP);
298 	return (error);
299 }
300 
301 
302 
303 
304 /*******************************************************\
305 * allow user processes to MMAP some memory sections	*
306 * instead of going through read/write			*
307 \*******************************************************/
308 static int
309 memmmap(dev_t dev, vm_offset_t offset, int nprot)
310 {
311 	switch (minor(dev))
312 	{
313 
314 /* minor device 0 is physical memory */
315 	case 0:
316         	return i386_btop(offset);
317 
318 /* minor device 1 is kernel memory */
319 	case 1:
320         	return i386_btop(vtophys(offset));
321 
322 	default:
323 		return -1;
324 	}
325 }
326 
327 static int
328 mmioctl(dev, cmd, data, flags, p)
329 	dev_t dev;
330 	u_long cmd;
331 	caddr_t data;
332 	int flags;
333 	struct proc *p;
334 {
335 
336 	switch (minor(dev)) {
337 	case 0:
338 		return mem_ioctl(dev, cmd, data, flags, p);
339 	case 3:
340 	case 4:
341 		return random_ioctl(dev, cmd, data, flags, p);
342 	}
343 	return (ENODEV);
344 }
345 
346 /*
347  * Operations for changing memory attributes.
348  *
349  * This is basically just an ioctl shim for mem_range_attr_get
350  * and mem_range_attr_set.
351  */
352 static int
353 mem_ioctl(dev, cmd, data, flags, p)
354 	dev_t dev;
355 	u_long cmd;
356 	caddr_t data;
357 	int flags;
358 	struct proc *p;
359 {
360 	int nd, error = 0;
361 	struct mem_range_op *mo = (struct mem_range_op *)data;
362 	struct mem_range_desc *md;
363 
364 	/* is this for us? */
365 	if ((cmd != MEMRANGE_GET) &&
366 	    (cmd != MEMRANGE_SET))
367 		return (ENOTTY);
368 
369 	/* any chance we can handle this? */
370 	if (mem_range_softc.mr_op == NULL)
371 		return (EOPNOTSUPP);
372 
373 	/* do we have any descriptors? */
374 	if (mem_range_softc.mr_ndesc == 0)
375 		return (ENXIO);
376 
377 	switch (cmd) {
378 	case MEMRANGE_GET:
379 		nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc);
380 		if (nd > 0) {
381 			md = (struct mem_range_desc *)
382 				malloc(nd * sizeof(struct mem_range_desc),
383 				       M_MEMDESC, M_WAITOK);
384 			error = mem_range_attr_get(md, &nd);
385 			if (!error)
386 				error = copyout(md, mo->mo_desc,
387 					nd * sizeof(struct mem_range_desc));
388 			free(md, M_MEMDESC);
389 		} else {
390 			nd = mem_range_softc.mr_ndesc;
391 		}
392 		mo->mo_arg[0] = nd;
393 		break;
394 
395 	case MEMRANGE_SET:
396 		md = (struct mem_range_desc *)malloc(sizeof(struct mem_range_desc),
397 						    M_MEMDESC, M_WAITOK);
398 		error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc));
399 		/* clamp description string */
400 		md->mr_owner[sizeof(md->mr_owner) - 1] = 0;
401 		if (error == 0)
402 			error = mem_range_attr_set(md, &mo->mo_arg[0]);
403 		free(md, M_MEMDESC);
404 		break;
405 	}
406 	return (error);
407 }
408 
409 /*
410  * Implementation-neutral, kernel-callable functions for manipulating
411  * memory range attributes.
412  */
413 int
414 mem_range_attr_get(mrd, arg)
415 	struct mem_range_desc *mrd;
416 	int *arg;
417 {
418 	/* can we handle this? */
419 	if (mem_range_softc.mr_op == NULL)
420 		return (EOPNOTSUPP);
421 
422 	if (*arg == 0) {
423 		*arg = mem_range_softc.mr_ndesc;
424 	} else {
425 		bcopy(mem_range_softc.mr_desc, mrd, (*arg) * sizeof(struct mem_range_desc));
426 	}
427 	return (0);
428 }
429 
430 int
431 mem_range_attr_set(mrd, arg)
432 	struct mem_range_desc *mrd;
433 	int *arg;
434 {
435 	/* can we handle this? */
436 	if (mem_range_softc.mr_op == NULL)
437 		return (EOPNOTSUPP);
438 
439 	return (mem_range_softc.mr_op->set(&mem_range_softc, mrd, arg));
440 }
441 
442 #ifdef SMP
443 void
444 mem_range_AP_init(void)
445 {
446 	if (mem_range_softc.mr_op && mem_range_softc.mr_op->initAP)
447 		return (mem_range_softc.mr_op->initAP(&mem_range_softc));
448 }
449 #endif
450 
451 static int
452 random_ioctl(dev, cmd, data, flags, p)
453 	dev_t dev;
454 	u_long cmd;
455 	caddr_t data;
456 	int flags;
457 	struct proc *p;
458 {
459 	static intrmask_t interrupt_allowed;
460 	intrmask_t interrupt_mask;
461 	int error, intr;
462 	struct random_softc *sc;
463 
464 	/*
465 	 * We're the random or urandom device.  The only ioctls are for
466 	 * selecting and inspecting which interrupts are used in the muck
467 	 * gathering business.
468 	 */
469 	if (cmd != MEM_SETIRQ && cmd != MEM_CLEARIRQ && cmd != MEM_RETURNIRQ)
470 		return (ENOTTY);
471 
472 	/*
473 	 * Even inspecting the state is privileged, since it gives a hint
474 	 * about how easily the randomness might be guessed.
475 	 */
476 	error = suser(p);
477 	if (error != 0)
478 		return (error);
479 
480 	/*
481 	 * XXX the data is 16-bit due to a historical botch, so we use
482 	 * magic 16's instead of ICU_LEN and can't support 24 interrupts
483 	 * under SMP.
484 	 */
485 	intr = *(int16_t *)data;
486 	if (cmd != MEM_RETURNIRQ && (intr < 0 || intr >= 16))
487 		return (EINVAL);
488 
489 	interrupt_mask = 1 << intr;
490 	sc = &random_softc[intr];
491 	switch (cmd) {
492 	case MEM_SETIRQ:
493 		if (interrupt_allowed & interrupt_mask)
494 			break;
495 		interrupt_allowed |= interrupt_mask;
496 		sc->sc_intr = intr;
497 		disable_intr();
498 		sc->sc_handler = intr_handler[intr];
499 		intr_handler[intr] = add_interrupt_randomness;
500 		sc->sc_arg = intr_unit[intr];
501 		intr_unit[intr] = sc;
502 		enable_intr();
503 		break;
504 	case MEM_CLEARIRQ:
505 		if (!(interrupt_allowed & interrupt_mask))
506 			break;
507 		interrupt_allowed &= ~interrupt_mask;
508 		disable_intr();
509 		intr_handler[intr] = sc->sc_handler;
510 		intr_unit[intr] = sc->sc_arg;
511 		enable_intr();
512 		break;
513 	case MEM_RETURNIRQ:
514 		*(u_int16_t *)data = interrupt_allowed;
515 		break;
516 	}
517 	return (0);
518 }
519 
520 int
521 mmpoll(dev, events, p)
522 	dev_t dev;
523 	int events;
524 	struct proc *p;
525 {
526 	switch (minor(dev)) {
527 	case 3:		/* /dev/random */
528 		return random_poll(dev, events, p);
529 	case 4:		/* /dev/urandom */
530 	default:
531 		return seltrue(dev, events, p);
532 	}
533 }
534 
535 int
536 iszerodev(dev)
537 	dev_t dev;
538 {
539 	return ((major(dev) == mem_cdevsw.d_maj)
540 	  && minor(dev) == 12);
541 }
542 
543 static void
544 mem_drvinit(void *unused)
545 {
546 
547 	/* Initialise memory range handling */
548 	if (mem_range_softc.mr_op != NULL)
549 		mem_range_softc.mr_op->init(&mem_range_softc);
550 
551 	make_dev(&mem_cdevsw, 0, UID_ROOT, GID_KMEM, 0640, "mem");
552 	make_dev(&mem_cdevsw, 1, UID_ROOT, GID_KMEM, 0640, "kmem");
553 	make_dev(&mem_cdevsw, 2, UID_ROOT, GID_WHEEL, 0666, "null");
554 	make_dev(&mem_cdevsw, 3, UID_ROOT, GID_WHEEL, 0644, "random");
555 	make_dev(&mem_cdevsw, 4, UID_ROOT, GID_WHEEL, 0644, "urandom");
556 	make_dev(&mem_cdevsw, 12, UID_ROOT, GID_WHEEL, 0666, "zero");
557 	make_dev(&mem_cdevsw, 14, UID_ROOT, GID_WHEEL, 0600, "io");
558 }
559 
560 SYSINIT(memdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,mem_drvinit,NULL)
561 
562