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 | * |
| 6 | * Added devfs support. |
| 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 | |
| 42 | if (-start & (PAGE_SIZE - 1)) |
| 43 | sz = -start & (PAGE_SIZE - 1); |
| 44 | else |
| 45 | sz = PAGE_SIZE; |
| 46 | |
| 47 | return min_t(unsigned long, sz, size); |
| 48 | } |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | /* |
| 51 | * Architectures vary in how they handle caching for addresses |
| 52 | * outside of main memory. |
| 53 | * |
| 54 | */ |
| 55 | static inline int uncached_access(struct file *file, unsigned long addr) |
| 56 | { |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 57 | #if defined(CONFIG_IA64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | /* |
Christoph Hellwig | 6b2f3d1 | 2009-10-27 11:05:28 +0100 | [diff] [blame] | 59 | * On ia64, we ignore O_DSYNC because we cannot tolerate memory attribute aliases. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | */ |
| 61 | return !(efi_mem_attributes(addr) & EFI_MEMORY_WB); |
Ralf Baechle | 24e9d0b96 | 2007-07-10 17:32:56 +0100 | [diff] [blame] | 62 | #elif defined(CONFIG_MIPS) |
| 63 | { |
| 64 | extern int __uncached_access(struct file *file, |
| 65 | unsigned long addr); |
| 66 | |
| 67 | return __uncached_access(file, addr); |
| 68 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | #else |
| 70 | /* |
| 71 | * Accessing memory above the top the kernel knows about or through a file pointer |
Christoph Hellwig | 6b2f3d1 | 2009-10-27 11:05:28 +0100 | [diff] [blame] | 72 | * that was marked O_DSYNC will be done non-cached. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | */ |
Christoph Hellwig | 6b2f3d1 | 2009-10-27 11:05:28 +0100 | [diff] [blame] | 74 | if (file->f_flags & O_DSYNC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | return 1; |
| 76 | return addr >= __pa(high_memory); |
| 77 | #endif |
| 78 | } |
| 79 | |
| 80 | #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE |
Bjorn Helgaas | 136939a | 2006-03-26 01:37:05 -0800 | [diff] [blame] | 81 | 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] | 82 | { |
Bjorn Helgaas | 136939a | 2006-03-26 01:37:05 -0800 | [diff] [blame] | 83 | if (addr + count > __pa(high_memory)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | return 0; |
| 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | return 1; |
| 87 | } |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 88 | |
Lennert Buytenhek | 06c67be | 2006-07-10 04:45:27 -0700 | [diff] [blame] | 89 | 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] | 90 | { |
| 91 | return 1; |
| 92 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | #endif |
| 94 | |
Ingo Molnar | d092633 | 2008-07-18 00:26:59 +0200 | [diff] [blame] | 95 | #ifdef CONFIG_STRICT_DEVMEM |
Venki Pallipadi | e2beb3e | 2008-03-06 23:01:47 -0800 | [diff] [blame] | 96 | 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] | 97 | { |
Venki Pallipadi | e2beb3e | 2008-03-06 23:01:47 -0800 | [diff] [blame] | 98 | u64 from = ((u64)pfn) << PAGE_SHIFT; |
| 99 | u64 to = from + size; |
| 100 | u64 cursor = from; |
Arjan van de Ven | ae531c2 | 2008-04-24 23:40:47 +0200 | [diff] [blame] | 101 | |
Venki Pallipadi | e2beb3e | 2008-03-06 23:01:47 -0800 | [diff] [blame] | 102 | while (cursor < to) { |
| 103 | if (!devmem_is_allowed(pfn)) { |
| 104 | printk(KERN_INFO |
| 105 | "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] | 106 | current->comm, from, to); |
| 107 | return 0; |
| 108 | } |
Venki Pallipadi | e2beb3e | 2008-03-06 23:01:47 -0800 | [diff] [blame] | 109 | cursor += PAGE_SIZE; |
| 110 | pfn++; |
Arjan van de Ven | ae531c2 | 2008-04-24 23:40:47 +0200 | [diff] [blame] | 111 | } |
| 112 | return 1; |
| 113 | } |
| 114 | #else |
Venki Pallipadi | e2beb3e | 2008-03-06 23:01:47 -0800 | [diff] [blame] | 115 | 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] | 116 | { |
| 117 | return 1; |
| 118 | } |
| 119 | #endif |
| 120 | |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 121 | void __attribute__((weak)) unxlate_dev_mem_ptr(unsigned long phys, void *addr) |
| 122 | { |
| 123 | } |
| 124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | /* |
| 126 | * This funcion reads the *physical* memory. The f_pos points directly to the |
| 127 | * memory location. |
| 128 | */ |
| 129 | static ssize_t read_mem(struct file * file, char __user * buf, |
| 130 | size_t count, loff_t *ppos) |
| 131 | { |
| 132 | unsigned long p = *ppos; |
| 133 | ssize_t read, sz; |
| 134 | char *ptr; |
| 135 | |
Bjorn Helgaas | 136939a | 2006-03-26 01:37:05 -0800 | [diff] [blame] | 136 | if (!valid_phys_addr_range(p, count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | return -EFAULT; |
| 138 | read = 0; |
| 139 | #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED |
| 140 | /* we don't have page 0 mapped on sparc and m68k.. */ |
| 141 | if (p < PAGE_SIZE) { |
| 142 | sz = PAGE_SIZE - p; |
| 143 | if (sz > count) |
| 144 | sz = count; |
| 145 | if (sz > 0) { |
| 146 | if (clear_user(buf, sz)) |
| 147 | return -EFAULT; |
| 148 | buf += sz; |
| 149 | p += sz; |
| 150 | count -= sz; |
| 151 | read += sz; |
| 152 | } |
| 153 | } |
| 154 | #endif |
| 155 | |
| 156 | while (count > 0) { |
Wu Fengguang | f222318 | 2009-12-14 17:58:07 -0800 | [diff] [blame^] | 157 | sz = size_inside_page(p, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 159 | if (!range_is_allowed(p >> PAGE_SHIFT, count)) |
| 160 | return -EPERM; |
| 161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | /* |
| 163 | * On ia64 if a page has been mapped somewhere as |
| 164 | * uncached, then it must also be accessed uncached |
| 165 | * by the kernel or data corruption may occur |
| 166 | */ |
| 167 | ptr = xlate_dev_mem_ptr(p); |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 168 | if (!ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | return -EFAULT; |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 170 | |
| 171 | if (copy_to_user(buf, ptr, sz)) { |
| 172 | unxlate_dev_mem_ptr(p, ptr); |
| 173 | return -EFAULT; |
| 174 | } |
| 175 | |
| 176 | unxlate_dev_mem_ptr(p, ptr); |
| 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | buf += sz; |
| 179 | p += sz; |
| 180 | count -= sz; |
| 181 | read += sz; |
| 182 | } |
| 183 | |
| 184 | *ppos += read; |
| 185 | return read; |
| 186 | } |
| 187 | |
| 188 | static ssize_t write_mem(struct file * file, const char __user * buf, |
| 189 | size_t count, loff_t *ppos) |
| 190 | { |
| 191 | unsigned long p = *ppos; |
| 192 | ssize_t written, sz; |
| 193 | unsigned long copied; |
| 194 | void *ptr; |
| 195 | |
Bjorn Helgaas | 136939a | 2006-03-26 01:37:05 -0800 | [diff] [blame] | 196 | if (!valid_phys_addr_range(p, count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | return -EFAULT; |
| 198 | |
| 199 | written = 0; |
| 200 | |
| 201 | #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED |
| 202 | /* we don't have page 0 mapped on sparc and m68k.. */ |
| 203 | if (p < PAGE_SIZE) { |
| 204 | unsigned long sz = PAGE_SIZE - p; |
| 205 | if (sz > count) |
| 206 | sz = count; |
| 207 | /* Hmm. Do something? */ |
| 208 | buf += sz; |
| 209 | p += sz; |
| 210 | count -= sz; |
| 211 | written += sz; |
| 212 | } |
| 213 | #endif |
| 214 | |
| 215 | while (count > 0) { |
Wu Fengguang | f222318 | 2009-12-14 17:58:07 -0800 | [diff] [blame^] | 216 | sz = size_inside_page(p, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 218 | if (!range_is_allowed(p >> PAGE_SHIFT, sz)) |
| 219 | return -EPERM; |
| 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | /* |
| 222 | * On ia64 if a page has been mapped somewhere as |
| 223 | * uncached, then it must also be accessed uncached |
| 224 | * by the kernel or data corruption may occur |
| 225 | */ |
| 226 | ptr = xlate_dev_mem_ptr(p); |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 227 | if (!ptr) { |
Jan Beulich | c654d60 | 2006-03-25 03:07:31 -0800 | [diff] [blame] | 228 | if (written) |
| 229 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | return -EFAULT; |
| 231 | } |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 232 | |
| 233 | copied = copy_from_user(ptr, buf, sz); |
| 234 | if (copied) { |
| 235 | written += sz - copied; |
| 236 | unxlate_dev_mem_ptr(p, ptr); |
| 237 | if (written) |
| 238 | break; |
| 239 | return -EFAULT; |
| 240 | } |
| 241 | |
| 242 | unxlate_dev_mem_ptr(p, ptr); |
| 243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | buf += sz; |
| 245 | p += sz; |
| 246 | count -= sz; |
| 247 | written += sz; |
| 248 | } |
| 249 | |
| 250 | *ppos += written; |
| 251 | return written; |
| 252 | } |
| 253 | |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 254 | int __attribute__((weak)) phys_mem_access_prot_allowed(struct file *file, |
| 255 | unsigned long pfn, unsigned long size, pgprot_t *vma_prot) |
| 256 | { |
| 257 | return 1; |
| 258 | } |
| 259 | |
Bjorn Helgaas | 44ac841 | 2006-01-08 01:04:10 -0800 | [diff] [blame] | 260 | #ifndef __HAVE_PHYS_MEM_ACCESS_PROT |
| 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 | f0f37e2f | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | static int mmap_mem(struct file * file, struct vm_area_struct * vma) |
| 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 | } |
| 339 | |
Arjan van de Ven | b781ecb | 2008-04-29 00:58:34 -0700 | [diff] [blame] | 340 | #ifdef CONFIG_DEVKMEM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | static int mmap_kmem(struct file * file, struct vm_area_struct * vma) |
| 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 | /* |
| 349 | * RED-PEN: on some architectures there is more mapped memory |
| 350 | * than available in mem_map which pfn_valid checks |
| 351 | * for. Perhaps should add a new macro here. |
| 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 | */ |
| 401 | static ssize_t read_kmem(struct file *file, char __user *buf, |
| 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 */ |
| 407 | |
| 408 | read = 0; |
| 409 | if (p < (unsigned long) high_memory) { |
| 410 | low_count = count; |
| 411 | if (count > (unsigned long) high_memory - p) |
| 412 | low_count = (unsigned long) high_memory - p; |
| 413 | |
| 414 | #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED |
| 415 | /* we don't have page 0 mapped on sparc and m68k.. */ |
| 416 | if (p < PAGE_SIZE && low_count > 0) { |
| 417 | size_t tmp = PAGE_SIZE - p; |
| 418 | if (tmp > low_count) tmp = low_count; |
| 419 | if (clear_user(buf, tmp)) |
| 420 | return -EFAULT; |
| 421 | buf += tmp; |
| 422 | p += tmp; |
| 423 | read += tmp; |
| 424 | low_count -= tmp; |
| 425 | count -= tmp; |
| 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 | f222318 | 2009-12-14 17:58:07 -0800 | [diff] [blame^] | 453 | int len = size_inside_page(p, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | len = vread(kbuf, (char *)p, len); |
| 456 | if (!len) |
| 457 | break; |
| 458 | if (copy_to_user(buf, kbuf, len)) { |
| 459 | free_page((unsigned long)kbuf); |
| 460 | return -EFAULT; |
| 461 | } |
| 462 | count -= len; |
| 463 | buf += len; |
| 464 | read += len; |
| 465 | p += len; |
| 466 | } |
| 467 | free_page((unsigned long)kbuf); |
| 468 | } |
| 469 | *ppos = p; |
| 470 | return read; |
| 471 | } |
| 472 | |
| 473 | |
| 474 | static inline ssize_t |
| 475 | do_write_kmem(void *p, unsigned long realp, const char __user * buf, |
| 476 | size_t count, loff_t *ppos) |
| 477 | { |
| 478 | ssize_t written, sz; |
| 479 | unsigned long copied; |
| 480 | |
| 481 | written = 0; |
| 482 | #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED |
| 483 | /* we don't have page 0 mapped on sparc and m68k.. */ |
| 484 | if (realp < PAGE_SIZE) { |
| 485 | unsigned long sz = PAGE_SIZE - realp; |
| 486 | if (sz > count) |
| 487 | sz = count; |
| 488 | /* Hmm. Do something? */ |
| 489 | buf += sz; |
| 490 | p += sz; |
| 491 | realp += sz; |
| 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 | f222318 | 2009-12-14 17:58:07 -0800 | [diff] [blame^] | 500 | sz = size_inside_page(realp, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
| 502 | /* |
| 503 | * On ia64 if a page has been mapped somewhere as |
| 504 | * uncached, then it must also be accessed uncached |
| 505 | * by the kernel or data corruption may occur |
| 506 | */ |
| 507 | ptr = xlate_dev_kmem_ptr(p); |
| 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; |
| 518 | realp += sz; |
| 519 | count -= sz; |
| 520 | written += sz; |
| 521 | } |
| 522 | |
| 523 | *ppos += written; |
| 524 | return written; |
| 525 | } |
| 526 | |
| 527 | |
| 528 | /* |
| 529 | * This function writes to the *virtual* memory as seen by the kernel. |
| 530 | */ |
| 531 | static ssize_t write_kmem(struct file * file, const char __user * buf, |
| 532 | size_t count, loff_t *ppos) |
| 533 | { |
| 534 | unsigned long p = *ppos; |
| 535 | ssize_t wrote = 0; |
| 536 | ssize_t virtr = 0; |
| 537 | ssize_t written; |
| 538 | char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */ |
| 539 | |
| 540 | if (p < (unsigned long) high_memory) { |
| 541 | |
| 542 | wrote = count; |
| 543 | if (count > (unsigned long) high_memory - p) |
| 544 | wrote = (unsigned long) high_memory - p; |
| 545 | |
| 546 | written = do_write_kmem((void*)p, p, buf, wrote, ppos); |
| 547 | if (written != wrote) |
| 548 | return written; |
| 549 | wrote = written; |
| 550 | p += wrote; |
| 551 | buf += wrote; |
| 552 | count -= wrote; |
| 553 | } |
| 554 | |
| 555 | if (count > 0) { |
| 556 | kbuf = (char *)__get_free_page(GFP_KERNEL); |
| 557 | if (!kbuf) |
| 558 | return wrote ? wrote : -ENOMEM; |
| 559 | while (count > 0) { |
Wu Fengguang | f222318 | 2009-12-14 17:58:07 -0800 | [diff] [blame^] | 560 | int len = size_inside_page(p, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
Wu Fengguang | 4ea2f43 | 2009-12-14 17:57:57 -0800 | [diff] [blame] | 562 | written = copy_from_user(kbuf, buf, len); |
| 563 | if (written) { |
| 564 | if (wrote + virtr) |
| 565 | break; |
| 566 | free_page((unsigned long)kbuf); |
| 567 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | } |
| 569 | len = vwrite(kbuf, (char *)p, len); |
| 570 | count -= len; |
| 571 | buf += len; |
| 572 | virtr += len; |
| 573 | p += len; |
| 574 | } |
| 575 | free_page((unsigned long)kbuf); |
| 576 | } |
| 577 | |
| 578 | *ppos = p; |
| 579 | return virtr + wrote; |
| 580 | } |
Arjan van de Ven | b781ecb | 2008-04-29 00:58:34 -0700 | [diff] [blame] | 581 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | |
Russell King | 4f911d6 | 2007-05-08 00:28:17 -0700 | [diff] [blame] | 583 | #ifdef CONFIG_DEVPORT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | static ssize_t read_port(struct file * file, char __user * buf, |
| 585 | size_t count, loff_t *ppos) |
| 586 | { |
| 587 | unsigned long i = *ppos; |
| 588 | char __user *tmp = buf; |
| 589 | |
| 590 | if (!access_ok(VERIFY_WRITE, buf, count)) |
| 591 | return -EFAULT; |
| 592 | while (count-- > 0 && i < 65536) { |
| 593 | if (__put_user(inb(i),tmp) < 0) |
| 594 | return -EFAULT; |
| 595 | i++; |
| 596 | tmp++; |
| 597 | } |
| 598 | *ppos = i; |
| 599 | return tmp-buf; |
| 600 | } |
| 601 | |
| 602 | static ssize_t write_port(struct file * file, const char __user * buf, |
| 603 | size_t count, loff_t *ppos) |
| 604 | { |
| 605 | unsigned long i = *ppos; |
| 606 | const char __user * tmp = buf; |
| 607 | |
| 608 | if (!access_ok(VERIFY_READ,buf,count)) |
| 609 | return -EFAULT; |
| 610 | while (count-- > 0 && i < 65536) { |
| 611 | char c; |
Jan Beulich | c654d60 | 2006-03-25 03:07:31 -0800 | [diff] [blame] | 612 | if (__get_user(c, tmp)) { |
| 613 | if (tmp > buf) |
| 614 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | return -EFAULT; |
Jan Beulich | c654d60 | 2006-03-25 03:07:31 -0800 | [diff] [blame] | 616 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | outb(c,i); |
| 618 | i++; |
| 619 | tmp++; |
| 620 | } |
| 621 | *ppos = i; |
| 622 | return tmp-buf; |
| 623 | } |
| 624 | #endif |
| 625 | |
| 626 | static ssize_t read_null(struct file * file, char __user * buf, |
| 627 | size_t count, loff_t *ppos) |
| 628 | { |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | static ssize_t write_null(struct file * file, const char __user * buf, |
| 633 | size_t count, loff_t *ppos) |
| 634 | { |
| 635 | return count; |
| 636 | } |
| 637 | |
Jens Axboe | 1ebd32f | 2006-04-26 14:40:08 +0200 | [diff] [blame] | 638 | static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf, |
| 639 | struct splice_desc *sd) |
| 640 | { |
| 641 | return sd->len; |
| 642 | } |
| 643 | |
| 644 | static ssize_t splice_write_null(struct pipe_inode_info *pipe,struct file *out, |
| 645 | loff_t *ppos, size_t len, unsigned int flags) |
| 646 | { |
| 647 | return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null); |
| 648 | } |
| 649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | static ssize_t read_zero(struct file * file, char __user * buf, |
| 651 | size_t count, loff_t *ppos) |
| 652 | { |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 653 | size_t written; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | |
| 655 | if (!count) |
| 656 | return 0; |
| 657 | |
| 658 | if (!access_ok(VERIFY_WRITE, buf, count)) |
| 659 | return -EFAULT; |
| 660 | |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 661 | written = 0; |
| 662 | while (count) { |
| 663 | unsigned long unwritten; |
| 664 | size_t chunk = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 666 | if (chunk > PAGE_SIZE) |
| 667 | chunk = PAGE_SIZE; /* Just for latency reasons */ |
Nikanth Karthikesan | bb521c5 | 2009-09-23 15:57:09 -0700 | [diff] [blame] | 668 | unwritten = __clear_user(buf, chunk); |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 669 | written += chunk - unwritten; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | if (unwritten) |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 671 | break; |
Linus Torvalds | 2b83868 | 2009-06-09 20:40:25 -0700 | [diff] [blame] | 672 | if (signal_pending(current)) |
| 673 | return written ? written : -ERESTARTSYS; |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 674 | buf += chunk; |
| 675 | count -= chunk; |
| 676 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | return written ? written : -EFAULT; |
| 679 | } |
| 680 | |
| 681 | static int mmap_zero(struct file * file, struct vm_area_struct * vma) |
| 682 | { |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 683 | #ifndef CONFIG_MMU |
| 684 | return -ENOSYS; |
| 685 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | if (vma->vm_flags & VM_SHARED) |
| 687 | return shmem_zero_setup(vma); |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 688 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | |
| 691 | static ssize_t write_full(struct file * file, const char __user * buf, |
| 692 | size_t count, loff_t *ppos) |
| 693 | { |
| 694 | return -ENOSPC; |
| 695 | } |
| 696 | |
| 697 | /* |
| 698 | * Special lseek() function for /dev/null and /dev/zero. Most notably, you |
| 699 | * can fopen() both devices with "a" now. This was previously impossible. |
| 700 | * -- SRB. |
| 701 | */ |
| 702 | |
| 703 | static loff_t null_lseek(struct file * file, loff_t offset, int orig) |
| 704 | { |
| 705 | return file->f_pos = 0; |
| 706 | } |
| 707 | |
| 708 | /* |
| 709 | * The memory devices use the full 32/64 bits of the offset, and so we cannot |
| 710 | * check against negative addresses: they are ok. The return value is weird, |
| 711 | * though, in that case (0). |
| 712 | * |
| 713 | * also note that seeking relative to the "end of file" isn't supported: |
| 714 | * it has no meaning, so it returns -EINVAL. |
| 715 | */ |
| 716 | static loff_t memory_lseek(struct file * file, loff_t offset, int orig) |
| 717 | { |
| 718 | loff_t ret; |
| 719 | |
Josef Sipek | a7113a9 | 2006-12-08 02:36:55 -0800 | [diff] [blame] | 720 | mutex_lock(&file->f_path.dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | switch (orig) { |
| 722 | case 0: |
| 723 | file->f_pos = offset; |
| 724 | ret = file->f_pos; |
| 725 | force_successful_syscall_return(); |
| 726 | break; |
| 727 | case 1: |
| 728 | file->f_pos += offset; |
| 729 | ret = file->f_pos; |
| 730 | force_successful_syscall_return(); |
| 731 | break; |
| 732 | default: |
| 733 | ret = -EINVAL; |
| 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 | |
| 739 | static int open_port(struct inode * inode, struct file * filp) |
| 740 | { |
| 741 | return capable(CAP_SYS_RAWIO) ? 0 : -EPERM; |
| 742 | } |
| 743 | |
| 744 | #define zero_lseek null_lseek |
| 745 | #define full_lseek null_lseek |
| 746 | #define write_zero write_null |
| 747 | #define read_full read_zero |
| 748 | #define open_mem open_port |
| 749 | #define open_kmem open_mem |
Vivek Goyal | 50b1fdb | 2005-06-25 14:58:23 -0700 | [diff] [blame] | 750 | #define open_oldmem open_mem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 752 | static const struct file_operations mem_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | .llseek = memory_lseek, |
| 754 | .read = read_mem, |
| 755 | .write = write_mem, |
| 756 | .mmap = mmap_mem, |
| 757 | .open = open_mem, |
David Howells | 5da6185 | 2006-09-27 01:50:16 -0700 | [diff] [blame] | 758 | .get_unmapped_area = get_unmapped_area_mem, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | }; |
| 760 | |
Arjan van de Ven | b781ecb | 2008-04-29 00:58:34 -0700 | [diff] [blame] | 761 | #ifdef CONFIG_DEVKMEM |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 762 | static const struct file_operations kmem_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | .llseek = memory_lseek, |
| 764 | .read = read_kmem, |
| 765 | .write = write_kmem, |
| 766 | .mmap = mmap_kmem, |
| 767 | .open = open_kmem, |
David Howells | 5da6185 | 2006-09-27 01:50:16 -0700 | [diff] [blame] | 768 | .get_unmapped_area = get_unmapped_area_mem, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | }; |
Arjan van de Ven | b781ecb | 2008-04-29 00:58:34 -0700 | [diff] [blame] | 770 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 772 | static const struct file_operations null_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | .llseek = null_lseek, |
| 774 | .read = read_null, |
| 775 | .write = write_null, |
Jens Axboe | 1ebd32f | 2006-04-26 14:40:08 +0200 | [diff] [blame] | 776 | .splice_write = splice_write_null, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | }; |
| 778 | |
Russell King | 4f911d6 | 2007-05-08 00:28:17 -0700 | [diff] [blame] | 779 | #ifdef CONFIG_DEVPORT |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 780 | static const struct file_operations port_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | .llseek = memory_lseek, |
| 782 | .read = read_port, |
| 783 | .write = write_port, |
| 784 | .open = open_port, |
| 785 | }; |
| 786 | #endif |
| 787 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 788 | static const struct file_operations zero_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | .llseek = zero_lseek, |
| 790 | .read = read_zero, |
| 791 | .write = write_zero, |
| 792 | .mmap = mmap_zero, |
| 793 | }; |
| 794 | |
David Howells | 5da6185 | 2006-09-27 01:50:16 -0700 | [diff] [blame] | 795 | /* |
| 796 | * capabilities for /dev/zero |
| 797 | * - permits private mappings, "copies" are taken of the source of zeros |
| 798 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | static struct backing_dev_info zero_bdi = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 800 | .name = "char/mem", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | .capabilities = BDI_CAP_MAP_COPY, |
| 802 | }; |
| 803 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 804 | static const struct file_operations full_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | .llseek = full_lseek, |
| 806 | .read = read_full, |
| 807 | .write = write_full, |
| 808 | }; |
| 809 | |
Vivek Goyal | 50b1fdb | 2005-06-25 14:58:23 -0700 | [diff] [blame] | 810 | #ifdef CONFIG_CRASH_DUMP |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 811 | static const struct file_operations oldmem_fops = { |
Vivek Goyal | 50b1fdb | 2005-06-25 14:58:23 -0700 | [diff] [blame] | 812 | .read = read_oldmem, |
| 813 | .open = open_oldmem, |
| 814 | }; |
| 815 | #endif |
| 816 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | static ssize_t kmsg_write(struct file * file, const char __user * buf, |
| 818 | size_t count, loff_t *ppos) |
| 819 | { |
| 820 | char *tmp; |
Guillaume Chazarain | cd140a5 | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 821 | ssize_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | |
| 823 | tmp = kmalloc(count + 1, GFP_KERNEL); |
| 824 | if (tmp == NULL) |
| 825 | return -ENOMEM; |
| 826 | ret = -EFAULT; |
| 827 | if (!copy_from_user(tmp, buf, count)) { |
| 828 | tmp[count] = 0; |
| 829 | ret = printk("%s", tmp); |
Guillaume Chazarain | cd140a5 | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 830 | if (ret > count) |
| 831 | /* printk can add a prefix */ |
| 832 | ret = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | } |
| 834 | kfree(tmp); |
| 835 | return ret; |
| 836 | } |
| 837 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 838 | static const struct file_operations kmsg_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | .write = kmsg_write, |
| 840 | }; |
| 841 | |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 842 | static const struct memdev { |
| 843 | const char *name; |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 844 | mode_t mode; |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 845 | const struct file_operations *fops; |
| 846 | struct backing_dev_info *dev_info; |
| 847 | } devlist[] = { |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 848 | [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi }, |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 849 | #ifdef CONFIG_DEVKMEM |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 850 | [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi }, |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 851 | #endif |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 852 | [3] = { "null", 0666, &null_fops, NULL }, |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 853 | #ifdef CONFIG_DEVPORT |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 854 | [4] = { "port", 0, &port_fops, NULL }, |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 855 | #endif |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 856 | [5] = { "zero", 0666, &zero_fops, &zero_bdi }, |
| 857 | [7] = { "full", 0666, &full_fops, NULL }, |
| 858 | [8] = { "random", 0666, &random_fops, NULL }, |
| 859 | [9] = { "urandom", 0666, &urandom_fops, NULL }, |
| 860 | [11] = { "kmsg", 0, &kmsg_fops, NULL }, |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 861 | #ifdef CONFIG_CRASH_DUMP |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 862 | [12] = { "oldmem", 0, &oldmem_fops, NULL }, |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 863 | #endif |
| 864 | }; |
| 865 | |
| 866 | static int memory_open(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | { |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 868 | int minor; |
| 869 | const struct memdev *dev; |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 870 | |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 871 | minor = iminor(inode); |
| 872 | if (minor >= ARRAY_SIZE(devlist)) |
Frederic Weisbecker | 205153a | 2009-10-09 20:31:02 +0200 | [diff] [blame] | 873 | return -ENXIO; |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 874 | |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 875 | dev = &devlist[minor]; |
| 876 | if (!dev->fops) |
Frederic Weisbecker | 205153a | 2009-10-09 20:31:02 +0200 | [diff] [blame] | 877 | return -ENXIO; |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 878 | |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 879 | filp->f_op = dev->fops; |
| 880 | if (dev->dev_info) |
| 881 | filp->f_mapping->backing_dev_info = dev->dev_info; |
| 882 | |
| 883 | if (dev->fops->open) |
Frederic Weisbecker | 205153a | 2009-10-09 20:31:02 +0200 | [diff] [blame] | 884 | return dev->fops->open(inode, filp); |
| 885 | |
| 886 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | } |
| 888 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 889 | static const struct file_operations memory_fops = { |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 890 | .open = memory_open, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | }; |
| 892 | |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 893 | static char *mem_devnode(struct device *dev, mode_t *mode) |
| 894 | { |
| 895 | if (mode && devlist[MINOR(dev->devt)].mode) |
| 896 | *mode = devlist[MINOR(dev->devt)].mode; |
| 897 | return NULL; |
| 898 | } |
| 899 | |
gregkh@suse.de | ca8eca6 | 2005-03-23 09:53:09 -0800 | [diff] [blame] | 900 | static struct class *mem_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | |
| 902 | static int __init chr_dev_init(void) |
| 903 | { |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 904 | int minor; |
Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 905 | int err; |
| 906 | |
| 907 | err = bdi_init(&zero_bdi); |
| 908 | if (err) |
| 909 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | |
| 911 | if (register_chrdev(MEM_MAJOR,"mem",&memory_fops)) |
| 912 | printk("unable to get major %d for memory devs\n", MEM_MAJOR); |
| 913 | |
gregkh@suse.de | ca8eca6 | 2005-03-23 09:53:09 -0800 | [diff] [blame] | 914 | mem_class = class_create(THIS_MODULE, "mem"); |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 915 | mem_class->devnode = mem_devnode; |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 916 | for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) { |
| 917 | if (!devlist[minor].name) |
| 918 | continue; |
| 919 | device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor), |
| 920 | NULL, devlist[minor].name); |
| 921 | } |
Greg Kroah-Hartman | ebf644c | 2006-07-25 17:13:31 -0700 | [diff] [blame] | 922 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | return 0; |
| 924 | } |
| 925 | |
| 926 | fs_initcall(chr_dev_init); |