1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * mm/char-mem.c 4 * 5 * Copyright (C) 1991, 1992 Linus Torvalds 6 * 7 * Added devfs support. 8 * Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu> 9 * Shared /dev/zero mmapping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com> 10 */ 11 12 #include <linux/mm.h> 13 #include <linux/miscdevice.h> 14 #include <linux/slab.h> 15 #include <linux/vmalloc.h> 16 #include <linux/mman.h> 17 #include <linux/random.h> 18 #include <linux/init.h> 19 #include <linux/tty.h> 20 #include <linux/capability.h> 21 #include <linux/ptrace.h> 22 #include <linux/device.h> 23 #include <linux/highmem.h> 24 #include <linux/backing-dev.h> 25 #include <linux/shmem_fs.h> 26 #include <linux/splice.h> 27 #include <linux/pfn.h> 28 #include <linux/export.h> 29 #include <linux/io.h> 30 #include <linux/uio.h> 31 #include <linux/uaccess.h> 32 #include <linux/security.h> 33 34 #include "internal.h" 35 36 #define DEVMEM_MINOR 1 37 #define DEVPORT_MINOR 4 38 39 static inline unsigned long size_inside_page(unsigned long start, 40 unsigned long size) 41 { 42 unsigned long sz; 43 44 sz = PAGE_SIZE - (start & (PAGE_SIZE - 1)); 45 46 return min(sz, size); 47 } 48 49 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE 50 static inline int valid_phys_addr_range(phys_addr_t addr, size_t count) 51 { 52 return addr + count <= __pa(high_memory); 53 } 54 55 static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size) 56 { 57 return 1; 58 } 59 #endif 60 61 #ifdef CONFIG_STRICT_DEVMEM 62 static inline int page_is_allowed(unsigned long pfn) 63 { 64 return devmem_is_allowed(pfn); 65 } 66 #else 67 static inline int page_is_allowed(unsigned long pfn) 68 { 69 return 1; 70 } 71 #endif 72 73 static inline bool should_stop_iteration(void) 74 { 75 if (need_resched()) 76 cond_resched(); 77 return signal_pending(current); 78 } 79 80 /* 81 * This funcion reads the *physical* memory. The f_pos points directly to the 82 * memory location. 83 */ 84 static ssize_t read_mem(struct file *file, char __user *buf, 85 size_t count, loff_t *ppos) 86 { 87 phys_addr_t p = *ppos; 88 ssize_t read, sz; 89 void *ptr; 90 char *bounce; 91 int err; 92 93 if (p != *ppos) 94 return 0; 95 96 if (!valid_phys_addr_range(p, count)) 97 return -EFAULT; 98 read = 0; 99 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED 100 /* we don't have page 0 mapped on sparc and m68k.. */ 101 if (p < PAGE_SIZE) { 102 sz = size_inside_page(p, count); 103 if (sz > 0) { 104 if (clear_user(buf, sz)) 105 return -EFAULT; 106 buf += sz; 107 p += sz; 108 count -= sz; 109 read += sz; 110 } 111 } 112 #endif 113 114 bounce = kmalloc(PAGE_SIZE, GFP_KERNEL); 115 if (!bounce) 116 return -ENOMEM; 117 118 while (count > 0) { 119 unsigned long remaining; 120 int allowed, probe; 121 122 sz = size_inside_page(p, count); 123 124 err = -EPERM; 125 allowed = page_is_allowed(p >> PAGE_SHIFT); 126 if (!allowed) 127 goto failed; 128 129 err = -EFAULT; 130 if (allowed == 2) { 131 /* Show zeros for restricted memory. */ 132 remaining = clear_user(buf, sz); 133 } else { 134 /* 135 * On ia64 if a page has been mapped somewhere as 136 * uncached, then it must also be accessed uncached 137 * by the kernel or data corruption may occur. 138 */ 139 ptr = xlate_dev_mem_ptr(p); 140 if (!ptr) 141 goto failed; 142 143 probe = copy_from_kernel_nofault(bounce, ptr, sz); 144 unxlate_dev_mem_ptr(p, ptr); 145 if (probe) 146 goto failed; 147 148 remaining = copy_to_user(buf, bounce, sz); 149 } 150 151 if (remaining) 152 goto failed; 153 154 buf += sz; 155 p += sz; 156 count -= sz; 157 read += sz; 158 if (should_stop_iteration()) 159 break; 160 } 161 kfree(bounce); 162 163 *ppos += read; 164 return read; 165 166 failed: 167 kfree(bounce); 168 return err; 169 } 170 171 static ssize_t write_mem(struct file *file, const char __user *buf, 172 size_t count, loff_t *ppos) 173 { 174 phys_addr_t p = *ppos; 175 ssize_t written, sz; 176 unsigned long copied; 177 void *ptr; 178 179 if (p != *ppos) 180 return -EFBIG; 181 182 if (!valid_phys_addr_range(p, count)) 183 return -EFAULT; 184 185 written = 0; 186 187 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED 188 /* we don't have page 0 mapped on sparc and m68k.. */ 189 if (p < PAGE_SIZE) { 190 sz = size_inside_page(p, count); 191 /* Hmm. Do something? */ 192 buf += sz; 193 p += sz; 194 count -= sz; 195 written += sz; 196 } 197 #endif 198 199 while (count > 0) { 200 int allowed; 201 202 sz = size_inside_page(p, count); 203 204 allowed = page_is_allowed(p >> PAGE_SHIFT); 205 if (!allowed) 206 return -EPERM; 207 208 /* Skip actual writing when a page is marked as restricted. */ 209 if (allowed == 1) { 210 /* 211 * On ia64 if a page has been mapped somewhere as 212 * uncached, then it must also be accessed uncached 213 * by the kernel or data corruption may occur. 214 */ 215 ptr = xlate_dev_mem_ptr(p); 216 if (!ptr) { 217 if (written) 218 break; 219 return -EFAULT; 220 } 221 222 copied = copy_from_user(ptr, buf, sz); 223 unxlate_dev_mem_ptr(p, ptr); 224 if (copied) { 225 written += sz - copied; 226 if (written) 227 break; 228 return -EFAULT; 229 } 230 } 231 232 buf += sz; 233 p += sz; 234 count -= sz; 235 written += sz; 236 if (should_stop_iteration()) 237 break; 238 } 239 240 *ppos += written; 241 return written; 242 } 243 244 int __weak phys_mem_access_prot_allowed(struct file *file, 245 unsigned long pfn, unsigned long size, pgprot_t *vma_prot) 246 { 247 return 1; 248 } 249 250 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT 251 252 /* 253 * Architectures vary in how they handle caching for addresses 254 * outside of main memory. 255 * 256 */ 257 #ifdef pgprot_noncached 258 static int uncached_access(struct file *file, phys_addr_t addr) 259 { 260 /* 261 * Accessing memory above the top the kernel knows about or through a 262 * file pointer 263 * that was marked O_DSYNC will be done non-cached. 264 */ 265 if (file->f_flags & O_DSYNC) 266 return 1; 267 return addr >= __pa(high_memory); 268 } 269 #endif 270 271 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, 272 unsigned long size, pgprot_t vma_prot) 273 { 274 #ifdef pgprot_noncached 275 phys_addr_t offset = pfn << PAGE_SHIFT; 276 277 if (uncached_access(file, offset)) 278 return pgprot_noncached(vma_prot); 279 #endif 280 return vma_prot; 281 } 282 #endif 283 284 #ifndef CONFIG_MMU 285 static unsigned long get_unmapped_area_mem(struct file *file, 286 unsigned long addr, 287 unsigned long len, 288 unsigned long pgoff, 289 unsigned long flags) 290 { 291 if (!valid_mmap_phys_addr_range(pgoff, len)) 292 return (unsigned long) -EINVAL; 293 return pgoff << PAGE_SHIFT; 294 } 295 296 /* permit direct mmap, for read, write or exec */ 297 static unsigned memory_mmap_capabilities(struct file *file) 298 { 299 return NOMMU_MAP_DIRECT | 300 NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC; 301 } 302 303 static unsigned zero_mmap_capabilities(struct file *file) 304 { 305 return NOMMU_MAP_COPY; 306 } 307 308 /* can't do an in-place private mapping if there's no MMU */ 309 static inline int private_mapping_ok(struct vm_area_desc *desc) 310 { 311 return is_nommu_shared_vma_flags(&desc->vma_flags); 312 } 313 #else 314 315 static inline int private_mapping_ok(struct vm_area_desc *desc) 316 { 317 return 1; 318 } 319 #endif 320 321 static const struct vm_operations_struct mmap_mem_ops = { 322 #ifdef CONFIG_HAVE_IOREMAP_PROT 323 .access = generic_access_phys 324 #endif 325 }; 326 327 static int mmap_mem_prepare(struct vm_area_desc *desc) 328 { 329 struct file *file = desc->file; 330 const size_t size = vma_desc_size(desc); 331 const phys_addr_t offset = (phys_addr_t)desc->pgoff << PAGE_SHIFT; 332 333 /* Does it even fit in phys_addr_t? */ 334 if (offset >> PAGE_SHIFT != desc->pgoff) 335 return -EINVAL; 336 337 /* It's illegal to wrap around the end of the physical address space. */ 338 if (offset + (phys_addr_t)size - 1 < offset) 339 return -EINVAL; 340 341 if (!valid_mmap_phys_addr_range(desc->pgoff, size)) 342 return -EINVAL; 343 344 if (!private_mapping_ok(desc)) 345 return -ENOSYS; 346 347 if (!range_is_allowed(desc->pgoff, size)) 348 return -EPERM; 349 350 if (!phys_mem_access_prot_allowed(file, desc->pgoff, size, 351 &desc->page_prot)) 352 return -EINVAL; 353 354 desc->page_prot = phys_mem_access_prot(file, desc->pgoff, 355 size, 356 desc->page_prot); 357 358 desc->vm_ops = &mmap_mem_ops; 359 360 /* Remap-pfn-range will mark the range with the I/O flag. */ 361 mmap_action_remap_full(desc, desc->pgoff); 362 desc->action.error_override = -EAGAIN; 363 364 return 0; 365 } 366 367 #ifdef CONFIG_DEVPORT 368 static ssize_t read_port(struct file *file, char __user *buf, 369 size_t count, loff_t *ppos) 370 { 371 unsigned long i = *ppos; 372 char __user *tmp = buf; 373 374 if (!access_ok(buf, count)) 375 return -EFAULT; 376 while (count-- > 0 && i < 65536) { 377 if (put_user(inb(i), tmp) < 0) 378 return -EFAULT; 379 i++; 380 tmp++; 381 } 382 *ppos = i; 383 return tmp-buf; 384 } 385 386 static ssize_t write_port(struct file *file, const char __user *buf, 387 size_t count, loff_t *ppos) 388 { 389 unsigned long i = *ppos; 390 const char __user *tmp = buf; 391 392 if (!access_ok(buf, count)) 393 return -EFAULT; 394 while (count-- > 0 && i < 65536) { 395 char c; 396 397 if (get_user(c, tmp)) { 398 if (tmp > buf) 399 break; 400 return -EFAULT; 401 } 402 outb(c, i); 403 i++; 404 tmp++; 405 } 406 *ppos = i; 407 return tmp-buf; 408 } 409 #endif 410 411 static ssize_t read_null(struct file *file, char __user *buf, 412 size_t count, loff_t *ppos) 413 { 414 return 0; 415 } 416 417 static ssize_t write_null(struct file *file, const char __user *buf, 418 size_t count, loff_t *ppos) 419 { 420 return count; 421 } 422 423 static ssize_t read_iter_null(struct kiocb *iocb, struct iov_iter *to) 424 { 425 return 0; 426 } 427 428 static ssize_t write_iter_null(struct kiocb *iocb, struct iov_iter *from) 429 { 430 size_t count = iov_iter_count(from); 431 iov_iter_advance(from, count); 432 return count; 433 } 434 435 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf, 436 struct splice_desc *sd) 437 { 438 return sd->len; 439 } 440 441 static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out, 442 loff_t *ppos, size_t len, unsigned int flags) 443 { 444 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null); 445 } 446 447 static int uring_cmd_null(struct io_uring_cmd *ioucmd, unsigned int issue_flags) 448 { 449 return 0; 450 } 451 452 static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter) 453 { 454 size_t written = 0; 455 456 while (iov_iter_count(iter)) { 457 size_t chunk = iov_iter_count(iter), n; 458 459 if (chunk > PAGE_SIZE) 460 chunk = PAGE_SIZE; /* Just for latency reasons */ 461 n = iov_iter_zero(chunk, iter); 462 if (!n && iov_iter_count(iter)) 463 return written ? written : -EFAULT; 464 written += n; 465 if (signal_pending(current)) 466 return written ? written : -ERESTARTSYS; 467 if (!need_resched()) 468 continue; 469 if (iocb->ki_flags & IOCB_NOWAIT) 470 return written ? written : -EAGAIN; 471 cond_resched(); 472 } 473 return written; 474 } 475 476 static ssize_t read_zero(struct file *file, char __user *buf, 477 size_t count, loff_t *ppos) 478 { 479 size_t cleared = 0; 480 481 while (count) { 482 size_t chunk = min_t(size_t, count, PAGE_SIZE); 483 size_t left; 484 485 left = clear_user(buf + cleared, chunk); 486 if (unlikely(left)) { 487 cleared += (chunk - left); 488 if (!cleared) 489 return -EFAULT; 490 break; 491 } 492 cleared += chunk; 493 count -= chunk; 494 495 if (signal_pending(current)) 496 break; 497 cond_resched(); 498 } 499 500 return cleared; 501 } 502 503 static int mmap_zero_prepare(struct vm_area_desc *desc) 504 { 505 #ifndef CONFIG_MMU 506 return -ENOSYS; 507 #endif 508 if (vma_desc_test(desc, VMA_MAYSHARE_BIT)) 509 return shmem_zero_setup_desc(desc); 510 511 /* MAP_PRIVATE semantics are taken care of for us by core mm. */ 512 return 0; 513 } 514 515 #ifndef CONFIG_MMU 516 static unsigned long get_unmapped_area_zero(struct file *file, 517 unsigned long addr, unsigned long len, 518 unsigned long pgoff, unsigned long flags) 519 { 520 return -ENOSYS; 521 } 522 #else 523 static unsigned long get_unmapped_area_zero(struct file *file, 524 unsigned long addr, unsigned long len, 525 unsigned long pgoff, unsigned long flags) 526 { 527 if (flags & MAP_SHARED) { 528 /* 529 * mmap_zero_prepare() will call shmem_zero_setup() to create a 530 * file, so use shmem's get_unmapped_area in case it can be 531 * huge; and pass NULL for file as in mmap.c's 532 * get_unmapped_area(), so as not to confuse shmem with our 533 * handle on "/dev/zero". 534 */ 535 return shmem_get_unmapped_area(NULL, addr, len, pgoff, flags); 536 } 537 538 /* 539 * Otherwise flags & MAP_PRIVATE: with no shmem object beneath it, 540 * attempt to map aligned to huge page size if possible, otherwise we 541 * fall back to system page size mappings. 542 */ 543 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 544 return thp_get_unmapped_area(file, addr, len, pgoff, flags); 545 #else 546 return mm_get_unmapped_area(file, addr, len, pgoff, flags); 547 #endif 548 } 549 #endif /* CONFIG_MMU */ 550 551 static ssize_t write_full(struct file *file, const char __user *buf, 552 size_t count, loff_t *ppos) 553 { 554 return -ENOSPC; 555 } 556 557 /* 558 * Special lseek() function for /dev/null and /dev/zero. Most notably, you 559 * can fopen() both devices with "a" now. This was previously impossible. 560 * -- SRB. 561 */ 562 static loff_t null_lseek(struct file *file, loff_t offset, int orig) 563 { 564 return file->f_pos = 0; 565 } 566 567 /* 568 * The memory devices use the full 32/64 bits of the offset, and so we cannot 569 * check against negative addresses: they are ok. The return value is weird, 570 * though, in that case (0). 571 * 572 * also note that seeking relative to the "end of file" isn't supported: 573 * it has no meaning, so it returns -EINVAL. 574 */ 575 static loff_t memory_lseek(struct file *file, loff_t offset, int orig) 576 { 577 loff_t ret; 578 579 inode_lock(file_inode(file)); 580 switch (orig) { 581 case SEEK_CUR: 582 offset += file->f_pos; 583 fallthrough; 584 case SEEK_SET: 585 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */ 586 if ((unsigned long long)offset >= -MAX_ERRNO) { 587 ret = -EOVERFLOW; 588 break; 589 } 590 file->f_pos = offset; 591 ret = file->f_pos; 592 force_successful_syscall_return(); 593 break; 594 default: 595 ret = -EINVAL; 596 } 597 inode_unlock(file_inode(file)); 598 return ret; 599 } 600 601 static int open_port(struct inode *inode, struct file *filp) 602 { 603 int rc; 604 605 if (!capable(CAP_SYS_RAWIO)) 606 return -EPERM; 607 608 rc = security_locked_down(LOCKDOWN_DEV_MEM); 609 if (rc) 610 return rc; 611 612 if (iminor(inode) != DEVMEM_MINOR) 613 return 0; 614 615 /* 616 * Use a unified address space to have a single point to manage 617 * revocations when drivers want to take over a /dev/mem mapped 618 * range. 619 */ 620 filp->f_mapping = iomem_get_mapping(); 621 622 return 0; 623 } 624 625 #define zero_lseek null_lseek 626 #define full_lseek null_lseek 627 #define write_zero write_null 628 #define write_iter_zero write_iter_null 629 #define splice_write_zero splice_write_null 630 #define open_mem open_port 631 632 static const struct file_operations __maybe_unused mem_fops = { 633 .llseek = memory_lseek, 634 .read = read_mem, 635 .write = write_mem, 636 .mmap_prepare = mmap_mem_prepare, 637 .open = open_mem, 638 #ifndef CONFIG_MMU 639 .get_unmapped_area = get_unmapped_area_mem, 640 .mmap_capabilities = memory_mmap_capabilities, 641 #endif 642 .fop_flags = FOP_UNSIGNED_OFFSET, 643 }; 644 645 static const struct file_operations null_fops = { 646 .llseek = null_lseek, 647 .read = read_null, 648 .write = write_null, 649 .read_iter = read_iter_null, 650 .write_iter = write_iter_null, 651 .splice_write = splice_write_null, 652 .uring_cmd = uring_cmd_null, 653 }; 654 655 #ifdef CONFIG_DEVPORT 656 static const struct file_operations port_fops = { 657 .llseek = memory_lseek, 658 .read = read_port, 659 .write = write_port, 660 .open = open_port, 661 }; 662 #endif 663 664 static const struct file_operations zero_fops = { 665 .llseek = zero_lseek, 666 .write = write_zero, 667 .read_iter = read_iter_zero, 668 .read = read_zero, 669 .write_iter = write_iter_zero, 670 .splice_read = copy_splice_read, 671 .splice_write = splice_write_zero, 672 .mmap_prepare = mmap_zero_prepare, 673 .get_unmapped_area = get_unmapped_area_zero, 674 #ifndef CONFIG_MMU 675 .mmap_capabilities = zero_mmap_capabilities, 676 #endif 677 }; 678 679 static const struct file_operations full_fops = { 680 .llseek = full_lseek, 681 .read_iter = read_iter_zero, 682 .write = write_full, 683 .splice_read = copy_splice_read, 684 }; 685 686 static const struct memdev { 687 const char *name; 688 const struct file_operations *fops; 689 fmode_t fmode; 690 umode_t mode; 691 } devlist[] = { 692 #ifdef CONFIG_DEVMEM 693 [DEVMEM_MINOR] = { "mem", &mem_fops, 0, 0 }, 694 #endif 695 [3] = { "null", &null_fops, FMODE_NOWAIT, 0666 }, 696 #ifdef CONFIG_DEVPORT 697 [4] = { "port", &port_fops, 0, 0 }, 698 #endif 699 [5] = { "zero", &zero_fops, FMODE_NOWAIT, 0666 }, 700 [7] = { "full", &full_fops, 0, 0666 }, 701 [8] = { "random", &random_fops, FMODE_NOWAIT, 0666 }, 702 [9] = { "urandom", &urandom_fops, FMODE_NOWAIT, 0666 }, 703 #ifdef CONFIG_PRINTK 704 [11] = { "kmsg", &kmsg_fops, 0, 0644 }, 705 #endif 706 }; 707 708 /** 709 * file_is_dev_zero() - is the specified @file associated with the /dev/zero 710 * driver? 711 * @file: File to test. 712 * Returns: true if it is, false otherwise. 713 */ 714 bool file_is_dev_zero(const struct file *file) 715 { 716 return file && file->f_op == &zero_fops; 717 } 718 719 static int memory_open(struct inode *inode, struct file *filp) 720 { 721 int minor; 722 const struct memdev *dev; 723 724 minor = iminor(inode); 725 if (minor >= ARRAY_SIZE(devlist)) 726 return -ENXIO; 727 728 dev = &devlist[minor]; 729 if (!dev->fops) 730 return -ENXIO; 731 732 filp->f_op = dev->fops; 733 filp->f_mode |= dev->fmode; 734 735 if (dev->fops->open) 736 return dev->fops->open(inode, filp); 737 738 return 0; 739 } 740 741 static const struct file_operations memory_fops = { 742 .open = memory_open, 743 .llseek = noop_llseek, 744 }; 745 746 static char *mem_devnode(const struct device *dev, umode_t *mode) 747 { 748 if (mode && devlist[MINOR(dev->devt)].mode) 749 *mode = devlist[MINOR(dev->devt)].mode; 750 return NULL; 751 } 752 753 static const struct class mem_class = { 754 .name = "mem", 755 .devnode = mem_devnode, 756 }; 757 758 static int __init chr_dev_init(void) 759 { 760 int retval; 761 int minor; 762 763 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops)) 764 printk("unable to get major %d for memory devs\n", MEM_MAJOR); 765 766 retval = class_register(&mem_class); 767 if (retval) 768 return retval; 769 770 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) { 771 if (!devlist[minor].name) 772 continue; 773 774 /* 775 * Create /dev/port? 776 */ 777 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port()) 778 continue; 779 780 device_create(&mem_class, NULL, MKDEV(MEM_MAJOR, minor), 781 NULL, devlist[minor].name); 782 } 783 784 return tty_init(); 785 } 786 787 fs_initcall(chr_dev_init); 788