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