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