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