blob: 6aefe5370e5b15c45bda8e156ccdfd1b99e9e3f5 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/drivers/char/mem.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 *
Andrew Mortond7d4d842010-03-10 15:21:52 -08007 * Added devfs support.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009 * Shared /dev/zero mmapping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/mm.h>
13#include <linux/miscdevice.h>
14#include <linux/slab.h>
15#include <linux/vmalloc.h>
16#include <linux/mman.h>
17#include <linux/random.h>
18#include <linux/init.h>
19#include <linux/raw.h>
20#include <linux/tty.h>
21#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/ptrace.h>
23#include <linux/device.h>
Vivek Goyal50b1fdb2005-06-25 14:58:23 -070024#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/backing-dev.h>
Hugh Dickinsc01d5b32016-07-26 15:26:15 -070026#include <linux/shmem_fs.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020027#include <linux/splice.h>
Linus Torvaldsb8a3ad52006-10-13 08:42:10 -070028#include <linux/pfn.h>
Paul Gortmaker66300e62011-07-10 12:14:53 -040029#include <linux/export.h>
Haren Mynenie1612de2012-07-11 15:18:44 +100030#include <linux/io.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080031#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Rob Ward35b6c7e2014-12-20 18:28:35 +000033#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#ifdef CONFIG_IA64
36# include <linux/efi.h>
37#endif
38
Haren Mynenie1612de2012-07-11 15:18:44 +100039#define DEVPORT_MINOR 4
40
Wu Fengguangf2223182009-12-14 17:58:07 -080041static inline unsigned long size_inside_page(unsigned long start,
42 unsigned long size)
43{
44 unsigned long sz;
45
Wu Fengguang7fabadd2009-12-14 17:58:09 -080046 sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
Wu Fengguangf2223182009-12-14 17:58:07 -080047
Wu Fengguang7fabadd2009-12-14 17:58:09 -080048 return min(sz, size);
Wu Fengguangf2223182009-12-14 17:58:07 -080049}
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -040052static inline int valid_phys_addr_range(phys_addr_t addr, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Changli Gaocfaf3462011-03-23 16:42:58 -070054 return addr + count <= __pa(high_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080056
Lennert Buytenhek06c67be2006-07-10 04:45:27 -070057static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080058{
59 return 1;
60}
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#endif
62
Ingo Molnard0926332008-07-18 00:26:59 +020063#ifdef CONFIG_STRICT_DEVMEM
Kees Cooka4866aa2017-04-05 09:39:08 -070064static inline int page_is_allowed(unsigned long pfn)
65{
66 return devmem_is_allowed(pfn);
67}
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080068static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020069{
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080070 u64 from = ((u64)pfn) << PAGE_SHIFT;
71 u64 to = from + size;
72 u64 cursor = from;
Arjan van de Venae531c22008-04-24 23:40:47 +020073
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080074 while (cursor < to) {
Jiri Kosina39380b82016-07-08 11:38:28 +020075 if (!devmem_is_allowed(pfn))
Arjan van de Venae531c22008-04-24 23:40:47 +020076 return 0;
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080077 cursor += PAGE_SIZE;
78 pfn++;
Arjan van de Venae531c22008-04-24 23:40:47 +020079 }
80 return 1;
81}
82#else
Kees Cooka4866aa2017-04-05 09:39:08 -070083static inline int page_is_allowed(unsigned long pfn)
84{
85 return 1;
86}
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080087static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020088{
89 return 1;
90}
91#endif
92
Thierry Reding4707a342014-07-28 17:20:33 +020093#ifndef unxlate_dev_mem_ptr
94#define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
95void __weak unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -070096{
97}
Thierry Reding4707a342014-07-28 17:20:33 +020098#endif
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -070099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800101 * This funcion reads the *physical* memory. The f_pos points directly to the
102 * memory location.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800104static ssize_t read_mem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 size_t count, loff_t *ppos)
106{
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400107 phys_addr_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 ssize_t read, sz;
Thierry Reding4707a342014-07-28 17:20:33 +0200109 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Petr Tesarik08d2d002014-01-30 09:48:02 +0100111 if (p != *ppos)
112 return 0;
113
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800114 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return -EFAULT;
116 read = 0;
117#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
118 /* we don't have page 0 mapped on sparc and m68k.. */
119 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800120 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (sz > 0) {
122 if (clear_user(buf, sz))
123 return -EFAULT;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800124 buf += sz;
125 p += sz;
126 count -= sz;
127 read += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129 }
130#endif
131
132 while (count > 0) {
Wu Fengguangfa29e972009-12-14 17:58:08 -0800133 unsigned long remaining;
Kees Cooka4866aa2017-04-05 09:39:08 -0700134 int allowed;
Wu Fengguangfa29e972009-12-14 17:58:08 -0800135
Wu Fengguangf2223182009-12-14 17:58:07 -0800136 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Kees Cooka4866aa2017-04-05 09:39:08 -0700138 allowed = page_is_allowed(p >> PAGE_SHIFT);
139 if (!allowed)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700140 return -EPERM;
Kees Cooka4866aa2017-04-05 09:39:08 -0700141 if (allowed == 2) {
142 /* Show zeros for restricted memory. */
143 remaining = clear_user(buf, sz);
144 } else {
145 /*
146 * On ia64 if a page has been mapped somewhere as
147 * uncached, then it must also be accessed uncached
148 * by the kernel or data corruption may occur.
149 */
150 ptr = xlate_dev_mem_ptr(p);
151 if (!ptr)
152 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700153
Kees Cooka4866aa2017-04-05 09:39:08 -0700154 remaining = copy_to_user(buf, ptr, sz);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700155
Kees Cooka4866aa2017-04-05 09:39:08 -0700156 unxlate_dev_mem_ptr(p, ptr);
157 }
158
Wu Fengguangfa29e972009-12-14 17:58:08 -0800159 if (remaining)
160 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 buf += sz;
163 p += sz;
164 count -= sz;
165 read += sz;
166 }
167
168 *ppos += read;
169 return read;
170}
171
Andrew Mortond7d4d842010-03-10 15:21:52 -0800172static ssize_t write_mem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 size_t count, loff_t *ppos)
174{
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400175 phys_addr_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 ssize_t written, sz;
177 unsigned long copied;
178 void *ptr;
179
Petr Tesarik08d2d002014-01-30 09:48:02 +0100180 if (p != *ppos)
181 return -EFBIG;
182
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800183 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return -EFAULT;
185
186 written = 0;
187
188#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
189 /* we don't have page 0 mapped on sparc and m68k.. */
190 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800191 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /* Hmm. Do something? */
193 buf += sz;
194 p += sz;
195 count -= sz;
196 written += sz;
197 }
198#endif
199
200 while (count > 0) {
Kees Cooka4866aa2017-04-05 09:39:08 -0700201 int allowed;
202
Wu Fengguangf2223182009-12-14 17:58:07 -0800203 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Kees Cooka4866aa2017-04-05 09:39:08 -0700205 allowed = page_is_allowed(p >> PAGE_SHIFT);
206 if (!allowed)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700207 return -EPERM;
208
Kees Cooka4866aa2017-04-05 09:39:08 -0700209 /* Skip actual writing when a page is marked as restricted. */
210 if (allowed == 1) {
211 /*
212 * On ia64 if a page has been mapped somewhere as
213 * uncached, then it must also be accessed uncached
214 * by the kernel or data corruption may occur.
215 */
216 ptr = xlate_dev_mem_ptr(p);
217 if (!ptr) {
218 if (written)
219 break;
220 return -EFAULT;
221 }
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700222
Kees Cooka4866aa2017-04-05 09:39:08 -0700223 copied = copy_from_user(ptr, buf, sz);
224 unxlate_dev_mem_ptr(p, ptr);
225 if (copied) {
226 written += sz - copied;
227 if (written)
228 break;
229 return -EFAULT;
230 }
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700231 }
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 buf += sz;
234 p += sz;
235 count -= sz;
236 written += sz;
237 }
238
239 *ppos += written;
240 return written;
241}
242
Andrew Mortond7d4d842010-03-10 15:21:52 -0800243int __weak phys_mem_access_prot_allowed(struct file *file,
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700244 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
245{
246 return 1;
247}
248
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800249#ifndef __HAVE_PHYS_MEM_ACCESS_PROT
Andrew Mortond7d4d842010-03-10 15:21:52 -0800250
251/*
252 * Architectures vary in how they handle caching for addresses
253 * outside of main memory.
254 *
255 */
David Howellsea56f412010-04-06 14:35:08 -0700256#ifdef pgprot_noncached
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400257static int uncached_access(struct file *file, phys_addr_t addr)
Andrew Mortond7d4d842010-03-10 15:21:52 -0800258{
259#if defined(CONFIG_IA64)
260 /*
261 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
262 * attribute aliases.
263 */
264 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
265#elif defined(CONFIG_MIPS)
266 {
267 extern int __uncached_access(struct file *file,
268 unsigned long addr);
269
270 return __uncached_access(file, addr);
271 }
272#else
273 /*
274 * Accessing memory above the top the kernel knows about or through a
275 * file pointer
276 * that was marked O_DSYNC will be done non-cached.
277 */
278 if (file->f_flags & O_DSYNC)
279 return 1;
280 return addr >= __pa(high_memory);
281#endif
282}
David Howellsea56f412010-04-06 14:35:08 -0700283#endif
Andrew Mortond7d4d842010-03-10 15:21:52 -0800284
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800285static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
286 unsigned long size, pgprot_t vma_prot)
287{
288#ifdef pgprot_noncached
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400289 phys_addr_t offset = pfn << PAGE_SHIFT;
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800290
291 if (uncached_access(file, offset))
292 return pgprot_noncached(vma_prot);
293#endif
294 return vma_prot;
295}
296#endif
297
David Howells5da61852006-09-27 01:50:16 -0700298#ifndef CONFIG_MMU
299static unsigned long get_unmapped_area_mem(struct file *file,
300 unsigned long addr,
301 unsigned long len,
302 unsigned long pgoff,
303 unsigned long flags)
304{
305 if (!valid_mmap_phys_addr_range(pgoff, len))
306 return (unsigned long) -EINVAL;
Benjamin Herrenschmidt8a932582007-04-16 22:53:16 -0700307 return pgoff << PAGE_SHIFT;
David Howells5da61852006-09-27 01:50:16 -0700308}
309
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100310/* permit direct mmap, for read, write or exec */
311static unsigned memory_mmap_capabilities(struct file *file)
312{
313 return NOMMU_MAP_DIRECT |
314 NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC;
315}
316
317static unsigned zero_mmap_capabilities(struct file *file)
318{
319 return NOMMU_MAP_COPY;
320}
321
David Howells5da61852006-09-27 01:50:16 -0700322/* can't do an in-place private mapping if there's no MMU */
323static inline int private_mapping_ok(struct vm_area_struct *vma)
324{
325 return vma->vm_flags & VM_MAYSHARE;
326}
327#else
David Howells5da61852006-09-27 01:50:16 -0700328
329static inline int private_mapping_ok(struct vm_area_struct *vma)
330{
331 return 1;
332}
333#endif
334
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400335static const struct vm_operations_struct mmap_mem_ops = {
Rik van Riel7ae8ed52008-07-23 21:27:07 -0700336#ifdef CONFIG_HAVE_IOREMAP_PROT
337 .access = generic_access_phys
338#endif
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700339};
340
Andrew Mortond7d4d842010-03-10 15:21:52 -0800341static int mmap_mem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800343 size_t size = vma->vm_end - vma->vm_start;
Julius Wernerb299cde2017-05-12 14:42:58 -0700344 phys_addr_t offset = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
345
Craig Bergstrombe62a322017-11-15 15:29:51 -0700346 /* Does it even fit in phys_addr_t? */
347 if (offset >> PAGE_SHIFT != vma->vm_pgoff)
348 return -EINVAL;
349
Julius Wernerb299cde2017-05-12 14:42:58 -0700350 /* It's illegal to wrap around the end of the physical address space. */
Julius Werner32829da2017-06-02 15:36:39 -0700351 if (offset + (phys_addr_t)size - 1 < offset)
Julius Wernerb299cde2017-05-12 14:42:58 -0700352 return -EINVAL;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800353
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700354 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800355 return -EINVAL;
356
David Howells5da61852006-09-27 01:50:16 -0700357 if (!private_mapping_ok(vma))
358 return -ENOSYS;
359
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800360 if (!range_is_allowed(vma->vm_pgoff, size))
361 return -EPERM;
362
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700363 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
364 &vma->vm_page_prot))
365 return -EINVAL;
366
Roland Dreier8b150472005-10-28 17:46:18 -0700367 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800368 size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700371 vma->vm_ops = &mmap_mem_ops;
372
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700373 /* Remap-pfn-range will mark the range VM_IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (remap_pfn_range(vma,
375 vma->vm_start,
376 vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800377 size,
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700378 vma->vm_page_prot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return -EAGAIN;
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return 0;
382}
383
Andrew Mortond7d4d842010-03-10 15:21:52 -0800384static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Linus Torvalds4bb82552005-08-13 14:22:59 -0700386 unsigned long pfn;
387
Linus Torvalds6d3154c2007-01-22 08:53:24 -0800388 /* Turn a kernel-virtual address into a physical page frame */
389 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800392 * RED-PEN: on some architectures there is more mapped memory than
393 * available in mem_map which pfn_valid checks for. Perhaps should add a
394 * new macro here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 *
396 * RED-PEN: vmalloc is not supported right now.
397 */
Linus Torvalds4bb82552005-08-13 14:22:59 -0700398 if (!pfn_valid(pfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return -EIO;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700400
401 vma->vm_pgoff = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return mmap_mem(file, vma);
403}
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405/*
406 * This function reads the *virtual* memory as seen by the kernel.
407 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800408static ssize_t read_kmem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 size_t count, loff_t *ppos)
410{
411 unsigned long p = *ppos;
412 ssize_t low_count, read, sz;
Hans Grob890537b2013-02-06 11:37:20 +0100413 char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800414 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 read = 0;
417 if (p < (unsigned long) high_memory) {
418 low_count = count;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800419 if (count > (unsigned long)high_memory - p)
420 low_count = (unsigned long)high_memory - p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
423 /* we don't have page 0 mapped on sparc and m68k.. */
424 if (p < PAGE_SIZE && low_count > 0) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800425 sz = size_inside_page(p, low_count);
426 if (clear_user(buf, sz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return -EFAULT;
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800428 buf += sz;
429 p += sz;
430 read += sz;
431 low_count -= sz;
432 count -= sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434#endif
435 while (low_count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800436 sz = size_inside_page(p, low_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 /*
439 * On ia64 if a page has been mapped somewhere as
440 * uncached, then it must also be accessed uncached
441 * by the kernel or data corruption may occur
442 */
Thierry Reding4707a342014-07-28 17:20:33 +0200443 kbuf = xlate_dev_kmem_ptr((void *)p);
Robin Murphy488debb92017-01-05 17:15:01 +0000444 if (!virt_addr_valid(kbuf))
445 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 if (copy_to_user(buf, kbuf, sz))
448 return -EFAULT;
449 buf += sz;
450 p += sz;
451 read += sz;
452 low_count -= sz;
453 count -= sz;
454 }
455 }
456
457 if (count > 0) {
458 kbuf = (char *)__get_free_page(GFP_KERNEL);
459 if (!kbuf)
460 return -ENOMEM;
461 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800462 sz = size_inside_page(p, count);
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800463 if (!is_vmalloc_or_module_addr((void *)p)) {
464 err = -ENXIO;
465 break;
466 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800467 sz = vread(kbuf, (char *)p, sz);
468 if (!sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 break;
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800470 if (copy_to_user(buf, kbuf, sz)) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800471 err = -EFAULT;
472 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800474 count -= sz;
475 buf += sz;
476 read += sz;
477 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479 free_page((unsigned long)kbuf);
480 }
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800481 *ppos = p;
482 return read ? read : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
485
Andrew Mortond7d4d842010-03-10 15:21:52 -0800486static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
487 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 ssize_t written, sz;
490 unsigned long copied;
491
492 written = 0;
493#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
494 /* we don't have page 0 mapped on sparc and m68k.. */
Wu Fengguangee323982009-12-14 17:58:10 -0800495 if (p < PAGE_SIZE) {
496 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 /* Hmm. Do something? */
498 buf += sz;
499 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 count -= sz;
501 written += sz;
502 }
503#endif
504
505 while (count > 0) {
Thierry Reding4707a342014-07-28 17:20:33 +0200506 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Wu Fengguangee323982009-12-14 17:58:10 -0800508 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800511 * On ia64 if a page has been mapped somewhere as uncached, then
512 * it must also be accessed uncached by the kernel or data
513 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 */
Thierry Reding4707a342014-07-28 17:20:33 +0200515 ptr = xlate_dev_kmem_ptr((void *)p);
Robin Murphy488debb92017-01-05 17:15:01 +0000516 if (!virt_addr_valid(ptr))
517 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 copied = copy_from_user(ptr, buf, sz);
520 if (copied) {
Jan Beulichc654d602006-03-25 03:07:31 -0800521 written += sz - copied;
522 if (written)
523 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return -EFAULT;
525 }
526 buf += sz;
527 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 count -= sz;
529 written += sz;
530 }
531
532 *ppos += written;
533 return written;
534}
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536/*
537 * This function writes to the *virtual* memory as seen by the kernel.
538 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800539static ssize_t write_kmem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 size_t count, loff_t *ppos)
541{
542 unsigned long p = *ppos;
543 ssize_t wrote = 0;
544 ssize_t virtr = 0;
Hans Grob890537b2013-02-06 11:37:20 +0100545 char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800546 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 if (p < (unsigned long) high_memory) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800549 unsigned long to_write = min_t(unsigned long, count,
550 (unsigned long)high_memory - p);
Wu Fengguangee323982009-12-14 17:58:10 -0800551 wrote = do_write_kmem(p, buf, to_write, ppos);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800552 if (wrote != to_write)
553 return wrote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 p += wrote;
555 buf += wrote;
556 count -= wrote;
557 }
558
559 if (count > 0) {
560 kbuf = (char *)__get_free_page(GFP_KERNEL);
561 if (!kbuf)
562 return wrote ? wrote : -ENOMEM;
563 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800564 unsigned long sz = size_inside_page(p, count);
565 unsigned long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800567 if (!is_vmalloc_or_module_addr((void *)p)) {
568 err = -ENXIO;
569 break;
570 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800571 n = copy_from_user(kbuf, buf, sz);
572 if (n) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800573 err = -EFAULT;
574 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Wu Fengguangc85e9a92010-02-02 13:44:06 -0800576 vwrite(kbuf, (char *)p, sz);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800577 count -= sz;
578 buf += sz;
579 virtr += sz;
580 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582 free_page((unsigned long)kbuf);
583 }
584
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800585 *ppos = p;
586 return virtr + wrote ? : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Andrew Mortond7d4d842010-03-10 15:21:52 -0800589static ssize_t read_port(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 size_t count, loff_t *ppos)
591{
592 unsigned long i = *ppos;
593 char __user *tmp = buf;
594
595 if (!access_ok(VERIFY_WRITE, buf, count))
Andrew Mortond7d4d842010-03-10 15:21:52 -0800596 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 while (count-- > 0 && i < 65536) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800598 if (__put_user(inb(i), tmp) < 0)
599 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 i++;
601 tmp++;
602 }
603 *ppos = i;
604 return tmp-buf;
605}
606
Andrew Mortond7d4d842010-03-10 15:21:52 -0800607static ssize_t write_port(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 size_t count, loff_t *ppos)
609{
610 unsigned long i = *ppos;
Hans Grob890537b2013-02-06 11:37:20 +0100611 const char __user *tmp = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Andrew Mortond7d4d842010-03-10 15:21:52 -0800613 if (!access_ok(VERIFY_READ, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return -EFAULT;
615 while (count-- > 0 && i < 65536) {
616 char c;
Rob Ward6a0061b2014-12-20 18:28:36 +0000617
Jan Beulichc654d602006-03-25 03:07:31 -0800618 if (__get_user(c, tmp)) {
619 if (tmp > buf)
620 break;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800621 return -EFAULT;
Jan Beulichc654d602006-03-25 03:07:31 -0800622 }
Andrew Mortond7d4d842010-03-10 15:21:52 -0800623 outb(c, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 i++;
625 tmp++;
626 }
627 *ppos = i;
628 return tmp-buf;
629}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Andrew Mortond7d4d842010-03-10 15:21:52 -0800631static ssize_t read_null(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 size_t count, loff_t *ppos)
633{
634 return 0;
635}
636
Andrew Mortond7d4d842010-03-10 15:21:52 -0800637static ssize_t write_null(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 size_t count, loff_t *ppos)
639{
640 return count;
641}
642
Al Virocd28e282015-04-03 15:57:04 -0400643static ssize_t read_iter_null(struct kiocb *iocb, struct iov_iter *to)
Zach Brown162934d2013-05-07 16:18:27 -0700644{
645 return 0;
646}
647
Al Virocd28e282015-04-03 15:57:04 -0400648static ssize_t write_iter_null(struct kiocb *iocb, struct iov_iter *from)
Zach Brown162934d2013-05-07 16:18:27 -0700649{
Al Virocd28e282015-04-03 15:57:04 -0400650 size_t count = iov_iter_count(from);
651 iov_iter_advance(from, count);
652 return count;
Zach Brown162934d2013-05-07 16:18:27 -0700653}
654
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200655static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
656 struct splice_desc *sd)
657{
658 return sd->len;
659}
660
Andrew Mortond7d4d842010-03-10 15:21:52 -0800661static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200662 loff_t *ppos, size_t len, unsigned int flags)
663{
664 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
665}
666
Al Viro13ba33e2014-08-18 10:04:12 -0400667static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
Zach Brown162934d2013-05-07 16:18:27 -0700668{
669 size_t written = 0;
Zach Brown162934d2013-05-07 16:18:27 -0700670
Al Viro13ba33e2014-08-18 10:04:12 -0400671 while (iov_iter_count(iter)) {
672 size_t chunk = iov_iter_count(iter), n;
Rob Ward6a0061b2014-12-20 18:28:36 +0000673
Al Viro13ba33e2014-08-18 10:04:12 -0400674 if (chunk > PAGE_SIZE)
675 chunk = PAGE_SIZE; /* Just for latency reasons */
676 n = iov_iter_zero(chunk, iter);
677 if (!n && iov_iter_count(iter))
678 return written ? written : -EFAULT;
679 written += n;
680 if (signal_pending(current))
681 return written ? written : -ERESTARTSYS;
682 cond_resched();
Zach Brown162934d2013-05-07 16:18:27 -0700683 }
Al Viro13ba33e2014-08-18 10:04:12 -0400684 return written;
Zach Brown162934d2013-05-07 16:18:27 -0700685}
686
Andrew Mortond7d4d842010-03-10 15:21:52 -0800687static int mmap_zero(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700689#ifndef CONFIG_MMU
690 return -ENOSYS;
691#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (vma->vm_flags & VM_SHARED)
693 return shmem_zero_setup(vma);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700694 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Hugh Dickinsc01d5b32016-07-26 15:26:15 -0700697static unsigned long get_unmapped_area_zero(struct file *file,
698 unsigned long addr, unsigned long len,
699 unsigned long pgoff, unsigned long flags)
700{
701#ifdef CONFIG_MMU
702 if (flags & MAP_SHARED) {
703 /*
704 * mmap_zero() will call shmem_zero_setup() to create a file,
705 * so use shmem's get_unmapped_area in case it can be huge;
706 * and pass NULL for file as in mmap.c's get_unmapped_area(),
707 * so as not to confuse shmem with our handle on "/dev/zero".
708 */
709 return shmem_get_unmapped_area(NULL, addr, len, pgoff, flags);
710 }
711
712 /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
713 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
714#else
715 return -ENOSYS;
716#endif
717}
718
Andrew Mortond7d4d842010-03-10 15:21:52 -0800719static ssize_t write_full(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 size_t count, loff_t *ppos)
721{
722 return -ENOSPC;
723}
724
725/*
726 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
727 * can fopen() both devices with "a" now. This was previously impossible.
728 * -- SRB.
729 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800730static loff_t null_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 return file->f_pos = 0;
733}
734
735/*
736 * The memory devices use the full 32/64 bits of the offset, and so we cannot
737 * check against negative addresses: they are ok. The return value is weird,
738 * though, in that case (0).
739 *
740 * also note that seeking relative to the "end of file" isn't supported:
741 * it has no meaning, so it returns -EINVAL.
742 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800743static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 loff_t ret;
746
Al Viro59551022016-01-22 15:40:57 -0500747 inode_lock(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 switch (orig) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800749 case SEEK_CUR:
750 offset += file->f_pos;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800751 case SEEK_SET:
752 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
Andrzej Hajdaecb63a12016-02-15 15:35:21 +0100753 if ((unsigned long long)offset >= -MAX_ERRNO) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800754 ret = -EOVERFLOW;
755 break;
756 }
757 file->f_pos = offset;
758 ret = file->f_pos;
759 force_successful_syscall_return();
760 break;
761 default:
762 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
Al Viro59551022016-01-22 15:40:57 -0500764 inode_unlock(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return ret;
766}
767
Hans Grob890537b2013-02-06 11:37:20 +0100768static int open_port(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
771}
772
773#define zero_lseek null_lseek
774#define full_lseek null_lseek
775#define write_zero write_null
Al Virocd28e282015-04-03 15:57:04 -0400776#define write_iter_zero write_iter_null
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777#define open_mem open_port
778#define open_kmem open_mem
779
Rob Ward73f07182014-12-07 15:40:33 +0000780static const struct file_operations __maybe_unused mem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 .llseek = memory_lseek,
782 .read = read_mem,
783 .write = write_mem,
784 .mmap = mmap_mem,
785 .open = open_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100786#ifndef CONFIG_MMU
David Howells5da61852006-09-27 01:50:16 -0700787 .get_unmapped_area = get_unmapped_area_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100788 .mmap_capabilities = memory_mmap_capabilities,
789#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790};
791
Rob Warda8c91252014-12-07 15:40:34 +0000792static const struct file_operations __maybe_unused kmem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 .llseek = memory_lseek,
794 .read = read_kmem,
795 .write = write_kmem,
796 .mmap = mmap_kmem,
797 .open = open_kmem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100798#ifndef CONFIG_MMU
David Howells5da61852006-09-27 01:50:16 -0700799 .get_unmapped_area = get_unmapped_area_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100800 .mmap_capabilities = memory_mmap_capabilities,
801#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802};
803
Arjan van de Ven62322d22006-07-03 00:24:21 -0700804static const struct file_operations null_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 .llseek = null_lseek,
806 .read = read_null,
807 .write = write_null,
Al Virocd28e282015-04-03 15:57:04 -0400808 .read_iter = read_iter_null,
809 .write_iter = write_iter_null,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200810 .splice_write = splice_write_null,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811};
812
Rob Ward3a4bc2f2014-12-07 15:40:35 +0000813static const struct file_operations __maybe_unused port_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 .llseek = memory_lseek,
815 .read = read_port,
816 .write = write_port,
817 .open = open_port,
818};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Arjan van de Ven62322d22006-07-03 00:24:21 -0700820static const struct file_operations zero_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 .llseek = zero_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 .write = write_zero,
Al Viro13ba33e2014-08-18 10:04:12 -0400823 .read_iter = read_iter_zero,
Al Virocd28e282015-04-03 15:57:04 -0400824 .write_iter = write_iter_zero,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 .mmap = mmap_zero,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -0700826 .get_unmapped_area = get_unmapped_area_zero,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100827#ifndef CONFIG_MMU
828 .mmap_capabilities = zero_mmap_capabilities,
829#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830};
831
Arjan van de Ven62322d22006-07-03 00:24:21 -0700832static const struct file_operations full_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 .llseek = full_lseek,
Al Viro13ba33e2014-08-18 10:04:12 -0400834 .read_iter = read_iter_zero,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 .write = write_full,
836};
837
Kay Sievers389e0cb2009-07-04 16:51:29 +0200838static const struct memdev {
839 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400840 umode_t mode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200841 const struct file_operations *fops;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100842 fmode_t fmode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200843} devlist[] = {
Rob Ward73f07182014-12-07 15:40:33 +0000844#ifdef CONFIG_DEVMEM
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100845 [1] = { "mem", 0, &mem_fops, FMODE_UNSIGNED_OFFSET },
Rob Ward73f07182014-12-07 15:40:33 +0000846#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700847#ifdef CONFIG_DEVKMEM
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100848 [2] = { "kmem", 0, &kmem_fops, FMODE_UNSIGNED_OFFSET },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700849#endif
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100850 [3] = { "null", 0666, &null_fops, 0 },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700851#ifdef CONFIG_DEVPORT
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100852 [4] = { "port", 0, &port_fops, 0 },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700853#endif
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100854 [5] = { "zero", 0666, &zero_fops, 0 },
855 [7] = { "full", 0666, &full_fops, 0 },
856 [8] = { "random", 0666, &random_fops, 0 },
857 [9] = { "urandom", 0666, &urandom_fops, 0 },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200858#ifdef CONFIG_PRINTK
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100859 [11] = { "kmsg", 0644, &kmsg_fops, 0 },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200860#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700861};
862
863static int memory_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200865 int minor;
866 const struct memdev *dev;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700867
Kay Sievers389e0cb2009-07-04 16:51:29 +0200868 minor = iminor(inode);
869 if (minor >= ARRAY_SIZE(devlist))
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200870 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700871
Kay Sievers389e0cb2009-07-04 16:51:29 +0200872 dev = &devlist[minor];
873 if (!dev->fops)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200874 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700875
Kay Sievers389e0cb2009-07-04 16:51:29 +0200876 filp->f_op = dev->fops;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100877 filp->f_mode |= dev->fmode;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700878
Kay Sievers389e0cb2009-07-04 16:51:29 +0200879 if (dev->fops->open)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200880 return dev->fops->open(inode, filp);
881
882 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
Arjan van de Ven62322d22006-07-03 00:24:21 -0700885static const struct file_operations memory_fops = {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800886 .open = memory_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200887 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888};
889
Al Viro2c9ede52011-07-23 20:24:48 -0400890static char *mem_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +0200891{
892 if (mode && devlist[MINOR(dev->devt)].mode)
893 *mode = devlist[MINOR(dev->devt)].mode;
894 return NULL;
895}
896
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800897static struct class *mem_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899static int __init chr_dev_init(void)
900{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200901 int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Andrew Mortond7d4d842010-03-10 15:21:52 -0800903 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
905
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800906 mem_class = class_create(THIS_MODULE, "mem");
Anton Blanchard6e191f72010-04-06 14:34:55 -0700907 if (IS_ERR(mem_class))
908 return PTR_ERR(mem_class);
909
Kay Sieverse454cea2009-09-18 23:01:12 +0200910 mem_class->devnode = mem_devnode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200911 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
912 if (!devlist[minor].name)
913 continue;
Haren Mynenie1612de2012-07-11 15:18:44 +1000914
915 /*
Hans Grob890537b2013-02-06 11:37:20 +0100916 * Create /dev/port?
Haren Mynenie1612de2012-07-11 15:18:44 +1000917 */
918 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
919 continue;
920
Kay Sievers389e0cb2009-07-04 16:51:29 +0200921 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
922 NULL, devlist[minor].name);
923 }
Greg Kroah-Hartmanebf644c2006-07-25 17:13:31 -0700924
David Howells31d1d482010-08-06 16:34:43 +0100925 return tty_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
928fs_initcall(chr_dev_init);