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