blob: 67e19b63199c0207316f6ee749e93ee2e4ef40c0 [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 *
Andrew Mortond7d4d842010-03-10 15:21:52 -08006 * Added devfs support.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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>
Paul Gortmaker66300e62011-07-10 12:14:53 -040029#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <asm/uaccess.h>
32#include <asm/io.h>
33
34#ifdef CONFIG_IA64
35# include <linux/efi.h>
36#endif
37
Wu Fengguangf2223182009-12-14 17:58:07 -080038static inline unsigned long size_inside_page(unsigned long start,
39 unsigned long size)
40{
41 unsigned long sz;
42
Wu Fengguang7fabadd2009-12-14 17:58:09 -080043 sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
Wu Fengguangf2223182009-12-14 17:58:07 -080044
Wu Fengguang7fabadd2009-12-14 17:58:09 -080045 return min(sz, size);
Wu Fengguangf2223182009-12-14 17:58:07 -080046}
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
Bjorn Helgaas136939a2006-03-26 01:37:05 -080049static inline int valid_phys_addr_range(unsigned long addr, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Changli Gaocfaf3462011-03-23 16:42:58 -070051 return addr + count <= __pa(high_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080053
Lennert Buytenhek06c67be2006-07-10 04:45:27 -070054static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080055{
56 return 1;
57}
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#endif
59
Robert Love1fcada32008-04-29 16:44:10 -040060#if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM)
Ingo Molnard0926332008-07-18 00:26:59 +020061#ifdef CONFIG_STRICT_DEVMEM
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080062static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020063{
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080064 u64 from = ((u64)pfn) << PAGE_SHIFT;
65 u64 to = from + size;
66 u64 cursor = from;
Arjan van de Venae531c22008-04-24 23:40:47 +020067
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080068 while (cursor < to) {
69 if (!devmem_is_allowed(pfn)) {
70 printk(KERN_INFO
71 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
Arjan van de Venae531c22008-04-24 23:40:47 +020072 current->comm, from, to);
73 return 0;
74 }
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080075 cursor += PAGE_SIZE;
76 pfn++;
Arjan van de Venae531c22008-04-24 23:40:47 +020077 }
78 return 1;
79}
80#else
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080081static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020082{
83 return 1;
84}
85#endif
Robert Love1fcada32008-04-29 16:44:10 -040086#endif
Arjan van de Venae531c22008-04-24 23:40:47 +020087
Robert Love1fcada32008-04-29 16:44:10 -040088#ifdef CONFIG_DEVMEM
Andrew Mortond7d4d842010-03-10 15:21:52 -080089void __weak unxlate_dev_mem_ptr(unsigned long phys, void *addr)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -070090{
91}
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/*
Andrew Mortond7d4d842010-03-10 15:21:52 -080094 * This funcion reads the *physical* memory. The f_pos points directly to the
95 * memory location.
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
Andrew Mortond7d4d842010-03-10 15:21:52 -080097static ssize_t read_mem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 size_t count, loff_t *ppos)
99{
100 unsigned long p = *ppos;
101 ssize_t read, sz;
102 char *ptr;
103
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800104 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return -EFAULT;
106 read = 0;
107#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
108 /* we don't have page 0 mapped on sparc and m68k.. */
109 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800110 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (sz > 0) {
112 if (clear_user(buf, sz))
113 return -EFAULT;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800114 buf += sz;
115 p += sz;
116 count -= sz;
117 read += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119 }
120#endif
121
122 while (count > 0) {
Wu Fengguangfa29e972009-12-14 17:58:08 -0800123 unsigned long remaining;
124
Wu Fengguangf2223182009-12-14 17:58:07 -0800125 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700127 if (!range_is_allowed(p >> PAGE_SHIFT, count))
128 return -EPERM;
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800131 * On ia64 if a page has been mapped somewhere as uncached, then
132 * it must also be accessed uncached by the kernel or data
133 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 */
135 ptr = xlate_dev_mem_ptr(p);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700136 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700138
Wu Fengguangfa29e972009-12-14 17:58:08 -0800139 remaining = copy_to_user(buf, ptr, sz);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700140 unxlate_dev_mem_ptr(p, ptr);
Wu Fengguangfa29e972009-12-14 17:58:08 -0800141 if (remaining)
142 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 buf += sz;
145 p += sz;
146 count -= sz;
147 read += sz;
148 }
149
150 *ppos += read;
151 return read;
152}
153
Andrew Mortond7d4d842010-03-10 15:21:52 -0800154static ssize_t write_mem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 size_t count, loff_t *ppos)
156{
157 unsigned long p = *ppos;
158 ssize_t written, sz;
159 unsigned long copied;
160 void *ptr;
161
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800162 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return -EFAULT;
164
165 written = 0;
166
167#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
168 /* we don't have page 0 mapped on sparc and m68k.. */
169 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800170 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 /* Hmm. Do something? */
172 buf += sz;
173 p += sz;
174 count -= sz;
175 written += sz;
176 }
177#endif
178
179 while (count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800180 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700182 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
183 return -EPERM;
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800186 * On ia64 if a page has been mapped somewhere as uncached, then
187 * it must also be accessed uncached by the kernel or data
188 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 */
190 ptr = xlate_dev_mem_ptr(p);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700191 if (!ptr) {
Jan Beulichc654d602006-03-25 03:07:31 -0800192 if (written)
193 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return -EFAULT;
195 }
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700196
197 copied = copy_from_user(ptr, buf, sz);
Wu Fengguangfa29e972009-12-14 17:58:08 -0800198 unxlate_dev_mem_ptr(p, ptr);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700199 if (copied) {
200 written += sz - copied;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700201 if (written)
202 break;
203 return -EFAULT;
204 }
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 buf += sz;
207 p += sz;
208 count -= sz;
209 written += sz;
210 }
211
212 *ppos += written;
213 return written;
214}
Robert Love1fcada32008-04-29 16:44:10 -0400215#endif /* CONFIG_DEVMEM */
216
217#if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Andrew Mortond7d4d842010-03-10 15:21:52 -0800219int __weak phys_mem_access_prot_allowed(struct file *file,
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700220 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
221{
222 return 1;
223}
224
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800225#ifndef __HAVE_PHYS_MEM_ACCESS_PROT
Andrew Mortond7d4d842010-03-10 15:21:52 -0800226
227/*
228 * Architectures vary in how they handle caching for addresses
229 * outside of main memory.
230 *
231 */
David Howellsea56f412010-04-06 14:35:08 -0700232#ifdef pgprot_noncached
Andrew Mortond7d4d842010-03-10 15:21:52 -0800233static int uncached_access(struct file *file, unsigned long addr)
234{
235#if defined(CONFIG_IA64)
236 /*
237 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
238 * attribute aliases.
239 */
240 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
241#elif defined(CONFIG_MIPS)
242 {
243 extern int __uncached_access(struct file *file,
244 unsigned long addr);
245
246 return __uncached_access(file, addr);
247 }
248#else
249 /*
250 * Accessing memory above the top the kernel knows about or through a
251 * file pointer
252 * that was marked O_DSYNC will be done non-cached.
253 */
254 if (file->f_flags & O_DSYNC)
255 return 1;
256 return addr >= __pa(high_memory);
257#endif
258}
David Howellsea56f412010-04-06 14:35:08 -0700259#endif
Andrew Mortond7d4d842010-03-10 15:21:52 -0800260
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800261static 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 Dobriyanf0f37e22009-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
Andrew Mortond7d4d842010-03-10 15:21:52 -0800306static int mmap_mem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
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}
Robert Love1fcada32008-04-29 16:44:10 -0400339#endif /* CONFIG_DEVMEM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700341#ifdef CONFIG_DEVKMEM
Andrew Mortond7d4d842010-03-10 15:21:52 -0800342static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Linus Torvalds4bb82552005-08-13 14:22:59 -0700344 unsigned long pfn;
345
Linus Torvalds6d3154c2007-01-22 08:53:24 -0800346 /* Turn a kernel-virtual address into a physical page frame */
347 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800350 * RED-PEN: on some architectures there is more mapped memory than
351 * available in mem_map which pfn_valid checks for. Perhaps should add a
352 * new macro here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 *
354 * RED-PEN: vmalloc is not supported right now.
355 */
Linus Torvalds4bb82552005-08-13 14:22:59 -0700356 if (!pfn_valid(pfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return -EIO;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700358
359 vma->vm_pgoff = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return mmap_mem(file, vma);
361}
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700362#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700364#ifdef CONFIG_CRASH_DUMP
365/*
366 * Read memory corresponding to the old kernel.
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700367 */
Vivek Goyal315c2152005-06-25 14:58:24 -0700368static ssize_t read_oldmem(struct file *file, char __user *buf,
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700369 size_t count, loff_t *ppos)
370{
Vivek Goyal315c2152005-06-25 14:58:24 -0700371 unsigned long pfn, offset;
372 size_t read = 0, csize;
373 int rc = 0;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700374
Maneesh Soni72414d32005-06-25 14:58:28 -0700375 while (count) {
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700376 pfn = *ppos / PAGE_SIZE;
Vivek Goyal315c2152005-06-25 14:58:24 -0700377 if (pfn > saved_max_pfn)
378 return read;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700379
Vivek Goyal315c2152005-06-25 14:58:24 -0700380 offset = (unsigned long)(*ppos % PAGE_SIZE);
381 if (count > PAGE_SIZE - offset)
382 csize = PAGE_SIZE - offset;
383 else
384 csize = count;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700385
Vivek Goyal315c2152005-06-25 14:58:24 -0700386 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
387 if (rc < 0)
388 return rc;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700389 buf += csize;
390 *ppos += csize;
391 read += csize;
392 count -= csize;
393 }
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700394 return read;
395}
396#endif
397
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700398#ifdef CONFIG_DEVKMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399/*
400 * This function reads the *virtual* memory as seen by the kernel.
401 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800402static ssize_t read_kmem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 size_t count, loff_t *ppos)
404{
405 unsigned long p = *ppos;
406 ssize_t low_count, read, sz;
407 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800408 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 read = 0;
411 if (p < (unsigned long) high_memory) {
412 low_count = count;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800413 if (count > (unsigned long)high_memory - p)
414 low_count = (unsigned long)high_memory - p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
417 /* we don't have page 0 mapped on sparc and m68k.. */
418 if (p < PAGE_SIZE && low_count > 0) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800419 sz = size_inside_page(p, low_count);
420 if (clear_user(buf, sz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return -EFAULT;
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800422 buf += sz;
423 p += sz;
424 read += sz;
425 low_count -= sz;
426 count -= sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428#endif
429 while (low_count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800430 sz = size_inside_page(p, low_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /*
433 * On ia64 if a page has been mapped somewhere as
434 * uncached, then it must also be accessed uncached
435 * by the kernel or data corruption may occur
436 */
437 kbuf = xlate_dev_kmem_ptr((char *)p);
438
439 if (copy_to_user(buf, kbuf, sz))
440 return -EFAULT;
441 buf += sz;
442 p += sz;
443 read += sz;
444 low_count -= sz;
445 count -= sz;
446 }
447 }
448
449 if (count > 0) {
450 kbuf = (char *)__get_free_page(GFP_KERNEL);
451 if (!kbuf)
452 return -ENOMEM;
453 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800454 sz = size_inside_page(p, count);
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800455 if (!is_vmalloc_or_module_addr((void *)p)) {
456 err = -ENXIO;
457 break;
458 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800459 sz = vread(kbuf, (char *)p, sz);
460 if (!sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 break;
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800462 if (copy_to_user(buf, kbuf, sz)) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800463 err = -EFAULT;
464 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800466 count -= sz;
467 buf += sz;
468 read += sz;
469 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471 free_page((unsigned long)kbuf);
472 }
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800473 *ppos = p;
474 return read ? read : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477
Andrew Mortond7d4d842010-03-10 15:21:52 -0800478static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
479 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 ssize_t written, sz;
482 unsigned long copied;
483
484 written = 0;
485#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
486 /* we don't have page 0 mapped on sparc and m68k.. */
Wu Fengguangee323982009-12-14 17:58:10 -0800487 if (p < PAGE_SIZE) {
488 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /* Hmm. Do something? */
490 buf += sz;
491 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 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 Fengguangee323982009-12-14 17:58:10 -0800500 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800503 * On ia64 if a page has been mapped somewhere as uncached, then
504 * it must also be accessed uncached by the kernel or data
505 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 */
Wu Fengguangee323982009-12-14 17:58:10 -0800507 ptr = xlate_dev_kmem_ptr((char *)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 count -= sz;
519 written += sz;
520 }
521
522 *ppos += written;
523 return written;
524}
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526/*
527 * This function writes to the *virtual* memory as seen by the kernel.
528 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800529static ssize_t write_kmem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 size_t count, loff_t *ppos)
531{
532 unsigned long p = *ppos;
533 ssize_t wrote = 0;
534 ssize_t virtr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800536 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 if (p < (unsigned long) high_memory) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800539 unsigned long to_write = min_t(unsigned long, count,
540 (unsigned long)high_memory - p);
Wu Fengguangee323982009-12-14 17:58:10 -0800541 wrote = do_write_kmem(p, buf, to_write, ppos);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800542 if (wrote != to_write)
543 return wrote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 p += wrote;
545 buf += wrote;
546 count -= wrote;
547 }
548
549 if (count > 0) {
550 kbuf = (char *)__get_free_page(GFP_KERNEL);
551 if (!kbuf)
552 return wrote ? wrote : -ENOMEM;
553 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800554 unsigned long sz = size_inside_page(p, count);
555 unsigned long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800557 if (!is_vmalloc_or_module_addr((void *)p)) {
558 err = -ENXIO;
559 break;
560 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800561 n = copy_from_user(kbuf, buf, sz);
562 if (n) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800563 err = -EFAULT;
564 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
Wu Fengguangc85e9a92010-02-02 13:44:06 -0800566 vwrite(kbuf, (char *)p, sz);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800567 count -= sz;
568 buf += sz;
569 virtr += sz;
570 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572 free_page((unsigned long)kbuf);
573 }
574
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800575 *ppos = p;
576 return virtr + wrote ? : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700578#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Russell King4f911d62007-05-08 00:28:17 -0700580#ifdef CONFIG_DEVPORT
Andrew Mortond7d4d842010-03-10 15:21:52 -0800581static ssize_t read_port(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 size_t count, loff_t *ppos)
583{
584 unsigned long i = *ppos;
585 char __user *tmp = buf;
586
587 if (!access_ok(VERIFY_WRITE, buf, count))
Andrew Mortond7d4d842010-03-10 15:21:52 -0800588 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 while (count-- > 0 && i < 65536) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800590 if (__put_user(inb(i), tmp) < 0)
591 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 i++;
593 tmp++;
594 }
595 *ppos = i;
596 return tmp-buf;
597}
598
Andrew Mortond7d4d842010-03-10 15:21:52 -0800599static ssize_t write_port(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 size_t count, loff_t *ppos)
601{
602 unsigned long i = *ppos;
603 const char __user * tmp = buf;
604
Andrew Mortond7d4d842010-03-10 15:21:52 -0800605 if (!access_ok(VERIFY_READ, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return -EFAULT;
607 while (count-- > 0 && i < 65536) {
608 char c;
Jan Beulichc654d602006-03-25 03:07:31 -0800609 if (__get_user(c, tmp)) {
610 if (tmp > buf)
611 break;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800612 return -EFAULT;
Jan Beulichc654d602006-03-25 03:07:31 -0800613 }
Andrew Mortond7d4d842010-03-10 15:21:52 -0800614 outb(c, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 i++;
616 tmp++;
617 }
618 *ppos = i;
619 return tmp-buf;
620}
621#endif
622
Andrew Mortond7d4d842010-03-10 15:21:52 -0800623static ssize_t read_null(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 size_t count, loff_t *ppos)
625{
626 return 0;
627}
628
Andrew Mortond7d4d842010-03-10 15:21:52 -0800629static ssize_t write_null(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 size_t count, loff_t *ppos)
631{
632 return count;
633}
634
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200635static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
636 struct splice_desc *sd)
637{
638 return sd->len;
639}
640
Andrew Mortond7d4d842010-03-10 15:21:52 -0800641static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200642 loff_t *ppos, size_t len, unsigned int flags)
643{
644 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
645}
646
Andrew Mortond7d4d842010-03-10 15:21:52 -0800647static ssize_t read_zero(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 size_t count, loff_t *ppos)
649{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700650 size_t written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 if (!count)
653 return 0;
654
655 if (!access_ok(VERIFY_WRITE, buf, count))
656 return -EFAULT;
657
Nick Piggin557ed1f2007-10-16 01:24:40 -0700658 written = 0;
659 while (count) {
660 unsigned long unwritten;
661 size_t chunk = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Nick Piggin557ed1f2007-10-16 01:24:40 -0700663 if (chunk > PAGE_SIZE)
664 chunk = PAGE_SIZE; /* Just for latency reasons */
Nikanth Karthikesanbb521c52009-09-23 15:57:09 -0700665 unwritten = __clear_user(buf, chunk);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700666 written += chunk - unwritten;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (unwritten)
Nick Piggin557ed1f2007-10-16 01:24:40 -0700668 break;
Linus Torvalds2b838682009-06-09 20:40:25 -0700669 if (signal_pending(current))
670 return written ? written : -ERESTARTSYS;
Nick Piggin557ed1f2007-10-16 01:24:40 -0700671 buf += chunk;
672 count -= chunk;
673 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return written ? written : -EFAULT;
676}
677
Andrew Mortond7d4d842010-03-10 15:21:52 -0800678static int mmap_zero(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700680#ifndef CONFIG_MMU
681 return -ENOSYS;
682#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (vma->vm_flags & VM_SHARED)
684 return shmem_zero_setup(vma);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700685 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Andrew Mortond7d4d842010-03-10 15:21:52 -0800688static ssize_t write_full(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 size_t count, loff_t *ppos)
690{
691 return -ENOSPC;
692}
693
694/*
695 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
696 * can fopen() both devices with "a" now. This was previously impossible.
697 * -- SRB.
698 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800699static loff_t null_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 return file->f_pos = 0;
702}
703
Robert Love1fcada32008-04-29 16:44:10 -0400704#if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM) || defined(CONFIG_DEVPORT)
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706/*
707 * The memory devices use the full 32/64 bits of the offset, and so we cannot
708 * check against negative addresses: they are ok. The return value is weird,
709 * though, in that case (0).
710 *
711 * also note that seeking relative to the "end of file" isn't supported:
712 * it has no meaning, so it returns -EINVAL.
713 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800714static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 loff_t ret;
717
Josef Sipeka7113a92006-12-08 02:36:55 -0800718 mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 switch (orig) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800720 case SEEK_CUR:
721 offset += file->f_pos;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800722 case SEEK_SET:
723 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
724 if ((unsigned long long)offset >= ~0xFFFULL) {
725 ret = -EOVERFLOW;
726 break;
727 }
728 file->f_pos = offset;
729 ret = file->f_pos;
730 force_successful_syscall_return();
731 break;
732 default:
733 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
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
Robert Love1fcada32008-04-29 16:44:10 -0400739#endif
740
741#if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM) || defined(CONFIG_DEVPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742static int open_port(struct inode * inode, struct file * filp)
743{
744 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
745}
Robert Love1fcada32008-04-29 16:44:10 -0400746#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748#define zero_lseek null_lseek
749#define full_lseek null_lseek
750#define write_zero write_null
751#define read_full read_zero
752#define open_mem open_port
753#define open_kmem open_mem
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700754#define open_oldmem open_mem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Robert Love1fcada32008-04-29 16:44:10 -0400756#ifdef CONFIG_DEVMEM
Arjan van de Ven62322d22006-07-03 00:24:21 -0700757static const struct file_operations mem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 .llseek = memory_lseek,
759 .read = read_mem,
760 .write = write_mem,
761 .mmap = mmap_mem,
762 .open = open_mem,
David Howells5da61852006-09-27 01:50:16 -0700763 .get_unmapped_area = get_unmapped_area_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764};
Robert Love1fcada32008-04-29 16:44:10 -0400765#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700767#ifdef CONFIG_DEVKMEM
Arjan van de Ven62322d22006-07-03 00:24:21 -0700768static const struct file_operations kmem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 .llseek = memory_lseek,
770 .read = read_kmem,
771 .write = write_kmem,
772 .mmap = mmap_kmem,
773 .open = open_kmem,
David Howells5da61852006-09-27 01:50:16 -0700774 .get_unmapped_area = get_unmapped_area_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775};
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700776#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Arjan van de Ven62322d22006-07-03 00:24:21 -0700778static const struct file_operations null_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 .llseek = null_lseek,
780 .read = read_null,
781 .write = write_null,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200782 .splice_write = splice_write_null,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783};
784
Russell King4f911d62007-05-08 00:28:17 -0700785#ifdef CONFIG_DEVPORT
Arjan van de Ven62322d22006-07-03 00:24:21 -0700786static const struct file_operations port_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 .llseek = memory_lseek,
788 .read = read_port,
789 .write = write_port,
790 .open = open_port,
791};
792#endif
793
Arjan van de Ven62322d22006-07-03 00:24:21 -0700794static const struct file_operations zero_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 .llseek = zero_lseek,
796 .read = read_zero,
797 .write = write_zero,
798 .mmap = mmap_zero,
799};
800
David Howells5da61852006-09-27 01:50:16 -0700801/*
802 * capabilities for /dev/zero
803 * - permits private mappings, "copies" are taken of the source of zeros
Jan Kara371d2172010-09-21 11:49:01 +0200804 * - no writeback happens
David Howells5da61852006-09-27 01:50:16 -0700805 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806static struct backing_dev_info zero_bdi = {
Jens Axboed9938312009-06-12 14:45:52 +0200807 .name = "char/mem",
Jan Kara371d2172010-09-21 11:49:01 +0200808 .capabilities = BDI_CAP_MAP_COPY | BDI_CAP_NO_ACCT_AND_WRITEBACK,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809};
810
Arjan van de Ven62322d22006-07-03 00:24:21 -0700811static const struct file_operations full_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 .llseek = full_lseek,
813 .read = read_full,
814 .write = write_full,
815};
816
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700817#ifdef CONFIG_CRASH_DUMP
Arjan van de Ven62322d22006-07-03 00:24:21 -0700818static const struct file_operations oldmem_fops = {
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700819 .read = read_oldmem,
820 .open = open_oldmem,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200821 .llseek = default_llseek,
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700822};
823#endif
824
Kay Sievers7e5b58b2011-04-07 04:29:20 +0200825static ssize_t kmsg_writev(struct kiocb *iocb, const struct iovec *iv,
826 unsigned long count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Kay Sievers7e5b58b2011-04-07 04:29:20 +0200828 char *line, *p;
Andrew Morton70a5f522011-04-12 16:13:31 -0700829 int i;
Kay Sievers7e5b58b2011-04-07 04:29:20 +0200830 ssize_t ret = -EFAULT;
Andrew Morton70a5f522011-04-12 16:13:31 -0700831 size_t len = iov_length(iv, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Andrew Morton70a5f522011-04-12 16:13:31 -0700833 line = kmalloc(len + 1, GFP_KERNEL);
Kay Sievers7e5b58b2011-04-07 04:29:20 +0200834 if (line == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return -ENOMEM;
Kay Sievers7e5b58b2011-04-07 04:29:20 +0200836
837 /*
838 * copy all vectors into a single string, to ensure we do
839 * not interleave our log line with other printk calls
840 */
Andrew Morton70a5f522011-04-12 16:13:31 -0700841 p = line;
Kay Sievers7e5b58b2011-04-07 04:29:20 +0200842 for (i = 0; i < count; i++) {
843 if (copy_from_user(p, iv[i].iov_base, iv[i].iov_len))
844 goto out;
845 p += iv[i].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
Kay Sievers7e5b58b2011-04-07 04:29:20 +0200847 p[0] = '\0';
848
849 ret = printk("%s", line);
850 /* printk can add a prefix */
851 if (ret > len)
852 ret = len;
853out:
854 kfree(line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return ret;
856}
857
Arjan van de Ven62322d22006-07-03 00:24:21 -0700858static const struct file_operations kmsg_fops = {
Kay Sievers7e5b58b2011-04-07 04:29:20 +0200859 .aio_write = kmsg_writev,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200860 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861};
862
Kay Sievers389e0cb2009-07-04 16:51:29 +0200863static const struct memdev {
864 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400865 umode_t mode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200866 const struct file_operations *fops;
867 struct backing_dev_info *dev_info;
868} devlist[] = {
Robert Love1fcada32008-04-29 16:44:10 -0400869#ifdef CONFIG_DEVMEM
Kay Sieverse454cea2009-09-18 23:01:12 +0200870 [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
Robert Love1fcada32008-04-29 16:44:10 -0400871#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700872#ifdef CONFIG_DEVKMEM
Kay Sieverse454cea2009-09-18 23:01:12 +0200873 [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700874#endif
Kay Sieverse454cea2009-09-18 23:01:12 +0200875 [3] = { "null", 0666, &null_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700876#ifdef CONFIG_DEVPORT
Kay Sieverse454cea2009-09-18 23:01:12 +0200877 [4] = { "port", 0, &port_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700878#endif
Kay Sieverse454cea2009-09-18 23:01:12 +0200879 [5] = { "zero", 0666, &zero_fops, &zero_bdi },
880 [7] = { "full", 0666, &full_fops, NULL },
881 [8] = { "random", 0666, &random_fops, NULL },
882 [9] = { "urandom", 0666, &urandom_fops, NULL },
883 [11] = { "kmsg", 0, &kmsg_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700884#ifdef CONFIG_CRASH_DUMP
Kay Sieverse454cea2009-09-18 23:01:12 +0200885 [12] = { "oldmem", 0, &oldmem_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700886#endif
887};
888
889static int memory_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200891 int minor;
892 const struct memdev *dev;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700893
Kay Sievers389e0cb2009-07-04 16:51:29 +0200894 minor = iminor(inode);
895 if (minor >= ARRAY_SIZE(devlist))
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200896 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700897
Kay Sievers389e0cb2009-07-04 16:51:29 +0200898 dev = &devlist[minor];
899 if (!dev->fops)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200900 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700901
Kay Sievers389e0cb2009-07-04 16:51:29 +0200902 filp->f_op = dev->fops;
903 if (dev->dev_info)
904 filp->f_mapping->backing_dev_info = dev->dev_info;
905
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700906 /* Is /dev/mem or /dev/kmem ? */
907 if (dev->dev_info == &directly_mappable_cdev_bdi)
908 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
909
Kay Sievers389e0cb2009-07-04 16:51:29 +0200910 if (dev->fops->open)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200911 return dev->fops->open(inode, filp);
912
913 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Arjan van de Ven62322d22006-07-03 00:24:21 -0700916static const struct file_operations memory_fops = {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800917 .open = memory_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200918 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919};
920
Al Viro2c9ede52011-07-23 20:24:48 -0400921static char *mem_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +0200922{
923 if (mode && devlist[MINOR(dev->devt)].mode)
924 *mode = devlist[MINOR(dev->devt)].mode;
925 return NULL;
926}
927
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800928static struct class *mem_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930static int __init chr_dev_init(void)
931{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200932 int minor;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700933 int err;
934
935 err = bdi_init(&zero_bdi);
936 if (err)
937 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Andrew Mortond7d4d842010-03-10 15:21:52 -0800939 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
941
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800942 mem_class = class_create(THIS_MODULE, "mem");
Anton Blanchard6e191f72010-04-06 14:34:55 -0700943 if (IS_ERR(mem_class))
944 return PTR_ERR(mem_class);
945
Kay Sieverse454cea2009-09-18 23:01:12 +0200946 mem_class->devnode = mem_devnode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200947 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
948 if (!devlist[minor].name)
949 continue;
950 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
951 NULL, devlist[minor].name);
952 }
Greg Kroah-Hartmanebf644c2006-07-25 17:13:31 -0700953
David Howells31d1d482010-08-06 16:34:43 +0100954 return tty_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957fs_initcall(chr_dev_init);