Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/drivers/char/mem.c |
| 4 | * |
| 5 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 6 | * |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 7 | * Added devfs support. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu> |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 9 | * Shared /dev/zero mmapping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/mm.h> |
| 13 | #include <linux/miscdevice.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/vmalloc.h> |
| 16 | #include <linux/mman.h> |
| 17 | #include <linux/random.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/raw.h> |
| 20 | #include <linux/tty.h> |
| 21 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/ptrace.h> |
| 23 | #include <linux/device.h> |
Vivek Goyal | 50b1fdb | 2005-06-25 14:58:23 -0700 | [diff] [blame] | 24 | #include <linux/highmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/backing-dev.h> |
Hugh Dickins | c01d5b3 | 2016-07-26 15:26:15 -0700 | [diff] [blame] | 26 | #include <linux/shmem_fs.h> |
Jens Axboe | d6b29d7 | 2007-06-04 09:59:47 +0200 | [diff] [blame] | 27 | #include <linux/splice.h> |
Linus Torvalds | b8a3ad5 | 2006-10-13 08:42:10 -0700 | [diff] [blame] | 28 | #include <linux/pfn.h> |
Paul Gortmaker | 66300e6 | 2011-07-10 12:14:53 -0400 | [diff] [blame] | 29 | #include <linux/export.h> |
Haren Myneni | e1612de | 2012-07-11 15:18:44 +1000 | [diff] [blame] | 30 | #include <linux/io.h> |
Christoph Hellwig | e2e40f2 | 2015-02-22 08:58:50 -0800 | [diff] [blame] | 31 | #include <linux/uio.h> |
Rob Ward | 35b6c7e | 2014-12-20 18:28:35 +0000 | [diff] [blame] | 32 | #include <linux/uaccess.h> |
Matthew Garrett | 9b9d8dd | 2019-08-19 17:17:41 -0700 | [diff] [blame] | 33 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | #ifdef CONFIG_IA64 |
| 36 | # include <linux/efi.h> |
| 37 | #endif |
| 38 | |
Dan Williams | 3234ac6 | 2020-05-21 14:06:17 -0700 | [diff] [blame] | 39 | #define DEVMEM_MINOR 1 |
Haren Myneni | e1612de | 2012-07-11 15:18:44 +1000 | [diff] [blame] | 40 | #define DEVPORT_MINOR 4 |
| 41 | |
Wu Fengguang | f222318 | 2009-12-14 17:58:07 -0800 | [diff] [blame] | 42 | static inline unsigned long size_inside_page(unsigned long start, |
| 43 | unsigned long size) |
| 44 | { |
| 45 | unsigned long sz; |
| 46 | |
Wu Fengguang | 7fabadd | 2009-12-14 17:58:09 -0800 | [diff] [blame] | 47 | sz = PAGE_SIZE - (start & (PAGE_SIZE - 1)); |
Wu Fengguang | f222318 | 2009-12-14 17:58:07 -0800 | [diff] [blame] | 48 | |
Wu Fengguang | 7fabadd | 2009-12-14 17:58:09 -0800 | [diff] [blame] | 49 | return min(sz, size); |
Wu Fengguang | f222318 | 2009-12-14 17:58:07 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE |
Cyril Chemparathy | 7e6735c | 2012-09-12 14:05:58 -0400 | [diff] [blame] | 53 | 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] | 54 | { |
Changli Gao | cfaf346 | 2011-03-23 16:42:58 -0700 | [diff] [blame] | 55 | return addr + count <= __pa(high_memory); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 57 | |
Lennert Buytenhek | 06c67be | 2006-07-10 04:45:27 -0700 | [diff] [blame] | 58 | 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] | 59 | { |
| 60 | return 1; |
| 61 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | #endif |
| 63 | |
Ingo Molnar | d092633 | 2008-07-18 00:26:59 +0200 | [diff] [blame] | 64 | #ifdef CONFIG_STRICT_DEVMEM |
Kees Cook | a4866aa | 2017-04-05 09:39:08 -0700 | [diff] [blame] | 65 | static inline int page_is_allowed(unsigned long pfn) |
| 66 | { |
| 67 | return devmem_is_allowed(pfn); |
| 68 | } |
Venki Pallipadi | e2beb3e | 2008-03-06 23:01:47 -0800 | [diff] [blame] | 69 | 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] | 70 | { |
Venki Pallipadi | e2beb3e | 2008-03-06 23:01:47 -0800 | [diff] [blame] | 71 | u64 from = ((u64)pfn) << PAGE_SHIFT; |
| 72 | u64 to = from + size; |
| 73 | u64 cursor = from; |
Arjan van de Ven | ae531c2 | 2008-04-24 23:40:47 +0200 | [diff] [blame] | 74 | |
Venki Pallipadi | e2beb3e | 2008-03-06 23:01:47 -0800 | [diff] [blame] | 75 | while (cursor < to) { |
Jiri Kosina | 39380b8 | 2016-07-08 11:38:28 +0200 | [diff] [blame] | 76 | if (!devmem_is_allowed(pfn)) |
Arjan van de Ven | ae531c2 | 2008-04-24 23:40:47 +0200 | [diff] [blame] | 77 | return 0; |
Venki Pallipadi | e2beb3e | 2008-03-06 23:01:47 -0800 | [diff] [blame] | 78 | cursor += PAGE_SIZE; |
| 79 | pfn++; |
Arjan van de Ven | ae531c2 | 2008-04-24 23:40:47 +0200 | [diff] [blame] | 80 | } |
| 81 | return 1; |
| 82 | } |
| 83 | #else |
Kees Cook | a4866aa | 2017-04-05 09:39:08 -0700 | [diff] [blame] | 84 | static inline int page_is_allowed(unsigned long pfn) |
| 85 | { |
| 86 | return 1; |
| 87 | } |
Venki Pallipadi | e2beb3e | 2008-03-06 23:01:47 -0800 | [diff] [blame] | 88 | 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] | 89 | { |
| 90 | return 1; |
| 91 | } |
| 92 | #endif |
| 93 | |
Thierry Reding | 4707a34 | 2014-07-28 17:20:33 +0200 | [diff] [blame] | 94 | #ifndef unxlate_dev_mem_ptr |
| 95 | #define unxlate_dev_mem_ptr unxlate_dev_mem_ptr |
| 96 | 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] | 97 | { |
| 98 | } |
Thierry Reding | 4707a34 | 2014-07-28 17:20:33 +0200 | [diff] [blame] | 99 | #endif |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 100 | |
Tetsuo Handa | 8619e5b | 2019-08-26 22:13:25 +0900 | [diff] [blame] | 101 | static inline bool should_stop_iteration(void) |
| 102 | { |
| 103 | if (need_resched()) |
| 104 | cond_resched(); |
| 105 | return fatal_signal_pending(current); |
| 106 | } |
| 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | /* |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 109 | * This funcion reads the *physical* memory. The f_pos points directly to the |
| 110 | * memory location. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | */ |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 112 | static ssize_t read_mem(struct file *file, char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | size_t count, loff_t *ppos) |
| 114 | { |
Cyril Chemparathy | 7e6735c | 2012-09-12 14:05:58 -0400 | [diff] [blame] | 115 | phys_addr_t p = *ppos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | ssize_t read, sz; |
Thierry Reding | 4707a34 | 2014-07-28 17:20:33 +0200 | [diff] [blame] | 117 | void *ptr; |
Kees Cook | 22ec1a2 | 2017-12-01 13:19:39 -0800 | [diff] [blame] | 118 | char *bounce; |
| 119 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Petr Tesarik | 08d2d00 | 2014-01-30 09:48:02 +0100 | [diff] [blame] | 121 | if (p != *ppos) |
| 122 | return 0; |
| 123 | |
Bjorn Helgaas | 136939a | 2006-03-26 01:37:05 -0800 | [diff] [blame] | 124 | if (!valid_phys_addr_range(p, count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | return -EFAULT; |
| 126 | read = 0; |
| 127 | #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED |
| 128 | /* we don't have page 0 mapped on sparc and m68k.. */ |
| 129 | if (p < PAGE_SIZE) { |
Wu Fengguang | 7fabadd | 2009-12-14 17:58:09 -0800 | [diff] [blame] | 130 | sz = size_inside_page(p, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | if (sz > 0) { |
| 132 | if (clear_user(buf, sz)) |
| 133 | return -EFAULT; |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 134 | buf += sz; |
| 135 | p += sz; |
| 136 | count -= sz; |
| 137 | read += sz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | #endif |
| 141 | |
Kees Cook | 22ec1a2 | 2017-12-01 13:19:39 -0800 | [diff] [blame] | 142 | bounce = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 143 | if (!bounce) |
| 144 | return -ENOMEM; |
| 145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | while (count > 0) { |
Wu Fengguang | fa29e97 | 2009-12-14 17:58:08 -0800 | [diff] [blame] | 147 | unsigned long remaining; |
Kees Cook | b5b3820 | 2018-03-27 14:06:14 -0700 | [diff] [blame] | 148 | int allowed, probe; |
Wu Fengguang | fa29e97 | 2009-12-14 17:58:08 -0800 | [diff] [blame] | 149 | |
Wu Fengguang | f222318 | 2009-12-14 17:58:07 -0800 | [diff] [blame] | 150 | sz = size_inside_page(p, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Kees Cook | 22ec1a2 | 2017-12-01 13:19:39 -0800 | [diff] [blame] | 152 | err = -EPERM; |
Kees Cook | a4866aa | 2017-04-05 09:39:08 -0700 | [diff] [blame] | 153 | allowed = page_is_allowed(p >> PAGE_SHIFT); |
| 154 | if (!allowed) |
Kees Cook | 22ec1a2 | 2017-12-01 13:19:39 -0800 | [diff] [blame] | 155 | goto failed; |
| 156 | |
| 157 | err = -EFAULT; |
Kees Cook | a4866aa | 2017-04-05 09:39:08 -0700 | [diff] [blame] | 158 | if (allowed == 2) { |
| 159 | /* Show zeros for restricted memory. */ |
| 160 | remaining = clear_user(buf, sz); |
| 161 | } else { |
| 162 | /* |
| 163 | * On ia64 if a page has been mapped somewhere as |
| 164 | * uncached, then it must also be accessed uncached |
| 165 | * by the kernel or data corruption may occur. |
| 166 | */ |
| 167 | ptr = xlate_dev_mem_ptr(p); |
| 168 | if (!ptr) |
Kees Cook | 22ec1a2 | 2017-12-01 13:19:39 -0800 | [diff] [blame] | 169 | goto failed; |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 170 | |
Christoph Hellwig | fe55731 | 2020-06-17 09:37:53 +0200 | [diff] [blame] | 171 | probe = copy_from_kernel_nofault(bounce, ptr, sz); |
Kees Cook | a4866aa | 2017-04-05 09:39:08 -0700 | [diff] [blame] | 172 | unxlate_dev_mem_ptr(p, ptr); |
Kees Cook | b5b3820 | 2018-03-27 14:06:14 -0700 | [diff] [blame] | 173 | if (probe) |
Kees Cook | 22ec1a2 | 2017-12-01 13:19:39 -0800 | [diff] [blame] | 174 | goto failed; |
| 175 | |
| 176 | remaining = copy_to_user(buf, bounce, sz); |
Kees Cook | a4866aa | 2017-04-05 09:39:08 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Wu Fengguang | fa29e97 | 2009-12-14 17:58:08 -0800 | [diff] [blame] | 179 | if (remaining) |
Kees Cook | 22ec1a2 | 2017-12-01 13:19:39 -0800 | [diff] [blame] | 180 | goto failed; |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | buf += sz; |
| 183 | p += sz; |
| 184 | count -= sz; |
| 185 | read += sz; |
Tetsuo Handa | 8619e5b | 2019-08-26 22:13:25 +0900 | [diff] [blame] | 186 | if (should_stop_iteration()) |
| 187 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
Kees Cook | 22ec1a2 | 2017-12-01 13:19:39 -0800 | [diff] [blame] | 189 | kfree(bounce); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
| 191 | *ppos += read; |
| 192 | return read; |
Kees Cook | 22ec1a2 | 2017-12-01 13:19:39 -0800 | [diff] [blame] | 193 | |
| 194 | failed: |
| 195 | kfree(bounce); |
| 196 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 199 | static ssize_t write_mem(struct file *file, const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | size_t count, loff_t *ppos) |
| 201 | { |
Cyril Chemparathy | 7e6735c | 2012-09-12 14:05:58 -0400 | [diff] [blame] | 202 | phys_addr_t p = *ppos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | ssize_t written, sz; |
| 204 | unsigned long copied; |
| 205 | void *ptr; |
| 206 | |
Petr Tesarik | 08d2d00 | 2014-01-30 09:48:02 +0100 | [diff] [blame] | 207 | if (p != *ppos) |
| 208 | return -EFBIG; |
| 209 | |
Bjorn Helgaas | 136939a | 2006-03-26 01:37:05 -0800 | [diff] [blame] | 210 | if (!valid_phys_addr_range(p, count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | return -EFAULT; |
| 212 | |
| 213 | written = 0; |
| 214 | |
| 215 | #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED |
| 216 | /* we don't have page 0 mapped on sparc and m68k.. */ |
| 217 | if (p < PAGE_SIZE) { |
Wu Fengguang | 7fabadd | 2009-12-14 17:58:09 -0800 | [diff] [blame] | 218 | sz = size_inside_page(p, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | /* Hmm. Do something? */ |
| 220 | buf += sz; |
| 221 | p += sz; |
| 222 | count -= sz; |
| 223 | written += sz; |
| 224 | } |
| 225 | #endif |
| 226 | |
| 227 | while (count > 0) { |
Kees Cook | a4866aa | 2017-04-05 09:39:08 -0700 | [diff] [blame] | 228 | int allowed; |
| 229 | |
Wu Fengguang | f222318 | 2009-12-14 17:58:07 -0800 | [diff] [blame] | 230 | sz = size_inside_page(p, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Kees Cook | a4866aa | 2017-04-05 09:39:08 -0700 | [diff] [blame] | 232 | allowed = page_is_allowed(p >> PAGE_SHIFT); |
| 233 | if (!allowed) |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 234 | return -EPERM; |
| 235 | |
Kees Cook | a4866aa | 2017-04-05 09:39:08 -0700 | [diff] [blame] | 236 | /* Skip actual writing when a page is marked as restricted. */ |
| 237 | if (allowed == 1) { |
| 238 | /* |
| 239 | * On ia64 if a page has been mapped somewhere as |
| 240 | * uncached, then it must also be accessed uncached |
| 241 | * by the kernel or data corruption may occur. |
| 242 | */ |
| 243 | ptr = xlate_dev_mem_ptr(p); |
| 244 | if (!ptr) { |
| 245 | if (written) |
| 246 | break; |
| 247 | return -EFAULT; |
| 248 | } |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 249 | |
Kees Cook | a4866aa | 2017-04-05 09:39:08 -0700 | [diff] [blame] | 250 | copied = copy_from_user(ptr, buf, sz); |
| 251 | unxlate_dev_mem_ptr(p, ptr); |
| 252 | if (copied) { |
| 253 | written += sz - copied; |
| 254 | if (written) |
| 255 | break; |
| 256 | return -EFAULT; |
| 257 | } |
venkatesh.pallipadi@intel.com | e045fb2 | 2008-03-18 17:00:15 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | buf += sz; |
| 261 | p += sz; |
| 262 | count -= sz; |
| 263 | written += sz; |
Tetsuo Handa | 8619e5b | 2019-08-26 22:13:25 +0900 | [diff] [blame] | 264 | if (should_stop_iteration()) |
| 265 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | *ppos += written; |
| 269 | return written; |
| 270 | } |
| 271 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 272 | int __weak phys_mem_access_prot_allowed(struct file *file, |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 273 | unsigned long pfn, unsigned long size, pgprot_t *vma_prot) |
| 274 | { |
| 275 | return 1; |
| 276 | } |
| 277 | |
Bjorn Helgaas | 44ac841 | 2006-01-08 01:04:10 -0800 | [diff] [blame] | 278 | #ifndef __HAVE_PHYS_MEM_ACCESS_PROT |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 279 | |
| 280 | /* |
| 281 | * Architectures vary in how they handle caching for addresses |
| 282 | * outside of main memory. |
| 283 | * |
| 284 | */ |
David Howells | ea56f41 | 2010-04-06 14:35:08 -0700 | [diff] [blame] | 285 | #ifdef pgprot_noncached |
Cyril Chemparathy | 7e6735c | 2012-09-12 14:05:58 -0400 | [diff] [blame] | 286 | static int uncached_access(struct file *file, phys_addr_t addr) |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 287 | { |
| 288 | #if defined(CONFIG_IA64) |
| 289 | /* |
| 290 | * On ia64, we ignore O_DSYNC because we cannot tolerate memory |
| 291 | * attribute aliases. |
| 292 | */ |
| 293 | return !(efi_mem_attributes(addr) & EFI_MEMORY_WB); |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 294 | #else |
| 295 | /* |
| 296 | * Accessing memory above the top the kernel knows about or through a |
| 297 | * file pointer |
| 298 | * that was marked O_DSYNC will be done non-cached. |
| 299 | */ |
| 300 | if (file->f_flags & O_DSYNC) |
| 301 | return 1; |
| 302 | return addr >= __pa(high_memory); |
| 303 | #endif |
| 304 | } |
David Howells | ea56f41 | 2010-04-06 14:35:08 -0700 | [diff] [blame] | 305 | #endif |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 306 | |
Bjorn Helgaas | 44ac841 | 2006-01-08 01:04:10 -0800 | [diff] [blame] | 307 | static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, |
| 308 | unsigned long size, pgprot_t vma_prot) |
| 309 | { |
| 310 | #ifdef pgprot_noncached |
Cyril Chemparathy | 7e6735c | 2012-09-12 14:05:58 -0400 | [diff] [blame] | 311 | phys_addr_t offset = pfn << PAGE_SHIFT; |
Bjorn Helgaas | 44ac841 | 2006-01-08 01:04:10 -0800 | [diff] [blame] | 312 | |
| 313 | if (uncached_access(file, offset)) |
| 314 | return pgprot_noncached(vma_prot); |
| 315 | #endif |
| 316 | return vma_prot; |
| 317 | } |
| 318 | #endif |
| 319 | |
David Howells | 5da6185 | 2006-09-27 01:50:16 -0700 | [diff] [blame] | 320 | #ifndef CONFIG_MMU |
| 321 | static unsigned long get_unmapped_area_mem(struct file *file, |
| 322 | unsigned long addr, |
| 323 | unsigned long len, |
| 324 | unsigned long pgoff, |
| 325 | unsigned long flags) |
| 326 | { |
| 327 | if (!valid_mmap_phys_addr_range(pgoff, len)) |
| 328 | return (unsigned long) -EINVAL; |
Benjamin Herrenschmidt | 8a93258 | 2007-04-16 22:53:16 -0700 | [diff] [blame] | 329 | return pgoff << PAGE_SHIFT; |
David Howells | 5da6185 | 2006-09-27 01:50:16 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 332 | /* permit direct mmap, for read, write or exec */ |
| 333 | static unsigned memory_mmap_capabilities(struct file *file) |
| 334 | { |
| 335 | return NOMMU_MAP_DIRECT | |
| 336 | NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC; |
| 337 | } |
| 338 | |
| 339 | static unsigned zero_mmap_capabilities(struct file *file) |
| 340 | { |
| 341 | return NOMMU_MAP_COPY; |
| 342 | } |
| 343 | |
David Howells | 5da6185 | 2006-09-27 01:50:16 -0700 | [diff] [blame] | 344 | /* can't do an in-place private mapping if there's no MMU */ |
| 345 | static inline int private_mapping_ok(struct vm_area_struct *vma) |
| 346 | { |
| 347 | return vma->vm_flags & VM_MAYSHARE; |
| 348 | } |
| 349 | #else |
David Howells | 5da6185 | 2006-09-27 01:50:16 -0700 | [diff] [blame] | 350 | |
| 351 | static inline int private_mapping_ok(struct vm_area_struct *vma) |
| 352 | { |
| 353 | return 1; |
| 354 | } |
| 355 | #endif |
| 356 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 357 | static const struct vm_operations_struct mmap_mem_ops = { |
Rik van Riel | 7ae8ed5 | 2008-07-23 21:27:07 -0700 | [diff] [blame] | 358 | #ifdef CONFIG_HAVE_IOREMAP_PROT |
| 359 | .access = generic_access_phys |
| 360 | #endif |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 361 | }; |
| 362 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 363 | static int mmap_mem(struct file *file, struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | { |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 365 | size_t size = vma->vm_end - vma->vm_start; |
Julius Werner | b299cde | 2017-05-12 14:42:58 -0700 | [diff] [blame] | 366 | phys_addr_t offset = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT; |
| 367 | |
Craig Bergstrom | be62a32 | 2017-11-15 15:29:51 -0700 | [diff] [blame] | 368 | /* Does it even fit in phys_addr_t? */ |
| 369 | if (offset >> PAGE_SHIFT != vma->vm_pgoff) |
| 370 | return -EINVAL; |
| 371 | |
Julius Werner | b299cde | 2017-05-12 14:42:58 -0700 | [diff] [blame] | 372 | /* It's illegal to wrap around the end of the physical address space. */ |
Julius Werner | 32829da | 2017-06-02 15:36:39 -0700 | [diff] [blame] | 373 | if (offset + (phys_addr_t)size - 1 < offset) |
Julius Werner | b299cde | 2017-05-12 14:42:58 -0700 | [diff] [blame] | 374 | return -EINVAL; |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 375 | |
Lennert Buytenhek | 06c67be | 2006-07-10 04:45:27 -0700 | [diff] [blame] | 376 | if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size)) |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 377 | return -EINVAL; |
| 378 | |
David Howells | 5da6185 | 2006-09-27 01:50:16 -0700 | [diff] [blame] | 379 | if (!private_mapping_ok(vma)) |
| 380 | return -ENOSYS; |
| 381 | |
Venki Pallipadi | e2beb3e | 2008-03-06 23:01:47 -0800 | [diff] [blame] | 382 | if (!range_is_allowed(vma->vm_pgoff, size)) |
| 383 | return -EPERM; |
| 384 | |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 385 | if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size, |
| 386 | &vma->vm_page_prot)) |
| 387 | return -EINVAL; |
| 388 | |
Roland Dreier | 8b15047 | 2005-10-28 17:46:18 -0700 | [diff] [blame] | 389 | vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff, |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 390 | size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | vma->vm_page_prot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 393 | vma->vm_ops = &mmap_mem_ops; |
| 394 | |
Konstantin Khlebnikov | 314e51b | 2012-10-08 16:29:02 -0700 | [diff] [blame] | 395 | /* Remap-pfn-range will mark the range VM_IO */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | if (remap_pfn_range(vma, |
| 397 | vma->vm_start, |
| 398 | vma->vm_pgoff, |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 399 | size, |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 400 | vma->vm_page_prot)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | return -EAGAIN; |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 402 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | return 0; |
| 404 | } |
| 405 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 406 | static ssize_t read_port(struct file *file, char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | size_t count, loff_t *ppos) |
| 408 | { |
| 409 | unsigned long i = *ppos; |
| 410 | char __user *tmp = buf; |
| 411 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 412 | if (!access_ok(buf, count)) |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 413 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | while (count-- > 0 && i < 65536) { |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 415 | if (__put_user(inb(i), tmp) < 0) |
| 416 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | i++; |
| 418 | tmp++; |
| 419 | } |
| 420 | *ppos = i; |
| 421 | return tmp-buf; |
| 422 | } |
| 423 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 424 | static ssize_t write_port(struct file *file, const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | size_t count, loff_t *ppos) |
| 426 | { |
| 427 | unsigned long i = *ppos; |
Hans Grob | 890537b | 2013-02-06 11:37:20 +0100 | [diff] [blame] | 428 | const char __user *tmp = buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 430 | if (!access_ok(buf, count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | return -EFAULT; |
| 432 | while (count-- > 0 && i < 65536) { |
| 433 | char c; |
Rob Ward | 6a0061b | 2014-12-20 18:28:36 +0000 | [diff] [blame] | 434 | |
Jan Beulich | c654d60 | 2006-03-25 03:07:31 -0800 | [diff] [blame] | 435 | if (__get_user(c, tmp)) { |
| 436 | if (tmp > buf) |
| 437 | break; |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 438 | return -EFAULT; |
Jan Beulich | c654d60 | 2006-03-25 03:07:31 -0800 | [diff] [blame] | 439 | } |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 440 | outb(c, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | i++; |
| 442 | tmp++; |
| 443 | } |
| 444 | *ppos = i; |
| 445 | return tmp-buf; |
| 446 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 448 | static ssize_t read_null(struct file *file, char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | size_t count, loff_t *ppos) |
| 450 | { |
| 451 | return 0; |
| 452 | } |
| 453 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 454 | static ssize_t write_null(struct file *file, const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | size_t count, loff_t *ppos) |
| 456 | { |
| 457 | return count; |
| 458 | } |
| 459 | |
Al Viro | cd28e28 | 2015-04-03 15:57:04 -0400 | [diff] [blame] | 460 | 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] | 461 | { |
| 462 | return 0; |
| 463 | } |
| 464 | |
Al Viro | cd28e28 | 2015-04-03 15:57:04 -0400 | [diff] [blame] | 465 | 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] | 466 | { |
Al Viro | cd28e28 | 2015-04-03 15:57:04 -0400 | [diff] [blame] | 467 | size_t count = iov_iter_count(from); |
| 468 | iov_iter_advance(from, count); |
| 469 | return count; |
Zach Brown | 162934d | 2013-05-07 16:18:27 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Jens Axboe | 1ebd32f | 2006-04-26 14:40:08 +0200 | [diff] [blame] | 472 | static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf, |
| 473 | struct splice_desc *sd) |
| 474 | { |
| 475 | return sd->len; |
| 476 | } |
| 477 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 478 | 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] | 479 | loff_t *ppos, size_t len, unsigned int flags) |
| 480 | { |
| 481 | return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null); |
| 482 | } |
| 483 | |
Al Viro | 13ba33e | 2014-08-18 10:04:12 -0400 | [diff] [blame] | 484 | 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] | 485 | { |
| 486 | size_t written = 0; |
Zach Brown | 162934d | 2013-05-07 16:18:27 -0700 | [diff] [blame] | 487 | |
Al Viro | 13ba33e | 2014-08-18 10:04:12 -0400 | [diff] [blame] | 488 | while (iov_iter_count(iter)) { |
| 489 | size_t chunk = iov_iter_count(iter), n; |
Rob Ward | 6a0061b | 2014-12-20 18:28:36 +0000 | [diff] [blame] | 490 | |
Al Viro | 13ba33e | 2014-08-18 10:04:12 -0400 | [diff] [blame] | 491 | if (chunk > PAGE_SIZE) |
| 492 | chunk = PAGE_SIZE; /* Just for latency reasons */ |
| 493 | n = iov_iter_zero(chunk, iter); |
| 494 | if (!n && iov_iter_count(iter)) |
| 495 | return written ? written : -EFAULT; |
| 496 | written += n; |
| 497 | if (signal_pending(current)) |
| 498 | return written ? written : -ERESTARTSYS; |
| 499 | cond_resched(); |
Zach Brown | 162934d | 2013-05-07 16:18:27 -0700 | [diff] [blame] | 500 | } |
Al Viro | 13ba33e | 2014-08-18 10:04:12 -0400 | [diff] [blame] | 501 | return written; |
Zach Brown | 162934d | 2013-05-07 16:18:27 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Christoph Hellwig | 99f6673 | 2020-09-03 17:59:22 +0200 | [diff] [blame] | 504 | static ssize_t read_zero(struct file *file, char __user *buf, |
| 505 | size_t count, loff_t *ppos) |
| 506 | { |
| 507 | size_t cleared = 0; |
| 508 | |
| 509 | while (count) { |
| 510 | size_t chunk = min_t(size_t, count, PAGE_SIZE); |
Christoph Hellwig | ab04de8 | 2020-09-07 10:27:00 +0200 | [diff] [blame] | 511 | size_t left; |
Christoph Hellwig | 99f6673 | 2020-09-03 17:59:22 +0200 | [diff] [blame] | 512 | |
Christoph Hellwig | ab04de8 | 2020-09-07 10:27:00 +0200 | [diff] [blame] | 513 | left = clear_user(buf + cleared, chunk); |
| 514 | if (unlikely(left)) { |
| 515 | cleared += (chunk - left); |
| 516 | if (!cleared) |
| 517 | return -EFAULT; |
| 518 | break; |
| 519 | } |
Christoph Hellwig | 99f6673 | 2020-09-03 17:59:22 +0200 | [diff] [blame] | 520 | cleared += chunk; |
| 521 | count -= chunk; |
| 522 | |
| 523 | if (signal_pending(current)) |
Christoph Hellwig | ab04de8 | 2020-09-07 10:27:00 +0200 | [diff] [blame] | 524 | break; |
Christoph Hellwig | 99f6673 | 2020-09-03 17:59:22 +0200 | [diff] [blame] | 525 | cond_resched(); |
| 526 | } |
| 527 | |
| 528 | return cleared; |
| 529 | } |
| 530 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 531 | static int mmap_zero(struct file *file, struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | { |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 533 | #ifndef CONFIG_MMU |
| 534 | return -ENOSYS; |
| 535 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | if (vma->vm_flags & VM_SHARED) |
| 537 | return shmem_zero_setup(vma); |
Kirill A. Shutemov | bfd40ea | 2018-07-26 16:37:35 -0700 | [diff] [blame] | 538 | vma_set_anonymous(vma); |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 539 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
Hugh Dickins | c01d5b3 | 2016-07-26 15:26:15 -0700 | [diff] [blame] | 542 | static unsigned long get_unmapped_area_zero(struct file *file, |
| 543 | unsigned long addr, unsigned long len, |
| 544 | unsigned long pgoff, unsigned long flags) |
| 545 | { |
| 546 | #ifdef CONFIG_MMU |
| 547 | if (flags & MAP_SHARED) { |
| 548 | /* |
| 549 | * mmap_zero() will call shmem_zero_setup() to create a file, |
| 550 | * so use shmem's get_unmapped_area in case it can be huge; |
| 551 | * and pass NULL for file as in mmap.c's get_unmapped_area(), |
| 552 | * so as not to confuse shmem with our handle on "/dev/zero". |
| 553 | */ |
| 554 | return shmem_get_unmapped_area(NULL, addr, len, pgoff, flags); |
| 555 | } |
| 556 | |
| 557 | /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */ |
| 558 | return current->mm->get_unmapped_area(file, addr, len, pgoff, flags); |
| 559 | #else |
| 560 | return -ENOSYS; |
| 561 | #endif |
| 562 | } |
| 563 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 564 | static ssize_t write_full(struct file *file, const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | size_t count, loff_t *ppos) |
| 566 | { |
| 567 | return -ENOSPC; |
| 568 | } |
| 569 | |
| 570 | /* |
| 571 | * Special lseek() function for /dev/null and /dev/zero. Most notably, you |
| 572 | * can fopen() both devices with "a" now. This was previously impossible. |
| 573 | * -- SRB. |
| 574 | */ |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 575 | 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] | 576 | { |
| 577 | return file->f_pos = 0; |
| 578 | } |
| 579 | |
| 580 | /* |
| 581 | * The memory devices use the full 32/64 bits of the offset, and so we cannot |
| 582 | * check against negative addresses: they are ok. The return value is weird, |
| 583 | * though, in that case (0). |
| 584 | * |
| 585 | * also note that seeking relative to the "end of file" isn't supported: |
| 586 | * it has no meaning, so it returns -EINVAL. |
| 587 | */ |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 588 | 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] | 589 | { |
| 590 | loff_t ret; |
| 591 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 592 | inode_lock(file_inode(file)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | switch (orig) { |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 594 | case SEEK_CUR: |
| 595 | offset += file->f_pos; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 596 | fallthrough; |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 597 | case SEEK_SET: |
| 598 | /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */ |
Andrzej Hajda | ecb63a1 | 2016-02-15 15:35:21 +0100 | [diff] [blame] | 599 | if ((unsigned long long)offset >= -MAX_ERRNO) { |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 600 | ret = -EOVERFLOW; |
| 601 | break; |
| 602 | } |
| 603 | file->f_pos = offset; |
| 604 | ret = file->f_pos; |
| 605 | force_successful_syscall_return(); |
| 606 | break; |
| 607 | default: |
| 608 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 610 | inode_unlock(file_inode(file)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | return ret; |
| 612 | } |
| 613 | |
Hans Grob | 890537b | 2013-02-06 11:37:20 +0100 | [diff] [blame] | 614 | static int open_port(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | { |
Dan Williams | 3234ac6 | 2020-05-21 14:06:17 -0700 | [diff] [blame] | 616 | int rc; |
| 617 | |
Matthew Garrett | 9b9d8dd | 2019-08-19 17:17:41 -0700 | [diff] [blame] | 618 | if (!capable(CAP_SYS_RAWIO)) |
| 619 | return -EPERM; |
| 620 | |
Dan Williams | 3234ac6 | 2020-05-21 14:06:17 -0700 | [diff] [blame] | 621 | rc = security_locked_down(LOCKDOWN_DEV_MEM); |
| 622 | if (rc) |
| 623 | return rc; |
| 624 | |
| 625 | if (iminor(inode) != DEVMEM_MINOR) |
| 626 | return 0; |
| 627 | |
| 628 | /* |
| 629 | * Use a unified address space to have a single point to manage |
| 630 | * revocations when drivers want to take over a /dev/mem mapped |
| 631 | * range. |
| 632 | */ |
Daniel Vetter | 71a1d8e | 2020-11-27 17:41:24 +0100 | [diff] [blame] | 633 | filp->f_mapping = iomem_get_mapping(); |
Dan Williams | 3234ac6 | 2020-05-21 14:06:17 -0700 | [diff] [blame] | 634 | |
| 635 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | #define zero_lseek null_lseek |
| 639 | #define full_lseek null_lseek |
| 640 | #define write_zero write_null |
Al Viro | cd28e28 | 2015-04-03 15:57:04 -0400 | [diff] [blame] | 641 | #define write_iter_zero write_iter_null |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | #define open_mem open_port |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | |
Rob Ward | 73f0718 | 2014-12-07 15:40:33 +0000 | [diff] [blame] | 644 | static const struct file_operations __maybe_unused mem_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | .llseek = memory_lseek, |
| 646 | .read = read_mem, |
| 647 | .write = write_mem, |
| 648 | .mmap = mmap_mem, |
| 649 | .open = open_mem, |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 650 | #ifndef CONFIG_MMU |
David Howells | 5da6185 | 2006-09-27 01:50:16 -0700 | [diff] [blame] | 651 | .get_unmapped_area = get_unmapped_area_mem, |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 652 | .mmap_capabilities = memory_mmap_capabilities, |
| 653 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | }; |
| 655 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 656 | static const struct file_operations null_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | .llseek = null_lseek, |
| 658 | .read = read_null, |
| 659 | .write = write_null, |
Al Viro | cd28e28 | 2015-04-03 15:57:04 -0400 | [diff] [blame] | 660 | .read_iter = read_iter_null, |
| 661 | .write_iter = write_iter_null, |
Jens Axboe | 1ebd32f | 2006-04-26 14:40:08 +0200 | [diff] [blame] | 662 | .splice_write = splice_write_null, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | }; |
| 664 | |
Rob Ward | 3a4bc2f | 2014-12-07 15:40:35 +0000 | [diff] [blame] | 665 | static const struct file_operations __maybe_unused port_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | .llseek = memory_lseek, |
| 667 | .read = read_port, |
| 668 | .write = write_port, |
| 669 | .open = open_port, |
| 670 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 672 | static const struct file_operations zero_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | .llseek = zero_lseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | .write = write_zero, |
Al Viro | 13ba33e | 2014-08-18 10:04:12 -0400 | [diff] [blame] | 675 | .read_iter = read_iter_zero, |
Christoph Hellwig | 99f6673 | 2020-09-03 17:59:22 +0200 | [diff] [blame] | 676 | .read = read_zero, |
Al Viro | cd28e28 | 2015-04-03 15:57:04 -0400 | [diff] [blame] | 677 | .write_iter = write_iter_zero, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | .mmap = mmap_zero, |
Hugh Dickins | c01d5b3 | 2016-07-26 15:26:15 -0700 | [diff] [blame] | 679 | .get_unmapped_area = get_unmapped_area_zero, |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 680 | #ifndef CONFIG_MMU |
| 681 | .mmap_capabilities = zero_mmap_capabilities, |
| 682 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | }; |
| 684 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 685 | static const struct file_operations full_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | .llseek = full_lseek, |
Al Viro | 13ba33e | 2014-08-18 10:04:12 -0400 | [diff] [blame] | 687 | .read_iter = read_iter_zero, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | .write = write_full, |
| 689 | }; |
| 690 | |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 691 | static const struct memdev { |
| 692 | const char *name; |
Al Viro | 2c9ede5 | 2011-07-23 20:24:48 -0400 | [diff] [blame] | 693 | umode_t mode; |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 694 | const struct file_operations *fops; |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 695 | fmode_t fmode; |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 696 | } devlist[] = { |
Rob Ward | 73f0718 | 2014-12-07 15:40:33 +0000 | [diff] [blame] | 697 | #ifdef CONFIG_DEVMEM |
Dan Williams | 3234ac6 | 2020-05-21 14:06:17 -0700 | [diff] [blame] | 698 | [DEVMEM_MINOR] = { "mem", 0, &mem_fops, FMODE_UNSIGNED_OFFSET }, |
Rob Ward | 73f0718 | 2014-12-07 15:40:33 +0000 | [diff] [blame] | 699 | #endif |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 700 | [3] = { "null", 0666, &null_fops, 0 }, |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 701 | #ifdef CONFIG_DEVPORT |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 702 | [4] = { "port", 0, &port_fops, 0 }, |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 703 | #endif |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 704 | [5] = { "zero", 0666, &zero_fops, 0 }, |
| 705 | [7] = { "full", 0666, &full_fops, 0 }, |
| 706 | [8] = { "random", 0666, &random_fops, 0 }, |
| 707 | [9] = { "urandom", 0666, &urandom_fops, 0 }, |
Kay Sievers | 7f3a781 | 2012-05-09 01:37:51 +0200 | [diff] [blame] | 708 | #ifdef CONFIG_PRINTK |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 709 | [11] = { "kmsg", 0644, &kmsg_fops, 0 }, |
Kay Sievers | 7f3a781 | 2012-05-09 01:37:51 +0200 | [diff] [blame] | 710 | #endif |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 711 | }; |
| 712 | |
| 713 | static int memory_open(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | { |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 715 | int minor; |
| 716 | const struct memdev *dev; |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 717 | |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 718 | minor = iminor(inode); |
| 719 | if (minor >= ARRAY_SIZE(devlist)) |
Frederic Weisbecker | 205153a | 2009-10-09 20:31:02 +0200 | [diff] [blame] | 720 | return -ENXIO; |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 721 | |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 722 | dev = &devlist[minor]; |
| 723 | if (!dev->fops) |
Frederic Weisbecker | 205153a | 2009-10-09 20:31:02 +0200 | [diff] [blame] | 724 | return -ENXIO; |
Adriano dos Santos Fernandes | d6f47be | 2009-06-17 16:27:48 -0700 | [diff] [blame] | 725 | |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 726 | filp->f_op = dev->fops; |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 727 | filp->f_mode |= dev->fmode; |
KAMEZAWA Hiroyuki | 4a3956c | 2010-10-01 14:20:22 -0700 | [diff] [blame] | 728 | |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 729 | if (dev->fops->open) |
Frederic Weisbecker | 205153a | 2009-10-09 20:31:02 +0200 | [diff] [blame] | 730 | return dev->fops->open(inode, filp); |
| 731 | |
| 732 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | } |
| 734 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 735 | static const struct file_operations memory_fops = { |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 736 | .open = memory_open, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 737 | .llseek = noop_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | }; |
| 739 | |
Al Viro | 2c9ede5 | 2011-07-23 20:24:48 -0400 | [diff] [blame] | 740 | static char *mem_devnode(struct device *dev, umode_t *mode) |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 741 | { |
| 742 | if (mode && devlist[MINOR(dev->devt)].mode) |
| 743 | *mode = devlist[MINOR(dev->devt)].mode; |
| 744 | return NULL; |
| 745 | } |
| 746 | |
gregkh@suse.de | ca8eca6 | 2005-03-23 09:53:09 -0800 | [diff] [blame] | 747 | static struct class *mem_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | |
| 749 | static int __init chr_dev_init(void) |
| 750 | { |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 751 | int minor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
Andrew Morton | d7d4d84 | 2010-03-10 15:21:52 -0800 | [diff] [blame] | 753 | if (register_chrdev(MEM_MAJOR, "mem", &memory_fops)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | printk("unable to get major %d for memory devs\n", MEM_MAJOR); |
| 755 | |
gregkh@suse.de | ca8eca6 | 2005-03-23 09:53:09 -0800 | [diff] [blame] | 756 | mem_class = class_create(THIS_MODULE, "mem"); |
Anton Blanchard | 6e191f7 | 2010-04-06 14:34:55 -0700 | [diff] [blame] | 757 | if (IS_ERR(mem_class)) |
| 758 | return PTR_ERR(mem_class); |
| 759 | |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 760 | mem_class->devnode = mem_devnode; |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 761 | for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) { |
| 762 | if (!devlist[minor].name) |
| 763 | continue; |
Haren Myneni | e1612de | 2012-07-11 15:18:44 +1000 | [diff] [blame] | 764 | |
| 765 | /* |
Hans Grob | 890537b | 2013-02-06 11:37:20 +0100 | [diff] [blame] | 766 | * Create /dev/port? |
Haren Myneni | e1612de | 2012-07-11 15:18:44 +1000 | [diff] [blame] | 767 | */ |
| 768 | if ((minor == DEVPORT_MINOR) && !arch_has_dev_port()) |
| 769 | continue; |
| 770 | |
Kay Sievers | 389e0cb | 2009-07-04 16:51:29 +0200 | [diff] [blame] | 771 | device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor), |
| 772 | NULL, devlist[minor].name); |
| 773 | } |
Greg Kroah-Hartman | ebf644c | 2006-07-25 17:13:31 -0700 | [diff] [blame] | 774 | |
David Howells | 31d1d48 | 2010-08-06 16:34:43 +0100 | [diff] [blame] | 775 | return tty_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | fs_initcall(chr_dev_init); |