blob: 83495885ada0e00fbf0ce33b13d03cc7ac746082 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -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 mmaping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/ptrace.h>
22#include <linux/device.h>
Vivek Goyal50b1fdb2005-06-25 14:58:23 -070023#include <linux/highmem.h>
24#include <linux/crash_dump.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/backing-dev.h>
Vivek Goyal315c2152005-06-25 14:58:24 -070026#include <linux/bootmem.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020027#include <linux/splice.h>
Linus Torvaldsb8a3ad52006-10-13 08:42:10 -070028#include <linux/pfn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <asm/uaccess.h>
31#include <asm/io.h>
32
33#ifdef CONFIG_IA64
34# include <linux/efi.h>
35#endif
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * Architectures vary in how they handle caching for addresses
39 * outside of main memory.
40 *
41 */
42static inline int uncached_access(struct file *file, unsigned long addr)
43{
Al Viroca5cd872007-10-29 04:31:16 +000044#if defined(__i386__) && !defined(__arch_um__)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 /*
46 * On the PPro and successors, the MTRRs are used to set
47 * memory types for physical addresses outside main memory,
48 * so blindly setting PCD or PWT on those pages is wrong.
49 * For Pentiums and earlier, the surround logic should disable
50 * caching for the high addresses through the KEN pin, but
51 * we maintain the tradition of paranoia in this code.
52 */
53 if (file->f_flags & O_SYNC)
54 return 1;
55 return !( test_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability) ||
56 test_bit(X86_FEATURE_K6_MTRR, boot_cpu_data.x86_capability) ||
57 test_bit(X86_FEATURE_CYRIX_ARR, boot_cpu_data.x86_capability) ||
58 test_bit(X86_FEATURE_CENTAUR_MCR, boot_cpu_data.x86_capability) )
59 && addr >= __pa(high_memory);
Al Viroca5cd872007-10-29 04:31:16 +000060#elif defined(__x86_64__) && !defined(__arch_um__)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 /*
62 * This is broken because it can generate memory type aliases,
63 * which can cause cache corruptions
64 * But it is only available for root and we have to be bug-to-bug
65 * compatible with i386.
66 */
67 if (file->f_flags & O_SYNC)
68 return 1;
69 /* same behaviour as i386. PAT always set to cached and MTRRs control the
70 caching behaviour.
71 Hopefully a full PAT implementation will fix that soon. */
72 return 0;
73#elif defined(CONFIG_IA64)
74 /*
75 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
76 */
77 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
Ralf Baechle24e9d0b962007-07-10 17:32:56 +010078#elif defined(CONFIG_MIPS)
79 {
80 extern int __uncached_access(struct file *file,
81 unsigned long addr);
82
83 return __uncached_access(file, addr);
84 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#else
86 /*
87 * Accessing memory above the top the kernel knows about or through a file pointer
88 * that was marked O_SYNC will be done non-cached.
89 */
90 if (file->f_flags & O_SYNC)
91 return 1;
92 return addr >= __pa(high_memory);
93#endif
94}
95
96#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
Bjorn Helgaas136939a2006-03-26 01:37:05 -080097static inline int valid_phys_addr_range(unsigned long addr, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Bjorn Helgaas136939a2006-03-26 01:37:05 -080099 if (addr + count > __pa(high_memory))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return 0;
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return 1;
103}
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800104
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700105static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800106{
107 return 1;
108}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#endif
110
Arjan van de Venae531c22008-04-24 23:40:47 +0200111#ifdef CONFIG_NONPROMISC_DEVMEM
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800112static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +0200113{
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800114 u64 from = ((u64)pfn) << PAGE_SHIFT;
115 u64 to = from + size;
116 u64 cursor = from;
Arjan van de Venae531c22008-04-24 23:40:47 +0200117
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800118 while (cursor < to) {
119 if (!devmem_is_allowed(pfn)) {
120 printk(KERN_INFO
121 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
Arjan van de Venae531c22008-04-24 23:40:47 +0200122 current->comm, from, to);
123 return 0;
124 }
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800125 cursor += PAGE_SIZE;
126 pfn++;
Arjan van de Venae531c22008-04-24 23:40:47 +0200127 }
128 return 1;
129}
130#else
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800131static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +0200132{
133 return 1;
134}
135#endif
136
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700137void __attribute__((weak)) unxlate_dev_mem_ptr(unsigned long phys, void *addr)
138{
139}
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/*
142 * This funcion reads the *physical* memory. The f_pos points directly to the
143 * memory location.
144 */
145static ssize_t read_mem(struct file * file, char __user * buf,
146 size_t count, loff_t *ppos)
147{
148 unsigned long p = *ppos;
149 ssize_t read, sz;
150 char *ptr;
151
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800152 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return -EFAULT;
154 read = 0;
155#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
156 /* we don't have page 0 mapped on sparc and m68k.. */
157 if (p < PAGE_SIZE) {
158 sz = PAGE_SIZE - p;
159 if (sz > count)
160 sz = count;
161 if (sz > 0) {
162 if (clear_user(buf, sz))
163 return -EFAULT;
164 buf += sz;
165 p += sz;
166 count -= sz;
167 read += sz;
168 }
169 }
170#endif
171
172 while (count > 0) {
173 /*
174 * Handle first page in case it's not aligned
175 */
176 if (-p & (PAGE_SIZE - 1))
177 sz = -p & (PAGE_SIZE - 1);
178 else
179 sz = PAGE_SIZE;
180
181 sz = min_t(unsigned long, sz, count);
182
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700183 if (!range_is_allowed(p >> PAGE_SHIFT, count))
184 return -EPERM;
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /*
187 * On ia64 if a page has been mapped somewhere as
188 * uncached, then it must also be accessed uncached
189 * by the kernel or data corruption may occur
190 */
191 ptr = xlate_dev_mem_ptr(p);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700192 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700194
195 if (copy_to_user(buf, ptr, sz)) {
196 unxlate_dev_mem_ptr(p, ptr);
197 return -EFAULT;
198 }
199
200 unxlate_dev_mem_ptr(p, ptr);
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 buf += sz;
203 p += sz;
204 count -= sz;
205 read += sz;
206 }
207
208 *ppos += read;
209 return read;
210}
211
212static ssize_t write_mem(struct file * file, const char __user * buf,
213 size_t count, loff_t *ppos)
214{
215 unsigned long p = *ppos;
216 ssize_t written, sz;
217 unsigned long copied;
218 void *ptr;
219
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800220 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return -EFAULT;
222
223 written = 0;
224
225#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
226 /* we don't have page 0 mapped on sparc and m68k.. */
227 if (p < PAGE_SIZE) {
228 unsigned long sz = PAGE_SIZE - p;
229 if (sz > count)
230 sz = count;
231 /* Hmm. Do something? */
232 buf += sz;
233 p += sz;
234 count -= sz;
235 written += sz;
236 }
237#endif
238
239 while (count > 0) {
240 /*
241 * Handle first page in case it's not aligned
242 */
243 if (-p & (PAGE_SIZE - 1))
244 sz = -p & (PAGE_SIZE - 1);
245 else
246 sz = PAGE_SIZE;
247
248 sz = min_t(unsigned long, sz, count);
249
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700250 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
251 return -EPERM;
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 /*
254 * On ia64 if a page has been mapped somewhere as
255 * uncached, then it must also be accessed uncached
256 * by the kernel or data corruption may occur
257 */
258 ptr = xlate_dev_mem_ptr(p);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700259 if (!ptr) {
Jan Beulichc654d602006-03-25 03:07:31 -0800260 if (written)
261 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return -EFAULT;
263 }
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700264
265 copied = copy_from_user(ptr, buf, sz);
266 if (copied) {
267 written += sz - copied;
268 unxlate_dev_mem_ptr(p, ptr);
269 if (written)
270 break;
271 return -EFAULT;
272 }
273
274 unxlate_dev_mem_ptr(p, ptr);
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 buf += sz;
277 p += sz;
278 count -= sz;
279 written += sz;
280 }
281
282 *ppos += written;
283 return written;
284}
285
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800286#ifndef __HAVE_PHYS_MEM_ACCESS_PROT
287static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
288 unsigned long size, pgprot_t vma_prot)
289{
290#ifdef pgprot_noncached
291 unsigned long offset = pfn << PAGE_SHIFT;
292
293 if (uncached_access(file, offset))
294 return pgprot_noncached(vma_prot);
295#endif
296 return vma_prot;
297}
298#endif
299
David Howells5da61852006-09-27 01:50:16 -0700300#ifndef CONFIG_MMU
301static unsigned long get_unmapped_area_mem(struct file *file,
302 unsigned long addr,
303 unsigned long len,
304 unsigned long pgoff,
305 unsigned long flags)
306{
307 if (!valid_mmap_phys_addr_range(pgoff, len))
308 return (unsigned long) -EINVAL;
Benjamin Herrenschmidt8a932582007-04-16 22:53:16 -0700309 return pgoff << PAGE_SHIFT;
David Howells5da61852006-09-27 01:50:16 -0700310}
311
312/* can't do an in-place private mapping if there's no MMU */
313static inline int private_mapping_ok(struct vm_area_struct *vma)
314{
315 return vma->vm_flags & VM_MAYSHARE;
316}
317#else
318#define get_unmapped_area_mem NULL
319
320static inline int private_mapping_ok(struct vm_area_struct *vma)
321{
322 return 1;
323}
324#endif
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326static int mmap_mem(struct file * file, struct vm_area_struct * vma)
327{
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800328 size_t size = vma->vm_end - vma->vm_start;
329
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700330 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800331 return -EINVAL;
332
David Howells5da61852006-09-27 01:50:16 -0700333 if (!private_mapping_ok(vma))
334 return -ENOSYS;
335
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800336 if (!range_is_allowed(vma->vm_pgoff, size))
337 return -EPERM;
338
Roland Dreier8b150472005-10-28 17:46:18 -0700339 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800340 size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
344 if (remap_pfn_range(vma,
345 vma->vm_start,
346 vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800347 size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 vma->vm_page_prot))
349 return -EAGAIN;
350 return 0;
351}
352
353static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
354{
Linus Torvalds4bb82552005-08-13 14:22:59 -0700355 unsigned long pfn;
356
Linus Torvalds6d3154c2007-01-22 08:53:24 -0800357 /* Turn a kernel-virtual address into a physical page frame */
358 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /*
361 * RED-PEN: on some architectures there is more mapped memory
362 * than available in mem_map which pfn_valid checks
363 * for. Perhaps should add a new macro here.
364 *
365 * RED-PEN: vmalloc is not supported right now.
366 */
Linus Torvalds4bb82552005-08-13 14:22:59 -0700367 if (!pfn_valid(pfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return -EIO;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700369
370 vma->vm_pgoff = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return mmap_mem(file, vma);
372}
373
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700374#ifdef CONFIG_CRASH_DUMP
375/*
376 * Read memory corresponding to the old kernel.
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700377 */
Vivek Goyal315c2152005-06-25 14:58:24 -0700378static ssize_t read_oldmem(struct file *file, char __user *buf,
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700379 size_t count, loff_t *ppos)
380{
Vivek Goyal315c2152005-06-25 14:58:24 -0700381 unsigned long pfn, offset;
382 size_t read = 0, csize;
383 int rc = 0;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700384
Maneesh Soni72414d32005-06-25 14:58:28 -0700385 while (count) {
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700386 pfn = *ppos / PAGE_SIZE;
Vivek Goyal315c2152005-06-25 14:58:24 -0700387 if (pfn > saved_max_pfn)
388 return read;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700389
Vivek Goyal315c2152005-06-25 14:58:24 -0700390 offset = (unsigned long)(*ppos % PAGE_SIZE);
391 if (count > PAGE_SIZE - offset)
392 csize = PAGE_SIZE - offset;
393 else
394 csize = count;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700395
Vivek Goyal315c2152005-06-25 14:58:24 -0700396 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
397 if (rc < 0)
398 return rc;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700399 buf += csize;
400 *ppos += csize;
401 read += csize;
402 count -= csize;
403 }
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700404 return read;
405}
406#endif
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408extern long vread(char *buf, char *addr, unsigned long count);
409extern long vwrite(char *buf, char *addr, unsigned long count);
410
411/*
412 * This function reads the *virtual* memory as seen by the kernel.
413 */
414static ssize_t read_kmem(struct file *file, char __user *buf,
415 size_t count, loff_t *ppos)
416{
417 unsigned long p = *ppos;
418 ssize_t low_count, read, sz;
419 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
420
421 read = 0;
422 if (p < (unsigned long) high_memory) {
423 low_count = count;
424 if (count > (unsigned long) high_memory - p)
425 low_count = (unsigned long) high_memory - p;
426
427#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
428 /* we don't have page 0 mapped on sparc and m68k.. */
429 if (p < PAGE_SIZE && low_count > 0) {
430 size_t tmp = PAGE_SIZE - p;
431 if (tmp > low_count) tmp = low_count;
432 if (clear_user(buf, tmp))
433 return -EFAULT;
434 buf += tmp;
435 p += tmp;
436 read += tmp;
437 low_count -= tmp;
438 count -= tmp;
439 }
440#endif
441 while (low_count > 0) {
442 /*
443 * Handle first page in case it's not aligned
444 */
445 if (-p & (PAGE_SIZE - 1))
446 sz = -p & (PAGE_SIZE - 1);
447 else
448 sz = PAGE_SIZE;
449
450 sz = min_t(unsigned long, sz, low_count);
451
452 /*
453 * On ia64 if a page has been mapped somewhere as
454 * uncached, then it must also be accessed uncached
455 * by the kernel or data corruption may occur
456 */
457 kbuf = xlate_dev_kmem_ptr((char *)p);
458
459 if (copy_to_user(buf, kbuf, sz))
460 return -EFAULT;
461 buf += sz;
462 p += sz;
463 read += sz;
464 low_count -= sz;
465 count -= sz;
466 }
467 }
468
469 if (count > 0) {
470 kbuf = (char *)__get_free_page(GFP_KERNEL);
471 if (!kbuf)
472 return -ENOMEM;
473 while (count > 0) {
474 int len = count;
475
476 if (len > PAGE_SIZE)
477 len = PAGE_SIZE;
478 len = vread(kbuf, (char *)p, len);
479 if (!len)
480 break;
481 if (copy_to_user(buf, kbuf, len)) {
482 free_page((unsigned long)kbuf);
483 return -EFAULT;
484 }
485 count -= len;
486 buf += len;
487 read += len;
488 p += len;
489 }
490 free_page((unsigned long)kbuf);
491 }
492 *ppos = p;
493 return read;
494}
495
496
497static inline ssize_t
498do_write_kmem(void *p, unsigned long realp, const char __user * buf,
499 size_t count, loff_t *ppos)
500{
501 ssize_t written, sz;
502 unsigned long copied;
503
504 written = 0;
505#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
506 /* we don't have page 0 mapped on sparc and m68k.. */
507 if (realp < PAGE_SIZE) {
508 unsigned long sz = PAGE_SIZE - realp;
509 if (sz > count)
510 sz = count;
511 /* Hmm. Do something? */
512 buf += sz;
513 p += sz;
514 realp += sz;
515 count -= sz;
516 written += sz;
517 }
518#endif
519
520 while (count > 0) {
521 char *ptr;
522 /*
523 * Handle first page in case it's not aligned
524 */
525 if (-realp & (PAGE_SIZE - 1))
526 sz = -realp & (PAGE_SIZE - 1);
527 else
528 sz = PAGE_SIZE;
529
530 sz = min_t(unsigned long, sz, count);
531
532 /*
533 * On ia64 if a page has been mapped somewhere as
534 * uncached, then it must also be accessed uncached
535 * by the kernel or data corruption may occur
536 */
537 ptr = xlate_dev_kmem_ptr(p);
538
539 copied = copy_from_user(ptr, buf, sz);
540 if (copied) {
Jan Beulichc654d602006-03-25 03:07:31 -0800541 written += sz - copied;
542 if (written)
543 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return -EFAULT;
545 }
546 buf += sz;
547 p += sz;
548 realp += sz;
549 count -= sz;
550 written += sz;
551 }
552
553 *ppos += written;
554 return written;
555}
556
557
558/*
559 * This function writes to the *virtual* memory as seen by the kernel.
560 */
561static ssize_t write_kmem(struct file * file, const char __user * buf,
562 size_t count, loff_t *ppos)
563{
564 unsigned long p = *ppos;
565 ssize_t wrote = 0;
566 ssize_t virtr = 0;
567 ssize_t written;
568 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
569
570 if (p < (unsigned long) high_memory) {
571
572 wrote = count;
573 if (count > (unsigned long) high_memory - p)
574 wrote = (unsigned long) high_memory - p;
575
576 written = do_write_kmem((void*)p, p, buf, wrote, ppos);
577 if (written != wrote)
578 return written;
579 wrote = written;
580 p += wrote;
581 buf += wrote;
582 count -= wrote;
583 }
584
585 if (count > 0) {
586 kbuf = (char *)__get_free_page(GFP_KERNEL);
587 if (!kbuf)
588 return wrote ? wrote : -ENOMEM;
589 while (count > 0) {
590 int len = count;
591
592 if (len > PAGE_SIZE)
593 len = PAGE_SIZE;
594 if (len) {
595 written = copy_from_user(kbuf, buf, len);
596 if (written) {
Jan Beulichc654d602006-03-25 03:07:31 -0800597 if (wrote + virtr)
598 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 free_page((unsigned long)kbuf);
Jan Beulichc654d602006-03-25 03:07:31 -0800600 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602 }
603 len = vwrite(kbuf, (char *)p, len);
604 count -= len;
605 buf += len;
606 virtr += len;
607 p += len;
608 }
609 free_page((unsigned long)kbuf);
610 }
611
612 *ppos = p;
613 return virtr + wrote;
614}
615
Russell King4f911d62007-05-08 00:28:17 -0700616#ifdef CONFIG_DEVPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617static ssize_t read_port(struct file * file, char __user * buf,
618 size_t count, loff_t *ppos)
619{
620 unsigned long i = *ppos;
621 char __user *tmp = buf;
622
623 if (!access_ok(VERIFY_WRITE, buf, count))
624 return -EFAULT;
625 while (count-- > 0 && i < 65536) {
626 if (__put_user(inb(i),tmp) < 0)
627 return -EFAULT;
628 i++;
629 tmp++;
630 }
631 *ppos = i;
632 return tmp-buf;
633}
634
635static ssize_t write_port(struct file * file, const char __user * buf,
636 size_t count, loff_t *ppos)
637{
638 unsigned long i = *ppos;
639 const char __user * tmp = buf;
640
641 if (!access_ok(VERIFY_READ,buf,count))
642 return -EFAULT;
643 while (count-- > 0 && i < 65536) {
644 char c;
Jan Beulichc654d602006-03-25 03:07:31 -0800645 if (__get_user(c, tmp)) {
646 if (tmp > buf)
647 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return -EFAULT;
Jan Beulichc654d602006-03-25 03:07:31 -0800649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 outb(c,i);
651 i++;
652 tmp++;
653 }
654 *ppos = i;
655 return tmp-buf;
656}
657#endif
658
659static ssize_t read_null(struct file * file, char __user * buf,
660 size_t count, loff_t *ppos)
661{
662 return 0;
663}
664
665static ssize_t write_null(struct file * file, const char __user * buf,
666 size_t count, loff_t *ppos)
667{
668 return count;
669}
670
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200671static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
672 struct splice_desc *sd)
673{
674 return sd->len;
675}
676
677static ssize_t splice_write_null(struct pipe_inode_info *pipe,struct file *out,
678 loff_t *ppos, size_t len, unsigned int flags)
679{
680 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
681}
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683static ssize_t read_zero(struct file * file, char __user * buf,
684 size_t count, loff_t *ppos)
685{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700686 size_t written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 if (!count)
689 return 0;
690
691 if (!access_ok(VERIFY_WRITE, buf, count))
692 return -EFAULT;
693
Nick Piggin557ed1f2007-10-16 01:24:40 -0700694 written = 0;
695 while (count) {
696 unsigned long unwritten;
697 size_t chunk = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Nick Piggin557ed1f2007-10-16 01:24:40 -0700699 if (chunk > PAGE_SIZE)
700 chunk = PAGE_SIZE; /* Just for latency reasons */
701 unwritten = clear_user(buf, chunk);
702 written += chunk - unwritten;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (unwritten)
Nick Piggin557ed1f2007-10-16 01:24:40 -0700704 break;
705 buf += chunk;
706 count -= chunk;
707 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return written ? written : -EFAULT;
710}
711
712static int mmap_zero(struct file * file, struct vm_area_struct * vma)
713{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700714#ifndef CONFIG_MMU
715 return -ENOSYS;
716#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (vma->vm_flags & VM_SHARED)
718 return shmem_zero_setup(vma);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700719 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722static ssize_t write_full(struct file * file, const char __user * buf,
723 size_t count, loff_t *ppos)
724{
725 return -ENOSPC;
726}
727
728/*
729 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
730 * can fopen() both devices with "a" now. This was previously impossible.
731 * -- SRB.
732 */
733
734static loff_t null_lseek(struct file * file, loff_t offset, int orig)
735{
736 return file->f_pos = 0;
737}
738
739/*
740 * The memory devices use the full 32/64 bits of the offset, and so we cannot
741 * check against negative addresses: they are ok. The return value is weird,
742 * though, in that case (0).
743 *
744 * also note that seeking relative to the "end of file" isn't supported:
745 * it has no meaning, so it returns -EINVAL.
746 */
747static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
748{
749 loff_t ret;
750
Josef Sipeka7113a92006-12-08 02:36:55 -0800751 mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 switch (orig) {
753 case 0:
754 file->f_pos = offset;
755 ret = file->f_pos;
756 force_successful_syscall_return();
757 break;
758 case 1:
759 file->f_pos += offset;
760 ret = file->f_pos;
761 force_successful_syscall_return();
762 break;
763 default:
764 ret = -EINVAL;
765 }
Josef Sipeka7113a92006-12-08 02:36:55 -0800766 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return ret;
768}
769
770static int open_port(struct inode * inode, struct file * filp)
771{
772 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
773}
774
775#define zero_lseek null_lseek
776#define full_lseek null_lseek
777#define write_zero write_null
778#define read_full read_zero
779#define open_mem open_port
780#define open_kmem open_mem
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700781#define open_oldmem open_mem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Arjan van de Ven62322d22006-07-03 00:24:21 -0700783static const struct file_operations mem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 .llseek = memory_lseek,
785 .read = read_mem,
786 .write = write_mem,
787 .mmap = mmap_mem,
788 .open = open_mem,
David Howells5da61852006-09-27 01:50:16 -0700789 .get_unmapped_area = get_unmapped_area_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790};
791
Arjan van de Ven62322d22006-07-03 00:24:21 -0700792static const struct file_operations kmem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 .llseek = memory_lseek,
794 .read = read_kmem,
795 .write = write_kmem,
796 .mmap = mmap_kmem,
797 .open = open_kmem,
David Howells5da61852006-09-27 01:50:16 -0700798 .get_unmapped_area = get_unmapped_area_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799};
800
Arjan van de Ven62322d22006-07-03 00:24:21 -0700801static const struct file_operations null_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 .llseek = null_lseek,
803 .read = read_null,
804 .write = write_null,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200805 .splice_write = splice_write_null,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806};
807
Russell King4f911d62007-05-08 00:28:17 -0700808#ifdef CONFIG_DEVPORT
Arjan van de Ven62322d22006-07-03 00:24:21 -0700809static const struct file_operations port_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 .llseek = memory_lseek,
811 .read = read_port,
812 .write = write_port,
813 .open = open_port,
814};
815#endif
816
Arjan van de Ven62322d22006-07-03 00:24:21 -0700817static const struct file_operations zero_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 .llseek = zero_lseek,
819 .read = read_zero,
820 .write = write_zero,
821 .mmap = mmap_zero,
822};
823
David Howells5da61852006-09-27 01:50:16 -0700824/*
825 * capabilities for /dev/zero
826 * - permits private mappings, "copies" are taken of the source of zeros
827 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828static struct backing_dev_info zero_bdi = {
829 .capabilities = BDI_CAP_MAP_COPY,
830};
831
Arjan van de Ven62322d22006-07-03 00:24:21 -0700832static const struct file_operations full_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 .llseek = full_lseek,
834 .read = read_full,
835 .write = write_full,
836};
837
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700838#ifdef CONFIG_CRASH_DUMP
Arjan van de Ven62322d22006-07-03 00:24:21 -0700839static const struct file_operations oldmem_fops = {
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700840 .read = read_oldmem,
841 .open = open_oldmem,
842};
843#endif
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845static ssize_t kmsg_write(struct file * file, const char __user * buf,
846 size_t count, loff_t *ppos)
847{
848 char *tmp;
Guillaume Chazaraincd140a52006-01-08 01:02:43 -0800849 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 tmp = kmalloc(count + 1, GFP_KERNEL);
852 if (tmp == NULL)
853 return -ENOMEM;
854 ret = -EFAULT;
855 if (!copy_from_user(tmp, buf, count)) {
856 tmp[count] = 0;
857 ret = printk("%s", tmp);
Guillaume Chazaraincd140a52006-01-08 01:02:43 -0800858 if (ret > count)
859 /* printk can add a prefix */
860 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862 kfree(tmp);
863 return ret;
864}
865
Arjan van de Ven62322d22006-07-03 00:24:21 -0700866static const struct file_operations kmsg_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 .write = kmsg_write,
868};
869
870static int memory_open(struct inode * inode, struct file * filp)
871{
872 switch (iminor(inode)) {
873 case 1:
874 filp->f_op = &mem_fops;
David Howells5da61852006-09-27 01:50:16 -0700875 filp->f_mapping->backing_dev_info =
876 &directly_mappable_cdev_bdi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 break;
878 case 2:
879 filp->f_op = &kmem_fops;
David Howells5da61852006-09-27 01:50:16 -0700880 filp->f_mapping->backing_dev_info =
881 &directly_mappable_cdev_bdi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 break;
883 case 3:
884 filp->f_op = &null_fops;
885 break;
Russell King4f911d62007-05-08 00:28:17 -0700886#ifdef CONFIG_DEVPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 case 4:
888 filp->f_op = &port_fops;
889 break;
890#endif
891 case 5:
892 filp->f_mapping->backing_dev_info = &zero_bdi;
893 filp->f_op = &zero_fops;
894 break;
895 case 7:
896 filp->f_op = &full_fops;
897 break;
898 case 8:
899 filp->f_op = &random_fops;
900 break;
901 case 9:
902 filp->f_op = &urandom_fops;
903 break;
904 case 11:
905 filp->f_op = &kmsg_fops;
906 break;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700907#ifdef CONFIG_CRASH_DUMP
908 case 12:
909 filp->f_op = &oldmem_fops;
910 break;
911#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 default:
913 return -ENXIO;
914 }
915 if (filp->f_op && filp->f_op->open)
916 return filp->f_op->open(inode,filp);
917 return 0;
918}
919
Arjan van de Ven62322d22006-07-03 00:24:21 -0700920static const struct file_operations memory_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 .open = memory_open, /* just a selector for the real open */
922};
923
924static const struct {
925 unsigned int minor;
926 char *name;
927 umode_t mode;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800928 const struct file_operations *fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929} devlist[] = { /* list of minor devices */
930 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
931 {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
932 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
Russell King4f911d62007-05-08 00:28:17 -0700933#ifdef CONFIG_DEVPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
935#endif
936 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
937 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
938 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
939 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
940 {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700941#ifdef CONFIG_CRASH_DUMP
942 {12,"oldmem", S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops},
943#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944};
945
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800946static struct class *mem_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948static int __init chr_dev_init(void)
949{
950 int i;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700951 int err;
952
953 err = bdi_init(&zero_bdi);
954 if (err)
955 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
958 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
959
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800960 mem_class = class_create(THIS_MODULE, "mem");
Greg Kroah-Hartman7c69ef72005-06-20 21:15:16 -0700961 for (i = 0; i < ARRAY_SIZE(devlist); i++)
Greg Kroah-Hartmanebf644c2006-07-25 17:13:31 -0700962 device_create(mem_class, NULL,
963 MKDEV(MEM_MAJOR, devlist[i].minor),
964 devlist[i].name);
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return 0;
967}
968
969fs_initcall(chr_dev_init);