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