blob: 15dc54fa1d472ad87051233210882729c0829729 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/drivers/char/mem.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 *
Andrew Mortond7d4d842010-03-10 15:21:52 -08007 * Added devfs support.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009 * Shared /dev/zero mmapping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#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 Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/ptrace.h>
23#include <linux/device.h>
Vivek Goyal50b1fdb2005-06-25 14:58:23 -070024#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/backing-dev.h>
Hugh Dickinsc01d5b32016-07-26 15:26:15 -070026#include <linux/shmem_fs.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020027#include <linux/splice.h>
Linus Torvaldsb8a3ad52006-10-13 08:42:10 -070028#include <linux/pfn.h>
Paul Gortmaker66300e62011-07-10 12:14:53 -040029#include <linux/export.h>
Haren Mynenie1612de2012-07-11 15:18:44 +100030#include <linux/io.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080031#include <linux/uio.h>
Rob Ward35b6c7e2014-12-20 18:28:35 +000032#include <linux/uaccess.h>
Matthew Garrett9b9d8dd2019-08-19 17:17:41 -070033#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#ifdef CONFIG_IA64
36# include <linux/efi.h>
37#endif
38
Dan Williams3234ac62020-05-21 14:06:17 -070039#define DEVMEM_MINOR 1
Haren Mynenie1612de2012-07-11 15:18:44 +100040#define DEVPORT_MINOR 4
41
Wu Fengguangf2223182009-12-14 17:58:07 -080042static inline unsigned long size_inside_page(unsigned long start,
43 unsigned long size)
44{
45 unsigned long sz;
46
Wu Fengguang7fabadd2009-12-14 17:58:09 -080047 sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
Wu Fengguangf2223182009-12-14 17:58:07 -080048
Wu Fengguang7fabadd2009-12-14 17:58:09 -080049 return min(sz, size);
Wu Fengguangf2223182009-12-14 17:58:07 -080050}
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -040053static inline int valid_phys_addr_range(phys_addr_t addr, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Changli Gaocfaf3462011-03-23 16:42:58 -070055 return addr + count <= __pa(high_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080057
Lennert Buytenhek06c67be2006-07-10 04:45:27 -070058static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080059{
60 return 1;
61}
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#endif
63
Ingo Molnard0926332008-07-18 00:26:59 +020064#ifdef CONFIG_STRICT_DEVMEM
Kees Cooka4866aa2017-04-05 09:39:08 -070065static inline int page_is_allowed(unsigned long pfn)
66{
67 return devmem_is_allowed(pfn);
68}
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080069static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020070{
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080071 u64 from = ((u64)pfn) << PAGE_SHIFT;
72 u64 to = from + size;
73 u64 cursor = from;
Arjan van de Venae531c22008-04-24 23:40:47 +020074
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080075 while (cursor < to) {
Jiri Kosina39380b82016-07-08 11:38:28 +020076 if (!devmem_is_allowed(pfn))
Arjan van de Venae531c22008-04-24 23:40:47 +020077 return 0;
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080078 cursor += PAGE_SIZE;
79 pfn++;
Arjan van de Venae531c22008-04-24 23:40:47 +020080 }
81 return 1;
82}
83#else
Kees Cooka4866aa2017-04-05 09:39:08 -070084static inline int page_is_allowed(unsigned long pfn)
85{
86 return 1;
87}
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080088static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020089{
90 return 1;
91}
92#endif
93
Thierry Reding4707a342014-07-28 17:20:33 +020094#ifndef unxlate_dev_mem_ptr
95#define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
96void __weak unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -070097{
98}
Thierry Reding4707a342014-07-28 17:20:33 +020099#endif
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700100
Tetsuo Handa8619e5b2019-08-26 22:13:25 +0900101static inline bool should_stop_iteration(void)
102{
103 if (need_resched())
104 cond_resched();
105 return fatal_signal_pending(current);
106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800109 * This funcion reads the *physical* memory. The f_pos points directly to the
110 * memory location.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800112static ssize_t read_mem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 size_t count, loff_t *ppos)
114{
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400115 phys_addr_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 ssize_t read, sz;
Thierry Reding4707a342014-07-28 17:20:33 +0200117 void *ptr;
Kees Cook22ec1a22017-12-01 13:19:39 -0800118 char *bounce;
119 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Petr Tesarik08d2d002014-01-30 09:48:02 +0100121 if (p != *ppos)
122 return 0;
123
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800124 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 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 Fengguang7fabadd2009-12-14 17:58:09 -0800130 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (sz > 0) {
132 if (clear_user(buf, sz))
133 return -EFAULT;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800134 buf += sz;
135 p += sz;
136 count -= sz;
137 read += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139 }
140#endif
141
Kees Cook22ec1a22017-12-01 13:19:39 -0800142 bounce = kmalloc(PAGE_SIZE, GFP_KERNEL);
143 if (!bounce)
144 return -ENOMEM;
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 while (count > 0) {
Wu Fengguangfa29e972009-12-14 17:58:08 -0800147 unsigned long remaining;
Kees Cookb5b38202018-03-27 14:06:14 -0700148 int allowed, probe;
Wu Fengguangfa29e972009-12-14 17:58:08 -0800149
Wu Fengguangf2223182009-12-14 17:58:07 -0800150 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Kees Cook22ec1a22017-12-01 13:19:39 -0800152 err = -EPERM;
Kees Cooka4866aa2017-04-05 09:39:08 -0700153 allowed = page_is_allowed(p >> PAGE_SHIFT);
154 if (!allowed)
Kees Cook22ec1a22017-12-01 13:19:39 -0800155 goto failed;
156
157 err = -EFAULT;
Kees Cooka4866aa2017-04-05 09:39:08 -0700158 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 Cook22ec1a22017-12-01 13:19:39 -0800169 goto failed;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700170
Christoph Hellwigfe557312020-06-17 09:37:53 +0200171 probe = copy_from_kernel_nofault(bounce, ptr, sz);
Kees Cooka4866aa2017-04-05 09:39:08 -0700172 unxlate_dev_mem_ptr(p, ptr);
Kees Cookb5b38202018-03-27 14:06:14 -0700173 if (probe)
Kees Cook22ec1a22017-12-01 13:19:39 -0800174 goto failed;
175
176 remaining = copy_to_user(buf, bounce, sz);
Kees Cooka4866aa2017-04-05 09:39:08 -0700177 }
178
Wu Fengguangfa29e972009-12-14 17:58:08 -0800179 if (remaining)
Kees Cook22ec1a22017-12-01 13:19:39 -0800180 goto failed;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 buf += sz;
183 p += sz;
184 count -= sz;
185 read += sz;
Tetsuo Handa8619e5b2019-08-26 22:13:25 +0900186 if (should_stop_iteration())
187 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
Kees Cook22ec1a22017-12-01 13:19:39 -0800189 kfree(bounce);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 *ppos += read;
192 return read;
Kees Cook22ec1a22017-12-01 13:19:39 -0800193
194failed:
195 kfree(bounce);
196 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Andrew Mortond7d4d842010-03-10 15:21:52 -0800199static ssize_t write_mem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 size_t count, loff_t *ppos)
201{
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400202 phys_addr_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 ssize_t written, sz;
204 unsigned long copied;
205 void *ptr;
206
Petr Tesarik08d2d002014-01-30 09:48:02 +0100207 if (p != *ppos)
208 return -EFBIG;
209
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800210 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 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 Fengguang7fabadd2009-12-14 17:58:09 -0800218 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 /* Hmm. Do something? */
220 buf += sz;
221 p += sz;
222 count -= sz;
223 written += sz;
224 }
225#endif
226
227 while (count > 0) {
Kees Cooka4866aa2017-04-05 09:39:08 -0700228 int allowed;
229
Wu Fengguangf2223182009-12-14 17:58:07 -0800230 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Kees Cooka4866aa2017-04-05 09:39:08 -0700232 allowed = page_is_allowed(p >> PAGE_SHIFT);
233 if (!allowed)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700234 return -EPERM;
235
Kees Cooka4866aa2017-04-05 09:39:08 -0700236 /* 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.come045fb22008-03-18 17:00:15 -0700249
Kees Cooka4866aa2017-04-05 09:39:08 -0700250 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.come045fb22008-03-18 17:00:15 -0700258 }
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 buf += sz;
261 p += sz;
262 count -= sz;
263 written += sz;
Tetsuo Handa8619e5b2019-08-26 22:13:25 +0900264 if (should_stop_iteration())
265 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267
268 *ppos += written;
269 return written;
270}
271
Andrew Mortond7d4d842010-03-10 15:21:52 -0800272int __weak phys_mem_access_prot_allowed(struct file *file,
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700273 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
274{
275 return 1;
276}
277
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800278#ifndef __HAVE_PHYS_MEM_ACCESS_PROT
Andrew Mortond7d4d842010-03-10 15:21:52 -0800279
280/*
281 * Architectures vary in how they handle caching for addresses
282 * outside of main memory.
283 *
284 */
David Howellsea56f412010-04-06 14:35:08 -0700285#ifdef pgprot_noncached
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400286static int uncached_access(struct file *file, phys_addr_t addr)
Andrew Mortond7d4d842010-03-10 15:21:52 -0800287{
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 Mortond7d4d842010-03-10 15:21:52 -0800294#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 Howellsea56f412010-04-06 14:35:08 -0700305#endif
Andrew Mortond7d4d842010-03-10 15:21:52 -0800306
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800307static 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 Chemparathy7e6735c2012-09-12 14:05:58 -0400311 phys_addr_t offset = pfn << PAGE_SHIFT;
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800312
313 if (uncached_access(file, offset))
314 return pgprot_noncached(vma_prot);
315#endif
316 return vma_prot;
317}
318#endif
319
David Howells5da61852006-09-27 01:50:16 -0700320#ifndef CONFIG_MMU
321static 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 Herrenschmidt8a932582007-04-16 22:53:16 -0700329 return pgoff << PAGE_SHIFT;
David Howells5da61852006-09-27 01:50:16 -0700330}
331
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100332/* permit direct mmap, for read, write or exec */
333static 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
339static unsigned zero_mmap_capabilities(struct file *file)
340{
341 return NOMMU_MAP_COPY;
342}
343
David Howells5da61852006-09-27 01:50:16 -0700344/* can't do an in-place private mapping if there's no MMU */
345static inline int private_mapping_ok(struct vm_area_struct *vma)
346{
347 return vma->vm_flags & VM_MAYSHARE;
348}
349#else
David Howells5da61852006-09-27 01:50:16 -0700350
351static inline int private_mapping_ok(struct vm_area_struct *vma)
352{
353 return 1;
354}
355#endif
356
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400357static const struct vm_operations_struct mmap_mem_ops = {
Rik van Riel7ae8ed52008-07-23 21:27:07 -0700358#ifdef CONFIG_HAVE_IOREMAP_PROT
359 .access = generic_access_phys
360#endif
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700361};
362
Andrew Mortond7d4d842010-03-10 15:21:52 -0800363static int mmap_mem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800365 size_t size = vma->vm_end - vma->vm_start;
Julius Wernerb299cde2017-05-12 14:42:58 -0700366 phys_addr_t offset = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
367
Craig Bergstrombe62a322017-11-15 15:29:51 -0700368 /* Does it even fit in phys_addr_t? */
369 if (offset >> PAGE_SHIFT != vma->vm_pgoff)
370 return -EINVAL;
371
Julius Wernerb299cde2017-05-12 14:42:58 -0700372 /* It's illegal to wrap around the end of the physical address space. */
Julius Werner32829da2017-06-02 15:36:39 -0700373 if (offset + (phys_addr_t)size - 1 < offset)
Julius Wernerb299cde2017-05-12 14:42:58 -0700374 return -EINVAL;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800375
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700376 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800377 return -EINVAL;
378
David Howells5da61852006-09-27 01:50:16 -0700379 if (!private_mapping_ok(vma))
380 return -ENOSYS;
381
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800382 if (!range_is_allowed(vma->vm_pgoff, size))
383 return -EPERM;
384
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700385 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
386 &vma->vm_page_prot))
387 return -EINVAL;
388
Roland Dreier8b150472005-10-28 17:46:18 -0700389 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800390 size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700393 vma->vm_ops = &mmap_mem_ops;
394
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700395 /* Remap-pfn-range will mark the range VM_IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (remap_pfn_range(vma,
397 vma->vm_start,
398 vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800399 size,
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700400 vma->vm_page_prot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return -EAGAIN;
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return 0;
404}
405
Andrew Mortond7d4d842010-03-10 15:21:52 -0800406static ssize_t read_port(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 size_t count, loff_t *ppos)
408{
409 unsigned long i = *ppos;
410 char __user *tmp = buf;
411
Linus Torvalds96d4f262019-01-03 18:57:57 -0800412 if (!access_ok(buf, count))
Andrew Mortond7d4d842010-03-10 15:21:52 -0800413 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 while (count-- > 0 && i < 65536) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800415 if (__put_user(inb(i), tmp) < 0)
416 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 i++;
418 tmp++;
419 }
420 *ppos = i;
421 return tmp-buf;
422}
423
Andrew Mortond7d4d842010-03-10 15:21:52 -0800424static ssize_t write_port(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 size_t count, loff_t *ppos)
426{
427 unsigned long i = *ppos;
Hans Grob890537b2013-02-06 11:37:20 +0100428 const char __user *tmp = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Linus Torvalds96d4f262019-01-03 18:57:57 -0800430 if (!access_ok(buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return -EFAULT;
432 while (count-- > 0 && i < 65536) {
433 char c;
Rob Ward6a0061b2014-12-20 18:28:36 +0000434
Jan Beulichc654d602006-03-25 03:07:31 -0800435 if (__get_user(c, tmp)) {
436 if (tmp > buf)
437 break;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800438 return -EFAULT;
Jan Beulichc654d602006-03-25 03:07:31 -0800439 }
Andrew Mortond7d4d842010-03-10 15:21:52 -0800440 outb(c, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 i++;
442 tmp++;
443 }
444 *ppos = i;
445 return tmp-buf;
446}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Andrew Mortond7d4d842010-03-10 15:21:52 -0800448static ssize_t read_null(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 size_t count, loff_t *ppos)
450{
451 return 0;
452}
453
Andrew Mortond7d4d842010-03-10 15:21:52 -0800454static ssize_t write_null(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 size_t count, loff_t *ppos)
456{
457 return count;
458}
459
Al Virocd28e282015-04-03 15:57:04 -0400460static ssize_t read_iter_null(struct kiocb *iocb, struct iov_iter *to)
Zach Brown162934d2013-05-07 16:18:27 -0700461{
462 return 0;
463}
464
Al Virocd28e282015-04-03 15:57:04 -0400465static ssize_t write_iter_null(struct kiocb *iocb, struct iov_iter *from)
Zach Brown162934d2013-05-07 16:18:27 -0700466{
Al Virocd28e282015-04-03 15:57:04 -0400467 size_t count = iov_iter_count(from);
468 iov_iter_advance(from, count);
469 return count;
Zach Brown162934d2013-05-07 16:18:27 -0700470}
471
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200472static 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 Mortond7d4d842010-03-10 15:21:52 -0800478static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200479 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 Viro13ba33e2014-08-18 10:04:12 -0400484static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
Zach Brown162934d2013-05-07 16:18:27 -0700485{
486 size_t written = 0;
Zach Brown162934d2013-05-07 16:18:27 -0700487
Al Viro13ba33e2014-08-18 10:04:12 -0400488 while (iov_iter_count(iter)) {
489 size_t chunk = iov_iter_count(iter), n;
Rob Ward6a0061b2014-12-20 18:28:36 +0000490
Al Viro13ba33e2014-08-18 10:04:12 -0400491 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 Brown162934d2013-05-07 16:18:27 -0700500 }
Al Viro13ba33e2014-08-18 10:04:12 -0400501 return written;
Zach Brown162934d2013-05-07 16:18:27 -0700502}
503
Christoph Hellwig99f66732020-09-03 17:59:22 +0200504static 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 Hellwigab04de82020-09-07 10:27:00 +0200511 size_t left;
Christoph Hellwig99f66732020-09-03 17:59:22 +0200512
Christoph Hellwigab04de82020-09-07 10:27:00 +0200513 left = clear_user(buf + cleared, chunk);
514 if (unlikely(left)) {
515 cleared += (chunk - left);
516 if (!cleared)
517 return -EFAULT;
518 break;
519 }
Christoph Hellwig99f66732020-09-03 17:59:22 +0200520 cleared += chunk;
521 count -= chunk;
522
523 if (signal_pending(current))
Christoph Hellwigab04de82020-09-07 10:27:00 +0200524 break;
Christoph Hellwig99f66732020-09-03 17:59:22 +0200525 cond_resched();
526 }
527
528 return cleared;
529}
530
Andrew Mortond7d4d842010-03-10 15:21:52 -0800531static int mmap_zero(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700533#ifndef CONFIG_MMU
534 return -ENOSYS;
535#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (vma->vm_flags & VM_SHARED)
537 return shmem_zero_setup(vma);
Kirill A. Shutemovbfd40ea2018-07-26 16:37:35 -0700538 vma_set_anonymous(vma);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Hugh Dickinsc01d5b32016-07-26 15:26:15 -0700542static 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 Mortond7d4d842010-03-10 15:21:52 -0800564static ssize_t write_full(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 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 Mortond7d4d842010-03-10 15:21:52 -0800575static loff_t null_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
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 Mortond7d4d842010-03-10 15:21:52 -0800588static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 loff_t ret;
591
Al Viro59551022016-01-22 15:40:57 -0500592 inode_lock(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 switch (orig) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800594 case SEEK_CUR:
595 offset += file->f_pos;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500596 fallthrough;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800597 case SEEK_SET:
598 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
Andrzej Hajdaecb63a12016-02-15 15:35:21 +0100599 if ((unsigned long long)offset >= -MAX_ERRNO) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800600 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 Torvalds1da177e2005-04-16 15:20:36 -0700609 }
Al Viro59551022016-01-22 15:40:57 -0500610 inode_unlock(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return ret;
612}
613
Hans Grob890537b2013-02-06 11:37:20 +0100614static int open_port(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Dan Williams3234ac62020-05-21 14:06:17 -0700616 int rc;
617
Matthew Garrett9b9d8dd2019-08-19 17:17:41 -0700618 if (!capable(CAP_SYS_RAWIO))
619 return -EPERM;
620
Dan Williams3234ac62020-05-21 14:06:17 -0700621 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 Vetter71a1d8e2020-11-27 17:41:24 +0100633 filp->f_mapping = iomem_get_mapping();
Dan Williams3234ac62020-05-21 14:06:17 -0700634
635 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
638#define zero_lseek null_lseek
639#define full_lseek null_lseek
640#define write_zero write_null
Al Virocd28e282015-04-03 15:57:04 -0400641#define write_iter_zero write_iter_null
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642#define open_mem open_port
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Rob Ward73f07182014-12-07 15:40:33 +0000644static const struct file_operations __maybe_unused mem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 .llseek = memory_lseek,
646 .read = read_mem,
647 .write = write_mem,
648 .mmap = mmap_mem,
649 .open = open_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100650#ifndef CONFIG_MMU
David Howells5da61852006-09-27 01:50:16 -0700651 .get_unmapped_area = get_unmapped_area_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100652 .mmap_capabilities = memory_mmap_capabilities,
653#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654};
655
Arjan van de Ven62322d22006-07-03 00:24:21 -0700656static const struct file_operations null_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 .llseek = null_lseek,
658 .read = read_null,
659 .write = write_null,
Al Virocd28e282015-04-03 15:57:04 -0400660 .read_iter = read_iter_null,
661 .write_iter = write_iter_null,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200662 .splice_write = splice_write_null,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663};
664
Rob Ward3a4bc2f2014-12-07 15:40:35 +0000665static const struct file_operations __maybe_unused port_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 .llseek = memory_lseek,
667 .read = read_port,
668 .write = write_port,
669 .open = open_port,
670};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Arjan van de Ven62322d22006-07-03 00:24:21 -0700672static const struct file_operations zero_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 .llseek = zero_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 .write = write_zero,
Al Viro13ba33e2014-08-18 10:04:12 -0400675 .read_iter = read_iter_zero,
Christoph Hellwig99f66732020-09-03 17:59:22 +0200676 .read = read_zero,
Al Virocd28e282015-04-03 15:57:04 -0400677 .write_iter = write_iter_zero,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 .mmap = mmap_zero,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -0700679 .get_unmapped_area = get_unmapped_area_zero,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100680#ifndef CONFIG_MMU
681 .mmap_capabilities = zero_mmap_capabilities,
682#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683};
684
Arjan van de Ven62322d22006-07-03 00:24:21 -0700685static const struct file_operations full_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 .llseek = full_lseek,
Al Viro13ba33e2014-08-18 10:04:12 -0400687 .read_iter = read_iter_zero,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 .write = write_full,
689};
690
Kay Sievers389e0cb2009-07-04 16:51:29 +0200691static const struct memdev {
692 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400693 umode_t mode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200694 const struct file_operations *fops;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100695 fmode_t fmode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200696} devlist[] = {
Rob Ward73f07182014-12-07 15:40:33 +0000697#ifdef CONFIG_DEVMEM
Dan Williams3234ac62020-05-21 14:06:17 -0700698 [DEVMEM_MINOR] = { "mem", 0, &mem_fops, FMODE_UNSIGNED_OFFSET },
Rob Ward73f07182014-12-07 15:40:33 +0000699#endif
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100700 [3] = { "null", 0666, &null_fops, 0 },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700701#ifdef CONFIG_DEVPORT
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100702 [4] = { "port", 0, &port_fops, 0 },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700703#endif
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100704 [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 Sievers7f3a7812012-05-09 01:37:51 +0200708#ifdef CONFIG_PRINTK
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100709 [11] = { "kmsg", 0644, &kmsg_fops, 0 },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200710#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700711};
712
713static int memory_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200715 int minor;
716 const struct memdev *dev;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700717
Kay Sievers389e0cb2009-07-04 16:51:29 +0200718 minor = iminor(inode);
719 if (minor >= ARRAY_SIZE(devlist))
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200720 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700721
Kay Sievers389e0cb2009-07-04 16:51:29 +0200722 dev = &devlist[minor];
723 if (!dev->fops)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200724 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700725
Kay Sievers389e0cb2009-07-04 16:51:29 +0200726 filp->f_op = dev->fops;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100727 filp->f_mode |= dev->fmode;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700728
Kay Sievers389e0cb2009-07-04 16:51:29 +0200729 if (dev->fops->open)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200730 return dev->fops->open(inode, filp);
731
732 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
Arjan van de Ven62322d22006-07-03 00:24:21 -0700735static const struct file_operations memory_fops = {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800736 .open = memory_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200737 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738};
739
Al Viro2c9ede52011-07-23 20:24:48 -0400740static char *mem_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +0200741{
742 if (mode && devlist[MINOR(dev->devt)].mode)
743 *mode = devlist[MINOR(dev->devt)].mode;
744 return NULL;
745}
746
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800747static struct class *mem_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749static int __init chr_dev_init(void)
750{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200751 int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Andrew Mortond7d4d842010-03-10 15:21:52 -0800753 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
755
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800756 mem_class = class_create(THIS_MODULE, "mem");
Anton Blanchard6e191f72010-04-06 14:34:55 -0700757 if (IS_ERR(mem_class))
758 return PTR_ERR(mem_class);
759
Kay Sieverse454cea2009-09-18 23:01:12 +0200760 mem_class->devnode = mem_devnode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200761 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
762 if (!devlist[minor].name)
763 continue;
Haren Mynenie1612de2012-07-11 15:18:44 +1000764
765 /*
Hans Grob890537b2013-02-06 11:37:20 +0100766 * Create /dev/port?
Haren Mynenie1612de2012-07-11 15:18:44 +1000767 */
768 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
769 continue;
770
Kay Sievers389e0cb2009-07-04 16:51:29 +0200771 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
772 NULL, devlist[minor].name);
773 }
Greg Kroah-Hartmanebf644c2006-07-25 17:13:31 -0700774
David Howells31d1d482010-08-06 16:34:43 +0100775 return tty_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
778fs_initcall(chr_dev_init);