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