blob: df66a9dd0aae3c5ef27a4b92410d347eb8f82a3a [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;
Kees Cook22ec1a22017-12-01 13:19:39 -0800110 char *bounce;
111 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Petr Tesarik08d2d002014-01-30 09:48:02 +0100113 if (p != *ppos)
114 return 0;
115
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800116 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return -EFAULT;
118 read = 0;
119#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
120 /* we don't have page 0 mapped on sparc and m68k.. */
121 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800122 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (sz > 0) {
124 if (clear_user(buf, sz))
125 return -EFAULT;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800126 buf += sz;
127 p += sz;
128 count -= sz;
129 read += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131 }
132#endif
133
Kees Cook22ec1a22017-12-01 13:19:39 -0800134 bounce = kmalloc(PAGE_SIZE, GFP_KERNEL);
135 if (!bounce)
136 return -ENOMEM;
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 while (count > 0) {
Wu Fengguangfa29e972009-12-14 17:58:08 -0800139 unsigned long remaining;
Kees Cookb5b38202018-03-27 14:06:14 -0700140 int allowed, probe;
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 Cook22ec1a22017-12-01 13:19:39 -0800144 err = -EPERM;
Kees Cooka4866aa2017-04-05 09:39:08 -0700145 allowed = page_is_allowed(p >> PAGE_SHIFT);
146 if (!allowed)
Kees Cook22ec1a22017-12-01 13:19:39 -0800147 goto failed;
148
149 err = -EFAULT;
Kees Cooka4866aa2017-04-05 09:39:08 -0700150 if (allowed == 2) {
151 /* Show zeros for restricted memory. */
152 remaining = clear_user(buf, sz);
153 } else {
154 /*
155 * On ia64 if a page has been mapped somewhere as
156 * uncached, then it must also be accessed uncached
157 * by the kernel or data corruption may occur.
158 */
159 ptr = xlate_dev_mem_ptr(p);
160 if (!ptr)
Kees Cook22ec1a22017-12-01 13:19:39 -0800161 goto failed;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700162
Kees Cookb5b38202018-03-27 14:06:14 -0700163 probe = probe_kernel_read(bounce, ptr, sz);
Kees Cooka4866aa2017-04-05 09:39:08 -0700164 unxlate_dev_mem_ptr(p, ptr);
Kees Cookb5b38202018-03-27 14:06:14 -0700165 if (probe)
Kees Cook22ec1a22017-12-01 13:19:39 -0800166 goto failed;
167
168 remaining = copy_to_user(buf, bounce, sz);
Kees Cooka4866aa2017-04-05 09:39:08 -0700169 }
170
Wu Fengguangfa29e972009-12-14 17:58:08 -0800171 if (remaining)
Kees Cook22ec1a22017-12-01 13:19:39 -0800172 goto failed;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 buf += sz;
175 p += sz;
176 count -= sz;
177 read += sz;
178 }
Kees Cook22ec1a22017-12-01 13:19:39 -0800179 kfree(bounce);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 *ppos += read;
182 return read;
Kees Cook22ec1a22017-12-01 13:19:39 -0800183
184failed:
185 kfree(bounce);
186 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Andrew Mortond7d4d842010-03-10 15:21:52 -0800189static ssize_t write_mem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 size_t count, loff_t *ppos)
191{
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400192 phys_addr_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 ssize_t written, sz;
194 unsigned long copied;
195 void *ptr;
196
Petr Tesarik08d2d002014-01-30 09:48:02 +0100197 if (p != *ppos)
198 return -EFBIG;
199
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800200 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return -EFAULT;
202
203 written = 0;
204
205#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
206 /* we don't have page 0 mapped on sparc and m68k.. */
207 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800208 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /* Hmm. Do something? */
210 buf += sz;
211 p += sz;
212 count -= sz;
213 written += sz;
214 }
215#endif
216
217 while (count > 0) {
Kees Cooka4866aa2017-04-05 09:39:08 -0700218 int allowed;
219
Wu Fengguangf2223182009-12-14 17:58:07 -0800220 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Kees Cooka4866aa2017-04-05 09:39:08 -0700222 allowed = page_is_allowed(p >> PAGE_SHIFT);
223 if (!allowed)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700224 return -EPERM;
225
Kees Cooka4866aa2017-04-05 09:39:08 -0700226 /* Skip actual writing when a page is marked as restricted. */
227 if (allowed == 1) {
228 /*
229 * On ia64 if a page has been mapped somewhere as
230 * uncached, then it must also be accessed uncached
231 * by the kernel or data corruption may occur.
232 */
233 ptr = xlate_dev_mem_ptr(p);
234 if (!ptr) {
235 if (written)
236 break;
237 return -EFAULT;
238 }
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700239
Kees Cooka4866aa2017-04-05 09:39:08 -0700240 copied = copy_from_user(ptr, buf, sz);
241 unxlate_dev_mem_ptr(p, ptr);
242 if (copied) {
243 written += sz - copied;
244 if (written)
245 break;
246 return -EFAULT;
247 }
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700248 }
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 buf += sz;
251 p += sz;
252 count -= sz;
253 written += sz;
254 }
255
256 *ppos += written;
257 return written;
258}
259
Andrew Mortond7d4d842010-03-10 15:21:52 -0800260int __weak phys_mem_access_prot_allowed(struct file *file,
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700261 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
262{
263 return 1;
264}
265
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800266#ifndef __HAVE_PHYS_MEM_ACCESS_PROT
Andrew Mortond7d4d842010-03-10 15:21:52 -0800267
268/*
269 * Architectures vary in how they handle caching for addresses
270 * outside of main memory.
271 *
272 */
David Howellsea56f412010-04-06 14:35:08 -0700273#ifdef pgprot_noncached
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400274static int uncached_access(struct file *file, phys_addr_t addr)
Andrew Mortond7d4d842010-03-10 15:21:52 -0800275{
276#if defined(CONFIG_IA64)
277 /*
278 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
279 * attribute aliases.
280 */
281 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
282#elif defined(CONFIG_MIPS)
283 {
284 extern int __uncached_access(struct file *file,
285 unsigned long addr);
286
287 return __uncached_access(file, addr);
288 }
289#else
290 /*
291 * Accessing memory above the top the kernel knows about or through a
292 * file pointer
293 * that was marked O_DSYNC will be done non-cached.
294 */
295 if (file->f_flags & O_DSYNC)
296 return 1;
297 return addr >= __pa(high_memory);
298#endif
299}
David Howellsea56f412010-04-06 14:35:08 -0700300#endif
Andrew Mortond7d4d842010-03-10 15:21:52 -0800301
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800302static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
303 unsigned long size, pgprot_t vma_prot)
304{
305#ifdef pgprot_noncached
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400306 phys_addr_t offset = pfn << PAGE_SHIFT;
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800307
308 if (uncached_access(file, offset))
309 return pgprot_noncached(vma_prot);
310#endif
311 return vma_prot;
312}
313#endif
314
David Howells5da61852006-09-27 01:50:16 -0700315#ifndef CONFIG_MMU
316static unsigned long get_unmapped_area_mem(struct file *file,
317 unsigned long addr,
318 unsigned long len,
319 unsigned long pgoff,
320 unsigned long flags)
321{
322 if (!valid_mmap_phys_addr_range(pgoff, len))
323 return (unsigned long) -EINVAL;
Benjamin Herrenschmidt8a932582007-04-16 22:53:16 -0700324 return pgoff << PAGE_SHIFT;
David Howells5da61852006-09-27 01:50:16 -0700325}
326
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100327/* permit direct mmap, for read, write or exec */
328static unsigned memory_mmap_capabilities(struct file *file)
329{
330 return NOMMU_MAP_DIRECT |
331 NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC;
332}
333
334static unsigned zero_mmap_capabilities(struct file *file)
335{
336 return NOMMU_MAP_COPY;
337}
338
David Howells5da61852006-09-27 01:50:16 -0700339/* can't do an in-place private mapping if there's no MMU */
340static inline int private_mapping_ok(struct vm_area_struct *vma)
341{
342 return vma->vm_flags & VM_MAYSHARE;
343}
344#else
David Howells5da61852006-09-27 01:50:16 -0700345
346static inline int private_mapping_ok(struct vm_area_struct *vma)
347{
348 return 1;
349}
350#endif
351
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400352static const struct vm_operations_struct mmap_mem_ops = {
Rik van Riel7ae8ed52008-07-23 21:27:07 -0700353#ifdef CONFIG_HAVE_IOREMAP_PROT
354 .access = generic_access_phys
355#endif
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700356};
357
Andrew Mortond7d4d842010-03-10 15:21:52 -0800358static int mmap_mem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800360 size_t size = vma->vm_end - vma->vm_start;
Julius Wernerb299cde2017-05-12 14:42:58 -0700361 phys_addr_t offset = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
362
Craig Bergstrombe62a322017-11-15 15:29:51 -0700363 /* Does it even fit in phys_addr_t? */
364 if (offset >> PAGE_SHIFT != vma->vm_pgoff)
365 return -EINVAL;
366
Julius Wernerb299cde2017-05-12 14:42:58 -0700367 /* It's illegal to wrap around the end of the physical address space. */
Julius Werner32829da2017-06-02 15:36:39 -0700368 if (offset + (phys_addr_t)size - 1 < offset)
Julius Wernerb299cde2017-05-12 14:42:58 -0700369 return -EINVAL;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800370
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700371 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800372 return -EINVAL;
373
David Howells5da61852006-09-27 01:50:16 -0700374 if (!private_mapping_ok(vma))
375 return -ENOSYS;
376
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800377 if (!range_is_allowed(vma->vm_pgoff, size))
378 return -EPERM;
379
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700380 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
381 &vma->vm_page_prot))
382 return -EINVAL;
383
Roland Dreier8b150472005-10-28 17:46:18 -0700384 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800385 size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700388 vma->vm_ops = &mmap_mem_ops;
389
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700390 /* Remap-pfn-range will mark the range VM_IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (remap_pfn_range(vma,
392 vma->vm_start,
393 vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800394 size,
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700395 vma->vm_page_prot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return -EAGAIN;
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return 0;
399}
400
Andrew Mortond7d4d842010-03-10 15:21:52 -0800401static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Linus Torvalds4bb82552005-08-13 14:22:59 -0700403 unsigned long pfn;
404
Linus Torvalds6d3154c2007-01-22 08:53:24 -0800405 /* Turn a kernel-virtual address into a physical page frame */
406 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800409 * RED-PEN: on some architectures there is more mapped memory than
410 * available in mem_map which pfn_valid checks for. Perhaps should add a
411 * new macro here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 *
413 * RED-PEN: vmalloc is not supported right now.
414 */
Linus Torvalds4bb82552005-08-13 14:22:59 -0700415 if (!pfn_valid(pfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return -EIO;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700417
418 vma->vm_pgoff = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return mmap_mem(file, vma);
420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422/*
423 * This function reads the *virtual* memory as seen by the kernel.
424 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800425static ssize_t read_kmem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 size_t count, loff_t *ppos)
427{
428 unsigned long p = *ppos;
429 ssize_t low_count, read, sz;
Hans Grob890537b2013-02-06 11:37:20 +0100430 char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800431 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 read = 0;
434 if (p < (unsigned long) high_memory) {
435 low_count = count;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800436 if (count > (unsigned long)high_memory - p)
437 low_count = (unsigned long)high_memory - p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
440 /* we don't have page 0 mapped on sparc and m68k.. */
441 if (p < PAGE_SIZE && low_count > 0) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800442 sz = size_inside_page(p, low_count);
443 if (clear_user(buf, sz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return -EFAULT;
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800445 buf += sz;
446 p += sz;
447 read += sz;
448 low_count -= sz;
449 count -= sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451#endif
452 while (low_count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800453 sz = size_inside_page(p, low_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 /*
456 * On ia64 if a page has been mapped somewhere as
457 * uncached, then it must also be accessed uncached
458 * by the kernel or data corruption may occur
459 */
Thierry Reding4707a342014-07-28 17:20:33 +0200460 kbuf = xlate_dev_kmem_ptr((void *)p);
Robin Murphy488debb92017-01-05 17:15:01 +0000461 if (!virt_addr_valid(kbuf))
462 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 if (copy_to_user(buf, kbuf, sz))
465 return -EFAULT;
466 buf += sz;
467 p += sz;
468 read += sz;
469 low_count -= sz;
470 count -= sz;
471 }
472 }
473
474 if (count > 0) {
475 kbuf = (char *)__get_free_page(GFP_KERNEL);
476 if (!kbuf)
477 return -ENOMEM;
478 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800479 sz = size_inside_page(p, count);
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800480 if (!is_vmalloc_or_module_addr((void *)p)) {
481 err = -ENXIO;
482 break;
483 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800484 sz = vread(kbuf, (char *)p, sz);
485 if (!sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 break;
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800487 if (copy_to_user(buf, kbuf, sz)) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800488 err = -EFAULT;
489 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800491 count -= sz;
492 buf += sz;
493 read += sz;
494 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496 free_page((unsigned long)kbuf);
497 }
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800498 *ppos = p;
499 return read ? read : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
502
Andrew Mortond7d4d842010-03-10 15:21:52 -0800503static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
504 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 ssize_t written, sz;
507 unsigned long copied;
508
509 written = 0;
510#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
511 /* we don't have page 0 mapped on sparc and m68k.. */
Wu Fengguangee323982009-12-14 17:58:10 -0800512 if (p < PAGE_SIZE) {
513 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /* Hmm. Do something? */
515 buf += sz;
516 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 count -= sz;
518 written += sz;
519 }
520#endif
521
522 while (count > 0) {
Thierry Reding4707a342014-07-28 17:20:33 +0200523 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Wu Fengguangee323982009-12-14 17:58:10 -0800525 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800528 * On ia64 if a page has been mapped somewhere as uncached, then
529 * it must also be accessed uncached by the kernel or data
530 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 */
Thierry Reding4707a342014-07-28 17:20:33 +0200532 ptr = xlate_dev_kmem_ptr((void *)p);
Robin Murphy488debb92017-01-05 17:15:01 +0000533 if (!virt_addr_valid(ptr))
534 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 copied = copy_from_user(ptr, buf, sz);
537 if (copied) {
Jan Beulichc654d602006-03-25 03:07:31 -0800538 written += sz - copied;
539 if (written)
540 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return -EFAULT;
542 }
543 buf += sz;
544 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 count -= sz;
546 written += sz;
547 }
548
549 *ppos += written;
550 return written;
551}
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553/*
554 * This function writes to the *virtual* memory as seen by the kernel.
555 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800556static ssize_t write_kmem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 size_t count, loff_t *ppos)
558{
559 unsigned long p = *ppos;
560 ssize_t wrote = 0;
561 ssize_t virtr = 0;
Hans Grob890537b2013-02-06 11:37:20 +0100562 char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800563 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 if (p < (unsigned long) high_memory) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800566 unsigned long to_write = min_t(unsigned long, count,
567 (unsigned long)high_memory - p);
Wu Fengguangee323982009-12-14 17:58:10 -0800568 wrote = do_write_kmem(p, buf, to_write, ppos);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800569 if (wrote != to_write)
570 return wrote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 p += wrote;
572 buf += wrote;
573 count -= wrote;
574 }
575
576 if (count > 0) {
577 kbuf = (char *)__get_free_page(GFP_KERNEL);
578 if (!kbuf)
579 return wrote ? wrote : -ENOMEM;
580 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800581 unsigned long sz = size_inside_page(p, count);
582 unsigned long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800584 if (!is_vmalloc_or_module_addr((void *)p)) {
585 err = -ENXIO;
586 break;
587 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800588 n = copy_from_user(kbuf, buf, sz);
589 if (n) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800590 err = -EFAULT;
591 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
Wu Fengguangc85e9a92010-02-02 13:44:06 -0800593 vwrite(kbuf, (char *)p, sz);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800594 count -= sz;
595 buf += sz;
596 virtr += sz;
597 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599 free_page((unsigned long)kbuf);
600 }
601
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800602 *ppos = p;
603 return virtr + wrote ? : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
Andrew Mortond7d4d842010-03-10 15:21:52 -0800606static ssize_t read_port(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 size_t count, loff_t *ppos)
608{
609 unsigned long i = *ppos;
610 char __user *tmp = buf;
611
612 if (!access_ok(VERIFY_WRITE, buf, count))
Andrew Mortond7d4d842010-03-10 15:21:52 -0800613 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 while (count-- > 0 && i < 65536) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800615 if (__put_user(inb(i), tmp) < 0)
616 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 i++;
618 tmp++;
619 }
620 *ppos = i;
621 return tmp-buf;
622}
623
Andrew Mortond7d4d842010-03-10 15:21:52 -0800624static ssize_t write_port(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 size_t count, loff_t *ppos)
626{
627 unsigned long i = *ppos;
Hans Grob890537b2013-02-06 11:37:20 +0100628 const char __user *tmp = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Andrew Mortond7d4d842010-03-10 15:21:52 -0800630 if (!access_ok(VERIFY_READ, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return -EFAULT;
632 while (count-- > 0 && i < 65536) {
633 char c;
Rob Ward6a0061b2014-12-20 18:28:36 +0000634
Jan Beulichc654d602006-03-25 03:07:31 -0800635 if (__get_user(c, tmp)) {
636 if (tmp > buf)
637 break;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800638 return -EFAULT;
Jan Beulichc654d602006-03-25 03:07:31 -0800639 }
Andrew Mortond7d4d842010-03-10 15:21:52 -0800640 outb(c, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 i++;
642 tmp++;
643 }
644 *ppos = i;
645 return tmp-buf;
646}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Andrew Mortond7d4d842010-03-10 15:21:52 -0800648static ssize_t read_null(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 size_t count, loff_t *ppos)
650{
651 return 0;
652}
653
Andrew Mortond7d4d842010-03-10 15:21:52 -0800654static ssize_t write_null(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 size_t count, loff_t *ppos)
656{
657 return count;
658}
659
Al Virocd28e282015-04-03 15:57:04 -0400660static ssize_t read_iter_null(struct kiocb *iocb, struct iov_iter *to)
Zach Brown162934d2013-05-07 16:18:27 -0700661{
662 return 0;
663}
664
Al Virocd28e282015-04-03 15:57:04 -0400665static ssize_t write_iter_null(struct kiocb *iocb, struct iov_iter *from)
Zach Brown162934d2013-05-07 16:18:27 -0700666{
Al Virocd28e282015-04-03 15:57:04 -0400667 size_t count = iov_iter_count(from);
668 iov_iter_advance(from, count);
669 return count;
Zach Brown162934d2013-05-07 16:18:27 -0700670}
671
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200672static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
673 struct splice_desc *sd)
674{
675 return sd->len;
676}
677
Andrew Mortond7d4d842010-03-10 15:21:52 -0800678static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200679 loff_t *ppos, size_t len, unsigned int flags)
680{
681 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
682}
683
Al Viro13ba33e2014-08-18 10:04:12 -0400684static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
Zach Brown162934d2013-05-07 16:18:27 -0700685{
686 size_t written = 0;
Zach Brown162934d2013-05-07 16:18:27 -0700687
Al Viro13ba33e2014-08-18 10:04:12 -0400688 while (iov_iter_count(iter)) {
689 size_t chunk = iov_iter_count(iter), n;
Rob Ward6a0061b2014-12-20 18:28:36 +0000690
Al Viro13ba33e2014-08-18 10:04:12 -0400691 if (chunk > PAGE_SIZE)
692 chunk = PAGE_SIZE; /* Just for latency reasons */
693 n = iov_iter_zero(chunk, iter);
694 if (!n && iov_iter_count(iter))
695 return written ? written : -EFAULT;
696 written += n;
697 if (signal_pending(current))
698 return written ? written : -ERESTARTSYS;
699 cond_resched();
Zach Brown162934d2013-05-07 16:18:27 -0700700 }
Al Viro13ba33e2014-08-18 10:04:12 -0400701 return written;
Zach Brown162934d2013-05-07 16:18:27 -0700702}
703
Andrew Mortond7d4d842010-03-10 15:21:52 -0800704static int mmap_zero(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700706#ifndef CONFIG_MMU
707 return -ENOSYS;
708#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (vma->vm_flags & VM_SHARED)
710 return shmem_zero_setup(vma);
Kirill A. Shutemovbfd40ea2018-07-26 16:37:35 -0700711 vma_set_anonymous(vma);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700712 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Hugh Dickinsc01d5b32016-07-26 15:26:15 -0700715static unsigned long get_unmapped_area_zero(struct file *file,
716 unsigned long addr, unsigned long len,
717 unsigned long pgoff, unsigned long flags)
718{
719#ifdef CONFIG_MMU
720 if (flags & MAP_SHARED) {
721 /*
722 * mmap_zero() will call shmem_zero_setup() to create a file,
723 * so use shmem's get_unmapped_area in case it can be huge;
724 * and pass NULL for file as in mmap.c's get_unmapped_area(),
725 * so as not to confuse shmem with our handle on "/dev/zero".
726 */
727 return shmem_get_unmapped_area(NULL, addr, len, pgoff, flags);
728 }
729
730 /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
731 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
732#else
733 return -ENOSYS;
734#endif
735}
736
Andrew Mortond7d4d842010-03-10 15:21:52 -0800737static ssize_t write_full(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 size_t count, loff_t *ppos)
739{
740 return -ENOSPC;
741}
742
743/*
744 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
745 * can fopen() both devices with "a" now. This was previously impossible.
746 * -- SRB.
747 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800748static loff_t null_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 return file->f_pos = 0;
751}
752
753/*
754 * The memory devices use the full 32/64 bits of the offset, and so we cannot
755 * check against negative addresses: they are ok. The return value is weird,
756 * though, in that case (0).
757 *
758 * also note that seeking relative to the "end of file" isn't supported:
759 * it has no meaning, so it returns -EINVAL.
760 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800761static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 loff_t ret;
764
Al Viro59551022016-01-22 15:40:57 -0500765 inode_lock(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 switch (orig) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800767 case SEEK_CUR:
768 offset += file->f_pos;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800769 case SEEK_SET:
770 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
Andrzej Hajdaecb63a12016-02-15 15:35:21 +0100771 if ((unsigned long long)offset >= -MAX_ERRNO) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800772 ret = -EOVERFLOW;
773 break;
774 }
775 file->f_pos = offset;
776 ret = file->f_pos;
777 force_successful_syscall_return();
778 break;
779 default:
780 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
Al Viro59551022016-01-22 15:40:57 -0500782 inode_unlock(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return ret;
784}
785
Hans Grob890537b2013-02-06 11:37:20 +0100786static int open_port(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
789}
790
791#define zero_lseek null_lseek
792#define full_lseek null_lseek
793#define write_zero write_null
Al Virocd28e282015-04-03 15:57:04 -0400794#define write_iter_zero write_iter_null
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795#define open_mem open_port
796#define open_kmem open_mem
797
Rob Ward73f07182014-12-07 15:40:33 +0000798static const struct file_operations __maybe_unused mem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 .llseek = memory_lseek,
800 .read = read_mem,
801 .write = write_mem,
802 .mmap = mmap_mem,
803 .open = open_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100804#ifndef CONFIG_MMU
David Howells5da61852006-09-27 01:50:16 -0700805 .get_unmapped_area = get_unmapped_area_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100806 .mmap_capabilities = memory_mmap_capabilities,
807#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808};
809
Rob Warda8c91252014-12-07 15:40:34 +0000810static const struct file_operations __maybe_unused kmem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 .llseek = memory_lseek,
812 .read = read_kmem,
813 .write = write_kmem,
814 .mmap = mmap_kmem,
815 .open = open_kmem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100816#ifndef CONFIG_MMU
David Howells5da61852006-09-27 01:50:16 -0700817 .get_unmapped_area = get_unmapped_area_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100818 .mmap_capabilities = memory_mmap_capabilities,
819#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820};
821
Arjan van de Ven62322d22006-07-03 00:24:21 -0700822static const struct file_operations null_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 .llseek = null_lseek,
824 .read = read_null,
825 .write = write_null,
Al Virocd28e282015-04-03 15:57:04 -0400826 .read_iter = read_iter_null,
827 .write_iter = write_iter_null,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200828 .splice_write = splice_write_null,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829};
830
Rob Ward3a4bc2f2014-12-07 15:40:35 +0000831static const struct file_operations __maybe_unused port_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 .llseek = memory_lseek,
833 .read = read_port,
834 .write = write_port,
835 .open = open_port,
836};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Arjan van de Ven62322d22006-07-03 00:24:21 -0700838static const struct file_operations zero_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 .llseek = zero_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 .write = write_zero,
Al Viro13ba33e2014-08-18 10:04:12 -0400841 .read_iter = read_iter_zero,
Al Virocd28e282015-04-03 15:57:04 -0400842 .write_iter = write_iter_zero,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 .mmap = mmap_zero,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -0700844 .get_unmapped_area = get_unmapped_area_zero,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100845#ifndef CONFIG_MMU
846 .mmap_capabilities = zero_mmap_capabilities,
847#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848};
849
Arjan van de Ven62322d22006-07-03 00:24:21 -0700850static const struct file_operations full_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 .llseek = full_lseek,
Al Viro13ba33e2014-08-18 10:04:12 -0400852 .read_iter = read_iter_zero,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 .write = write_full,
854};
855
Kay Sievers389e0cb2009-07-04 16:51:29 +0200856static const struct memdev {
857 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400858 umode_t mode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200859 const struct file_operations *fops;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100860 fmode_t fmode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200861} devlist[] = {
Rob Ward73f07182014-12-07 15:40:33 +0000862#ifdef CONFIG_DEVMEM
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100863 [1] = { "mem", 0, &mem_fops, FMODE_UNSIGNED_OFFSET },
Rob Ward73f07182014-12-07 15:40:33 +0000864#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700865#ifdef CONFIG_DEVKMEM
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100866 [2] = { "kmem", 0, &kmem_fops, FMODE_UNSIGNED_OFFSET },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700867#endif
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100868 [3] = { "null", 0666, &null_fops, 0 },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700869#ifdef CONFIG_DEVPORT
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100870 [4] = { "port", 0, &port_fops, 0 },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700871#endif
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100872 [5] = { "zero", 0666, &zero_fops, 0 },
873 [7] = { "full", 0666, &full_fops, 0 },
874 [8] = { "random", 0666, &random_fops, 0 },
875 [9] = { "urandom", 0666, &urandom_fops, 0 },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200876#ifdef CONFIG_PRINTK
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100877 [11] = { "kmsg", 0644, &kmsg_fops, 0 },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200878#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700879};
880
881static int memory_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200883 int minor;
884 const struct memdev *dev;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700885
Kay Sievers389e0cb2009-07-04 16:51:29 +0200886 minor = iminor(inode);
887 if (minor >= ARRAY_SIZE(devlist))
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200888 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700889
Kay Sievers389e0cb2009-07-04 16:51:29 +0200890 dev = &devlist[minor];
891 if (!dev->fops)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200892 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700893
Kay Sievers389e0cb2009-07-04 16:51:29 +0200894 filp->f_op = dev->fops;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100895 filp->f_mode |= dev->fmode;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700896
Kay Sievers389e0cb2009-07-04 16:51:29 +0200897 if (dev->fops->open)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200898 return dev->fops->open(inode, filp);
899
900 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
Arjan van de Ven62322d22006-07-03 00:24:21 -0700903static const struct file_operations memory_fops = {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800904 .open = memory_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200905 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906};
907
Al Viro2c9ede52011-07-23 20:24:48 -0400908static char *mem_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +0200909{
910 if (mode && devlist[MINOR(dev->devt)].mode)
911 *mode = devlist[MINOR(dev->devt)].mode;
912 return NULL;
913}
914
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800915static struct class *mem_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917static int __init chr_dev_init(void)
918{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200919 int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Andrew Mortond7d4d842010-03-10 15:21:52 -0800921 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
923
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800924 mem_class = class_create(THIS_MODULE, "mem");
Anton Blanchard6e191f72010-04-06 14:34:55 -0700925 if (IS_ERR(mem_class))
926 return PTR_ERR(mem_class);
927
Kay Sieverse454cea2009-09-18 23:01:12 +0200928 mem_class->devnode = mem_devnode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200929 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
930 if (!devlist[minor].name)
931 continue;
Haren Mynenie1612de2012-07-11 15:18:44 +1000932
933 /*
Hans Grob890537b2013-02-06 11:37:20 +0100934 * Create /dev/port?
Haren Mynenie1612de2012-07-11 15:18:44 +1000935 */
936 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
937 continue;
938
Kay Sievers389e0cb2009-07-04 16:51:29 +0200939 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
940 NULL, devlist[minor].name);
941 }
Greg Kroah-Hartmanebf644c2006-07-25 17:13:31 -0700942
David Howells31d1d482010-08-06 16:34:43 +0100943 return tty_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
946fs_initcall(chr_dev_init);