1 /*- 2 * Copyright (c) 1988 University of Utah. 3 * Copyright (c) 1982, 1986, 1990 The Regents of the University of California. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * the Systems Programming Group of the University of Utah Computer 8 * Science Department, and code derived from software contributed to 9 * Berkeley by William Jolitz. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * from: Utah $Hdr: mem.c 1.13 89/10/08$ 40 * from: @(#)mem.c 7.2 (Berkeley) 5/9/91 41 * $FreeBSD$ 42 */ 43 44 /* 45 * Memory special file 46 */ 47 48 #include <sys/param.h> 49 #include <sys/conf.h> 50 #include <sys/fcntl.h> 51 #include <sys/ioccom.h> 52 #include <sys/kernel.h> 53 #include <sys/lock.h> 54 #include <sys/malloc.h> 55 #include <sys/memrange.h> 56 #include <sys/mutex.h> 57 #include <sys/proc.h> 58 #include <sys/signalvar.h> 59 #include <sys/systm.h> 60 #include <sys/uio.h> 61 62 #include <machine/db_machdep.h> 63 #include <machine/frame.h> 64 #include <machine/psl.h> 65 #include <machine/specialreg.h> 66 #include <machine/vmparam.h> 67 68 #include <vm/vm.h> 69 #include <vm/pmap.h> 70 #include <vm/vm_extern.h> 71 72 static dev_t memdev, kmemdev, iodev; 73 74 static d_open_t mmopen; 75 static d_close_t mmclose; 76 static d_read_t mmrw; 77 static d_ioctl_t mmioctl; 78 static d_mmap_t memmmap; 79 80 #define CDEV_MAJOR 2 81 static struct cdevsw mem_cdevsw = { 82 .d_open = mmopen, 83 .d_close = mmclose, 84 .d_read = mmrw, 85 .d_write = mmrw, 86 .d_ioctl = mmioctl, 87 .d_mmap = memmmap, 88 .d_name = "mem", 89 .d_maj = CDEV_MAJOR, 90 .d_flags = D_MEM, 91 }; 92 93 MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors"); 94 95 struct mem_range_softc mem_range_softc; 96 97 static int 98 mmclose(dev_t dev, int flags, int fmt, struct thread *td) 99 { 100 switch (minor(dev)) { 101 case 14: 102 td->td_frame->tf_rflags &= ~PSL_IOPL; 103 } 104 return (0); 105 } 106 107 static int 108 mmopen(dev_t dev, int flags, int fmt, struct thread *td) 109 { 110 int error; 111 112 switch (minor(dev)) { 113 case 0: 114 case 1: 115 if (flags & FWRITE) { 116 error = securelevel_gt(td->td_ucred, 0); 117 if (error != 0) 118 return (error); 119 } 120 break; 121 case 14: 122 error = suser(td); 123 if (error != 0) 124 return (error); 125 error = securelevel_gt(td->td_ucred, 0); 126 if (error != 0) 127 return (error); 128 td->td_frame->tf_rflags |= PSL_IOPL; 129 break; 130 } 131 return (0); 132 } 133 134 /*ARGSUSED*/ 135 static int 136 mmrw(dev_t dev, struct uio *uio, int flags) 137 { 138 int o; 139 u_long c = 0, v; 140 struct iovec *iov; 141 int error = 0; 142 vm_offset_t addr, eaddr; 143 144 GIANT_REQUIRED; 145 146 while (uio->uio_resid > 0 && error == 0) { 147 iov = uio->uio_iov; 148 if (iov->iov_len == 0) { 149 uio->uio_iov++; 150 uio->uio_iovcnt--; 151 if (uio->uio_iovcnt < 0) 152 panic("mmrw"); 153 continue; 154 } 155 switch (minor(dev)) { 156 157 /* minor device 0 is physical memory */ 158 case 0: 159 v = uio->uio_offset; 160 kmemphys: 161 o = v & PAGE_MASK; 162 c = min(uio->uio_resid, (u_int)(PAGE_SIZE - o)); 163 error = uiomove((void *)PHYS_TO_DMAP(v), (int)c, uio); 164 continue; 165 166 /* minor device 1 is kernel memory */ 167 case 1: 168 v = uio->uio_offset; 169 170 if (v >= DMAP_MIN_ADDRESS && v < DMAP_MAX_ADDRESS) { 171 v = DMAP_TO_PHYS(v); 172 goto kmemphys; 173 } 174 175 c = iov->iov_len; 176 /* 177 * Make sure that all of the pages are currently resident so 178 * that we don't create any zero-fill pages. 179 */ 180 addr = trunc_page(v); 181 eaddr = round_page(v + c); 182 183 if (addr < (vm_offset_t)KERNBASE) 184 return (EFAULT); 185 for (; addr < eaddr; addr += PAGE_SIZE) 186 if (pmap_extract(kernel_pmap, addr) == 0) 187 return (EFAULT); 188 189 if (!kernacc((caddr_t)(long)v, c, 190 uio->uio_rw == UIO_READ ? 191 VM_PROT_READ : VM_PROT_WRITE)) 192 return (EFAULT); 193 194 error = uiomove((caddr_t)(long)v, (int)c, uio); 195 continue; 196 197 default: 198 return (ENODEV); 199 } 200 201 if (error) 202 break; 203 iov->iov_base = (char *)iov->iov_base + c; 204 iov->iov_len -= c; 205 uio->uio_offset += c; 206 uio->uio_resid -= c; 207 } 208 return (error); 209 } 210 211 /*******************************************************\ 212 * allow user processes to MMAP some memory sections * 213 * instead of going through read/write * 214 \*******************************************************/ 215 static int 216 memmmap(dev_t dev, vm_offset_t offset, vm_paddr_t *paddr, int prot) 217 { 218 switch (minor(dev)) 219 { 220 221 /* minor device 0 is physical memory */ 222 case 0: 223 *paddr = offset; 224 break; 225 226 /* minor device 1 is kernel memory */ 227 case 1: 228 *paddr = vtophys(offset); 229 break; 230 231 default: 232 return (-1); 233 } 234 return (0); 235 } 236 237 /* 238 * Operations for changing memory attributes. 239 * 240 * This is basically just an ioctl shim for mem_range_attr_get 241 * and mem_range_attr_set. 242 */ 243 static int 244 mmioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td) 245 { 246 int nd, error = 0; 247 struct mem_range_op *mo = (struct mem_range_op *)data; 248 struct mem_range_desc *md; 249 250 /* is this for us? */ 251 if ((cmd != MEMRANGE_GET) && 252 (cmd != MEMRANGE_SET)) 253 return (ENOTTY); 254 255 /* any chance we can handle this? */ 256 if (mem_range_softc.mr_op == NULL) 257 return (EOPNOTSUPP); 258 259 /* do we have any descriptors? */ 260 if (mem_range_softc.mr_ndesc == 0) 261 return (ENXIO); 262 263 switch (cmd) { 264 case MEMRANGE_GET: 265 nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc); 266 if (nd > 0) { 267 md = (struct mem_range_desc *) 268 malloc(nd * sizeof(struct mem_range_desc), 269 M_MEMDESC, M_WAITOK); 270 error = mem_range_attr_get(md, &nd); 271 if (!error) 272 error = copyout(md, mo->mo_desc, 273 nd * sizeof(struct mem_range_desc)); 274 free(md, M_MEMDESC); 275 } 276 else 277 nd = mem_range_softc.mr_ndesc; 278 mo->mo_arg[0] = nd; 279 break; 280 281 case MEMRANGE_SET: 282 md = (struct mem_range_desc *)malloc(sizeof(struct mem_range_desc), 283 M_MEMDESC, M_WAITOK); 284 error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc)); 285 /* clamp description string */ 286 md->mr_owner[sizeof(md->mr_owner) - 1] = 0; 287 if (error == 0) 288 error = mem_range_attr_set(md, &mo->mo_arg[0]); 289 free(md, M_MEMDESC); 290 break; 291 } 292 return (error); 293 } 294 295 /* 296 * Implementation-neutral, kernel-callable functions for manipulating 297 * memory range attributes. 298 */ 299 int 300 mem_range_attr_get(struct mem_range_desc *mrd, int *arg) 301 { 302 /* can we handle this? */ 303 if (mem_range_softc.mr_op == NULL) 304 return (EOPNOTSUPP); 305 306 if (*arg == 0) 307 *arg = mem_range_softc.mr_ndesc; 308 else 309 bcopy(mem_range_softc.mr_desc, mrd, 310 (*arg) * sizeof(struct mem_range_desc)); 311 return (0); 312 } 313 314 int 315 mem_range_attr_set(struct mem_range_desc *mrd, int *arg) 316 { 317 /* can we handle this? */ 318 if (mem_range_softc.mr_op == NULL) 319 return (EOPNOTSUPP); 320 321 return (mem_range_softc.mr_op->set(&mem_range_softc, mrd, arg)); 322 } 323 324 static int 325 mem_modevent(module_t mod, int type, void *data) 326 { 327 switch(type) { 328 case MOD_LOAD: 329 if (bootverbose) 330 printf("mem: <memory & I/O>\n"); 331 /* Initialise memory range handling */ 332 if (mem_range_softc.mr_op != NULL) 333 mem_range_softc.mr_op->init(&mem_range_softc); 334 335 memdev = make_dev(&mem_cdevsw, 0, UID_ROOT, GID_KMEM, 336 0640, "mem"); 337 kmemdev = make_dev(&mem_cdevsw, 1, UID_ROOT, GID_KMEM, 338 0640, "kmem"); 339 iodev = make_dev(&mem_cdevsw, 14, UID_ROOT, GID_WHEEL, 340 0600, "io"); 341 return (0); 342 343 case MOD_UNLOAD: 344 destroy_dev(memdev); 345 destroy_dev(kmemdev); 346 destroy_dev(iodev); 347 return (0); 348 349 case MOD_SHUTDOWN: 350 return (0); 351 352 default: 353 return (EOPNOTSUPP); 354 } 355 } 356 357 DEV_MODULE(mem, mem_modevent, NULL); 358