blob: 6e0cbe09222059f0d8e8ccf821edfee5c575703e [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;
Julius Wernerb299cde2017-05-12 14:42:58 -0700343 phys_addr_t offset = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
344
345 /* It's illegal to wrap around the end of the physical address space. */
346 if (offset + (phys_addr_t)size < offset)
347 return -EINVAL;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800348
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700349 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800350 return -EINVAL;
351
David Howells5da61852006-09-27 01:50:16 -0700352 if (!private_mapping_ok(vma))
353 return -ENOSYS;
354
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800355 if (!range_is_allowed(vma->vm_pgoff, size))
356 return -EPERM;
357
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700358 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
359 &vma->vm_page_prot))
360 return -EINVAL;
361
Roland Dreier8b150472005-10-28 17:46:18 -0700362 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800363 size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700366 vma->vm_ops = &mmap_mem_ops;
367
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700368 /* Remap-pfn-range will mark the range VM_IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (remap_pfn_range(vma,
370 vma->vm_start,
371 vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800372 size,
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700373 vma->vm_page_prot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return -EAGAIN;
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return 0;
377}
378
Andrew Mortond7d4d842010-03-10 15:21:52 -0800379static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Linus Torvalds4bb82552005-08-13 14:22:59 -0700381 unsigned long pfn;
382
Linus Torvalds6d3154c2007-01-22 08:53:24 -0800383 /* Turn a kernel-virtual address into a physical page frame */
384 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800387 * RED-PEN: on some architectures there is more mapped memory than
388 * available in mem_map which pfn_valid checks for. Perhaps should add a
389 * new macro here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 *
391 * RED-PEN: vmalloc is not supported right now.
392 */
Linus Torvalds4bb82552005-08-13 14:22:59 -0700393 if (!pfn_valid(pfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return -EIO;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700395
396 vma->vm_pgoff = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return mmap_mem(file, vma);
398}
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/*
401 * This function reads the *virtual* memory as seen by the kernel.
402 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800403static ssize_t read_kmem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 size_t count, loff_t *ppos)
405{
406 unsigned long p = *ppos;
407 ssize_t low_count, read, sz;
Hans Grob890537b2013-02-06 11:37:20 +0100408 char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800409 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 read = 0;
412 if (p < (unsigned long) high_memory) {
413 low_count = count;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800414 if (count > (unsigned long)high_memory - p)
415 low_count = (unsigned long)high_memory - p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
418 /* we don't have page 0 mapped on sparc and m68k.. */
419 if (p < PAGE_SIZE && low_count > 0) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800420 sz = size_inside_page(p, low_count);
421 if (clear_user(buf, sz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return -EFAULT;
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800423 buf += sz;
424 p += sz;
425 read += sz;
426 low_count -= sz;
427 count -= sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429#endif
430 while (low_count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800431 sz = size_inside_page(p, low_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 /*
434 * On ia64 if a page has been mapped somewhere as
435 * uncached, then it must also be accessed uncached
436 * by the kernel or data corruption may occur
437 */
Thierry Reding4707a342014-07-28 17:20:33 +0200438 kbuf = xlate_dev_kmem_ptr((void *)p);
Robin Murphy488debb92017-01-05 17:15:01 +0000439 if (!virt_addr_valid(kbuf))
440 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 if (copy_to_user(buf, kbuf, sz))
443 return -EFAULT;
444 buf += sz;
445 p += sz;
446 read += sz;
447 low_count -= sz;
448 count -= sz;
449 }
450 }
451
452 if (count > 0) {
453 kbuf = (char *)__get_free_page(GFP_KERNEL);
454 if (!kbuf)
455 return -ENOMEM;
456 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800457 sz = size_inside_page(p, count);
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800458 if (!is_vmalloc_or_module_addr((void *)p)) {
459 err = -ENXIO;
460 break;
461 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800462 sz = vread(kbuf, (char *)p, sz);
463 if (!sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 break;
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800465 if (copy_to_user(buf, kbuf, sz)) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800466 err = -EFAULT;
467 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800469 count -= sz;
470 buf += sz;
471 read += sz;
472 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474 free_page((unsigned long)kbuf);
475 }
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800476 *ppos = p;
477 return read ? read : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
480
Andrew Mortond7d4d842010-03-10 15:21:52 -0800481static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
482 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 ssize_t written, sz;
485 unsigned long copied;
486
487 written = 0;
488#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
489 /* we don't have page 0 mapped on sparc and m68k.. */
Wu Fengguangee323982009-12-14 17:58:10 -0800490 if (p < PAGE_SIZE) {
491 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /* Hmm. Do something? */
493 buf += sz;
494 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 count -= sz;
496 written += sz;
497 }
498#endif
499
500 while (count > 0) {
Thierry Reding4707a342014-07-28 17:20:33 +0200501 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Wu Fengguangee323982009-12-14 17:58:10 -0800503 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800506 * On ia64 if a page has been mapped somewhere as uncached, then
507 * it must also be accessed uncached by the kernel or data
508 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 */
Thierry Reding4707a342014-07-28 17:20:33 +0200510 ptr = xlate_dev_kmem_ptr((void *)p);
Robin Murphy488debb92017-01-05 17:15:01 +0000511 if (!virt_addr_valid(ptr))
512 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 copied = copy_from_user(ptr, buf, sz);
515 if (copied) {
Jan Beulichc654d602006-03-25 03:07:31 -0800516 written += sz - copied;
517 if (written)
518 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return -EFAULT;
520 }
521 buf += sz;
522 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 count -= sz;
524 written += sz;
525 }
526
527 *ppos += written;
528 return written;
529}
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531/*
532 * This function writes to the *virtual* memory as seen by the kernel.
533 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800534static ssize_t write_kmem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 size_t count, loff_t *ppos)
536{
537 unsigned long p = *ppos;
538 ssize_t wrote = 0;
539 ssize_t virtr = 0;
Hans Grob890537b2013-02-06 11:37:20 +0100540 char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800541 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 if (p < (unsigned long) high_memory) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800544 unsigned long to_write = min_t(unsigned long, count,
545 (unsigned long)high_memory - p);
Wu Fengguangee323982009-12-14 17:58:10 -0800546 wrote = do_write_kmem(p, buf, to_write, ppos);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800547 if (wrote != to_write)
548 return wrote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 p += wrote;
550 buf += wrote;
551 count -= wrote;
552 }
553
554 if (count > 0) {
555 kbuf = (char *)__get_free_page(GFP_KERNEL);
556 if (!kbuf)
557 return wrote ? wrote : -ENOMEM;
558 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800559 unsigned long sz = size_inside_page(p, count);
560 unsigned long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800562 if (!is_vmalloc_or_module_addr((void *)p)) {
563 err = -ENXIO;
564 break;
565 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800566 n = copy_from_user(kbuf, buf, sz);
567 if (n) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800568 err = -EFAULT;
569 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
Wu Fengguangc85e9a92010-02-02 13:44:06 -0800571 vwrite(kbuf, (char *)p, sz);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800572 count -= sz;
573 buf += sz;
574 virtr += sz;
575 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577 free_page((unsigned long)kbuf);
578 }
579
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800580 *ppos = p;
581 return virtr + wrote ? : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Andrew Mortond7d4d842010-03-10 15:21:52 -0800584static ssize_t read_port(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 size_t count, loff_t *ppos)
586{
587 unsigned long i = *ppos;
588 char __user *tmp = buf;
589
590 if (!access_ok(VERIFY_WRITE, buf, count))
Andrew Mortond7d4d842010-03-10 15:21:52 -0800591 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 while (count-- > 0 && i < 65536) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800593 if (__put_user(inb(i), tmp) < 0)
594 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 i++;
596 tmp++;
597 }
598 *ppos = i;
599 return tmp-buf;
600}
601
Andrew Mortond7d4d842010-03-10 15:21:52 -0800602static ssize_t write_port(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 size_t count, loff_t *ppos)
604{
605 unsigned long i = *ppos;
Hans Grob890537b2013-02-06 11:37:20 +0100606 const char __user *tmp = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Andrew Mortond7d4d842010-03-10 15:21:52 -0800608 if (!access_ok(VERIFY_READ, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return -EFAULT;
610 while (count-- > 0 && i < 65536) {
611 char c;
Rob Ward6a0061b2014-12-20 18:28:36 +0000612
Jan Beulichc654d602006-03-25 03:07:31 -0800613 if (__get_user(c, tmp)) {
614 if (tmp > buf)
615 break;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800616 return -EFAULT;
Jan Beulichc654d602006-03-25 03:07:31 -0800617 }
Andrew Mortond7d4d842010-03-10 15:21:52 -0800618 outb(c, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 i++;
620 tmp++;
621 }
622 *ppos = i;
623 return tmp-buf;
624}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Andrew Mortond7d4d842010-03-10 15:21:52 -0800626static ssize_t read_null(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 size_t count, loff_t *ppos)
628{
629 return 0;
630}
631
Andrew Mortond7d4d842010-03-10 15:21:52 -0800632static ssize_t write_null(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 size_t count, loff_t *ppos)
634{
635 return count;
636}
637
Al Virocd28e282015-04-03 15:57:04 -0400638static ssize_t read_iter_null(struct kiocb *iocb, struct iov_iter *to)
Zach Brown162934d2013-05-07 16:18:27 -0700639{
640 return 0;
641}
642
Al Virocd28e282015-04-03 15:57:04 -0400643static ssize_t write_iter_null(struct kiocb *iocb, struct iov_iter *from)
Zach Brown162934d2013-05-07 16:18:27 -0700644{
Al Virocd28e282015-04-03 15:57:04 -0400645 size_t count = iov_iter_count(from);
646 iov_iter_advance(from, count);
647 return count;
Zach Brown162934d2013-05-07 16:18:27 -0700648}
649
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200650static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
651 struct splice_desc *sd)
652{
653 return sd->len;
654}
655
Andrew Mortond7d4d842010-03-10 15:21:52 -0800656static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200657 loff_t *ppos, size_t len, unsigned int flags)
658{
659 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
660}
661
Al Viro13ba33e2014-08-18 10:04:12 -0400662static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
Zach Brown162934d2013-05-07 16:18:27 -0700663{
664 size_t written = 0;
Zach Brown162934d2013-05-07 16:18:27 -0700665
Al Viro13ba33e2014-08-18 10:04:12 -0400666 while (iov_iter_count(iter)) {
667 size_t chunk = iov_iter_count(iter), n;
Rob Ward6a0061b2014-12-20 18:28:36 +0000668
Al Viro13ba33e2014-08-18 10:04:12 -0400669 if (chunk > PAGE_SIZE)
670 chunk = PAGE_SIZE; /* Just for latency reasons */
671 n = iov_iter_zero(chunk, iter);
672 if (!n && iov_iter_count(iter))
673 return written ? written : -EFAULT;
674 written += n;
675 if (signal_pending(current))
676 return written ? written : -ERESTARTSYS;
677 cond_resched();
Zach Brown162934d2013-05-07 16:18:27 -0700678 }
Al Viro13ba33e2014-08-18 10:04:12 -0400679 return written;
Zach Brown162934d2013-05-07 16:18:27 -0700680}
681
Andrew Mortond7d4d842010-03-10 15:21:52 -0800682static int mmap_zero(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700684#ifndef CONFIG_MMU
685 return -ENOSYS;
686#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (vma->vm_flags & VM_SHARED)
688 return shmem_zero_setup(vma);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700689 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Hugh Dickinsc01d5b32016-07-26 15:26:15 -0700692static unsigned long get_unmapped_area_zero(struct file *file,
693 unsigned long addr, unsigned long len,
694 unsigned long pgoff, unsigned long flags)
695{
696#ifdef CONFIG_MMU
697 if (flags & MAP_SHARED) {
698 /*
699 * mmap_zero() will call shmem_zero_setup() to create a file,
700 * so use shmem's get_unmapped_area in case it can be huge;
701 * and pass NULL for file as in mmap.c's get_unmapped_area(),
702 * so as not to confuse shmem with our handle on "/dev/zero".
703 */
704 return shmem_get_unmapped_area(NULL, addr, len, pgoff, flags);
705 }
706
707 /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
708 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
709#else
710 return -ENOSYS;
711#endif
712}
713
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 Viro59551022016-01-22 15:40:57 -0500742 inode_lock(file_inode(file));
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 */
Andrzej Hajdaecb63a12016-02-15 15:35:21 +0100748 if ((unsigned long long)offset >= -MAX_ERRNO) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800749 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 Viro59551022016-01-22 15:40:57 -0500759 inode_unlock(file_inode(file));
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
Al Virocd28e282015-04-03 15:57:04 -0400771#define write_iter_zero write_iter_null
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772#define open_mem open_port
773#define open_kmem open_mem
774
Rob Ward73f07182014-12-07 15:40:33 +0000775static const struct file_operations __maybe_unused mem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 .llseek = memory_lseek,
777 .read = read_mem,
778 .write = write_mem,
779 .mmap = mmap_mem,
780 .open = open_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100781#ifndef CONFIG_MMU
David Howells5da61852006-09-27 01:50:16 -0700782 .get_unmapped_area = get_unmapped_area_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100783 .mmap_capabilities = memory_mmap_capabilities,
784#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785};
786
Rob Warda8c91252014-12-07 15:40:34 +0000787static const struct file_operations __maybe_unused 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,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100793#ifndef CONFIG_MMU
David Howells5da61852006-09-27 01:50:16 -0700794 .get_unmapped_area = get_unmapped_area_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100795 .mmap_capabilities = memory_mmap_capabilities,
796#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797};
798
Arjan van de Ven62322d22006-07-03 00:24:21 -0700799static const struct file_operations null_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 .llseek = null_lseek,
801 .read = read_null,
802 .write = write_null,
Al Virocd28e282015-04-03 15:57:04 -0400803 .read_iter = read_iter_null,
804 .write_iter = write_iter_null,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200805 .splice_write = splice_write_null,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806};
807
Rob Ward3a4bc2f2014-12-07 15:40:35 +0000808static const struct file_operations __maybe_unused 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};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 .write = write_zero,
Al Viro13ba33e2014-08-18 10:04:12 -0400818 .read_iter = read_iter_zero,
Al Virocd28e282015-04-03 15:57:04 -0400819 .write_iter = write_iter_zero,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 .mmap = mmap_zero,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -0700821 .get_unmapped_area = get_unmapped_area_zero,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100822#ifndef CONFIG_MMU
823 .mmap_capabilities = zero_mmap_capabilities,
824#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825};
826
Arjan van de Ven62322d22006-07-03 00:24:21 -0700827static const struct file_operations full_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 .llseek = full_lseek,
Al Viro13ba33e2014-08-18 10:04:12 -0400829 .read_iter = read_iter_zero,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 .write = write_full,
831};
832
Kay Sievers389e0cb2009-07-04 16:51:29 +0200833static const struct memdev {
834 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400835 umode_t mode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200836 const struct file_operations *fops;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100837 fmode_t fmode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200838} devlist[] = {
Rob Ward73f07182014-12-07 15:40:33 +0000839#ifdef CONFIG_DEVMEM
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100840 [1] = { "mem", 0, &mem_fops, FMODE_UNSIGNED_OFFSET },
Rob Ward73f07182014-12-07 15:40:33 +0000841#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700842#ifdef CONFIG_DEVKMEM
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100843 [2] = { "kmem", 0, &kmem_fops, FMODE_UNSIGNED_OFFSET },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700844#endif
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100845 [3] = { "null", 0666, &null_fops, 0 },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700846#ifdef CONFIG_DEVPORT
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100847 [4] = { "port", 0, &port_fops, 0 },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700848#endif
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100849 [5] = { "zero", 0666, &zero_fops, 0 },
850 [7] = { "full", 0666, &full_fops, 0 },
851 [8] = { "random", 0666, &random_fops, 0 },
852 [9] = { "urandom", 0666, &urandom_fops, 0 },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200853#ifdef CONFIG_PRINTK
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100854 [11] = { "kmsg", 0644, &kmsg_fops, 0 },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200855#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700856};
857
858static int memory_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200860 int minor;
861 const struct memdev *dev;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700862
Kay Sievers389e0cb2009-07-04 16:51:29 +0200863 minor = iminor(inode);
864 if (minor >= ARRAY_SIZE(devlist))
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200865 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700866
Kay Sievers389e0cb2009-07-04 16:51:29 +0200867 dev = &devlist[minor];
868 if (!dev->fops)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200869 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700870
Kay Sievers389e0cb2009-07-04 16:51:29 +0200871 filp->f_op = dev->fops;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100872 filp->f_mode |= dev->fmode;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700873
Kay Sievers389e0cb2009-07-04 16:51:29 +0200874 if (dev->fops->open)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200875 return dev->fops->open(inode, filp);
876
877 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Arjan van de Ven62322d22006-07-03 00:24:21 -0700880static const struct file_operations memory_fops = {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800881 .open = memory_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200882 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883};
884
Al Viro2c9ede52011-07-23 20:24:48 -0400885static char *mem_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +0200886{
887 if (mode && devlist[MINOR(dev->devt)].mode)
888 *mode = devlist[MINOR(dev->devt)].mode;
889 return NULL;
890}
891
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800892static struct class *mem_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894static int __init chr_dev_init(void)
895{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200896 int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Andrew Mortond7d4d842010-03-10 15:21:52 -0800898 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
900
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800901 mem_class = class_create(THIS_MODULE, "mem");
Anton Blanchard6e191f72010-04-06 14:34:55 -0700902 if (IS_ERR(mem_class))
903 return PTR_ERR(mem_class);
904
Kay Sieverse454cea2009-09-18 23:01:12 +0200905 mem_class->devnode = mem_devnode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200906 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
907 if (!devlist[minor].name)
908 continue;
Haren Mynenie1612de2012-07-11 15:18:44 +1000909
910 /*
Hans Grob890537b2013-02-06 11:37:20 +0100911 * Create /dev/port?
Haren Mynenie1612de2012-07-11 15:18:44 +1000912 */
913 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
914 continue;
915
Kay Sievers389e0cb2009-07-04 16:51:29 +0200916 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
917 NULL, devlist[minor].name);
918 }
Greg Kroah-Hartmanebf644c2006-07-25 17:13:31 -0700919
David Howells31d1d482010-08-06 16:34:43 +0100920 return tty_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
923fs_initcall(chr_dev_init);