xref: /original-bsd/sys/pmax/pmax/mem.c (revision e66e06db)
1 /*
2  * Copyright (c) 1988 University of Utah.
3  * Copyright (c) 1992, 1993
4  *	The Regents of the University of California.  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 Ralph Campbell.
9  *
10  * %sccs.include.redist.c%
11  *
12  * from: Utah $Hdr: mem.c 1.14 90/10/12$
13  *
14  *	@(#)mem.c	8.2 (Berkeley) 03/28/94
15  */
16 
17 /*
18  * Memory special file
19  */
20 
21 #include <sys/param.h>
22 #include <sys/conf.h>
23 #include <sys/buf.h>
24 #include <sys/systm.h>
25 #include <sys/malloc.h>
26 
27 #include <machine/cpu.h>
28 
29 #include <vm/vm_param.h>
30 #include <vm/lock.h>
31 #include <vm/vm_prot.h>
32 #include <vm/pmap.h>
33 
34 /*ARGSUSED*/
35 mmrw(dev, uio, flags)
36 	dev_t dev;
37 	struct uio *uio;
38 	int flags;
39 {
40 	register u_long v;
41 	register u_int c;
42 	register struct iovec *iov;
43 	int error = 0;
44 	caddr_t zbuf = NULL;
45 	extern vm_offset_t avail_end;
46 
47 	while (uio->uio_resid > 0 && error == 0) {
48 		iov = uio->uio_iov;
49 		if (iov->iov_len == 0) {
50 			uio->uio_iov++;
51 			uio->uio_iovcnt--;
52 			if (uio->uio_iovcnt < 0)
53 				panic("mmrw");
54 			continue;
55 		}
56 		switch (minor(dev)) {
57 
58 /* minor device 0 is physical memory */
59 		case 0:
60 			v = (u_long)uio->uio_offset;
61 			c = iov->iov_len;
62 			if (v + c <= btoc(physmem))
63 				v += MACH_CACHED_MEMORY_ADDR;
64 			else
65 				return (EFAULT);
66 			error = uiomove((caddr_t)v, (int)c, uio);
67 			continue;
68 
69 /* minor device 1 is kernel memory */
70 		case 1:
71 			v = (u_long)uio->uio_offset;
72 			if (v < MACH_CACHED_MEMORY_ADDR)
73 				return (EFAULT);
74 			c = iov->iov_len;
75 			if (v + c <= MACH_PHYS_TO_CACHED(avail_end) ||
76 			    v >= MACH_KSEG2_ADDR && kernacc((caddr_t)v, c,
77 			    uio->uio_rw == UIO_READ ? B_READ : B_WRITE)) {
78 				error = uiomove((caddr_t)v, (int)c, uio);
79 				continue;
80 			}
81 			return (EFAULT);
82 
83 /* minor device 2 is EOF/RATHOLE */
84 		case 2:
85 			if (uio->uio_rw == UIO_WRITE)
86 				uio->uio_resid = 0;
87 			return (0);
88 
89 /* minor device 12 (/dev/zero) is source of nulls on read, rathole on write */
90 		case 12:
91 			if (uio->uio_rw == UIO_WRITE) {
92 				c = iov->iov_len;
93 				break;
94 			}
95 			if (zbuf == NULL) {
96 				zbuf = (caddr_t)
97 				    malloc(CLBYTES, M_TEMP, M_WAITOK);
98 				bzero(zbuf, CLBYTES);
99 			}
100 			c = min(iov->iov_len, CLBYTES);
101 			error = uiomove(zbuf, (int)c, uio);
102 			continue;
103 
104 		default:
105 			return (ENXIO);
106 		}
107 		if (error)
108 			break;
109 		iov->iov_base += c;
110 		iov->iov_len -= c;
111 		uio->uio_offset += c;
112 		uio->uio_resid -= c;
113 	}
114 	if (zbuf)
115 		free(zbuf, M_TEMP);
116 	return (error);
117 }
118