xref: /386bsd/usr/src/kernel/mem/mem.c (revision a2142627)
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  *	$Id: mem.c,v 1.3 94/07/07 21:05:52 bill Exp Locker: bill $
40  */
41 
42 /*
43  * Memory special file
44  */
45 static char *mem_config =
46 	"mem 2.	 # mem device /dev/mem $Revision: 1.3 $";
47 
48 #include "sys/param.h"
49 #include "sys/errno.h"
50 #include "sys/mman.h"
51 #include "malloc.h"
52 #include "uio.h"
53 #include "modconfig.h"
54 #include "prototypes.h"
55 
56 #include "machine/cpu.h"
57 
58 #include "vm.h"
59 #include "vmspace.h"
60 
61 extern        char *vmmap;            /* poor name! */
62 
63 int
mmrw(dev_t dev,struct uio * uio,int flags)64 mmrw(dev_t dev, struct uio *uio, int flags)
65 {
66 	register int o;
67 	register u_int c, v;
68 	register struct iovec *iov;
69 	int error = 0;
70 	caddr_t zbuf = NULL;
71 
72 	while (uio->uio_resid > 0 && error == 0) {
73 		iov = uio->uio_iov;
74 		if (iov->iov_len == 0) {
75 			uio->uio_iov++;
76 			uio->uio_iovcnt--;
77 			if (uio->uio_iovcnt < 0)
78 				panic("mmrw");
79 			continue;
80 		}
81 		switch (minor(dev)) {
82 
83 /* minor device 0 is physical memory */
84 		case 0:
85 			v = uio->uio_offset;
86 			pmap_enter(pmap_kernel(), (vm_offset_t) vmmap, v,
87 				uio->uio_rw == UIO_READ ? VM_PROT_READ : VM_PROT_WRITE,
88 				TRUE, AM_NONE);
89 			o = (int)uio->uio_offset & PGOFSET;
90 			c = (u_int)(NBPG - ((int)iov->iov_base & PGOFSET));
91 			c = min(c, (u_int)(NBPG - o));
92 			c = min(c, (u_int)iov->iov_len);
93 			error = uiomove((caddr_t)&vmmap[o], (int)c, uio);
94 			pmap_remove(pmap_kernel(), (vm_offset_t) vmmap, (vm_offset_t)vmmap+NBPG);
95 			continue;
96 
97 /* minor device 1 is kernel memory */
98 		case 1:
99 			c = iov->iov_len;
100 			if (!vmspace_access(&kernspace, (caddr_t)uio->uio_offset, c,
101 			    uio->uio_rw == UIO_READ ? PROT_READ : PROT_WRITE))
102 				return(EFAULT);
103 			error = uiomove((caddr_t)uio->uio_offset, (int)c, uio);
104 			continue;
105 
106 /* minor device 2 is EOF/RATHOLE */
107 		case 2:
108 			if (uio->uio_rw == UIO_READ)
109 				return (0);
110 			c = iov->iov_len;
111 			break;
112 
113 /* minor device 12 (/dev/zero) is source of nulls on read, rathole on write */
114 		case 12:
115 			if (uio->uio_rw == UIO_WRITE) {
116 				c = iov->iov_len;
117 				break;
118 			}
119 			if (zbuf == NULL) {
120 				zbuf = (caddr_t)
121 				    malloc(CLBYTES, M_TEMP, M_WAITOK);
122 				memset(zbuf, 0, CLBYTES);
123 			}
124 			c = min(iov->iov_len, CLBYTES);
125 			error = uiomove(zbuf, (int)c, uio);
126 			continue;
127 
128 #ifdef notyet
129 #ifdef i386
130 /* 386 I/O address space (/dev/ioport[bwl]) is a read/write access to seperate
131    i/o device address bus, different than memory bus. Semantics here are
132    very different than ordinary read/write, as if iov_len is a multiple
133    an implied string move from a single port will be done. Note that lseek
134    must be used to set the port number reliably. */
135 		case 14:
136 			if (iov->iov_len == 1) {
137 				u_char tmp;
138 				tmp = inb(uio->uio_offset);
139 				error = uiomove (&tmp, iov->iov_len, uio);
140 			} else {
141 				if (!useracc((caddr_t)iov->iov_base,
142 					iov->iov_len, uio->uio_rw))
143 					return (EFAULT);
144 				insb(uio->uio_offset, iov->iov_base,
145 					iov->iov_len);
146 			}
147 			break;
148 		case 15:
149 			if (iov->iov_len == sizeof (short)) {
150 				u_short tmp;
151 				tmp = inw(uio->uio_offset);
152 				error = uiomove (&tmp, iov->iov_len, uio);
153 			} else {
154 				if (!useracc((caddr_t)iov->iov_base,
155 					iov->iov_len, uio->uio_rw))
156 					return (EFAULT);
157 				insw(uio->uio_offset, iov->iov_base,
158 					iov->iov_len/ sizeof (short));
159 			}
160 			break;
161 		case 16:
162 			if (iov->iov_len == sizeof (long)) {
163 				u_long tmp;
164 				tmp = inl(uio->uio_offset);
165 				error = uiomove (&tmp, iov->iov_len, uio);
166 			} else {
167 				if (!useracc((caddr_t)iov->iov_base,
168 					iov->iov_len, uio->uio_rw))
169 					return (EFAULT);
170 				insl(uio->uio_offset, iov->iov_base,
171 					iov->iov_len/ sizeof (long));
172 			}
173 			break;
174 #endif
175 #endif
176 
177 		default:
178 			return (ENXIO);
179 		}
180 		if (error)
181 			break;
182 		iov->iov_base += c;
183 		iov->iov_len -= c;
184 		uio->uio_offset += c;
185 		uio->uio_resid -= c;
186 	}
187 	if (zbuf)
188 		free(zbuf, M_TEMP);
189 	return (error);
190 }
191 
192 /*
193  * XXX experimental
194  * (there is no way for dynamicly remapped regions in the kernel
195  *  to shoot down mmap aliases to the kernel pages, as the association
196  *  is busted by the mapping -wfj)
197  */
198 int
mmmmap(dev_t dev,int offset,int nprot)199 mmmmap(dev_t dev, int offset, int nprot)
200 {
201 
202 	switch (minor(dev)) {
203 
204 	case 0:
205 		break;
206 	case 1:
207 		/* is it in kernel map? */
208 		if (!vmspace_access(&kernspace, (caddr_t)offset, NBPG, nprot))
209 			return(-1);
210 		/* what physical address are we at? */
211 		 offset = (int) pmap_extract(pmap_kernel(),
212 					(vm_offset_t) offset);
213 		/* XXX pmap_extract should return -1, not 0 for non-existant */
214 		/* if (offset == 0)
215 			return (-1); */
216 		break;
217 	default:
218 		return (-1);
219 	}
220 	return i386_btop(offset);	/* XXX */
221 }
222 
223 static struct devif mem_devif =
224 {
225 	{0}, -1, -2, 0, 0, 0, 0, 0, 0,
226 	0, 0, 0, mmrw, mmrw, 0, 0,
227 	0, 0, 0, 0,
228 };
229 
230 int mem_no;
231 
DRIVER_MODCONFIG()232 DRIVER_MODCONFIG() {
233 	char *cfg_string = mem_config;
234 
235 	/* configure device? */
236 	if (devif_config(&cfg_string, &mem_devif) == 0)
237 		return;
238 
239 	/* XXX trivial kernel dependancy: kernel knows driver state. */
240 	mem_no = mem_devif.di_cmajor;	/* should this be a di_flags bit? */
241 }
242