1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/drivers/char/mem.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * Added devfs support.
81da177e4SLinus Torvalds * Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
9af901ca1SAndré Goddard Rosa * Shared /dev/zero mmapping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
101da177e4SLinus Torvalds */
111da177e4SLinus Torvalds
121da177e4SLinus Torvalds #include <linux/mm.h>
13*7be022c9SCédric Le Goater #include <linux/moduleparam.h>
141da177e4SLinus Torvalds #include <linux/miscdevice.h>
151da177e4SLinus Torvalds #include <linux/slab.h>
161da177e4SLinus Torvalds #include <linux/vmalloc.h>
171da177e4SLinus Torvalds #include <linux/mman.h>
181da177e4SLinus Torvalds #include <linux/random.h>
191da177e4SLinus Torvalds #include <linux/init.h>
201da177e4SLinus Torvalds #include <linux/tty.h>
211da177e4SLinus Torvalds #include <linux/capability.h>
221da177e4SLinus Torvalds #include <linux/ptrace.h>
231da177e4SLinus Torvalds #include <linux/device.h>
2450b1fdbdSVivek Goyal #include <linux/highmem.h>
251da177e4SLinus Torvalds #include <linux/backing-dev.h>
26c01d5b30SHugh Dickins #include <linux/shmem_fs.h>
27d6b29d7cSJens Axboe #include <linux/splice.h>
28b8a3ad5bSLinus Torvalds #include <linux/pfn.h>
2966300e66SPaul Gortmaker #include <linux/export.h>
30e1612de9SHaren Myneni #include <linux/io.h>
31e2e40f2cSChristoph Hellwig #include <linux/uio.h>
3235b6c7e4SRob Ward #include <linux/uaccess.h>
339b9d8ddaSMatthew Garrett #include <linux/security.h>
341da177e4SLinus Torvalds
351da177e4SLinus Torvalds #ifdef CONFIG_IA64
361da177e4SLinus Torvalds # include <linux/efi.h>
371da177e4SLinus Torvalds #endif
381da177e4SLinus Torvalds
393234ac66SDan Williams #define DEVMEM_MINOR 1
40e1612de9SHaren Myneni #define DEVPORT_MINOR 4
41e1612de9SHaren Myneni
size_inside_page(unsigned long start,unsigned long size)42f222318eSWu Fengguang static inline unsigned long size_inside_page(unsigned long start,
43f222318eSWu Fengguang unsigned long size)
44f222318eSWu Fengguang {
45f222318eSWu Fengguang unsigned long sz;
46f222318eSWu Fengguang
477fabadddSWu Fengguang sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
48f222318eSWu Fengguang
497fabadddSWu Fengguang return min(sz, size);
50f222318eSWu Fengguang }
51f222318eSWu Fengguang
521da177e4SLinus Torvalds #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
valid_phys_addr_range(phys_addr_t addr,size_t count)537e6735c3SCyril Chemparathy static inline int valid_phys_addr_range(phys_addr_t addr, size_t count)
541da177e4SLinus Torvalds {
55cfaf346cSChangli Gao return addr + count <= __pa(high_memory);
561da177e4SLinus Torvalds }
5780851ef2SBjorn Helgaas
valid_mmap_phys_addr_range(unsigned long pfn,size_t size)5806c67befSLennert Buytenhek static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
5980851ef2SBjorn Helgaas {
6080851ef2SBjorn Helgaas return 1;
6180851ef2SBjorn Helgaas }
621da177e4SLinus Torvalds #endif
631da177e4SLinus Torvalds
64d092633bSIngo Molnar #ifdef CONFIG_STRICT_DEVMEM
page_is_allowed(unsigned long pfn)65a4866aa8SKees Cook static inline int page_is_allowed(unsigned long pfn)
66a4866aa8SKees Cook {
67a4866aa8SKees Cook return devmem_is_allowed(pfn);
68a4866aa8SKees Cook }
range_is_allowed(unsigned long pfn,unsigned long size)69e2beb3eaSVenki Pallipadi static inline int range_is_allowed(unsigned long pfn, unsigned long size)
70ae531c26SArjan van de Ven {
71e2beb3eaSVenki Pallipadi u64 from = ((u64)pfn) << PAGE_SHIFT;
72e2beb3eaSVenki Pallipadi u64 to = from + size;
73e2beb3eaSVenki Pallipadi u64 cursor = from;
74ae531c26SArjan van de Ven
75e2beb3eaSVenki Pallipadi while (cursor < to) {
7639380b80SJiri Kosina if (!devmem_is_allowed(pfn))
77ae531c26SArjan van de Ven return 0;
78e2beb3eaSVenki Pallipadi cursor += PAGE_SIZE;
79e2beb3eaSVenki Pallipadi pfn++;
80ae531c26SArjan van de Ven }
81ae531c26SArjan van de Ven return 1;
82ae531c26SArjan van de Ven }
83ae531c26SArjan van de Ven #else
page_is_allowed(unsigned long pfn)84a4866aa8SKees Cook static inline int page_is_allowed(unsigned long pfn)
85a4866aa8SKees Cook {
86a4866aa8SKees Cook return 1;
87a4866aa8SKees Cook }
range_is_allowed(unsigned long pfn,unsigned long size)88e2beb3eaSVenki Pallipadi static inline int range_is_allowed(unsigned long pfn, unsigned long size)
89ae531c26SArjan van de Ven {
90ae531c26SArjan van de Ven return 1;
91ae531c26SArjan van de Ven }
92ae531c26SArjan van de Ven #endif
93ae531c26SArjan van de Ven
should_stop_iteration(void)948619e5bdSTetsuo Handa static inline bool should_stop_iteration(void)
958619e5bdSTetsuo Handa {
968619e5bdSTetsuo Handa if (need_resched())
978619e5bdSTetsuo Handa cond_resched();
98830a4e5cSJason A. Donenfeld return signal_pending(current);
998619e5bdSTetsuo Handa }
1008619e5bdSTetsuo Handa
1011da177e4SLinus Torvalds /*
1021da177e4SLinus Torvalds * This funcion reads the *physical* memory. The f_pos points directly to the
1031da177e4SLinus Torvalds * memory location.
1041da177e4SLinus Torvalds */
read_mem(struct file * file,char __user * buf,size_t count,loff_t * ppos)1051da177e4SLinus Torvalds static ssize_t read_mem(struct file *file, char __user *buf,
1061da177e4SLinus Torvalds size_t count, loff_t *ppos)
1071da177e4SLinus Torvalds {
1087e6735c3SCyril Chemparathy phys_addr_t p = *ppos;
1091da177e4SLinus Torvalds ssize_t read, sz;
1104707a341SThierry Reding void *ptr;
11122ec1a2aSKees Cook char *bounce;
11222ec1a2aSKees Cook int err;
1131da177e4SLinus Torvalds
11408d2d00bSPetr Tesarik if (p != *ppos)
11508d2d00bSPetr Tesarik return 0;
11608d2d00bSPetr Tesarik
117136939a2SBjorn Helgaas if (!valid_phys_addr_range(p, count))
1181da177e4SLinus Torvalds return -EFAULT;
1191da177e4SLinus Torvalds read = 0;
1201da177e4SLinus Torvalds #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
1211da177e4SLinus Torvalds /* we don't have page 0 mapped on sparc and m68k.. */
1221da177e4SLinus Torvalds if (p < PAGE_SIZE) {
1237fabadddSWu Fengguang sz = size_inside_page(p, count);
1241da177e4SLinus Torvalds if (sz > 0) {
1251da177e4SLinus Torvalds if (clear_user(buf, sz))
1261da177e4SLinus Torvalds return -EFAULT;
1271da177e4SLinus Torvalds buf += sz;
1281da177e4SLinus Torvalds p += sz;
1291da177e4SLinus Torvalds count -= sz;
1301da177e4SLinus Torvalds read += sz;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds #endif
1341da177e4SLinus Torvalds
13522ec1a2aSKees Cook bounce = kmalloc(PAGE_SIZE, GFP_KERNEL);
13622ec1a2aSKees Cook if (!bounce)
13722ec1a2aSKees Cook return -ENOMEM;
13822ec1a2aSKees Cook
1391da177e4SLinus Torvalds while (count > 0) {
140fa29e97bSWu Fengguang unsigned long remaining;
141b5b38200SKees Cook int allowed, probe;
142fa29e97bSWu Fengguang
143f222318eSWu Fengguang sz = size_inside_page(p, count);
1441da177e4SLinus Torvalds
14522ec1a2aSKees Cook err = -EPERM;
146a4866aa8SKees Cook allowed = page_is_allowed(p >> PAGE_SHIFT);
147a4866aa8SKees Cook if (!allowed)
14822ec1a2aSKees Cook goto failed;
14922ec1a2aSKees Cook
15022ec1a2aSKees Cook err = -EFAULT;
151a4866aa8SKees Cook if (allowed == 2) {
152a4866aa8SKees Cook /* Show zeros for restricted memory. */
153a4866aa8SKees Cook remaining = clear_user(buf, sz);
154a4866aa8SKees Cook } else {
1551da177e4SLinus Torvalds /*
156a4866aa8SKees Cook * On ia64 if a page has been mapped somewhere as
157a4866aa8SKees Cook * uncached, then it must also be accessed uncached
158a4866aa8SKees Cook * by the kernel or data corruption may occur.
1591da177e4SLinus Torvalds */
1601da177e4SLinus Torvalds ptr = xlate_dev_mem_ptr(p);
161e045fb2aSvenkatesh.pallipadi@intel.com if (!ptr)
16222ec1a2aSKees Cook goto failed;
163e045fb2aSvenkatesh.pallipadi@intel.com
164fe557319SChristoph Hellwig probe = copy_from_kernel_nofault(bounce, ptr, sz);
165e045fb2aSvenkatesh.pallipadi@intel.com unxlate_dev_mem_ptr(p, ptr);
166b5b38200SKees Cook if (probe)
16722ec1a2aSKees Cook goto failed;
16822ec1a2aSKees Cook
16922ec1a2aSKees Cook remaining = copy_to_user(buf, bounce, sz);
170a4866aa8SKees Cook }
171a4866aa8SKees Cook
172fa29e97bSWu Fengguang if (remaining)
17322ec1a2aSKees Cook goto failed;
174e045fb2aSvenkatesh.pallipadi@intel.com
1751da177e4SLinus Torvalds buf += sz;
1761da177e4SLinus Torvalds p += sz;
1771da177e4SLinus Torvalds count -= sz;
1781da177e4SLinus Torvalds read += sz;
1798619e5bdSTetsuo Handa if (should_stop_iteration())
1808619e5bdSTetsuo Handa break;
1811da177e4SLinus Torvalds }
18222ec1a2aSKees Cook kfree(bounce);
1831da177e4SLinus Torvalds
1841da177e4SLinus Torvalds *ppos += read;
1851da177e4SLinus Torvalds return read;
18622ec1a2aSKees Cook
18722ec1a2aSKees Cook failed:
18822ec1a2aSKees Cook kfree(bounce);
18922ec1a2aSKees Cook return err;
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds
write_mem(struct file * file,const char __user * buf,size_t count,loff_t * ppos)1921da177e4SLinus Torvalds static ssize_t write_mem(struct file *file, const char __user *buf,
1931da177e4SLinus Torvalds size_t count, loff_t *ppos)
1941da177e4SLinus Torvalds {
1957e6735c3SCyril Chemparathy phys_addr_t p = *ppos;
1961da177e4SLinus Torvalds ssize_t written, sz;
1971da177e4SLinus Torvalds unsigned long copied;
1981da177e4SLinus Torvalds void *ptr;
1991da177e4SLinus Torvalds
20008d2d00bSPetr Tesarik if (p != *ppos)
20108d2d00bSPetr Tesarik return -EFBIG;
20208d2d00bSPetr Tesarik
203136939a2SBjorn Helgaas if (!valid_phys_addr_range(p, count))
2041da177e4SLinus Torvalds return -EFAULT;
2051da177e4SLinus Torvalds
2061da177e4SLinus Torvalds written = 0;
2071da177e4SLinus Torvalds
2081da177e4SLinus Torvalds #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
2091da177e4SLinus Torvalds /* we don't have page 0 mapped on sparc and m68k.. */
2101da177e4SLinus Torvalds if (p < PAGE_SIZE) {
2117fabadddSWu Fengguang sz = size_inside_page(p, count);
2121da177e4SLinus Torvalds /* Hmm. Do something? */
2131da177e4SLinus Torvalds buf += sz;
2141da177e4SLinus Torvalds p += sz;
2151da177e4SLinus Torvalds count -= sz;
2161da177e4SLinus Torvalds written += sz;
2171da177e4SLinus Torvalds }
2181da177e4SLinus Torvalds #endif
2191da177e4SLinus Torvalds
2201da177e4SLinus Torvalds while (count > 0) {
221a4866aa8SKees Cook int allowed;
222a4866aa8SKees Cook
223f222318eSWu Fengguang sz = size_inside_page(p, count);
2241da177e4SLinus Torvalds
225a4866aa8SKees Cook allowed = page_is_allowed(p >> PAGE_SHIFT);
226a4866aa8SKees Cook if (!allowed)
227e045fb2aSvenkatesh.pallipadi@intel.com return -EPERM;
228e045fb2aSvenkatesh.pallipadi@intel.com
229a4866aa8SKees Cook /* Skip actual writing when a page is marked as restricted. */
230a4866aa8SKees Cook if (allowed == 1) {
2311da177e4SLinus Torvalds /*
232a4866aa8SKees Cook * On ia64 if a page has been mapped somewhere as
233a4866aa8SKees Cook * uncached, then it must also be accessed uncached
234a4866aa8SKees Cook * by the kernel or data corruption may occur.
2351da177e4SLinus Torvalds */
2361da177e4SLinus Torvalds ptr = xlate_dev_mem_ptr(p);
237e045fb2aSvenkatesh.pallipadi@intel.com if (!ptr) {
238c654d60eSJan Beulich if (written)
239c654d60eSJan Beulich break;
2401da177e4SLinus Torvalds return -EFAULT;
2411da177e4SLinus Torvalds }
242e045fb2aSvenkatesh.pallipadi@intel.com
243e045fb2aSvenkatesh.pallipadi@intel.com copied = copy_from_user(ptr, buf, sz);
244fa29e97bSWu Fengguang unxlate_dev_mem_ptr(p, ptr);
245e045fb2aSvenkatesh.pallipadi@intel.com if (copied) {
246e045fb2aSvenkatesh.pallipadi@intel.com written += sz - copied;
247e045fb2aSvenkatesh.pallipadi@intel.com if (written)
248e045fb2aSvenkatesh.pallipadi@intel.com break;
249e045fb2aSvenkatesh.pallipadi@intel.com return -EFAULT;
250e045fb2aSvenkatesh.pallipadi@intel.com }
251a4866aa8SKees Cook }
252e045fb2aSvenkatesh.pallipadi@intel.com
2531da177e4SLinus Torvalds buf += sz;
2541da177e4SLinus Torvalds p += sz;
2551da177e4SLinus Torvalds count -= sz;
2561da177e4SLinus Torvalds written += sz;
2578619e5bdSTetsuo Handa if (should_stop_iteration())
2588619e5bdSTetsuo Handa break;
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds
2611da177e4SLinus Torvalds *ppos += written;
2621da177e4SLinus Torvalds return written;
2631da177e4SLinus Torvalds }
2641da177e4SLinus Torvalds
phys_mem_access_prot_allowed(struct file * file,unsigned long pfn,unsigned long size,pgprot_t * vma_prot)265d7d4d849SAndrew Morton int __weak phys_mem_access_prot_allowed(struct file *file,
266f0970c13Svenkatesh.pallipadi@intel.com unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
267f0970c13Svenkatesh.pallipadi@intel.com {
268f0970c13Svenkatesh.pallipadi@intel.com return 1;
269f0970c13Svenkatesh.pallipadi@intel.com }
270f0970c13Svenkatesh.pallipadi@intel.com
27144ac8413SBjorn Helgaas #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
272d7d4d849SAndrew Morton
273d7d4d849SAndrew Morton /*
274d7d4d849SAndrew Morton * Architectures vary in how they handle caching for addresses
275d7d4d849SAndrew Morton * outside of main memory.
276d7d4d849SAndrew Morton *
277d7d4d849SAndrew Morton */
278ea56f411SDavid Howells #ifdef pgprot_noncached
uncached_access(struct file * file,phys_addr_t addr)2797e6735c3SCyril Chemparathy static int uncached_access(struct file *file, phys_addr_t addr)
280d7d4d849SAndrew Morton {
281d7d4d849SAndrew Morton #if defined(CONFIG_IA64)
282d7d4d849SAndrew Morton /*
283d7d4d849SAndrew Morton * On ia64, we ignore O_DSYNC because we cannot tolerate memory
284d7d4d849SAndrew Morton * attribute aliases.
285d7d4d849SAndrew Morton */
286d7d4d849SAndrew Morton return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
287d7d4d849SAndrew Morton #else
288d7d4d849SAndrew Morton /*
289d7d4d849SAndrew Morton * Accessing memory above the top the kernel knows about or through a
290d7d4d849SAndrew Morton * file pointer
291d7d4d849SAndrew Morton * that was marked O_DSYNC will be done non-cached.
292d7d4d849SAndrew Morton */
293d7d4d849SAndrew Morton if (file->f_flags & O_DSYNC)
294d7d4d849SAndrew Morton return 1;
295d7d4d849SAndrew Morton return addr >= __pa(high_memory);
296d7d4d849SAndrew Morton #endif
297d7d4d849SAndrew Morton }
298ea56f411SDavid Howells #endif
299d7d4d849SAndrew Morton
phys_mem_access_prot(struct file * file,unsigned long pfn,unsigned long size,pgprot_t vma_prot)30044ac8413SBjorn Helgaas static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
30144ac8413SBjorn Helgaas unsigned long size, pgprot_t vma_prot)
30244ac8413SBjorn Helgaas {
30344ac8413SBjorn Helgaas #ifdef pgprot_noncached
3047e6735c3SCyril Chemparathy phys_addr_t offset = pfn << PAGE_SHIFT;
30544ac8413SBjorn Helgaas
30644ac8413SBjorn Helgaas if (uncached_access(file, offset))
30744ac8413SBjorn Helgaas return pgprot_noncached(vma_prot);
30844ac8413SBjorn Helgaas #endif
30944ac8413SBjorn Helgaas return vma_prot;
31044ac8413SBjorn Helgaas }
31144ac8413SBjorn Helgaas #endif
31244ac8413SBjorn Helgaas
3135da6185bSDavid Howells #ifndef CONFIG_MMU
get_unmapped_area_mem(struct file * file,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)3145da6185bSDavid Howells static unsigned long get_unmapped_area_mem(struct file *file,
3155da6185bSDavid Howells unsigned long addr,
3165da6185bSDavid Howells unsigned long len,
3175da6185bSDavid Howells unsigned long pgoff,
3185da6185bSDavid Howells unsigned long flags)
3195da6185bSDavid Howells {
3205da6185bSDavid Howells if (!valid_mmap_phys_addr_range(pgoff, len))
3215da6185bSDavid Howells return (unsigned long) -EINVAL;
3228a93258cSBenjamin Herrenschmidt return pgoff << PAGE_SHIFT;
3235da6185bSDavid Howells }
3245da6185bSDavid Howells
325b4caecd4SChristoph Hellwig /* permit direct mmap, for read, write or exec */
memory_mmap_capabilities(struct file * file)326b4caecd4SChristoph Hellwig static unsigned memory_mmap_capabilities(struct file *file)
327b4caecd4SChristoph Hellwig {
328b4caecd4SChristoph Hellwig return NOMMU_MAP_DIRECT |
329b4caecd4SChristoph Hellwig NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC;
330b4caecd4SChristoph Hellwig }
331b4caecd4SChristoph Hellwig
zero_mmap_capabilities(struct file * file)332b4caecd4SChristoph Hellwig static unsigned zero_mmap_capabilities(struct file *file)
333b4caecd4SChristoph Hellwig {
334b4caecd4SChristoph Hellwig return NOMMU_MAP_COPY;
335b4caecd4SChristoph Hellwig }
336b4caecd4SChristoph Hellwig
3375da6185bSDavid Howells /* can't do an in-place private mapping if there's no MMU */
private_mapping_ok(struct vm_area_struct * vma)3385da6185bSDavid Howells static inline int private_mapping_ok(struct vm_area_struct *vma)
3395da6185bSDavid Howells {
340fc4f4be9SDavid Hildenbrand return is_nommu_shared_mapping(vma->vm_flags);
3415da6185bSDavid Howells }
3425da6185bSDavid Howells #else
3435da6185bSDavid Howells
private_mapping_ok(struct vm_area_struct * vma)3445da6185bSDavid Howells static inline int private_mapping_ok(struct vm_area_struct *vma)
3455da6185bSDavid Howells {
3465da6185bSDavid Howells return 1;
3475da6185bSDavid Howells }
3485da6185bSDavid Howells #endif
3495da6185bSDavid Howells
350f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct mmap_mem_ops = {
3517ae8ed50SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT
3527ae8ed50SRik van Riel .access = generic_access_phys
3537ae8ed50SRik van Riel #endif
354e7f260a2Svenkatesh.pallipadi@intel.com };
355e7f260a2Svenkatesh.pallipadi@intel.com
mmap_mem(struct file * file,struct vm_area_struct * vma)3561da177e4SLinus Torvalds static int mmap_mem(struct file *file, struct vm_area_struct *vma)
3571da177e4SLinus Torvalds {
35880851ef2SBjorn Helgaas size_t size = vma->vm_end - vma->vm_start;
359b299cde2SJulius Werner phys_addr_t offset = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
360b299cde2SJulius Werner
361be62a320SCraig Bergstrom /* Does it even fit in phys_addr_t? */
362be62a320SCraig Bergstrom if (offset >> PAGE_SHIFT != vma->vm_pgoff)
363be62a320SCraig Bergstrom return -EINVAL;
364be62a320SCraig Bergstrom
365b299cde2SJulius Werner /* It's illegal to wrap around the end of the physical address space. */
36632829da5SJulius Werner if (offset + (phys_addr_t)size - 1 < offset)
367b299cde2SJulius Werner return -EINVAL;
36880851ef2SBjorn Helgaas
36906c67befSLennert Buytenhek if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
37080851ef2SBjorn Helgaas return -EINVAL;
37180851ef2SBjorn Helgaas
3725da6185bSDavid Howells if (!private_mapping_ok(vma))
3735da6185bSDavid Howells return -ENOSYS;
3745da6185bSDavid Howells
375e2beb3eaSVenki Pallipadi if (!range_is_allowed(vma->vm_pgoff, size))
376e2beb3eaSVenki Pallipadi return -EPERM;
377e2beb3eaSVenki Pallipadi
378f0970c13Svenkatesh.pallipadi@intel.com if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
379f0970c13Svenkatesh.pallipadi@intel.com &vma->vm_page_prot))
380f0970c13Svenkatesh.pallipadi@intel.com return -EINVAL;
381f0970c13Svenkatesh.pallipadi@intel.com
3828b150478SRoland Dreier vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
38380851ef2SBjorn Helgaas size,
3841da177e4SLinus Torvalds vma->vm_page_prot);
3851da177e4SLinus Torvalds
386e7f260a2Svenkatesh.pallipadi@intel.com vma->vm_ops = &mmap_mem_ops;
387e7f260a2Svenkatesh.pallipadi@intel.com
388314e51b9SKonstantin Khlebnikov /* Remap-pfn-range will mark the range VM_IO */
3891da177e4SLinus Torvalds if (remap_pfn_range(vma,
3901da177e4SLinus Torvalds vma->vm_start,
3911da177e4SLinus Torvalds vma->vm_pgoff,
39280851ef2SBjorn Helgaas size,
393e7f260a2Svenkatesh.pallipadi@intel.com vma->vm_page_prot)) {
3941da177e4SLinus Torvalds return -EAGAIN;
395e7f260a2Svenkatesh.pallipadi@intel.com }
3961da177e4SLinus Torvalds return 0;
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds
read_port(struct file * file,char __user * buf,size_t count,loff_t * ppos)3991da177e4SLinus Torvalds static ssize_t read_port(struct file *file, char __user *buf,
4001da177e4SLinus Torvalds size_t count, loff_t *ppos)
4011da177e4SLinus Torvalds {
4021da177e4SLinus Torvalds unsigned long i = *ppos;
4031da177e4SLinus Torvalds char __user *tmp = buf;
4041da177e4SLinus Torvalds
40596d4f267SLinus Torvalds if (!access_ok(buf, count))
4061da177e4SLinus Torvalds return -EFAULT;
4071da177e4SLinus Torvalds while (count-- > 0 && i < 65536) {
4081da177e4SLinus Torvalds if (__put_user(inb(i), tmp) < 0)
4091da177e4SLinus Torvalds return -EFAULT;
4101da177e4SLinus Torvalds i++;
4111da177e4SLinus Torvalds tmp++;
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds *ppos = i;
4141da177e4SLinus Torvalds return tmp-buf;
4151da177e4SLinus Torvalds }
4161da177e4SLinus Torvalds
write_port(struct file * file,const char __user * buf,size_t count,loff_t * ppos)4171da177e4SLinus Torvalds static ssize_t write_port(struct file *file, const char __user *buf,
4181da177e4SLinus Torvalds size_t count, loff_t *ppos)
4191da177e4SLinus Torvalds {
4201da177e4SLinus Torvalds unsigned long i = *ppos;
4211da177e4SLinus Torvalds const char __user *tmp = buf;
4221da177e4SLinus Torvalds
42396d4f267SLinus Torvalds if (!access_ok(buf, count))
4241da177e4SLinus Torvalds return -EFAULT;
4251da177e4SLinus Torvalds while (count-- > 0 && i < 65536) {
4261da177e4SLinus Torvalds char c;
4276a0061baSRob Ward
428c654d60eSJan Beulich if (__get_user(c, tmp)) {
429c654d60eSJan Beulich if (tmp > buf)
430c654d60eSJan Beulich break;
4311da177e4SLinus Torvalds return -EFAULT;
432c654d60eSJan Beulich }
4331da177e4SLinus Torvalds outb(c, i);
4341da177e4SLinus Torvalds i++;
4351da177e4SLinus Torvalds tmp++;
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds *ppos = i;
4381da177e4SLinus Torvalds return tmp-buf;
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds
read_null(struct file * file,char __user * buf,size_t count,loff_t * ppos)4411da177e4SLinus Torvalds static ssize_t read_null(struct file *file, char __user *buf,
4421da177e4SLinus Torvalds size_t count, loff_t *ppos)
4431da177e4SLinus Torvalds {
4441da177e4SLinus Torvalds return 0;
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds
write_null(struct file * file,const char __user * buf,size_t count,loff_t * ppos)4471da177e4SLinus Torvalds static ssize_t write_null(struct file *file, const char __user *buf,
4481da177e4SLinus Torvalds size_t count, loff_t *ppos)
4491da177e4SLinus Torvalds {
4501da177e4SLinus Torvalds return count;
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds
read_iter_null(struct kiocb * iocb,struct iov_iter * to)453cd28e28dSAl Viro static ssize_t read_iter_null(struct kiocb *iocb, struct iov_iter *to)
454162934deSZach Brown {
455162934deSZach Brown return 0;
456162934deSZach Brown }
457162934deSZach Brown
write_iter_null(struct kiocb * iocb,struct iov_iter * from)458cd28e28dSAl Viro static ssize_t write_iter_null(struct kiocb *iocb, struct iov_iter *from)
459162934deSZach Brown {
460cd28e28dSAl Viro size_t count = iov_iter_count(from);
461cd28e28dSAl Viro iov_iter_advance(from, count);
462cd28e28dSAl Viro return count;
463162934deSZach Brown }
464162934deSZach Brown
pipe_to_null(struct pipe_inode_info * info,struct pipe_buffer * buf,struct splice_desc * sd)4651ebd32fcSJens Axboe static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
4661ebd32fcSJens Axboe struct splice_desc *sd)
4671ebd32fcSJens Axboe {
4681ebd32fcSJens Axboe return sd->len;
4691ebd32fcSJens Axboe }
4701ebd32fcSJens Axboe
splice_write_null(struct pipe_inode_info * pipe,struct file * out,loff_t * ppos,size_t len,unsigned int flags)4711ebd32fcSJens Axboe static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
4721ebd32fcSJens Axboe loff_t *ppos, size_t len, unsigned int flags)
4731ebd32fcSJens Axboe {
4741ebd32fcSJens Axboe return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
4751ebd32fcSJens Axboe }
4761ebd32fcSJens Axboe
uring_cmd_null(struct io_uring_cmd * ioucmd,unsigned int issue_flags)47770752795SPaul Moore static int uring_cmd_null(struct io_uring_cmd *ioucmd, unsigned int issue_flags)
47870752795SPaul Moore {
47970752795SPaul Moore return 0;
48070752795SPaul Moore }
48170752795SPaul Moore
read_iter_zero(struct kiocb * iocb,struct iov_iter * iter)48213ba33e8SAl Viro static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
483162934deSZach Brown {
484162934deSZach Brown size_t written = 0;
485162934deSZach Brown
48613ba33e8SAl Viro while (iov_iter_count(iter)) {
48713ba33e8SAl Viro size_t chunk = iov_iter_count(iter), n;
4886a0061baSRob Ward
48913ba33e8SAl Viro if (chunk > PAGE_SIZE)
49013ba33e8SAl Viro chunk = PAGE_SIZE; /* Just for latency reasons */
49113ba33e8SAl Viro n = iov_iter_zero(chunk, iter);
49213ba33e8SAl Viro if (!n && iov_iter_count(iter))
493162934deSZach Brown return written ? written : -EFAULT;
49413ba33e8SAl Viro written += n;
49513ba33e8SAl Viro if (signal_pending(current))
49613ba33e8SAl Viro return written ? written : -ERESTARTSYS;
497e5f71d60SPavel Begunkov if (!need_resched())
498e5f71d60SPavel Begunkov continue;
499e5f71d60SPavel Begunkov if (iocb->ki_flags & IOCB_NOWAIT)
500e5f71d60SPavel Begunkov return written ? written : -EAGAIN;
50113ba33e8SAl Viro cond_resched();
50213ba33e8SAl Viro }
50313ba33e8SAl Viro return written;
504162934deSZach Brown }
505162934deSZach Brown
read_zero(struct file * file,char __user * buf,size_t count,loff_t * ppos)50699f66735SChristoph Hellwig static ssize_t read_zero(struct file *file, char __user *buf,
50799f66735SChristoph Hellwig size_t count, loff_t *ppos)
50899f66735SChristoph Hellwig {
50999f66735SChristoph Hellwig size_t cleared = 0;
51099f66735SChristoph Hellwig
51199f66735SChristoph Hellwig while (count) {
51299f66735SChristoph Hellwig size_t chunk = min_t(size_t, count, PAGE_SIZE);
513ab04de8eSChristoph Hellwig size_t left;
51499f66735SChristoph Hellwig
515ab04de8eSChristoph Hellwig left = clear_user(buf + cleared, chunk);
516ab04de8eSChristoph Hellwig if (unlikely(left)) {
517ab04de8eSChristoph Hellwig cleared += (chunk - left);
518ab04de8eSChristoph Hellwig if (!cleared)
519ab04de8eSChristoph Hellwig return -EFAULT;
520ab04de8eSChristoph Hellwig break;
521ab04de8eSChristoph Hellwig }
52299f66735SChristoph Hellwig cleared += chunk;
52399f66735SChristoph Hellwig count -= chunk;
52499f66735SChristoph Hellwig
52599f66735SChristoph Hellwig if (signal_pending(current))
526ab04de8eSChristoph Hellwig break;
52799f66735SChristoph Hellwig cond_resched();
52899f66735SChristoph Hellwig }
52999f66735SChristoph Hellwig
53099f66735SChristoph Hellwig return cleared;
53199f66735SChristoph Hellwig }
53299f66735SChristoph Hellwig
mmap_zero(struct file * file,struct vm_area_struct * vma)5331da177e4SLinus Torvalds static int mmap_zero(struct file *file, struct vm_area_struct *vma)
5341da177e4SLinus Torvalds {
535557ed1faSNick Piggin #ifndef CONFIG_MMU
536557ed1faSNick Piggin return -ENOSYS;
537557ed1faSNick Piggin #endif
5381da177e4SLinus Torvalds if (vma->vm_flags & VM_SHARED)
5391da177e4SLinus Torvalds return shmem_zero_setup(vma);
540bfd40eafSKirill A. Shutemov vma_set_anonymous(vma);
541557ed1faSNick Piggin return 0;
5421da177e4SLinus Torvalds }
5431da177e4SLinus Torvalds
get_unmapped_area_zero(struct file * file,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)544c01d5b30SHugh Dickins static unsigned long get_unmapped_area_zero(struct file *file,
545c01d5b30SHugh Dickins unsigned long addr, unsigned long len,
546c01d5b30SHugh Dickins unsigned long pgoff, unsigned long flags)
547c01d5b30SHugh Dickins {
548c01d5b30SHugh Dickins #ifdef CONFIG_MMU
549c01d5b30SHugh Dickins if (flags & MAP_SHARED) {
550c01d5b30SHugh Dickins /*
551c01d5b30SHugh Dickins * mmap_zero() will call shmem_zero_setup() to create a file,
552c01d5b30SHugh Dickins * so use shmem's get_unmapped_area in case it can be huge;
553c01d5b30SHugh Dickins * and pass NULL for file as in mmap.c's get_unmapped_area(),
554c01d5b30SHugh Dickins * so as not to confuse shmem with our handle on "/dev/zero".
555c01d5b30SHugh Dickins */
556c01d5b30SHugh Dickins return shmem_get_unmapped_area(NULL, addr, len, pgoff, flags);
557c01d5b30SHugh Dickins }
558c01d5b30SHugh Dickins
559c01d5b30SHugh Dickins /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
560c01d5b30SHugh Dickins return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
561c01d5b30SHugh Dickins #else
562c01d5b30SHugh Dickins return -ENOSYS;
563c01d5b30SHugh Dickins #endif
564c01d5b30SHugh Dickins }
565c01d5b30SHugh Dickins
write_full(struct file * file,const char __user * buf,size_t count,loff_t * ppos)5661da177e4SLinus Torvalds static ssize_t write_full(struct file *file, const char __user *buf,
5671da177e4SLinus Torvalds size_t count, loff_t *ppos)
5681da177e4SLinus Torvalds {
5691da177e4SLinus Torvalds return -ENOSPC;
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds
5721da177e4SLinus Torvalds /*
5731da177e4SLinus Torvalds * Special lseek() function for /dev/null and /dev/zero. Most notably, you
5741da177e4SLinus Torvalds * can fopen() both devices with "a" now. This was previously impossible.
5751da177e4SLinus Torvalds * -- SRB.
5761da177e4SLinus Torvalds */
null_lseek(struct file * file,loff_t offset,int orig)5771da177e4SLinus Torvalds static loff_t null_lseek(struct file *file, loff_t offset, int orig)
5781da177e4SLinus Torvalds {
5791da177e4SLinus Torvalds return file->f_pos = 0;
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds
5821da177e4SLinus Torvalds /*
5831da177e4SLinus Torvalds * The memory devices use the full 32/64 bits of the offset, and so we cannot
5841da177e4SLinus Torvalds * check against negative addresses: they are ok. The return value is weird,
5851da177e4SLinus Torvalds * though, in that case (0).
5861da177e4SLinus Torvalds *
5871da177e4SLinus Torvalds * also note that seeking relative to the "end of file" isn't supported:
5881da177e4SLinus Torvalds * it has no meaning, so it returns -EINVAL.
5891da177e4SLinus Torvalds */
memory_lseek(struct file * file,loff_t offset,int orig)5901da177e4SLinus Torvalds static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
5911da177e4SLinus Torvalds {
5921da177e4SLinus Torvalds loff_t ret;
5931da177e4SLinus Torvalds
5945955102cSAl Viro inode_lock(file_inode(file));
5951da177e4SLinus Torvalds switch (orig) {
596dcefafb6SWu Fengguang case SEEK_CUR:
597dcefafb6SWu Fengguang offset += file->f_pos;
598df561f66SGustavo A. R. Silva fallthrough;
599dcefafb6SWu Fengguang case SEEK_SET:
600dcefafb6SWu Fengguang /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
601ecb63a1bSAndrzej Hajda if ((unsigned long long)offset >= -MAX_ERRNO) {
602dcefafb6SWu Fengguang ret = -EOVERFLOW;
603dcefafb6SWu Fengguang break;
604dcefafb6SWu Fengguang }
605dcefafb6SWu Fengguang file->f_pos = offset;
6061da177e4SLinus Torvalds ret = file->f_pos;
6071da177e4SLinus Torvalds force_successful_syscall_return();
6081da177e4SLinus Torvalds break;
6091da177e4SLinus Torvalds default:
6101da177e4SLinus Torvalds ret = -EINVAL;
6111da177e4SLinus Torvalds }
6125955102cSAl Viro inode_unlock(file_inode(file));
6131da177e4SLinus Torvalds return ret;
6141da177e4SLinus Torvalds }
6151da177e4SLinus Torvalds
open_port(struct inode * inode,struct file * filp)6161da177e4SLinus Torvalds static int open_port(struct inode *inode, struct file *filp)
6171da177e4SLinus Torvalds {
6183234ac66SDan Williams int rc;
6193234ac66SDan Williams
6209b9d8ddaSMatthew Garrett if (!capable(CAP_SYS_RAWIO))
6219b9d8ddaSMatthew Garrett return -EPERM;
6229b9d8ddaSMatthew Garrett
6233234ac66SDan Williams rc = security_locked_down(LOCKDOWN_DEV_MEM);
6243234ac66SDan Williams if (rc)
6253234ac66SDan Williams return rc;
6263234ac66SDan Williams
6273234ac66SDan Williams if (iminor(inode) != DEVMEM_MINOR)
6283234ac66SDan Williams return 0;
6293234ac66SDan Williams
6303234ac66SDan Williams /*
6313234ac66SDan Williams * Use a unified address space to have a single point to manage
6323234ac66SDan Williams * revocations when drivers want to take over a /dev/mem mapped
6333234ac66SDan Williams * range.
6343234ac66SDan Williams */
63571a1d8edSDaniel Vetter filp->f_mapping = iomem_get_mapping();
6363234ac66SDan Williams
6373234ac66SDan Williams return 0;
6381da177e4SLinus Torvalds }
6391da177e4SLinus Torvalds
6401da177e4SLinus Torvalds #define zero_lseek null_lseek
6411da177e4SLinus Torvalds #define full_lseek null_lseek
6421da177e4SLinus Torvalds #define write_zero write_null
643cd28e28dSAl Viro #define write_iter_zero write_iter_null
6441da177e4SLinus Torvalds #define open_mem open_port
6451da177e4SLinus Torvalds
64673f0718eSRob Ward static const struct file_operations __maybe_unused mem_fops = {
6471da177e4SLinus Torvalds .llseek = memory_lseek,
6481da177e4SLinus Torvalds .read = read_mem,
6491da177e4SLinus Torvalds .write = write_mem,
6501da177e4SLinus Torvalds .mmap = mmap_mem,
6511da177e4SLinus Torvalds .open = open_mem,
652b4caecd4SChristoph Hellwig #ifndef CONFIG_MMU
6535da6185bSDavid Howells .get_unmapped_area = get_unmapped_area_mem,
654b4caecd4SChristoph Hellwig .mmap_capabilities = memory_mmap_capabilities,
655b4caecd4SChristoph Hellwig #endif
6561da177e4SLinus Torvalds };
6571da177e4SLinus Torvalds
65862322d25SArjan van de Ven static const struct file_operations null_fops = {
6591da177e4SLinus Torvalds .llseek = null_lseek,
6601da177e4SLinus Torvalds .read = read_null,
6611da177e4SLinus Torvalds .write = write_null,
662cd28e28dSAl Viro .read_iter = read_iter_null,
663cd28e28dSAl Viro .write_iter = write_iter_null,
6641ebd32fcSJens Axboe .splice_write = splice_write_null,
66570752795SPaul Moore .uring_cmd = uring_cmd_null,
6661da177e4SLinus Torvalds };
6671da177e4SLinus Torvalds
6683a4bc2fbSRob Ward static const struct file_operations __maybe_unused port_fops = {
6691da177e4SLinus Torvalds .llseek = memory_lseek,
6701da177e4SLinus Torvalds .read = read_port,
6711da177e4SLinus Torvalds .write = write_port,
6721da177e4SLinus Torvalds .open = open_port,
6731da177e4SLinus Torvalds };
6741da177e4SLinus Torvalds
67562322d25SArjan van de Ven static const struct file_operations zero_fops = {
6761da177e4SLinus Torvalds .llseek = zero_lseek,
6771da177e4SLinus Torvalds .write = write_zero,
67813ba33e8SAl Viro .read_iter = read_iter_zero,
67999f66735SChristoph Hellwig .read = read_zero,
680cd28e28dSAl Viro .write_iter = write_iter_zero,
6811da177e4SLinus Torvalds .mmap = mmap_zero,
682c01d5b30SHugh Dickins .get_unmapped_area = get_unmapped_area_zero,
683b4caecd4SChristoph Hellwig #ifndef CONFIG_MMU
684b4caecd4SChristoph Hellwig .mmap_capabilities = zero_mmap_capabilities,
685b4caecd4SChristoph Hellwig #endif
6861da177e4SLinus Torvalds };
6871da177e4SLinus Torvalds
68862322d25SArjan van de Ven static const struct file_operations full_fops = {
6891da177e4SLinus Torvalds .llseek = full_lseek,
69013ba33e8SAl Viro .read_iter = read_iter_zero,
6911da177e4SLinus Torvalds .write = write_full,
6921da177e4SLinus Torvalds };
6931da177e4SLinus Torvalds
694389e0cb9SKay Sievers static const struct memdev {
695389e0cb9SKay Sievers const char *name;
696d6f47befSAdriano dos Santos Fernandes const struct file_operations *fops;
697b4caecd4SChristoph Hellwig fmode_t fmode;
698ed1af26cSAlexey Dobriyan umode_t mode;
699389e0cb9SKay Sievers } devlist[] = {
70073f0718eSRob Ward #ifdef CONFIG_DEVMEM
701ed1af26cSAlexey Dobriyan [DEVMEM_MINOR] = { "mem", &mem_fops, FMODE_UNSIGNED_OFFSET, 0 },
70273f0718eSRob Ward #endif
703ed1af26cSAlexey Dobriyan [3] = { "null", &null_fops, FMODE_NOWAIT, 0666 },
704d6f47befSAdriano dos Santos Fernandes #ifdef CONFIG_DEVPORT
705ed1af26cSAlexey Dobriyan [4] = { "port", &port_fops, 0, 0 },
706d6f47befSAdriano dos Santos Fernandes #endif
707ed1af26cSAlexey Dobriyan [5] = { "zero", &zero_fops, FMODE_NOWAIT, 0666 },
708ed1af26cSAlexey Dobriyan [7] = { "full", &full_fops, 0, 0666 },
709ed1af26cSAlexey Dobriyan [8] = { "random", &random_fops, FMODE_NOWAIT, 0666 },
710ed1af26cSAlexey Dobriyan [9] = { "urandom", &urandom_fops, FMODE_NOWAIT, 0666 },
7117f3a781dSKay Sievers #ifdef CONFIG_PRINTK
712ed1af26cSAlexey Dobriyan [11] = { "kmsg", &kmsg_fops, 0, 0644 },
7137f3a781dSKay Sievers #endif
714d6f47befSAdriano dos Santos Fernandes };
715d6f47befSAdriano dos Santos Fernandes
memory_open(struct inode * inode,struct file * filp)7161da177e4SLinus Torvalds static int memory_open(struct inode *inode, struct file *filp)
7171da177e4SLinus Torvalds {
718389e0cb9SKay Sievers int minor;
719389e0cb9SKay Sievers const struct memdev *dev;
720d6f47befSAdriano dos Santos Fernandes
721389e0cb9SKay Sievers minor = iminor(inode);
722389e0cb9SKay Sievers if (minor >= ARRAY_SIZE(devlist))
723205153aaSFrederic Weisbecker return -ENXIO;
724d6f47befSAdriano dos Santos Fernandes
725389e0cb9SKay Sievers dev = &devlist[minor];
726389e0cb9SKay Sievers if (!dev->fops)
727205153aaSFrederic Weisbecker return -ENXIO;
728d6f47befSAdriano dos Santos Fernandes
729389e0cb9SKay Sievers filp->f_op = dev->fops;
730b4caecd4SChristoph Hellwig filp->f_mode |= dev->fmode;
7314a3956c7SKAMEZAWA Hiroyuki
732389e0cb9SKay Sievers if (dev->fops->open)
733205153aaSFrederic Weisbecker return dev->fops->open(inode, filp);
734205153aaSFrederic Weisbecker
735205153aaSFrederic Weisbecker return 0;
7361da177e4SLinus Torvalds }
7371da177e4SLinus Torvalds
73862322d25SArjan van de Ven static const struct file_operations memory_fops = {
739389e0cb9SKay Sievers .open = memory_open,
7406038f373SArnd Bergmann .llseek = noop_llseek,
7411da177e4SLinus Torvalds };
7421da177e4SLinus Torvalds
mem_devnode(const struct device * dev,umode_t * mode)743ff62b8e6SGreg Kroah-Hartman static char *mem_devnode(const struct device *dev, umode_t *mode)
744e454cea2SKay Sievers {
745e454cea2SKay Sievers if (mode && devlist[MINOR(dev->devt)].mode)
746e454cea2SKay Sievers *mode = devlist[MINOR(dev->devt)].mode;
747e454cea2SKay Sievers return NULL;
748e454cea2SKay Sievers }
749e454cea2SKay Sievers
750*7be022c9SCédric Le Goater #ifdef CONFIG_DEVMEM_BOOTPARAM
751*7be022c9SCédric Le Goater static bool devmem;
752*7be022c9SCédric Le Goater module_param(devmem, bool, 0444);
753*7be022c9SCédric Le Goater MODULE_PARM_DESC(devmem, "kernel parameter to activate /dev/mem");
754*7be022c9SCédric Le Goater #endif
755*7be022c9SCédric Le Goater
7567671284bSIvan Orlov static const struct class mem_class = {
7577671284bSIvan Orlov .name = "mem",
7587671284bSIvan Orlov .devnode = mem_devnode,
7597671284bSIvan Orlov };
7601da177e4SLinus Torvalds
chr_dev_init(void)7611da177e4SLinus Torvalds static int __init chr_dev_init(void)
7621da177e4SLinus Torvalds {
7637671284bSIvan Orlov int retval;
764389e0cb9SKay Sievers int minor;
7651da177e4SLinus Torvalds
7661da177e4SLinus Torvalds if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
7671da177e4SLinus Torvalds printk("unable to get major %d for memory devs\n", MEM_MAJOR);
7681da177e4SLinus Torvalds
7697671284bSIvan Orlov retval = class_register(&mem_class);
7707671284bSIvan Orlov if (retval)
7717671284bSIvan Orlov return retval;
7726e191f7bSAnton Blanchard
773389e0cb9SKay Sievers for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
774389e0cb9SKay Sievers if (!devlist[minor].name)
775389e0cb9SKay Sievers continue;
776e1612de9SHaren Myneni
777*7be022c9SCédric Le Goater #ifdef CONFIG_DEVMEM_BOOTPARAM
778*7be022c9SCédric Le Goater if (minor == DEVMEM_MINOR && !devmem)
779*7be022c9SCédric Le Goater continue;
780*7be022c9SCédric Le Goater #endif
781e1612de9SHaren Myneni /*
782e1612de9SHaren Myneni * Create /dev/port?
783e1612de9SHaren Myneni */
784e1612de9SHaren Myneni if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
785e1612de9SHaren Myneni continue;
786e1612de9SHaren Myneni
7877671284bSIvan Orlov device_create(&mem_class, NULL, MKDEV(MEM_MAJOR, minor),
788389e0cb9SKay Sievers NULL, devlist[minor].name);
789389e0cb9SKay Sievers }
7901da177e4SLinus Torvalds
79131d1d48eSDavid Howells return tty_init();
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds
7941da177e4SLinus Torvalds fs_initcall(chr_dev_init);
795