blob: 1ccbe9482faa5dbc3930478a17f493781849f161 [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 Gaocfaf346c2011-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
Ingo Molnard0926332008-07-18 00:26:59 +020063#ifdef CONFIG_STRICT_DEVMEM
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080064static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020065{
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080066 u64 from = ((u64)pfn) << PAGE_SHIFT;
67 u64 to = from + size;
68 u64 cursor = from;
Arjan van de Venae531c22008-04-24 23:40:47 +020069
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080070 while (cursor < to) {
71 if (!devmem_is_allowed(pfn)) {
72 printk(KERN_INFO
73 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
Arjan van de Venae531c22008-04-24 23:40:47 +020074 current->comm, from, to);
75 return 0;
76 }
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080077 cursor += PAGE_SIZE;
78 pfn++;
Arjan van de Venae531c22008-04-24 23:40:47 +020079 }
80 return 1;
81}
82#else
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080083static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020084{
85 return 1;
86}
87#endif
88
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{
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400100 phys_addr_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 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{
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400157 phys_addr_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 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}
215
Andrew Mortond7d4d842010-03-10 15:21:52 -0800216int __weak phys_mem_access_prot_allowed(struct file *file,
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700217 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
218{
219 return 1;
220}
221
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800222#ifndef __HAVE_PHYS_MEM_ACCESS_PROT
Andrew Mortond7d4d842010-03-10 15:21:52 -0800223
224/*
225 * Architectures vary in how they handle caching for addresses
226 * outside of main memory.
227 *
228 */
David Howellsea56f412010-04-06 14:35:08 -0700229#ifdef pgprot_noncached
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400230static int uncached_access(struct file *file, phys_addr_t addr)
Andrew Mortond7d4d842010-03-10 15:21:52 -0800231{
232#if defined(CONFIG_IA64)
233 /*
234 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
235 * attribute aliases.
236 */
237 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
238#elif defined(CONFIG_MIPS)
239 {
240 extern int __uncached_access(struct file *file,
241 unsigned long addr);
242
243 return __uncached_access(file, addr);
244 }
245#else
246 /*
247 * Accessing memory above the top the kernel knows about or through a
248 * file pointer
249 * that was marked O_DSYNC will be done non-cached.
250 */
251 if (file->f_flags & O_DSYNC)
252 return 1;
253 return addr >= __pa(high_memory);
254#endif
255}
David Howellsea56f412010-04-06 14:35:08 -0700256#endif
Andrew Mortond7d4d842010-03-10 15:21:52 -0800257
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800258static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
259 unsigned long size, pgprot_t vma_prot)
260{
261#ifdef pgprot_noncached
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400262 phys_addr_t offset = pfn << PAGE_SHIFT;
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800263
264 if (uncached_access(file, offset))
265 return pgprot_noncached(vma_prot);
266#endif
267 return vma_prot;
268}
269#endif
270
David Howells5da61852006-09-27 01:50:16 -0700271#ifndef CONFIG_MMU
272static unsigned long get_unmapped_area_mem(struct file *file,
273 unsigned long addr,
274 unsigned long len,
275 unsigned long pgoff,
276 unsigned long flags)
277{
278 if (!valid_mmap_phys_addr_range(pgoff, len))
279 return (unsigned long) -EINVAL;
Benjamin Herrenschmidt8a932582007-04-16 22:53:16 -0700280 return pgoff << PAGE_SHIFT;
David Howells5da61852006-09-27 01:50:16 -0700281}
282
283/* can't do an in-place private mapping if there's no MMU */
284static inline int private_mapping_ok(struct vm_area_struct *vma)
285{
286 return vma->vm_flags & VM_MAYSHARE;
287}
288#else
289#define get_unmapped_area_mem NULL
290
291static inline int private_mapping_ok(struct vm_area_struct *vma)
292{
293 return 1;
294}
295#endif
296
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400297static const struct vm_operations_struct mmap_mem_ops = {
Rik van Riel7ae8ed52008-07-23 21:27:07 -0700298#ifdef CONFIG_HAVE_IOREMAP_PROT
299 .access = generic_access_phys
300#endif
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700301};
302
Andrew Mortond7d4d842010-03-10 15:21:52 -0800303static int mmap_mem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800305 size_t size = vma->vm_end - vma->vm_start;
306
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700307 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800308 return -EINVAL;
309
David Howells5da61852006-09-27 01:50:16 -0700310 if (!private_mapping_ok(vma))
311 return -ENOSYS;
312
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800313 if (!range_is_allowed(vma->vm_pgoff, size))
314 return -EPERM;
315
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700316 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
317 &vma->vm_page_prot))
318 return -EINVAL;
319
Roland Dreier8b150472005-10-28 17:46:18 -0700320 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800321 size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700324 vma->vm_ops = &mmap_mem_ops;
325
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700326 /* Remap-pfn-range will mark the range VM_IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (remap_pfn_range(vma,
328 vma->vm_start,
329 vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800330 size,
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700331 vma->vm_page_prot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return -EAGAIN;
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return 0;
335}
336
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700337#ifdef CONFIG_DEVKMEM
Andrew Mortond7d4d842010-03-10 15:21:52 -0800338static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Linus Torvalds4bb82552005-08-13 14:22:59 -0700340 unsigned long pfn;
341
Linus Torvalds6d3154c2007-01-22 08:53:24 -0800342 /* Turn a kernel-virtual address into a physical page frame */
343 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800346 * RED-PEN: on some architectures there is more mapped memory than
347 * available in mem_map which pfn_valid checks for. Perhaps should add a
348 * new macro here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 *
350 * RED-PEN: vmalloc is not supported right now.
351 */
Linus Torvalds4bb82552005-08-13 14:22:59 -0700352 if (!pfn_valid(pfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return -EIO;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700354
355 vma->vm_pgoff = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return mmap_mem(file, vma);
357}
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700358#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700360#ifdef CONFIG_CRASH_DUMP
361/*
362 * Read memory corresponding to the old kernel.
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700363 */
Vivek Goyal315c2152005-06-25 14:58:24 -0700364static ssize_t read_oldmem(struct file *file, char __user *buf,
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700365 size_t count, loff_t *ppos)
366{
Vivek Goyal315c2152005-06-25 14:58:24 -0700367 unsigned long pfn, offset;
368 size_t read = 0, csize;
369 int rc = 0;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700370
Maneesh Soni72414d32005-06-25 14:58:28 -0700371 while (count) {
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700372 pfn = *ppos / PAGE_SIZE;
Vivek Goyal315c2152005-06-25 14:58:24 -0700373 if (pfn > saved_max_pfn)
374 return read;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700375
Vivek Goyal315c2152005-06-25 14:58:24 -0700376 offset = (unsigned long)(*ppos % PAGE_SIZE);
377 if (count > PAGE_SIZE - offset)
378 csize = PAGE_SIZE - offset;
379 else
380 csize = count;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700381
Vivek Goyal315c2152005-06-25 14:58:24 -0700382 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
383 if (rc < 0)
384 return rc;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700385 buf += csize;
386 *ppos += csize;
387 read += csize;
388 count -= csize;
389 }
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700390 return read;
391}
392#endif
393
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700394#ifdef CONFIG_DEVKMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395/*
396 * This function reads the *virtual* memory as seen by the kernel.
397 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800398static ssize_t read_kmem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 size_t count, loff_t *ppos)
400{
401 unsigned long p = *ppos;
402 ssize_t low_count, read, sz;
Hans Grob890537b2013-02-06 11:37:20 +0100403 char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800404 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 read = 0;
407 if (p < (unsigned long) high_memory) {
408 low_count = count;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800409 if (count > (unsigned long)high_memory - p)
410 low_count = (unsigned long)high_memory - p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
413 /* we don't have page 0 mapped on sparc and m68k.. */
414 if (p < PAGE_SIZE && low_count > 0) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800415 sz = size_inside_page(p, low_count);
416 if (clear_user(buf, sz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return -EFAULT;
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800418 buf += sz;
419 p += sz;
420 read += sz;
421 low_count -= sz;
422 count -= sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424#endif
425 while (low_count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800426 sz = size_inside_page(p, low_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 /*
429 * On ia64 if a page has been mapped somewhere as
430 * uncached, then it must also be accessed uncached
431 * by the kernel or data corruption may occur
432 */
433 kbuf = xlate_dev_kmem_ptr((char *)p);
434
435 if (copy_to_user(buf, kbuf, sz))
436 return -EFAULT;
437 buf += sz;
438 p += sz;
439 read += sz;
440 low_count -= sz;
441 count -= sz;
442 }
443 }
444
445 if (count > 0) {
446 kbuf = (char *)__get_free_page(GFP_KERNEL);
447 if (!kbuf)
448 return -ENOMEM;
449 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800450 sz = size_inside_page(p, count);
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800451 if (!is_vmalloc_or_module_addr((void *)p)) {
452 err = -ENXIO;
453 break;
454 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800455 sz = vread(kbuf, (char *)p, sz);
456 if (!sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 break;
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800458 if (copy_to_user(buf, kbuf, sz)) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800459 err = -EFAULT;
460 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800462 count -= sz;
463 buf += sz;
464 read += sz;
465 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467 free_page((unsigned long)kbuf);
468 }
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800469 *ppos = p;
470 return read ? read : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
473
Andrew Mortond7d4d842010-03-10 15:21:52 -0800474static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
475 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 ssize_t written, sz;
478 unsigned long copied;
479
480 written = 0;
481#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
482 /* we don't have page 0 mapped on sparc and m68k.. */
Wu Fengguangee323982009-12-14 17:58:10 -0800483 if (p < PAGE_SIZE) {
484 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 /* Hmm. Do something? */
486 buf += sz;
487 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 count -= sz;
489 written += sz;
490 }
491#endif
492
493 while (count > 0) {
494 char *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Wu Fengguangee323982009-12-14 17:58:10 -0800496 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800499 * On ia64 if a page has been mapped somewhere as uncached, then
500 * it must also be accessed uncached by the kernel or data
501 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 */
Wu Fengguangee323982009-12-14 17:58:10 -0800503 ptr = xlate_dev_kmem_ptr((char *)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 copied = copy_from_user(ptr, buf, sz);
506 if (copied) {
Jan Beulichc654d602006-03-25 03:07:31 -0800507 written += sz - copied;
508 if (written)
509 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return -EFAULT;
511 }
512 buf += sz;
513 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 count -= sz;
515 written += sz;
516 }
517
518 *ppos += written;
519 return written;
520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/*
523 * This function writes to the *virtual* memory as seen by the kernel.
524 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800525static ssize_t write_kmem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 size_t count, loff_t *ppos)
527{
528 unsigned long p = *ppos;
529 ssize_t wrote = 0;
530 ssize_t virtr = 0;
Hans Grob890537b2013-02-06 11:37:20 +0100531 char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800532 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 if (p < (unsigned long) high_memory) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800535 unsigned long to_write = min_t(unsigned long, count,
536 (unsigned long)high_memory - p);
Wu Fengguangee323982009-12-14 17:58:10 -0800537 wrote = do_write_kmem(p, buf, to_write, ppos);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800538 if (wrote != to_write)
539 return wrote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 p += wrote;
541 buf += wrote;
542 count -= wrote;
543 }
544
545 if (count > 0) {
546 kbuf = (char *)__get_free_page(GFP_KERNEL);
547 if (!kbuf)
548 return wrote ? wrote : -ENOMEM;
549 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800550 unsigned long sz = size_inside_page(p, count);
551 unsigned long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800553 if (!is_vmalloc_or_module_addr((void *)p)) {
554 err = -ENXIO;
555 break;
556 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800557 n = copy_from_user(kbuf, buf, sz);
558 if (n) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800559 err = -EFAULT;
560 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
Wu Fengguangc85e9a92010-02-02 13:44:06 -0800562 vwrite(kbuf, (char *)p, sz);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800563 count -= sz;
564 buf += sz;
565 virtr += sz;
566 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568 free_page((unsigned long)kbuf);
569 }
570
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800571 *ppos = p;
572 return virtr + wrote ? : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700574#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Russell King4f911d62007-05-08 00:28:17 -0700576#ifdef CONFIG_DEVPORT
Andrew Mortond7d4d842010-03-10 15:21:52 -0800577static ssize_t read_port(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 size_t count, loff_t *ppos)
579{
580 unsigned long i = *ppos;
581 char __user *tmp = buf;
582
583 if (!access_ok(VERIFY_WRITE, buf, count))
Andrew Mortond7d4d842010-03-10 15:21:52 -0800584 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 while (count-- > 0 && i < 65536) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800586 if (__put_user(inb(i), tmp) < 0)
587 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 i++;
589 tmp++;
590 }
591 *ppos = i;
592 return tmp-buf;
593}
594
Andrew Mortond7d4d842010-03-10 15:21:52 -0800595static ssize_t write_port(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 size_t count, loff_t *ppos)
597{
598 unsigned long i = *ppos;
Hans Grob890537b2013-02-06 11:37:20 +0100599 const char __user *tmp = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Andrew Mortond7d4d842010-03-10 15:21:52 -0800601 if (!access_ok(VERIFY_READ, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return -EFAULT;
603 while (count-- > 0 && i < 65536) {
604 char c;
Jan Beulichc654d602006-03-25 03:07:31 -0800605 if (__get_user(c, tmp)) {
606 if (tmp > buf)
607 break;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800608 return -EFAULT;
Jan Beulichc654d602006-03-25 03:07:31 -0800609 }
Andrew Mortond7d4d842010-03-10 15:21:52 -0800610 outb(c, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 i++;
612 tmp++;
613 }
614 *ppos = i;
615 return tmp-buf;
616}
617#endif
618
Andrew Mortond7d4d842010-03-10 15:21:52 -0800619static ssize_t read_null(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 size_t count, loff_t *ppos)
621{
622 return 0;
623}
624
Andrew Mortond7d4d842010-03-10 15:21:52 -0800625static ssize_t write_null(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 size_t count, loff_t *ppos)
627{
628 return count;
629}
630
Zach Brown162934d2013-05-07 16:18:27 -0700631static ssize_t aio_read_null(struct kiocb *iocb, const struct iovec *iov,
632 unsigned long nr_segs, loff_t pos)
633{
634 return 0;
635}
636
637static ssize_t aio_write_null(struct kiocb *iocb, const struct iovec *iov,
638 unsigned long nr_segs, loff_t pos)
639{
640 return iov_length(iov, nr_segs);
641}
642
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200643static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
644 struct splice_desc *sd)
645{
646 return sd->len;
647}
648
Andrew Mortond7d4d842010-03-10 15:21:52 -0800649static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200650 loff_t *ppos, size_t len, unsigned int flags)
651{
652 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
653}
654
Andrew Mortond7d4d842010-03-10 15:21:52 -0800655static ssize_t read_zero(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 size_t count, loff_t *ppos)
657{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700658 size_t written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 if (!count)
661 return 0;
662
663 if (!access_ok(VERIFY_WRITE, buf, count))
664 return -EFAULT;
665
Nick Piggin557ed1f2007-10-16 01:24:40 -0700666 written = 0;
667 while (count) {
668 unsigned long unwritten;
669 size_t chunk = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Nick Piggin557ed1f2007-10-16 01:24:40 -0700671 if (chunk > PAGE_SIZE)
672 chunk = PAGE_SIZE; /* Just for latency reasons */
Nikanth Karthikesanbb521c52009-09-23 15:57:09 -0700673 unwritten = __clear_user(buf, chunk);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700674 written += chunk - unwritten;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (unwritten)
Nick Piggin557ed1f2007-10-16 01:24:40 -0700676 break;
Linus Torvalds2b838682009-06-09 20:40:25 -0700677 if (signal_pending(current))
678 return written ? written : -ERESTARTSYS;
Nick Piggin557ed1f2007-10-16 01:24:40 -0700679 buf += chunk;
680 count -= chunk;
681 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return written ? written : -EFAULT;
684}
685
Zach Brown162934d2013-05-07 16:18:27 -0700686static ssize_t aio_read_zero(struct kiocb *iocb, const struct iovec *iov,
687 unsigned long nr_segs, loff_t pos)
688{
689 size_t written = 0;
690 unsigned long i;
691 ssize_t ret;
692
693 for (i = 0; i < nr_segs; i++) {
694 ret = read_zero(iocb->ki_filp, iov[i].iov_base, iov[i].iov_len,
695 &pos);
696 if (ret < 0)
697 break;
698 written += ret;
699 }
700
701 return written ? written : -EFAULT;
702}
703
Andrew Mortond7d4d842010-03-10 15:21:52 -0800704static int mmap_zero(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700706#ifndef CONFIG_MMU
707 return -ENOSYS;
708#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (vma->vm_flags & VM_SHARED)
710 return shmem_zero_setup(vma);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Andrew Mortond7d4d842010-03-10 15:21:52 -0800714static ssize_t write_full(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 size_t count, loff_t *ppos)
716{
717 return -ENOSPC;
718}
719
720/*
721 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
722 * can fopen() both devices with "a" now. This was previously impossible.
723 * -- SRB.
724 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800725static loff_t null_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 return file->f_pos = 0;
728}
729
730/*
731 * The memory devices use the full 32/64 bits of the offset, and so we cannot
732 * check against negative addresses: they are ok. The return value is weird,
733 * though, in that case (0).
734 *
735 * also note that seeking relative to the "end of file" isn't supported:
736 * it has no meaning, so it returns -EINVAL.
737 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800738static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
740 loff_t ret;
741
Al Viro496ad9a2013-01-23 17:07:38 -0500742 mutex_lock(&file_inode(file)->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 switch (orig) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800744 case SEEK_CUR:
745 offset += file->f_pos;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800746 case SEEK_SET:
747 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
748 if ((unsigned long long)offset >= ~0xFFFULL) {
749 ret = -EOVERFLOW;
750 break;
751 }
752 file->f_pos = offset;
753 ret = file->f_pos;
754 force_successful_syscall_return();
755 break;
756 default:
757 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Al Viro496ad9a2013-01-23 17:07:38 -0500759 mutex_unlock(&file_inode(file)->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return ret;
761}
762
Hans Grob890537b2013-02-06 11:37:20 +0100763static int open_port(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
766}
767
768#define zero_lseek null_lseek
769#define full_lseek null_lseek
770#define write_zero write_null
771#define read_full read_zero
Zach Brown162934d2013-05-07 16:18:27 -0700772#define aio_write_zero aio_write_null
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773#define open_mem open_port
774#define open_kmem open_mem
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700775#define open_oldmem open_mem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Arjan van de Ven62322d22006-07-03 00:24:21 -0700777static const struct file_operations mem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 .llseek = memory_lseek,
779 .read = read_mem,
780 .write = write_mem,
781 .mmap = mmap_mem,
782 .open = open_mem,
David Howells5da61852006-09-27 01:50:16 -0700783 .get_unmapped_area = get_unmapped_area_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784};
785
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700786#ifdef CONFIG_DEVKMEM
Arjan van de Ven62322d22006-07-03 00:24:21 -0700787static const struct file_operations kmem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 .llseek = memory_lseek,
789 .read = read_kmem,
790 .write = write_kmem,
791 .mmap = mmap_kmem,
792 .open = open_kmem,
David Howells5da61852006-09-27 01:50:16 -0700793 .get_unmapped_area = get_unmapped_area_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794};
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700795#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Arjan van de Ven62322d22006-07-03 00:24:21 -0700797static const struct file_operations null_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 .llseek = null_lseek,
799 .read = read_null,
800 .write = write_null,
Zach Brown162934d2013-05-07 16:18:27 -0700801 .aio_read = aio_read_null,
802 .aio_write = aio_write_null,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200803 .splice_write = splice_write_null,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804};
805
Russell King4f911d62007-05-08 00:28:17 -0700806#ifdef CONFIG_DEVPORT
Arjan van de Ven62322d22006-07-03 00:24:21 -0700807static const struct file_operations port_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 .llseek = memory_lseek,
809 .read = read_port,
810 .write = write_port,
811 .open = open_port,
812};
813#endif
814
Arjan van de Ven62322d22006-07-03 00:24:21 -0700815static const struct file_operations zero_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 .llseek = zero_lseek,
817 .read = read_zero,
818 .write = write_zero,
Zach Brown162934d2013-05-07 16:18:27 -0700819 .aio_read = aio_read_zero,
820 .aio_write = aio_write_zero,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 .mmap = mmap_zero,
822};
823
David Howells5da61852006-09-27 01:50:16 -0700824/*
825 * capabilities for /dev/zero
826 * - permits private mappings, "copies" are taken of the source of zeros
Jan Kara371d2172010-09-21 11:49:01 +0200827 * - no writeback happens
David Howells5da61852006-09-27 01:50:16 -0700828 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829static struct backing_dev_info zero_bdi = {
Jens Axboed9938312009-06-12 14:45:52 +0200830 .name = "char/mem",
Jan Kara371d2172010-09-21 11:49:01 +0200831 .capabilities = BDI_CAP_MAP_COPY | BDI_CAP_NO_ACCT_AND_WRITEBACK,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832};
833
Arjan van de Ven62322d22006-07-03 00:24:21 -0700834static const struct file_operations full_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 .llseek = full_lseek,
836 .read = read_full,
837 .write = write_full,
838};
839
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700840#ifdef CONFIG_CRASH_DUMP
Arjan van de Ven62322d22006-07-03 00:24:21 -0700841static const struct file_operations oldmem_fops = {
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700842 .read = read_oldmem,
843 .open = open_oldmem,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200844 .llseek = default_llseek,
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700845};
846#endif
847
Kay Sievers389e0cb2009-07-04 16:51:29 +0200848static const struct memdev {
849 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400850 umode_t mode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200851 const struct file_operations *fops;
852 struct backing_dev_info *dev_info;
853} devlist[] = {
Kay Sieverse454cea2009-09-18 23:01:12 +0200854 [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700855#ifdef CONFIG_DEVKMEM
Kay Sieverse454cea2009-09-18 23:01:12 +0200856 [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700857#endif
Kay Sieverse454cea2009-09-18 23:01:12 +0200858 [3] = { "null", 0666, &null_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700859#ifdef CONFIG_DEVPORT
Kay Sieverse454cea2009-09-18 23:01:12 +0200860 [4] = { "port", 0, &port_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700861#endif
Kay Sieverse454cea2009-09-18 23:01:12 +0200862 [5] = { "zero", 0666, &zero_fops, &zero_bdi },
863 [7] = { "full", 0666, &full_fops, NULL },
864 [8] = { "random", 0666, &random_fops, NULL },
865 [9] = { "urandom", 0666, &urandom_fops, NULL },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200866#ifdef CONFIG_PRINTK
Kay Sieverse11fea92012-05-03 02:29:41 +0200867 [11] = { "kmsg", 0644, &kmsg_fops, NULL },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200868#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700869#ifdef CONFIG_CRASH_DUMP
Kay Sieverse454cea2009-09-18 23:01:12 +0200870 [12] = { "oldmem", 0, &oldmem_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700871#endif
872};
873
874static int memory_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200876 int minor;
877 const struct memdev *dev;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700878
Kay Sievers389e0cb2009-07-04 16:51:29 +0200879 minor = iminor(inode);
880 if (minor >= ARRAY_SIZE(devlist))
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200881 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700882
Kay Sievers389e0cb2009-07-04 16:51:29 +0200883 dev = &devlist[minor];
884 if (!dev->fops)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200885 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700886
Kay Sievers389e0cb2009-07-04 16:51:29 +0200887 filp->f_op = dev->fops;
888 if (dev->dev_info)
889 filp->f_mapping->backing_dev_info = dev->dev_info;
890
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700891 /* Is /dev/mem or /dev/kmem ? */
892 if (dev->dev_info == &directly_mappable_cdev_bdi)
893 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
894
Kay Sievers389e0cb2009-07-04 16:51:29 +0200895 if (dev->fops->open)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200896 return dev->fops->open(inode, filp);
897
898 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
Arjan van de Ven62322d22006-07-03 00:24:21 -0700901static const struct file_operations memory_fops = {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800902 .open = memory_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200903 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904};
905
Al Viro2c9ede52011-07-23 20:24:48 -0400906static char *mem_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +0200907{
908 if (mode && devlist[MINOR(dev->devt)].mode)
909 *mode = devlist[MINOR(dev->devt)].mode;
910 return NULL;
911}
912
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800913static struct class *mem_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915static int __init chr_dev_init(void)
916{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200917 int minor;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700918 int err;
919
920 err = bdi_init(&zero_bdi);
921 if (err)
922 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Andrew Mortond7d4d842010-03-10 15:21:52 -0800924 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
926
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800927 mem_class = class_create(THIS_MODULE, "mem");
Anton Blanchard6e191f72010-04-06 14:34:55 -0700928 if (IS_ERR(mem_class))
929 return PTR_ERR(mem_class);
930
Kay Sieverse454cea2009-09-18 23:01:12 +0200931 mem_class->devnode = mem_devnode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200932 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
933 if (!devlist[minor].name)
934 continue;
Haren Mynenie1612de2012-07-11 15:18:44 +1000935
936 /*
Hans Grob890537b2013-02-06 11:37:20 +0100937 * Create /dev/port?
Haren Mynenie1612de2012-07-11 15:18:44 +1000938 */
939 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
940 continue;
941
Kay Sievers389e0cb2009-07-04 16:51:29 +0200942 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
943 NULL, devlist[minor].name);
944 }
Greg Kroah-Hartmanebf644c2006-07-25 17:13:31 -0700945
David Howells31d1d482010-08-06 16:34:43 +0100946 return tty_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
949fs_initcall(chr_dev_init);