projects/linux·blob master · refs

778 lines · 16.6 KiB

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