blob: e83623ead4414efaf02a8d7e124f5719b18479c4 [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{
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -070044#if defined(CONFIG_IA64)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 /*
46 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
47 */
48 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
Ralf Baechle24e9d0b2007-07-10 17:32:56 +010049#elif defined(CONFIG_MIPS)
50 {
51 extern int __uncached_access(struct file *file,
52 unsigned long addr);
53
54 return __uncached_access(file, addr);
55 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#else
57 /*
58 * Accessing memory above the top the kernel knows about or through a file pointer
59 * that was marked O_SYNC will be done non-cached.
60 */
61 if (file->f_flags & O_SYNC)
62 return 1;
63 return addr >= __pa(high_memory);
64#endif
65}
66
67#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
Bjorn Helgaas136939a2006-03-26 01:37:05 -080068static inline int valid_phys_addr_range(unsigned long addr, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Bjorn Helgaas136939a2006-03-26 01:37:05 -080070 if (addr + count > __pa(high_memory))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return 0;
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return 1;
74}
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080075
Lennert Buytenhek06c67be2006-07-10 04:45:27 -070076static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080077{
78 return 1;
79}
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#endif
81
Arjan van de Venae531c22008-04-24 23:40:47 +020082#ifdef CONFIG_NONPROMISC_DEVMEM
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080083static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020084{
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080085 u64 from = ((u64)pfn) << PAGE_SHIFT;
86 u64 to = from + size;
87 u64 cursor = from;
Arjan van de Venae531c22008-04-24 23:40:47 +020088
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080089 while (cursor < to) {
90 if (!devmem_is_allowed(pfn)) {
91 printk(KERN_INFO
92 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
Arjan van de Venae531c22008-04-24 23:40:47 +020093 current->comm, from, to);
94 return 0;
95 }
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080096 cursor += PAGE_SIZE;
97 pfn++;
Arjan van de Venae531c22008-04-24 23:40:47 +020098 }
99 return 1;
100}
101#else
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800102static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +0200103{
104 return 1;
105}
106#endif
107
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700108void __attribute__((weak)) unxlate_dev_mem_ptr(unsigned long phys, void *addr)
109{
110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/*
113 * This funcion reads the *physical* memory. The f_pos points directly to the
114 * memory location.
115 */
116static ssize_t read_mem(struct file * file, char __user * buf,
117 size_t count, loff_t *ppos)
118{
119 unsigned long p = *ppos;
120 ssize_t read, sz;
121 char *ptr;
122
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800123 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return -EFAULT;
125 read = 0;
126#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
127 /* we don't have page 0 mapped on sparc and m68k.. */
128 if (p < PAGE_SIZE) {
129 sz = PAGE_SIZE - p;
130 if (sz > count)
131 sz = count;
132 if (sz > 0) {
133 if (clear_user(buf, sz))
134 return -EFAULT;
135 buf += sz;
136 p += sz;
137 count -= sz;
138 read += sz;
139 }
140 }
141#endif
142
143 while (count > 0) {
144 /*
145 * Handle first page in case it's not aligned
146 */
147 if (-p & (PAGE_SIZE - 1))
148 sz = -p & (PAGE_SIZE - 1);
149 else
150 sz = PAGE_SIZE;
151
152 sz = min_t(unsigned long, sz, count);
153
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700154 if (!range_is_allowed(p >> PAGE_SHIFT, count))
155 return -EPERM;
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /*
158 * On ia64 if a page has been mapped somewhere as
159 * uncached, then it must also be accessed uncached
160 * by the kernel or data corruption may occur
161 */
162 ptr = xlate_dev_mem_ptr(p);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700163 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700165
166 if (copy_to_user(buf, ptr, sz)) {
167 unxlate_dev_mem_ptr(p, ptr);
168 return -EFAULT;
169 }
170
171 unxlate_dev_mem_ptr(p, ptr);
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 buf += sz;
174 p += sz;
175 count -= sz;
176 read += sz;
177 }
178
179 *ppos += read;
180 return read;
181}
182
183static ssize_t write_mem(struct file * file, const char __user * buf,
184 size_t count, loff_t *ppos)
185{
186 unsigned long p = *ppos;
187 ssize_t written, sz;
188 unsigned long copied;
189 void *ptr;
190
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800191 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return -EFAULT;
193
194 written = 0;
195
196#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
197 /* we don't have page 0 mapped on sparc and m68k.. */
198 if (p < PAGE_SIZE) {
199 unsigned long sz = PAGE_SIZE - p;
200 if (sz > count)
201 sz = count;
202 /* Hmm. Do something? */
203 buf += sz;
204 p += sz;
205 count -= sz;
206 written += sz;
207 }
208#endif
209
210 while (count > 0) {
211 /*
212 * Handle first page in case it's not aligned
213 */
214 if (-p & (PAGE_SIZE - 1))
215 sz = -p & (PAGE_SIZE - 1);
216 else
217 sz = PAGE_SIZE;
218
219 sz = min_t(unsigned long, sz, count);
220
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700221 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
222 return -EPERM;
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 /*
225 * On ia64 if a page has been mapped somewhere as
226 * uncached, then it must also be accessed uncached
227 * by the kernel or data corruption may occur
228 */
229 ptr = xlate_dev_mem_ptr(p);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700230 if (!ptr) {
Jan Beulichc654d602006-03-25 03:07:31 -0800231 if (written)
232 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return -EFAULT;
234 }
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700235
236 copied = copy_from_user(ptr, buf, sz);
237 if (copied) {
238 written += sz - copied;
239 unxlate_dev_mem_ptr(p, ptr);
240 if (written)
241 break;
242 return -EFAULT;
243 }
244
245 unxlate_dev_mem_ptr(p, ptr);
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 buf += sz;
248 p += sz;
249 count -= sz;
250 written += sz;
251 }
252
253 *ppos += written;
254 return written;
255}
256
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700257int __attribute__((weak)) phys_mem_access_prot_allowed(struct file *file,
258 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
259{
260 return 1;
261}
262
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800263#ifndef __HAVE_PHYS_MEM_ACCESS_PROT
264static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
265 unsigned long size, pgprot_t vma_prot)
266{
267#ifdef pgprot_noncached
268 unsigned long offset = pfn << PAGE_SHIFT;
269
270 if (uncached_access(file, offset))
271 return pgprot_noncached(vma_prot);
272#endif
273 return vma_prot;
274}
275#endif
276
David Howells5da61852006-09-27 01:50:16 -0700277#ifndef CONFIG_MMU
278static unsigned long get_unmapped_area_mem(struct file *file,
279 unsigned long addr,
280 unsigned long len,
281 unsigned long pgoff,
282 unsigned long flags)
283{
284 if (!valid_mmap_phys_addr_range(pgoff, len))
285 return (unsigned long) -EINVAL;
Benjamin Herrenschmidt8a932582007-04-16 22:53:16 -0700286 return pgoff << PAGE_SHIFT;
David Howells5da61852006-09-27 01:50:16 -0700287}
288
289/* can't do an in-place private mapping if there's no MMU */
290static inline int private_mapping_ok(struct vm_area_struct *vma)
291{
292 return vma->vm_flags & VM_MAYSHARE;
293}
294#else
295#define get_unmapped_area_mem NULL
296
297static inline int private_mapping_ok(struct vm_area_struct *vma)
298{
299 return 1;
300}
301#endif
302
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700303void __attribute__((weak))
304map_devmem(unsigned long pfn, unsigned long len, pgprot_t prot)
305{
306 /* nothing. architectures can override. */
307}
308
309void __attribute__((weak))
310unmap_devmem(unsigned long pfn, unsigned long len, pgprot_t prot)
311{
312 /* nothing. architectures can override. */
313}
314
315static void mmap_mem_open(struct vm_area_struct *vma)
316{
317 map_devmem(vma->vm_pgoff, vma->vm_end - vma->vm_start,
318 vma->vm_page_prot);
319}
320
321static void mmap_mem_close(struct vm_area_struct *vma)
322{
323 unmap_devmem(vma->vm_pgoff, vma->vm_end - vma->vm_start,
324 vma->vm_page_prot);
325}
326
327static struct vm_operations_struct mmap_mem_ops = {
328 .open = mmap_mem_open,
329 .close = mmap_mem_close
330};
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static int mmap_mem(struct file * file, struct vm_area_struct * vma)
333{
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800334 size_t size = vma->vm_end - vma->vm_start;
335
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700336 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800337 return -EINVAL;
338
David Howells5da61852006-09-27 01:50:16 -0700339 if (!private_mapping_ok(vma))
340 return -ENOSYS;
341
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800342 if (!range_is_allowed(vma->vm_pgoff, size))
343 return -EPERM;
344
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700345 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
346 &vma->vm_page_prot))
347 return -EINVAL;
348
Roland Dreier8b150472005-10-28 17:46:18 -0700349 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800350 size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700353 vma->vm_ops = &mmap_mem_ops;
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
356 if (remap_pfn_range(vma,
357 vma->vm_start,
358 vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800359 size,
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700360 vma->vm_page_prot)) {
361 unmap_devmem(vma->vm_pgoff, size, vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return -EAGAIN;
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 0;
365}
366
367static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
368{
Linus Torvalds4bb82552005-08-13 14:22:59 -0700369 unsigned long pfn;
370
Linus Torvalds6d3154c2007-01-22 08:53:24 -0800371 /* Turn a kernel-virtual address into a physical page frame */
372 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /*
375 * RED-PEN: on some architectures there is more mapped memory
376 * than available in mem_map which pfn_valid checks
377 * for. Perhaps should add a new macro here.
378 *
379 * RED-PEN: vmalloc is not supported right now.
380 */
Linus Torvalds4bb82552005-08-13 14:22:59 -0700381 if (!pfn_valid(pfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return -EIO;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700383
384 vma->vm_pgoff = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return mmap_mem(file, vma);
386}
387
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700388#ifdef CONFIG_CRASH_DUMP
389/*
390 * Read memory corresponding to the old kernel.
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700391 */
Vivek Goyal315c2152005-06-25 14:58:24 -0700392static ssize_t read_oldmem(struct file *file, char __user *buf,
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700393 size_t count, loff_t *ppos)
394{
Vivek Goyal315c2152005-06-25 14:58:24 -0700395 unsigned long pfn, offset;
396 size_t read = 0, csize;
397 int rc = 0;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700398
Maneesh Soni72414d32005-06-25 14:58:28 -0700399 while (count) {
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700400 pfn = *ppos / PAGE_SIZE;
Vivek Goyal315c2152005-06-25 14:58:24 -0700401 if (pfn > saved_max_pfn)
402 return read;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700403
Vivek Goyal315c2152005-06-25 14:58:24 -0700404 offset = (unsigned long)(*ppos % PAGE_SIZE);
405 if (count > PAGE_SIZE - offset)
406 csize = PAGE_SIZE - offset;
407 else
408 csize = count;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700409
Vivek Goyal315c2152005-06-25 14:58:24 -0700410 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
411 if (rc < 0)
412 return rc;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700413 buf += csize;
414 *ppos += csize;
415 read += csize;
416 count -= csize;
417 }
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700418 return read;
419}
420#endif
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422extern long vread(char *buf, char *addr, unsigned long count);
423extern long vwrite(char *buf, char *addr, unsigned long count);
424
425/*
426 * This function reads the *virtual* memory as seen by the kernel.
427 */
428static ssize_t read_kmem(struct file *file, char __user *buf,
429 size_t count, loff_t *ppos)
430{
431 unsigned long p = *ppos;
432 ssize_t low_count, read, sz;
433 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
434
435 read = 0;
436 if (p < (unsigned long) high_memory) {
437 low_count = count;
438 if (count > (unsigned long) high_memory - p)
439 low_count = (unsigned long) high_memory - p;
440
441#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
442 /* we don't have page 0 mapped on sparc and m68k.. */
443 if (p < PAGE_SIZE && low_count > 0) {
444 size_t tmp = PAGE_SIZE - p;
445 if (tmp > low_count) tmp = low_count;
446 if (clear_user(buf, tmp))
447 return -EFAULT;
448 buf += tmp;
449 p += tmp;
450 read += tmp;
451 low_count -= tmp;
452 count -= tmp;
453 }
454#endif
455 while (low_count > 0) {
456 /*
457 * Handle first page in case it's not aligned
458 */
459 if (-p & (PAGE_SIZE - 1))
460 sz = -p & (PAGE_SIZE - 1);
461 else
462 sz = PAGE_SIZE;
463
464 sz = min_t(unsigned long, sz, low_count);
465
466 /*
467 * On ia64 if a page has been mapped somewhere as
468 * uncached, then it must also be accessed uncached
469 * by the kernel or data corruption may occur
470 */
471 kbuf = xlate_dev_kmem_ptr((char *)p);
472
473 if (copy_to_user(buf, kbuf, sz))
474 return -EFAULT;
475 buf += sz;
476 p += sz;
477 read += sz;
478 low_count -= sz;
479 count -= sz;
480 }
481 }
482
483 if (count > 0) {
484 kbuf = (char *)__get_free_page(GFP_KERNEL);
485 if (!kbuf)
486 return -ENOMEM;
487 while (count > 0) {
488 int len = count;
489
490 if (len > PAGE_SIZE)
491 len = PAGE_SIZE;
492 len = vread(kbuf, (char *)p, len);
493 if (!len)
494 break;
495 if (copy_to_user(buf, kbuf, len)) {
496 free_page((unsigned long)kbuf);
497 return -EFAULT;
498 }
499 count -= len;
500 buf += len;
501 read += len;
502 p += len;
503 }
504 free_page((unsigned long)kbuf);
505 }
506 *ppos = p;
507 return read;
508}
509
510
511static inline ssize_t
512do_write_kmem(void *p, unsigned long realp, const char __user * buf,
513 size_t count, loff_t *ppos)
514{
515 ssize_t written, sz;
516 unsigned long copied;
517
518 written = 0;
519#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
520 /* we don't have page 0 mapped on sparc and m68k.. */
521 if (realp < PAGE_SIZE) {
522 unsigned long sz = PAGE_SIZE - realp;
523 if (sz > count)
524 sz = count;
525 /* Hmm. Do something? */
526 buf += sz;
527 p += sz;
528 realp += sz;
529 count -= sz;
530 written += sz;
531 }
532#endif
533
534 while (count > 0) {
535 char *ptr;
536 /*
537 * Handle first page in case it's not aligned
538 */
539 if (-realp & (PAGE_SIZE - 1))
540 sz = -realp & (PAGE_SIZE - 1);
541 else
542 sz = PAGE_SIZE;
543
544 sz = min_t(unsigned long, sz, count);
545
546 /*
547 * On ia64 if a page has been mapped somewhere as
548 * uncached, then it must also be accessed uncached
549 * by the kernel or data corruption may occur
550 */
551 ptr = xlate_dev_kmem_ptr(p);
552
553 copied = copy_from_user(ptr, buf, sz);
554 if (copied) {
Jan Beulichc654d602006-03-25 03:07:31 -0800555 written += sz - copied;
556 if (written)
557 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return -EFAULT;
559 }
560 buf += sz;
561 p += sz;
562 realp += sz;
563 count -= sz;
564 written += sz;
565 }
566
567 *ppos += written;
568 return written;
569}
570
571
572/*
573 * This function writes to the *virtual* memory as seen by the kernel.
574 */
575static ssize_t write_kmem(struct file * file, const char __user * buf,
576 size_t count, loff_t *ppos)
577{
578 unsigned long p = *ppos;
579 ssize_t wrote = 0;
580 ssize_t virtr = 0;
581 ssize_t written;
582 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
583
584 if (p < (unsigned long) high_memory) {
585
586 wrote = count;
587 if (count > (unsigned long) high_memory - p)
588 wrote = (unsigned long) high_memory - p;
589
590 written = do_write_kmem((void*)p, p, buf, wrote, ppos);
591 if (written != wrote)
592 return written;
593 wrote = written;
594 p += wrote;
595 buf += wrote;
596 count -= wrote;
597 }
598
599 if (count > 0) {
600 kbuf = (char *)__get_free_page(GFP_KERNEL);
601 if (!kbuf)
602 return wrote ? wrote : -ENOMEM;
603 while (count > 0) {
604 int len = count;
605
606 if (len > PAGE_SIZE)
607 len = PAGE_SIZE;
608 if (len) {
609 written = copy_from_user(kbuf, buf, len);
610 if (written) {
Jan Beulichc654d602006-03-25 03:07:31 -0800611 if (wrote + virtr)
612 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 free_page((unsigned long)kbuf);
Jan Beulichc654d602006-03-25 03:07:31 -0800614 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616 }
617 len = vwrite(kbuf, (char *)p, len);
618 count -= len;
619 buf += len;
620 virtr += len;
621 p += len;
622 }
623 free_page((unsigned long)kbuf);
624 }
625
626 *ppos = p;
627 return virtr + wrote;
628}
629
Russell King4f911d62007-05-08 00:28:17 -0700630#ifdef CONFIG_DEVPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631static ssize_t read_port(struct file * file, char __user * buf,
632 size_t count, loff_t *ppos)
633{
634 unsigned long i = *ppos;
635 char __user *tmp = buf;
636
637 if (!access_ok(VERIFY_WRITE, buf, count))
638 return -EFAULT;
639 while (count-- > 0 && i < 65536) {
640 if (__put_user(inb(i),tmp) < 0)
641 return -EFAULT;
642 i++;
643 tmp++;
644 }
645 *ppos = i;
646 return tmp-buf;
647}
648
649static ssize_t write_port(struct file * file, const char __user * buf,
650 size_t count, loff_t *ppos)
651{
652 unsigned long i = *ppos;
653 const char __user * tmp = buf;
654
655 if (!access_ok(VERIFY_READ,buf,count))
656 return -EFAULT;
657 while (count-- > 0 && i < 65536) {
658 char c;
Jan Beulichc654d602006-03-25 03:07:31 -0800659 if (__get_user(c, tmp)) {
660 if (tmp > buf)
661 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return -EFAULT;
Jan Beulichc654d602006-03-25 03:07:31 -0800663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 outb(c,i);
665 i++;
666 tmp++;
667 }
668 *ppos = i;
669 return tmp-buf;
670}
671#endif
672
673static ssize_t read_null(struct file * file, char __user * buf,
674 size_t count, loff_t *ppos)
675{
676 return 0;
677}
678
679static ssize_t write_null(struct file * file, const char __user * buf,
680 size_t count, loff_t *ppos)
681{
682 return count;
683}
684
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200685static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
686 struct splice_desc *sd)
687{
688 return sd->len;
689}
690
691static ssize_t splice_write_null(struct pipe_inode_info *pipe,struct file *out,
692 loff_t *ppos, size_t len, unsigned int flags)
693{
694 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
695}
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697static ssize_t read_zero(struct file * file, char __user * buf,
698 size_t count, loff_t *ppos)
699{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700700 size_t written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 if (!count)
703 return 0;
704
705 if (!access_ok(VERIFY_WRITE, buf, count))
706 return -EFAULT;
707
Nick Piggin557ed1f2007-10-16 01:24:40 -0700708 written = 0;
709 while (count) {
710 unsigned long unwritten;
711 size_t chunk = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Nick Piggin557ed1f2007-10-16 01:24:40 -0700713 if (chunk > PAGE_SIZE)
714 chunk = PAGE_SIZE; /* Just for latency reasons */
715 unwritten = clear_user(buf, chunk);
716 written += chunk - unwritten;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (unwritten)
Nick Piggin557ed1f2007-10-16 01:24:40 -0700718 break;
719 buf += chunk;
720 count -= chunk;
721 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return written ? written : -EFAULT;
724}
725
726static int mmap_zero(struct file * file, struct vm_area_struct * vma)
727{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700728#ifndef CONFIG_MMU
729 return -ENOSYS;
730#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (vma->vm_flags & VM_SHARED)
732 return shmem_zero_setup(vma);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700733 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736static ssize_t write_full(struct file * file, const char __user * buf,
737 size_t count, loff_t *ppos)
738{
739 return -ENOSPC;
740}
741
742/*
743 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
744 * can fopen() both devices with "a" now. This was previously impossible.
745 * -- SRB.
746 */
747
748static loff_t null_lseek(struct file * file, loff_t offset, int orig)
749{
750 return file->f_pos = 0;
751}
752
753/*
754 * The memory devices use the full 32/64 bits of the offset, and so we cannot
755 * check against negative addresses: they are ok. The return value is weird,
756 * though, in that case (0).
757 *
758 * also note that seeking relative to the "end of file" isn't supported:
759 * it has no meaning, so it returns -EINVAL.
760 */
761static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
762{
763 loff_t ret;
764
Josef Sipeka7113a92006-12-08 02:36:55 -0800765 mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 switch (orig) {
767 case 0:
768 file->f_pos = offset;
769 ret = file->f_pos;
770 force_successful_syscall_return();
771 break;
772 case 1:
773 file->f_pos += offset;
774 ret = file->f_pos;
775 force_successful_syscall_return();
776 break;
777 default:
778 ret = -EINVAL;
779 }
Josef Sipeka7113a92006-12-08 02:36:55 -0800780 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return ret;
782}
783
784static int open_port(struct inode * inode, struct file * filp)
785{
786 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
787}
788
789#define zero_lseek null_lseek
790#define full_lseek null_lseek
791#define write_zero write_null
792#define read_full read_zero
793#define open_mem open_port
794#define open_kmem open_mem
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700795#define open_oldmem open_mem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Arjan van de Ven62322d22006-07-03 00:24:21 -0700797static const struct file_operations mem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 .llseek = memory_lseek,
799 .read = read_mem,
800 .write = write_mem,
801 .mmap = mmap_mem,
802 .open = open_mem,
David Howells5da61852006-09-27 01:50:16 -0700803 .get_unmapped_area = get_unmapped_area_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804};
805
Arjan van de Ven62322d22006-07-03 00:24:21 -0700806static const struct file_operations kmem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 .llseek = memory_lseek,
808 .read = read_kmem,
809 .write = write_kmem,
810 .mmap = mmap_kmem,
811 .open = open_kmem,
David Howells5da61852006-09-27 01:50:16 -0700812 .get_unmapped_area = get_unmapped_area_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813};
814
Arjan van de Ven62322d22006-07-03 00:24:21 -0700815static const struct file_operations null_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 .llseek = null_lseek,
817 .read = read_null,
818 .write = write_null,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200819 .splice_write = splice_write_null,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820};
821
Russell King4f911d62007-05-08 00:28:17 -0700822#ifdef CONFIG_DEVPORT
Arjan van de Ven62322d22006-07-03 00:24:21 -0700823static const struct file_operations port_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 .llseek = memory_lseek,
825 .read = read_port,
826 .write = write_port,
827 .open = open_port,
828};
829#endif
830
Arjan van de Ven62322d22006-07-03 00:24:21 -0700831static const struct file_operations zero_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 .llseek = zero_lseek,
833 .read = read_zero,
834 .write = write_zero,
835 .mmap = mmap_zero,
836};
837
David Howells5da61852006-09-27 01:50:16 -0700838/*
839 * capabilities for /dev/zero
840 * - permits private mappings, "copies" are taken of the source of zeros
841 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842static struct backing_dev_info zero_bdi = {
843 .capabilities = BDI_CAP_MAP_COPY,
844};
845
Arjan van de Ven62322d22006-07-03 00:24:21 -0700846static const struct file_operations full_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 .llseek = full_lseek,
848 .read = read_full,
849 .write = write_full,
850};
851
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700852#ifdef CONFIG_CRASH_DUMP
Arjan van de Ven62322d22006-07-03 00:24:21 -0700853static const struct file_operations oldmem_fops = {
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700854 .read = read_oldmem,
855 .open = open_oldmem,
856};
857#endif
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859static ssize_t kmsg_write(struct file * file, const char __user * buf,
860 size_t count, loff_t *ppos)
861{
862 char *tmp;
Guillaume Chazaraincd140a52006-01-08 01:02:43 -0800863 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 tmp = kmalloc(count + 1, GFP_KERNEL);
866 if (tmp == NULL)
867 return -ENOMEM;
868 ret = -EFAULT;
869 if (!copy_from_user(tmp, buf, count)) {
870 tmp[count] = 0;
871 ret = printk("%s", tmp);
Guillaume Chazaraincd140a52006-01-08 01:02:43 -0800872 if (ret > count)
873 /* printk can add a prefix */
874 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876 kfree(tmp);
877 return ret;
878}
879
Arjan van de Ven62322d22006-07-03 00:24:21 -0700880static const struct file_operations kmsg_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 .write = kmsg_write,
882};
883
884static int memory_open(struct inode * inode, struct file * filp)
885{
886 switch (iminor(inode)) {
887 case 1:
888 filp->f_op = &mem_fops;
David Howells5da61852006-09-27 01:50:16 -0700889 filp->f_mapping->backing_dev_info =
890 &directly_mappable_cdev_bdi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 break;
892 case 2:
893 filp->f_op = &kmem_fops;
David Howells5da61852006-09-27 01:50:16 -0700894 filp->f_mapping->backing_dev_info =
895 &directly_mappable_cdev_bdi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 break;
897 case 3:
898 filp->f_op = &null_fops;
899 break;
Russell King4f911d62007-05-08 00:28:17 -0700900#ifdef CONFIG_DEVPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 case 4:
902 filp->f_op = &port_fops;
903 break;
904#endif
905 case 5:
906 filp->f_mapping->backing_dev_info = &zero_bdi;
907 filp->f_op = &zero_fops;
908 break;
909 case 7:
910 filp->f_op = &full_fops;
911 break;
912 case 8:
913 filp->f_op = &random_fops;
914 break;
915 case 9:
916 filp->f_op = &urandom_fops;
917 break;
918 case 11:
919 filp->f_op = &kmsg_fops;
920 break;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700921#ifdef CONFIG_CRASH_DUMP
922 case 12:
923 filp->f_op = &oldmem_fops;
924 break;
925#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 default:
927 return -ENXIO;
928 }
929 if (filp->f_op && filp->f_op->open)
930 return filp->f_op->open(inode,filp);
931 return 0;
932}
933
Arjan van de Ven62322d22006-07-03 00:24:21 -0700934static const struct file_operations memory_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 .open = memory_open, /* just a selector for the real open */
936};
937
938static const struct {
939 unsigned int minor;
940 char *name;
941 umode_t mode;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800942 const struct file_operations *fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943} devlist[] = { /* list of minor devices */
944 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
945 {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
946 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
Russell King4f911d62007-05-08 00:28:17 -0700947#ifdef CONFIG_DEVPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
949#endif
950 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
951 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
952 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
953 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
954 {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700955#ifdef CONFIG_CRASH_DUMP
956 {12,"oldmem", S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops},
957#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958};
959
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800960static struct class *mem_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962static int __init chr_dev_init(void)
963{
964 int i;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700965 int err;
966
967 err = bdi_init(&zero_bdi);
968 if (err)
969 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
972 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
973
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800974 mem_class = class_create(THIS_MODULE, "mem");
Greg Kroah-Hartman7c69ef72005-06-20 21:15:16 -0700975 for (i = 0; i < ARRAY_SIZE(devlist); i++)
Greg Kroah-Hartmanebf644c2006-07-25 17:13:31 -0700976 device_create(mem_class, NULL,
977 MKDEV(MEM_MAJOR, devlist[i].minor),
978 devlist[i].name);
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return 0;
981}
982
983fs_initcall(chr_dev_init);