blob: aaa9c24d4c14f706729e3e23279d21a227d17eb4 [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>
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008 * Shared /dev/zero mmapping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
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
Wu Fengguangf2223182009-12-14 17:58:07 -080037static inline unsigned long size_inside_page(unsigned long start,
38 unsigned long size)
39{
40 unsigned long sz;
41
42 if (-start & (PAGE_SIZE - 1))
43 sz = -start & (PAGE_SIZE - 1);
44 else
45 sz = PAGE_SIZE;
46
47 return min_t(unsigned long, sz, size);
48}
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
51 * Architectures vary in how they handle caching for addresses
52 * outside of main memory.
53 *
54 */
55static inline int uncached_access(struct file *file, unsigned long addr)
56{
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -070057#if defined(CONFIG_IA64)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 /*
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +010059 * On ia64, we ignore O_DSYNC because we cannot tolerate memory attribute aliases.
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 */
61 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
Ralf Baechle24e9d0b962007-07-10 17:32:56 +010062#elif defined(CONFIG_MIPS)
63 {
64 extern int __uncached_access(struct file *file,
65 unsigned long addr);
66
67 return __uncached_access(file, addr);
68 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#else
70 /*
71 * Accessing memory above the top the kernel knows about or through a file pointer
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +010072 * that was marked O_DSYNC will be done non-cached.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +010074 if (file->f_flags & O_DSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return 1;
76 return addr >= __pa(high_memory);
77#endif
78}
79
80#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
Bjorn Helgaas136939a2006-03-26 01:37:05 -080081static inline int valid_phys_addr_range(unsigned long addr, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Bjorn Helgaas136939a2006-03-26 01:37:05 -080083 if (addr + count > __pa(high_memory))
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return 0;
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return 1;
87}
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080088
Lennert Buytenhek06c67be2006-07-10 04:45:27 -070089static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080090{
91 return 1;
92}
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#endif
94
Ingo Molnard0926332008-07-18 00:26:59 +020095#ifdef CONFIG_STRICT_DEVMEM
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080096static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020097{
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080098 u64 from = ((u64)pfn) << PAGE_SHIFT;
99 u64 to = from + size;
100 u64 cursor = from;
Arjan van de Venae531c22008-04-24 23:40:47 +0200101
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800102 while (cursor < to) {
103 if (!devmem_is_allowed(pfn)) {
104 printk(KERN_INFO
105 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
Arjan van de Venae531c22008-04-24 23:40:47 +0200106 current->comm, from, to);
107 return 0;
108 }
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800109 cursor += PAGE_SIZE;
110 pfn++;
Arjan van de Venae531c22008-04-24 23:40:47 +0200111 }
112 return 1;
113}
114#else
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800115static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +0200116{
117 return 1;
118}
119#endif
120
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700121void __attribute__((weak)) unxlate_dev_mem_ptr(unsigned long phys, void *addr)
122{
123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/*
126 * This funcion reads the *physical* memory. The f_pos points directly to the
127 * memory location.
128 */
129static ssize_t read_mem(struct file * file, char __user * buf,
130 size_t count, loff_t *ppos)
131{
132 unsigned long p = *ppos;
133 ssize_t read, sz;
134 char *ptr;
135
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800136 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return -EFAULT;
138 read = 0;
139#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
140 /* we don't have page 0 mapped on sparc and m68k.. */
141 if (p < PAGE_SIZE) {
142 sz = PAGE_SIZE - p;
143 if (sz > count)
144 sz = count;
145 if (sz > 0) {
146 if (clear_user(buf, sz))
147 return -EFAULT;
148 buf += sz;
149 p += sz;
150 count -= sz;
151 read += sz;
152 }
153 }
154#endif
155
156 while (count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800157 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700159 if (!range_is_allowed(p >> PAGE_SHIFT, count))
160 return -EPERM;
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 /*
163 * On ia64 if a page has been mapped somewhere as
164 * uncached, then it must also be accessed uncached
165 * by the kernel or data corruption may occur
166 */
167 ptr = xlate_dev_mem_ptr(p);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700168 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700170
171 if (copy_to_user(buf, ptr, sz)) {
172 unxlate_dev_mem_ptr(p, ptr);
173 return -EFAULT;
174 }
175
176 unxlate_dev_mem_ptr(p, ptr);
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 buf += sz;
179 p += sz;
180 count -= sz;
181 read += sz;
182 }
183
184 *ppos += read;
185 return read;
186}
187
188static ssize_t write_mem(struct file * file, const char __user * buf,
189 size_t count, loff_t *ppos)
190{
191 unsigned long p = *ppos;
192 ssize_t written, sz;
193 unsigned long copied;
194 void *ptr;
195
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800196 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return -EFAULT;
198
199 written = 0;
200
201#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
202 /* we don't have page 0 mapped on sparc and m68k.. */
203 if (p < PAGE_SIZE) {
204 unsigned long sz = PAGE_SIZE - p;
205 if (sz > count)
206 sz = count;
207 /* Hmm. Do something? */
208 buf += sz;
209 p += sz;
210 count -= sz;
211 written += sz;
212 }
213#endif
214
215 while (count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800216 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700218 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
219 return -EPERM;
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 /*
222 * On ia64 if a page has been mapped somewhere as
223 * uncached, then it must also be accessed uncached
224 * by the kernel or data corruption may occur
225 */
226 ptr = xlate_dev_mem_ptr(p);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700227 if (!ptr) {
Jan Beulichc654d602006-03-25 03:07:31 -0800228 if (written)
229 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return -EFAULT;
231 }
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700232
233 copied = copy_from_user(ptr, buf, sz);
234 if (copied) {
235 written += sz - copied;
236 unxlate_dev_mem_ptr(p, ptr);
237 if (written)
238 break;
239 return -EFAULT;
240 }
241
242 unxlate_dev_mem_ptr(p, ptr);
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 buf += sz;
245 p += sz;
246 count -= sz;
247 written += sz;
248 }
249
250 *ppos += written;
251 return written;
252}
253
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700254int __attribute__((weak)) phys_mem_access_prot_allowed(struct file *file,
255 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
256{
257 return 1;
258}
259
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800260#ifndef __HAVE_PHYS_MEM_ACCESS_PROT
261static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
262 unsigned long size, pgprot_t vma_prot)
263{
264#ifdef pgprot_noncached
265 unsigned long offset = pfn << PAGE_SHIFT;
266
267 if (uncached_access(file, offset))
268 return pgprot_noncached(vma_prot);
269#endif
270 return vma_prot;
271}
272#endif
273
David Howells5da61852006-09-27 01:50:16 -0700274#ifndef CONFIG_MMU
275static unsigned long get_unmapped_area_mem(struct file *file,
276 unsigned long addr,
277 unsigned long len,
278 unsigned long pgoff,
279 unsigned long flags)
280{
281 if (!valid_mmap_phys_addr_range(pgoff, len))
282 return (unsigned long) -EINVAL;
Benjamin Herrenschmidt8a932582007-04-16 22:53:16 -0700283 return pgoff << PAGE_SHIFT;
David Howells5da61852006-09-27 01:50:16 -0700284}
285
286/* can't do an in-place private mapping if there's no MMU */
287static inline int private_mapping_ok(struct vm_area_struct *vma)
288{
289 return vma->vm_flags & VM_MAYSHARE;
290}
291#else
292#define get_unmapped_area_mem NULL
293
294static inline int private_mapping_ok(struct vm_area_struct *vma)
295{
296 return 1;
297}
298#endif
299
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400300static const struct vm_operations_struct mmap_mem_ops = {
Rik van Riel7ae8ed52008-07-23 21:27:07 -0700301#ifdef CONFIG_HAVE_IOREMAP_PROT
302 .access = generic_access_phys
303#endif
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700304};
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static int mmap_mem(struct file * file, struct vm_area_struct * vma)
307{
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800308 size_t size = vma->vm_end - vma->vm_start;
309
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700310 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800311 return -EINVAL;
312
David Howells5da61852006-09-27 01:50:16 -0700313 if (!private_mapping_ok(vma))
314 return -ENOSYS;
315
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800316 if (!range_is_allowed(vma->vm_pgoff, size))
317 return -EPERM;
318
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700319 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
320 &vma->vm_page_prot))
321 return -EINVAL;
322
Roland Dreier8b150472005-10-28 17:46:18 -0700323 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800324 size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700327 vma->vm_ops = &mmap_mem_ops;
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
330 if (remap_pfn_range(vma,
331 vma->vm_start,
332 vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800333 size,
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700334 vma->vm_page_prot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return -EAGAIN;
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return 0;
338}
339
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700340#ifdef CONFIG_DEVKMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
342{
Linus Torvalds4bb82552005-08-13 14:22:59 -0700343 unsigned long pfn;
344
Linus Torvalds6d3154c2007-01-22 08:53:24 -0800345 /* Turn a kernel-virtual address into a physical page frame */
346 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 /*
349 * RED-PEN: on some architectures there is more mapped memory
350 * than available in mem_map which pfn_valid checks
351 * for. Perhaps should add a new macro here.
352 *
353 * RED-PEN: vmalloc is not supported right now.
354 */
Linus Torvalds4bb82552005-08-13 14:22:59 -0700355 if (!pfn_valid(pfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return -EIO;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700357
358 vma->vm_pgoff = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return mmap_mem(file, vma);
360}
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700361#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700363#ifdef CONFIG_CRASH_DUMP
364/*
365 * Read memory corresponding to the old kernel.
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700366 */
Vivek Goyal315c2152005-06-25 14:58:24 -0700367static ssize_t read_oldmem(struct file *file, char __user *buf,
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700368 size_t count, loff_t *ppos)
369{
Vivek Goyal315c2152005-06-25 14:58:24 -0700370 unsigned long pfn, offset;
371 size_t read = 0, csize;
372 int rc = 0;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700373
Maneesh Soni72414d32005-06-25 14:58:28 -0700374 while (count) {
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700375 pfn = *ppos / PAGE_SIZE;
Vivek Goyal315c2152005-06-25 14:58:24 -0700376 if (pfn > saved_max_pfn)
377 return read;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700378
Vivek Goyal315c2152005-06-25 14:58:24 -0700379 offset = (unsigned long)(*ppos % PAGE_SIZE);
380 if (count > PAGE_SIZE - offset)
381 csize = PAGE_SIZE - offset;
382 else
383 csize = count;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700384
Vivek Goyal315c2152005-06-25 14:58:24 -0700385 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
386 if (rc < 0)
387 return rc;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700388 buf += csize;
389 *ppos += csize;
390 read += csize;
391 count -= csize;
392 }
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700393 return read;
394}
395#endif
396
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700397#ifdef CONFIG_DEVKMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398/*
399 * This function reads the *virtual* memory as seen by the kernel.
400 */
401static ssize_t read_kmem(struct file *file, char __user *buf,
402 size_t count, loff_t *ppos)
403{
404 unsigned long p = *ppos;
405 ssize_t low_count, read, sz;
406 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
407
408 read = 0;
409 if (p < (unsigned long) high_memory) {
410 low_count = count;
411 if (count > (unsigned long) high_memory - p)
412 low_count = (unsigned long) high_memory - p;
413
414#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
415 /* we don't have page 0 mapped on sparc and m68k.. */
416 if (p < PAGE_SIZE && low_count > 0) {
417 size_t tmp = PAGE_SIZE - p;
418 if (tmp > low_count) tmp = low_count;
419 if (clear_user(buf, tmp))
420 return -EFAULT;
421 buf += tmp;
422 p += tmp;
423 read += tmp;
424 low_count -= tmp;
425 count -= tmp;
426 }
427#endif
428 while (low_count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800429 sz = size_inside_page(p, low_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /*
432 * On ia64 if a page has been mapped somewhere as
433 * uncached, then it must also be accessed uncached
434 * by the kernel or data corruption may occur
435 */
436 kbuf = xlate_dev_kmem_ptr((char *)p);
437
438 if (copy_to_user(buf, kbuf, sz))
439 return -EFAULT;
440 buf += sz;
441 p += sz;
442 read += sz;
443 low_count -= sz;
444 count -= sz;
445 }
446 }
447
448 if (count > 0) {
449 kbuf = (char *)__get_free_page(GFP_KERNEL);
450 if (!kbuf)
451 return -ENOMEM;
452 while (count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800453 int len = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 len = vread(kbuf, (char *)p, len);
456 if (!len)
457 break;
458 if (copy_to_user(buf, kbuf, len)) {
459 free_page((unsigned long)kbuf);
460 return -EFAULT;
461 }
462 count -= len;
463 buf += len;
464 read += len;
465 p += len;
466 }
467 free_page((unsigned long)kbuf);
468 }
469 *ppos = p;
470 return read;
471}
472
473
474static inline ssize_t
475do_write_kmem(void *p, unsigned long realp, const char __user * buf,
476 size_t count, loff_t *ppos)
477{
478 ssize_t written, sz;
479 unsigned long copied;
480
481 written = 0;
482#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
483 /* we don't have page 0 mapped on sparc and m68k.. */
484 if (realp < PAGE_SIZE) {
485 unsigned long sz = PAGE_SIZE - realp;
486 if (sz > count)
487 sz = count;
488 /* Hmm. Do something? */
489 buf += sz;
490 p += sz;
491 realp += sz;
492 count -= sz;
493 written += sz;
494 }
495#endif
496
497 while (count > 0) {
498 char *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Wu Fengguangf2223182009-12-14 17:58:07 -0800500 sz = size_inside_page(realp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /*
503 * On ia64 if a page has been mapped somewhere as
504 * uncached, then it must also be accessed uncached
505 * by the kernel or data corruption may occur
506 */
507 ptr = xlate_dev_kmem_ptr(p);
508
509 copied = copy_from_user(ptr, buf, sz);
510 if (copied) {
Jan Beulichc654d602006-03-25 03:07:31 -0800511 written += sz - copied;
512 if (written)
513 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return -EFAULT;
515 }
516 buf += sz;
517 p += sz;
518 realp += sz;
519 count -= sz;
520 written += sz;
521 }
522
523 *ppos += written;
524 return written;
525}
526
527
528/*
529 * This function writes to the *virtual* memory as seen by the kernel.
530 */
531static ssize_t write_kmem(struct file * file, const char __user * buf,
532 size_t count, loff_t *ppos)
533{
534 unsigned long p = *ppos;
535 ssize_t wrote = 0;
536 ssize_t virtr = 0;
537 ssize_t written;
538 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
539
540 if (p < (unsigned long) high_memory) {
541
542 wrote = count;
543 if (count > (unsigned long) high_memory - p)
544 wrote = (unsigned long) high_memory - p;
545
546 written = do_write_kmem((void*)p, p, buf, wrote, ppos);
547 if (written != wrote)
548 return written;
549 wrote = written;
550 p += wrote;
551 buf += wrote;
552 count -= wrote;
553 }
554
555 if (count > 0) {
556 kbuf = (char *)__get_free_page(GFP_KERNEL);
557 if (!kbuf)
558 return wrote ? wrote : -ENOMEM;
559 while (count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800560 int len = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Wu Fengguang4ea2f432009-12-14 17:57:57 -0800562 written = copy_from_user(kbuf, buf, len);
563 if (written) {
564 if (wrote + virtr)
565 break;
566 free_page((unsigned long)kbuf);
567 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569 len = vwrite(kbuf, (char *)p, len);
570 count -= len;
571 buf += len;
572 virtr += len;
573 p += len;
574 }
575 free_page((unsigned long)kbuf);
576 }
577
578 *ppos = p;
579 return virtr + wrote;
580}
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700581#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Russell King4f911d62007-05-08 00:28:17 -0700583#ifdef CONFIG_DEVPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584static ssize_t read_port(struct file * file, char __user * buf,
585 size_t count, loff_t *ppos)
586{
587 unsigned long i = *ppos;
588 char __user *tmp = buf;
589
590 if (!access_ok(VERIFY_WRITE, buf, count))
591 return -EFAULT;
592 while (count-- > 0 && i < 65536) {
593 if (__put_user(inb(i),tmp) < 0)
594 return -EFAULT;
595 i++;
596 tmp++;
597 }
598 *ppos = i;
599 return tmp-buf;
600}
601
602static ssize_t write_port(struct file * file, const char __user * buf,
603 size_t count, loff_t *ppos)
604{
605 unsigned long i = *ppos;
606 const char __user * tmp = buf;
607
608 if (!access_ok(VERIFY_READ,buf,count))
609 return -EFAULT;
610 while (count-- > 0 && i < 65536) {
611 char c;
Jan Beulichc654d602006-03-25 03:07:31 -0800612 if (__get_user(c, tmp)) {
613 if (tmp > buf)
614 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return -EFAULT;
Jan Beulichc654d602006-03-25 03:07:31 -0800616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 outb(c,i);
618 i++;
619 tmp++;
620 }
621 *ppos = i;
622 return tmp-buf;
623}
624#endif
625
626static ssize_t read_null(struct file * file, char __user * buf,
627 size_t count, loff_t *ppos)
628{
629 return 0;
630}
631
632static ssize_t write_null(struct file * file, const char __user * buf,
633 size_t count, loff_t *ppos)
634{
635 return count;
636}
637
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200638static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
639 struct splice_desc *sd)
640{
641 return sd->len;
642}
643
644static ssize_t splice_write_null(struct pipe_inode_info *pipe,struct file *out,
645 loff_t *ppos, size_t len, unsigned int flags)
646{
647 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
648}
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650static ssize_t read_zero(struct file * file, char __user * buf,
651 size_t count, loff_t *ppos)
652{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700653 size_t written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 if (!count)
656 return 0;
657
658 if (!access_ok(VERIFY_WRITE, buf, count))
659 return -EFAULT;
660
Nick Piggin557ed1f2007-10-16 01:24:40 -0700661 written = 0;
662 while (count) {
663 unsigned long unwritten;
664 size_t chunk = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Nick Piggin557ed1f2007-10-16 01:24:40 -0700666 if (chunk > PAGE_SIZE)
667 chunk = PAGE_SIZE; /* Just for latency reasons */
Nikanth Karthikesanbb521c52009-09-23 15:57:09 -0700668 unwritten = __clear_user(buf, chunk);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700669 written += chunk - unwritten;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (unwritten)
Nick Piggin557ed1f2007-10-16 01:24:40 -0700671 break;
Linus Torvalds2b838682009-06-09 20:40:25 -0700672 if (signal_pending(current))
673 return written ? written : -ERESTARTSYS;
Nick Piggin557ed1f2007-10-16 01:24:40 -0700674 buf += chunk;
675 count -= chunk;
676 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return written ? written : -EFAULT;
679}
680
681static int mmap_zero(struct file * file, struct vm_area_struct * vma)
682{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700683#ifndef CONFIG_MMU
684 return -ENOSYS;
685#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (vma->vm_flags & VM_SHARED)
687 return shmem_zero_setup(vma);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700688 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691static ssize_t write_full(struct file * file, const char __user * buf,
692 size_t count, loff_t *ppos)
693{
694 return -ENOSPC;
695}
696
697/*
698 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
699 * can fopen() both devices with "a" now. This was previously impossible.
700 * -- SRB.
701 */
702
703static loff_t null_lseek(struct file * file, loff_t offset, int orig)
704{
705 return file->f_pos = 0;
706}
707
708/*
709 * The memory devices use the full 32/64 bits of the offset, and so we cannot
710 * check against negative addresses: they are ok. The return value is weird,
711 * though, in that case (0).
712 *
713 * also note that seeking relative to the "end of file" isn't supported:
714 * it has no meaning, so it returns -EINVAL.
715 */
716static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
717{
718 loff_t ret;
719
Josef Sipeka7113a92006-12-08 02:36:55 -0800720 mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 switch (orig) {
722 case 0:
723 file->f_pos = offset;
724 ret = file->f_pos;
725 force_successful_syscall_return();
726 break;
727 case 1:
728 file->f_pos += offset;
729 ret = file->f_pos;
730 force_successful_syscall_return();
731 break;
732 default:
733 ret = -EINVAL;
734 }
Josef Sipeka7113a92006-12-08 02:36:55 -0800735 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return ret;
737}
738
739static int open_port(struct inode * inode, struct file * filp)
740{
741 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
742}
743
744#define zero_lseek null_lseek
745#define full_lseek null_lseek
746#define write_zero write_null
747#define read_full read_zero
748#define open_mem open_port
749#define open_kmem open_mem
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700750#define open_oldmem open_mem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Arjan van de Ven62322d22006-07-03 00:24:21 -0700752static const struct file_operations mem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 .llseek = memory_lseek,
754 .read = read_mem,
755 .write = write_mem,
756 .mmap = mmap_mem,
757 .open = open_mem,
David Howells5da61852006-09-27 01:50:16 -0700758 .get_unmapped_area = get_unmapped_area_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759};
760
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700761#ifdef CONFIG_DEVKMEM
Arjan van de Ven62322d22006-07-03 00:24:21 -0700762static const struct file_operations kmem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 .llseek = memory_lseek,
764 .read = read_kmem,
765 .write = write_kmem,
766 .mmap = mmap_kmem,
767 .open = open_kmem,
David Howells5da61852006-09-27 01:50:16 -0700768 .get_unmapped_area = get_unmapped_area_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769};
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700770#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Arjan van de Ven62322d22006-07-03 00:24:21 -0700772static const struct file_operations null_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 .llseek = null_lseek,
774 .read = read_null,
775 .write = write_null,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200776 .splice_write = splice_write_null,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777};
778
Russell King4f911d62007-05-08 00:28:17 -0700779#ifdef CONFIG_DEVPORT
Arjan van de Ven62322d22006-07-03 00:24:21 -0700780static const struct file_operations port_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 .llseek = memory_lseek,
782 .read = read_port,
783 .write = write_port,
784 .open = open_port,
785};
786#endif
787
Arjan van de Ven62322d22006-07-03 00:24:21 -0700788static const struct file_operations zero_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 .llseek = zero_lseek,
790 .read = read_zero,
791 .write = write_zero,
792 .mmap = mmap_zero,
793};
794
David Howells5da61852006-09-27 01:50:16 -0700795/*
796 * capabilities for /dev/zero
797 * - permits private mappings, "copies" are taken of the source of zeros
798 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799static struct backing_dev_info zero_bdi = {
Jens Axboed9938312009-06-12 14:45:52 +0200800 .name = "char/mem",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 .capabilities = BDI_CAP_MAP_COPY,
802};
803
Arjan van de Ven62322d22006-07-03 00:24:21 -0700804static const struct file_operations full_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 .llseek = full_lseek,
806 .read = read_full,
807 .write = write_full,
808};
809
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700810#ifdef CONFIG_CRASH_DUMP
Arjan van de Ven62322d22006-07-03 00:24:21 -0700811static const struct file_operations oldmem_fops = {
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700812 .read = read_oldmem,
813 .open = open_oldmem,
814};
815#endif
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817static ssize_t kmsg_write(struct file * file, const char __user * buf,
818 size_t count, loff_t *ppos)
819{
820 char *tmp;
Guillaume Chazaraincd140a52006-01-08 01:02:43 -0800821 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 tmp = kmalloc(count + 1, GFP_KERNEL);
824 if (tmp == NULL)
825 return -ENOMEM;
826 ret = -EFAULT;
827 if (!copy_from_user(tmp, buf, count)) {
828 tmp[count] = 0;
829 ret = printk("%s", tmp);
Guillaume Chazaraincd140a52006-01-08 01:02:43 -0800830 if (ret > count)
831 /* printk can add a prefix */
832 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834 kfree(tmp);
835 return ret;
836}
837
Arjan van de Ven62322d22006-07-03 00:24:21 -0700838static const struct file_operations kmsg_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 .write = kmsg_write,
840};
841
Kay Sievers389e0cb2009-07-04 16:51:29 +0200842static const struct memdev {
843 const char *name;
Kay Sieverse454cea2009-09-18 23:01:12 +0200844 mode_t mode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200845 const struct file_operations *fops;
846 struct backing_dev_info *dev_info;
847} devlist[] = {
Kay Sieverse454cea2009-09-18 23:01:12 +0200848 [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700849#ifdef CONFIG_DEVKMEM
Kay Sieverse454cea2009-09-18 23:01:12 +0200850 [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700851#endif
Kay Sieverse454cea2009-09-18 23:01:12 +0200852 [3] = { "null", 0666, &null_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700853#ifdef CONFIG_DEVPORT
Kay Sieverse454cea2009-09-18 23:01:12 +0200854 [4] = { "port", 0, &port_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700855#endif
Kay Sieverse454cea2009-09-18 23:01:12 +0200856 [5] = { "zero", 0666, &zero_fops, &zero_bdi },
857 [7] = { "full", 0666, &full_fops, NULL },
858 [8] = { "random", 0666, &random_fops, NULL },
859 [9] = { "urandom", 0666, &urandom_fops, NULL },
860 [11] = { "kmsg", 0, &kmsg_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700861#ifdef CONFIG_CRASH_DUMP
Kay Sieverse454cea2009-09-18 23:01:12 +0200862 [12] = { "oldmem", 0, &oldmem_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700863#endif
864};
865
866static int memory_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200868 int minor;
869 const struct memdev *dev;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700870
Kay Sievers389e0cb2009-07-04 16:51:29 +0200871 minor = iminor(inode);
872 if (minor >= ARRAY_SIZE(devlist))
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200873 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700874
Kay Sievers389e0cb2009-07-04 16:51:29 +0200875 dev = &devlist[minor];
876 if (!dev->fops)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200877 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700878
Kay Sievers389e0cb2009-07-04 16:51:29 +0200879 filp->f_op = dev->fops;
880 if (dev->dev_info)
881 filp->f_mapping->backing_dev_info = dev->dev_info;
882
883 if (dev->fops->open)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200884 return dev->fops->open(inode, filp);
885
886 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Arjan van de Ven62322d22006-07-03 00:24:21 -0700889static const struct file_operations memory_fops = {
Kay Sievers389e0cb2009-07-04 16:51:29 +0200890 .open = memory_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891};
892
Kay Sieverse454cea2009-09-18 23:01:12 +0200893static char *mem_devnode(struct device *dev, mode_t *mode)
894{
895 if (mode && devlist[MINOR(dev->devt)].mode)
896 *mode = devlist[MINOR(dev->devt)].mode;
897 return NULL;
898}
899
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800900static struct class *mem_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902static int __init chr_dev_init(void)
903{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200904 int minor;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700905 int err;
906
907 err = bdi_init(&zero_bdi);
908 if (err)
909 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
912 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
913
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800914 mem_class = class_create(THIS_MODULE, "mem");
Kay Sieverse454cea2009-09-18 23:01:12 +0200915 mem_class->devnode = mem_devnode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200916 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
917 if (!devlist[minor].name)
918 continue;
919 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
920 NULL, devlist[minor].name);
921 }
Greg Kroah-Hartmanebf644c2006-07-25 17:13:31 -0700922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return 0;
924}
925
926fs_initcall(chr_dev_init);