xref: /original-bsd/sys/hp300/hp300/mem.c (revision 3705696b)
1 /*
2  * Copyright (c) 1988 University of Utah.
3  * Copyright (c) 1982, 1986, 1990, 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.
9  *
10  * %sccs.include.redist.c%
11  *
12  * from: Utah $Hdr: mem.c 1.14 90/10/12$
13  *
14  *	@(#)mem.c	8.1 (Berkeley) 06/10/93
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 int o;
41 	register u_int c, v;
42 	register struct iovec *iov;
43 	int error = 0;
44 	caddr_t zbuf = NULL;
45 	int kernloc;
46 	extern u_int lowram;
47 
48 	while (uio->uio_resid > 0 && error == 0) {
49 		iov = uio->uio_iov;
50 		if (iov->iov_len == 0) {
51 			uio->uio_iov++;
52 			uio->uio_iovcnt--;
53 			if (uio->uio_iovcnt < 0)
54 				panic("mmrw");
55 			continue;
56 		}
57 		switch (minor(dev)) {
58 
59 /* minor device 0 is physical memory */
60 		case 0:
61 			v = uio->uio_offset;
62 #ifndef DEBUG
63 			/* allow reads only in RAM (except for DEBUG) */
64 			if (v >= 0xFFFFFFFC || v < lowram)
65 				return (EFAULT);
66 #endif
67 			pmap_enter(kernel_pmap, (vm_offset_t)vmmap,
68 			    trunc_page(v), uio->uio_rw == UIO_READ ?
69 			    VM_PROT_READ : VM_PROT_WRITE, TRUE);
70 			o = (int)uio->uio_offset & PGOFSET;
71 			c = (u_int)(NBPG - ((int)iov->iov_base & PGOFSET));
72 			c = min(c, (u_int)(NBPG - o));
73 			c = min(c, (u_int)iov->iov_len);
74 			error = uiomove((caddr_t)&vmmap[o], (int)c, uio);
75 			pmap_remove(kernel_pmap, (vm_offset_t)vmmap,
76 			    (vm_offset_t)&vmmap[NBPG]);
77 			continue;
78 
79 /* minor device 1 is kernel memory */
80 		case 1:
81 			kernloc = uio->uio_offset;
82 			c = min(iov->iov_len, MAXPHYS);
83 			if (!kernacc((caddr_t)kernloc, c,
84 			    uio->uio_rw == UIO_READ ? B_READ : B_WRITE))
85 				return (EFAULT);
86 			error = uiomove((caddr_t)kernloc, (int)c, uio);
87 			continue;
88 
89 /* minor device 2 is EOF/RATHOLE */
90 		case 2:
91 			if (uio->uio_rw == UIO_WRITE)
92 				uio->uio_resid = 0;
93 			return (0);
94 
95 /* minor device 12 (/dev/zero) is source of nulls on read, rathole on write */
96 		case 12:
97 			if (uio->uio_rw == UIO_WRITE) {
98 				c = iov->iov_len;
99 				break;
100 			}
101 			if (zbuf == NULL) {
102 				zbuf = (caddr_t)
103 				    malloc(CLBYTES, M_TEMP, M_WAITOK);
104 				bzero(zbuf, CLBYTES);
105 			}
106 			c = min(iov->iov_len, CLBYTES);
107 			error = uiomove(zbuf, (int)c, uio);
108 			continue;
109 
110 		default:
111 			return (ENXIO);
112 		}
113 		if (error)
114 			break;
115 		iov->iov_base += c;
116 		iov->iov_len -= c;
117 		uio->uio_offset += c;
118 		uio->uio_resid -= c;
119 	}
120 	if (zbuf)
121 		free(zbuf, M_TEMP);
122 	return (error);
123 }
124