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