1 /* $OpenBSD: mem.c,v 1.10 2024/12/30 02:46:00 guenther Exp $ */ 2 3 /* 4 * Copyright (c) 1988 University of Utah. 5 * Copyright (c) 1982, 1986, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)mem.c 8.3 (Berkeley) 1/12/94 37 */ 38 39 /* 40 * Memory special file 41 */ 42 43 #include <sys/param.h> 44 #include <sys/buf.h> 45 #include <sys/filio.h> 46 #include <sys/systm.h> 47 #include <sys/uio.h> 48 #include <sys/malloc.h> 49 #include <sys/atomic.h> 50 51 #include <machine/conf.h> 52 53 #include <uvm/uvm_extern.h> 54 55 caddr_t zpage; 56 extern vaddr_t first_addr, last_addr; 57 extern caddr_t kernelstart; 58 extern void *etext; 59 60 int 61 mmopen(dev_t dev, int flag, int mode, struct proc *p) 62 { 63 extern int allowkmem; 64 65 switch (minor(dev)) { 66 case 0: 67 case 1: 68 if ((int)atomic_load_int(&securelevel) <= 0 || 69 atomic_load_int(&allowkmem)) 70 break; 71 return (EPERM); 72 case 2: 73 case 12: 74 break; 75 default: 76 return (ENXIO); 77 } 78 return (0); 79 } 80 81 int 82 mmclose(dev_t dev, int flag, int mode, struct proc *p) 83 { 84 85 return (0); 86 } 87 88 int 89 mmrw(dev_t dev, struct uio *uio, int flags) 90 { 91 vaddr_t v; 92 size_t c; 93 struct iovec *iov; 94 int error = 0; 95 96 while (uio->uio_resid > 0 && error == 0) { 97 iov = uio->uio_iov; 98 if (iov->iov_len == 0) { 99 uio->uio_iov++; 100 uio->uio_iovcnt--; 101 if (uio->uio_iovcnt < 0) 102 panic("mmrw"); 103 continue; 104 } 105 switch (minor(dev)) { 106 107 /* minor device 0 is physical memory */ 108 case 0: 109 v = uio->uio_offset; 110 error = uiomove((caddr_t)v, uio->uio_resid, uio); 111 continue; 112 113 /* minor device 1 is kernel memory */ 114 case 1: 115 v = uio->uio_offset; 116 c = ulmin(iov->iov_len, MAXPHYS); 117 if (v >= (vaddr_t)&kernelstart && 118 v < (vaddr_t)first_addr) { 119 if (v < (vaddr_t)etext && 120 uio->uio_rw == UIO_WRITE) 121 return (EFAULT); 122 } else if (!uvm_kernacc((caddr_t)v, c, 123 uio->uio_rw == UIO_READ ? B_READ : B_WRITE)) 124 return (EFAULT); 125 error = uiomove((caddr_t)v, c, uio); 126 continue; 127 128 /* minor device 2 is /dev/null */ 129 case 2: 130 if (uio->uio_rw == UIO_WRITE) 131 uio->uio_resid = 0; 132 return (0); 133 134 /* minor device 12 is /dev/zero */ 135 case 12: 136 if (uio->uio_rw == UIO_WRITE) { 137 c = iov->iov_len; 138 break; 139 } 140 if (zpage == NULL) 141 zpage = malloc(PAGE_SIZE, M_TEMP, 142 M_WAITOK | M_ZERO); 143 c = ulmin(iov->iov_len, PAGE_SIZE); 144 error = uiomove(zpage, c, uio); 145 continue; 146 147 default: 148 return (ENXIO); 149 } 150 if (error) 151 break; 152 iov->iov_base += c; 153 iov->iov_len -= c; 154 uio->uio_offset += c; 155 uio->uio_resid -= c; 156 } 157 158 return (error); 159 } 160 161 paddr_t 162 mmmmap(dev_t dev, off_t off, int prot) 163 { 164 return (-1); 165 } 166 167 int 168 mmioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p) 169 { 170 switch (cmd) { 171 case FIOASYNC: 172 /* handled by fd layer */ 173 return 0; 174 } 175 176 return (ENOTTY); 177 } 178