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