blob: 01948961aafe6c20e59c0cbe6e7f28637ff09a0f [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
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/crash_dump.h>
25#include <linux/backing-dev.h>
26#include <linux/bootmem.h>
27#include <linux/splice.h>
28#include <linux/pfn.h>
29#include <linux/export.h>
30
31#include <asm/uaccess.h>
32#include <asm/io.h>
33
34#ifdef CONFIG_IA64
35# include <linux/efi.h>
36#endif
37
38static inline unsigned long size_inside_page(unsigned long start,
39 unsigned long size)
40{
41 unsigned long sz;
42
43 sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
44
45 return min(sz, size);
46}
47
48#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
49static inline int valid_phys_addr_range(unsigned long addr, size_t count)
50{
51 return addr + count <= __pa(high_memory);
52}
53
54static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
55{
56 return 1;
57}
58#endif
59
60#if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM)
61#ifdef CONFIG_STRICT_DEVMEM
62static inline int range_is_allowed(unsigned long pfn, unsigned long size)
63{
64 u64 from = ((u64)pfn) << PAGE_SHIFT;
65 u64 to = from + size;
66 u64 cursor = from;
67
68 while (cursor < to) {
69 if (!devmem_is_allowed(pfn)) {
70 printk(KERN_INFO
71 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
72 current->comm, from, to);
73 return 0;
74 }
75 cursor += PAGE_SIZE;
76 pfn++;
77 }
78 return 1;
79}
80#else
81static inline int range_is_allowed(unsigned long pfn, unsigned long size)
82{
83 return 1;
84}
85#endif
86#endif
87
88#ifdef CONFIG_DEVMEM
89void __weak unxlate_dev_mem_ptr(unsigned long phys, void *addr)
90{
91}
92
93/*
94 * This funcion reads the *physical* memory. The f_pos points directly to the
95 * memory location.
96 */
97static ssize_t read_mem(struct file *file, char __user *buf,
98 size_t count, loff_t *ppos)
99{
100 unsigned long p = *ppos;
101 ssize_t read, sz;
102 char *ptr;
103
104 if (!valid_phys_addr_range(p, count))
105 return -EFAULT;
106 read = 0;
107#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
108 /* we don't have page 0 mapped on sparc and m68k.. */
109 if (p < PAGE_SIZE) {
110 sz = size_inside_page(p, count);
111 if (sz > 0) {
112 if (clear_user(buf, sz))
113 return -EFAULT;
114 buf += sz;
115 p += sz;
116 count -= sz;
117 read += sz;
118 }
119 }
120#endif
121
122 while (count > 0) {
123 unsigned long remaining;
124
125 sz = size_inside_page(p, count);
126
127 if (!range_is_allowed(p >> PAGE_SHIFT, count))
128 return -EPERM;
129
130 /*
131 * On ia64 if a page has been mapped somewhere as uncached, then
132 * it must also be accessed uncached by the kernel or data
133 * corruption may occur.
134 */
135 ptr = xlate_dev_mem_ptr(p);
136 if (!ptr)
137 return -EFAULT;
138
139 remaining = copy_to_user(buf, ptr, sz);
140 unxlate_dev_mem_ptr(p, ptr);
141 if (remaining)
142 return -EFAULT;
143
144 buf += sz;
145 p += sz;
146 count -= sz;
147 read += sz;
148 }
149
150 *ppos += read;
151 return read;
152}
153
154static ssize_t write_mem(struct file *file, const char __user *buf,
155 size_t count, loff_t *ppos)
156{
157 unsigned long p = *ppos;
158 ssize_t written, sz;
159 unsigned long copied;
160 void *ptr;
161
162 if (!valid_phys_addr_range(p, count))
163 return -EFAULT;
164
165 written = 0;
166
167#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
168 /* we don't have page 0 mapped on sparc and m68k.. */
169 if (p < PAGE_SIZE) {
170 sz = size_inside_page(p, count);
171 /* Hmm. Do something? */
172 buf += sz;
173 p += sz;
174 count -= sz;
175 written += sz;
176 }
177#endif
178
179 while (count > 0) {
180 sz = size_inside_page(p, count);
181
182 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
183 return -EPERM;
184
185 /*
186 * On ia64 if a page has been mapped somewhere as uncached, then
187 * it must also be accessed uncached by the kernel or data
188 * corruption may occur.
189 */
190 ptr = xlate_dev_mem_ptr(p);
191 if (!ptr) {
192 if (written)
193 break;
194 return -EFAULT;
195 }
196
197 copied = copy_from_user(ptr, buf, sz);
198 unxlate_dev_mem_ptr(p, ptr);
199 if (copied) {
200 written += sz - copied;
201 if (written)
202 break;
203 return -EFAULT;
204 }
205
206 buf += sz;
207 p += sz;
208 count -= sz;
209 written += sz;
210 }
211
212 *ppos += written;
213 return written;
214}
215#endif /* CONFIG_DEVMEM */
216
217#if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM)
218
219int __weak phys_mem_access_prot_allowed(struct file *file,
220 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
221{
222 return 1;
223}
224
225#ifndef __HAVE_PHYS_MEM_ACCESS_PROT
226
227/*
228 * Architectures vary in how they handle caching for addresses
229 * outside of main memory.
230 *
231 */
232#ifdef pgprot_noncached
233static int uncached_access(struct file *file, unsigned long addr)
234{
235#if defined(CONFIG_IA64)
236 /*
237 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
238 * attribute aliases.
239 */
240 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
241#elif defined(CONFIG_MIPS)
242 {
243 extern int __uncached_access(struct file *file,
244 unsigned long addr);
245
246 return __uncached_access(file, addr);
247 }
248#else
249 /*
250 * Accessing memory above the top the kernel knows about or through a
251 * file pointer
252 * that was marked O_DSYNC will be done non-cached.
253 */
254 if (file->f_flags & O_DSYNC)
255 return 1;
256 return addr >= __pa(high_memory);
257#endif
258}
259#endif
260
261static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
262 unsigned long size, pgprot_t vma_prot)
263{
264#ifdef pgprot_noncached
265 unsigned long offset = pfn << PAGE_SHIFT;
266
267 if (uncached_access(file, offset))
268 return pgprot_noncached(vma_prot);
269#endif
270 return vma_prot;
271}
272#endif
273
274#ifndef CONFIG_MMU
275static unsigned long get_unmapped_area_mem(struct file *file,
276 unsigned long addr,
277 unsigned long len,
278 unsigned long pgoff,
279 unsigned long flags)
280{
281 if (!valid_mmap_phys_addr_range(pgoff, len))
282 return (unsigned long) -EINVAL;
283 return pgoff << PAGE_SHIFT;
284}
285
286/* can't do an in-place private mapping if there's no MMU */
287static inline int private_mapping_ok(struct vm_area_struct *vma)
288{
289 return vma->vm_flags & VM_MAYSHARE;
290}
291#else
292#define get_unmapped_area_mem NULL
293
294static inline int private_mapping_ok(struct vm_area_struct *vma)
295{
296 return 1;
297}
298#endif
299
300static const struct vm_operations_struct mmap_mem_ops = {
301#ifdef CONFIG_HAVE_IOREMAP_PROT
302 .access = generic_access_phys
303#endif
304};
305
306static int mmap_mem(struct file *file, struct vm_area_struct *vma)
307{
308 size_t size = vma->vm_end - vma->vm_start;
309
310 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
311 return -EINVAL;
312
313 if (!private_mapping_ok(vma))
314 return -ENOSYS;
315
316 if (!range_is_allowed(vma->vm_pgoff, size))
317 return -EPERM;
318
319 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
320 &vma->vm_page_prot))
321 return -EINVAL;
322
323 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
324 size,
325 vma->vm_page_prot);
326
327 vma->vm_ops = &mmap_mem_ops;
328
329 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
330 if (remap_pfn_range(vma,
331 vma->vm_start,
332 vma->vm_pgoff,
333 size,
334 vma->vm_page_prot)) {
335 return -EAGAIN;
336 }
337 return 0;
338}
339#endif /* CONFIG_DEVMEM */
340
341#ifdef CONFIG_DEVKMEM
342static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
343{
344 unsigned long pfn;
345
346 /* Turn a kernel-virtual address into a physical page frame */
347 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
348
349 /*
350 * RED-PEN: on some architectures there is more mapped memory than
351 * available in mem_map which pfn_valid checks for. Perhaps should add a
352 * new macro here.
353 *
354 * RED-PEN: vmalloc is not supported right now.
355 */
356 if (!pfn_valid(pfn))
357 return -EIO;
358
359 vma->vm_pgoff = pfn;
360 return mmap_mem(file, vma);
361}
362#endif
363
364#ifdef CONFIG_CRASH_DUMP
365/*
366 * Read memory corresponding to the old kernel.
367 */
368static ssize_t read_oldmem(struct file *file, char __user *buf,
369 size_t count, loff_t *ppos)
370{
371 unsigned long pfn, offset;
372 size_t read = 0, csize;
373 int rc = 0;
374
375 while (count) {
376 pfn = *ppos / PAGE_SIZE;
377 if (pfn > saved_max_pfn)
378 return read;
379
380 offset = (unsigned long)(*ppos % PAGE_SIZE);
381 if (count > PAGE_SIZE - offset)
382 csize = PAGE_SIZE - offset;
383 else
384 csize = count;
385
386 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
387 if (rc < 0)
388 return rc;
389 buf += csize;
390 *ppos += csize;
391 read += csize;
392 count -= csize;
393 }
394 return read;
395}
396#endif
397
398#ifdef CONFIG_DEVKMEM
399/*
400 * This function reads the *virtual* memory as seen by the kernel.
401 */
402static 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
478static 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 */
529static 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
581static 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
599static 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
623static ssize_t read_null(struct file *file, char __user *buf,
624 size_t count, loff_t *ppos)
625{
626 return 0;
627}
628
629static ssize_t write_null(struct file *file, const char __user *buf,
630 size_t count, loff_t *ppos)
631{
632 return count;
633}
634
635static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
636 struct splice_desc *sd)
637{
638 return sd->len;
639}
640
641static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
642 loff_t *ppos, size_t len, unsigned int flags)
643{
644 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
645}
646
647static ssize_t read_zero(struct file *file, char __user *buf,
648 size_t count, loff_t *ppos)
649{
650 size_t written;
651
652 if (!count)
653 return 0;
654
655 if (!access_ok(VERIFY_WRITE, buf, count))
656 return -EFAULT;
657
658 written = 0;
659 while (count) {
660 unsigned long unwritten;
661 size_t chunk = count;
662
663 if (chunk > PAGE_SIZE)
664 chunk = PAGE_SIZE; /* Just for latency reasons */
665 unwritten = __clear_user(buf, chunk);
666 written += chunk - unwritten;
667 if (unwritten)
668 break;
669 if (signal_pending(current))
670 return written ? written : -ERESTARTSYS;
671 buf += chunk;
672 count -= chunk;
673 cond_resched();
674 }
675 return written ? written : -EFAULT;
676}
677
678static 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
688static 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 */
699static 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 */
714static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
715{
716 loff_t ret;
717
718 mutex_lock(&file->f_path.dentry->d_inode->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 ((unsigned long long)offset >= ~0xFFFULL) {
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->f_path.dentry->d_inode->i_mutex);
736 return ret;
737}
738
739#endif
740
741#if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM) || defined(CONFIG_DEVPORT)
742static 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 read_full read_zero
752#define open_mem open_port
753#define open_kmem open_mem
754#define open_oldmem open_mem
755
756#ifdef CONFIG_DEVMEM
757static const struct file_operations mem_fops = {
758 .llseek = memory_lseek,
759 .read = read_mem,
760 .write = write_mem,
761 .mmap = mmap_mem,
762 .open = open_mem,
763 .get_unmapped_area = get_unmapped_area_mem,
764};
765#endif
766
767#ifdef CONFIG_DEVKMEM
768static const struct file_operations kmem_fops = {
769 .llseek = memory_lseek,
770 .read = read_kmem,
771 .write = write_kmem,
772 .mmap = mmap_kmem,
773 .open = open_kmem,
774 .get_unmapped_area = get_unmapped_area_mem,
775};
776#endif
777
778static const struct file_operations null_fops = {
779 .llseek = null_lseek,
780 .read = read_null,
781 .write = write_null,
782 .splice_write = splice_write_null,
783};
784
785#ifdef CONFIG_DEVPORT
786static const struct file_operations port_fops = {
787 .llseek = memory_lseek,
788 .read = read_port,
789 .write = write_port,
790 .open = open_port,
791};
792#endif
793
794static const struct file_operations zero_fops = {
795 .llseek = zero_lseek,
796 .read = read_zero,
797 .write = write_zero,
798 .mmap = mmap_zero,
799};
800
801/*
802 * capabilities for /dev/zero
803 * - permits private mappings, "copies" are taken of the source of zeros
804 * - no writeback happens
805 */
806static struct backing_dev_info zero_bdi = {
807 .name = "char/mem",
808 .capabilities = BDI_CAP_MAP_COPY | BDI_CAP_NO_ACCT_AND_WRITEBACK,
809};
810
811static const struct file_operations full_fops = {
812 .llseek = full_lseek,
813 .read = read_full,
814 .write = write_full,
815};
816
817#ifdef CONFIG_CRASH_DUMP
818static const struct file_operations oldmem_fops = {
819 .read = read_oldmem,
820 .open = open_oldmem,
821 .llseek = default_llseek,
822};
823#endif
824
825static ssize_t kmsg_writev(struct kiocb *iocb, const struct iovec *iv,
826 unsigned long count, loff_t pos)
827{
828 char *line, *p;
829 int i;
830 ssize_t ret = -EFAULT;
831 size_t len = iov_length(iv, count);
832
833 line = kmalloc(len + 1, GFP_KERNEL);
834 if (line == NULL)
835 return -ENOMEM;
836
837 /*
838 * copy all vectors into a single string, to ensure we do
839 * not interleave our log line with other printk calls
840 */
841 p = line;
842 for (i = 0; i < count; i++) {
843 if (copy_from_user(p, iv[i].iov_base, iv[i].iov_len))
844 goto out;
845 p += iv[i].iov_len;
846 }
847 p[0] = '\0';
848
849 ret = printk("%s", line);
850 /* printk can add a prefix */
851 if (ret > len)
852 ret = len;
853out:
854 kfree(line);
855 return ret;
856}
857
858static const struct file_operations kmsg_fops = {
859 .aio_write = kmsg_writev,
860 .llseek = noop_llseek,
861};
862
863static const struct memdev {
864 const char *name;
865 umode_t mode;
866 const struct file_operations *fops;
867 struct backing_dev_info *dev_info;
868} devlist[] = {
869#ifdef CONFIG_DEVMEM
870 [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
871#endif
872#ifdef CONFIG_DEVKMEM
873 [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
874#endif
875 [3] = { "null", 0666, &null_fops, NULL },
876#ifdef CONFIG_DEVPORT
877 [4] = { "port", 0, &port_fops, NULL },
878#endif
879 [5] = { "zero", 0666, &zero_fops, &zero_bdi },
880 [7] = { "full", 0666, &full_fops, NULL },
881 [8] = { "random", 0666, &random_fops, NULL },
882 [9] = { "urandom", 0666, &urandom_fops, NULL },
883 [11] = { "kmsg", 0, &kmsg_fops, NULL },
884#ifdef CONFIG_CRASH_DUMP
885 [12] = { "oldmem", 0, &oldmem_fops, NULL },
886#endif
887};
888
889static int memory_open(struct inode *inode, struct file *filp)
890{
891 int minor;
892 const struct memdev *dev;
893
894 minor = iminor(inode);
895 if (minor >= ARRAY_SIZE(devlist))
896 return -ENXIO;
897
898 dev = &devlist[minor];
899 if (!dev->fops)
900 return -ENXIO;
901
902 filp->f_op = dev->fops;
903 if (dev->dev_info)
904 filp->f_mapping->backing_dev_info = dev->dev_info;
905
906 /* Is /dev/mem or /dev/kmem ? */
907 if (dev->dev_info == &directly_mappable_cdev_bdi)
908 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
909
910 if (dev->fops->open)
911 return dev->fops->open(inode, filp);
912
913 return 0;
914}
915
916static const struct file_operations memory_fops = {
917 .open = memory_open,
918 .llseek = noop_llseek,
919};
920
921static char *mem_devnode(struct device *dev, umode_t *mode)
922{
923 #ifdef CONFIG_KLOCWORK
924 if(MINOR(dev->devt) >=ARRAY_SIZE(devlist))
925 return NULL;
926 #endif
927
928 if (mode && devlist[MINOR(dev->devt)].mode)
929 *mode = devlist[MINOR(dev->devt)].mode;
930 return NULL;
931}
932
933static struct class *mem_class;
934
935static int __init chr_dev_init(void)
936{
937 int minor;
938 int err;
939
940 err = bdi_init(&zero_bdi);
941 if (err)
942 return err;
943
944 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
945 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
946
947 mem_class = class_create(THIS_MODULE, "mem");
948 if (IS_ERR(mem_class))
949 return PTR_ERR(mem_class);
950
951 mem_class->devnode = mem_devnode;
952 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
953 if (!devlist[minor].name)
954 continue;
955 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
956 NULL, devlist[minor].name);
957 }
958
959 return tty_init();
960}
961
962fs_initcall(chr_dev_init);