blob: 970e1242a282a097405cf30d0c87e7518bca67f4 [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 Dobriyanf0f37e2f2009-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
346 /* It's illegal to wrap around the end of the physical address space. */
Julius Werner32829da2017-06-02 15:36:39 -0700347 if (offset + (phys_addr_t)size - 1 < offset)
Julius Wernerb299cde2017-05-12 14:42:58 -0700348 return -EINVAL;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800349
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700350 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800351 return -EINVAL;
352
David Howells5da61852006-09-27 01:50:16 -0700353 if (!private_mapping_ok(vma))
354 return -ENOSYS;
355
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800356 if (!range_is_allowed(vma->vm_pgoff, size))
357 return -EPERM;
358
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700359 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
360 &vma->vm_page_prot))
361 return -EINVAL;
362
Roland Dreier8b150472005-10-28 17:46:18 -0700363 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800364 size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700367 vma->vm_ops = &mmap_mem_ops;
368
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700369 /* Remap-pfn-range will mark the range VM_IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (remap_pfn_range(vma,
371 vma->vm_start,
372 vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800373 size,
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700374 vma->vm_page_prot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return -EAGAIN;
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return 0;
378}
379
Andrew Mortond7d4d842010-03-10 15:21:52 -0800380static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Linus Torvalds4bb82552005-08-13 14:22:59 -0700382 unsigned long pfn;
383
Linus Torvalds6d3154c2007-01-22 08:53:24 -0800384 /* Turn a kernel-virtual address into a physical page frame */
385 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800388 * RED-PEN: on some architectures there is more mapped memory than
389 * available in mem_map which pfn_valid checks for. Perhaps should add a
390 * new macro here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 *
392 * RED-PEN: vmalloc is not supported right now.
393 */
Linus Torvalds4bb82552005-08-13 14:22:59 -0700394 if (!pfn_valid(pfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return -EIO;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700396
397 vma->vm_pgoff = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return mmap_mem(file, vma);
399}
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401/*
402 * This function reads the *virtual* memory as seen by the kernel.
403 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800404static ssize_t read_kmem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 size_t count, loff_t *ppos)
406{
407 unsigned long p = *ppos;
408 ssize_t low_count, read, sz;
Hans Grob890537b2013-02-06 11:37:20 +0100409 char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800410 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 read = 0;
413 if (p < (unsigned long) high_memory) {
414 low_count = count;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800415 if (count > (unsigned long)high_memory - p)
416 low_count = (unsigned long)high_memory - p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
419 /* we don't have page 0 mapped on sparc and m68k.. */
420 if (p < PAGE_SIZE && low_count > 0) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800421 sz = size_inside_page(p, low_count);
422 if (clear_user(buf, sz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return -EFAULT;
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800424 buf += sz;
425 p += sz;
426 read += sz;
427 low_count -= sz;
428 count -= sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430#endif
431 while (low_count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800432 sz = size_inside_page(p, low_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 /*
435 * On ia64 if a page has been mapped somewhere as
436 * uncached, then it must also be accessed uncached
437 * by the kernel or data corruption may occur
438 */
Thierry Reding4707a342014-07-28 17:20:33 +0200439 kbuf = xlate_dev_kmem_ptr((void *)p);
Robin Murphy488debb92017-01-05 17:15:01 +0000440 if (!virt_addr_valid(kbuf))
441 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 if (copy_to_user(buf, kbuf, sz))
444 return -EFAULT;
445 buf += sz;
446 p += sz;
447 read += sz;
448 low_count -= sz;
449 count -= sz;
450 }
451 }
452
453 if (count > 0) {
454 kbuf = (char *)__get_free_page(GFP_KERNEL);
455 if (!kbuf)
456 return -ENOMEM;
457 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800458 sz = size_inside_page(p, count);
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800459 if (!is_vmalloc_or_module_addr((void *)p)) {
460 err = -ENXIO;
461 break;
462 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800463 sz = vread(kbuf, (char *)p, sz);
464 if (!sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 break;
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800466 if (copy_to_user(buf, kbuf, sz)) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800467 err = -EFAULT;
468 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800470 count -= sz;
471 buf += sz;
472 read += sz;
473 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475 free_page((unsigned long)kbuf);
476 }
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800477 *ppos = p;
478 return read ? read : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
481
Andrew Mortond7d4d842010-03-10 15:21:52 -0800482static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
483 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 ssize_t written, sz;
486 unsigned long copied;
487
488 written = 0;
489#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
490 /* we don't have page 0 mapped on sparc and m68k.. */
Wu Fengguangee323982009-12-14 17:58:10 -0800491 if (p < PAGE_SIZE) {
492 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /* Hmm. Do something? */
494 buf += sz;
495 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 count -= sz;
497 written += sz;
498 }
499#endif
500
501 while (count > 0) {
Thierry Reding4707a342014-07-28 17:20:33 +0200502 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Wu Fengguangee323982009-12-14 17:58:10 -0800504 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800507 * On ia64 if a page has been mapped somewhere as uncached, then
508 * it must also be accessed uncached by the kernel or data
509 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 */
Thierry Reding4707a342014-07-28 17:20:33 +0200511 ptr = xlate_dev_kmem_ptr((void *)p);
Robin Murphy488debb92017-01-05 17:15:01 +0000512 if (!virt_addr_valid(ptr))
513 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 copied = copy_from_user(ptr, buf, sz);
516 if (copied) {
Jan Beulichc654d602006-03-25 03:07:31 -0800517 written += sz - copied;
518 if (written)
519 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return -EFAULT;
521 }
522 buf += sz;
523 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 count -= sz;
525 written += sz;
526 }
527
528 *ppos += written;
529 return written;
530}
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532/*
533 * This function writes to the *virtual* memory as seen by the kernel.
534 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800535static ssize_t write_kmem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 size_t count, loff_t *ppos)
537{
538 unsigned long p = *ppos;
539 ssize_t wrote = 0;
540 ssize_t virtr = 0;
Hans Grob890537b2013-02-06 11:37:20 +0100541 char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800542 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 if (p < (unsigned long) high_memory) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800545 unsigned long to_write = min_t(unsigned long, count,
546 (unsigned long)high_memory - p);
Wu Fengguangee323982009-12-14 17:58:10 -0800547 wrote = do_write_kmem(p, buf, to_write, ppos);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800548 if (wrote != to_write)
549 return wrote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 p += wrote;
551 buf += wrote;
552 count -= wrote;
553 }
554
555 if (count > 0) {
556 kbuf = (char *)__get_free_page(GFP_KERNEL);
557 if (!kbuf)
558 return wrote ? wrote : -ENOMEM;
559 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800560 unsigned long sz = size_inside_page(p, count);
561 unsigned long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800563 if (!is_vmalloc_or_module_addr((void *)p)) {
564 err = -ENXIO;
565 break;
566 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800567 n = copy_from_user(kbuf, buf, sz);
568 if (n) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800569 err = -EFAULT;
570 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
Wu Fengguangc85e9a92010-02-02 13:44:06 -0800572 vwrite(kbuf, (char *)p, sz);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800573 count -= sz;
574 buf += sz;
575 virtr += sz;
576 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578 free_page((unsigned long)kbuf);
579 }
580
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800581 *ppos = p;
582 return virtr + wrote ? : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Andrew Mortond7d4d842010-03-10 15:21:52 -0800585static ssize_t read_port(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 size_t count, loff_t *ppos)
587{
588 unsigned long i = *ppos;
589 char __user *tmp = buf;
590
591 if (!access_ok(VERIFY_WRITE, buf, count))
Andrew Mortond7d4d842010-03-10 15:21:52 -0800592 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 while (count-- > 0 && i < 65536) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800594 if (__put_user(inb(i), tmp) < 0)
595 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 i++;
597 tmp++;
598 }
599 *ppos = i;
600 return tmp-buf;
601}
602
Andrew Mortond7d4d842010-03-10 15:21:52 -0800603static ssize_t write_port(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 size_t count, loff_t *ppos)
605{
606 unsigned long i = *ppos;
Hans Grob890537b2013-02-06 11:37:20 +0100607 const char __user *tmp = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Andrew Mortond7d4d842010-03-10 15:21:52 -0800609 if (!access_ok(VERIFY_READ, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return -EFAULT;
611 while (count-- > 0 && i < 65536) {
612 char c;
Rob Ward6a0061b2014-12-20 18:28:36 +0000613
Jan Beulichc654d602006-03-25 03:07:31 -0800614 if (__get_user(c, tmp)) {
615 if (tmp > buf)
616 break;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800617 return -EFAULT;
Jan Beulichc654d602006-03-25 03:07:31 -0800618 }
Andrew Mortond7d4d842010-03-10 15:21:52 -0800619 outb(c, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 i++;
621 tmp++;
622 }
623 *ppos = i;
624 return tmp-buf;
625}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Andrew Mortond7d4d842010-03-10 15:21:52 -0800627static ssize_t read_null(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 size_t count, loff_t *ppos)
629{
630 return 0;
631}
632
Andrew Mortond7d4d842010-03-10 15:21:52 -0800633static ssize_t write_null(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 size_t count, loff_t *ppos)
635{
636 return count;
637}
638
Al Virocd28e282015-04-03 15:57:04 -0400639static ssize_t read_iter_null(struct kiocb *iocb, struct iov_iter *to)
Zach Brown162934d2013-05-07 16:18:27 -0700640{
641 return 0;
642}
643
Al Virocd28e282015-04-03 15:57:04 -0400644static ssize_t write_iter_null(struct kiocb *iocb, struct iov_iter *from)
Zach Brown162934d2013-05-07 16:18:27 -0700645{
Al Virocd28e282015-04-03 15:57:04 -0400646 size_t count = iov_iter_count(from);
647 iov_iter_advance(from, count);
648 return count;
Zach Brown162934d2013-05-07 16:18:27 -0700649}
650
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200651static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
652 struct splice_desc *sd)
653{
654 return sd->len;
655}
656
Andrew Mortond7d4d842010-03-10 15:21:52 -0800657static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200658 loff_t *ppos, size_t len, unsigned int flags)
659{
660 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
661}
662
Al Viro13ba33e2014-08-18 10:04:12 -0400663static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
Zach Brown162934d2013-05-07 16:18:27 -0700664{
665 size_t written = 0;
Zach Brown162934d2013-05-07 16:18:27 -0700666
Al Viro13ba33e2014-08-18 10:04:12 -0400667 while (iov_iter_count(iter)) {
668 size_t chunk = iov_iter_count(iter), n;
Rob Ward6a0061b2014-12-20 18:28:36 +0000669
Al Viro13ba33e2014-08-18 10:04:12 -0400670 if (chunk > PAGE_SIZE)
671 chunk = PAGE_SIZE; /* Just for latency reasons */
672 n = iov_iter_zero(chunk, iter);
673 if (!n && iov_iter_count(iter))
674 return written ? written : -EFAULT;
675 written += n;
676 if (signal_pending(current))
677 return written ? written : -ERESTARTSYS;
678 cond_resched();
Zach Brown162934d2013-05-07 16:18:27 -0700679 }
Al Viro13ba33e2014-08-18 10:04:12 -0400680 return written;
Zach Brown162934d2013-05-07 16:18:27 -0700681}
682
Andrew Mortond7d4d842010-03-10 15:21:52 -0800683static int mmap_zero(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700685#ifndef CONFIG_MMU
686 return -ENOSYS;
687#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (vma->vm_flags & VM_SHARED)
689 return shmem_zero_setup(vma);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700690 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Hugh Dickinsc01d5b32016-07-26 15:26:15 -0700693static unsigned long get_unmapped_area_zero(struct file *file,
694 unsigned long addr, unsigned long len,
695 unsigned long pgoff, unsigned long flags)
696{
697#ifdef CONFIG_MMU
698 if (flags & MAP_SHARED) {
699 /*
700 * mmap_zero() will call shmem_zero_setup() to create a file,
701 * so use shmem's get_unmapped_area in case it can be huge;
702 * and pass NULL for file as in mmap.c's get_unmapped_area(),
703 * so as not to confuse shmem with our handle on "/dev/zero".
704 */
705 return shmem_get_unmapped_area(NULL, addr, len, pgoff, flags);
706 }
707
708 /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
709 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
710#else
711 return -ENOSYS;
712#endif
713}
714
Andrew Mortond7d4d842010-03-10 15:21:52 -0800715static ssize_t write_full(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 size_t count, loff_t *ppos)
717{
718 return -ENOSPC;
719}
720
721/*
722 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
723 * can fopen() both devices with "a" now. This was previously impossible.
724 * -- SRB.
725 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800726static loff_t null_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 return file->f_pos = 0;
729}
730
731/*
732 * The memory devices use the full 32/64 bits of the offset, and so we cannot
733 * check against negative addresses: they are ok. The return value is weird,
734 * though, in that case (0).
735 *
736 * also note that seeking relative to the "end of file" isn't supported:
737 * it has no meaning, so it returns -EINVAL.
738 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800739static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 loff_t ret;
742
Al Viro59551022016-01-22 15:40:57 -0500743 inode_lock(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 switch (orig) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800745 case SEEK_CUR:
746 offset += file->f_pos;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800747 case SEEK_SET:
748 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
Andrzej Hajdaecb63a12016-02-15 15:35:21 +0100749 if ((unsigned long long)offset >= -MAX_ERRNO) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800750 ret = -EOVERFLOW;
751 break;
752 }
753 file->f_pos = offset;
754 ret = file->f_pos;
755 force_successful_syscall_return();
756 break;
757 default:
758 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Al Viro59551022016-01-22 15:40:57 -0500760 inode_unlock(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return ret;
762}
763
Hans Grob890537b2013-02-06 11:37:20 +0100764static int open_port(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
767}
768
769#define zero_lseek null_lseek
770#define full_lseek null_lseek
771#define write_zero write_null
Al Virocd28e282015-04-03 15:57:04 -0400772#define write_iter_zero write_iter_null
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773#define open_mem open_port
774#define open_kmem open_mem
775
Rob Ward73f07182014-12-07 15:40:33 +0000776static const struct file_operations __maybe_unused mem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 .llseek = memory_lseek,
778 .read = read_mem,
779 .write = write_mem,
780 .mmap = mmap_mem,
781 .open = open_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100782#ifndef CONFIG_MMU
David Howells5da61852006-09-27 01:50:16 -0700783 .get_unmapped_area = get_unmapped_area_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100784 .mmap_capabilities = memory_mmap_capabilities,
785#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786};
787
Rob Warda8c91252014-12-07 15:40:34 +0000788static const struct file_operations __maybe_unused kmem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 .llseek = memory_lseek,
790 .read = read_kmem,
791 .write = write_kmem,
792 .mmap = mmap_kmem,
793 .open = open_kmem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100794#ifndef CONFIG_MMU
David Howells5da61852006-09-27 01:50:16 -0700795 .get_unmapped_area = get_unmapped_area_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100796 .mmap_capabilities = memory_mmap_capabilities,
797#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798};
799
Arjan van de Ven62322d22006-07-03 00:24:21 -0700800static const struct file_operations null_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 .llseek = null_lseek,
802 .read = read_null,
803 .write = write_null,
Al Virocd28e282015-04-03 15:57:04 -0400804 .read_iter = read_iter_null,
805 .write_iter = write_iter_null,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200806 .splice_write = splice_write_null,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807};
808
Rob Ward3a4bc2f2014-12-07 15:40:35 +0000809static const struct file_operations __maybe_unused port_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 .llseek = memory_lseek,
811 .read = read_port,
812 .write = write_port,
813 .open = open_port,
814};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Arjan van de Ven62322d22006-07-03 00:24:21 -0700816static const struct file_operations zero_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 .llseek = zero_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 .write = write_zero,
Al Viro13ba33e2014-08-18 10:04:12 -0400819 .read_iter = read_iter_zero,
Al Virocd28e282015-04-03 15:57:04 -0400820 .write_iter = write_iter_zero,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 .mmap = mmap_zero,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -0700822 .get_unmapped_area = get_unmapped_area_zero,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100823#ifndef CONFIG_MMU
824 .mmap_capabilities = zero_mmap_capabilities,
825#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826};
827
Arjan van de Ven62322d22006-07-03 00:24:21 -0700828static const struct file_operations full_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 .llseek = full_lseek,
Al Viro13ba33e2014-08-18 10:04:12 -0400830 .read_iter = read_iter_zero,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 .write = write_full,
832};
833
Kay Sievers389e0cb2009-07-04 16:51:29 +0200834static const struct memdev {
835 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400836 umode_t mode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200837 const struct file_operations *fops;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100838 fmode_t fmode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200839} devlist[] = {
Rob Ward73f07182014-12-07 15:40:33 +0000840#ifdef CONFIG_DEVMEM
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100841 [1] = { "mem", 0, &mem_fops, FMODE_UNSIGNED_OFFSET },
Rob Ward73f07182014-12-07 15:40:33 +0000842#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700843#ifdef CONFIG_DEVKMEM
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100844 [2] = { "kmem", 0, &kmem_fops, FMODE_UNSIGNED_OFFSET },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700845#endif
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100846 [3] = { "null", 0666, &null_fops, 0 },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700847#ifdef CONFIG_DEVPORT
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100848 [4] = { "port", 0, &port_fops, 0 },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700849#endif
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100850 [5] = { "zero", 0666, &zero_fops, 0 },
851 [7] = { "full", 0666, &full_fops, 0 },
852 [8] = { "random", 0666, &random_fops, 0 },
853 [9] = { "urandom", 0666, &urandom_fops, 0 },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200854#ifdef CONFIG_PRINTK
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100855 [11] = { "kmsg", 0644, &kmsg_fops, 0 },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200856#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700857};
858
859static int memory_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200861 int minor;
862 const struct memdev *dev;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700863
Kay Sievers389e0cb2009-07-04 16:51:29 +0200864 minor = iminor(inode);
865 if (minor >= ARRAY_SIZE(devlist))
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200866 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700867
Kay Sievers389e0cb2009-07-04 16:51:29 +0200868 dev = &devlist[minor];
869 if (!dev->fops)
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 filp->f_op = dev->fops;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100873 filp->f_mode |= dev->fmode;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700874
Kay Sievers389e0cb2009-07-04 16:51:29 +0200875 if (dev->fops->open)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200876 return dev->fops->open(inode, filp);
877
878 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
Arjan van de Ven62322d22006-07-03 00:24:21 -0700881static const struct file_operations memory_fops = {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800882 .open = memory_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200883 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884};
885
Al Viro2c9ede52011-07-23 20:24:48 -0400886static char *mem_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +0200887{
888 if (mode && devlist[MINOR(dev->devt)].mode)
889 *mode = devlist[MINOR(dev->devt)].mode;
890 return NULL;
891}
892
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800893static struct class *mem_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895static int __init chr_dev_init(void)
896{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200897 int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Andrew Mortond7d4d842010-03-10 15:21:52 -0800899 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
901
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800902 mem_class = class_create(THIS_MODULE, "mem");
Anton Blanchard6e191f72010-04-06 14:34:55 -0700903 if (IS_ERR(mem_class))
904 return PTR_ERR(mem_class);
905
Kay Sieverse454cea2009-09-18 23:01:12 +0200906 mem_class->devnode = mem_devnode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200907 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
908 if (!devlist[minor].name)
909 continue;
Haren Mynenie1612de2012-07-11 15:18:44 +1000910
911 /*
Hans Grob890537b2013-02-06 11:37:20 +0100912 * Create /dev/port?
Haren Mynenie1612de2012-07-11 15:18:44 +1000913 */
914 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
915 continue;
916
Kay Sievers389e0cb2009-07-04 16:51:29 +0200917 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
918 NULL, devlist[minor].name);
919 }
Greg Kroah-Hartmanebf644c2006-07-25 17:13:31 -0700920
David Howells31d1d482010-08-06 16:34:43 +0100921 return tty_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
924fs_initcall(chr_dev_init);