blob: 5b052449285555fa2f1bd1e9093bd880f14654ce [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/char/mem.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
Andrew Mortond7d4d842010-03-10 15:21:52 -08006 * Added devfs support.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008 * Shared /dev/zero mmapping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/mm.h>
12#include <linux/miscdevice.h>
13#include <linux/slab.h>
14#include <linux/vmalloc.h>
15#include <linux/mman.h>
16#include <linux/random.h>
17#include <linux/init.h>
18#include <linux/raw.h>
19#include <linux/tty.h>
20#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/ptrace.h>
22#include <linux/device.h>
Vivek Goyal50b1fdb2005-06-25 14:58:23 -070023#include <linux/highmem.h>
24#include <linux/crash_dump.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/backing-dev.h>
Vivek Goyal315c2152005-06-25 14:58:24 -070026#include <linux/bootmem.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020027#include <linux/splice.h>
Linus Torvaldsb8a3ad52006-10-13 08:42:10 -070028#include <linux/pfn.h>
Paul Gortmaker66300e62011-07-10 12:14:53 -040029#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <asm/uaccess.h>
32#include <asm/io.h>
33
34#ifdef CONFIG_IA64
35# include <linux/efi.h>
36#endif
37
Wu Fengguangf2223182009-12-14 17:58:07 -080038static inline unsigned long size_inside_page(unsigned long start,
39 unsigned long size)
40{
41 unsigned long sz;
42
Wu Fengguang7fabadd2009-12-14 17:58:09 -080043 sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
Wu Fengguangf2223182009-12-14 17:58:07 -080044
Wu Fengguang7fabadd2009-12-14 17:58:09 -080045 return min(sz, size);
Wu Fengguangf2223182009-12-14 17:58:07 -080046}
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
Bjorn Helgaas136939a2006-03-26 01:37:05 -080049static inline int valid_phys_addr_range(unsigned long addr, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Changli Gaocfaf3462011-03-23 16:42:58 -070051 return addr + count <= __pa(high_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080053
Lennert Buytenhek06c67be2006-07-10 04:45:27 -070054static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080055{
56 return 1;
57}
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#endif
59
Robert Love1fcada32008-04-29 16:44:10 -040060#if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM)
Ingo Molnard0926332008-07-18 00:26:59 +020061#ifdef CONFIG_STRICT_DEVMEM
Kees Cook5b679d42017-04-05 09:39:08 -070062static inline int page_is_allowed(unsigned long pfn)
63{
64 return devmem_is_allowed(pfn);
65}
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080066static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020067{
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080068 u64 from = ((u64)pfn) << PAGE_SHIFT;
69 u64 to = from + size;
70 u64 cursor = from;
Arjan van de Venae531c22008-04-24 23:40:47 +020071
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080072 while (cursor < to) {
73 if (!devmem_is_allowed(pfn)) {
74 printk(KERN_INFO
75 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
Arjan van de Venae531c22008-04-24 23:40:47 +020076 current->comm, from, to);
77 return 0;
78 }
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080079 cursor += PAGE_SIZE;
80 pfn++;
Arjan van de Venae531c22008-04-24 23:40:47 +020081 }
82 return 1;
83}
84#else
Kees Cook5b679d42017-04-05 09:39:08 -070085static inline int page_is_allowed(unsigned long pfn)
86{
87 return 1;
88}
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080089static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020090{
91 return 1;
92}
93#endif
Robert Love1fcada32008-04-29 16:44:10 -040094#endif
Arjan van de Venae531c22008-04-24 23:40:47 +020095
Robert Love1fcada32008-04-29 16:44:10 -040096#ifdef CONFIG_DEVMEM
Andrew Mortond7d4d842010-03-10 15:21:52 -080097void __weak unxlate_dev_mem_ptr(unsigned long phys, void *addr)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -070098{
99}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800102 * This funcion reads the *physical* memory. The f_pos points directly to the
103 * memory location.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800105static ssize_t read_mem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 size_t count, loff_t *ppos)
107{
108 unsigned long p = *ppos;
109 ssize_t read, sz;
110 char *ptr;
111
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800112 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return -EFAULT;
114 read = 0;
115#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
116 /* we don't have page 0 mapped on sparc and m68k.. */
117 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800118 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (sz > 0) {
120 if (clear_user(buf, sz))
121 return -EFAULT;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800122 buf += sz;
123 p += sz;
124 count -= sz;
125 read += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127 }
128#endif
129
130 while (count > 0) {
Wu Fengguangfa29e972009-12-14 17:58:08 -0800131 unsigned long remaining;
Kees Cook5b679d42017-04-05 09:39:08 -0700132 int allowed;
Wu Fengguangfa29e972009-12-14 17:58:08 -0800133
Wu Fengguangf2223182009-12-14 17:58:07 -0800134 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Kees Cook5b679d42017-04-05 09:39:08 -0700136 allowed = page_is_allowed(p >> PAGE_SHIFT);
137 if (!allowed)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700138 return -EPERM;
Kees Cook5b679d42017-04-05 09:39:08 -0700139 if (allowed == 2) {
140 /* Show zeros for restricted memory. */
141 remaining = clear_user(buf, sz);
142 } else {
143 /*
144 * On ia64 if a page has been mapped somewhere as
145 * uncached, then it must also be accessed uncached
146 * by the kernel or data corruption may occur.
147 */
148 ptr = xlate_dev_mem_ptr(p);
149 if (!ptr)
150 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700151
Kees Cook5b679d42017-04-05 09:39:08 -0700152 remaining = copy_to_user(buf, ptr, sz);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700153
Kees Cook5b679d42017-04-05 09:39:08 -0700154 unxlate_dev_mem_ptr(p, ptr);
155 }
156
Wu Fengguangfa29e972009-12-14 17:58:08 -0800157 if (remaining)
158 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 buf += sz;
161 p += sz;
162 count -= sz;
163 read += sz;
164 }
165
166 *ppos += read;
167 return read;
168}
169
Andrew Mortond7d4d842010-03-10 15:21:52 -0800170static ssize_t write_mem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 size_t count, loff_t *ppos)
172{
173 unsigned long p = *ppos;
174 ssize_t written, sz;
175 unsigned long copied;
176 void *ptr;
177
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800178 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return -EFAULT;
180
181 written = 0;
182
183#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
184 /* we don't have page 0 mapped on sparc and m68k.. */
185 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800186 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /* Hmm. Do something? */
188 buf += sz;
189 p += sz;
190 count -= sz;
191 written += sz;
192 }
193#endif
194
195 while (count > 0) {
Kees Cook5b679d42017-04-05 09:39:08 -0700196 int allowed;
197
Wu Fengguangf2223182009-12-14 17:58:07 -0800198 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Kees Cook5b679d42017-04-05 09:39:08 -0700200 allowed = page_is_allowed(p >> PAGE_SHIFT);
201 if (!allowed)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700202 return -EPERM;
203
Kees Cook5b679d42017-04-05 09:39:08 -0700204 /* Skip actual writing when a page is marked as restricted. */
205 if (allowed == 1) {
206 /*
207 * On ia64 if a page has been mapped somewhere as
208 * uncached, then it must also be accessed uncached
209 * by the kernel or data corruption may occur.
210 */
211 ptr = xlate_dev_mem_ptr(p);
212 if (!ptr) {
213 if (written)
214 break;
215 return -EFAULT;
216 }
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700217
Kees Cook5b679d42017-04-05 09:39:08 -0700218 copied = copy_from_user(ptr, buf, sz);
219 unxlate_dev_mem_ptr(p, ptr);
220 if (copied) {
221 written += sz - copied;
222 if (written)
223 break;
224 return -EFAULT;
225 }
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700226 }
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 buf += sz;
229 p += sz;
230 count -= sz;
231 written += sz;
232 }
233
234 *ppos += written;
235 return written;
236}
Robert Love1fcada32008-04-29 16:44:10 -0400237#endif /* CONFIG_DEVMEM */
238
239#if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Andrew Mortond7d4d842010-03-10 15:21:52 -0800241int __weak phys_mem_access_prot_allowed(struct file *file,
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700242 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
243{
244 return 1;
245}
246
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800247#ifndef __HAVE_PHYS_MEM_ACCESS_PROT
Andrew Mortond7d4d842010-03-10 15:21:52 -0800248
249/*
250 * Architectures vary in how they handle caching for addresses
251 * outside of main memory.
252 *
253 */
David Howellsea56f412010-04-06 14:35:08 -0700254#ifdef pgprot_noncached
Andrew Mortond7d4d842010-03-10 15:21:52 -0800255static int uncached_access(struct file *file, unsigned long addr)
256{
257#if defined(CONFIG_IA64)
258 /*
259 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
260 * attribute aliases.
261 */
262 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
263#elif defined(CONFIG_MIPS)
264 {
265 extern int __uncached_access(struct file *file,
266 unsigned long addr);
267
268 return __uncached_access(file, addr);
269 }
270#else
271 /*
272 * Accessing memory above the top the kernel knows about or through a
273 * file pointer
274 * that was marked O_DSYNC will be done non-cached.
275 */
276 if (file->f_flags & O_DSYNC)
277 return 1;
278 return addr >= __pa(high_memory);
279#endif
280}
David Howellsea56f412010-04-06 14:35:08 -0700281#endif
Andrew Mortond7d4d842010-03-10 15:21:52 -0800282
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800283static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
284 unsigned long size, pgprot_t vma_prot)
285{
286#ifdef pgprot_noncached
287 unsigned long offset = pfn << PAGE_SHIFT;
288
289 if (uncached_access(file, offset))
290 return pgprot_noncached(vma_prot);
291#endif
292 return vma_prot;
293}
294#endif
295
David Howells5da61852006-09-27 01:50:16 -0700296#ifndef CONFIG_MMU
297static unsigned long get_unmapped_area_mem(struct file *file,
298 unsigned long addr,
299 unsigned long len,
300 unsigned long pgoff,
301 unsigned long flags)
302{
303 if (!valid_mmap_phys_addr_range(pgoff, len))
304 return (unsigned long) -EINVAL;
Benjamin Herrenschmidt8a932582007-04-16 22:53:16 -0700305 return pgoff << PAGE_SHIFT;
David Howells5da61852006-09-27 01:50:16 -0700306}
307
308/* can't do an in-place private mapping if there's no MMU */
309static inline int private_mapping_ok(struct vm_area_struct *vma)
310{
311 return vma->vm_flags & VM_MAYSHARE;
312}
313#else
314#define get_unmapped_area_mem NULL
315
316static inline int private_mapping_ok(struct vm_area_struct *vma)
317{
318 return 1;
319}
320#endif
321
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400322static const struct vm_operations_struct mmap_mem_ops = {
Rik van Riel7ae8ed52008-07-23 21:27:07 -0700323#ifdef CONFIG_HAVE_IOREMAP_PROT
324 .access = generic_access_phys
325#endif
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700326};
327
Andrew Mortond7d4d842010-03-10 15:21:52 -0800328static int mmap_mem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800330 size_t size = vma->vm_end - vma->vm_start;
331
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700332 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800333 return -EINVAL;
334
David Howells5da61852006-09-27 01:50:16 -0700335 if (!private_mapping_ok(vma))
336 return -ENOSYS;
337
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800338 if (!range_is_allowed(vma->vm_pgoff, size))
339 return -EPERM;
340
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700341 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
342 &vma->vm_page_prot))
343 return -EINVAL;
344
Roland Dreier8b150472005-10-28 17:46:18 -0700345 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800346 size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700349 vma->vm_ops = &mmap_mem_ops;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
352 if (remap_pfn_range(vma,
353 vma->vm_start,
354 vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800355 size,
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700356 vma->vm_page_prot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return -EAGAIN;
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return 0;
360}
Robert Love1fcada32008-04-29 16:44:10 -0400361#endif /* CONFIG_DEVMEM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700363#ifdef CONFIG_DEVKMEM
Andrew Mortond7d4d842010-03-10 15:21:52 -0800364static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Linus Torvalds4bb82552005-08-13 14:22:59 -0700366 unsigned long pfn;
367
Linus Torvalds6d3154c2007-01-22 08:53:24 -0800368 /* Turn a kernel-virtual address into a physical page frame */
369 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800372 * RED-PEN: on some architectures there is more mapped memory than
373 * available in mem_map which pfn_valid checks for. Perhaps should add a
374 * new macro here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 *
376 * RED-PEN: vmalloc is not supported right now.
377 */
Linus Torvalds4bb82552005-08-13 14:22:59 -0700378 if (!pfn_valid(pfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return -EIO;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700380
381 vma->vm_pgoff = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return mmap_mem(file, vma);
383}
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700384#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700386#ifdef CONFIG_CRASH_DUMP
387/*
388 * Read memory corresponding to the old kernel.
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700389 */
Vivek Goyal315c2152005-06-25 14:58:24 -0700390static ssize_t read_oldmem(struct file *file, char __user *buf,
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700391 size_t count, loff_t *ppos)
392{
Vivek Goyal315c2152005-06-25 14:58:24 -0700393 unsigned long pfn, offset;
394 size_t read = 0, csize;
395 int rc = 0;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700396
Maneesh Soni72414d32005-06-25 14:58:28 -0700397 while (count) {
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700398 pfn = *ppos / PAGE_SIZE;
Vivek Goyal315c2152005-06-25 14:58:24 -0700399 if (pfn > saved_max_pfn)
400 return read;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700401
Vivek Goyal315c2152005-06-25 14:58:24 -0700402 offset = (unsigned long)(*ppos % PAGE_SIZE);
403 if (count > PAGE_SIZE - offset)
404 csize = PAGE_SIZE - offset;
405 else
406 csize = count;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700407
Vivek Goyal315c2152005-06-25 14:58:24 -0700408 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
409 if (rc < 0)
410 return rc;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700411 buf += csize;
412 *ppos += csize;
413 read += csize;
414 count -= csize;
415 }
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700416 return read;
417}
418#endif
419
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700420#ifdef CONFIG_DEVKMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421/*
422 * This function reads the *virtual* memory as seen by the kernel.
423 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800424static ssize_t read_kmem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 size_t count, loff_t *ppos)
426{
427 unsigned long p = *ppos;
428 ssize_t low_count, read, sz;
429 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800430 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 read = 0;
433 if (p < (unsigned long) high_memory) {
434 low_count = count;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800435 if (count > (unsigned long)high_memory - p)
436 low_count = (unsigned long)high_memory - p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
439 /* we don't have page 0 mapped on sparc and m68k.. */
440 if (p < PAGE_SIZE && low_count > 0) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800441 sz = size_inside_page(p, low_count);
442 if (clear_user(buf, sz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return -EFAULT;
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800444 buf += sz;
445 p += sz;
446 read += sz;
447 low_count -= sz;
448 count -= sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450#endif
451 while (low_count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800452 sz = size_inside_page(p, low_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 /*
455 * On ia64 if a page has been mapped somewhere as
456 * uncached, then it must also be accessed uncached
457 * by the kernel or data corruption may occur
458 */
459 kbuf = xlate_dev_kmem_ptr((char *)p);
460
461 if (copy_to_user(buf, kbuf, sz))
462 return -EFAULT;
463 buf += sz;
464 p += sz;
465 read += sz;
466 low_count -= sz;
467 count -= sz;
468 }
469 }
470
471 if (count > 0) {
472 kbuf = (char *)__get_free_page(GFP_KERNEL);
473 if (!kbuf)
474 return -ENOMEM;
475 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800476 sz = size_inside_page(p, count);
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800477 if (!is_vmalloc_or_module_addr((void *)p)) {
478 err = -ENXIO;
479 break;
480 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800481 sz = vread(kbuf, (char *)p, sz);
482 if (!sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 break;
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800484 if (copy_to_user(buf, kbuf, sz)) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800485 err = -EFAULT;
486 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800488 count -= sz;
489 buf += sz;
490 read += sz;
491 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493 free_page((unsigned long)kbuf);
494 }
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800495 *ppos = p;
496 return read ? read : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499
Andrew Mortond7d4d842010-03-10 15:21:52 -0800500static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
501 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 ssize_t written, sz;
504 unsigned long copied;
505
506 written = 0;
507#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
508 /* we don't have page 0 mapped on sparc and m68k.. */
Wu Fengguangee323982009-12-14 17:58:10 -0800509 if (p < PAGE_SIZE) {
510 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* Hmm. Do something? */
512 buf += sz;
513 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 count -= sz;
515 written += sz;
516 }
517#endif
518
519 while (count > 0) {
520 char *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Wu Fengguangee323982009-12-14 17:58:10 -0800522 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800525 * On ia64 if a page has been mapped somewhere as uncached, then
526 * it must also be accessed uncached by the kernel or data
527 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 */
Wu Fengguangee323982009-12-14 17:58:10 -0800529 ptr = xlate_dev_kmem_ptr((char *)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 copied = copy_from_user(ptr, buf, sz);
532 if (copied) {
Jan Beulichc654d602006-03-25 03:07:31 -0800533 written += sz - copied;
534 if (written)
535 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return -EFAULT;
537 }
538 buf += sz;
539 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 count -= sz;
541 written += sz;
542 }
543
544 *ppos += written;
545 return written;
546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548/*
549 * This function writes to the *virtual* memory as seen by the kernel.
550 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800551static ssize_t write_kmem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 size_t count, loff_t *ppos)
553{
554 unsigned long p = *ppos;
555 ssize_t wrote = 0;
556 ssize_t virtr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800558 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 if (p < (unsigned long) high_memory) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800561 unsigned long to_write = min_t(unsigned long, count,
562 (unsigned long)high_memory - p);
Wu Fengguangee323982009-12-14 17:58:10 -0800563 wrote = do_write_kmem(p, buf, to_write, ppos);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800564 if (wrote != to_write)
565 return wrote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 p += wrote;
567 buf += wrote;
568 count -= wrote;
569 }
570
571 if (count > 0) {
572 kbuf = (char *)__get_free_page(GFP_KERNEL);
573 if (!kbuf)
574 return wrote ? wrote : -ENOMEM;
575 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800576 unsigned long sz = size_inside_page(p, count);
577 unsigned long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800579 if (!is_vmalloc_or_module_addr((void *)p)) {
580 err = -ENXIO;
581 break;
582 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800583 n = copy_from_user(kbuf, buf, sz);
584 if (n) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800585 err = -EFAULT;
586 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
Wu Fengguangc85e9a92010-02-02 13:44:06 -0800588 vwrite(kbuf, (char *)p, sz);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800589 count -= sz;
590 buf += sz;
591 virtr += sz;
592 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594 free_page((unsigned long)kbuf);
595 }
596
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800597 *ppos = p;
598 return virtr + wrote ? : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700600#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Russell King4f911d62007-05-08 00:28:17 -0700602#ifdef CONFIG_DEVPORT
Andrew Mortond7d4d842010-03-10 15:21:52 -0800603static ssize_t read_port(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 size_t count, loff_t *ppos)
605{
606 unsigned long i = *ppos;
607 char __user *tmp = buf;
608
609 if (!access_ok(VERIFY_WRITE, buf, count))
Andrew Mortond7d4d842010-03-10 15:21:52 -0800610 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 while (count-- > 0 && i < 65536) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800612 if (__put_user(inb(i), tmp) < 0)
613 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 i++;
615 tmp++;
616 }
617 *ppos = i;
618 return tmp-buf;
619}
620
Andrew Mortond7d4d842010-03-10 15:21:52 -0800621static ssize_t write_port(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 size_t count, loff_t *ppos)
623{
624 unsigned long i = *ppos;
625 const char __user * tmp = buf;
626
Andrew Mortond7d4d842010-03-10 15:21:52 -0800627 if (!access_ok(VERIFY_READ, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return -EFAULT;
629 while (count-- > 0 && i < 65536) {
630 char c;
Jan Beulichc654d602006-03-25 03:07:31 -0800631 if (__get_user(c, tmp)) {
632 if (tmp > buf)
633 break;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800634 return -EFAULT;
Jan Beulichc654d602006-03-25 03:07:31 -0800635 }
Andrew Mortond7d4d842010-03-10 15:21:52 -0800636 outb(c, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 i++;
638 tmp++;
639 }
640 *ppos = i;
641 return tmp-buf;
642}
643#endif
644
Andrew Mortond7d4d842010-03-10 15:21:52 -0800645static ssize_t read_null(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 size_t count, loff_t *ppos)
647{
648 return 0;
649}
650
Andrew Mortond7d4d842010-03-10 15:21:52 -0800651static ssize_t write_null(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 size_t count, loff_t *ppos)
653{
654 return count;
655}
656
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200657static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
658 struct splice_desc *sd)
659{
660 return sd->len;
661}
662
Andrew Mortond7d4d842010-03-10 15:21:52 -0800663static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200664 loff_t *ppos, size_t len, unsigned int flags)
665{
666 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
667}
668
Andrew Mortond7d4d842010-03-10 15:21:52 -0800669static ssize_t read_zero(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 size_t count, loff_t *ppos)
671{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700672 size_t written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 if (!count)
675 return 0;
676
677 if (!access_ok(VERIFY_WRITE, buf, count))
678 return -EFAULT;
679
Nick Piggin557ed1f2007-10-16 01:24:40 -0700680 written = 0;
681 while (count) {
682 unsigned long unwritten;
683 size_t chunk = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Nick Piggin557ed1f2007-10-16 01:24:40 -0700685 if (chunk > PAGE_SIZE)
686 chunk = PAGE_SIZE; /* Just for latency reasons */
Nikanth Karthikesanbb521c52009-09-23 15:57:09 -0700687 unwritten = __clear_user(buf, chunk);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700688 written += chunk - unwritten;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (unwritten)
Nick Piggin557ed1f2007-10-16 01:24:40 -0700690 break;
Linus Torvalds2b838682009-06-09 20:40:25 -0700691 if (signal_pending(current))
692 return written ? written : -ERESTARTSYS;
Nick Piggin557ed1f2007-10-16 01:24:40 -0700693 buf += chunk;
694 count -= chunk;
695 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return written ? written : -EFAULT;
698}
699
Andrew Mortond7d4d842010-03-10 15:21:52 -0800700static int mmap_zero(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700702#ifndef CONFIG_MMU
703 return -ENOSYS;
704#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (vma->vm_flags & VM_SHARED)
706 return shmem_zero_setup(vma);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700707 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Andrew Mortond7d4d842010-03-10 15:21:52 -0800710static ssize_t write_full(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 size_t count, loff_t *ppos)
712{
713 return -ENOSPC;
714}
715
716/*
717 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
718 * can fopen() both devices with "a" now. This was previously impossible.
719 * -- SRB.
720 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800721static loff_t null_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 return file->f_pos = 0;
724}
725
Robert Love1fcada32008-04-29 16:44:10 -0400726#if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM) || defined(CONFIG_DEVPORT)
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728/*
729 * The memory devices use the full 32/64 bits of the offset, and so we cannot
730 * check against negative addresses: they are ok. The return value is weird,
731 * though, in that case (0).
732 *
733 * also note that seeking relative to the "end of file" isn't supported:
734 * it has no meaning, so it returns -EINVAL.
735 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800736static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 loff_t ret;
739
Josef Sipeka7113a92006-12-08 02:36:55 -0800740 mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 switch (orig) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800742 case SEEK_CUR:
743 offset += file->f_pos;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800744 case SEEK_SET:
745 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
746 if ((unsigned long long)offset >= ~0xFFFULL) {
747 ret = -EOVERFLOW;
748 break;
749 }
750 file->f_pos = offset;
751 ret = file->f_pos;
752 force_successful_syscall_return();
753 break;
754 default:
755 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
Josef Sipeka7113a92006-12-08 02:36:55 -0800757 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return ret;
759}
760
Robert Love1fcada32008-04-29 16:44:10 -0400761#endif
762
763#if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM) || defined(CONFIG_DEVPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764static int open_port(struct inode * inode, struct file * filp)
765{
766 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
767}
Robert Love1fcada32008-04-29 16:44:10 -0400768#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770#define zero_lseek null_lseek
771#define full_lseek null_lseek
772#define write_zero write_null
773#define read_full read_zero
774#define open_mem open_port
775#define open_kmem open_mem
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700776#define open_oldmem open_mem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Robert Love1fcada32008-04-29 16:44:10 -0400778#ifdef CONFIG_DEVMEM
Arjan van de Ven62322d22006-07-03 00:24:21 -0700779static const struct file_operations mem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 .llseek = memory_lseek,
781 .read = read_mem,
782 .write = write_mem,
783 .mmap = mmap_mem,
784 .open = open_mem,
David Howells5da61852006-09-27 01:50:16 -0700785 .get_unmapped_area = get_unmapped_area_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786};
Robert Love1fcada32008-04-29 16:44:10 -0400787#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700789#ifdef CONFIG_DEVKMEM
Arjan van de Ven62322d22006-07-03 00:24:21 -0700790static const struct file_operations kmem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 .llseek = memory_lseek,
792 .read = read_kmem,
793 .write = write_kmem,
794 .mmap = mmap_kmem,
795 .open = open_kmem,
David Howells5da61852006-09-27 01:50:16 -0700796 .get_unmapped_area = get_unmapped_area_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797};
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700798#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Arjan van de Ven62322d22006-07-03 00:24:21 -0700800static const struct file_operations null_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 .llseek = null_lseek,
802 .read = read_null,
803 .write = write_null,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200804 .splice_write = splice_write_null,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805};
806
Russell King4f911d62007-05-08 00:28:17 -0700807#ifdef CONFIG_DEVPORT
Arjan van de Ven62322d22006-07-03 00:24:21 -0700808static const struct file_operations port_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 .llseek = memory_lseek,
810 .read = read_port,
811 .write = write_port,
812 .open = open_port,
813};
814#endif
815
Arjan van de Ven62322d22006-07-03 00:24:21 -0700816static const struct file_operations zero_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 .llseek = zero_lseek,
818 .read = read_zero,
819 .write = write_zero,
820 .mmap = mmap_zero,
821};
822
David Howells5da61852006-09-27 01:50:16 -0700823/*
824 * capabilities for /dev/zero
825 * - permits private mappings, "copies" are taken of the source of zeros
Jan Kara371d2172010-09-21 11:49:01 +0200826 * - no writeback happens
David Howells5da61852006-09-27 01:50:16 -0700827 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828static struct backing_dev_info zero_bdi = {
Jens Axboed9938312009-06-12 14:45:52 +0200829 .name = "char/mem",
Jan Kara371d2172010-09-21 11:49:01 +0200830 .capabilities = BDI_CAP_MAP_COPY | BDI_CAP_NO_ACCT_AND_WRITEBACK,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831};
832
Arjan van de Ven62322d22006-07-03 00:24:21 -0700833static const struct file_operations full_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 .llseek = full_lseek,
835 .read = read_full,
836 .write = write_full,
837};
838
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700839#ifdef CONFIG_CRASH_DUMP
Arjan van de Ven62322d22006-07-03 00:24:21 -0700840static const struct file_operations oldmem_fops = {
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700841 .read = read_oldmem,
842 .open = open_oldmem,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200843 .llseek = default_llseek,
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700844};
845#endif
846
Kay Sievers389e0cb2009-07-04 16:51:29 +0200847static const struct memdev {
848 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400849 umode_t mode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200850 const struct file_operations *fops;
851 struct backing_dev_info *dev_info;
852} devlist[] = {
Robert Love1fcada32008-04-29 16:44:10 -0400853#ifdef CONFIG_DEVMEM
Kay Sieverse454cea2009-09-18 23:01:12 +0200854 [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
Robert Love1fcada32008-04-29 16:44:10 -0400855#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700856#ifdef CONFIG_DEVKMEM
Kay Sieverse454cea2009-09-18 23:01:12 +0200857 [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700858#endif
Kay Sieverse454cea2009-09-18 23:01:12 +0200859 [3] = { "null", 0666, &null_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700860#ifdef CONFIG_DEVPORT
Kay Sieverse454cea2009-09-18 23:01:12 +0200861 [4] = { "port", 0, &port_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700862#endif
Kay Sieverse454cea2009-09-18 23:01:12 +0200863 [5] = { "zero", 0666, &zero_fops, &zero_bdi },
864 [7] = { "full", 0666, &full_fops, NULL },
865 [8] = { "random", 0666, &random_fops, NULL },
866 [9] = { "urandom", 0666, &urandom_fops, NULL },
Kay Sievers82c4abe2015-09-15 15:21:13 +0530867 [11] = { "kmsg", 0644, &kmsg_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700868#ifdef CONFIG_CRASH_DUMP
Kay Sieverse454cea2009-09-18 23:01:12 +0200869 [12] = { "oldmem", 0, &oldmem_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700870#endif
871};
872
873static int memory_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200875 int minor;
876 const struct memdev *dev;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700877
Kay Sievers389e0cb2009-07-04 16:51:29 +0200878 minor = iminor(inode);
879 if (minor >= ARRAY_SIZE(devlist))
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200880 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700881
Kay Sievers389e0cb2009-07-04 16:51:29 +0200882 dev = &devlist[minor];
883 if (!dev->fops)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200884 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700885
Kay Sievers389e0cb2009-07-04 16:51:29 +0200886 filp->f_op = dev->fops;
887 if (dev->dev_info)
888 filp->f_mapping->backing_dev_info = dev->dev_info;
889
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700890 /* Is /dev/mem or /dev/kmem ? */
891 if (dev->dev_info == &directly_mappable_cdev_bdi)
892 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
893
Kay Sievers389e0cb2009-07-04 16:51:29 +0200894 if (dev->fops->open)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200895 return dev->fops->open(inode, filp);
896
897 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
Arjan van de Ven62322d22006-07-03 00:24:21 -0700900static const struct file_operations memory_fops = {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800901 .open = memory_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200902 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903};
904
Al Viro2c9ede52011-07-23 20:24:48 -0400905static char *mem_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +0200906{
907 if (mode && devlist[MINOR(dev->devt)].mode)
908 *mode = devlist[MINOR(dev->devt)].mode;
909 return NULL;
910}
911
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800912static struct class *mem_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914static int __init chr_dev_init(void)
915{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200916 int minor;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700917 int err;
918
919 err = bdi_init(&zero_bdi);
920 if (err)
921 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Andrew Mortond7d4d842010-03-10 15:21:52 -0800923 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
925
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800926 mem_class = class_create(THIS_MODULE, "mem");
Anton Blanchard6e191f72010-04-06 14:34:55 -0700927 if (IS_ERR(mem_class))
928 return PTR_ERR(mem_class);
929
Kay Sieverse454cea2009-09-18 23:01:12 +0200930 mem_class->devnode = mem_devnode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200931 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
932 if (!devlist[minor].name)
933 continue;
934 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
935 NULL, devlist[minor].name);
936 }
Greg Kroah-Hartmanebf644c2006-07-25 17:13:31 -0700937
David Howells31d1d482010-08-06 16:34:43 +0100938 return tty_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
941fs_initcall(chr_dev_init);