Home | History | Annotate | Line # | Download | only in dev
mm.c revision 1.14
      1 /*	$NetBSD: mm.c,v 1.14 2011/06/12 03:35:51 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.14 2011/06/12 03:35:51 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	__read_mostly;
     53 static kmutex_t		dev_mem_lock	__cacheline_aligned;
     54 static vaddr_t		dev_mem_addr	__read_mostly;
     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 #if 0
     93 	pmap_protect(pmap_kernel(), pg, pg + PAGE_SIZE, VM_PROT_READ);
     94 #endif
     95 	pmap_update(pmap_kernel());
     96 	dev_zero_page = (void *)pg;
     97 
     98 #ifndef __HAVE_MM_MD_CACHE_ALIASING
     99 	/* KVA for mappings during I/O. */
    100 	dev_mem_addr = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
    101 	    UVM_KMF_VAONLY|UVM_KMF_WAITVA);
    102 	KASSERT(dev_mem_addr != 0);
    103 #else
    104 	dev_mem_addr = 0;
    105 #endif
    106 }
    107 
    108 
    109 /*
    110  * dev_mem_getva: get a special virtual address.  If architecture requires,
    111  * allocate VA according to PA, which avoids cache-aliasing issues.  Use a
    112  * constant, general mapping address otherwise.
    113  */
    114 static inline vaddr_t
    115 dev_mem_getva(paddr_t pa)
    116 {
    117 #ifdef __HAVE_MM_MD_CACHE_ALIASING
    118 	const vsize_t coloroff = trunc_page(pa) & ptoa(uvmexp.colormask);
    119 	const vaddr_t kva = uvm_km_alloc(kernel_map, PAGE_SIZE + coloroff,
    120 	    ptoa(uvmexp.ncolors), UVM_KMF_VAONLY | UVM_KMF_WAITVA);
    121 
    122 	return kva + coloroff;
    123 #else
    124 	return dev_mem_addr;
    125 #endif
    126 }
    127 
    128 static inline void
    129 dev_mem_relva(paddr_t pa, vaddr_t va)
    130 {
    131 #ifdef __HAVE_MM_MD_CACHE_ALIASING
    132 	const vsize_t coloroff = trunc_page(pa) & ptoa(uvmexp.colormask);
    133 	const vaddr_t origva = va - coloroff;
    134 
    135 	uvm_km_free(kernel_map, origva, PAGE_SIZE + coloroff, UVM_KMF_VAONLY);
    136 #else
    137 	KASSERT(dev_mem_addr == va);
    138 #endif
    139 }
    140 
    141 /*
    142  * dev_kmem_readwrite: helper for DEV_MEM (/dev/mem) case of R/W.
    143  */
    144 static int
    145 dev_mem_readwrite(struct uio *uio, struct iovec *iov)
    146 {
    147 	paddr_t paddr;
    148 	vaddr_t vaddr;
    149 	vm_prot_t prot;
    150 	size_t len, offset;
    151 	bool have_direct;
    152 	int error;
    153 
    154 	/* Check for wrap around. */
    155 	if ((intptr_t)uio->uio_offset != uio->uio_offset) {
    156 		return EFAULT;
    157 	}
    158 	paddr = uio->uio_offset & ~PAGE_MASK;
    159 	prot = (uio->uio_rw == UIO_WRITE) ? VM_PROT_WRITE : VM_PROT_READ;
    160 	error = mm_md_physacc(paddr, prot);
    161 	if (error) {
    162 		return error;
    163 	}
    164 	offset = uio->uio_offset & PAGE_MASK;
    165 	len = MIN(uio->uio_resid, PAGE_SIZE - offset);
    166 
    167 #ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS
    168 	/* Is physical address directly mapped?  Return VA. */
    169 	have_direct = mm_md_direct_mapped_phys(paddr, &vaddr);
    170 #else
    171 	have_direct = false;
    172 #endif
    173 	if (!have_direct) {
    174 		/* Get a special virtual address. */
    175 		const vaddr_t va = dev_mem_getva(paddr);
    176 
    177 		/* Map selected KVA to physical address. */
    178 		mutex_enter(&dev_mem_lock);
    179 		pmap_kenter_pa(va, paddr, prot, 0);
    180 		pmap_update(pmap_kernel());
    181 
    182 		/* Perform I/O. */
    183 		vaddr = va + offset;
    184 		error = uiomove((void *)vaddr, len, uio);
    185 
    186 		/* Unmap, flush before unlock. */
    187 		pmap_kremove(va, PAGE_SIZE);
    188 		pmap_update(pmap_kernel());
    189 		mutex_exit(&dev_mem_lock);
    190 
    191 		/* "Release" the virtual address. */
    192 		dev_mem_relva(paddr, va);
    193 	} else {
    194 		/* Direct map, just perform I/O. */
    195 		vaddr += offset;
    196 		error = uiomove((void *)vaddr, len, uio);
    197 	}
    198 	return error;
    199 }
    200 
    201 /*
    202  * dev_kmem_readwrite: helper for DEV_KMEM (/dev/kmem) case of R/W.
    203  */
    204 static int
    205 dev_kmem_readwrite(struct uio *uio, struct iovec *iov)
    206 {
    207 	void *addr;
    208 	size_t len, offset;
    209 	vm_prot_t prot;
    210 	int error;
    211 	bool md_kva;
    212 
    213 	/* Check for wrap around. */
    214 	addr = (void *)(intptr_t)uio->uio_offset;
    215 	if ((uintptr_t)addr != uio->uio_offset) {
    216 		return EFAULT;
    217 	}
    218 	/*
    219 	 * Handle non-page aligned offset.
    220 	 * Otherwise, we operate in page-by-page basis.
    221 	 */
    222 	offset = uio->uio_offset & PAGE_MASK;
    223 	len = MIN(uio->uio_resid, PAGE_SIZE - offset);
    224 	prot = (uio->uio_rw == UIO_WRITE) ? VM_PROT_WRITE : VM_PROT_READ;
    225 
    226 	md_kva = false;
    227 
    228 #ifdef __HAVE_MM_MD_DIRECT_MAPPED_IO
    229 	paddr_t paddr;
    230 	/* MD case: is this is a directly mapped address? */
    231 	if (mm_md_direct_mapped_io(addr, &paddr)) {
    232 		/* If so, validate physical address. */
    233 		error = mm_md_physacc(paddr, prot);
    234 		if (error) {
    235 			return error;
    236 		}
    237 		md_kva = true;
    238 	}
    239 #endif
    240 	if (!md_kva) {
    241 		bool checked = false;
    242 
    243 #ifdef __HAVE_MM_MD_KERNACC
    244 		/* MD check for the address. */
    245 		error = mm_md_kernacc(addr, prot, &checked);
    246 		if (error) {
    247 			return error;
    248 		}
    249 #endif
    250 		/* UVM check for the address (unless MD indicated to not). */
    251 		if (!checked && !uvm_kernacc(addr, len, prot)) {
    252 			return EFAULT;
    253 		}
    254 	}
    255 	error = uiomove(addr, len, uio);
    256 	return error;
    257 }
    258 
    259 /*
    260  * dev_zero_readwrite: helper for DEV_ZERO (/dev/null) case of R/W.
    261  */
    262 static inline int
    263 dev_zero_readwrite(struct uio *uio, struct iovec *iov)
    264 {
    265 	size_t len;
    266 
    267 	/* Nothing to do for the write case. */
    268 	if (uio->uio_rw == UIO_WRITE) {
    269 		uio->uio_resid = 0;
    270 		return 0;
    271 	}
    272 	/*
    273 	 * Read in page-by-page basis, caller will continue.
    274 	 * Cut appropriately for a single/last-iteration cases.
    275 	 */
    276 	len = MIN(iov->iov_len, PAGE_SIZE);
    277 	return uiomove(dev_zero_page, len, uio);
    278 }
    279 
    280 /*
    281  * mm_readwrite: general memory R/W function.
    282  */
    283 static int
    284 mm_readwrite(dev_t dev, struct uio *uio, int flags)
    285 {
    286 	struct iovec *iov;
    287 	int error;
    288 
    289 #ifdef __HAVE_MM_MD_READWRITE
    290 	/* If defined - there are extra MD cases. */
    291 	switch (minor(dev)) {
    292 	case DEV_MEM:
    293 	case DEV_KMEM:
    294 	case DEV_NULL:
    295 	case DEV_ZERO:
    296 #if defined(COMPAT_16) && defined(__arm)
    297 	case _DEV_ZERO_oARM:
    298 #endif
    299 		break;
    300 	default:
    301 		return mm_md_readwrite(dev, uio);
    302 	}
    303 #endif
    304 	error = 0;
    305 	while (uio->uio_resid > 0 && error == 0) {
    306 		iov = uio->uio_iov;
    307 		if (iov->iov_len == 0) {
    308 			/* Processed; next I/O vector. */
    309 			uio->uio_iov++;
    310 			uio->uio_iovcnt--;
    311 			KASSERT(uio->uio_iovcnt >= 0);
    312 			continue;
    313 		}
    314 		/* Helper functions will process in page-by-page basis. */
    315 		switch (minor(dev)) {
    316 		case DEV_MEM:
    317 			error = dev_mem_readwrite(uio, iov);
    318 			break;
    319 		case DEV_KMEM:
    320 			error = dev_kmem_readwrite(uio, iov);
    321 			break;
    322 		case DEV_NULL:
    323 			if (uio->uio_rw == UIO_WRITE) {
    324 				uio->uio_resid = 0;
    325 			}
    326 			/* Break directly out of the loop. */
    327 			return 0;
    328 #if defined(COMPAT_16) && defined(__arm)
    329 		case _DEV_ZERO_oARM:
    330 #endif
    331 		case DEV_ZERO:
    332 			error = dev_zero_readwrite(uio, iov);
    333 			break;
    334 		default:
    335 			error = ENXIO;
    336 			break;
    337 		}
    338 	}
    339 	return error;
    340 }
    341 
    342 /*
    343  * mm_mmap: general mmap() handler.
    344  */
    345 static paddr_t
    346 mm_mmap(dev_t dev, off_t off, int acc)
    347 {
    348 	vm_prot_t prot;
    349 
    350 #ifdef __HAVE_MM_MD_MMAP
    351 	/* If defined - there are extra mmap() MD cases. */
    352 	switch (minor(dev)) {
    353 	case DEV_MEM:
    354 	case DEV_KMEM:
    355 	case DEV_NULL:
    356 #if defined(COMPAT_16) && defined(__arm)
    357 	case _DEV_ZERO_oARM:
    358 #endif
    359 	case DEV_ZERO:
    360 		break;
    361 	default:
    362 		return mm_md_mmap(dev, off, acc);
    363 	}
    364 #endif
    365 	/*
    366 	 * /dev/null does not make sense, /dev/kmem is volatile and
    367 	 * /dev/zero is handled in mmap already.
    368 	 */
    369 	if (minor(dev) != DEV_MEM) {
    370 		return -1;
    371 	}
    372 
    373 	prot = 0;
    374 	if (acc & PROT_EXEC)
    375 		prot |= VM_PROT_EXECUTE;
    376 	if (acc & PROT_READ)
    377 		prot |= VM_PROT_READ;
    378 	if (acc & PROT_WRITE)
    379 		prot |= VM_PROT_WRITE;
    380 
    381 	/* Validate the physical address. */
    382 	if (mm_md_physacc(off, prot) != 0) {
    383 		return -1;
    384 	}
    385 	return off >> PGSHIFT;
    386 }
    387 
    388 static int
    389 mm_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    390 {
    391 
    392 	switch (cmd) {
    393 	case FIONBIO:
    394 		/* We never block anyway. */
    395 		return 0;
    396 
    397 	case FIOSETOWN:
    398 	case FIOGETOWN:
    399 	case TIOCGPGRP:
    400 	case TIOCSPGRP:
    401 	case TIOCGETA:
    402 		return ENOTTY;
    403 
    404 	case FIOASYNC:
    405 		if ((*(int *)data) == 0) {
    406 			return 0;
    407 		}
    408 		/* FALLTHROUGH */
    409 	default:
    410 		return EOPNOTSUPP;
    411 	}
    412 }
    413