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: src/sys/i386/i386/mem.c,v 1.79.2.9 2003/01/04 22:58:01 njl Exp $ 42 * $DragonFly: src/sys/kern/kern_memio.c,v 1.32 2008/07/23 16:39:28 dillon Exp $ 43 */ 44 45 /* 46 * Memory special file 47 */ 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/buf.h> 52 #include <sys/conf.h> 53 #include <sys/fcntl.h> 54 #include <sys/filio.h> 55 #include <sys/kernel.h> 56 #include <sys/malloc.h> 57 #include <sys/memrange.h> 58 #include <sys/proc.h> 59 #include <sys/priv.h> 60 #include <sys/random.h> 61 #include <sys/signalvar.h> 62 #include <sys/signal2.h> 63 #include <sys/uio.h> 64 #include <sys/vnode.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 mmread; 74 static d_write_t mmwrite; 75 static d_ioctl_t mmioctl; 76 static d_mmap_t memmmap; 77 static d_kqfilter_t mmkqfilter; 78 79 #define CDEV_MAJOR 2 80 static struct dev_ops mem_ops = { 81 { "mem", CDEV_MAJOR, D_MEM | D_MPSAFE_READ | D_MPSAFE_WRITE | D_KQFILTER }, 82 .d_open = mmopen, 83 .d_close = mmclose, 84 .d_read = mmread, 85 .d_write = mmwrite, 86 .d_ioctl = mmioctl, 87 .d_kqfilter = mmkqfilter, 88 .d_mmap = memmmap, 89 }; 90 91 static int rand_bolt; 92 static caddr_t zbuf; 93 static cdev_t zerodev = NULL; 94 95 MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors"); 96 static int mem_ioctl (cdev_t, u_long, caddr_t, int, struct ucred *); 97 static int random_ioctl (cdev_t, u_long, caddr_t, int, struct ucred *); 98 99 struct mem_range_softc mem_range_softc; 100 101 102 static int 103 mmopen(struct dev_open_args *ap) 104 { 105 cdev_t dev = ap->a_head.a_dev; 106 int error; 107 108 switch (minor(dev)) { 109 case 0: 110 case 1: 111 if (ap->a_oflags & FWRITE) { 112 if (securelevel > 0 || kernel_mem_readonly) 113 return (EPERM); 114 } 115 error = 0; 116 break; 117 case 14: 118 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0); 119 if (error != 0) 120 break; 121 if (securelevel > 0 || kernel_mem_readonly) { 122 error = EPERM; 123 break; 124 } 125 error = cpu_set_iopl(); 126 break; 127 default: 128 error = 0; 129 break; 130 } 131 return (error); 132 } 133 134 static int 135 mmclose(struct dev_close_args *ap) 136 { 137 cdev_t dev = ap->a_head.a_dev; 138 int error; 139 140 switch (minor(dev)) { 141 case 14: 142 error = cpu_clr_iopl(); 143 break; 144 default: 145 error = 0; 146 break; 147 } 148 return (error); 149 } 150 151 152 static int 153 mmrw(cdev_t dev, struct uio *uio, int flags) 154 { 155 int o; 156 u_int c, v; 157 u_int poolsize; 158 struct iovec *iov; 159 int error = 0; 160 caddr_t buf = NULL; 161 162 while (uio->uio_resid > 0 && error == 0) { 163 iov = uio->uio_iov; 164 if (iov->iov_len == 0) { 165 uio->uio_iov++; 166 uio->uio_iovcnt--; 167 if (uio->uio_iovcnt < 0) 168 panic("mmrw"); 169 continue; 170 } 171 switch (minor(dev)) { 172 case 0: 173 /* 174 * minor device 0 is physical memory, /dev/mem 175 */ 176 v = uio->uio_offset; 177 v &= ~PAGE_MASK; 178 pmap_kenter((vm_offset_t)ptvmmap, v); 179 o = (int)uio->uio_offset & PAGE_MASK; 180 c = (u_int)(PAGE_SIZE - ((uintptr_t)iov->iov_base & PAGE_MASK)); 181 c = min(c, (u_int)(PAGE_SIZE - o)); 182 c = min(c, (u_int)iov->iov_len); 183 error = uiomove((caddr_t)&ptvmmap[o], (int)c, uio); 184 pmap_kremove((vm_offset_t)ptvmmap); 185 continue; 186 187 case 1: { 188 /* 189 * minor device 1 is kernel memory, /dev/kmem 190 */ 191 vm_offset_t saddr, eaddr; 192 int prot; 193 194 c = iov->iov_len; 195 196 /* 197 * Make sure that all of the pages are currently 198 * resident so that we don't create any zero-fill 199 * pages. 200 */ 201 saddr = trunc_page(uio->uio_offset); 202 eaddr = round_page(uio->uio_offset + c); 203 if (saddr > eaddr) 204 return EFAULT; 205 206 /* 207 * Make sure the kernel addresses are mapped. 208 * platform_direct_mapped() can be used to bypass 209 * default mapping via the page table (virtual kernels 210 * contain a lot of out-of-band data). 211 */ 212 prot = VM_PROT_READ; 213 if (uio->uio_rw != UIO_READ) 214 prot |= VM_PROT_WRITE; 215 error = kvm_access_check(saddr, eaddr, prot); 216 if (error) 217 return (error); 218 error = uiomove((caddr_t)(vm_offset_t)uio->uio_offset, 219 (int)c, uio); 220 continue; 221 } 222 case 2: 223 /* 224 * minor device 2 is EOF/RATHOLE 225 */ 226 if (uio->uio_rw == UIO_READ) 227 return (0); 228 c = iov->iov_len; 229 break; 230 case 3: 231 /* 232 * minor device 3 (/dev/random) is source of filth 233 * on read, seeder on write 234 */ 235 if (buf == NULL) 236 buf = kmalloc(PAGE_SIZE, M_TEMP, M_WAITOK); 237 c = min(iov->iov_len, PAGE_SIZE); 238 if (uio->uio_rw == UIO_WRITE) { 239 error = uiomove(buf, (int)c, uio); 240 if (error == 0) 241 error = add_buffer_randomness(buf, c); 242 } else { 243 poolsize = read_random(buf, c); 244 if (poolsize == 0) { 245 if (buf) 246 kfree(buf, M_TEMP); 247 if ((flags & IO_NDELAY) != 0) 248 return (EWOULDBLOCK); 249 return (0); 250 } 251 c = min(c, poolsize); 252 error = uiomove(buf, (int)c, uio); 253 } 254 continue; 255 case 4: 256 /* 257 * minor device 4 (/dev/urandom) is source of muck 258 * on read, writes are disallowed. 259 */ 260 c = min(iov->iov_len, PAGE_SIZE); 261 if (uio->uio_rw == UIO_WRITE) { 262 error = EPERM; 263 break; 264 } 265 if (CURSIG(curthread->td_lwp) != 0) { 266 /* 267 * Use tsleep() to get the error code right. 268 * It should return immediately. 269 */ 270 error = tsleep(&rand_bolt, PCATCH, "urand", 1); 271 if (error != 0 && error != EWOULDBLOCK) 272 continue; 273 } 274 if (buf == NULL) 275 buf = kmalloc(PAGE_SIZE, M_TEMP, M_WAITOK); 276 poolsize = read_random_unlimited(buf, c); 277 c = min(c, poolsize); 278 error = uiomove(buf, (int)c, uio); 279 continue; 280 case 12: 281 /* 282 * minor device 12 (/dev/zero) is source of nulls 283 * on read, write are disallowed. 284 */ 285 if (uio->uio_rw == UIO_WRITE) { 286 c = iov->iov_len; 287 break; 288 } 289 if (zbuf == NULL) { 290 zbuf = (caddr_t)kmalloc(PAGE_SIZE, M_TEMP, 291 M_WAITOK | M_ZERO); 292 } 293 c = min(iov->iov_len, PAGE_SIZE); 294 error = uiomove(zbuf, (int)c, uio); 295 continue; 296 default: 297 return (ENODEV); 298 } 299 if (error) 300 break; 301 iov->iov_base = (char *)iov->iov_base + c; 302 iov->iov_len -= c; 303 uio->uio_offset += c; 304 uio->uio_resid -= c; 305 } 306 if (buf) 307 kfree(buf, M_TEMP); 308 return (error); 309 } 310 311 static int 312 mmread(struct dev_read_args *ap) 313 { 314 return(mmrw(ap->a_head.a_dev, ap->a_uio, ap->a_ioflag)); 315 } 316 317 static int 318 mmwrite(struct dev_write_args *ap) 319 { 320 return(mmrw(ap->a_head.a_dev, ap->a_uio, ap->a_ioflag)); 321 } 322 323 324 325 326 327 /*******************************************************\ 328 * allow user processes to MMAP some memory sections * 329 * instead of going through read/write * 330 \*******************************************************/ 331 332 static int 333 memmmap(struct dev_mmap_args *ap) 334 { 335 cdev_t dev = ap->a_head.a_dev; 336 337 switch (minor(dev)) { 338 case 0: 339 /* 340 * minor device 0 is physical memory 341 */ 342 #if defined(__i386__) 343 ap->a_result = i386_btop(ap->a_offset); 344 #elif defined(__x86_64__) 345 ap->a_result = x86_64_btop(ap->a_offset); 346 #endif 347 return 0; 348 case 1: 349 /* 350 * minor device 1 is kernel memory 351 */ 352 #if defined(__i386__) 353 ap->a_result = i386_btop(vtophys(ap->a_offset)); 354 #elif defined(__x86_64__) 355 ap->a_result = x86_64_btop(vtophys(ap->a_offset)); 356 #endif 357 return 0; 358 359 default: 360 return EINVAL; 361 } 362 } 363 364 static int 365 mmioctl(struct dev_ioctl_args *ap) 366 { 367 cdev_t dev = ap->a_head.a_dev; 368 369 switch (minor(dev)) { 370 case 0: 371 return mem_ioctl(dev, ap->a_cmd, ap->a_data, 372 ap->a_fflag, ap->a_cred); 373 case 3: 374 case 4: 375 return random_ioctl(dev, ap->a_cmd, ap->a_data, 376 ap->a_fflag, ap->a_cred); 377 } 378 return (ENODEV); 379 } 380 381 /* 382 * Operations for changing memory attributes. 383 * 384 * This is basically just an ioctl shim for mem_range_attr_get 385 * and mem_range_attr_set. 386 */ 387 static int 388 mem_ioctl(cdev_t dev, u_long cmd, caddr_t data, int flags, struct ucred *cred) 389 { 390 int nd, error = 0; 391 struct mem_range_op *mo = (struct mem_range_op *)data; 392 struct mem_range_desc *md; 393 394 /* is this for us? */ 395 if ((cmd != MEMRANGE_GET) && 396 (cmd != MEMRANGE_SET)) 397 return (ENOTTY); 398 399 /* any chance we can handle this? */ 400 if (mem_range_softc.mr_op == NULL) 401 return (EOPNOTSUPP); 402 403 /* do we have any descriptors? */ 404 if (mem_range_softc.mr_ndesc == 0) 405 return (ENXIO); 406 407 switch (cmd) { 408 case MEMRANGE_GET: 409 nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc); 410 if (nd > 0) { 411 md = (struct mem_range_desc *) 412 kmalloc(nd * sizeof(struct mem_range_desc), 413 M_MEMDESC, M_WAITOK); 414 error = mem_range_attr_get(md, &nd); 415 if (!error) 416 error = copyout(md, mo->mo_desc, 417 nd * sizeof(struct mem_range_desc)); 418 kfree(md, M_MEMDESC); 419 } else { 420 nd = mem_range_softc.mr_ndesc; 421 } 422 mo->mo_arg[0] = nd; 423 break; 424 425 case MEMRANGE_SET: 426 md = (struct mem_range_desc *)kmalloc(sizeof(struct mem_range_desc), 427 M_MEMDESC, M_WAITOK); 428 error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc)); 429 /* clamp description string */ 430 md->mr_owner[sizeof(md->mr_owner) - 1] = 0; 431 if (error == 0) 432 error = mem_range_attr_set(md, &mo->mo_arg[0]); 433 kfree(md, M_MEMDESC); 434 break; 435 } 436 return (error); 437 } 438 439 /* 440 * Implementation-neutral, kernel-callable functions for manipulating 441 * memory range attributes. 442 */ 443 int 444 mem_range_attr_get(struct mem_range_desc *mrd, int *arg) 445 { 446 /* can we handle this? */ 447 if (mem_range_softc.mr_op == NULL) 448 return (EOPNOTSUPP); 449 450 if (*arg == 0) { 451 *arg = mem_range_softc.mr_ndesc; 452 } else { 453 bcopy(mem_range_softc.mr_desc, mrd, (*arg) * sizeof(struct mem_range_desc)); 454 } 455 return (0); 456 } 457 458 int 459 mem_range_attr_set(struct mem_range_desc *mrd, int *arg) 460 { 461 /* can we handle this? */ 462 if (mem_range_softc.mr_op == NULL) 463 return (EOPNOTSUPP); 464 465 return (mem_range_softc.mr_op->set(&mem_range_softc, mrd, arg)); 466 } 467 468 #ifdef SMP 469 void 470 mem_range_AP_init(void) 471 { 472 if (mem_range_softc.mr_op && mem_range_softc.mr_op->initAP) 473 return (mem_range_softc.mr_op->initAP(&mem_range_softc)); 474 } 475 #endif 476 477 static int 478 random_ioctl(cdev_t dev, u_long cmd, caddr_t data, int flags, struct ucred *cred) 479 { 480 int error; 481 int intr; 482 483 /* 484 * Even inspecting the state is privileged, since it gives a hint 485 * about how easily the randomness might be guessed. 486 */ 487 error = 0; 488 489 switch (cmd) { 490 /* Really handled in upper layer */ 491 case FIOASYNC: 492 break; 493 case MEM_SETIRQ: 494 intr = *(int16_t *)data; 495 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0) 496 break; 497 if (intr < 0 || intr >= MAX_INTS) 498 return (EINVAL); 499 register_randintr(intr); 500 break; 501 case MEM_CLEARIRQ: 502 intr = *(int16_t *)data; 503 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0) 504 break; 505 if (intr < 0 || intr >= MAX_INTS) 506 return (EINVAL); 507 unregister_randintr(intr); 508 break; 509 case MEM_RETURNIRQ: 510 error = ENOTSUP; 511 break; 512 case MEM_FINDIRQ: 513 intr = *(int16_t *)data; 514 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0) 515 break; 516 if (intr < 0 || intr >= MAX_INTS) 517 return (EINVAL); 518 intr = next_registered_randintr(intr); 519 if (intr == MAX_INTS) 520 return (ENOENT); 521 *(u_int16_t *)data = intr; 522 break; 523 default: 524 error = ENOTSUP; 525 break; 526 } 527 return (error); 528 } 529 530 static int 531 mm_filter_read(struct knote *kn, long hint) 532 { 533 return (1); 534 } 535 536 static void 537 dummy_filter_detach(struct knote *kn) {} 538 539 static struct filterops random_read_filtops = 540 { FILTEROP_ISFD, NULL, dummy_filter_detach, random_filter_read }; 541 542 static struct filterops mm_read_filtops = 543 { FILTEROP_ISFD, NULL, dummy_filter_detach, mm_filter_read }; 544 545 int 546 mmkqfilter(struct dev_kqfilter_args *ap) 547 { 548 struct knote *kn = ap->a_kn; 549 cdev_t dev = ap->a_head.a_dev; 550 551 ap->a_result = 0; 552 switch (kn->kn_filter) { 553 case EVFILT_READ: 554 switch (minor(dev)) { 555 case 3: 556 kn->kn_fop = &random_read_filtops; 557 break; 558 default: 559 kn->kn_fop = &mm_read_filtops; 560 break; 561 } 562 break; 563 default: 564 ap->a_result = EOPNOTSUPP; 565 return (0); 566 } 567 568 return (0); 569 } 570 571 int 572 iszerodev(cdev_t dev) 573 { 574 return (zerodev == dev); 575 } 576 577 static void 578 mem_drvinit(void *unused) 579 { 580 581 /* Initialise memory range handling */ 582 if (mem_range_softc.mr_op != NULL) 583 mem_range_softc.mr_op->init(&mem_range_softc); 584 585 make_dev(&mem_ops, 0, UID_ROOT, GID_KMEM, 0640, "mem"); 586 make_dev(&mem_ops, 1, UID_ROOT, GID_KMEM, 0640, "kmem"); 587 make_dev(&mem_ops, 2, UID_ROOT, GID_WHEEL, 0666, "null"); 588 make_dev(&mem_ops, 3, UID_ROOT, GID_WHEEL, 0644, "random"); 589 make_dev(&mem_ops, 4, UID_ROOT, GID_WHEEL, 0644, "urandom"); 590 zerodev = make_dev(&mem_ops, 12, UID_ROOT, GID_WHEEL, 0666, "zero"); 591 make_dev(&mem_ops, 14, UID_ROOT, GID_WHEEL, 0600, "io"); 592 } 593 594 SYSINIT(memdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,mem_drvinit,NULL) 595 596