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