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