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