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