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