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