blob: e87a40c198fa6a1f3de1ab9642459b5d9f8b4cb4 [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 Gaocfaf346c2011-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 Cook2c0ad232017-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 Cook2c0ad232017-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
Tetsuo Handa8c521632019-08-26 22:13:25 +090099static inline bool should_stop_iteration(void)
100{
101 if (need_resched())
102 cond_resched();
103 return fatal_signal_pending(current);
104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800107 * This funcion reads the *physical* memory. The f_pos points directly to the
108 * memory location.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800110static ssize_t read_mem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 size_t count, loff_t *ppos)
112{
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400113 phys_addr_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 ssize_t read, sz;
Thierry Reding4707a342014-07-28 17:20:33 +0200115 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Petr Tesarik08d2d002014-01-30 09:48:02 +0100117 if (p != *ppos)
118 return 0;
119
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800120 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return -EFAULT;
122 read = 0;
123#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
124 /* we don't have page 0 mapped on sparc and m68k.. */
125 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800126 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (sz > 0) {
128 if (clear_user(buf, sz))
129 return -EFAULT;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800130 buf += sz;
131 p += sz;
132 count -= sz;
133 read += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135 }
136#endif
137
138 while (count > 0) {
Wu Fengguangfa29e972009-12-14 17:58:08 -0800139 unsigned long remaining;
Kees Cook2c0ad232017-04-05 09:39:08 -0700140 int allowed;
Wu Fengguangfa29e972009-12-14 17:58:08 -0800141
Wu Fengguangf2223182009-12-14 17:58:07 -0800142 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Kees Cook2c0ad232017-04-05 09:39:08 -0700144 allowed = page_is_allowed(p >> PAGE_SHIFT);
145 if (!allowed)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700146 return -EPERM;
Kees Cook2c0ad232017-04-05 09:39:08 -0700147 if (allowed == 2) {
148 /* Show zeros for restricted memory. */
149 remaining = clear_user(buf, sz);
150 } else {
151 /*
152 * On ia64 if a page has been mapped somewhere as
153 * uncached, then it must also be accessed uncached
154 * by the kernel or data corruption may occur.
155 */
156 ptr = xlate_dev_mem_ptr(p);
157 if (!ptr)
158 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700159
Kees Cook2c0ad232017-04-05 09:39:08 -0700160 remaining = copy_to_user(buf, ptr, sz);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700161
Kees Cook2c0ad232017-04-05 09:39:08 -0700162 unxlate_dev_mem_ptr(p, ptr);
163 }
164
Wu Fengguangfa29e972009-12-14 17:58:08 -0800165 if (remaining)
166 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 buf += sz;
169 p += sz;
170 count -= sz;
171 read += sz;
Tetsuo Handa8c521632019-08-26 22:13:25 +0900172 if (should_stop_iteration())
173 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175
176 *ppos += read;
177 return read;
178}
179
Andrew Mortond7d4d842010-03-10 15:21:52 -0800180static ssize_t write_mem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 size_t count, loff_t *ppos)
182{
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400183 phys_addr_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 ssize_t written, sz;
185 unsigned long copied;
186 void *ptr;
187
Petr Tesarik08d2d002014-01-30 09:48:02 +0100188 if (p != *ppos)
189 return -EFBIG;
190
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800191 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return -EFAULT;
193
194 written = 0;
195
196#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
197 /* we don't have page 0 mapped on sparc and m68k.. */
198 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800199 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /* Hmm. Do something? */
201 buf += sz;
202 p += sz;
203 count -= sz;
204 written += sz;
205 }
206#endif
207
208 while (count > 0) {
Kees Cook2c0ad232017-04-05 09:39:08 -0700209 int allowed;
210
Wu Fengguangf2223182009-12-14 17:58:07 -0800211 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Kees Cook2c0ad232017-04-05 09:39:08 -0700213 allowed = page_is_allowed(p >> PAGE_SHIFT);
214 if (!allowed)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700215 return -EPERM;
216
Kees Cook2c0ad232017-04-05 09:39:08 -0700217 /* Skip actual writing when a page is marked as restricted. */
218 if (allowed == 1) {
219 /*
220 * On ia64 if a page has been mapped somewhere as
221 * uncached, then it must also be accessed uncached
222 * by the kernel or data corruption may occur.
223 */
224 ptr = xlate_dev_mem_ptr(p);
225 if (!ptr) {
226 if (written)
227 break;
228 return -EFAULT;
229 }
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700230
Kees Cook2c0ad232017-04-05 09:39:08 -0700231 copied = copy_from_user(ptr, buf, sz);
232 unxlate_dev_mem_ptr(p, ptr);
233 if (copied) {
234 written += sz - copied;
235 if (written)
236 break;
237 return -EFAULT;
238 }
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700239 }
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 buf += sz;
242 p += sz;
243 count -= sz;
244 written += sz;
Tetsuo Handa8c521632019-08-26 22:13:25 +0900245 if (should_stop_iteration())
246 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
249 *ppos += written;
250 return written;
251}
252
Andrew Mortond7d4d842010-03-10 15:21:52 -0800253int __weak phys_mem_access_prot_allowed(struct file *file,
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700254 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
255{
256 return 1;
257}
258
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800259#ifndef __HAVE_PHYS_MEM_ACCESS_PROT
Andrew Mortond7d4d842010-03-10 15:21:52 -0800260
261/*
262 * Architectures vary in how they handle caching for addresses
263 * outside of main memory.
264 *
265 */
David Howellsea56f412010-04-06 14:35:08 -0700266#ifdef pgprot_noncached
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400267static int uncached_access(struct file *file, phys_addr_t addr)
Andrew Mortond7d4d842010-03-10 15:21:52 -0800268{
269#if defined(CONFIG_IA64)
270 /*
271 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
272 * attribute aliases.
273 */
274 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
275#elif defined(CONFIG_MIPS)
276 {
277 extern int __uncached_access(struct file *file,
278 unsigned long addr);
279
280 return __uncached_access(file, addr);
281 }
282#else
283 /*
284 * Accessing memory above the top the kernel knows about or through a
285 * file pointer
286 * that was marked O_DSYNC will be done non-cached.
287 */
288 if (file->f_flags & O_DSYNC)
289 return 1;
290 return addr >= __pa(high_memory);
291#endif
292}
David Howellsea56f412010-04-06 14:35:08 -0700293#endif
Andrew Mortond7d4d842010-03-10 15:21:52 -0800294
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800295static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
296 unsigned long size, pgprot_t vma_prot)
297{
298#ifdef pgprot_noncached
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400299 phys_addr_t offset = pfn << PAGE_SHIFT;
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800300
301 if (uncached_access(file, offset))
302 return pgprot_noncached(vma_prot);
303#endif
304 return vma_prot;
305}
306#endif
307
David Howells5da61852006-09-27 01:50:16 -0700308#ifndef CONFIG_MMU
309static unsigned long get_unmapped_area_mem(struct file *file,
310 unsigned long addr,
311 unsigned long len,
312 unsigned long pgoff,
313 unsigned long flags)
314{
315 if (!valid_mmap_phys_addr_range(pgoff, len))
316 return (unsigned long) -EINVAL;
Benjamin Herrenschmidt8a932582007-04-16 22:53:16 -0700317 return pgoff << PAGE_SHIFT;
David Howells5da61852006-09-27 01:50:16 -0700318}
319
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100320/* permit direct mmap, for read, write or exec */
321static unsigned memory_mmap_capabilities(struct file *file)
322{
323 return NOMMU_MAP_DIRECT |
324 NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC;
325}
326
327static unsigned zero_mmap_capabilities(struct file *file)
328{
329 return NOMMU_MAP_COPY;
330}
331
David Howells5da61852006-09-27 01:50:16 -0700332/* can't do an in-place private mapping if there's no MMU */
333static inline int private_mapping_ok(struct vm_area_struct *vma)
334{
335 return vma->vm_flags & VM_MAYSHARE;
336}
337#else
David Howells5da61852006-09-27 01:50:16 -0700338
339static inline int private_mapping_ok(struct vm_area_struct *vma)
340{
341 return 1;
342}
343#endif
344
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400345static const struct vm_operations_struct mmap_mem_ops = {
Rik van Riel7ae8ed52008-07-23 21:27:07 -0700346#ifdef CONFIG_HAVE_IOREMAP_PROT
347 .access = generic_access_phys
348#endif
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700349};
350
Andrew Mortond7d4d842010-03-10 15:21:52 -0800351static int mmap_mem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800353 size_t size = vma->vm_end - vma->vm_start;
Julius Werner14891832017-05-12 14:42:58 -0700354 phys_addr_t offset = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
355
356 /* It's illegal to wrap around the end of the physical address space. */
Julius Werner9ff4a1a2017-06-02 15:36:39 -0700357 if (offset + (phys_addr_t)size - 1 < offset)
Julius Werner14891832017-05-12 14:42:58 -0700358 return -EINVAL;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800359
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700360 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800361 return -EINVAL;
362
David Howells5da61852006-09-27 01:50:16 -0700363 if (!private_mapping_ok(vma))
364 return -ENOSYS;
365
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800366 if (!range_is_allowed(vma->vm_pgoff, size))
367 return -EPERM;
368
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700369 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
370 &vma->vm_page_prot))
371 return -EINVAL;
372
Roland Dreier8b150472005-10-28 17:46:18 -0700373 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800374 size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700377 vma->vm_ops = &mmap_mem_ops;
378
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700379 /* Remap-pfn-range will mark the range VM_IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (remap_pfn_range(vma,
381 vma->vm_start,
382 vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800383 size,
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700384 vma->vm_page_prot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return -EAGAIN;
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return 0;
388}
389
Andrew Mortond7d4d842010-03-10 15:21:52 -0800390static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Linus Torvalds4bb82552005-08-13 14:22:59 -0700392 unsigned long pfn;
393
Linus Torvalds6d3154c2007-01-22 08:53:24 -0800394 /* Turn a kernel-virtual address into a physical page frame */
395 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800398 * RED-PEN: on some architectures there is more mapped memory than
399 * available in mem_map which pfn_valid checks for. Perhaps should add a
400 * new macro here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 *
402 * RED-PEN: vmalloc is not supported right now.
403 */
Linus Torvalds4bb82552005-08-13 14:22:59 -0700404 if (!pfn_valid(pfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return -EIO;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700406
407 vma->vm_pgoff = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return mmap_mem(file, vma);
409}
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411/*
412 * This function reads the *virtual* memory as seen by the kernel.
413 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800414static ssize_t read_kmem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 size_t count, loff_t *ppos)
416{
417 unsigned long p = *ppos;
418 ssize_t low_count, read, sz;
Hans Grob890537b2013-02-06 11:37:20 +0100419 char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800420 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 read = 0;
423 if (p < (unsigned long) high_memory) {
424 low_count = count;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800425 if (count > (unsigned long)high_memory - p)
426 low_count = (unsigned long)high_memory - p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
429 /* we don't have page 0 mapped on sparc and m68k.. */
430 if (p < PAGE_SIZE && low_count > 0) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800431 sz = size_inside_page(p, low_count);
432 if (clear_user(buf, sz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return -EFAULT;
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800434 buf += sz;
435 p += sz;
436 read += sz;
437 low_count -= sz;
438 count -= sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440#endif
441 while (low_count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800442 sz = size_inside_page(p, low_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 /*
445 * On ia64 if a page has been mapped somewhere as
446 * uncached, then it must also be accessed uncached
447 * by the kernel or data corruption may occur
448 */
Thierry Reding4707a342014-07-28 17:20:33 +0200449 kbuf = xlate_dev_kmem_ptr((void *)p);
Robin Murphy3fbaff32017-01-05 17:15:01 +0000450 if (!virt_addr_valid(kbuf))
451 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 if (copy_to_user(buf, kbuf, sz))
454 return -EFAULT;
455 buf += sz;
456 p += sz;
457 read += sz;
458 low_count -= sz;
459 count -= sz;
Tetsuo Handa8c521632019-08-26 22:13:25 +0900460 if (should_stop_iteration()) {
461 count = 0;
462 break;
463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465 }
466
467 if (count > 0) {
468 kbuf = (char *)__get_free_page(GFP_KERNEL);
469 if (!kbuf)
470 return -ENOMEM;
471 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800472 sz = size_inside_page(p, count);
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800473 if (!is_vmalloc_or_module_addr((void *)p)) {
474 err = -ENXIO;
475 break;
476 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800477 sz = vread(kbuf, (char *)p, sz);
478 if (!sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 break;
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800480 if (copy_to_user(buf, kbuf, sz)) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800481 err = -EFAULT;
482 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800484 count -= sz;
485 buf += sz;
486 read += sz;
487 p += sz;
Tetsuo Handa8c521632019-08-26 22:13:25 +0900488 if (should_stop_iteration())
489 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491 free_page((unsigned long)kbuf);
492 }
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800493 *ppos = p;
494 return read ? read : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497
Andrew Mortond7d4d842010-03-10 15:21:52 -0800498static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
499 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 ssize_t written, sz;
502 unsigned long copied;
503
504 written = 0;
505#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
506 /* we don't have page 0 mapped on sparc and m68k.. */
Wu Fengguangee323982009-12-14 17:58:10 -0800507 if (p < PAGE_SIZE) {
508 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 /* Hmm. Do something? */
510 buf += sz;
511 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 count -= sz;
513 written += sz;
514 }
515#endif
516
517 while (count > 0) {
Thierry Reding4707a342014-07-28 17:20:33 +0200518 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Wu Fengguangee323982009-12-14 17:58:10 -0800520 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800523 * On ia64 if a page has been mapped somewhere as uncached, then
524 * it must also be accessed uncached by the kernel or data
525 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 */
Thierry Reding4707a342014-07-28 17:20:33 +0200527 ptr = xlate_dev_kmem_ptr((void *)p);
Robin Murphy3fbaff32017-01-05 17:15:01 +0000528 if (!virt_addr_valid(ptr))
529 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 copied = copy_from_user(ptr, buf, sz);
532 if (copied) {
Jan Beulichc654d602006-03-25 03:07:31 -0800533 written += sz - copied;
534 if (written)
535 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return -EFAULT;
537 }
538 buf += sz;
539 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 count -= sz;
541 written += sz;
Tetsuo Handa8c521632019-08-26 22:13:25 +0900542 if (should_stop_iteration())
543 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545
546 *ppos += written;
547 return written;
548}
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550/*
551 * This function writes to the *virtual* memory as seen by the kernel.
552 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800553static ssize_t write_kmem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 size_t count, loff_t *ppos)
555{
556 unsigned long p = *ppos;
557 ssize_t wrote = 0;
558 ssize_t virtr = 0;
Hans Grob890537b2013-02-06 11:37:20 +0100559 char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800560 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 if (p < (unsigned long) high_memory) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800563 unsigned long to_write = min_t(unsigned long, count,
564 (unsigned long)high_memory - p);
Wu Fengguangee323982009-12-14 17:58:10 -0800565 wrote = do_write_kmem(p, buf, to_write, ppos);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800566 if (wrote != to_write)
567 return wrote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 p += wrote;
569 buf += wrote;
570 count -= wrote;
571 }
572
573 if (count > 0) {
574 kbuf = (char *)__get_free_page(GFP_KERNEL);
575 if (!kbuf)
576 return wrote ? wrote : -ENOMEM;
577 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800578 unsigned long sz = size_inside_page(p, count);
579 unsigned long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800581 if (!is_vmalloc_or_module_addr((void *)p)) {
582 err = -ENXIO;
583 break;
584 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800585 n = copy_from_user(kbuf, buf, sz);
586 if (n) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800587 err = -EFAULT;
588 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
Wu Fengguangc85e9a92010-02-02 13:44:06 -0800590 vwrite(kbuf, (char *)p, sz);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800591 count -= sz;
592 buf += sz;
593 virtr += sz;
594 p += sz;
Tetsuo Handa8c521632019-08-26 22:13:25 +0900595 if (should_stop_iteration())
596 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598 free_page((unsigned long)kbuf);
599 }
600
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800601 *ppos = p;
602 return virtr + wrote ? : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
Andrew Mortond7d4d842010-03-10 15:21:52 -0800605static ssize_t read_port(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 size_t count, loff_t *ppos)
607{
608 unsigned long i = *ppos;
609 char __user *tmp = buf;
610
611 if (!access_ok(VERIFY_WRITE, buf, count))
Andrew Mortond7d4d842010-03-10 15:21:52 -0800612 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 while (count-- > 0 && i < 65536) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800614 if (__put_user(inb(i), tmp) < 0)
615 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 i++;
617 tmp++;
618 }
619 *ppos = i;
620 return tmp-buf;
621}
622
Andrew Mortond7d4d842010-03-10 15:21:52 -0800623static ssize_t write_port(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 size_t count, loff_t *ppos)
625{
626 unsigned long i = *ppos;
Hans Grob890537b2013-02-06 11:37:20 +0100627 const char __user *tmp = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Andrew Mortond7d4d842010-03-10 15:21:52 -0800629 if (!access_ok(VERIFY_READ, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return -EFAULT;
631 while (count-- > 0 && i < 65536) {
632 char c;
Rob Ward6a0061b2014-12-20 18:28:36 +0000633
Jan Beulichc654d602006-03-25 03:07:31 -0800634 if (__get_user(c, tmp)) {
635 if (tmp > buf)
636 break;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800637 return -EFAULT;
Jan Beulichc654d602006-03-25 03:07:31 -0800638 }
Andrew Mortond7d4d842010-03-10 15:21:52 -0800639 outb(c, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 i++;
641 tmp++;
642 }
643 *ppos = i;
644 return tmp-buf;
645}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Andrew Mortond7d4d842010-03-10 15:21:52 -0800647static ssize_t read_null(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 size_t count, loff_t *ppos)
649{
650 return 0;
651}
652
Andrew Mortond7d4d842010-03-10 15:21:52 -0800653static ssize_t write_null(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 size_t count, loff_t *ppos)
655{
656 return count;
657}
658
Al Virocd28e282015-04-03 15:57:04 -0400659static ssize_t read_iter_null(struct kiocb *iocb, struct iov_iter *to)
Zach Brown162934d2013-05-07 16:18:27 -0700660{
661 return 0;
662}
663
Al Virocd28e282015-04-03 15:57:04 -0400664static ssize_t write_iter_null(struct kiocb *iocb, struct iov_iter *from)
Zach Brown162934d2013-05-07 16:18:27 -0700665{
Al Virocd28e282015-04-03 15:57:04 -0400666 size_t count = iov_iter_count(from);
667 iov_iter_advance(from, count);
668 return count;
Zach Brown162934d2013-05-07 16:18:27 -0700669}
670
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200671static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
672 struct splice_desc *sd)
673{
674 return sd->len;
675}
676
Andrew Mortond7d4d842010-03-10 15:21:52 -0800677static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200678 loff_t *ppos, size_t len, unsigned int flags)
679{
680 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
681}
682
Al Viro13ba33e2014-08-18 10:04:12 -0400683static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
Zach Brown162934d2013-05-07 16:18:27 -0700684{
685 size_t written = 0;
Zach Brown162934d2013-05-07 16:18:27 -0700686
Al Viro13ba33e2014-08-18 10:04:12 -0400687 while (iov_iter_count(iter)) {
688 size_t chunk = iov_iter_count(iter), n;
Rob Ward6a0061b2014-12-20 18:28:36 +0000689
Al Viro13ba33e2014-08-18 10:04:12 -0400690 if (chunk > PAGE_SIZE)
691 chunk = PAGE_SIZE; /* Just for latency reasons */
692 n = iov_iter_zero(chunk, iter);
693 if (!n && iov_iter_count(iter))
694 return written ? written : -EFAULT;
695 written += n;
696 if (signal_pending(current))
697 return written ? written : -ERESTARTSYS;
698 cond_resched();
Zach Brown162934d2013-05-07 16:18:27 -0700699 }
Al Viro13ba33e2014-08-18 10:04:12 -0400700 return written;
Zach Brown162934d2013-05-07 16:18:27 -0700701}
702
Andrew Mortond7d4d842010-03-10 15:21:52 -0800703static int mmap_zero(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700705#ifndef CONFIG_MMU
706 return -ENOSYS;
707#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (vma->vm_flags & VM_SHARED)
709 return shmem_zero_setup(vma);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700710 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Hugh Dickinsc01d5b32016-07-26 15:26:15 -0700713static unsigned long get_unmapped_area_zero(struct file *file,
714 unsigned long addr, unsigned long len,
715 unsigned long pgoff, unsigned long flags)
716{
717#ifdef CONFIG_MMU
718 if (flags & MAP_SHARED) {
719 /*
720 * mmap_zero() will call shmem_zero_setup() to create a file,
721 * so use shmem's get_unmapped_area in case it can be huge;
722 * and pass NULL for file as in mmap.c's get_unmapped_area(),
723 * so as not to confuse shmem with our handle on "/dev/zero".
724 */
725 return shmem_get_unmapped_area(NULL, addr, len, pgoff, flags);
726 }
727
728 /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
729 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
730#else
731 return -ENOSYS;
732#endif
733}
734
Andrew Mortond7d4d842010-03-10 15:21:52 -0800735static ssize_t write_full(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 size_t count, loff_t *ppos)
737{
738 return -ENOSPC;
739}
740
741/*
742 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
743 * can fopen() both devices with "a" now. This was previously impossible.
744 * -- SRB.
745 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800746static loff_t null_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 return file->f_pos = 0;
749}
750
751/*
752 * The memory devices use the full 32/64 bits of the offset, and so we cannot
753 * check against negative addresses: they are ok. The return value is weird,
754 * though, in that case (0).
755 *
756 * also note that seeking relative to the "end of file" isn't supported:
757 * it has no meaning, so it returns -EINVAL.
758 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800759static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 loff_t ret;
762
Al Viro59551022016-01-22 15:40:57 -0500763 inode_lock(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 switch (orig) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800765 case SEEK_CUR:
766 offset += file->f_pos;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800767 case SEEK_SET:
768 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
Andrzej Hajdaecb63a12016-02-15 15:35:21 +0100769 if ((unsigned long long)offset >= -MAX_ERRNO) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800770 ret = -EOVERFLOW;
771 break;
772 }
773 file->f_pos = offset;
774 ret = file->f_pos;
775 force_successful_syscall_return();
776 break;
777 default:
778 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
Al Viro59551022016-01-22 15:40:57 -0500780 inode_unlock(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return ret;
782}
783
Hans Grob890537b2013-02-06 11:37:20 +0100784static int open_port(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
787}
788
789#define zero_lseek null_lseek
790#define full_lseek null_lseek
791#define write_zero write_null
Al Virocd28e282015-04-03 15:57:04 -0400792#define write_iter_zero write_iter_null
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793#define open_mem open_port
794#define open_kmem open_mem
795
Rob Ward73f07182014-12-07 15:40:33 +0000796static const struct file_operations __maybe_unused mem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 .llseek = memory_lseek,
798 .read = read_mem,
799 .write = write_mem,
800 .mmap = mmap_mem,
801 .open = open_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100802#ifndef CONFIG_MMU
David Howells5da61852006-09-27 01:50:16 -0700803 .get_unmapped_area = get_unmapped_area_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100804 .mmap_capabilities = memory_mmap_capabilities,
805#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806};
807
Rob Warda8c91252014-12-07 15:40:34 +0000808static const struct file_operations __maybe_unused kmem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 .llseek = memory_lseek,
810 .read = read_kmem,
811 .write = write_kmem,
812 .mmap = mmap_kmem,
813 .open = open_kmem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100814#ifndef CONFIG_MMU
David Howells5da61852006-09-27 01:50:16 -0700815 .get_unmapped_area = get_unmapped_area_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100816 .mmap_capabilities = memory_mmap_capabilities,
817#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818};
819
Arjan van de Ven62322d22006-07-03 00:24:21 -0700820static const struct file_operations null_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 .llseek = null_lseek,
822 .read = read_null,
823 .write = write_null,
Al Virocd28e282015-04-03 15:57:04 -0400824 .read_iter = read_iter_null,
825 .write_iter = write_iter_null,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200826 .splice_write = splice_write_null,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827};
828
Rob Ward3a4bc2f2014-12-07 15:40:35 +0000829static const struct file_operations __maybe_unused port_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 .llseek = memory_lseek,
831 .read = read_port,
832 .write = write_port,
833 .open = open_port,
834};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Arjan van de Ven62322d22006-07-03 00:24:21 -0700836static const struct file_operations zero_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 .llseek = zero_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 .write = write_zero,
Al Viro13ba33e2014-08-18 10:04:12 -0400839 .read_iter = read_iter_zero,
Al Virocd28e282015-04-03 15:57:04 -0400840 .write_iter = write_iter_zero,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 .mmap = mmap_zero,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -0700842 .get_unmapped_area = get_unmapped_area_zero,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100843#ifndef CONFIG_MMU
844 .mmap_capabilities = zero_mmap_capabilities,
845#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846};
847
Arjan van de Ven62322d22006-07-03 00:24:21 -0700848static const struct file_operations full_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 .llseek = full_lseek,
Al Viro13ba33e2014-08-18 10:04:12 -0400850 .read_iter = read_iter_zero,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 .write = write_full,
852};
853
Kay Sievers389e0cb2009-07-04 16:51:29 +0200854static const struct memdev {
855 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400856 umode_t mode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200857 const struct file_operations *fops;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100858 fmode_t fmode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200859} devlist[] = {
Rob Ward73f07182014-12-07 15:40:33 +0000860#ifdef CONFIG_DEVMEM
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100861 [1] = { "mem", 0, &mem_fops, FMODE_UNSIGNED_OFFSET },
Rob Ward73f07182014-12-07 15:40:33 +0000862#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700863#ifdef CONFIG_DEVKMEM
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100864 [2] = { "kmem", 0, &kmem_fops, FMODE_UNSIGNED_OFFSET },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700865#endif
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100866 [3] = { "null", 0666, &null_fops, 0 },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700867#ifdef CONFIG_DEVPORT
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100868 [4] = { "port", 0, &port_fops, 0 },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700869#endif
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100870 [5] = { "zero", 0666, &zero_fops, 0 },
871 [7] = { "full", 0666, &full_fops, 0 },
872 [8] = { "random", 0666, &random_fops, 0 },
873 [9] = { "urandom", 0666, &urandom_fops, 0 },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200874#ifdef CONFIG_PRINTK
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100875 [11] = { "kmsg", 0644, &kmsg_fops, 0 },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200876#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700877};
878
879static int memory_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200881 int minor;
882 const struct memdev *dev;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700883
Kay Sievers389e0cb2009-07-04 16:51:29 +0200884 minor = iminor(inode);
885 if (minor >= ARRAY_SIZE(devlist))
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200886 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700887
Kay Sievers389e0cb2009-07-04 16:51:29 +0200888 dev = &devlist[minor];
889 if (!dev->fops)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200890 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700891
Kay Sievers389e0cb2009-07-04 16:51:29 +0200892 filp->f_op = dev->fops;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100893 filp->f_mode |= dev->fmode;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700894
Kay Sievers389e0cb2009-07-04 16:51:29 +0200895 if (dev->fops->open)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200896 return dev->fops->open(inode, filp);
897
898 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
Arjan van de Ven62322d22006-07-03 00:24:21 -0700901static const struct file_operations memory_fops = {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800902 .open = memory_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200903 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904};
905
Al Viro2c9ede52011-07-23 20:24:48 -0400906static char *mem_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +0200907{
908 if (mode && devlist[MINOR(dev->devt)].mode)
909 *mode = devlist[MINOR(dev->devt)].mode;
910 return NULL;
911}
912
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800913static struct class *mem_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915static int __init chr_dev_init(void)
916{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200917 int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Andrew Mortond7d4d842010-03-10 15:21:52 -0800919 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
921
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800922 mem_class = class_create(THIS_MODULE, "mem");
Anton Blanchard6e191f72010-04-06 14:34:55 -0700923 if (IS_ERR(mem_class))
924 return PTR_ERR(mem_class);
925
Kay Sieverse454cea2009-09-18 23:01:12 +0200926 mem_class->devnode = mem_devnode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200927 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
928 if (!devlist[minor].name)
929 continue;
Haren Mynenie1612de2012-07-11 15:18:44 +1000930
931 /*
Hans Grob890537b2013-02-06 11:37:20 +0100932 * Create /dev/port?
Haren Mynenie1612de2012-07-11 15:18:44 +1000933 */
934 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
935 continue;
936
Kay Sievers389e0cb2009-07-04 16:51:29 +0200937 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
938 NULL, devlist[minor].name);
939 }
Greg Kroah-Hartmanebf644c2006-07-25 17:13:31 -0700940
David Howells31d1d482010-08-06 16:34:43 +0100941 return tty_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944fs_initcall(chr_dev_init);