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