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