blob: 7e4a9d1296bb7fb666f6b37ced2757a8585b7d75 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/backing-dev.h>
Hugh Dickinsc01d5b32016-07-26 15:26:15 -070025#include <linux/shmem_fs.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020026#include <linux/splice.h>
Linus Torvaldsb8a3ad52006-10-13 08:42:10 -070027#include <linux/pfn.h>
Paul Gortmaker66300e62011-07-10 12:14:53 -040028#include <linux/export.h>
Haren Mynenie1612de2012-07-11 15:18:44 +100029#include <linux/io.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080030#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Rob Ward35b6c7e2014-12-20 18:28:35 +000032#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#ifdef CONFIG_IA64
35# include <linux/efi.h>
36#endif
37
Haren Mynenie1612de2012-07-11 15:18:44 +100038#define DEVPORT_MINOR 4
39
Wu Fengguangf2223182009-12-14 17:58:07 -080040static inline unsigned long size_inside_page(unsigned long start,
41 unsigned long size)
42{
43 unsigned long sz;
44
Wu Fengguang7fabadd2009-12-14 17:58:09 -080045 sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
Wu Fengguangf2223182009-12-14 17:58:07 -080046
Wu Fengguang7fabadd2009-12-14 17:58:09 -080047 return min(sz, size);
Wu Fengguangf2223182009-12-14 17:58:07 -080048}
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -040051static inline int valid_phys_addr_range(phys_addr_t addr, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Changli Gaocfaf3462011-03-23 16:42:58 -070053 return addr + count <= __pa(high_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080055
Lennert Buytenhek06c67be2006-07-10 04:45:27 -070056static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080057{
58 return 1;
59}
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#endif
61
Ingo Molnard0926332008-07-18 00:26:59 +020062#ifdef CONFIG_STRICT_DEVMEM
Kees Cooka4866aa2017-04-05 09:39:08 -070063static inline int page_is_allowed(unsigned long pfn)
64{
65 return devmem_is_allowed(pfn);
66}
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080067static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020068{
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080069 u64 from = ((u64)pfn) << PAGE_SHIFT;
70 u64 to = from + size;
71 u64 cursor = from;
Arjan van de Venae531c22008-04-24 23:40:47 +020072
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080073 while (cursor < to) {
Jiri Kosina39380b82016-07-08 11:38:28 +020074 if (!devmem_is_allowed(pfn))
Arjan van de Venae531c22008-04-24 23:40:47 +020075 return 0;
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080076 cursor += PAGE_SIZE;
77 pfn++;
Arjan van de Venae531c22008-04-24 23:40:47 +020078 }
79 return 1;
80}
81#else
Kees Cooka4866aa2017-04-05 09:39:08 -070082static inline int page_is_allowed(unsigned long pfn)
83{
84 return 1;
85}
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080086static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020087{
88 return 1;
89}
90#endif
91
Thierry Reding4707a342014-07-28 17:20:33 +020092#ifndef unxlate_dev_mem_ptr
93#define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
94void __weak unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -070095{
96}
Thierry Reding4707a342014-07-28 17:20:33 +020097#endif
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -070098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800100 * This funcion reads the *physical* memory. The f_pos points directly to the
101 * memory location.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800103static ssize_t read_mem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 size_t count, loff_t *ppos)
105{
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400106 phys_addr_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 ssize_t read, sz;
Thierry Reding4707a342014-07-28 17:20:33 +0200108 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Petr Tesarik08d2d002014-01-30 09:48:02 +0100110 if (p != *ppos)
111 return 0;
112
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800113 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return -EFAULT;
115 read = 0;
116#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
117 /* we don't have page 0 mapped on sparc and m68k.. */
118 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800119 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (sz > 0) {
121 if (clear_user(buf, sz))
122 return -EFAULT;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800123 buf += sz;
124 p += sz;
125 count -= sz;
126 read += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
128 }
129#endif
130
131 while (count > 0) {
Wu Fengguangfa29e972009-12-14 17:58:08 -0800132 unsigned long remaining;
Kees Cooka4866aa2017-04-05 09:39:08 -0700133 int allowed;
Wu Fengguangfa29e972009-12-14 17:58:08 -0800134
Wu Fengguangf2223182009-12-14 17:58:07 -0800135 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Kees Cooka4866aa2017-04-05 09:39:08 -0700137 allowed = page_is_allowed(p >> PAGE_SHIFT);
138 if (!allowed)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700139 return -EPERM;
Kees Cooka4866aa2017-04-05 09:39:08 -0700140 if (allowed == 2) {
141 /* Show zeros for restricted memory. */
142 remaining = clear_user(buf, sz);
143 } else {
144 /*
145 * On ia64 if a page has been mapped somewhere as
146 * uncached, then it must also be accessed uncached
147 * by the kernel or data corruption may occur.
148 */
149 ptr = xlate_dev_mem_ptr(p);
150 if (!ptr)
151 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700152
Kees Cooka4866aa2017-04-05 09:39:08 -0700153 remaining = copy_to_user(buf, ptr, sz);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700154
Kees Cooka4866aa2017-04-05 09:39:08 -0700155 unxlate_dev_mem_ptr(p, ptr);
156 }
157
Wu Fengguangfa29e972009-12-14 17:58:08 -0800158 if (remaining)
159 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 buf += sz;
162 p += sz;
163 count -= sz;
164 read += sz;
165 }
166
167 *ppos += read;
168 return read;
169}
170
Andrew Mortond7d4d842010-03-10 15:21:52 -0800171static ssize_t write_mem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 size_t count, loff_t *ppos)
173{
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400174 phys_addr_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 ssize_t written, sz;
176 unsigned long copied;
177 void *ptr;
178
Petr Tesarik08d2d002014-01-30 09:48:02 +0100179 if (p != *ppos)
180 return -EFBIG;
181
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800182 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return -EFAULT;
184
185 written = 0;
186
187#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
188 /* we don't have page 0 mapped on sparc and m68k.. */
189 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800190 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* Hmm. Do something? */
192 buf += sz;
193 p += sz;
194 count -= sz;
195 written += sz;
196 }
197#endif
198
199 while (count > 0) {
Kees Cooka4866aa2017-04-05 09:39:08 -0700200 int allowed;
201
Wu Fengguangf2223182009-12-14 17:58:07 -0800202 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Kees Cooka4866aa2017-04-05 09:39:08 -0700204 allowed = page_is_allowed(p >> PAGE_SHIFT);
205 if (!allowed)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700206 return -EPERM;
207
Kees Cooka4866aa2017-04-05 09:39:08 -0700208 /* Skip actual writing when a page is marked as restricted. */
209 if (allowed == 1) {
210 /*
211 * On ia64 if a page has been mapped somewhere as
212 * uncached, then it must also be accessed uncached
213 * by the kernel or data corruption may occur.
214 */
215 ptr = xlate_dev_mem_ptr(p);
216 if (!ptr) {
217 if (written)
218 break;
219 return -EFAULT;
220 }
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700221
Kees Cooka4866aa2017-04-05 09:39:08 -0700222 copied = copy_from_user(ptr, buf, sz);
223 unxlate_dev_mem_ptr(p, ptr);
224 if (copied) {
225 written += sz - copied;
226 if (written)
227 break;
228 return -EFAULT;
229 }
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700230 }
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 buf += sz;
233 p += sz;
234 count -= sz;
235 written += sz;
236 }
237
238 *ppos += written;
239 return written;
240}
241
Andrew Mortond7d4d842010-03-10 15:21:52 -0800242int __weak phys_mem_access_prot_allowed(struct file *file,
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700243 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
244{
245 return 1;
246}
247
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800248#ifndef __HAVE_PHYS_MEM_ACCESS_PROT
Andrew Mortond7d4d842010-03-10 15:21:52 -0800249
250/*
251 * Architectures vary in how they handle caching for addresses
252 * outside of main memory.
253 *
254 */
David Howellsea56f412010-04-06 14:35:08 -0700255#ifdef pgprot_noncached
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400256static int uncached_access(struct file *file, phys_addr_t addr)
Andrew Mortond7d4d842010-03-10 15:21:52 -0800257{
258#if defined(CONFIG_IA64)
259 /*
260 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
261 * attribute aliases.
262 */
263 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
264#elif defined(CONFIG_MIPS)
265 {
266 extern int __uncached_access(struct file *file,
267 unsigned long addr);
268
269 return __uncached_access(file, addr);
270 }
271#else
272 /*
273 * Accessing memory above the top the kernel knows about or through a
274 * file pointer
275 * that was marked O_DSYNC will be done non-cached.
276 */
277 if (file->f_flags & O_DSYNC)
278 return 1;
279 return addr >= __pa(high_memory);
280#endif
281}
David Howellsea56f412010-04-06 14:35:08 -0700282#endif
Andrew Mortond7d4d842010-03-10 15:21:52 -0800283
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800284static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
285 unsigned long size, pgprot_t vma_prot)
286{
287#ifdef pgprot_noncached
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400288 phys_addr_t offset = pfn << PAGE_SHIFT;
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800289
290 if (uncached_access(file, offset))
291 return pgprot_noncached(vma_prot);
292#endif
293 return vma_prot;
294}
295#endif
296
David Howells5da61852006-09-27 01:50:16 -0700297#ifndef CONFIG_MMU
298static unsigned long get_unmapped_area_mem(struct file *file,
299 unsigned long addr,
300 unsigned long len,
301 unsigned long pgoff,
302 unsigned long flags)
303{
304 if (!valid_mmap_phys_addr_range(pgoff, len))
305 return (unsigned long) -EINVAL;
Benjamin Herrenschmidt8a932582007-04-16 22:53:16 -0700306 return pgoff << PAGE_SHIFT;
David Howells5da61852006-09-27 01:50:16 -0700307}
308
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100309/* permit direct mmap, for read, write or exec */
310static unsigned memory_mmap_capabilities(struct file *file)
311{
312 return NOMMU_MAP_DIRECT |
313 NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC;
314}
315
316static unsigned zero_mmap_capabilities(struct file *file)
317{
318 return NOMMU_MAP_COPY;
319}
320
David Howells5da61852006-09-27 01:50:16 -0700321/* can't do an in-place private mapping if there's no MMU */
322static inline int private_mapping_ok(struct vm_area_struct *vma)
323{
324 return vma->vm_flags & VM_MAYSHARE;
325}
326#else
David Howells5da61852006-09-27 01:50:16 -0700327
328static inline int private_mapping_ok(struct vm_area_struct *vma)
329{
330 return 1;
331}
332#endif
333
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400334static const struct vm_operations_struct mmap_mem_ops = {
Rik van Riel7ae8ed52008-07-23 21:27:07 -0700335#ifdef CONFIG_HAVE_IOREMAP_PROT
336 .access = generic_access_phys
337#endif
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700338};
339
Andrew Mortond7d4d842010-03-10 15:21:52 -0800340static int mmap_mem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800342 size_t size = vma->vm_end - vma->vm_start;
343
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700344 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800345 return -EINVAL;
346
David Howells5da61852006-09-27 01:50:16 -0700347 if (!private_mapping_ok(vma))
348 return -ENOSYS;
349
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800350 if (!range_is_allowed(vma->vm_pgoff, size))
351 return -EPERM;
352
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700353 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
354 &vma->vm_page_prot))
355 return -EINVAL;
356
Roland Dreier8b150472005-10-28 17:46:18 -0700357 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800358 size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700361 vma->vm_ops = &mmap_mem_ops;
362
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700363 /* Remap-pfn-range will mark the range VM_IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (remap_pfn_range(vma,
365 vma->vm_start,
366 vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800367 size,
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700368 vma->vm_page_prot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return -EAGAIN;
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return 0;
372}
373
Andrew Mortond7d4d842010-03-10 15:21:52 -0800374static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Linus Torvalds4bb82552005-08-13 14:22:59 -0700376 unsigned long pfn;
377
Linus Torvalds6d3154c2007-01-22 08:53:24 -0800378 /* Turn a kernel-virtual address into a physical page frame */
379 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800382 * RED-PEN: on some architectures there is more mapped memory than
383 * available in mem_map which pfn_valid checks for. Perhaps should add a
384 * new macro here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 *
386 * RED-PEN: vmalloc is not supported right now.
387 */
Linus Torvalds4bb82552005-08-13 14:22:59 -0700388 if (!pfn_valid(pfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return -EIO;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700390
391 vma->vm_pgoff = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return mmap_mem(file, vma);
393}
394
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 */
Thierry Reding4707a342014-07-28 17:20:33 +0200433 kbuf = xlate_dev_kmem_ptr((void *)p);
Robin Murphy488debb92017-01-05 17:15:01 +0000434 if (!virt_addr_valid(kbuf))
435 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 if (copy_to_user(buf, kbuf, sz))
438 return -EFAULT;
439 buf += sz;
440 p += sz;
441 read += sz;
442 low_count -= sz;
443 count -= sz;
444 }
445 }
446
447 if (count > 0) {
448 kbuf = (char *)__get_free_page(GFP_KERNEL);
449 if (!kbuf)
450 return -ENOMEM;
451 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800452 sz = size_inside_page(p, count);
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800453 if (!is_vmalloc_or_module_addr((void *)p)) {
454 err = -ENXIO;
455 break;
456 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800457 sz = vread(kbuf, (char *)p, sz);
458 if (!sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800460 if (copy_to_user(buf, kbuf, sz)) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800461 err = -EFAULT;
462 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800464 count -= sz;
465 buf += sz;
466 read += sz;
467 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469 free_page((unsigned long)kbuf);
470 }
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800471 *ppos = p;
472 return read ? read : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
475
Andrew Mortond7d4d842010-03-10 15:21:52 -0800476static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
477 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 ssize_t written, sz;
480 unsigned long copied;
481
482 written = 0;
483#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
484 /* we don't have page 0 mapped on sparc and m68k.. */
Wu Fengguangee323982009-12-14 17:58:10 -0800485 if (p < PAGE_SIZE) {
486 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 /* Hmm. Do something? */
488 buf += sz;
489 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 count -= sz;
491 written += sz;
492 }
493#endif
494
495 while (count > 0) {
Thierry Reding4707a342014-07-28 17:20:33 +0200496 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Wu Fengguangee323982009-12-14 17:58:10 -0800498 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800501 * On ia64 if a page has been mapped somewhere as uncached, then
502 * it must also be accessed uncached by the kernel or data
503 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 */
Thierry Reding4707a342014-07-28 17:20:33 +0200505 ptr = xlate_dev_kmem_ptr((void *)p);
Robin Murphy488debb92017-01-05 17:15:01 +0000506 if (!virt_addr_valid(ptr))
507 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 copied = copy_from_user(ptr, buf, sz);
510 if (copied) {
Jan Beulichc654d602006-03-25 03:07:31 -0800511 written += sz - copied;
512 if (written)
513 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return -EFAULT;
515 }
516 buf += sz;
517 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 count -= sz;
519 written += sz;
520 }
521
522 *ppos += written;
523 return written;
524}
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526/*
527 * This function writes to the *virtual* memory as seen by the kernel.
528 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800529static ssize_t write_kmem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 size_t count, loff_t *ppos)
531{
532 unsigned long p = *ppos;
533 ssize_t wrote = 0;
534 ssize_t virtr = 0;
Hans Grob890537b2013-02-06 11:37:20 +0100535 char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800536 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 if (p < (unsigned long) high_memory) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800539 unsigned long to_write = min_t(unsigned long, count,
540 (unsigned long)high_memory - p);
Wu Fengguangee323982009-12-14 17:58:10 -0800541 wrote = do_write_kmem(p, buf, to_write, ppos);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800542 if (wrote != to_write)
543 return wrote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 p += wrote;
545 buf += wrote;
546 count -= wrote;
547 }
548
549 if (count > 0) {
550 kbuf = (char *)__get_free_page(GFP_KERNEL);
551 if (!kbuf)
552 return wrote ? wrote : -ENOMEM;
553 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800554 unsigned long sz = size_inside_page(p, count);
555 unsigned long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800557 if (!is_vmalloc_or_module_addr((void *)p)) {
558 err = -ENXIO;
559 break;
560 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800561 n = copy_from_user(kbuf, buf, sz);
562 if (n) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800563 err = -EFAULT;
564 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
Wu Fengguangc85e9a92010-02-02 13:44:06 -0800566 vwrite(kbuf, (char *)p, sz);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800567 count -= sz;
568 buf += sz;
569 virtr += sz;
570 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572 free_page((unsigned long)kbuf);
573 }
574
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800575 *ppos = p;
576 return virtr + wrote ? : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
Andrew Mortond7d4d842010-03-10 15:21:52 -0800579static ssize_t read_port(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 size_t count, loff_t *ppos)
581{
582 unsigned long i = *ppos;
583 char __user *tmp = buf;
584
585 if (!access_ok(VERIFY_WRITE, buf, count))
Andrew Mortond7d4d842010-03-10 15:21:52 -0800586 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 while (count-- > 0 && i < 65536) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800588 if (__put_user(inb(i), tmp) < 0)
589 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 i++;
591 tmp++;
592 }
593 *ppos = i;
594 return tmp-buf;
595}
596
Andrew Mortond7d4d842010-03-10 15:21:52 -0800597static ssize_t write_port(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 size_t count, loff_t *ppos)
599{
600 unsigned long i = *ppos;
Hans Grob890537b2013-02-06 11:37:20 +0100601 const char __user *tmp = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Andrew Mortond7d4d842010-03-10 15:21:52 -0800603 if (!access_ok(VERIFY_READ, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return -EFAULT;
605 while (count-- > 0 && i < 65536) {
606 char c;
Rob Ward6a0061b2014-12-20 18:28:36 +0000607
Jan Beulichc654d602006-03-25 03:07:31 -0800608 if (__get_user(c, tmp)) {
609 if (tmp > buf)
610 break;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800611 return -EFAULT;
Jan Beulichc654d602006-03-25 03:07:31 -0800612 }
Andrew Mortond7d4d842010-03-10 15:21:52 -0800613 outb(c, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 i++;
615 tmp++;
616 }
617 *ppos = i;
618 return tmp-buf;
619}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Andrew Mortond7d4d842010-03-10 15:21:52 -0800621static ssize_t read_null(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 size_t count, loff_t *ppos)
623{
624 return 0;
625}
626
Andrew Mortond7d4d842010-03-10 15:21:52 -0800627static ssize_t write_null(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 size_t count, loff_t *ppos)
629{
630 return count;
631}
632
Al Virocd28e282015-04-03 15:57:04 -0400633static ssize_t read_iter_null(struct kiocb *iocb, struct iov_iter *to)
Zach Brown162934d2013-05-07 16:18:27 -0700634{
635 return 0;
636}
637
Al Virocd28e282015-04-03 15:57:04 -0400638static ssize_t write_iter_null(struct kiocb *iocb, struct iov_iter *from)
Zach Brown162934d2013-05-07 16:18:27 -0700639{
Al Virocd28e282015-04-03 15:57:04 -0400640 size_t count = iov_iter_count(from);
641 iov_iter_advance(from, count);
642 return count;
Zach Brown162934d2013-05-07 16:18:27 -0700643}
644
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200645static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
646 struct splice_desc *sd)
647{
648 return sd->len;
649}
650
Andrew Mortond7d4d842010-03-10 15:21:52 -0800651static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200652 loff_t *ppos, size_t len, unsigned int flags)
653{
654 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
655}
656
Al Viro13ba33e2014-08-18 10:04:12 -0400657static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
Zach Brown162934d2013-05-07 16:18:27 -0700658{
659 size_t written = 0;
Zach Brown162934d2013-05-07 16:18:27 -0700660
Al Viro13ba33e2014-08-18 10:04:12 -0400661 while (iov_iter_count(iter)) {
662 size_t chunk = iov_iter_count(iter), n;
Rob Ward6a0061b2014-12-20 18:28:36 +0000663
Al Viro13ba33e2014-08-18 10:04:12 -0400664 if (chunk > PAGE_SIZE)
665 chunk = PAGE_SIZE; /* Just for latency reasons */
666 n = iov_iter_zero(chunk, iter);
667 if (!n && iov_iter_count(iter))
668 return written ? written : -EFAULT;
669 written += n;
670 if (signal_pending(current))
671 return written ? written : -ERESTARTSYS;
672 cond_resched();
Zach Brown162934d2013-05-07 16:18:27 -0700673 }
Al Viro13ba33e2014-08-18 10:04:12 -0400674 return written;
Zach Brown162934d2013-05-07 16:18:27 -0700675}
676
Andrew Mortond7d4d842010-03-10 15:21:52 -0800677static int mmap_zero(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700679#ifndef CONFIG_MMU
680 return -ENOSYS;
681#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (vma->vm_flags & VM_SHARED)
683 return shmem_zero_setup(vma);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700684 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Hugh Dickinsc01d5b32016-07-26 15:26:15 -0700687static unsigned long get_unmapped_area_zero(struct file *file,
688 unsigned long addr, unsigned long len,
689 unsigned long pgoff, unsigned long flags)
690{
691#ifdef CONFIG_MMU
692 if (flags & MAP_SHARED) {
693 /*
694 * mmap_zero() will call shmem_zero_setup() to create a file,
695 * so use shmem's get_unmapped_area in case it can be huge;
696 * and pass NULL for file as in mmap.c's get_unmapped_area(),
697 * so as not to confuse shmem with our handle on "/dev/zero".
698 */
699 return shmem_get_unmapped_area(NULL, addr, len, pgoff, flags);
700 }
701
702 /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
703 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
704#else
705 return -ENOSYS;
706#endif
707}
708
Andrew Mortond7d4d842010-03-10 15:21:52 -0800709static ssize_t write_full(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 size_t count, loff_t *ppos)
711{
712 return -ENOSPC;
713}
714
715/*
716 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
717 * can fopen() both devices with "a" now. This was previously impossible.
718 * -- SRB.
719 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800720static loff_t null_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 return file->f_pos = 0;
723}
724
725/*
726 * The memory devices use the full 32/64 bits of the offset, and so we cannot
727 * check against negative addresses: they are ok. The return value is weird,
728 * though, in that case (0).
729 *
730 * also note that seeking relative to the "end of file" isn't supported:
731 * it has no meaning, so it returns -EINVAL.
732 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800733static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 loff_t ret;
736
Al Viro59551022016-01-22 15:40:57 -0500737 inode_lock(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 switch (orig) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800739 case SEEK_CUR:
740 offset += file->f_pos;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800741 case SEEK_SET:
742 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
Andrzej Hajdaecb63a12016-02-15 15:35:21 +0100743 if ((unsigned long long)offset >= -MAX_ERRNO) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800744 ret = -EOVERFLOW;
745 break;
746 }
747 file->f_pos = offset;
748 ret = file->f_pos;
749 force_successful_syscall_return();
750 break;
751 default:
752 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Al Viro59551022016-01-22 15:40:57 -0500754 inode_unlock(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return ret;
756}
757
Hans Grob890537b2013-02-06 11:37:20 +0100758static int open_port(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
760 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
761}
762
763#define zero_lseek null_lseek
764#define full_lseek null_lseek
765#define write_zero write_null
Al Virocd28e282015-04-03 15:57:04 -0400766#define write_iter_zero write_iter_null
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767#define open_mem open_port
768#define open_kmem open_mem
769
Rob Ward73f07182014-12-07 15:40:33 +0000770static const struct file_operations __maybe_unused mem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 .llseek = memory_lseek,
772 .read = read_mem,
773 .write = write_mem,
774 .mmap = mmap_mem,
775 .open = open_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100776#ifndef CONFIG_MMU
David Howells5da61852006-09-27 01:50:16 -0700777 .get_unmapped_area = get_unmapped_area_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100778 .mmap_capabilities = memory_mmap_capabilities,
779#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780};
781
Rob Warda8c91252014-12-07 15:40:34 +0000782static const struct file_operations __maybe_unused kmem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 .llseek = memory_lseek,
784 .read = read_kmem,
785 .write = write_kmem,
786 .mmap = mmap_kmem,
787 .open = open_kmem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100788#ifndef CONFIG_MMU
David Howells5da61852006-09-27 01:50:16 -0700789 .get_unmapped_area = get_unmapped_area_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100790 .mmap_capabilities = memory_mmap_capabilities,
791#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792};
793
Arjan van de Ven62322d22006-07-03 00:24:21 -0700794static const struct file_operations null_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 .llseek = null_lseek,
796 .read = read_null,
797 .write = write_null,
Al Virocd28e282015-04-03 15:57:04 -0400798 .read_iter = read_iter_null,
799 .write_iter = write_iter_null,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200800 .splice_write = splice_write_null,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801};
802
Rob Ward3a4bc2f2014-12-07 15:40:35 +0000803static const struct file_operations __maybe_unused port_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 .llseek = memory_lseek,
805 .read = read_port,
806 .write = write_port,
807 .open = open_port,
808};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Arjan van de Ven62322d22006-07-03 00:24:21 -0700810static const struct file_operations zero_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 .llseek = zero_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 .write = write_zero,
Al Viro13ba33e2014-08-18 10:04:12 -0400813 .read_iter = read_iter_zero,
Al Virocd28e282015-04-03 15:57:04 -0400814 .write_iter = write_iter_zero,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 .mmap = mmap_zero,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -0700816 .get_unmapped_area = get_unmapped_area_zero,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100817#ifndef CONFIG_MMU
818 .mmap_capabilities = zero_mmap_capabilities,
819#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820};
821
Arjan van de Ven62322d22006-07-03 00:24:21 -0700822static const struct file_operations full_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 .llseek = full_lseek,
Al Viro13ba33e2014-08-18 10:04:12 -0400824 .read_iter = read_iter_zero,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 .write = write_full,
826};
827
Kay Sievers389e0cb2009-07-04 16:51:29 +0200828static const struct memdev {
829 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400830 umode_t mode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200831 const struct file_operations *fops;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100832 fmode_t fmode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200833} devlist[] = {
Rob Ward73f07182014-12-07 15:40:33 +0000834#ifdef CONFIG_DEVMEM
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100835 [1] = { "mem", 0, &mem_fops, FMODE_UNSIGNED_OFFSET },
Rob Ward73f07182014-12-07 15:40:33 +0000836#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700837#ifdef CONFIG_DEVKMEM
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100838 [2] = { "kmem", 0, &kmem_fops, FMODE_UNSIGNED_OFFSET },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700839#endif
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100840 [3] = { "null", 0666, &null_fops, 0 },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700841#ifdef CONFIG_DEVPORT
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100842 [4] = { "port", 0, &port_fops, 0 },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700843#endif
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100844 [5] = { "zero", 0666, &zero_fops, 0 },
845 [7] = { "full", 0666, &full_fops, 0 },
846 [8] = { "random", 0666, &random_fops, 0 },
847 [9] = { "urandom", 0666, &urandom_fops, 0 },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200848#ifdef CONFIG_PRINTK
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100849 [11] = { "kmsg", 0644, &kmsg_fops, 0 },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200850#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700851};
852
853static int memory_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200855 int minor;
856 const struct memdev *dev;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700857
Kay Sievers389e0cb2009-07-04 16:51:29 +0200858 minor = iminor(inode);
859 if (minor >= ARRAY_SIZE(devlist))
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200860 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700861
Kay Sievers389e0cb2009-07-04 16:51:29 +0200862 dev = &devlist[minor];
863 if (!dev->fops)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200864 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700865
Kay Sievers389e0cb2009-07-04 16:51:29 +0200866 filp->f_op = dev->fops;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100867 filp->f_mode |= dev->fmode;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700868
Kay Sievers389e0cb2009-07-04 16:51:29 +0200869 if (dev->fops->open)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200870 return dev->fops->open(inode, filp);
871
872 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
Arjan van de Ven62322d22006-07-03 00:24:21 -0700875static const struct file_operations memory_fops = {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800876 .open = memory_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200877 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878};
879
Al Viro2c9ede52011-07-23 20:24:48 -0400880static char *mem_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +0200881{
882 if (mode && devlist[MINOR(dev->devt)].mode)
883 *mode = devlist[MINOR(dev->devt)].mode;
884 return NULL;
885}
886
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800887static struct class *mem_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889static int __init chr_dev_init(void)
890{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200891 int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Andrew Mortond7d4d842010-03-10 15:21:52 -0800893 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
895
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800896 mem_class = class_create(THIS_MODULE, "mem");
Anton Blanchard6e191f72010-04-06 14:34:55 -0700897 if (IS_ERR(mem_class))
898 return PTR_ERR(mem_class);
899
Kay Sieverse454cea2009-09-18 23:01:12 +0200900 mem_class->devnode = mem_devnode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200901 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
902 if (!devlist[minor].name)
903 continue;
Haren Mynenie1612de2012-07-11 15:18:44 +1000904
905 /*
Hans Grob890537b2013-02-06 11:37:20 +0100906 * Create /dev/port?
Haren Mynenie1612de2012-07-11 15:18:44 +1000907 */
908 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
909 continue;
910
Kay Sievers389e0cb2009-07-04 16:51:29 +0200911 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
912 NULL, devlist[minor].name);
913 }
Greg Kroah-Hartmanebf644c2006-07-25 17:13:31 -0700914
David Howells31d1d482010-08-06 16:34:43 +0100915 return tty_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
918fs_initcall(chr_dev_init);