Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/char/mem.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | * |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 6 | * Added devfs support. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu> |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 8 | * Shared /dev/zero mmapping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/ptrace.h> |
| 22 | #include <linux/device.h> |
Vivek Goyal | 50b1fdb | 2005-06-25 14:58:23 -0700 | [diff] [blame] | 23 | #include <linux/highmem.h> |
| 24 | #include <linux/crash_dump.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/backing-dev.h> |
Vivek Goyal | 315c215 | 2005-06-25 14:58:24 -0700 | [diff] [blame] | 26 | #include <linux/bootmem.h> |
Jens Axboe | d6b29d7 | 2007-06-04 09:59:47 +0200 | [diff] [blame] | 27 | #include <linux/splice.h> |
Linus Torvalds | b8a3ad5 | 2006-10-13 08:42:10 -0700 | [diff] [blame] | 28 | #include <linux/pfn.h> |
Paul Gortmaker | 66300e6 | 2011-07-10 12:14:53 -0400 | [diff] [blame] | 29 | #include <linux/export.h> |
Haren Myneni | e1612de | 2012-07-11 15:18:44 +1000 | [diff] [blame] | 30 | #include <linux/io.h> |
Kent Overstreet | a27bb33 | 2013-05-07 16:19:08 -0700 | [diff] [blame] | 31 | #include <linux/aio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | #ifdef CONFIG_IA64 |
| 36 | # include <linux/efi.h> |
| 37 | #endif |
| 38 | |
Haren Myneni | e1612de | 2012-07-11 15:18:44 +1000 | [diff] [blame] | 39 | #define DEVPORT_MINOR 4 |
| 40 | |
Wu Fengguang | f222318 | 2009-12-14 17:58:07 -0800 | [diff] [blame] | 41 | static inline unsigned long size_inside_page(unsigned long start, |
| 42 | unsigned long size) |
| 43 | { |
| 44 | unsigned long sz; |
| 45 | |
Wu Fengguang | 7fabadd | 2009-12-14 17:58:09 -0800 | [diff] [blame] | 46 | sz = PAGE_SIZE - (start & (PAGE_SIZE - 1)); |
Wu Fengguang | f222318 | 2009-12-14 17:58:07 -0800 | [diff] [blame] | 47 | |
Wu Fengguang | 7fabadd | 2009-12-14 17:58:09 -0800 | [diff] [blame] | 48 | return min(sz, size); |
Wu Fengguang | f222318 | 2009-12-14 17:58:07 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE |
Cyril Chemparathy | 7e6735c | 2012-09-12 14:05:58 -0400 | [diff] [blame] | 52 | static inline int valid_phys_addr_range(phys_addr_t addr, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | { |
Changli Gao | cfaf346c | 2011-03-23 16:42:58 -0700 | [diff] [blame] | 54 | return addr + count <= __pa(high_memory); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 56 | |
Lennert Buytenhek | 06c67be | 2006-07-10 04:45:27 -0700 | [diff] [blame] | 57 | static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size) |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 58 | { |
| 59 | return 1; |
| 60 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #endif |
| 62 | |
Ingo Molnar | d092633 | 2008-07-18 00:26:59 +0200 | [diff] [blame] | 63 | #ifdef CONFIG_STRICT_DEVMEM |
Venki Pallipadi | e2beb3e | 2008-03-06 23:01:47 -0800 | [diff] [blame] | 64 | static inline int range_is_allowed(unsigned long pfn, unsigned long size) |
Arjan van de Ven | ae531c2 | 2008-04-24 23:40:47 +0200 | [diff] [blame] | 65 | { |
Venki Pallipadi | e2beb3e | 2008-03-06 23:01:47 -0800 | [diff] [blame] | 66 | u64 from = ((u64)pfn) << PAGE_SHIFT; |
| 67 | u64 to = from + size; |
| 68 | u64 cursor = from; |
Arjan van de Ven | ae531c2 | 2008-04-24 23:40:47 +0200 | [diff] [blame] | 69 | |
Venki Pallipadi | e2beb3e | 2008-03-06 23:01:47 -0800 | [diff] [blame] | 70 | while (cursor < to) { |
| 71 | if (!devmem_is_allowed(pfn)) { |
| 72 | printk(KERN_INFO |
| 73 | "Program %s tried to access /dev/mem between %Lx->%Lx.\n", |
Arjan van de Ven | ae531c2 | 2008-04-24 23:40:47 +0200 | [diff] [blame] | 74 | current->comm, from, to); |
| 75 | return 0; |
| 76 | } |
Venki Pallipadi | e2beb3e | 2008-03-06 23:01:47 -0800 | [diff] [blame] | 77 | cursor += PAGE_SIZE; |
| 78 | pfn++; |
Arjan van de Ven | ae531c2 | 2008-04-24 23:40:47 +0200 | [diff] [blame] | 79 | } |
| 80 | return 1; |
| 81 | } |
| 82 | #else |
Venki Pallipadi | e2beb3e | 2008-03-06 23:01:47 -0800 | [diff] [blame] | 83 | static inline int range_is_allowed(unsigned long pfn, unsigned long size) |
Arjan van de Ven | ae531c2 | 2008-04-24 23:40:47 +0200 | [diff] [blame] | 84 | { |
| 85 | return 1; |
| 86 | } |
| 87 | #endif |
| 88 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 89 | void __weak unxlate_dev_mem_ptr(unsigned long phys, void *addr) |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 90 | { |
| 91 | } |
| 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | /* |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 94 | * This funcion reads the *physical* memory. The f_pos points directly to the |
| 95 | * memory location. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | */ |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 97 | static ssize_t read_mem(struct file *file, char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | size_t count, loff_t *ppos) |
| 99 | { |
Cyril Chemparathy | 7e6735c | 2012-09-12 14:05:58 -0400 | [diff] [blame] | 100 | phys_addr_t p = *ppos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | ssize_t read, sz; |
| 102 | char *ptr; |
| 103 | |
Bjorn Helgaas | 136939a | 2006-03-26 01:37:05 -0800 | [diff] [blame] | 104 | if (!valid_phys_addr_range(p, count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | return -EFAULT; |
| 106 | read = 0; |
| 107 | #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED |
| 108 | /* we don't have page 0 mapped on sparc and m68k.. */ |
| 109 | if (p < PAGE_SIZE) { |
Wu Fengguang | 7fabadd | 2009-12-14 17:58:09 -0800 | [diff] [blame] | 110 | sz = size_inside_page(p, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | if (sz > 0) { |
| 112 | if (clear_user(buf, sz)) |
| 113 | return -EFAULT; |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 114 | buf += sz; |
| 115 | p += sz; |
| 116 | count -= sz; |
| 117 | read += sz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | #endif |
| 121 | |
| 122 | while (count > 0) { |
Wu Fengguang | fa29e97 | 2009-12-14 17:58:08 -0800 | [diff] [blame] | 123 | unsigned long remaining; |
| 124 | |
Wu Fengguang | f222318 | 2009-12-14 17:58:07 -0800 | [diff] [blame] | 125 | sz = size_inside_page(p, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 127 | if (!range_is_allowed(p >> PAGE_SHIFT, count)) |
| 128 | return -EPERM; |
| 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | /* |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 131 | * On ia64 if a page has been mapped somewhere as uncached, then |
| 132 | * it must also be accessed uncached by the kernel or data |
| 133 | * corruption may occur. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | */ |
| 135 | ptr = xlate_dev_mem_ptr(p); |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 136 | if (!ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | return -EFAULT; |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 138 | |
Wu Fengguang | fa29e97 | 2009-12-14 17:58:08 -0800 | [diff] [blame] | 139 | remaining = copy_to_user(buf, ptr, sz); |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 140 | unxlate_dev_mem_ptr(p, ptr); |
Wu Fengguang | fa29e97 | 2009-12-14 17:58:08 -0800 | [diff] [blame] | 141 | if (remaining) |
| 142 | return -EFAULT; |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | buf += sz; |
| 145 | p += sz; |
| 146 | count -= sz; |
| 147 | read += sz; |
| 148 | } |
| 149 | |
| 150 | *ppos += read; |
| 151 | return read; |
| 152 | } |
| 153 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 154 | static ssize_t write_mem(struct file *file, const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | size_t count, loff_t *ppos) |
| 156 | { |
Cyril Chemparathy | 7e6735c | 2012-09-12 14:05:58 -0400 | [diff] [blame] | 157 | phys_addr_t p = *ppos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | ssize_t written, sz; |
| 159 | unsigned long copied; |
| 160 | void *ptr; |
| 161 | |
Bjorn Helgaas | 136939a | 2006-03-26 01:37:05 -0800 | [diff] [blame] | 162 | if (!valid_phys_addr_range(p, count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | return -EFAULT; |
| 164 | |
| 165 | written = 0; |
| 166 | |
| 167 | #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED |
| 168 | /* we don't have page 0 mapped on sparc and m68k.. */ |
| 169 | if (p < PAGE_SIZE) { |
Wu Fengguang | 7fabadd | 2009-12-14 17:58:09 -0800 | [diff] [blame] | 170 | sz = size_inside_page(p, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | /* Hmm. Do something? */ |
| 172 | buf += sz; |
| 173 | p += sz; |
| 174 | count -= sz; |
| 175 | written += sz; |
| 176 | } |
| 177 | #endif |
| 178 | |
| 179 | while (count > 0) { |
Wu Fengguang | f222318 | 2009-12-14 17:58:07 -0800 | [diff] [blame] | 180 | sz = size_inside_page(p, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 182 | if (!range_is_allowed(p >> PAGE_SHIFT, sz)) |
| 183 | return -EPERM; |
| 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | /* |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 186 | * On ia64 if a page has been mapped somewhere as uncached, then |
| 187 | * it must also be accessed uncached by the kernel or data |
| 188 | * corruption may occur. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | */ |
| 190 | ptr = xlate_dev_mem_ptr(p); |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 191 | if (!ptr) { |
Jan Beulich | c654d60 | 2006-03-25 03:07:31 -0800 | [diff] [blame] | 192 | if (written) |
| 193 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | return -EFAULT; |
| 195 | } |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 196 | |
| 197 | copied = copy_from_user(ptr, buf, sz); |
Wu Fengguang | fa29e97 | 2009-12-14 17:58:08 -0800 | [diff] [blame] | 198 | unxlate_dev_mem_ptr(p, ptr); |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 199 | if (copied) { |
| 200 | written += sz - copied; |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 201 | if (written) |
| 202 | break; |
| 203 | return -EFAULT; |
| 204 | } |
| 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | buf += sz; |
| 207 | p += sz; |
| 208 | count -= sz; |
| 209 | written += sz; |
| 210 | } |
| 211 | |
| 212 | *ppos += written; |
| 213 | return written; |
| 214 | } |
| 215 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 216 | int __weak phys_mem_access_prot_allowed(struct file *file, |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 217 | unsigned long pfn, unsigned long size, pgprot_t *vma_prot) |
| 218 | { |
| 219 | return 1; |
| 220 | } |
| 221 | |
Bjorn Helgaas | 44ac841 | 2006-01-08 01:04:10 -0800 | [diff] [blame] | 222 | #ifndef __HAVE_PHYS_MEM_ACCESS_PROT |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 223 | |
| 224 | /* |
| 225 | * Architectures vary in how they handle caching for addresses |
| 226 | * outside of main memory. |
| 227 | * |
| 228 | */ |
David Howells | ea56f41 | 2010-04-06 14:35:08 -0700 | [diff] [blame] | 229 | #ifdef pgprot_noncached |
Cyril Chemparathy | 7e6735c | 2012-09-12 14:05:58 -0400 | [diff] [blame] | 230 | static int uncached_access(struct file *file, phys_addr_t addr) |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 231 | { |
| 232 | #if defined(CONFIG_IA64) |
| 233 | /* |
| 234 | * On ia64, we ignore O_DSYNC because we cannot tolerate memory |
| 235 | * attribute aliases. |
| 236 | */ |
| 237 | return !(efi_mem_attributes(addr) & EFI_MEMORY_WB); |
| 238 | #elif defined(CONFIG_MIPS) |
| 239 | { |
| 240 | extern int __uncached_access(struct file *file, |
| 241 | unsigned long addr); |
| 242 | |
| 243 | return __uncached_access(file, addr); |
| 244 | } |
| 245 | #else |
| 246 | /* |
| 247 | * Accessing memory above the top the kernel knows about or through a |
| 248 | * file pointer |
| 249 | * that was marked O_DSYNC will be done non-cached. |
| 250 | */ |
| 251 | if (file->f_flags & O_DSYNC) |
| 252 | return 1; |
| 253 | return addr >= __pa(high_memory); |
| 254 | #endif |
| 255 | } |
David Howells | ea56f41 | 2010-04-06 14:35:08 -0700 | [diff] [blame] | 256 | #endif |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 257 | |
Bjorn Helgaas | 44ac841 | 2006-01-08 01:04:10 -0800 | [diff] [blame] | 258 | static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, |
| 259 | unsigned long size, pgprot_t vma_prot) |
| 260 | { |
| 261 | #ifdef pgprot_noncached |
Cyril Chemparathy | 7e6735c | 2012-09-12 14:05:58 -0400 | [diff] [blame] | 262 | phys_addr_t offset = pfn << PAGE_SHIFT; |
Bjorn Helgaas | 44ac841 | 2006-01-08 01:04:10 -0800 | [diff] [blame] | 263 | |
| 264 | if (uncached_access(file, offset)) |
| 265 | return pgprot_noncached(vma_prot); |
| 266 | #endif |
| 267 | return vma_prot; |
| 268 | } |
| 269 | #endif |
| 270 | |
David Howells | 5da6185 | 2006-09-27 01:50:16 -0700 | [diff] [blame] | 271 | #ifndef CONFIG_MMU |
| 272 | static unsigned long get_unmapped_area_mem(struct file *file, |
| 273 | unsigned long addr, |
| 274 | unsigned long len, |
| 275 | unsigned long pgoff, |
| 276 | unsigned long flags) |
| 277 | { |
| 278 | if (!valid_mmap_phys_addr_range(pgoff, len)) |
| 279 | return (unsigned long) -EINVAL; |
Benjamin Herrenschmidt | 8a93258 | 2007-04-16 22:53:16 -0700 | [diff] [blame] | 280 | return pgoff << PAGE_SHIFT; |
David Howells | 5da6185 | 2006-09-27 01:50:16 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /* can't do an in-place private mapping if there's no MMU */ |
| 284 | static inline int private_mapping_ok(struct vm_area_struct *vma) |
| 285 | { |
| 286 | return vma->vm_flags & VM_MAYSHARE; |
| 287 | } |
| 288 | #else |
| 289 | #define get_unmapped_area_mem NULL |
| 290 | |
| 291 | static inline int private_mapping_ok(struct vm_area_struct *vma) |
| 292 | { |
| 293 | return 1; |
| 294 | } |
| 295 | #endif |
| 296 | |
Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 297 | static const struct vm_operations_struct mmap_mem_ops = { |
Rik van Riel | 7ae8ed5 | 2008-07-23 21:27:07 -0700 | [diff] [blame] | 298 | #ifdef CONFIG_HAVE_IOREMAP_PROT |
| 299 | .access = generic_access_phys |
| 300 | #endif |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 301 | }; |
| 302 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 303 | static int mmap_mem(struct file *file, struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | { |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 305 | size_t size = vma->vm_end - vma->vm_start; |
| 306 | |
Lennert Buytenhek | 06c67be | 2006-07-10 04:45:27 -0700 | [diff] [blame] | 307 | if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size)) |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 308 | return -EINVAL; |
| 309 | |
David Howells | 5da6185 | 2006-09-27 01:50:16 -0700 | [diff] [blame] | 310 | if (!private_mapping_ok(vma)) |
| 311 | return -ENOSYS; |
| 312 | |
Venki Pallipadi | e2beb3e | 2008-03-06 23:01:47 -0800 | [diff] [blame] | 313 | if (!range_is_allowed(vma->vm_pgoff, size)) |
| 314 | return -EPERM; |
| 315 | |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 316 | if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size, |
| 317 | &vma->vm_page_prot)) |
| 318 | return -EINVAL; |
| 319 | |
Roland Dreier | 8b15047 | 2005-10-28 17:46:18 -0700 | [diff] [blame] | 320 | vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff, |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 321 | size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | vma->vm_page_prot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 324 | vma->vm_ops = &mmap_mem_ops; |
| 325 | |
Konstantin Khlebnikov | 314e51b | 2012-10-08 16:29:02 -0700 | [diff] [blame] | 326 | /* Remap-pfn-range will mark the range VM_IO */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | if (remap_pfn_range(vma, |
| 328 | vma->vm_start, |
| 329 | vma->vm_pgoff, |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 330 | size, |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 331 | vma->vm_page_prot)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | return -EAGAIN; |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 333 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | return 0; |
| 335 | } |
| 336 | |
Arjan van de Ven | b781ecb | 2008-04-29 00:58:34 -0700 | [diff] [blame] | 337 | #ifdef CONFIG_DEVKMEM |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 338 | static int mmap_kmem(struct file *file, struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | { |
Linus Torvalds | 4bb8255 | 2005-08-13 14:22:59 -0700 | [diff] [blame] | 340 | unsigned long pfn; |
| 341 | |
Linus Torvalds | 6d3154c | 2007-01-22 08:53:24 -0800 | [diff] [blame] | 342 | /* Turn a kernel-virtual address into a physical page frame */ |
| 343 | pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT; |
Linus Torvalds | 4bb8255 | 2005-08-13 14:22:59 -0700 | [diff] [blame] | 344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | /* |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 346 | * RED-PEN: on some architectures there is more mapped memory than |
| 347 | * available in mem_map which pfn_valid checks for. Perhaps should add a |
| 348 | * new macro here. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | * |
| 350 | * RED-PEN: vmalloc is not supported right now. |
| 351 | */ |
Linus Torvalds | 4bb8255 | 2005-08-13 14:22:59 -0700 | [diff] [blame] | 352 | if (!pfn_valid(pfn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | return -EIO; |
Linus Torvalds | 4bb8255 | 2005-08-13 14:22:59 -0700 | [diff] [blame] | 354 | |
| 355 | vma->vm_pgoff = pfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | return mmap_mem(file, vma); |
| 357 | } |
Arjan van de Ven | b781ecb | 2008-04-29 00:58:34 -0700 | [diff] [blame] | 358 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
Vivek Goyal | 50b1fdb | 2005-06-25 14:58:23 -0700 | [diff] [blame] | 360 | #ifdef CONFIG_CRASH_DUMP |
| 361 | /* |
| 362 | * Read memory corresponding to the old kernel. |
Vivek Goyal | 50b1fdb | 2005-06-25 14:58:23 -0700 | [diff] [blame] | 363 | */ |
Vivek Goyal | 315c215 | 2005-06-25 14:58:24 -0700 | [diff] [blame] | 364 | static ssize_t read_oldmem(struct file *file, char __user *buf, |
Vivek Goyal | 50b1fdb | 2005-06-25 14:58:23 -0700 | [diff] [blame] | 365 | size_t count, loff_t *ppos) |
| 366 | { |
Vivek Goyal | 315c215 | 2005-06-25 14:58:24 -0700 | [diff] [blame] | 367 | unsigned long pfn, offset; |
| 368 | size_t read = 0, csize; |
| 369 | int rc = 0; |
Vivek Goyal | 50b1fdb | 2005-06-25 14:58:23 -0700 | [diff] [blame] | 370 | |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 371 | while (count) { |
Vivek Goyal | 50b1fdb | 2005-06-25 14:58:23 -0700 | [diff] [blame] | 372 | pfn = *ppos / PAGE_SIZE; |
Vivek Goyal | 315c215 | 2005-06-25 14:58:24 -0700 | [diff] [blame] | 373 | if (pfn > saved_max_pfn) |
| 374 | return read; |
Vivek Goyal | 50b1fdb | 2005-06-25 14:58:23 -0700 | [diff] [blame] | 375 | |
Vivek Goyal | 315c215 | 2005-06-25 14:58:24 -0700 | [diff] [blame] | 376 | offset = (unsigned long)(*ppos % PAGE_SIZE); |
| 377 | if (count > PAGE_SIZE - offset) |
| 378 | csize = PAGE_SIZE - offset; |
| 379 | else |
| 380 | csize = count; |
Vivek Goyal | 50b1fdb | 2005-06-25 14:58:23 -0700 | [diff] [blame] | 381 | |
Vivek Goyal | 315c215 | 2005-06-25 14:58:24 -0700 | [diff] [blame] | 382 | rc = copy_oldmem_page(pfn, buf, csize, offset, 1); |
| 383 | if (rc < 0) |
| 384 | return rc; |
Vivek Goyal | 50b1fdb | 2005-06-25 14:58:23 -0700 | [diff] [blame] | 385 | buf += csize; |
| 386 | *ppos += csize; |
| 387 | read += csize; |
| 388 | count -= csize; |
| 389 | } |
Vivek Goyal | 50b1fdb | 2005-06-25 14:58:23 -0700 | [diff] [blame] | 390 | return read; |
| 391 | } |
| 392 | #endif |
| 393 | |
Arjan van de Ven | b781ecb | 2008-04-29 00:58:34 -0700 | [diff] [blame] | 394 | #ifdef CONFIG_DEVKMEM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | /* |
| 396 | * This function reads the *virtual* memory as seen by the kernel. |
| 397 | */ |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 398 | static ssize_t read_kmem(struct file *file, char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | size_t count, loff_t *ppos) |
| 400 | { |
| 401 | unsigned long p = *ppos; |
| 402 | ssize_t low_count, read, sz; |
Hans Grob | 890537b | 2013-02-06 11:37:20 +0100 | [diff] [blame] | 403 | char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */ |
KAMEZAWA Hiroyuki | 325fda7 | 2010-02-02 13:44:05 -0800 | [diff] [blame] | 404 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
| 406 | read = 0; |
| 407 | if (p < (unsigned long) high_memory) { |
| 408 | low_count = count; |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 409 | if (count > (unsigned long)high_memory - p) |
| 410 | low_count = (unsigned long)high_memory - p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
| 412 | #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED |
| 413 | /* we don't have page 0 mapped on sparc and m68k.. */ |
| 414 | if (p < PAGE_SIZE && low_count > 0) { |
Wu Fengguang | 7fabadd | 2009-12-14 17:58:09 -0800 | [diff] [blame] | 415 | sz = size_inside_page(p, low_count); |
| 416 | if (clear_user(buf, sz)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | return -EFAULT; |
Wu Fengguang | 7fabadd | 2009-12-14 17:58:09 -0800 | [diff] [blame] | 418 | buf += sz; |
| 419 | p += sz; |
| 420 | read += sz; |
| 421 | low_count -= sz; |
| 422 | count -= sz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | } |
| 424 | #endif |
| 425 | while (low_count > 0) { |
Wu Fengguang | f222318 | 2009-12-14 17:58:07 -0800 | [diff] [blame] | 426 | sz = size_inside_page(p, low_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
| 428 | /* |
| 429 | * On ia64 if a page has been mapped somewhere as |
| 430 | * uncached, then it must also be accessed uncached |
| 431 | * by the kernel or data corruption may occur |
| 432 | */ |
| 433 | kbuf = xlate_dev_kmem_ptr((char *)p); |
| 434 | |
| 435 | if (copy_to_user(buf, kbuf, sz)) |
| 436 | return -EFAULT; |
| 437 | buf += sz; |
| 438 | p += sz; |
| 439 | read += sz; |
| 440 | low_count -= sz; |
| 441 | count -= sz; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | if (count > 0) { |
| 446 | kbuf = (char *)__get_free_page(GFP_KERNEL); |
| 447 | if (!kbuf) |
| 448 | return -ENOMEM; |
| 449 | while (count > 0) { |
Wu Fengguang | 80ad89a | 2009-12-14 17:58:10 -0800 | [diff] [blame] | 450 | sz = size_inside_page(p, count); |
KAMEZAWA Hiroyuki | 325fda7 | 2010-02-02 13:44:05 -0800 | [diff] [blame] | 451 | if (!is_vmalloc_or_module_addr((void *)p)) { |
| 452 | err = -ENXIO; |
| 453 | break; |
| 454 | } |
Wu Fengguang | 80ad89a | 2009-12-14 17:58:10 -0800 | [diff] [blame] | 455 | sz = vread(kbuf, (char *)p, sz); |
| 456 | if (!sz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | break; |
Wu Fengguang | 80ad89a | 2009-12-14 17:58:10 -0800 | [diff] [blame] | 458 | if (copy_to_user(buf, kbuf, sz)) { |
KAMEZAWA Hiroyuki | 325fda7 | 2010-02-02 13:44:05 -0800 | [diff] [blame] | 459 | err = -EFAULT; |
| 460 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | } |
Wu Fengguang | 80ad89a | 2009-12-14 17:58:10 -0800 | [diff] [blame] | 462 | count -= sz; |
| 463 | buf += sz; |
| 464 | read += sz; |
| 465 | p += sz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } |
| 467 | free_page((unsigned long)kbuf); |
| 468 | } |
KAMEZAWA Hiroyuki | 325fda7 | 2010-02-02 13:44:05 -0800 | [diff] [blame] | 469 | *ppos = p; |
| 470 | return read ? read : err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 474 | static ssize_t do_write_kmem(unsigned long p, const char __user *buf, |
| 475 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | { |
| 477 | ssize_t written, sz; |
| 478 | unsigned long copied; |
| 479 | |
| 480 | written = 0; |
| 481 | #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED |
| 482 | /* we don't have page 0 mapped on sparc and m68k.. */ |
Wu Fengguang | ee32398 | 2009-12-14 17:58:10 -0800 | [diff] [blame] | 483 | if (p < PAGE_SIZE) { |
| 484 | sz = size_inside_page(p, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | /* Hmm. Do something? */ |
| 486 | buf += sz; |
| 487 | p += sz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | count -= sz; |
| 489 | written += sz; |
| 490 | } |
| 491 | #endif |
| 492 | |
| 493 | while (count > 0) { |
| 494 | char *ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
Wu Fengguang | ee32398 | 2009-12-14 17:58:10 -0800 | [diff] [blame] | 496 | sz = size_inside_page(p, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
| 498 | /* |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 499 | * On ia64 if a page has been mapped somewhere as uncached, then |
| 500 | * it must also be accessed uncached by the kernel or data |
| 501 | * corruption may occur. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | */ |
Wu Fengguang | ee32398 | 2009-12-14 17:58:10 -0800 | [diff] [blame] | 503 | ptr = xlate_dev_kmem_ptr((char *)p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | |
| 505 | copied = copy_from_user(ptr, buf, sz); |
| 506 | if (copied) { |
Jan Beulich | c654d60 | 2006-03-25 03:07:31 -0800 | [diff] [blame] | 507 | written += sz - copied; |
| 508 | if (written) |
| 509 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | return -EFAULT; |
| 511 | } |
| 512 | buf += sz; |
| 513 | p += sz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | count -= sz; |
| 515 | written += sz; |
| 516 | } |
| 517 | |
| 518 | *ppos += written; |
| 519 | return written; |
| 520 | } |
| 521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | /* |
| 523 | * This function writes to the *virtual* memory as seen by the kernel. |
| 524 | */ |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 525 | static ssize_t write_kmem(struct file *file, const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | size_t count, loff_t *ppos) |
| 527 | { |
| 528 | unsigned long p = *ppos; |
| 529 | ssize_t wrote = 0; |
| 530 | ssize_t virtr = 0; |
Hans Grob | 890537b | 2013-02-06 11:37:20 +0100 | [diff] [blame] | 531 | char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */ |
KAMEZAWA Hiroyuki | 325fda7 | 2010-02-02 13:44:05 -0800 | [diff] [blame] | 532 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | |
| 534 | if (p < (unsigned long) high_memory) { |
Wu Fengguang | 80ad89a | 2009-12-14 17:58:10 -0800 | [diff] [blame] | 535 | unsigned long to_write = min_t(unsigned long, count, |
| 536 | (unsigned long)high_memory - p); |
Wu Fengguang | ee32398 | 2009-12-14 17:58:10 -0800 | [diff] [blame] | 537 | wrote = do_write_kmem(p, buf, to_write, ppos); |
Wu Fengguang | 80ad89a | 2009-12-14 17:58:10 -0800 | [diff] [blame] | 538 | if (wrote != to_write) |
| 539 | return wrote; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | p += wrote; |
| 541 | buf += wrote; |
| 542 | count -= wrote; |
| 543 | } |
| 544 | |
| 545 | if (count > 0) { |
| 546 | kbuf = (char *)__get_free_page(GFP_KERNEL); |
| 547 | if (!kbuf) |
| 548 | return wrote ? wrote : -ENOMEM; |
| 549 | while (count > 0) { |
Wu Fengguang | 80ad89a | 2009-12-14 17:58:10 -0800 | [diff] [blame] | 550 | unsigned long sz = size_inside_page(p, count); |
| 551 | unsigned long n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
KAMEZAWA Hiroyuki | 325fda7 | 2010-02-02 13:44:05 -0800 | [diff] [blame] | 553 | if (!is_vmalloc_or_module_addr((void *)p)) { |
| 554 | err = -ENXIO; |
| 555 | break; |
| 556 | } |
Wu Fengguang | 80ad89a | 2009-12-14 17:58:10 -0800 | [diff] [blame] | 557 | n = copy_from_user(kbuf, buf, sz); |
| 558 | if (n) { |
KAMEZAWA Hiroyuki | 325fda7 | 2010-02-02 13:44:05 -0800 | [diff] [blame] | 559 | err = -EFAULT; |
| 560 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | } |
Wu Fengguang | c85e9a9 | 2010-02-02 13:44:06 -0800 | [diff] [blame] | 562 | vwrite(kbuf, (char *)p, sz); |
Wu Fengguang | 80ad89a | 2009-12-14 17:58:10 -0800 | [diff] [blame] | 563 | count -= sz; |
| 564 | buf += sz; |
| 565 | virtr += sz; |
| 566 | p += sz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | } |
| 568 | free_page((unsigned long)kbuf); |
| 569 | } |
| 570 | |
KAMEZAWA Hiroyuki | 325fda7 | 2010-02-02 13:44:05 -0800 | [diff] [blame] | 571 | *ppos = p; |
| 572 | return virtr + wrote ? : err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | } |
Arjan van de Ven | b781ecb | 2008-04-29 00:58:34 -0700 | [diff] [blame] | 574 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
Russell King | 4f911d6 | 2007-05-08 00:28:17 -0700 | [diff] [blame] | 576 | #ifdef CONFIG_DEVPORT |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 577 | static ssize_t read_port(struct file *file, char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | size_t count, loff_t *ppos) |
| 579 | { |
| 580 | unsigned long i = *ppos; |
| 581 | char __user *tmp = buf; |
| 582 | |
| 583 | if (!access_ok(VERIFY_WRITE, buf, count)) |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 584 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | while (count-- > 0 && i < 65536) { |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 586 | if (__put_user(inb(i), tmp) < 0) |
| 587 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | i++; |
| 589 | tmp++; |
| 590 | } |
| 591 | *ppos = i; |
| 592 | return tmp-buf; |
| 593 | } |
| 594 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 595 | static ssize_t write_port(struct file *file, const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | size_t count, loff_t *ppos) |
| 597 | { |
| 598 | unsigned long i = *ppos; |
Hans Grob | 890537b | 2013-02-06 11:37:20 +0100 | [diff] [blame] | 599 | const char __user *tmp = buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 601 | if (!access_ok(VERIFY_READ, buf, count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | return -EFAULT; |
| 603 | while (count-- > 0 && i < 65536) { |
| 604 | char c; |
Jan Beulich | c654d60 | 2006-03-25 03:07:31 -0800 | [diff] [blame] | 605 | if (__get_user(c, tmp)) { |
| 606 | if (tmp > buf) |
| 607 | break; |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 608 | return -EFAULT; |
Jan Beulich | c654d60 | 2006-03-25 03:07:31 -0800 | [diff] [blame] | 609 | } |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 610 | outb(c, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | i++; |
| 612 | tmp++; |
| 613 | } |
| 614 | *ppos = i; |
| 615 | return tmp-buf; |
| 616 | } |
| 617 | #endif |
| 618 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 619 | static ssize_t read_null(struct file *file, char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | size_t count, loff_t *ppos) |
| 621 | { |
| 622 | return 0; |
| 623 | } |
| 624 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 625 | static ssize_t write_null(struct file *file, const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | size_t count, loff_t *ppos) |
| 627 | { |
| 628 | return count; |
| 629 | } |
| 630 | |
Zach Brown | 162934d | 2013-05-07 16:18:27 -0700 | [diff] [blame] | 631 | static ssize_t aio_read_null(struct kiocb *iocb, const struct iovec *iov, |
| 632 | unsigned long nr_segs, loff_t pos) |
| 633 | { |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | static ssize_t aio_write_null(struct kiocb *iocb, const struct iovec *iov, |
| 638 | unsigned long nr_segs, loff_t pos) |
| 639 | { |
| 640 | return iov_length(iov, nr_segs); |
| 641 | } |
| 642 | |
Jens Axboe | 1ebd32f | 2006-04-26 14:40:08 +0200 | [diff] [blame] | 643 | static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf, |
| 644 | struct splice_desc *sd) |
| 645 | { |
| 646 | return sd->len; |
| 647 | } |
| 648 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 649 | static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out, |
Jens Axboe | 1ebd32f | 2006-04-26 14:40:08 +0200 | [diff] [blame] | 650 | loff_t *ppos, size_t len, unsigned int flags) |
| 651 | { |
| 652 | return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null); |
| 653 | } |
| 654 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 655 | static ssize_t read_zero(struct file *file, char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | size_t count, loff_t *ppos) |
| 657 | { |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 658 | size_t written; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
| 660 | if (!count) |
| 661 | return 0; |
| 662 | |
| 663 | if (!access_ok(VERIFY_WRITE, buf, count)) |
| 664 | return -EFAULT; |
| 665 | |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 666 | written = 0; |
| 667 | while (count) { |
| 668 | unsigned long unwritten; |
| 669 | size_t chunk = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 671 | if (chunk > PAGE_SIZE) |
| 672 | chunk = PAGE_SIZE; /* Just for latency reasons */ |
Nikanth Karthikesan | bb521c5 | 2009-09-23 15:57:09 -0700 | [diff] [blame] | 673 | unwritten = __clear_user(buf, chunk); |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 674 | written += chunk - unwritten; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | if (unwritten) |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 676 | break; |
Linus Torvalds | 2b83868 | 2009-06-09 20:40:25 -0700 | [diff] [blame] | 677 | if (signal_pending(current)) |
| 678 | return written ? written : -ERESTARTSYS; |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 679 | buf += chunk; |
| 680 | count -= chunk; |
| 681 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | return written ? written : -EFAULT; |
| 684 | } |
| 685 | |
Zach Brown | 162934d | 2013-05-07 16:18:27 -0700 | [diff] [blame] | 686 | static ssize_t aio_read_zero(struct kiocb *iocb, const struct iovec *iov, |
| 687 | unsigned long nr_segs, loff_t pos) |
| 688 | { |
| 689 | size_t written = 0; |
| 690 | unsigned long i; |
| 691 | ssize_t ret; |
| 692 | |
| 693 | for (i = 0; i < nr_segs; i++) { |
| 694 | ret = read_zero(iocb->ki_filp, iov[i].iov_base, iov[i].iov_len, |
| 695 | &pos); |
| 696 | if (ret < 0) |
| 697 | break; |
| 698 | written += ret; |
| 699 | } |
| 700 | |
| 701 | return written ? written : -EFAULT; |
| 702 | } |
| 703 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 704 | static int mmap_zero(struct file *file, struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | { |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 706 | #ifndef CONFIG_MMU |
| 707 | return -ENOSYS; |
| 708 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | if (vma->vm_flags & VM_SHARED) |
| 710 | return shmem_zero_setup(vma); |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 711 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 714 | static ssize_t write_full(struct file *file, const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | size_t count, loff_t *ppos) |
| 716 | { |
| 717 | return -ENOSPC; |
| 718 | } |
| 719 | |
| 720 | /* |
| 721 | * Special lseek() function for /dev/null and /dev/zero. Most notably, you |
| 722 | * can fopen() both devices with "a" now. This was previously impossible. |
| 723 | * -- SRB. |
| 724 | */ |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 725 | static loff_t null_lseek(struct file *file, loff_t offset, int orig) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | { |
| 727 | return file->f_pos = 0; |
| 728 | } |
| 729 | |
| 730 | /* |
| 731 | * The memory devices use the full 32/64 bits of the offset, and so we cannot |
| 732 | * check against negative addresses: they are ok. The return value is weird, |
| 733 | * though, in that case (0). |
| 734 | * |
| 735 | * also note that seeking relative to the "end of file" isn't supported: |
| 736 | * it has no meaning, so it returns -EINVAL. |
| 737 | */ |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 738 | static loff_t memory_lseek(struct file *file, loff_t offset, int orig) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | { |
| 740 | loff_t ret; |
| 741 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 742 | mutex_lock(&file_inode(file)->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | switch (orig) { |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 744 | case SEEK_CUR: |
| 745 | offset += file->f_pos; |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 746 | case SEEK_SET: |
| 747 | /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */ |
| 748 | if ((unsigned long long)offset >= ~0xFFFULL) { |
| 749 | ret = -EOVERFLOW; |
| 750 | break; |
| 751 | } |
| 752 | file->f_pos = offset; |
| 753 | ret = file->f_pos; |
| 754 | force_successful_syscall_return(); |
| 755 | break; |
| 756 | default: |
| 757 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | } |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 759 | mutex_unlock(&file_inode(file)->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | return ret; |
| 761 | } |
| 762 | |
Hans Grob | 890537b | 2013-02-06 11:37:20 +0100 | [diff] [blame] | 763 | static int open_port(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | { |
| 765 | return capable(CAP_SYS_RAWIO) ? 0 : -EPERM; |
| 766 | } |
| 767 | |
| 768 | #define zero_lseek null_lseek |
| 769 | #define full_lseek null_lseek |
| 770 | #define write_zero write_null |
| 771 | #define read_full read_zero |
Zach Brown | 162934d | 2013-05-07 16:18:27 -0700 | [diff] [blame] | 772 | #define aio_write_zero aio_write_null |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | #define open_mem open_port |
| 774 | #define open_kmem open_mem |
Vivek Goyal | 50b1fdb | 2005-06-25 14:58:23 -0700 | [diff] [blame] | 775 | #define open_oldmem open_mem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 777 | static const struct file_operations mem_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | .llseek = memory_lseek, |
| 779 | .read = read_mem, |
| 780 | .write = write_mem, |
| 781 | .mmap = mmap_mem, |
| 782 | .open = open_mem, |
David Howells | 5da6185 | 2006-09-27 01:50:16 -0700 | [diff] [blame] | 783 | .get_unmapped_area = get_unmapped_area_mem, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | }; |
| 785 | |
Arjan van de Ven | b781ecb | 2008-04-29 00:58:34 -0700 | [diff] [blame] | 786 | #ifdef CONFIG_DEVKMEM |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 787 | static const struct file_operations kmem_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | .llseek = memory_lseek, |
| 789 | .read = read_kmem, |
| 790 | .write = write_kmem, |
| 791 | .mmap = mmap_kmem, |
| 792 | .open = open_kmem, |
David Howells | 5da6185 | 2006-09-27 01:50:16 -0700 | [diff] [blame] | 793 | .get_unmapped_area = get_unmapped_area_mem, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | }; |
Arjan van de Ven | b781ecb | 2008-04-29 00:58:34 -0700 | [diff] [blame] | 795 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 797 | static const struct file_operations null_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | .llseek = null_lseek, |
| 799 | .read = read_null, |
| 800 | .write = write_null, |
Zach Brown | 162934d | 2013-05-07 16:18:27 -0700 | [diff] [blame] | 801 | .aio_read = aio_read_null, |
| 802 | .aio_write = aio_write_null, |
Jens Axboe | 1ebd32f | 2006-04-26 14:40:08 +0200 | [diff] [blame] | 803 | .splice_write = splice_write_null, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | }; |
| 805 | |
Russell King | 4f911d6 | 2007-05-08 00:28:17 -0700 | [diff] [blame] | 806 | #ifdef CONFIG_DEVPORT |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 807 | static const struct file_operations port_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | .llseek = memory_lseek, |
| 809 | .read = read_port, |
| 810 | .write = write_port, |
| 811 | .open = open_port, |
| 812 | }; |
| 813 | #endif |
| 814 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 815 | static const struct file_operations zero_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | .llseek = zero_lseek, |
| 817 | .read = read_zero, |
| 818 | .write = write_zero, |
Zach Brown | 162934d | 2013-05-07 16:18:27 -0700 | [diff] [blame] | 819 | .aio_read = aio_read_zero, |
| 820 | .aio_write = aio_write_zero, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | .mmap = mmap_zero, |
| 822 | }; |
| 823 | |
David Howells | 5da6185 | 2006-09-27 01:50:16 -0700 | [diff] [blame] | 824 | /* |
| 825 | * capabilities for /dev/zero |
| 826 | * - permits private mappings, "copies" are taken of the source of zeros |
Jan Kara | 371d217 | 2010-09-21 11:49:01 +0200 | [diff] [blame] | 827 | * - no writeback happens |
David Howells | 5da6185 | 2006-09-27 01:50:16 -0700 | [diff] [blame] | 828 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | static struct backing_dev_info zero_bdi = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 830 | .name = "char/mem", |
Jan Kara | 371d217 | 2010-09-21 11:49:01 +0200 | [diff] [blame] | 831 | .capabilities = BDI_CAP_MAP_COPY | BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | }; |
| 833 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 834 | static const struct file_operations full_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | .llseek = full_lseek, |
| 836 | .read = read_full, |
| 837 | .write = write_full, |
| 838 | }; |
| 839 | |
Vivek Goyal | 50b1fdb | 2005-06-25 14:58:23 -0700 | [diff] [blame] | 840 | #ifdef CONFIG_CRASH_DUMP |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 841 | static const struct file_operations oldmem_fops = { |
Vivek Goyal | 50b1fdb | 2005-06-25 14:58:23 -0700 | [diff] [blame] | 842 | .read = read_oldmem, |
| 843 | .open = open_oldmem, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 844 | .llseek = default_llseek, |
Vivek Goyal | 50b1fdb | 2005-06-25 14:58:23 -0700 | [diff] [blame] | 845 | }; |
| 846 | #endif |
| 847 | |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 848 | static const struct memdev { |
| 849 | const char *name; |
Al Viro | 2c9ede5 | 2011-07-23 20:24:48 -0400 | [diff] [blame] | 850 | umode_t mode; |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 851 | const struct file_operations *fops; |
| 852 | struct backing_dev_info *dev_info; |
| 853 | } devlist[] = { |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 854 | [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi }, |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 855 | #ifdef CONFIG_DEVKMEM |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 856 | [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi }, |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 857 | #endif |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 858 | [3] = { "null", 0666, &null_fops, NULL }, |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 859 | #ifdef CONFIG_DEVPORT |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 860 | [4] = { "port", 0, &port_fops, NULL }, |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 861 | #endif |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 862 | [5] = { "zero", 0666, &zero_fops, &zero_bdi }, |
| 863 | [7] = { "full", 0666, &full_fops, NULL }, |
| 864 | [8] = { "random", 0666, &random_fops, NULL }, |
| 865 | [9] = { "urandom", 0666, &urandom_fops, NULL }, |
Kay Sievers | 7f3a781 | 2012-05-09 01:37:51 +0200 | [diff] [blame] | 866 | #ifdef CONFIG_PRINTK |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 867 | [11] = { "kmsg", 0644, &kmsg_fops, NULL }, |
Kay Sievers | 7f3a781 | 2012-05-09 01:37:51 +0200 | [diff] [blame] | 868 | #endif |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 869 | #ifdef CONFIG_CRASH_DUMP |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 870 | [12] = { "oldmem", 0, &oldmem_fops, NULL }, |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 871 | #endif |
| 872 | }; |
| 873 | |
| 874 | static int memory_open(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | { |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 876 | int minor; |
| 877 | const struct memdev *dev; |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 878 | |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 879 | minor = iminor(inode); |
| 880 | if (minor >= ARRAY_SIZE(devlist)) |
Frederic Weisbecker | 205153a | 2009-10-09 20:31:02 +0200 | [diff] [blame] | 881 | return -ENXIO; |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 882 | |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 883 | dev = &devlist[minor]; |
| 884 | if (!dev->fops) |
Frederic Weisbecker | 205153a | 2009-10-09 20:31:02 +0200 | [diff] [blame] | 885 | return -ENXIO; |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 886 | |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 887 | filp->f_op = dev->fops; |
| 888 | if (dev->dev_info) |
| 889 | filp->f_mapping->backing_dev_info = dev->dev_info; |
| 890 | |
KAMEZAWA Hiroyuki | 4a3956c | 2010-10-01 14:20:22 -0700 | [diff] [blame] | 891 | /* Is /dev/mem or /dev/kmem ? */ |
| 892 | if (dev->dev_info == &directly_mappable_cdev_bdi) |
| 893 | filp->f_mode |= FMODE_UNSIGNED_OFFSET; |
| 894 | |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 895 | if (dev->fops->open) |
Frederic Weisbecker | 205153a | 2009-10-09 20:31:02 +0200 | [diff] [blame] | 896 | return dev->fops->open(inode, filp); |
| 897 | |
| 898 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | } |
| 900 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 901 | static const struct file_operations memory_fops = { |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 902 | .open = memory_open, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 903 | .llseek = noop_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | }; |
| 905 | |
Al Viro | 2c9ede5 | 2011-07-23 20:24:48 -0400 | [diff] [blame] | 906 | static char *mem_devnode(struct device *dev, umode_t *mode) |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 907 | { |
| 908 | if (mode && devlist[MINOR(dev->devt)].mode) |
| 909 | *mode = devlist[MINOR(dev->devt)].mode; |
| 910 | return NULL; |
| 911 | } |
| 912 | |
gregkh@suse.de | ca8eca6 | 2005-03-23 09:53:09 -0800 | [diff] [blame] | 913 | static struct class *mem_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | |
| 915 | static int __init chr_dev_init(void) |
| 916 | { |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 917 | int minor; |
Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 918 | int err; |
| 919 | |
| 920 | err = bdi_init(&zero_bdi); |
| 921 | if (err) |
| 922 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 924 | if (register_chrdev(MEM_MAJOR, "mem", &memory_fops)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | printk("unable to get major %d for memory devs\n", MEM_MAJOR); |
| 926 | |
gregkh@suse.de | ca8eca6 | 2005-03-23 09:53:09 -0800 | [diff] [blame] | 927 | mem_class = class_create(THIS_MODULE, "mem"); |
Anton Blanchard | 6e191f7 | 2010-04-06 14:34:55 -0700 | [diff] [blame] | 928 | if (IS_ERR(mem_class)) |
| 929 | return PTR_ERR(mem_class); |
| 930 | |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 931 | mem_class->devnode = mem_devnode; |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 932 | for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) { |
| 933 | if (!devlist[minor].name) |
| 934 | continue; |
Haren Myneni | e1612de | 2012-07-11 15:18:44 +1000 | [diff] [blame] | 935 | |
| 936 | /* |
Hans Grob | 890537b | 2013-02-06 11:37:20 +0100 | [diff] [blame] | 937 | * Create /dev/port? |
Haren Myneni | e1612de | 2012-07-11 15:18:44 +1000 | [diff] [blame] | 938 | */ |
| 939 | if ((minor == DEVPORT_MINOR) && !arch_has_dev_port()) |
| 940 | continue; |
| 941 | |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 942 | device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor), |
| 943 | NULL, devlist[minor].name); |
| 944 | } |
Greg Kroah-Hartman | ebf644c | 2006-07-25 17:13:31 -0700 | [diff] [blame] | 945 | |
David Howells | 31d1d48 | 2010-08-06 16:34:43 +0100 | [diff] [blame] | 946 | return tty_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | fs_initcall(chr_dev_init); |