mm.c revision 1.13.16.4 1 /* $NetBSD: mm.c,v 1.13.16.4 2010/06/02 03:12:43 rmind Exp $ */
2
3 /*-
4 * Copyright (c) 2002, 2008, 2010 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas, Joerg Sonnenberger and Mindaugas Rasiukevicius.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Special /dev/{mem,kmem,zero,null} memory devices.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: mm.c,v 1.13.16.4 2010/06/02 03:12:43 rmind Exp $");
38
39 #include "opt_compat_netbsd.h"
40
41 #include <sys/param.h>
42 #include <sys/conf.h>
43 #include <sys/ioctl.h>
44 #include <sys/mman.h>
45 #include <sys/uio.h>
46 #include <sys/termios.h>
47
48 #include <dev/mm.h>
49
50 #include <uvm/uvm_extern.h>
51
52 static void * dev_zero_page;
53 static kmutex_t dev_mem_lock;
54 static vaddr_t dev_mem_addr;
55
56 static dev_type_read(mm_readwrite);
57 static dev_type_ioctl(mm_ioctl);
58 static dev_type_mmap(mm_mmap);
59 static dev_type_ioctl(mm_ioctl);
60
61 const struct cdevsw mem_cdevsw = {
62 #ifdef __HAVE_MM_MD_OPEN
63 mm_md_open,
64 #else
65 nullopen,
66 #endif
67 nullclose, mm_readwrite, mm_readwrite,
68 mm_ioctl, nostop, notty, nopoll, mm_mmap, nokqfilter,
69 D_MPSAFE
70 };
71
72 #ifdef pmax /* XXX */
73 const struct cdevsw mem_ultrix_cdevsw = {
74 nullopen, nullclose, mm_readwrite, mm_readwrite, mm_ioctl,
75 nostop, notty, nopoll, mm_mmap, nokqfilter, D_MPSAFE
76 };
77 #endif
78
79 /*
80 * mm_init: initialize memory device driver.
81 */
82 void
83 mm_init(void)
84 {
85 vaddr_t pg;
86
87 mutex_init(&dev_mem_lock, MUTEX_DEFAULT, IPL_NONE);
88
89 /* Read-only zero-page. */
90 pg = uvm_km_alloc(kernel_map, PAGE_SIZE, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
91 KASSERT(pg != 0);
92 pmap_protect(pmap_kernel(), pg, pg + PAGE_SIZE, VM_PROT_READ);
93 pmap_update(pmap_kernel());
94 dev_zero_page = (void *)pg;
95
96 #ifndef __HAVE_MM_MD_CACHE_ALIASING
97 /* KVA for mappings during I/O. */
98 dev_mem_addr = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
99 UVM_KMF_VAONLY|UVM_KMF_WAITVA);
100 KASSERT(dev_mem_addr != 0);
101 #else
102 dev_mem_addr = 0;
103 #endif
104 }
105
106
107 /*
108 * dev_mem_getva: get a special virtual address. If architecture requires,
109 * allocate VA according to PA, which avoids cache-aliasing issues. Use a
110 * constant, general mapping address otherwise.
111 */
112 static inline vaddr_t
113 dev_mem_getva(paddr_t pa)
114 {
115 #ifdef __HAVE_MM_MD_CACHE_ALIASING
116 const vsize_t coloroff = trunc_page(pa) & ptoa(uvmexp.colormask);
117 const vaddr_t kva = uvm_km_alloc(kernel_map, PAGE_SIZE + coloroff,
118 ptoa(uvmexp.ncolors), UVM_KMF_VAONLY | UVM_KMF_WAITVA);
119
120 return kva + coloroff;
121 #else
122 return dev_mem_addr;
123 #endif
124 }
125
126 static inline void
127 dev_mem_relva(paddr_t pa, vaddr_t va)
128 {
129 #ifdef __HAVE_MM_MD_CACHE_ALIASING
130 const vsize_t coloroff = trunc_page(pa) & ptoa(uvmexp.colormask);
131 const vaddr_t origva = va - coloroff;
132
133 uvm_km_free(kernel_map, origva, PAGE_SIZE + coloroff, UVM_KMF_VAONLY);
134 #else
135 KASSERT(dev_mem_addr == va);
136 #endif
137 }
138
139 /*
140 * dev_kmem_readwrite: helper for DEV_MEM (/dev/mem) case of R/W.
141 */
142 static int
143 dev_mem_readwrite(struct uio *uio, struct iovec *iov)
144 {
145 paddr_t paddr;
146 vaddr_t vaddr;
147 vm_prot_t prot;
148 size_t len, offset;
149 bool have_direct;
150 int error;
151
152 /* Check for wrap around. */
153 if ((intptr_t)uio->uio_offset != uio->uio_offset) {
154 return EFAULT;
155 }
156 paddr = uio->uio_offset & ~PAGE_MASK;
157 prot = (uio->uio_rw == UIO_WRITE) ? VM_PROT_WRITE : VM_PROT_READ;
158 error = mm_md_physacc(paddr, prot);
159 if (error) {
160 return error;
161 }
162 offset = uio->uio_offset & PAGE_MASK;
163 len = min(uio->uio_resid, PAGE_SIZE - offset);
164
165 #ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS
166 /* Is physical address directly mapped? Return VA. */
167 have_direct = mm_md_direct_mapped_phys(paddr, &vaddr);
168 #else
169 have_direct = false;
170 #endif
171 if (!have_direct) {
172 /* Get a special virtual address. */
173 const vaddr_t va = dev_mem_getva(paddr);
174
175 /* Map selected KVA to physical address. */
176 mutex_enter(&dev_mem_lock);
177 pmap_kenter_pa(va, paddr, prot, 0);
178 pmap_update(pmap_kernel());
179
180 /* Perform I/O. */
181 vaddr = va + offset;
182 error = uiomove((void *)vaddr, len, uio);
183
184 /* Unmap, flush before unlock. */
185 pmap_kremove(va, PAGE_SIZE);
186 pmap_update(pmap_kernel());
187 mutex_exit(&dev_mem_lock);
188
189 /* "Release" the virtual address. */
190 dev_mem_relva(paddr, va);
191 } else {
192 /* Direct map, just perform I/O. */
193 vaddr += offset;
194 error = uiomove((void *)vaddr, len, uio);
195 }
196 return error;
197 }
198
199 /*
200 * dev_kmem_readwrite: helper for DEV_KMEM (/dev/kmem) case of R/W.
201 */
202 static int
203 dev_kmem_readwrite(struct uio *uio, struct iovec *iov)
204 {
205 void *addr;
206 size_t len, offset;
207 vm_prot_t prot;
208 int error;
209 bool md_kva;
210
211 /* Check for wrap around. */
212 addr = (void *)(intptr_t)uio->uio_offset;
213 if ((uintptr_t)addr != uio->uio_offset) {
214 return EFAULT;
215 }
216 /*
217 * Handle non-page aligned offset.
218 * Otherwise, we operate in page-by-page basis.
219 */
220 offset = uio->uio_offset & PAGE_MASK;
221 len = min(uio->uio_resid, PAGE_SIZE - offset);
222 prot = (uio->uio_rw == UIO_WRITE) ? VM_PROT_WRITE : VM_PROT_READ;
223
224 md_kva = false;
225
226 #ifdef __HAVE_MM_MD_DIRECT_MAPPED_IO
227 paddr_t paddr;
228 /* MD case: is this is a directly mapped address? */
229 if (mm_md_direct_mapped_io(addr, &paddr)) {
230 /* If so, validate physical address. */
231 error = mm_md_physacc(paddr, prot);
232 if (error) {
233 return error;
234 }
235 md_kva = true;
236 }
237 #endif
238 if (!md_kva) {
239 bool checked = false;
240
241 #ifdef __HAVE_MM_MD_KERNACC
242 /* MD check for the address. */
243 error = mm_md_kernacc(addr, prot, &checked);
244 if (error) {
245 return error;
246 }
247 #endif
248 /* UVM check for the address (unless MD indicated to not). */
249 if (!checked && !uvm_kernacc(addr, len, prot)) {
250 return EFAULT;
251 }
252 }
253 error = uiomove(addr, len, uio);
254 return error;
255 }
256
257 /*
258 * dev_zero_readwrite: helper for DEV_ZERO (/dev/null) case of R/W.
259 */
260 static inline int
261 dev_zero_readwrite(struct uio *uio, struct iovec *iov)
262 {
263 size_t len;
264
265 /* Nothing to do for the write case. */
266 if (uio->uio_rw == UIO_WRITE) {
267 uio->uio_resid = 0;
268 return 0;
269 }
270 /*
271 * Read in page-by-page basis, caller will continue.
272 * Cut appropriately for a single/last-iteration cases.
273 */
274 len = min(iov->iov_len, PAGE_SIZE);
275 return uiomove(dev_zero_page, len, uio);
276 }
277
278 /*
279 * mm_readwrite: general memory R/W function.
280 */
281 static int
282 mm_readwrite(dev_t dev, struct uio *uio, int flags)
283 {
284 struct iovec *iov;
285 int error;
286
287 #ifdef __HAVE_MM_MD_READWRITE
288 /* If defined - there are extra MD cases. */
289 switch (minor(dev)) {
290 case DEV_MEM:
291 case DEV_KMEM:
292 case DEV_NULL:
293 case DEV_ZERO:
294 #if defined(COMPAT_16) && defined(__arm)
295 case _DEV_ZERO_oARM:
296 #endif
297 break;
298 default:
299 return mm_md_readwrite(dev, uio);
300 }
301 #endif
302 error = 0;
303 while (uio->uio_resid > 0 && error == 0) {
304 iov = uio->uio_iov;
305 if (iov->iov_len == 0) {
306 /* Processed; next I/O vector. */
307 uio->uio_iov++;
308 uio->uio_iovcnt--;
309 KASSERT(uio->uio_iovcnt >= 0);
310 continue;
311 }
312 /* Helper functions will process in page-by-page basis. */
313 switch (minor(dev)) {
314 case DEV_MEM:
315 error = dev_mem_readwrite(uio, iov);
316 break;
317 case DEV_KMEM:
318 error = dev_kmem_readwrite(uio, iov);
319 break;
320 case DEV_NULL:
321 if (uio->uio_rw == UIO_WRITE) {
322 uio->uio_resid = 0;
323 }
324 /* Break directly out of the loop. */
325 return 0;
326 #if defined(COMPAT_16) && defined(__arm)
327 case _DEV_ZERO_oARM:
328 #endif
329 case DEV_ZERO:
330 error = dev_zero_readwrite(uio, iov);
331 break;
332 default:
333 error = ENXIO;
334 break;
335 }
336 }
337 return error;
338 }
339
340 /*
341 * mm_mmap: general mmap() handler.
342 */
343 static paddr_t
344 mm_mmap(dev_t dev, off_t off, int acc)
345 {
346 vm_prot_t prot;
347
348 #ifdef __HAVE_MM_MD_MMAP
349 /* If defined - there are extra mmap() MD cases. */
350 switch (minor(dev)) {
351 case DEV_MEM:
352 case DEV_KMEM:
353 case DEV_NULL:
354 #if defined(COMPAT_16) && defined(__arm)
355 case _DEV_ZERO_oARM:
356 #endif
357 case DEV_ZERO:
358 break;
359 default:
360 return mm_md_mmap(dev, off, acc);
361 }
362 #endif
363 /*
364 * /dev/null does not make sense, /dev/kmem is volatile and
365 * /dev/zero is handled in mmap already.
366 */
367 if (minor(dev) != DEV_MEM) {
368 return -1;
369 }
370
371 prot = 0;
372 if (acc & PROT_EXEC)
373 prot |= VM_PROT_EXECUTE;
374 if (acc & PROT_READ)
375 prot |= VM_PROT_READ;
376 if (acc & PROT_WRITE)
377 prot |= VM_PROT_WRITE;
378
379 /* Validate the physical address. */
380 if (mm_md_physacc(off, prot) != 0) {
381 return -1;
382 }
383 return off >> PGSHIFT;
384 }
385
386 static int
387 mm_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
388 {
389
390 switch (cmd) {
391 case FIONBIO:
392 /* We never block anyway. */
393 return 0;
394
395 case FIOSETOWN:
396 case FIOGETOWN:
397 case TIOCGPGRP:
398 case TIOCSPGRP:
399 case TIOCGETA:
400 return ENOTTY;
401
402 case FIOASYNC:
403 if ((*(int *)data) == 0) {
404 return 0;
405 }
406 /* FALLTHROUGH */
407 default:
408 return EOPNOTSUPP;
409 }
410 }
411