blob: 38d3069b7f0a45150da1d02615a36b022a3754f9 [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>
Haren Mynenie1612de2012-07-11 15:18:44 +100030#include <linux/io.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070031#include <linux/aio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#ifdef CONFIG_IA64
36# include <linux/efi.h>
37#endif
38
Haren Mynenie1612de2012-07-11 15:18:44 +100039#define DEVPORT_MINOR 4
40
Wu Fengguangf2223182009-12-14 17:58:07 -080041static inline unsigned long size_inside_page(unsigned long start,
42 unsigned long size)
43{
44 unsigned long sz;
45
Wu Fengguang7fabadd2009-12-14 17:58:09 -080046 sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
Wu Fengguangf2223182009-12-14 17:58:07 -080047
Wu Fengguang7fabadd2009-12-14 17:58:09 -080048 return min(sz, size);
Wu Fengguangf2223182009-12-14 17:58:07 -080049}
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -040052static inline int valid_phys_addr_range(phys_addr_t addr, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Changli Gaocfaf3462011-03-23 16:42:58 -070054 return addr + count <= __pa(high_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080056
Lennert Buytenhek06c67be2006-07-10 04:45:27 -070057static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080058{
59 return 1;
60}
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#endif
62
Robert Lovedf30b7c2008-04-29 16:44:10 -040063#if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM)
Ingo Molnard0926332008-07-18 00:26:59 +020064#ifdef CONFIG_STRICT_DEVMEM
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080065static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020066{
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080067 u64 from = ((u64)pfn) << PAGE_SHIFT;
68 u64 to = from + size;
69 u64 cursor = from;
Arjan van de Venae531c22008-04-24 23:40:47 +020070
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080071 while (cursor < to) {
72 if (!devmem_is_allowed(pfn)) {
73 printk(KERN_INFO
74 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
Arjan van de Venae531c22008-04-24 23:40:47 +020075 current->comm, from, to);
76 return 0;
77 }
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080078 cursor += PAGE_SIZE;
79 pfn++;
Arjan van de Venae531c22008-04-24 23:40:47 +020080 }
81 return 1;
82}
83#else
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080084static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020085{
86 return 1;
87}
88#endif
Robert Lovedf30b7c2008-04-29 16:44:10 -040089#endif
Arjan van de Venae531c22008-04-24 23:40:47 +020090
Robert Lovedf30b7c2008-04-29 16:44:10 -040091#ifdef CONFIG_DEVMEM
Andrew Mortond7d4d842010-03-10 15:21:52 -080092void __weak unxlate_dev_mem_ptr(unsigned long phys, void *addr)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -070093{
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/*
Andrew Mortond7d4d842010-03-10 15:21:52 -080097 * This funcion reads the *physical* memory. The f_pos points directly to the
98 * memory location.
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800100static ssize_t read_mem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 size_t count, loff_t *ppos)
102{
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400103 phys_addr_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 ssize_t read, sz;
105 char *ptr;
106
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800107 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return -EFAULT;
109 read = 0;
110#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
111 /* we don't have page 0 mapped on sparc and m68k.. */
112 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800113 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (sz > 0) {
115 if (clear_user(buf, sz))
116 return -EFAULT;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800117 buf += sz;
118 p += sz;
119 count -= sz;
120 read += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122 }
123#endif
124
125 while (count > 0) {
Wu Fengguangfa29e972009-12-14 17:58:08 -0800126 unsigned long remaining;
127
Wu Fengguangf2223182009-12-14 17:58:07 -0800128 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700130 if (!range_is_allowed(p >> PAGE_SHIFT, count))
131 return -EPERM;
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800134 * On ia64 if a page has been mapped somewhere as uncached, then
135 * it must also be accessed uncached by the kernel or data
136 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 */
138 ptr = xlate_dev_mem_ptr(p);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700139 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700141
Wu Fengguangfa29e972009-12-14 17:58:08 -0800142 remaining = copy_to_user(buf, ptr, sz);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700143 unxlate_dev_mem_ptr(p, ptr);
Wu Fengguangfa29e972009-12-14 17:58:08 -0800144 if (remaining)
145 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 buf += sz;
148 p += sz;
149 count -= sz;
150 read += sz;
151 }
152
153 *ppos += read;
154 return read;
155}
156
Andrew Mortond7d4d842010-03-10 15:21:52 -0800157static ssize_t write_mem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 size_t count, loff_t *ppos)
159{
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400160 phys_addr_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 ssize_t written, sz;
162 unsigned long copied;
163 void *ptr;
164
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800165 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return -EFAULT;
167
168 written = 0;
169
170#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
171 /* we don't have page 0 mapped on sparc and m68k.. */
172 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800173 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 /* Hmm. Do something? */
175 buf += sz;
176 p += sz;
177 count -= sz;
178 written += sz;
179 }
180#endif
181
182 while (count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800183 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700185 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
186 return -EPERM;
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800189 * On ia64 if a page has been mapped somewhere as uncached, then
190 * it must also be accessed uncached by the kernel or data
191 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 */
193 ptr = xlate_dev_mem_ptr(p);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700194 if (!ptr) {
Jan Beulichc654d602006-03-25 03:07:31 -0800195 if (written)
196 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return -EFAULT;
198 }
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700199
200 copied = copy_from_user(ptr, buf, sz);
Wu Fengguangfa29e972009-12-14 17:58:08 -0800201 unxlate_dev_mem_ptr(p, ptr);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700202 if (copied) {
203 written += sz - copied;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700204 if (written)
205 break;
206 return -EFAULT;
207 }
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 buf += sz;
210 p += sz;
211 count -= sz;
212 written += sz;
213 }
214
215 *ppos += written;
216 return written;
217}
Robert Lovedf30b7c2008-04-29 16:44:10 -0400218#endif /* CONFIG_DEVMEM */
219
220#if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Andrew Mortond7d4d842010-03-10 15:21:52 -0800222int __weak phys_mem_access_prot_allowed(struct file *file,
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700223 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
224{
225 return 1;
226}
227
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800228#ifndef __HAVE_PHYS_MEM_ACCESS_PROT
Andrew Mortond7d4d842010-03-10 15:21:52 -0800229
230/*
231 * Architectures vary in how they handle caching for addresses
232 * outside of main memory.
233 *
234 */
David Howellsea56f412010-04-06 14:35:08 -0700235#ifdef pgprot_noncached
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400236static int uncached_access(struct file *file, phys_addr_t addr)
Andrew Mortond7d4d842010-03-10 15:21:52 -0800237{
238#if defined(CONFIG_IA64)
239 /*
240 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
241 * attribute aliases.
242 */
243 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
244#elif defined(CONFIG_MIPS)
245 {
246 extern int __uncached_access(struct file *file,
247 unsigned long addr);
248
249 return __uncached_access(file, addr);
250 }
251#else
252 /*
253 * Accessing memory above the top the kernel knows about or through a
254 * file pointer
255 * that was marked O_DSYNC will be done non-cached.
256 */
257 if (file->f_flags & O_DSYNC)
258 return 1;
259 return addr >= __pa(high_memory);
260#endif
261}
David Howellsea56f412010-04-06 14:35:08 -0700262#endif
Andrew Mortond7d4d842010-03-10 15:21:52 -0800263
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800264static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
265 unsigned long size, pgprot_t vma_prot)
266{
267#ifdef pgprot_noncached
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400268 phys_addr_t offset = pfn << PAGE_SHIFT;
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800269
270 if (uncached_access(file, offset))
271 return pgprot_noncached(vma_prot);
272#endif
273 return vma_prot;
274}
275#endif
276
David Howells5da61852006-09-27 01:50:16 -0700277#ifndef CONFIG_MMU
278static unsigned long get_unmapped_area_mem(struct file *file,
279 unsigned long addr,
280 unsigned long len,
281 unsigned long pgoff,
282 unsigned long flags)
283{
284 if (!valid_mmap_phys_addr_range(pgoff, len))
285 return (unsigned long) -EINVAL;
Benjamin Herrenschmidt8a932582007-04-16 22:53:16 -0700286 return pgoff << PAGE_SHIFT;
David Howells5da61852006-09-27 01:50:16 -0700287}
288
289/* can't do an in-place private mapping if there's no MMU */
290static inline int private_mapping_ok(struct vm_area_struct *vma)
291{
292 return vma->vm_flags & VM_MAYSHARE;
293}
294#else
295#define get_unmapped_area_mem NULL
296
297static inline int private_mapping_ok(struct vm_area_struct *vma)
298{
299 return 1;
300}
301#endif
302
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400303static const struct vm_operations_struct mmap_mem_ops = {
Rik van Riel7ae8ed52008-07-23 21:27:07 -0700304#ifdef CONFIG_HAVE_IOREMAP_PROT
305 .access = generic_access_phys
306#endif
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700307};
308
Andrew Mortond7d4d842010-03-10 15:21:52 -0800309static int mmap_mem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800311 size_t size = vma->vm_end - vma->vm_start;
312
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700313 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800314 return -EINVAL;
315
David Howells5da61852006-09-27 01:50:16 -0700316 if (!private_mapping_ok(vma))
317 return -ENOSYS;
318
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800319 if (!range_is_allowed(vma->vm_pgoff, size))
320 return -EPERM;
321
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700322 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
323 &vma->vm_page_prot))
324 return -EINVAL;
325
Roland Dreier8b150472005-10-28 17:46:18 -0700326 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800327 size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700330 vma->vm_ops = &mmap_mem_ops;
331
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700332 /* Remap-pfn-range will mark the range VM_IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (remap_pfn_range(vma,
334 vma->vm_start,
335 vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800336 size,
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700337 vma->vm_page_prot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return -EAGAIN;
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return 0;
341}
Robert Lovedf30b7c2008-04-29 16:44:10 -0400342#endif /* CONFIG_DEVMEM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700344#ifdef CONFIG_DEVKMEM
Andrew Mortond7d4d842010-03-10 15:21:52 -0800345static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Linus Torvalds4bb82552005-08-13 14:22:59 -0700347 unsigned long pfn;
348
Linus Torvalds6d3154c2007-01-22 08:53:24 -0800349 /* Turn a kernel-virtual address into a physical page frame */
350 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800353 * RED-PEN: on some architectures there is more mapped memory than
354 * available in mem_map which pfn_valid checks for. Perhaps should add a
355 * new macro here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 *
357 * RED-PEN: vmalloc is not supported right now.
358 */
Linus Torvalds4bb82552005-08-13 14:22:59 -0700359 if (!pfn_valid(pfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return -EIO;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700361
362 vma->vm_pgoff = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return mmap_mem(file, vma);
364}
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700365#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700367#ifdef CONFIG_CRASH_DUMP
368/*
369 * Read memory corresponding to the old kernel.
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700370 */
Vivek Goyal315c2152005-06-25 14:58:24 -0700371static ssize_t read_oldmem(struct file *file, char __user *buf,
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700372 size_t count, loff_t *ppos)
373{
Vivek Goyal315c2152005-06-25 14:58:24 -0700374 unsigned long pfn, offset;
375 size_t read = 0, csize;
376 int rc = 0;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700377
Maneesh Soni72414d32005-06-25 14:58:28 -0700378 while (count) {
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700379 pfn = *ppos / PAGE_SIZE;
Vivek Goyal315c2152005-06-25 14:58:24 -0700380 if (pfn > saved_max_pfn)
381 return read;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700382
Vivek Goyal315c2152005-06-25 14:58:24 -0700383 offset = (unsigned long)(*ppos % PAGE_SIZE);
384 if (count > PAGE_SIZE - offset)
385 csize = PAGE_SIZE - offset;
386 else
387 csize = count;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700388
Vivek Goyal315c2152005-06-25 14:58:24 -0700389 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
390 if (rc < 0)
391 return rc;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700392 buf += csize;
393 *ppos += csize;
394 read += csize;
395 count -= csize;
396 }
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700397 return read;
398}
399#endif
400
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700401#ifdef CONFIG_DEVKMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402/*
403 * This function reads the *virtual* memory as seen by the kernel.
404 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800405static ssize_t read_kmem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 size_t count, loff_t *ppos)
407{
408 unsigned long p = *ppos;
409 ssize_t low_count, read, sz;
Hans Grob890537b2013-02-06 11:37:20 +0100410 char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800411 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 read = 0;
414 if (p < (unsigned long) high_memory) {
415 low_count = count;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800416 if (count > (unsigned long)high_memory - p)
417 low_count = (unsigned long)high_memory - p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
420 /* we don't have page 0 mapped on sparc and m68k.. */
421 if (p < PAGE_SIZE && low_count > 0) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800422 sz = size_inside_page(p, low_count);
423 if (clear_user(buf, sz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return -EFAULT;
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800425 buf += sz;
426 p += sz;
427 read += sz;
428 low_count -= sz;
429 count -= sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431#endif
432 while (low_count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800433 sz = size_inside_page(p, low_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /*
436 * On ia64 if a page has been mapped somewhere as
437 * uncached, then it must also be accessed uncached
438 * by the kernel or data corruption may occur
439 */
440 kbuf = xlate_dev_kmem_ptr((char *)p);
441
442 if (copy_to_user(buf, kbuf, sz))
443 return -EFAULT;
444 buf += sz;
445 p += sz;
446 read += sz;
447 low_count -= sz;
448 count -= sz;
449 }
450 }
451
452 if (count > 0) {
453 kbuf = (char *)__get_free_page(GFP_KERNEL);
454 if (!kbuf)
455 return -ENOMEM;
456 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800457 sz = size_inside_page(p, count);
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800458 if (!is_vmalloc_or_module_addr((void *)p)) {
459 err = -ENXIO;
460 break;
461 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800462 sz = vread(kbuf, (char *)p, sz);
463 if (!sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 break;
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800465 if (copy_to_user(buf, kbuf, sz)) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800466 err = -EFAULT;
467 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800469 count -= sz;
470 buf += sz;
471 read += sz;
472 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474 free_page((unsigned long)kbuf);
475 }
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800476 *ppos = p;
477 return read ? read : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
480
Andrew Mortond7d4d842010-03-10 15:21:52 -0800481static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
482 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 ssize_t written, sz;
485 unsigned long copied;
486
487 written = 0;
488#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
489 /* we don't have page 0 mapped on sparc and m68k.. */
Wu Fengguangee323982009-12-14 17:58:10 -0800490 if (p < PAGE_SIZE) {
491 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /* Hmm. Do something? */
493 buf += sz;
494 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 count -= sz;
496 written += sz;
497 }
498#endif
499
500 while (count > 0) {
501 char *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Wu Fengguangee323982009-12-14 17:58:10 -0800503 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800506 * On ia64 if a page has been mapped somewhere as uncached, then
507 * it must also be accessed uncached by the kernel or data
508 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 */
Wu Fengguangee323982009-12-14 17:58:10 -0800510 ptr = xlate_dev_kmem_ptr((char *)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 copied = copy_from_user(ptr, buf, sz);
513 if (copied) {
Jan Beulichc654d602006-03-25 03:07:31 -0800514 written += sz - copied;
515 if (written)
516 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return -EFAULT;
518 }
519 buf += sz;
520 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 count -= sz;
522 written += sz;
523 }
524
525 *ppos += written;
526 return written;
527}
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529/*
530 * This function writes to the *virtual* memory as seen by the kernel.
531 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800532static ssize_t write_kmem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 size_t count, loff_t *ppos)
534{
535 unsigned long p = *ppos;
536 ssize_t wrote = 0;
537 ssize_t virtr = 0;
Hans Grob890537b2013-02-06 11:37:20 +0100538 char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800539 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 if (p < (unsigned long) high_memory) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800542 unsigned long to_write = min_t(unsigned long, count,
543 (unsigned long)high_memory - p);
Wu Fengguangee323982009-12-14 17:58:10 -0800544 wrote = do_write_kmem(p, buf, to_write, ppos);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800545 if (wrote != to_write)
546 return wrote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 p += wrote;
548 buf += wrote;
549 count -= wrote;
550 }
551
552 if (count > 0) {
553 kbuf = (char *)__get_free_page(GFP_KERNEL);
554 if (!kbuf)
555 return wrote ? wrote : -ENOMEM;
556 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800557 unsigned long sz = size_inside_page(p, count);
558 unsigned long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800560 if (!is_vmalloc_or_module_addr((void *)p)) {
561 err = -ENXIO;
562 break;
563 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800564 n = copy_from_user(kbuf, buf, sz);
565 if (n) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800566 err = -EFAULT;
567 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Wu Fengguangc85e9a92010-02-02 13:44:06 -0800569 vwrite(kbuf, (char *)p, sz);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800570 count -= sz;
571 buf += sz;
572 virtr += sz;
573 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575 free_page((unsigned long)kbuf);
576 }
577
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800578 *ppos = p;
579 return virtr + wrote ? : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
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
Andrew Mortond7d4d842010-03-10 15:21:52 -0800584static ssize_t read_port(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 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))
Andrew Mortond7d4d842010-03-10 15:21:52 -0800591 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 while (count-- > 0 && i < 65536) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800593 if (__put_user(inb(i), tmp) < 0)
594 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 i++;
596 tmp++;
597 }
598 *ppos = i;
599 return tmp-buf;
600}
601
Andrew Mortond7d4d842010-03-10 15:21:52 -0800602static ssize_t write_port(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 size_t count, loff_t *ppos)
604{
605 unsigned long i = *ppos;
Hans Grob890537b2013-02-06 11:37:20 +0100606 const char __user *tmp = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Andrew Mortond7d4d842010-03-10 15:21:52 -0800608 if (!access_ok(VERIFY_READ, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 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;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800615 return -EFAULT;
Jan Beulichc654d602006-03-25 03:07:31 -0800616 }
Andrew Mortond7d4d842010-03-10 15:21:52 -0800617 outb(c, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 i++;
619 tmp++;
620 }
621 *ppos = i;
622 return tmp-buf;
623}
624#endif
625
Andrew Mortond7d4d842010-03-10 15:21:52 -0800626static ssize_t read_null(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 size_t count, loff_t *ppos)
628{
629 return 0;
630}
631
Andrew Mortond7d4d842010-03-10 15:21:52 -0800632static ssize_t write_null(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 size_t count, loff_t *ppos)
634{
635 return count;
636}
637
Zach Brown162934d2013-05-07 16:18:27 -0700638static ssize_t aio_read_null(struct kiocb *iocb, const struct iovec *iov,
639 unsigned long nr_segs, loff_t pos)
640{
641 return 0;
642}
643
644static ssize_t aio_write_null(struct kiocb *iocb, const struct iovec *iov,
645 unsigned long nr_segs, loff_t pos)
646{
647 return iov_length(iov, nr_segs);
648}
649
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200650static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
651 struct splice_desc *sd)
652{
653 return sd->len;
654}
655
Andrew Mortond7d4d842010-03-10 15:21:52 -0800656static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200657 loff_t *ppos, size_t len, unsigned int flags)
658{
659 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
660}
661
Andrew Mortond7d4d842010-03-10 15:21:52 -0800662static ssize_t read_zero(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 size_t count, loff_t *ppos)
664{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700665 size_t written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 if (!count)
668 return 0;
669
670 if (!access_ok(VERIFY_WRITE, buf, count))
671 return -EFAULT;
672
Nick Piggin557ed1f2007-10-16 01:24:40 -0700673 written = 0;
674 while (count) {
675 unsigned long unwritten;
676 size_t chunk = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Nick Piggin557ed1f2007-10-16 01:24:40 -0700678 if (chunk > PAGE_SIZE)
679 chunk = PAGE_SIZE; /* Just for latency reasons */
Nikanth Karthikesanbb521c52009-09-23 15:57:09 -0700680 unwritten = __clear_user(buf, chunk);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700681 written += chunk - unwritten;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (unwritten)
Nick Piggin557ed1f2007-10-16 01:24:40 -0700683 break;
Linus Torvalds2b838682009-06-09 20:40:25 -0700684 if (signal_pending(current))
685 return written ? written : -ERESTARTSYS;
Nick Piggin557ed1f2007-10-16 01:24:40 -0700686 buf += chunk;
687 count -= chunk;
688 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return written ? written : -EFAULT;
691}
692
Zach Brown162934d2013-05-07 16:18:27 -0700693static ssize_t aio_read_zero(struct kiocb *iocb, const struct iovec *iov,
694 unsigned long nr_segs, loff_t pos)
695{
696 size_t written = 0;
697 unsigned long i;
698 ssize_t ret;
699
700 for (i = 0; i < nr_segs; i++) {
701 ret = read_zero(iocb->ki_filp, iov[i].iov_base, iov[i].iov_len,
702 &pos);
703 if (ret < 0)
704 break;
705 written += ret;
706 }
707
708 return written ? written : -EFAULT;
709}
710
Andrew Mortond7d4d842010-03-10 15:21:52 -0800711static int mmap_zero(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700713#ifndef CONFIG_MMU
714 return -ENOSYS;
715#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (vma->vm_flags & VM_SHARED)
717 return shmem_zero_setup(vma);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700718 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Andrew Mortond7d4d842010-03-10 15:21:52 -0800721static ssize_t write_full(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 size_t count, loff_t *ppos)
723{
724 return -ENOSPC;
725}
726
727/*
728 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
729 * can fopen() both devices with "a" now. This was previously impossible.
730 * -- SRB.
731 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800732static loff_t null_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 return file->f_pos = 0;
735}
736
Robert Lovedf30b7c2008-04-29 16:44:10 -0400737#if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM) || defined(CONFIG_DEVPORT)
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739/*
740 * The memory devices use the full 32/64 bits of the offset, and so we cannot
741 * check against negative addresses: they are ok. The return value is weird,
742 * though, in that case (0).
743 *
744 * also note that seeking relative to the "end of file" isn't supported:
745 * it has no meaning, so it returns -EINVAL.
746 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800747static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 loff_t ret;
750
Al Viro496ad9a2013-01-23 17:07:38 -0500751 mutex_lock(&file_inode(file)->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 switch (orig) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800753 case SEEK_CUR:
754 offset += file->f_pos;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800755 case SEEK_SET:
756 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
757 if ((unsigned long long)offset >= ~0xFFFULL) {
758 ret = -EOVERFLOW;
759 break;
760 }
761 file->f_pos = offset;
762 ret = file->f_pos;
763 force_successful_syscall_return();
764 break;
765 default:
766 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
Al Viro496ad9a2013-01-23 17:07:38 -0500768 mutex_unlock(&file_inode(file)->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return ret;
770}
771
Robert Lovedf30b7c2008-04-29 16:44:10 -0400772#endif
773
774#if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM) || defined(CONFIG_DEVPORT)
Hans Grob890537b2013-02-06 11:37:20 +0100775static int open_port(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
778}
Robert Lovedf30b7c2008-04-29 16:44:10 -0400779#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781#define zero_lseek null_lseek
782#define full_lseek null_lseek
783#define write_zero write_null
784#define read_full read_zero
Zach Brown162934d2013-05-07 16:18:27 -0700785#define aio_write_zero aio_write_null
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786#define open_mem open_port
787#define open_kmem open_mem
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700788#define open_oldmem open_mem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Robert Lovedf30b7c2008-04-29 16:44:10 -0400790#ifdef CONFIG_DEVMEM
Arjan van de Ven62322d22006-07-03 00:24:21 -0700791static const struct file_operations mem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 .llseek = memory_lseek,
793 .read = read_mem,
794 .write = write_mem,
795 .mmap = mmap_mem,
796 .open = open_mem,
David Howells5da61852006-09-27 01:50:16 -0700797 .get_unmapped_area = get_unmapped_area_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798};
Robert Lovedf30b7c2008-04-29 16:44:10 -0400799#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700801#ifdef CONFIG_DEVKMEM
Arjan van de Ven62322d22006-07-03 00:24:21 -0700802static const struct file_operations kmem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 .llseek = memory_lseek,
804 .read = read_kmem,
805 .write = write_kmem,
806 .mmap = mmap_kmem,
807 .open = open_kmem,
David Howells5da61852006-09-27 01:50:16 -0700808 .get_unmapped_area = get_unmapped_area_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809};
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700810#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Arjan van de Ven62322d22006-07-03 00:24:21 -0700812static const struct file_operations null_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 .llseek = null_lseek,
814 .read = read_null,
815 .write = write_null,
Zach Brown162934d2013-05-07 16:18:27 -0700816 .aio_read = aio_read_null,
817 .aio_write = aio_write_null,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200818 .splice_write = splice_write_null,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819};
820
Russell King4f911d62007-05-08 00:28:17 -0700821#ifdef CONFIG_DEVPORT
Arjan van de Ven62322d22006-07-03 00:24:21 -0700822static const struct file_operations port_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 .llseek = memory_lseek,
824 .read = read_port,
825 .write = write_port,
826 .open = open_port,
827};
828#endif
829
Arjan van de Ven62322d22006-07-03 00:24:21 -0700830static const struct file_operations zero_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 .llseek = zero_lseek,
832 .read = read_zero,
833 .write = write_zero,
Zach Brown162934d2013-05-07 16:18:27 -0700834 .aio_read = aio_read_zero,
835 .aio_write = aio_write_zero,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 .mmap = mmap_zero,
837};
838
David Howells5da61852006-09-27 01:50:16 -0700839/*
840 * capabilities for /dev/zero
841 * - permits private mappings, "copies" are taken of the source of zeros
Jan Kara371d2172010-09-21 11:49:01 +0200842 * - no writeback happens
David Howells5da61852006-09-27 01:50:16 -0700843 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844static struct backing_dev_info zero_bdi = {
Jens Axboed9938312009-06-12 14:45:52 +0200845 .name = "char/mem",
Jan Kara371d2172010-09-21 11:49:01 +0200846 .capabilities = BDI_CAP_MAP_COPY | BDI_CAP_NO_ACCT_AND_WRITEBACK,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847};
848
Arjan van de Ven62322d22006-07-03 00:24:21 -0700849static const struct file_operations full_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 .llseek = full_lseek,
851 .read = read_full,
852 .write = write_full,
853};
854
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700855#ifdef CONFIG_CRASH_DUMP
Arjan van de Ven62322d22006-07-03 00:24:21 -0700856static const struct file_operations oldmem_fops = {
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700857 .read = read_oldmem,
858 .open = open_oldmem,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200859 .llseek = default_llseek,
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700860};
861#endif
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 Lovedf30b7c2008-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 Lovedf30b7c2008-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 },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200883#ifdef CONFIG_PRINTK
Kay Sieverse11fea92012-05-03 02:29:41 +0200884 [11] = { "kmsg", 0644, &kmsg_fops, NULL },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200885#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700886#ifdef CONFIG_CRASH_DUMP
Kay Sieverse454cea2009-09-18 23:01:12 +0200887 [12] = { "oldmem", 0, &oldmem_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700888#endif
889};
890
891static int memory_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200893 int minor;
894 const struct memdev *dev;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700895
Kay Sievers389e0cb2009-07-04 16:51:29 +0200896 minor = iminor(inode);
897 if (minor >= ARRAY_SIZE(devlist))
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200898 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700899
Kay Sievers389e0cb2009-07-04 16:51:29 +0200900 dev = &devlist[minor];
901 if (!dev->fops)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200902 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700903
Kay Sievers389e0cb2009-07-04 16:51:29 +0200904 filp->f_op = dev->fops;
905 if (dev->dev_info)
906 filp->f_mapping->backing_dev_info = dev->dev_info;
907
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700908 /* Is /dev/mem or /dev/kmem ? */
909 if (dev->dev_info == &directly_mappable_cdev_bdi)
910 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
911
Kay Sievers389e0cb2009-07-04 16:51:29 +0200912 if (dev->fops->open)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200913 return dev->fops->open(inode, filp);
914
915 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
Arjan van de Ven62322d22006-07-03 00:24:21 -0700918static const struct file_operations memory_fops = {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800919 .open = memory_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200920 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921};
922
Al Viro2c9ede52011-07-23 20:24:48 -0400923static char *mem_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +0200924{
925 if (mode && devlist[MINOR(dev->devt)].mode)
926 *mode = devlist[MINOR(dev->devt)].mode;
927 return NULL;
928}
929
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800930static struct class *mem_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932static int __init chr_dev_init(void)
933{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200934 int minor;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700935 int err;
936
937 err = bdi_init(&zero_bdi);
938 if (err)
939 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Andrew Mortond7d4d842010-03-10 15:21:52 -0800941 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
943
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800944 mem_class = class_create(THIS_MODULE, "mem");
Anton Blanchard6e191f72010-04-06 14:34:55 -0700945 if (IS_ERR(mem_class))
946 return PTR_ERR(mem_class);
947
Kay Sieverse454cea2009-09-18 23:01:12 +0200948 mem_class->devnode = mem_devnode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200949 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
950 if (!devlist[minor].name)
951 continue;
Haren Mynenie1612de2012-07-11 15:18:44 +1000952
953 /*
Hans Grob890537b2013-02-06 11:37:20 +0100954 * Create /dev/port?
Haren Mynenie1612de2012-07-11 15:18:44 +1000955 */
956 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
957 continue;
958
Kay Sievers389e0cb2009-07-04 16:51:29 +0200959 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
960 NULL, devlist[minor].name);
961 }
Greg Kroah-Hartmanebf644c2006-07-25 17:13:31 -0700962
David Howells31d1d482010-08-06 16:34:43 +0100963 return tty_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
966fs_initcall(chr_dev_init);