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