blob: 9b1eb188acdcf03c96083dd84c96fb2b2e87c0a8 [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>
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
Wu Fengguang7fabadd2009-12-14 17:58:09 -080042 sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
Wu Fengguangf2223182009-12-14 17:58:07 -080043
Wu Fengguang7fabadd2009-12-14 17:58:09 -080044 return min(sz, size);
Wu Fengguangf2223182009-12-14 17:58:07 -080045}
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
Bjorn Helgaas136939a2006-03-26 01:37:05 -080048static inline int valid_phys_addr_range(unsigned long addr, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Changli Gaocfaf3462011-03-23 16:42:58 -070050 return addr + count <= __pa(high_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080052
Lennert Buytenhek06c67be2006-07-10 04:45:27 -070053static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080054{
55 return 1;
56}
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#endif
58
Robert Love2a1970e2008-04-29 16:44:10 -040059#if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM)
Ingo Molnard0926332008-07-18 00:26:59 +020060#ifdef CONFIG_STRICT_DEVMEM
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080061static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020062{
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080063 u64 from = ((u64)pfn) << PAGE_SHIFT;
64 u64 to = from + size;
65 u64 cursor = from;
Arjan van de Venae531c22008-04-24 23:40:47 +020066
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080067 while (cursor < to) {
68 if (!devmem_is_allowed(pfn)) {
69 printk(KERN_INFO
70 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
Arjan van de Venae531c22008-04-24 23:40:47 +020071 current->comm, from, to);
72 return 0;
73 }
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080074 cursor += PAGE_SIZE;
75 pfn++;
Arjan van de Venae531c22008-04-24 23:40:47 +020076 }
77 return 1;
78}
79#else
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080080static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020081{
82 return 1;
83}
84#endif
Robert Love2a1970e2008-04-29 16:44:10 -040085#endif
Arjan van de Venae531c22008-04-24 23:40:47 +020086
Robert Love2a1970e2008-04-29 16:44:10 -040087#ifdef CONFIG_DEVMEM
Andrew Mortond7d4d842010-03-10 15:21:52 -080088void __weak unxlate_dev_mem_ptr(unsigned long phys, void *addr)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -070089{
90}
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
Andrew Mortond7d4d842010-03-10 15:21:52 -080093 * This funcion reads the *physical* memory. The f_pos points directly to the
94 * memory location.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 */
Andrew Mortond7d4d842010-03-10 15:21:52 -080096static ssize_t read_mem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 size_t count, loff_t *ppos)
98{
99 unsigned long p = *ppos;
100 ssize_t read, sz;
101 char *ptr;
102
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800103 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return -EFAULT;
105 read = 0;
106#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
107 /* we don't have page 0 mapped on sparc and m68k.. */
108 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800109 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (sz > 0) {
111 if (clear_user(buf, sz))
112 return -EFAULT;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800113 buf += sz;
114 p += sz;
115 count -= sz;
116 read += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118 }
119#endif
120
121 while (count > 0) {
Wu Fengguangfa29e972009-12-14 17:58:08 -0800122 unsigned long remaining;
123
Wu Fengguangf2223182009-12-14 17:58:07 -0800124 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700126 if (!range_is_allowed(p >> PAGE_SHIFT, count))
127 return -EPERM;
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800130 * On ia64 if a page has been mapped somewhere as uncached, then
131 * it must also be accessed uncached by the kernel or data
132 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
134 ptr = xlate_dev_mem_ptr(p);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700135 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700137
Wu Fengguangfa29e972009-12-14 17:58:08 -0800138 remaining = copy_to_user(buf, ptr, sz);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700139 unxlate_dev_mem_ptr(p, ptr);
Wu Fengguangfa29e972009-12-14 17:58:08 -0800140 if (remaining)
141 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 buf += sz;
144 p += sz;
145 count -= sz;
146 read += sz;
147 }
148
149 *ppos += read;
150 return read;
151}
152
Andrew Mortond7d4d842010-03-10 15:21:52 -0800153static ssize_t write_mem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 size_t count, loff_t *ppos)
155{
156 unsigned long p = *ppos;
157 ssize_t written, sz;
158 unsigned long copied;
159 void *ptr;
160
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800161 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return -EFAULT;
163
164 written = 0;
165
166#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
167 /* we don't have page 0 mapped on sparc and m68k.. */
168 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800169 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 /* Hmm. Do something? */
171 buf += sz;
172 p += sz;
173 count -= sz;
174 written += sz;
175 }
176#endif
177
178 while (count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800179 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700181 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
182 return -EPERM;
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800185 * On ia64 if a page has been mapped somewhere as uncached, then
186 * it must also be accessed uncached by the kernel or data
187 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 */
189 ptr = xlate_dev_mem_ptr(p);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700190 if (!ptr) {
Jan Beulichc654d602006-03-25 03:07:31 -0800191 if (written)
192 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return -EFAULT;
194 }
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700195
196 copied = copy_from_user(ptr, buf, sz);
Wu Fengguangfa29e972009-12-14 17:58:08 -0800197 unxlate_dev_mem_ptr(p, ptr);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700198 if (copied) {
199 written += sz - copied;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700200 if (written)
201 break;
202 return -EFAULT;
203 }
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 buf += sz;
206 p += sz;
207 count -= sz;
208 written += sz;
209 }
210
211 *ppos += written;
212 return written;
213}
Robert Love2a1970e2008-04-29 16:44:10 -0400214#endif /* CONFIG_DEVMEM */
215
216#if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Andrew Mortond7d4d842010-03-10 15:21:52 -0800218int __weak phys_mem_access_prot_allowed(struct file *file,
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700219 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
220{
221 return 1;
222}
223
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800224#ifndef __HAVE_PHYS_MEM_ACCESS_PROT
Andrew Mortond7d4d842010-03-10 15:21:52 -0800225
226/*
227 * Architectures vary in how they handle caching for addresses
228 * outside of main memory.
229 *
230 */
David Howellsea56f412010-04-06 14:35:08 -0700231#ifdef pgprot_noncached
Andrew Mortond7d4d842010-03-10 15:21:52 -0800232static int uncached_access(struct file *file, unsigned long addr)
233{
234#if defined(CONFIG_IA64)
235 /*
236 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
237 * attribute aliases.
238 */
239 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
240#elif defined(CONFIG_MIPS)
241 {
242 extern int __uncached_access(struct file *file,
243 unsigned long addr);
244
245 return __uncached_access(file, addr);
246 }
247#else
248 /*
249 * Accessing memory above the top the kernel knows about or through a
250 * file pointer
251 * that was marked O_DSYNC will be done non-cached.
252 */
253 if (file->f_flags & O_DSYNC)
254 return 1;
255 return addr >= __pa(high_memory);
256#endif
257}
David Howellsea56f412010-04-06 14:35:08 -0700258#endif
Andrew Mortond7d4d842010-03-10 15:21:52 -0800259
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800260static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
261 unsigned long size, pgprot_t vma_prot)
262{
263#ifdef pgprot_noncached
264 unsigned long offset = pfn << PAGE_SHIFT;
265
266 if (uncached_access(file, offset))
267 return pgprot_noncached(vma_prot);
268#endif
269 return vma_prot;
270}
271#endif
272
David Howells5da61852006-09-27 01:50:16 -0700273#ifndef CONFIG_MMU
274static unsigned long get_unmapped_area_mem(struct file *file,
275 unsigned long addr,
276 unsigned long len,
277 unsigned long pgoff,
278 unsigned long flags)
279{
280 if (!valid_mmap_phys_addr_range(pgoff, len))
281 return (unsigned long) -EINVAL;
Benjamin Herrenschmidt8a932582007-04-16 22:53:16 -0700282 return pgoff << PAGE_SHIFT;
David Howells5da61852006-09-27 01:50:16 -0700283}
284
285/* can't do an in-place private mapping if there's no MMU */
286static inline int private_mapping_ok(struct vm_area_struct *vma)
287{
288 return vma->vm_flags & VM_MAYSHARE;
289}
290#else
291#define get_unmapped_area_mem NULL
292
293static inline int private_mapping_ok(struct vm_area_struct *vma)
294{
295 return 1;
296}
297#endif
298
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400299static const struct vm_operations_struct mmap_mem_ops = {
Rik van Riel7ae8ed52008-07-23 21:27:07 -0700300#ifdef CONFIG_HAVE_IOREMAP_PROT
301 .access = generic_access_phys
302#endif
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700303};
304
Andrew Mortond7d4d842010-03-10 15:21:52 -0800305static int mmap_mem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800307 size_t size = vma->vm_end - vma->vm_start;
308
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700309 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800310 return -EINVAL;
311
David Howells5da61852006-09-27 01:50:16 -0700312 if (!private_mapping_ok(vma))
313 return -ENOSYS;
314
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800315 if (!range_is_allowed(vma->vm_pgoff, size))
316 return -EPERM;
317
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700318 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
319 &vma->vm_page_prot))
320 return -EINVAL;
321
Roland Dreier8b150472005-10-28 17:46:18 -0700322 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800323 size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700326 vma->vm_ops = &mmap_mem_ops;
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
329 if (remap_pfn_range(vma,
330 vma->vm_start,
331 vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800332 size,
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700333 vma->vm_page_prot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return -EAGAIN;
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return 0;
337}
Robert Love2a1970e2008-04-29 16:44:10 -0400338#endif /* CONFIG_DEVMEM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700340#ifdef CONFIG_DEVKMEM
Andrew Mortond7d4d842010-03-10 15:21:52 -0800341static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
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 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800349 * RED-PEN: on some architectures there is more mapped memory than
350 * available in mem_map which pfn_valid checks for. Perhaps should add a
351 * new macro here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 *
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 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800401static ssize_t read_kmem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 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 */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800407 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 read = 0;
410 if (p < (unsigned long) high_memory) {
411 low_count = count;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800412 if (count > (unsigned long)high_memory - p)
413 low_count = (unsigned long)high_memory - p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
416 /* we don't have page 0 mapped on sparc and m68k.. */
417 if (p < PAGE_SIZE && low_count > 0) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800418 sz = size_inside_page(p, low_count);
419 if (clear_user(buf, sz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return -EFAULT;
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800421 buf += sz;
422 p += sz;
423 read += sz;
424 low_count -= sz;
425 count -= sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
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 Fengguang80ad89a2009-12-14 17:58:10 -0800453 sz = size_inside_page(p, count);
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800454 if (!is_vmalloc_or_module_addr((void *)p)) {
455 err = -ENXIO;
456 break;
457 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800458 sz = vread(kbuf, (char *)p, sz);
459 if (!sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 break;
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800461 if (copy_to_user(buf, kbuf, sz)) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800462 err = -EFAULT;
463 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800465 count -= sz;
466 buf += sz;
467 read += sz;
468 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470 free_page((unsigned long)kbuf);
471 }
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800472 *ppos = p;
473 return read ? read : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476
Andrew Mortond7d4d842010-03-10 15:21:52 -0800477static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
478 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 ssize_t written, sz;
481 unsigned long copied;
482
483 written = 0;
484#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
485 /* we don't have page 0 mapped on sparc and m68k.. */
Wu Fengguangee323982009-12-14 17:58:10 -0800486 if (p < PAGE_SIZE) {
487 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 /* Hmm. Do something? */
489 buf += sz;
490 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 count -= sz;
492 written += sz;
493 }
494#endif
495
496 while (count > 0) {
497 char *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Wu Fengguangee323982009-12-14 17:58:10 -0800499 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800502 * On ia64 if a page has been mapped somewhere as uncached, then
503 * it must also be accessed uncached by the kernel or data
504 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 */
Wu Fengguangee323982009-12-14 17:58:10 -0800506 ptr = xlate_dev_kmem_ptr((char *)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 copied = copy_from_user(ptr, buf, sz);
509 if (copied) {
Jan Beulichc654d602006-03-25 03:07:31 -0800510 written += sz - copied;
511 if (written)
512 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return -EFAULT;
514 }
515 buf += sz;
516 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 count -= sz;
518 written += sz;
519 }
520
521 *ppos += written;
522 return written;
523}
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525/*
526 * This function writes to the *virtual* memory as seen by the kernel.
527 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800528static ssize_t write_kmem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 size_t count, loff_t *ppos)
530{
531 unsigned long p = *ppos;
532 ssize_t wrote = 0;
533 ssize_t virtr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800535 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 if (p < (unsigned long) high_memory) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800538 unsigned long to_write = min_t(unsigned long, count,
539 (unsigned long)high_memory - p);
Wu Fengguangee323982009-12-14 17:58:10 -0800540 wrote = do_write_kmem(p, buf, to_write, ppos);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800541 if (wrote != to_write)
542 return wrote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 p += wrote;
544 buf += wrote;
545 count -= wrote;
546 }
547
548 if (count > 0) {
549 kbuf = (char *)__get_free_page(GFP_KERNEL);
550 if (!kbuf)
551 return wrote ? wrote : -ENOMEM;
552 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800553 unsigned long sz = size_inside_page(p, count);
554 unsigned long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800556 if (!is_vmalloc_or_module_addr((void *)p)) {
557 err = -ENXIO;
558 break;
559 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800560 n = copy_from_user(kbuf, buf, sz);
561 if (n) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800562 err = -EFAULT;
563 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
Wu Fengguangc85e9a92010-02-02 13:44:06 -0800565 vwrite(kbuf, (char *)p, sz);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800566 count -= sz;
567 buf += sz;
568 virtr += sz;
569 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571 free_page((unsigned long)kbuf);
572 }
573
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800574 *ppos = p;
575 return virtr + wrote ? : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700577#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Russell King4f911d62007-05-08 00:28:17 -0700579#ifdef CONFIG_DEVPORT
Andrew Mortond7d4d842010-03-10 15:21:52 -0800580static ssize_t read_port(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 size_t count, loff_t *ppos)
582{
583 unsigned long i = *ppos;
584 char __user *tmp = buf;
585
586 if (!access_ok(VERIFY_WRITE, buf, count))
Andrew Mortond7d4d842010-03-10 15:21:52 -0800587 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 while (count-- > 0 && i < 65536) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800589 if (__put_user(inb(i), tmp) < 0)
590 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 i++;
592 tmp++;
593 }
594 *ppos = i;
595 return tmp-buf;
596}
597
Andrew Mortond7d4d842010-03-10 15:21:52 -0800598static ssize_t write_port(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 size_t count, loff_t *ppos)
600{
601 unsigned long i = *ppos;
602 const char __user * tmp = buf;
603
Andrew Mortond7d4d842010-03-10 15:21:52 -0800604 if (!access_ok(VERIFY_READ, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return -EFAULT;
606 while (count-- > 0 && i < 65536) {
607 char c;
Jan Beulichc654d602006-03-25 03:07:31 -0800608 if (__get_user(c, tmp)) {
609 if (tmp > buf)
610 break;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800611 return -EFAULT;
Jan Beulichc654d602006-03-25 03:07:31 -0800612 }
Andrew Mortond7d4d842010-03-10 15:21:52 -0800613 outb(c, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 i++;
615 tmp++;
616 }
617 *ppos = i;
618 return tmp-buf;
619}
620#endif
621
Andrew Mortond7d4d842010-03-10 15:21:52 -0800622static ssize_t read_null(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 size_t count, loff_t *ppos)
624{
625 return 0;
626}
627
Andrew Mortond7d4d842010-03-10 15:21:52 -0800628static ssize_t write_null(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 size_t count, loff_t *ppos)
630{
631 return count;
632}
633
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200634static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
635 struct splice_desc *sd)
636{
637 return sd->len;
638}
639
Andrew Mortond7d4d842010-03-10 15:21:52 -0800640static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200641 loff_t *ppos, size_t len, unsigned int flags)
642{
643 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
644}
645
Andrew Mortond7d4d842010-03-10 15:21:52 -0800646static ssize_t read_zero(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 size_t count, loff_t *ppos)
648{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700649 size_t written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 if (!count)
652 return 0;
653
654 if (!access_ok(VERIFY_WRITE, buf, count))
655 return -EFAULT;
656
Nick Piggin557ed1f2007-10-16 01:24:40 -0700657 written = 0;
658 while (count) {
659 unsigned long unwritten;
660 size_t chunk = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Nick Piggin557ed1f2007-10-16 01:24:40 -0700662 if (chunk > PAGE_SIZE)
663 chunk = PAGE_SIZE; /* Just for latency reasons */
Nikanth Karthikesanbb521c52009-09-23 15:57:09 -0700664 unwritten = __clear_user(buf, chunk);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700665 written += chunk - unwritten;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (unwritten)
Nick Piggin557ed1f2007-10-16 01:24:40 -0700667 break;
Linus Torvalds2b838682009-06-09 20:40:25 -0700668 if (signal_pending(current))
669 return written ? written : -ERESTARTSYS;
Nick Piggin557ed1f2007-10-16 01:24:40 -0700670 buf += chunk;
671 count -= chunk;
672 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return written ? written : -EFAULT;
675}
676
Andrew Mortond7d4d842010-03-10 15:21:52 -0800677static int mmap_zero(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700679#ifndef CONFIG_MMU
680 return -ENOSYS;
681#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (vma->vm_flags & VM_SHARED)
683 return shmem_zero_setup(vma);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700684 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Andrew Mortond7d4d842010-03-10 15:21:52 -0800687static ssize_t write_full(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 size_t count, loff_t *ppos)
689{
690 return -ENOSPC;
691}
692
693/*
694 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
695 * can fopen() both devices with "a" now. This was previously impossible.
696 * -- SRB.
697 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800698static loff_t null_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 return file->f_pos = 0;
701}
702
Robert Love2a1970e2008-04-29 16:44:10 -0400703#if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM) || defined(CONFIG_DEVPORT)
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705/*
706 * The memory devices use the full 32/64 bits of the offset, and so we cannot
707 * check against negative addresses: they are ok. The return value is weird,
708 * though, in that case (0).
709 *
710 * also note that seeking relative to the "end of file" isn't supported:
711 * it has no meaning, so it returns -EINVAL.
712 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800713static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 loff_t ret;
716
Josef Sipeka7113a92006-12-08 02:36:55 -0800717 mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 switch (orig) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800719 case SEEK_CUR:
720 offset += file->f_pos;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800721 case SEEK_SET:
722 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
723 if ((unsigned long long)offset >= ~0xFFFULL) {
724 ret = -EOVERFLOW;
725 break;
726 }
727 file->f_pos = offset;
728 ret = file->f_pos;
729 force_successful_syscall_return();
730 break;
731 default:
732 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
Josef Sipeka7113a92006-12-08 02:36:55 -0800734 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return ret;
736}
737
Robert Love2a1970e2008-04-29 16:44:10 -0400738#endif
739
740#if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM) || defined(CONFIG_DEVPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741static int open_port(struct inode * inode, struct file * filp)
742{
743 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
744}
Robert Love2a1970e2008-04-29 16:44:10 -0400745#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747#define zero_lseek null_lseek
748#define full_lseek null_lseek
749#define write_zero write_null
750#define read_full read_zero
751#define open_mem open_port
752#define open_kmem open_mem
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700753#define open_oldmem open_mem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Robert Love2a1970e2008-04-29 16:44:10 -0400755#ifdef CONFIG_DEVMEM
Arjan van de Ven62322d22006-07-03 00:24:21 -0700756static const struct file_operations mem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 .llseek = memory_lseek,
758 .read = read_mem,
759 .write = write_mem,
760 .mmap = mmap_mem,
761 .open = open_mem,
David Howells5da61852006-09-27 01:50:16 -0700762 .get_unmapped_area = get_unmapped_area_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763};
Robert Love2a1970e2008-04-29 16:44:10 -0400764#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700766#ifdef CONFIG_DEVKMEM
Arjan van de Ven62322d22006-07-03 00:24:21 -0700767static const struct file_operations kmem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 .llseek = memory_lseek,
769 .read = read_kmem,
770 .write = write_kmem,
771 .mmap = mmap_kmem,
772 .open = open_kmem,
David Howells5da61852006-09-27 01:50:16 -0700773 .get_unmapped_area = get_unmapped_area_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774};
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700775#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Arjan van de Ven62322d22006-07-03 00:24:21 -0700777static const struct file_operations null_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 .llseek = null_lseek,
779 .read = read_null,
780 .write = write_null,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200781 .splice_write = splice_write_null,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782};
783
Russell King4f911d62007-05-08 00:28:17 -0700784#ifdef CONFIG_DEVPORT
Arjan van de Ven62322d22006-07-03 00:24:21 -0700785static const struct file_operations port_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 .llseek = memory_lseek,
787 .read = read_port,
788 .write = write_port,
789 .open = open_port,
790};
791#endif
792
Arjan van de Ven62322d22006-07-03 00:24:21 -0700793static const struct file_operations zero_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 .llseek = zero_lseek,
795 .read = read_zero,
796 .write = write_zero,
797 .mmap = mmap_zero,
798};
799
David Howells5da61852006-09-27 01:50:16 -0700800/*
801 * capabilities for /dev/zero
802 * - permits private mappings, "copies" are taken of the source of zeros
Jan Kara371d2172010-09-21 11:49:01 +0200803 * - no writeback happens
David Howells5da61852006-09-27 01:50:16 -0700804 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805static struct backing_dev_info zero_bdi = {
Jens Axboed9938312009-06-12 14:45:52 +0200806 .name = "char/mem",
Jan Kara371d2172010-09-21 11:49:01 +0200807 .capabilities = BDI_CAP_MAP_COPY | BDI_CAP_NO_ACCT_AND_WRITEBACK,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808};
809
Arjan van de Ven62322d22006-07-03 00:24:21 -0700810static const struct file_operations full_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 .llseek = full_lseek,
812 .read = read_full,
813 .write = write_full,
814};
815
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700816#ifdef CONFIG_CRASH_DUMP
Arjan van de Ven62322d22006-07-03 00:24:21 -0700817static const struct file_operations oldmem_fops = {
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700818 .read = read_oldmem,
819 .open = open_oldmem,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200820 .llseek = default_llseek,
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700821};
822#endif
823
Kay Sievers7e5b58b2011-04-07 04:29:20 +0200824static ssize_t kmsg_writev(struct kiocb *iocb, const struct iovec *iv,
825 unsigned long count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Kay Sievers7e5b58b2011-04-07 04:29:20 +0200827 char *line, *p;
Andrew Morton70a5f522011-04-12 16:13:31 -0700828 int i;
Kay Sievers7e5b58b2011-04-07 04:29:20 +0200829 ssize_t ret = -EFAULT;
Andrew Morton70a5f522011-04-12 16:13:31 -0700830 size_t len = iov_length(iv, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Andrew Morton70a5f522011-04-12 16:13:31 -0700832 line = kmalloc(len + 1, GFP_KERNEL);
Kay Sievers7e5b58b2011-04-07 04:29:20 +0200833 if (line == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return -ENOMEM;
Kay Sievers7e5b58b2011-04-07 04:29:20 +0200835
836 /*
837 * copy all vectors into a single string, to ensure we do
838 * not interleave our log line with other printk calls
839 */
Andrew Morton70a5f522011-04-12 16:13:31 -0700840 p = line;
Kay Sievers7e5b58b2011-04-07 04:29:20 +0200841 for (i = 0; i < count; i++) {
842 if (copy_from_user(p, iv[i].iov_base, iv[i].iov_len))
843 goto out;
844 p += iv[i].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
Kay Sievers7e5b58b2011-04-07 04:29:20 +0200846 p[0] = '\0';
847
848 ret = printk("%s", line);
849 /* printk can add a prefix */
850 if (ret > len)
851 ret = len;
852out:
853 kfree(line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return ret;
855}
856
Arjan van de Ven62322d22006-07-03 00:24:21 -0700857static const struct file_operations kmsg_fops = {
Kay Sievers7e5b58b2011-04-07 04:29:20 +0200858 .aio_write = kmsg_writev,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200859 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860};
861
Kay Sievers389e0cb2009-07-04 16:51:29 +0200862static const struct memdev {
863 const char *name;
Kay Sieverse454cea2009-09-18 23:01:12 +0200864 mode_t mode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200865 const struct file_operations *fops;
866 struct backing_dev_info *dev_info;
867} devlist[] = {
Robert Love2a1970e2008-04-29 16:44:10 -0400868#ifdef CONFIG_DEVMEM
Kay Sieverse454cea2009-09-18 23:01:12 +0200869 [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
Robert Love2a1970e2008-04-29 16:44:10 -0400870#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700871#ifdef CONFIG_DEVKMEM
Kay Sieverse454cea2009-09-18 23:01:12 +0200872 [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700873#endif
Kay Sieverse454cea2009-09-18 23:01:12 +0200874 [3] = { "null", 0666, &null_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700875#ifdef CONFIG_DEVPORT
Kay Sieverse454cea2009-09-18 23:01:12 +0200876 [4] = { "port", 0, &port_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700877#endif
Kay Sieverse454cea2009-09-18 23:01:12 +0200878 [5] = { "zero", 0666, &zero_fops, &zero_bdi },
879 [7] = { "full", 0666, &full_fops, NULL },
880 [8] = { "random", 0666, &random_fops, NULL },
881 [9] = { "urandom", 0666, &urandom_fops, NULL },
882 [11] = { "kmsg", 0, &kmsg_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700883#ifdef CONFIG_CRASH_DUMP
Kay Sieverse454cea2009-09-18 23:01:12 +0200884 [12] = { "oldmem", 0, &oldmem_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700885#endif
886};
887
888static int memory_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200890 int minor;
891 const struct memdev *dev;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700892
Kay Sievers389e0cb2009-07-04 16:51:29 +0200893 minor = iminor(inode);
894 if (minor >= ARRAY_SIZE(devlist))
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200895 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700896
Kay Sievers389e0cb2009-07-04 16:51:29 +0200897 dev = &devlist[minor];
898 if (!dev->fops)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200899 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700900
Kay Sievers389e0cb2009-07-04 16:51:29 +0200901 filp->f_op = dev->fops;
902 if (dev->dev_info)
903 filp->f_mapping->backing_dev_info = dev->dev_info;
904
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700905 /* Is /dev/mem or /dev/kmem ? */
906 if (dev->dev_info == &directly_mappable_cdev_bdi)
907 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
908
Kay Sievers389e0cb2009-07-04 16:51:29 +0200909 if (dev->fops->open)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200910 return dev->fops->open(inode, filp);
911
912 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
Arjan van de Ven62322d22006-07-03 00:24:21 -0700915static const struct file_operations memory_fops = {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800916 .open = memory_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200917 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918};
919
Kay Sieverse454cea2009-09-18 23:01:12 +0200920static char *mem_devnode(struct device *dev, mode_t *mode)
921{
922 if (mode && devlist[MINOR(dev->devt)].mode)
923 *mode = devlist[MINOR(dev->devt)].mode;
924 return NULL;
925}
926
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800927static struct class *mem_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929static int __init chr_dev_init(void)
930{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200931 int minor;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700932 int err;
933
934 err = bdi_init(&zero_bdi);
935 if (err)
936 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Andrew Mortond7d4d842010-03-10 15:21:52 -0800938 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
940
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800941 mem_class = class_create(THIS_MODULE, "mem");
Anton Blanchard6e191f72010-04-06 14:34:55 -0700942 if (IS_ERR(mem_class))
943 return PTR_ERR(mem_class);
944
Kay Sieverse454cea2009-09-18 23:01:12 +0200945 mem_class->devnode = mem_devnode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200946 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
947 if (!devlist[minor].name)
948 continue;
949 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
950 NULL, devlist[minor].name);
951 }
Greg Kroah-Hartmanebf644c2006-07-25 17:13:31 -0700952
David Howells31d1d482010-08-06 16:34:43 +0100953 return tty_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
955
956fs_initcall(chr_dev_init);