1 /*
2 * linux/drivers/char/mem.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * Added devfs support.
7 * Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
8 * Shared /dev/zero mmapping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
9 */
10
11 #include <linux/mm.h>
12 #include <linux/miscdevice.h>
13 #include <linux/slab.h>
14 #include <linux/vmalloc.h>
15 #include <linux/mman.h>
16 #include <linux/random.h>
17 #include <linux/init.h>
18 #include <linux/raw.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/splice.h>
26 #include <linux/pfn.h>
27 #include <linux/export.h>
28 #include <linux/io.h>
29 #include <linux/aio.h>
30
31 #include <asm/uaccess.h>
32
33 #ifdef CONFIG_IA64
34 # include <linux/efi.h>
35 #endif
36
37 #define DEVPORT_MINOR 4
38
size_inside_page(unsigned long start,unsigned long size)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
valid_phys_addr_range(phys_addr_t addr,size_t count)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
valid_mmap_phys_addr_range(unsigned long pfn,size_t size)55 static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
56 {
57 return 1;
58 }
59 #endif
60
61 #if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM)
62 #ifdef CONFIG_STRICT_DEVMEM
page_is_allowed(unsigned long pfn)63 static inline int page_is_allowed(unsigned long pfn)
64 {
65 return devmem_is_allowed(pfn);
66 }
range_is_allowed(unsigned long pfn,unsigned long size)67 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
68 {
69 u64 from = ((u64)pfn) << PAGE_SHIFT;
70 u64 to = from + size;
71 u64 cursor = from;
72
73 while (cursor < to) {
74 if (!devmem_is_allowed(pfn)) {
75 printk(KERN_INFO
76 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
77 current->comm, from, to);
78 return 0;
79 }
80 cursor += PAGE_SIZE;
81 pfn++;
82 }
83 return 1;
84 }
85 #else
page_is_allowed(unsigned long pfn)86 static inline int page_is_allowed(unsigned long pfn)
87 {
88 return 1;
89 }
range_is_allowed(unsigned long pfn,unsigned long size)90 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
91 {
92 return 1;
93 }
94 #endif
95 #endif
96
97 #ifdef CONFIG_DEVMEM
unxlate_dev_mem_ptr(unsigned long phys,void * addr)98 void __weak unxlate_dev_mem_ptr(unsigned long phys, void *addr)
99 {
100 }
101
102 /*
103 * This funcion reads the *physical* memory. The f_pos points directly to the
104 * memory location.
105 */
read_mem(struct file * file,char __user * buf,size_t count,loff_t * ppos)106 static ssize_t read_mem(struct file *file, char __user *buf,
107 size_t count, loff_t *ppos)
108 {
109 phys_addr_t p = *ppos;
110 ssize_t read, sz;
111 char *ptr;
112
113 if (p != *ppos)
114 return 0;
115
116 if (!valid_phys_addr_range(p, count))
117 return -EFAULT;
118 read = 0;
119 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
120 /* we don't have page 0 mapped on sparc and m68k.. */
121 if (p < PAGE_SIZE) {
122 sz = size_inside_page(p, count);
123 if (sz > 0) {
124 if (clear_user(buf, sz))
125 return -EFAULT;
126 buf += sz;
127 p += sz;
128 count -= sz;
129 read += sz;
130 }
131 }
132 #endif
133
134 while (count > 0) {
135 unsigned long remaining;
136 int allowed;
137
138 sz = size_inside_page(p, count);
139
140 allowed = page_is_allowed(p >> PAGE_SHIFT);
141 if (!allowed)
142 return -EPERM;
143 if (allowed == 2) {
144 /* Show zeros for restricted memory. */
145 remaining = clear_user(buf, sz);
146 } else {
147 /*
148 * On ia64 if a page has been mapped somewhere as
149 * uncached, then it must also be accessed uncached
150 * by the kernel or data corruption may occur.
151 */
152 ptr = xlate_dev_mem_ptr(p);
153 if (!ptr)
154 return -EFAULT;
155
156 remaining = copy_to_user(buf, ptr, sz);
157
158 unxlate_dev_mem_ptr(p, ptr);
159 }
160
161 if (remaining)
162 return -EFAULT;
163
164 buf += sz;
165 p += sz;
166 count -= sz;
167 read += sz;
168 }
169
170 *ppos += read;
171 return read;
172 }
173
write_mem(struct file * file,const char __user * buf,size_t count,loff_t * ppos)174 static ssize_t write_mem(struct file *file, const char __user *buf,
175 size_t count, loff_t *ppos)
176 {
177 phys_addr_t p = *ppos;
178 ssize_t written, sz;
179 unsigned long copied;
180 void *ptr;
181
182 if (p != *ppos)
183 return -EFBIG;
184
185 if (!valid_phys_addr_range(p, count))
186 return -EFAULT;
187
188 written = 0;
189
190 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
191 /* we don't have page 0 mapped on sparc and m68k.. */
192 if (p < PAGE_SIZE) {
193 sz = size_inside_page(p, count);
194 /* Hmm. Do something? */
195 buf += sz;
196 p += sz;
197 count -= sz;
198 written += sz;
199 }
200 #endif
201
202 while (count > 0) {
203 int allowed;
204
205 sz = size_inside_page(p, count);
206
207 allowed = page_is_allowed(p >> PAGE_SHIFT);
208 if (!allowed)
209 return -EPERM;
210
211 /* Skip actual writing when a page is marked as restricted. */
212 if (allowed == 1) {
213 /*
214 * On ia64 if a page has been mapped somewhere as
215 * uncached, then it must also be accessed uncached
216 * by the kernel or data corruption may occur.
217 */
218 ptr = xlate_dev_mem_ptr(p);
219 if (!ptr) {
220 if (written)
221 break;
222 return -EFAULT;
223 }
224
225 copied = copy_from_user(ptr, buf, sz);
226 unxlate_dev_mem_ptr(p, ptr);
227 if (copied) {
228 written += sz - copied;
229 if (written)
230 break;
231 return -EFAULT;
232 }
233 }
234
235 buf += sz;
236 p += sz;
237 count -= sz;
238 written += sz;
239 }
240
241 *ppos += written;
242 return written;
243 }
244 #endif /* CONFIG_DEVMEM */
245
246 #if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM)
247
phys_mem_access_prot_allowed(struct file * file,unsigned long pfn,unsigned long size,pgprot_t * vma_prot)248 int __weak phys_mem_access_prot_allowed(struct file *file,
249 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
250 {
251 return 1;
252 }
253
254 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
255
256 /*
257 * Architectures vary in how they handle caching for addresses
258 * outside of main memory.
259 *
260 */
261 #ifdef pgprot_noncached
uncached_access(struct file * file,phys_addr_t addr)262 static int uncached_access(struct file *file, phys_addr_t addr)
263 {
264 #if defined(CONFIG_IA64)
265 /*
266 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
267 * attribute aliases.
268 */
269 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
270 #elif defined(CONFIG_MIPS)
271 {
272 extern int __uncached_access(struct file *file,
273 unsigned long addr);
274
275 return __uncached_access(file, addr);
276 }
277 #else
278 /*
279 * Accessing memory above the top the kernel knows about or through a
280 * file pointer
281 * that was marked O_DSYNC will be done non-cached.
282 */
283 if (file->f_flags & O_DSYNC)
284 return 1;
285 return addr >= __pa(high_memory);
286 #endif
287 }
288 #endif
289
phys_mem_access_prot(struct file * file,unsigned long pfn,unsigned long size,pgprot_t vma_prot)290 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
291 unsigned long size, pgprot_t vma_prot)
292 {
293 #ifdef pgprot_noncached
294 phys_addr_t offset = pfn << PAGE_SHIFT;
295
296 if (uncached_access(file, offset))
297 return pgprot_noncached(vma_prot);
298 #endif
299 return vma_prot;
300 }
301 #endif
302
303 #ifndef CONFIG_MMU
get_unmapped_area_mem(struct file * file,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)304 static unsigned long get_unmapped_area_mem(struct file *file,
305 unsigned long addr,
306 unsigned long len,
307 unsigned long pgoff,
308 unsigned long flags)
309 {
310 if (!valid_mmap_phys_addr_range(pgoff, len))
311 return (unsigned long) -EINVAL;
312 return pgoff << PAGE_SHIFT;
313 }
314
315 /* can't do an in-place private mapping if there's no MMU */
private_mapping_ok(struct vm_area_struct * vma)316 static inline int private_mapping_ok(struct vm_area_struct *vma)
317 {
318 return vma->vm_flags & VM_MAYSHARE;
319 }
320 #else
321 #define get_unmapped_area_mem NULL
322
private_mapping_ok(struct vm_area_struct * vma)323 static inline int private_mapping_ok(struct vm_area_struct *vma)
324 {
325 return 1;
326 }
327 #endif
328
329 static const struct vm_operations_struct mmap_mem_ops = {
330 #ifdef CONFIG_HAVE_IOREMAP_PROT
331 .access = generic_access_phys
332 #endif
333 };
334
mmap_mem(struct file * file,struct vm_area_struct * vma)335 static int mmap_mem(struct file *file, struct vm_area_struct *vma)
336 {
337 size_t size = vma->vm_end - vma->vm_start;
338 phys_addr_t offset = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
339
340 /* It's illegal to wrap around the end of the physical address space. */
341 if (offset + (phys_addr_t)size - 1 < offset)
342 return -EINVAL;
343
344 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
345 return -EINVAL;
346
347 if (!private_mapping_ok(vma))
348 return -ENOSYS;
349
350 if (!range_is_allowed(vma->vm_pgoff, size))
351 return -EPERM;
352
353 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
354 &vma->vm_page_prot))
355 return -EINVAL;
356
357 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
358 size,
359 vma->vm_page_prot);
360
361 vma->vm_ops = &mmap_mem_ops;
362
363 /* Remap-pfn-range will mark the range VM_IO */
364 if (remap_pfn_range(vma,
365 vma->vm_start,
366 vma->vm_pgoff,
367 size,
368 vma->vm_page_prot)) {
369 return -EAGAIN;
370 }
371 return 0;
372 }
373 #endif /* CONFIG_DEVMEM */
374
375 #ifdef CONFIG_DEVKMEM
mmap_kmem(struct file * file,struct vm_area_struct * vma)376 static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
377 {
378 unsigned long pfn;
379
380 /* Turn a kernel-virtual address into a physical page frame */
381 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
382
383 /*
384 * RED-PEN: on some architectures there is more mapped memory than
385 * available in mem_map which pfn_valid checks for. Perhaps should add a
386 * new macro here.
387 *
388 * RED-PEN: vmalloc is not supported right now.
389 */
390 if (!pfn_valid(pfn))
391 return -EIO;
392
393 vma->vm_pgoff = pfn;
394 return mmap_mem(file, vma);
395 }
396 #endif
397
398 #ifdef CONFIG_DEVKMEM
399 /*
400 * This function reads the *virtual* memory as seen by the kernel.
401 */
read_kmem(struct file * file,char __user * buf,size_t count,loff_t * ppos)402 static ssize_t read_kmem(struct file *file, char __user *buf,
403 size_t count, loff_t *ppos)
404 {
405 unsigned long p = *ppos;
406 ssize_t low_count, read, sz;
407 char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
408 int err = 0;
409
410 read = 0;
411 if (p < (unsigned long) high_memory) {
412 low_count = count;
413 if (count > (unsigned long)high_memory - p)
414 low_count = (unsigned long)high_memory - p;
415
416 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
417 /* we don't have page 0 mapped on sparc and m68k.. */
418 if (p < PAGE_SIZE && low_count > 0) {
419 sz = size_inside_page(p, low_count);
420 if (clear_user(buf, sz))
421 return -EFAULT;
422 buf += sz;
423 p += sz;
424 read += sz;
425 low_count -= sz;
426 count -= sz;
427 }
428 #endif
429 while (low_count > 0) {
430 sz = size_inside_page(p, low_count);
431
432 /*
433 * On ia64 if a page has been mapped somewhere as
434 * uncached, then it must also be accessed uncached
435 * by the kernel or data corruption may occur
436 */
437 kbuf = xlate_dev_kmem_ptr((char *)p);
438
439 if (copy_to_user(buf, kbuf, sz))
440 return -EFAULT;
441 buf += sz;
442 p += sz;
443 read += sz;
444 low_count -= sz;
445 count -= sz;
446 }
447 }
448
449 if (count > 0) {
450 kbuf = (char *)__get_free_page(GFP_KERNEL);
451 if (!kbuf)
452 return -ENOMEM;
453 while (count > 0) {
454 sz = size_inside_page(p, count);
455 if (!is_vmalloc_or_module_addr((void *)p)) {
456 err = -ENXIO;
457 break;
458 }
459 sz = vread(kbuf, (char *)p, sz);
460 if (!sz)
461 break;
462 if (copy_to_user(buf, kbuf, sz)) {
463 err = -EFAULT;
464 break;
465 }
466 count -= sz;
467 buf += sz;
468 read += sz;
469 p += sz;
470 }
471 free_page((unsigned long)kbuf);
472 }
473 *ppos = p;
474 return read ? read : err;
475 }
476
477
do_write_kmem(unsigned long p,const char __user * buf,size_t count,loff_t * ppos)478 static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
479 size_t count, loff_t *ppos)
480 {
481 ssize_t written, sz;
482 unsigned long copied;
483
484 written = 0;
485 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
486 /* we don't have page 0 mapped on sparc and m68k.. */
487 if (p < PAGE_SIZE) {
488 sz = size_inside_page(p, count);
489 /* Hmm. Do something? */
490 buf += sz;
491 p += sz;
492 count -= sz;
493 written += sz;
494 }
495 #endif
496
497 while (count > 0) {
498 char *ptr;
499
500 sz = size_inside_page(p, count);
501
502 /*
503 * On ia64 if a page has been mapped somewhere as uncached, then
504 * it must also be accessed uncached by the kernel or data
505 * corruption may occur.
506 */
507 ptr = xlate_dev_kmem_ptr((char *)p);
508
509 copied = copy_from_user(ptr, buf, sz);
510 if (copied) {
511 written += sz - copied;
512 if (written)
513 break;
514 return -EFAULT;
515 }
516 buf += sz;
517 p += sz;
518 count -= sz;
519 written += sz;
520 }
521
522 *ppos += written;
523 return written;
524 }
525
526 /*
527 * This function writes to the *virtual* memory as seen by the kernel.
528 */
write_kmem(struct file * file,const char __user * buf,size_t count,loff_t * ppos)529 static ssize_t write_kmem(struct file *file, const char __user *buf,
530 size_t count, loff_t *ppos)
531 {
532 unsigned long p = *ppos;
533 ssize_t wrote = 0;
534 ssize_t virtr = 0;
535 char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
536 int err = 0;
537
538 if (p < (unsigned long) high_memory) {
539 unsigned long to_write = min_t(unsigned long, count,
540 (unsigned long)high_memory - p);
541 wrote = do_write_kmem(p, buf, to_write, ppos);
542 if (wrote != to_write)
543 return wrote;
544 p += wrote;
545 buf += wrote;
546 count -= wrote;
547 }
548
549 if (count > 0) {
550 kbuf = (char *)__get_free_page(GFP_KERNEL);
551 if (!kbuf)
552 return wrote ? wrote : -ENOMEM;
553 while (count > 0) {
554 unsigned long sz = size_inside_page(p, count);
555 unsigned long n;
556
557 if (!is_vmalloc_or_module_addr((void *)p)) {
558 err = -ENXIO;
559 break;
560 }
561 n = copy_from_user(kbuf, buf, sz);
562 if (n) {
563 err = -EFAULT;
564 break;
565 }
566 vwrite(kbuf, (char *)p, sz);
567 count -= sz;
568 buf += sz;
569 virtr += sz;
570 p += sz;
571 }
572 free_page((unsigned long)kbuf);
573 }
574
575 *ppos = p;
576 return virtr + wrote ? : err;
577 }
578 #endif
579
580 #ifdef CONFIG_DEVPORT
read_port(struct file * file,char __user * buf,size_t count,loff_t * ppos)581 static ssize_t read_port(struct file *file, char __user *buf,
582 size_t count, loff_t *ppos)
583 {
584 unsigned long i = *ppos;
585 char __user *tmp = buf;
586
587 if (!access_ok(VERIFY_WRITE, buf, count))
588 return -EFAULT;
589 while (count-- > 0 && i < 65536) {
590 if (__put_user(inb(i), tmp) < 0)
591 return -EFAULT;
592 i++;
593 tmp++;
594 }
595 *ppos = i;
596 return tmp-buf;
597 }
598
write_port(struct file * file,const char __user * buf,size_t count,loff_t * ppos)599 static ssize_t write_port(struct file *file, const char __user *buf,
600 size_t count, loff_t *ppos)
601 {
602 unsigned long i = *ppos;
603 const char __user *tmp = buf;
604
605 if (!access_ok(VERIFY_READ, buf, count))
606 return -EFAULT;
607 while (count-- > 0 && i < 65536) {
608 char c;
609 if (__get_user(c, tmp)) {
610 if (tmp > buf)
611 break;
612 return -EFAULT;
613 }
614 outb(c, i);
615 i++;
616 tmp++;
617 }
618 *ppos = i;
619 return tmp-buf;
620 }
621 #endif
622
read_null(struct file * file,char __user * buf,size_t count,loff_t * ppos)623 static ssize_t read_null(struct file *file, char __user *buf,
624 size_t count, loff_t *ppos)
625 {
626 return 0;
627 }
628
write_null(struct file * file,const char __user * buf,size_t count,loff_t * ppos)629 static ssize_t write_null(struct file *file, const char __user *buf,
630 size_t count, loff_t *ppos)
631 {
632 return count;
633 }
634
aio_read_null(struct kiocb * iocb,const struct iovec * iov,unsigned long nr_segs,loff_t pos)635 static ssize_t aio_read_null(struct kiocb *iocb, const struct iovec *iov,
636 unsigned long nr_segs, loff_t pos)
637 {
638 return 0;
639 }
640
aio_write_null(struct kiocb * iocb,const struct iovec * iov,unsigned long nr_segs,loff_t pos)641 static ssize_t aio_write_null(struct kiocb *iocb, const struct iovec *iov,
642 unsigned long nr_segs, loff_t pos)
643 {
644 return iov_length(iov, nr_segs);
645 }
646
pipe_to_null(struct pipe_inode_info * info,struct pipe_buffer * buf,struct splice_desc * sd)647 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
648 struct splice_desc *sd)
649 {
650 return sd->len;
651 }
652
splice_write_null(struct pipe_inode_info * pipe,struct file * out,loff_t * ppos,size_t len,unsigned int flags)653 static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
654 loff_t *ppos, size_t len, unsigned int flags)
655 {
656 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
657 }
658
read_iter_zero(struct kiocb * iocb,struct iov_iter * iter)659 static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
660 {
661 size_t written = 0;
662
663 while (iov_iter_count(iter)) {
664 size_t chunk = iov_iter_count(iter), n;
665 if (chunk > PAGE_SIZE)
666 chunk = PAGE_SIZE; /* Just for latency reasons */
667 n = iov_iter_zero(chunk, iter);
668 if (!n && iov_iter_count(iter))
669 return written ? written : -EFAULT;
670 written += n;
671 if (signal_pending(current))
672 return written ? written : -ERESTARTSYS;
673 cond_resched();
674 }
675 return written;
676 }
677
mmap_zero(struct file * file,struct vm_area_struct * vma)678 static int mmap_zero(struct file *file, struct vm_area_struct *vma)
679 {
680 #ifndef CONFIG_MMU
681 return -ENOSYS;
682 #endif
683 if (vma->vm_flags & VM_SHARED)
684 return shmem_zero_setup(vma);
685 return 0;
686 }
687
write_full(struct file * file,const char __user * buf,size_t count,loff_t * ppos)688 static ssize_t write_full(struct file *file, const char __user *buf,
689 size_t count, loff_t *ppos)
690 {
691 return -ENOSPC;
692 }
693
694 /*
695 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
696 * can fopen() both devices with "a" now. This was previously impossible.
697 * -- SRB.
698 */
null_lseek(struct file * file,loff_t offset,int orig)699 static loff_t null_lseek(struct file *file, loff_t offset, int orig)
700 {
701 return file->f_pos = 0;
702 }
703
704 #if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM) || defined(CONFIG_DEVPORT)
705
706 /*
707 * The memory devices use the full 32/64 bits of the offset, and so we cannot
708 * check against negative addresses: they are ok. The return value is weird,
709 * though, in that case (0).
710 *
711 * also note that seeking relative to the "end of file" isn't supported:
712 * it has no meaning, so it returns -EINVAL.
713 */
memory_lseek(struct file * file,loff_t offset,int orig)714 static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
715 {
716 loff_t ret;
717
718 mutex_lock(&file_inode(file)->i_mutex);
719 switch (orig) {
720 case SEEK_CUR:
721 offset += file->f_pos;
722 case SEEK_SET:
723 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
724 if (IS_ERR_VALUE((unsigned long long)offset)) {
725 ret = -EOVERFLOW;
726 break;
727 }
728 file->f_pos = offset;
729 ret = file->f_pos;
730 force_successful_syscall_return();
731 break;
732 default:
733 ret = -EINVAL;
734 }
735 mutex_unlock(&file_inode(file)->i_mutex);
736 return ret;
737 }
738
739 #endif
740
741 #if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM) || defined(CONFIG_DEVPORT)
open_port(struct inode * inode,struct file * filp)742 static int open_port(struct inode *inode, struct file *filp)
743 {
744 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
745 }
746 #endif
747
748 #define zero_lseek null_lseek
749 #define full_lseek null_lseek
750 #define write_zero write_null
751 #define aio_write_zero aio_write_null
752 #define open_mem open_port
753 #define open_kmem open_mem
754
755 #ifdef CONFIG_DEVMEM
756 static const struct file_operations mem_fops = {
757 .llseek = memory_lseek,
758 .read = read_mem,
759 .write = write_mem,
760 .mmap = mmap_mem,
761 .open = open_mem,
762 .get_unmapped_area = get_unmapped_area_mem,
763 };
764 #endif
765
766 #ifdef CONFIG_DEVKMEM
767 static const struct file_operations kmem_fops = {
768 .llseek = memory_lseek,
769 .read = read_kmem,
770 .write = write_kmem,
771 .mmap = mmap_kmem,
772 .open = open_kmem,
773 .get_unmapped_area = get_unmapped_area_mem,
774 };
775 #endif
776
777 static const struct file_operations null_fops = {
778 .llseek = null_lseek,
779 .read = read_null,
780 .write = write_null,
781 .aio_read = aio_read_null,
782 .aio_write = aio_write_null,
783 .splice_write = splice_write_null,
784 };
785
786 #ifdef CONFIG_DEVPORT
787 static const struct file_operations port_fops = {
788 .llseek = memory_lseek,
789 .read = read_port,
790 .write = write_port,
791 .open = open_port,
792 };
793 #endif
794
795 static const struct file_operations zero_fops = {
796 .llseek = zero_lseek,
797 .read = new_sync_read,
798 .write = write_zero,
799 .read_iter = read_iter_zero,
800 .aio_write = aio_write_zero,
801 .mmap = mmap_zero,
802 };
803
804 /*
805 * capabilities for /dev/zero
806 * - permits private mappings, "copies" are taken of the source of zeros
807 * - no writeback happens
808 */
809 static struct backing_dev_info zero_bdi = {
810 .name = "char/mem",
811 .capabilities = BDI_CAP_MAP_COPY | BDI_CAP_NO_ACCT_AND_WRITEBACK,
812 };
813
814 static const struct file_operations full_fops = {
815 .llseek = full_lseek,
816 .read = new_sync_read,
817 .read_iter = read_iter_zero,
818 .write = write_full,
819 };
820
821 static const struct memdev {
822 const char *name;
823 umode_t mode;
824 const struct file_operations *fops;
825 struct backing_dev_info *dev_info;
826 } devlist[] = {
827 #ifdef CONFIG_DEVMEM
828 [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
829 #endif
830 #ifdef CONFIG_DEVKMEM
831 [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
832 #endif
833 [3] = { "null", 0666, &null_fops, NULL },
834 #ifdef CONFIG_DEVPORT
835 [4] = { "port", 0, &port_fops, NULL },
836 #endif
837 [5] = { "zero", 0666, &zero_fops, &zero_bdi },
838 [7] = { "full", 0666, &full_fops, NULL },
839 [8] = { "random", 0666, &random_fops, NULL },
840 [9] = { "urandom", 0666, &urandom_fops, NULL },
841 #ifdef CONFIG_PRINTK
842 [11] = { "kmsg", 0644, &kmsg_fops, NULL },
843 #endif
844 };
845
memory_open(struct inode * inode,struct file * filp)846 static int memory_open(struct inode *inode, struct file *filp)
847 {
848 int minor;
849 const struct memdev *dev;
850
851 minor = iminor(inode);
852 if (minor >= ARRAY_SIZE(devlist))
853 return -ENXIO;
854
855 dev = &devlist[minor];
856 if (!dev->fops)
857 return -ENXIO;
858
859 filp->f_op = dev->fops;
860 if (dev->dev_info)
861 filp->f_mapping->backing_dev_info = dev->dev_info;
862
863 /* Is /dev/mem or /dev/kmem ? */
864 if (dev->dev_info == &directly_mappable_cdev_bdi)
865 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
866
867 if (dev->fops->open)
868 return dev->fops->open(inode, filp);
869
870 return 0;
871 }
872
873 static const struct file_operations memory_fops = {
874 .open = memory_open,
875 .llseek = noop_llseek,
876 };
877
mem_devnode(struct device * dev,umode_t * mode)878 static char *mem_devnode(struct device *dev, umode_t *mode)
879 {
880 if (mode && devlist[MINOR(dev->devt)].mode)
881 *mode = devlist[MINOR(dev->devt)].mode;
882 return NULL;
883 }
884
885 static struct class *mem_class;
886
chr_dev_init(void)887 static int __init chr_dev_init(void)
888 {
889 int minor;
890 int err;
891
892 err = bdi_init(&zero_bdi);
893 if (err)
894 return err;
895
896 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
897 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
898
899 mem_class = class_create(THIS_MODULE, "mem");
900 if (IS_ERR(mem_class))
901 return PTR_ERR(mem_class);
902
903 mem_class->devnode = mem_devnode;
904 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
905 if (!devlist[minor].name)
906 continue;
907
908 /*
909 * Create /dev/port?
910 */
911 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
912 continue;
913
914 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
915 NULL, devlist[minor].name);
916 }
917
918 return tty_init();
919 }
920
921 fs_initcall(chr_dev_init);
922