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/systm.h> 50 #include <sys/conf.h> 51 #include <sys/buf.h> 52 #include <sys/kernel.h> 53 #include <sys/uio.h> 54 #include <sys/ioccom.h> 55 #include <sys/malloc.h> 56 #include <sys/memrange.h> 57 #include <sys/proc.h> 58 #include <sys/signalvar.h> 59 60 #include <machine/frame.h> 61 #include <machine/random.h> 62 #include <machine/psl.h> 63 #include <machine/specialreg.h> 64 #include <i386/isa/intr_machdep.h> 65 66 #include <vm/vm.h> 67 #include <vm/pmap.h> 68 #include <vm/vm_extern.h> 69 70 71 static d_open_t mmopen; 72 static d_close_t mmclose; 73 static d_read_t mmrw; 74 static d_ioctl_t mmioctl; 75 static d_mmap_t memmmap; 76 static d_poll_t mmpoll; 77 78 #define CDEV_MAJOR 2 79 static struct cdevsw mem_cdevsw = { 80 /* open */ mmopen, 81 /* close */ mmclose, 82 /* read */ mmrw, 83 /* write */ mmrw, 84 /* ioctl */ mmioctl, 85 /* poll */ mmpoll, 86 /* mmap */ memmmap, 87 /* strategy */ nostrategy, 88 /* name */ "mem", 89 /* maj */ CDEV_MAJOR, 90 /* dump */ nodump, 91 /* psize */ nopsize, 92 /* flags */ D_MEM, 93 /* bmaj */ -1 94 }; 95 96 static struct random_softc random_softc[16]; 97 static caddr_t zbuf; 98 99 MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors"); 100 static int mem_ioctl __P((dev_t, u_long, caddr_t, int, struct proc *)); 101 static int random_ioctl __P((dev_t, u_long, caddr_t, int, struct proc *)); 102 103 struct mem_range_softc mem_range_softc; 104 105 106 static int 107 mmclose(dev, flags, fmt, p) 108 dev_t dev; 109 int flags; 110 int fmt; 111 struct proc *p; 112 { 113 switch (minor(dev)) { 114 case 14: 115 curproc->p_md.md_regs->tf_eflags &= ~PSL_IOPL; 116 break; 117 default: 118 break; 119 } 120 return (0); 121 } 122 123 static int 124 mmopen(dev, flags, fmt, p) 125 dev_t dev; 126 int flags; 127 int fmt; 128 struct proc *p; 129 { 130 int error; 131 132 switch (minor(dev)) { 133 case 14: 134 error = suser(p); 135 if (error != 0) 136 return (error); 137 if (securelevel > 0) 138 return (EPERM); 139 curproc->p_md.md_regs->tf_eflags |= PSL_IOPL; 140 break; 141 default: 142 break; 143 } 144 return (0); 145 } 146 147 static int 148 mmrw(dev, uio, flags) 149 dev_t dev; 150 struct uio *uio; 151 int flags; 152 { 153 register int o; 154 register u_int c, v; 155 u_int poolsize; 156 register struct iovec *iov; 157 int error = 0; 158 caddr_t buf = NULL; 159 160 while (uio->uio_resid > 0 && error == 0) { 161 iov = uio->uio_iov; 162 if (iov->iov_len == 0) { 163 uio->uio_iov++; 164 uio->uio_iovcnt--; 165 if (uio->uio_iovcnt < 0) 166 panic("mmrw"); 167 continue; 168 } 169 switch (minor(dev)) { 170 171 /* minor device 0 is physical memory */ 172 case 0: 173 v = uio->uio_offset; 174 pmap_enter(kernel_pmap, (vm_offset_t)ptvmmap, v, 175 uio->uio_rw == UIO_READ ? VM_PROT_READ : VM_PROT_WRITE, 176 TRUE); 177 o = (int)uio->uio_offset & PAGE_MASK; 178 c = (u_int)(PAGE_SIZE - ((int)iov->iov_base & PAGE_MASK)); 179 c = min(c, (u_int)(PAGE_SIZE - o)); 180 c = min(c, (u_int)iov->iov_len); 181 error = uiomove((caddr_t)&ptvmmap[o], (int)c, uio); 182 pmap_remove(kernel_pmap, (vm_offset_t)ptvmmap, 183 (vm_offset_t)&ptvmmap[PAGE_SIZE]); 184 continue; 185 186 /* minor device 1 is kernel memory */ 187 case 1: { 188 vm_offset_t addr, eaddr; 189 c = iov->iov_len; 190 191 /* 192 * Make sure that all of the pages are currently resident so 193 * that we don't create any zero-fill pages. 194 */ 195 addr = trunc_page(uio->uio_offset); 196 eaddr = round_page(uio->uio_offset + c); 197 198 if (addr < (vm_offset_t)VADDR(PTDPTDI, 0)) 199 return EFAULT; 200 if (eaddr >= (vm_offset_t)VADDR(APTDPTDI, 0)) 201 return EFAULT; 202 for (; addr < eaddr; addr += PAGE_SIZE) 203 if (pmap_extract(kernel_pmap, addr) == 0) 204 return EFAULT; 205 206 if (!kernacc((caddr_t)(int)uio->uio_offset, c, 207 uio->uio_rw == UIO_READ ? 208 VM_PROT_READ : VM_PROT_WRITE)) 209 return (EFAULT); 210 error = uiomove((caddr_t)(int)uio->uio_offset, (int)c, uio); 211 continue; 212 } 213 214 /* minor device 2 is EOF/RATHOLE */ 215 case 2: 216 if (uio->uio_rw == UIO_READ) 217 return (0); 218 c = iov->iov_len; 219 break; 220 221 /* minor device 3 (/dev/random) is source of filth on read, rathole on write */ 222 case 3: 223 if (uio->uio_rw == UIO_WRITE) { 224 c = iov->iov_len; 225 break; 226 } 227 if (buf == NULL) 228 buf = (caddr_t) 229 malloc(PAGE_SIZE, M_TEMP, M_WAITOK); 230 c = min(iov->iov_len, PAGE_SIZE); 231 poolsize = read_random(buf, c); 232 if (poolsize == 0) { 233 if (buf) 234 free(buf, M_TEMP); 235 return (0); 236 } 237 c = min(c, poolsize); 238 error = uiomove(buf, (int)c, uio); 239 continue; 240 241 /* minor device 4 (/dev/urandom) is source of muck on read, rathole on write */ 242 case 4: 243 if (uio->uio_rw == UIO_WRITE) { 244 c = iov->iov_len; 245 break; 246 } 247 if (CURSIG(curproc) != 0) { 248 /* 249 * Use tsleep() to get the error code right. 250 * It should return immediately. 251 */ 252 error = tsleep(&random_softc[0], 253 PZERO | PCATCH, "urand", 1); 254 if (error != 0 && error != EWOULDBLOCK) 255 continue; 256 } 257 if (buf == NULL) 258 buf = (caddr_t) 259 malloc(PAGE_SIZE, M_TEMP, M_WAITOK); 260 c = min(iov->iov_len, PAGE_SIZE); 261 poolsize = read_random_unlimited(buf, c); 262 c = min(c, poolsize); 263 error = uiomove(buf, (int)c, uio); 264 continue; 265 266 /* minor device 12 (/dev/zero) is source of nulls on read, rathole on write */ 267 case 12: 268 if (uio->uio_rw == UIO_WRITE) { 269 c = iov->iov_len; 270 break; 271 } 272 if (zbuf == NULL) { 273 zbuf = (caddr_t) 274 malloc(PAGE_SIZE, M_TEMP, M_WAITOK); 275 bzero(zbuf, PAGE_SIZE); 276 } 277 c = min(iov->iov_len, PAGE_SIZE); 278 error = uiomove(zbuf, (int)c, uio); 279 continue; 280 281 default: 282 return (ENXIO); 283 } 284 if (error) 285 break; 286 iov->iov_base += c; 287 iov->iov_len -= c; 288 uio->uio_offset += c; 289 uio->uio_resid -= c; 290 } 291 if (buf) 292 free(buf, M_TEMP); 293 return (error); 294 } 295 296 297 298 299 /*******************************************************\ 300 * allow user processes to MMAP some memory sections * 301 * instead of going through read/write * 302 \*******************************************************/ 303 static int 304 memmmap(dev_t dev, vm_offset_t offset, int nprot) 305 { 306 switch (minor(dev)) 307 { 308 309 /* minor device 0 is physical memory */ 310 case 0: 311 return i386_btop(offset); 312 313 /* minor device 1 is kernel memory */ 314 case 1: 315 return i386_btop(vtophys(offset)); 316 317 default: 318 return -1; 319 } 320 } 321 322 static int 323 mmioctl(dev, cmd, data, flags, p) 324 dev_t dev; 325 u_long cmd; 326 caddr_t data; 327 int flags; 328 struct proc *p; 329 { 330 331 switch (minor(dev)) { 332 case 0: 333 return mem_ioctl(dev, cmd, data, flags, p); 334 case 3: 335 case 4: 336 return random_ioctl(dev, cmd, data, flags, p); 337 } 338 return (ENODEV); 339 } 340 341 /* 342 * Operations for changing memory attributes. 343 * 344 * This is basically just an ioctl shim for mem_range_attr_get 345 * and mem_range_attr_set. 346 */ 347 static int 348 mem_ioctl(dev, cmd, data, flags, p) 349 dev_t dev; 350 u_long cmd; 351 caddr_t data; 352 int flags; 353 struct proc *p; 354 { 355 int nd, error = 0; 356 struct mem_range_op *mo = (struct mem_range_op *)data; 357 struct mem_range_desc *md; 358 359 /* is this for us? */ 360 if ((cmd != MEMRANGE_GET) && 361 (cmd != MEMRANGE_SET)) 362 return (ENOTTY); 363 364 /* any chance we can handle this? */ 365 if (mem_range_softc.mr_op == NULL) 366 return (EOPNOTSUPP); 367 368 /* do we have any descriptors? */ 369 if (mem_range_softc.mr_ndesc == 0) 370 return (ENXIO); 371 372 switch (cmd) { 373 case MEMRANGE_GET: 374 nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc); 375 if (nd > 0) { 376 md = (struct mem_range_desc *) 377 malloc(nd * sizeof(struct mem_range_desc), 378 M_MEMDESC, M_WAITOK); 379 error = mem_range_attr_get(md, &nd); 380 if (!error) 381 error = copyout(md, mo->mo_desc, 382 nd * sizeof(struct mem_range_desc)); 383 free(md, M_MEMDESC); 384 } else { 385 nd = mem_range_softc.mr_ndesc; 386 } 387 mo->mo_arg[0] = nd; 388 break; 389 390 case MEMRANGE_SET: 391 md = (struct mem_range_desc *)malloc(sizeof(struct mem_range_desc), 392 M_MEMDESC, M_WAITOK); 393 error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc)); 394 /* clamp description string */ 395 md->mr_owner[sizeof(md->mr_owner) - 1] = 0; 396 if (error == 0) 397 error = mem_range_attr_set(md, &mo->mo_arg[0]); 398 free(md, M_MEMDESC); 399 break; 400 } 401 return (error); 402 } 403 404 /* 405 * Implementation-neutral, kernel-callable functions for manipulating 406 * memory range attributes. 407 */ 408 int 409 mem_range_attr_get(mrd, arg) 410 struct mem_range_desc *mrd; 411 int *arg; 412 { 413 /* can we handle this? */ 414 if (mem_range_softc.mr_op == NULL) 415 return (EOPNOTSUPP); 416 417 if (*arg == 0) { 418 *arg = mem_range_softc.mr_ndesc; 419 } else { 420 bcopy(mem_range_softc.mr_desc, mrd, (*arg) * sizeof(struct mem_range_desc)); 421 } 422 return (0); 423 } 424 425 int 426 mem_range_attr_set(mrd, arg) 427 struct mem_range_desc *mrd; 428 int *arg; 429 { 430 /* can we handle this? */ 431 if (mem_range_softc.mr_op == NULL) 432 return (EOPNOTSUPP); 433 434 return (mem_range_softc.mr_op->set(&mem_range_softc, mrd, arg)); 435 } 436 437 #ifdef SMP 438 void 439 mem_range_AP_init(void) 440 { 441 if (mem_range_softc.mr_op && mem_range_softc.mr_op->initAP) 442 return (mem_range_softc.mr_op->initAP(&mem_range_softc)); 443 } 444 #endif 445 446 static int 447 random_ioctl(dev, cmd, data, flags, p) 448 dev_t dev; 449 u_long cmd; 450 caddr_t data; 451 int flags; 452 struct proc *p; 453 { 454 static intrmask_t interrupt_allowed; 455 intrmask_t interrupt_mask; 456 int error, intr; 457 struct random_softc *sc; 458 459 /* 460 * We're the random or urandom device. The only ioctls are for 461 * selecting and inspecting which interrupts are used in the muck 462 * gathering business. 463 */ 464 if (cmd != MEM_SETIRQ && cmd != MEM_CLEARIRQ && cmd != MEM_RETURNIRQ) 465 return (ENOTTY); 466 467 /* 468 * Even inspecting the state is privileged, since it gives a hint 469 * about how easily the randomness might be guessed. 470 */ 471 error = suser(p); 472 if (error != 0) 473 return (error); 474 475 /* 476 * XXX the data is 16-bit due to a historical botch, so we use 477 * magic 16's instead of ICU_LEN and can't support 24 interrupts 478 * under SMP. 479 */ 480 intr = *(int16_t *)data; 481 if (cmd != MEM_RETURNIRQ && (intr < 0 || intr >= 16)) 482 return (EINVAL); 483 484 interrupt_mask = 1 << intr; 485 sc = &random_softc[intr]; 486 switch (cmd) { 487 case MEM_SETIRQ: 488 if (interrupt_allowed & interrupt_mask) 489 break; 490 interrupt_allowed |= interrupt_mask; 491 sc->sc_intr = intr; 492 disable_intr(); 493 sc->sc_handler = intr_handler[intr]; 494 intr_handler[intr] = add_interrupt_randomness; 495 sc->sc_arg = intr_unit[intr]; 496 intr_unit[intr] = sc; 497 enable_intr(); 498 break; 499 case MEM_CLEARIRQ: 500 if (!(interrupt_allowed & interrupt_mask)) 501 break; 502 interrupt_allowed &= ~interrupt_mask; 503 disable_intr(); 504 intr_handler[intr] = sc->sc_handler; 505 intr_unit[intr] = sc->sc_arg; 506 enable_intr(); 507 break; 508 case MEM_RETURNIRQ: 509 *(u_int16_t *)data = interrupt_allowed; 510 break; 511 } 512 return (0); 513 } 514 515 int 516 mmpoll(dev, events, p) 517 dev_t dev; 518 int events; 519 struct proc *p; 520 { 521 switch (minor(dev)) { 522 case 3: /* /dev/random */ 523 return random_poll(dev, events, p); 524 case 4: /* /dev/urandom */ 525 default: 526 return seltrue(dev, events, p); 527 } 528 } 529 530 /* 531 * Routine that identifies /dev/mem and /dev/kmem. 532 * 533 * A minimal stub routine can always return 0. 534 */ 535 int 536 iskmemdev(dev) 537 dev_t dev; 538 { 539 540 return ((major(dev) == mem_cdevsw.d_maj) 541 && (minor(dev) == 0 || minor(dev) == 1)); 542 } 543 544 int 545 iszerodev(dev) 546 dev_t dev; 547 { 548 return ((major(dev) == mem_cdevsw.d_maj) 549 && minor(dev) == 12); 550 } 551 552 static void 553 mem_drvinit(void *unused) 554 { 555 556 /* Initialise memory range handling */ 557 if (mem_range_softc.mr_op != NULL) 558 mem_range_softc.mr_op->init(&mem_range_softc); 559 560 make_dev(&mem_cdevsw, 0, UID_ROOT, GID_KMEM, 0640, "mem"); 561 make_dev(&mem_cdevsw, 1, UID_ROOT, GID_KMEM, 0640, "kmem"); 562 make_dev(&mem_cdevsw, 2, UID_ROOT, GID_WHEEL, 0666, "null"); 563 make_dev(&mem_cdevsw, 3, UID_ROOT, GID_WHEEL, 0644, "random"); 564 make_dev(&mem_cdevsw, 4, UID_ROOT, GID_WHEEL, 0644, "urandom"); 565 make_dev(&mem_cdevsw, 12, UID_ROOT, GID_WHEEL, 0666, "zero"); 566 make_dev(&mem_cdevsw, 14, UID_ROOT, GID_WHEEL, 0600, "io"); 567 } 568 569 SYSINIT(memdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,mem_drvinit,NULL) 570 571