xref: /OK3568_Linux_fs/kernel/drivers/char/mem.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  linux/drivers/char/mem.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *  Copyright (C) 1991, 1992  Linus Torvalds
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  *  Added devfs support.
8*4882a593Smuzhiyun  *    Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
9*4882a593Smuzhiyun  *  Shared /dev/zero mmapping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/mm.h>
13*4882a593Smuzhiyun #include <linux/miscdevice.h>
14*4882a593Smuzhiyun #include <linux/slab.h>
15*4882a593Smuzhiyun #include <linux/vmalloc.h>
16*4882a593Smuzhiyun #include <linux/mman.h>
17*4882a593Smuzhiyun #include <linux/random.h>
18*4882a593Smuzhiyun #include <linux/init.h>
19*4882a593Smuzhiyun #include <linux/raw.h>
20*4882a593Smuzhiyun #include <linux/tty.h>
21*4882a593Smuzhiyun #include <linux/capability.h>
22*4882a593Smuzhiyun #include <linux/ptrace.h>
23*4882a593Smuzhiyun #include <linux/device.h>
24*4882a593Smuzhiyun #include <linux/highmem.h>
25*4882a593Smuzhiyun #include <linux/backing-dev.h>
26*4882a593Smuzhiyun #include <linux/shmem_fs.h>
27*4882a593Smuzhiyun #include <linux/splice.h>
28*4882a593Smuzhiyun #include <linux/pfn.h>
29*4882a593Smuzhiyun #include <linux/export.h>
30*4882a593Smuzhiyun #include <linux/io.h>
31*4882a593Smuzhiyun #include <linux/uio.h>
32*4882a593Smuzhiyun #include <linux/uaccess.h>
33*4882a593Smuzhiyun #include <linux/security.h>
34*4882a593Smuzhiyun #include <linux/pseudo_fs.h>
35*4882a593Smuzhiyun #include <uapi/linux/magic.h>
36*4882a593Smuzhiyun #include <linux/mount.h>
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #ifdef CONFIG_IA64
39*4882a593Smuzhiyun # include <linux/efi.h>
40*4882a593Smuzhiyun #endif
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #define DEVMEM_MINOR	1
43*4882a593Smuzhiyun #define DEVPORT_MINOR	4
44*4882a593Smuzhiyun 
size_inside_page(unsigned long start,unsigned long size)45*4882a593Smuzhiyun static inline unsigned long size_inside_page(unsigned long start,
46*4882a593Smuzhiyun 					     unsigned long size)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun 	unsigned long sz;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	return min(sz, size);
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
valid_phys_addr_range(phys_addr_t addr,size_t count)56*4882a593Smuzhiyun static inline int valid_phys_addr_range(phys_addr_t addr, size_t count)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun 	return addr + count <= __pa(high_memory);
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun 
valid_mmap_phys_addr_range(unsigned long pfn,size_t size)61*4882a593Smuzhiyun static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	return 1;
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun #endif
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun #ifdef CONFIG_STRICT_DEVMEM
page_is_allowed(unsigned long pfn)68*4882a593Smuzhiyun static inline int page_is_allowed(unsigned long pfn)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	return devmem_is_allowed(pfn);
71*4882a593Smuzhiyun }
range_is_allowed(unsigned long pfn,unsigned long size)72*4882a593Smuzhiyun static inline int range_is_allowed(unsigned long pfn, unsigned long size)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun 	u64 from = ((u64)pfn) << PAGE_SHIFT;
75*4882a593Smuzhiyun 	u64 to = from + size;
76*4882a593Smuzhiyun 	u64 cursor = from;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	while (cursor < to) {
79*4882a593Smuzhiyun 		if (!devmem_is_allowed(pfn))
80*4882a593Smuzhiyun 			return 0;
81*4882a593Smuzhiyun 		cursor += PAGE_SIZE;
82*4882a593Smuzhiyun 		pfn++;
83*4882a593Smuzhiyun 	}
84*4882a593Smuzhiyun 	return 1;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun #else
page_is_allowed(unsigned long pfn)87*4882a593Smuzhiyun static inline int page_is_allowed(unsigned long pfn)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun 	return 1;
90*4882a593Smuzhiyun }
range_is_allowed(unsigned long pfn,unsigned long size)91*4882a593Smuzhiyun static inline int range_is_allowed(unsigned long pfn, unsigned long size)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun 	return 1;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun #endif
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun #ifndef unxlate_dev_mem_ptr
98*4882a593Smuzhiyun #define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
unxlate_dev_mem_ptr(phys_addr_t phys,void * addr)99*4882a593Smuzhiyun void __weak unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun #endif
103*4882a593Smuzhiyun 
should_stop_iteration(void)104*4882a593Smuzhiyun static inline bool should_stop_iteration(void)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	if (need_resched())
107*4882a593Smuzhiyun 		cond_resched();
108*4882a593Smuzhiyun 	return fatal_signal_pending(current);
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun /*
112*4882a593Smuzhiyun  * This funcion reads the *physical* memory. The f_pos points directly to the
113*4882a593Smuzhiyun  * memory location.
114*4882a593Smuzhiyun  */
read_mem(struct file * file,char __user * buf,size_t count,loff_t * ppos)115*4882a593Smuzhiyun static ssize_t read_mem(struct file *file, char __user *buf,
116*4882a593Smuzhiyun 			size_t count, loff_t *ppos)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun 	phys_addr_t p = *ppos;
119*4882a593Smuzhiyun 	ssize_t read, sz;
120*4882a593Smuzhiyun 	void *ptr;
121*4882a593Smuzhiyun 	char *bounce;
122*4882a593Smuzhiyun 	int err;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	if (p != *ppos)
125*4882a593Smuzhiyun 		return 0;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	if (!valid_phys_addr_range(p, count))
128*4882a593Smuzhiyun 		return -EFAULT;
129*4882a593Smuzhiyun 	read = 0;
130*4882a593Smuzhiyun #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
131*4882a593Smuzhiyun 	/* we don't have page 0 mapped on sparc and m68k.. */
132*4882a593Smuzhiyun 	if (p < PAGE_SIZE) {
133*4882a593Smuzhiyun 		sz = size_inside_page(p, count);
134*4882a593Smuzhiyun 		if (sz > 0) {
135*4882a593Smuzhiyun 			if (clear_user(buf, sz))
136*4882a593Smuzhiyun 				return -EFAULT;
137*4882a593Smuzhiyun 			buf += sz;
138*4882a593Smuzhiyun 			p += sz;
139*4882a593Smuzhiyun 			count -= sz;
140*4882a593Smuzhiyun 			read += sz;
141*4882a593Smuzhiyun 		}
142*4882a593Smuzhiyun 	}
143*4882a593Smuzhiyun #endif
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	bounce = kmalloc(PAGE_SIZE, GFP_KERNEL);
146*4882a593Smuzhiyun 	if (!bounce)
147*4882a593Smuzhiyun 		return -ENOMEM;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	while (count > 0) {
150*4882a593Smuzhiyun 		unsigned long remaining;
151*4882a593Smuzhiyun 		int allowed, probe;
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 		sz = size_inside_page(p, count);
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 		err = -EPERM;
156*4882a593Smuzhiyun 		allowed = page_is_allowed(p >> PAGE_SHIFT);
157*4882a593Smuzhiyun 		if (!allowed)
158*4882a593Smuzhiyun 			goto failed;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 		err = -EFAULT;
161*4882a593Smuzhiyun 		if (allowed == 2) {
162*4882a593Smuzhiyun 			/* Show zeros for restricted memory. */
163*4882a593Smuzhiyun 			remaining = clear_user(buf, sz);
164*4882a593Smuzhiyun 		} else {
165*4882a593Smuzhiyun 			/*
166*4882a593Smuzhiyun 			 * On ia64 if a page has been mapped somewhere as
167*4882a593Smuzhiyun 			 * uncached, then it must also be accessed uncached
168*4882a593Smuzhiyun 			 * by the kernel or data corruption may occur.
169*4882a593Smuzhiyun 			 */
170*4882a593Smuzhiyun 			ptr = xlate_dev_mem_ptr(p);
171*4882a593Smuzhiyun 			if (!ptr)
172*4882a593Smuzhiyun 				goto failed;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 			probe = copy_from_kernel_nofault(bounce, ptr, sz);
175*4882a593Smuzhiyun 			unxlate_dev_mem_ptr(p, ptr);
176*4882a593Smuzhiyun 			if (probe)
177*4882a593Smuzhiyun 				goto failed;
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 			remaining = copy_to_user(buf, bounce, sz);
180*4882a593Smuzhiyun 		}
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 		if (remaining)
183*4882a593Smuzhiyun 			goto failed;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 		buf += sz;
186*4882a593Smuzhiyun 		p += sz;
187*4882a593Smuzhiyun 		count -= sz;
188*4882a593Smuzhiyun 		read += sz;
189*4882a593Smuzhiyun 		if (should_stop_iteration())
190*4882a593Smuzhiyun 			break;
191*4882a593Smuzhiyun 	}
192*4882a593Smuzhiyun 	kfree(bounce);
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	*ppos += read;
195*4882a593Smuzhiyun 	return read;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun failed:
198*4882a593Smuzhiyun 	kfree(bounce);
199*4882a593Smuzhiyun 	return err;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun 
write_mem(struct file * file,const char __user * buf,size_t count,loff_t * ppos)202*4882a593Smuzhiyun static ssize_t write_mem(struct file *file, const char __user *buf,
203*4882a593Smuzhiyun 			 size_t count, loff_t *ppos)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun 	phys_addr_t p = *ppos;
206*4882a593Smuzhiyun 	ssize_t written, sz;
207*4882a593Smuzhiyun 	unsigned long copied;
208*4882a593Smuzhiyun 	void *ptr;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	if (p != *ppos)
211*4882a593Smuzhiyun 		return -EFBIG;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	if (!valid_phys_addr_range(p, count))
214*4882a593Smuzhiyun 		return -EFAULT;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	written = 0;
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
219*4882a593Smuzhiyun 	/* we don't have page 0 mapped on sparc and m68k.. */
220*4882a593Smuzhiyun 	if (p < PAGE_SIZE) {
221*4882a593Smuzhiyun 		sz = size_inside_page(p, count);
222*4882a593Smuzhiyun 		/* Hmm. Do something? */
223*4882a593Smuzhiyun 		buf += sz;
224*4882a593Smuzhiyun 		p += sz;
225*4882a593Smuzhiyun 		count -= sz;
226*4882a593Smuzhiyun 		written += sz;
227*4882a593Smuzhiyun 	}
228*4882a593Smuzhiyun #endif
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	while (count > 0) {
231*4882a593Smuzhiyun 		int allowed;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 		sz = size_inside_page(p, count);
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 		allowed = page_is_allowed(p >> PAGE_SHIFT);
236*4882a593Smuzhiyun 		if (!allowed)
237*4882a593Smuzhiyun 			return -EPERM;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 		/* Skip actual writing when a page is marked as restricted. */
240*4882a593Smuzhiyun 		if (allowed == 1) {
241*4882a593Smuzhiyun 			/*
242*4882a593Smuzhiyun 			 * On ia64 if a page has been mapped somewhere as
243*4882a593Smuzhiyun 			 * uncached, then it must also be accessed uncached
244*4882a593Smuzhiyun 			 * by the kernel or data corruption may occur.
245*4882a593Smuzhiyun 			 */
246*4882a593Smuzhiyun 			ptr = xlate_dev_mem_ptr(p);
247*4882a593Smuzhiyun 			if (!ptr) {
248*4882a593Smuzhiyun 				if (written)
249*4882a593Smuzhiyun 					break;
250*4882a593Smuzhiyun 				return -EFAULT;
251*4882a593Smuzhiyun 			}
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 			copied = copy_from_user(ptr, buf, sz);
254*4882a593Smuzhiyun 			unxlate_dev_mem_ptr(p, ptr);
255*4882a593Smuzhiyun 			if (copied) {
256*4882a593Smuzhiyun 				written += sz - copied;
257*4882a593Smuzhiyun 				if (written)
258*4882a593Smuzhiyun 					break;
259*4882a593Smuzhiyun 				return -EFAULT;
260*4882a593Smuzhiyun 			}
261*4882a593Smuzhiyun 		}
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 		buf += sz;
264*4882a593Smuzhiyun 		p += sz;
265*4882a593Smuzhiyun 		count -= sz;
266*4882a593Smuzhiyun 		written += sz;
267*4882a593Smuzhiyun 		if (should_stop_iteration())
268*4882a593Smuzhiyun 			break;
269*4882a593Smuzhiyun 	}
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	*ppos += written;
272*4882a593Smuzhiyun 	return written;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun 
phys_mem_access_prot_allowed(struct file * file,unsigned long pfn,unsigned long size,pgprot_t * vma_prot)275*4882a593Smuzhiyun int __weak phys_mem_access_prot_allowed(struct file *file,
276*4882a593Smuzhiyun 	unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun 	return 1;
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun /*
284*4882a593Smuzhiyun  * Architectures vary in how they handle caching for addresses
285*4882a593Smuzhiyun  * outside of main memory.
286*4882a593Smuzhiyun  *
287*4882a593Smuzhiyun  */
288*4882a593Smuzhiyun #ifdef pgprot_noncached
uncached_access(struct file * file,phys_addr_t addr)289*4882a593Smuzhiyun static int uncached_access(struct file *file, phys_addr_t addr)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun #if defined(CONFIG_IA64)
292*4882a593Smuzhiyun 	/*
293*4882a593Smuzhiyun 	 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
294*4882a593Smuzhiyun 	 * attribute aliases.
295*4882a593Smuzhiyun 	 */
296*4882a593Smuzhiyun 	return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
297*4882a593Smuzhiyun #elif defined(CONFIG_MIPS)
298*4882a593Smuzhiyun 	{
299*4882a593Smuzhiyun 		extern int __uncached_access(struct file *file,
300*4882a593Smuzhiyun 					     unsigned long addr);
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 		return __uncached_access(file, addr);
303*4882a593Smuzhiyun 	}
304*4882a593Smuzhiyun #else
305*4882a593Smuzhiyun 	/*
306*4882a593Smuzhiyun 	 * Accessing memory above the top the kernel knows about or through a
307*4882a593Smuzhiyun 	 * file pointer
308*4882a593Smuzhiyun 	 * that was marked O_DSYNC will be done non-cached.
309*4882a593Smuzhiyun 	 */
310*4882a593Smuzhiyun 	if (file->f_flags & O_DSYNC)
311*4882a593Smuzhiyun 		return 1;
312*4882a593Smuzhiyun 	return addr >= __pa(high_memory);
313*4882a593Smuzhiyun #endif
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun #endif
316*4882a593Smuzhiyun 
phys_mem_access_prot(struct file * file,unsigned long pfn,unsigned long size,pgprot_t vma_prot)317*4882a593Smuzhiyun static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
318*4882a593Smuzhiyun 				     unsigned long size, pgprot_t vma_prot)
319*4882a593Smuzhiyun {
320*4882a593Smuzhiyun #ifdef pgprot_noncached
321*4882a593Smuzhiyun 	phys_addr_t offset = pfn << PAGE_SHIFT;
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 	if (uncached_access(file, offset))
324*4882a593Smuzhiyun 		return pgprot_noncached(vma_prot);
325*4882a593Smuzhiyun #endif
326*4882a593Smuzhiyun 	return vma_prot;
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun #endif
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun #ifndef CONFIG_MMU
get_unmapped_area_mem(struct file * file,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)331*4882a593Smuzhiyun static unsigned long get_unmapped_area_mem(struct file *file,
332*4882a593Smuzhiyun 					   unsigned long addr,
333*4882a593Smuzhiyun 					   unsigned long len,
334*4882a593Smuzhiyun 					   unsigned long pgoff,
335*4882a593Smuzhiyun 					   unsigned long flags)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun 	if (!valid_mmap_phys_addr_range(pgoff, len))
338*4882a593Smuzhiyun 		return (unsigned long) -EINVAL;
339*4882a593Smuzhiyun 	return pgoff << PAGE_SHIFT;
340*4882a593Smuzhiyun }
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun /* permit direct mmap, for read, write or exec */
memory_mmap_capabilities(struct file * file)343*4882a593Smuzhiyun static unsigned memory_mmap_capabilities(struct file *file)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun 	return NOMMU_MAP_DIRECT |
346*4882a593Smuzhiyun 		NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC;
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun 
zero_mmap_capabilities(struct file * file)349*4882a593Smuzhiyun static unsigned zero_mmap_capabilities(struct file *file)
350*4882a593Smuzhiyun {
351*4882a593Smuzhiyun 	return NOMMU_MAP_COPY;
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun /* can't do an in-place private mapping if there's no MMU */
private_mapping_ok(struct vm_area_struct * vma)355*4882a593Smuzhiyun static inline int private_mapping_ok(struct vm_area_struct *vma)
356*4882a593Smuzhiyun {
357*4882a593Smuzhiyun 	return vma->vm_flags & VM_MAYSHARE;
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun #else
360*4882a593Smuzhiyun 
private_mapping_ok(struct vm_area_struct * vma)361*4882a593Smuzhiyun static inline int private_mapping_ok(struct vm_area_struct *vma)
362*4882a593Smuzhiyun {
363*4882a593Smuzhiyun 	return 1;
364*4882a593Smuzhiyun }
365*4882a593Smuzhiyun #endif
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun static const struct vm_operations_struct mmap_mem_ops = {
368*4882a593Smuzhiyun #ifdef CONFIG_HAVE_IOREMAP_PROT
369*4882a593Smuzhiyun 	.access = generic_access_phys
370*4882a593Smuzhiyun #endif
371*4882a593Smuzhiyun };
372*4882a593Smuzhiyun 
mmap_mem(struct file * file,struct vm_area_struct * vma)373*4882a593Smuzhiyun static int mmap_mem(struct file *file, struct vm_area_struct *vma)
374*4882a593Smuzhiyun {
375*4882a593Smuzhiyun 	size_t size = vma->vm_end - vma->vm_start;
376*4882a593Smuzhiyun 	phys_addr_t offset = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	/* Does it even fit in phys_addr_t? */
379*4882a593Smuzhiyun 	if (offset >> PAGE_SHIFT != vma->vm_pgoff)
380*4882a593Smuzhiyun 		return -EINVAL;
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	/* It's illegal to wrap around the end of the physical address space. */
383*4882a593Smuzhiyun 	if (offset + (phys_addr_t)size - 1 < offset)
384*4882a593Smuzhiyun 		return -EINVAL;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
387*4882a593Smuzhiyun 		return -EINVAL;
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	if (!private_mapping_ok(vma))
390*4882a593Smuzhiyun 		return -ENOSYS;
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	if (!range_is_allowed(vma->vm_pgoff, size))
393*4882a593Smuzhiyun 		return -EPERM;
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
396*4882a593Smuzhiyun 						&vma->vm_page_prot))
397*4882a593Smuzhiyun 		return -EINVAL;
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
400*4882a593Smuzhiyun 						 size,
401*4882a593Smuzhiyun 						 vma->vm_page_prot);
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	vma->vm_ops = &mmap_mem_ops;
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 	/* Remap-pfn-range will mark the range VM_IO */
406*4882a593Smuzhiyun 	if (remap_pfn_range(vma,
407*4882a593Smuzhiyun 			    vma->vm_start,
408*4882a593Smuzhiyun 			    vma->vm_pgoff,
409*4882a593Smuzhiyun 			    size,
410*4882a593Smuzhiyun 			    vma->vm_page_prot)) {
411*4882a593Smuzhiyun 		return -EAGAIN;
412*4882a593Smuzhiyun 	}
413*4882a593Smuzhiyun 	return 0;
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun 
mmap_kmem(struct file * file,struct vm_area_struct * vma)416*4882a593Smuzhiyun static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
417*4882a593Smuzhiyun {
418*4882a593Smuzhiyun 	unsigned long pfn;
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	/* Turn a kernel-virtual address into a physical page frame */
421*4882a593Smuzhiyun 	pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	/*
424*4882a593Smuzhiyun 	 * RED-PEN: on some architectures there is more mapped memory than
425*4882a593Smuzhiyun 	 * available in mem_map which pfn_valid checks for. Perhaps should add a
426*4882a593Smuzhiyun 	 * new macro here.
427*4882a593Smuzhiyun 	 *
428*4882a593Smuzhiyun 	 * RED-PEN: vmalloc is not supported right now.
429*4882a593Smuzhiyun 	 */
430*4882a593Smuzhiyun 	if (!pfn_valid(pfn))
431*4882a593Smuzhiyun 		return -EIO;
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	vma->vm_pgoff = pfn;
434*4882a593Smuzhiyun 	return mmap_mem(file, vma);
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun /*
438*4882a593Smuzhiyun  * This function reads the *virtual* memory as seen by the kernel.
439*4882a593Smuzhiyun  */
read_kmem(struct file * file,char __user * buf,size_t count,loff_t * ppos)440*4882a593Smuzhiyun static ssize_t read_kmem(struct file *file, char __user *buf,
441*4882a593Smuzhiyun 			 size_t count, loff_t *ppos)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun 	unsigned long p = *ppos;
444*4882a593Smuzhiyun 	ssize_t low_count, read, sz;
445*4882a593Smuzhiyun 	char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
446*4882a593Smuzhiyun 	int err = 0;
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	read = 0;
449*4882a593Smuzhiyun 	if (p < (unsigned long) high_memory) {
450*4882a593Smuzhiyun 		low_count = count;
451*4882a593Smuzhiyun 		if (count > (unsigned long)high_memory - p)
452*4882a593Smuzhiyun 			low_count = (unsigned long)high_memory - p;
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
455*4882a593Smuzhiyun 		/* we don't have page 0 mapped on sparc and m68k.. */
456*4882a593Smuzhiyun 		if (p < PAGE_SIZE && low_count > 0) {
457*4882a593Smuzhiyun 			sz = size_inside_page(p, low_count);
458*4882a593Smuzhiyun 			if (clear_user(buf, sz))
459*4882a593Smuzhiyun 				return -EFAULT;
460*4882a593Smuzhiyun 			buf += sz;
461*4882a593Smuzhiyun 			p += sz;
462*4882a593Smuzhiyun 			read += sz;
463*4882a593Smuzhiyun 			low_count -= sz;
464*4882a593Smuzhiyun 			count -= sz;
465*4882a593Smuzhiyun 		}
466*4882a593Smuzhiyun #endif
467*4882a593Smuzhiyun 		while (low_count > 0) {
468*4882a593Smuzhiyun 			sz = size_inside_page(p, low_count);
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun 			/*
471*4882a593Smuzhiyun 			 * On ia64 if a page has been mapped somewhere as
472*4882a593Smuzhiyun 			 * uncached, then it must also be accessed uncached
473*4882a593Smuzhiyun 			 * by the kernel or data corruption may occur
474*4882a593Smuzhiyun 			 */
475*4882a593Smuzhiyun 			kbuf = xlate_dev_kmem_ptr((void *)p);
476*4882a593Smuzhiyun 			if (!virt_addr_valid(kbuf))
477*4882a593Smuzhiyun 				return -ENXIO;
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 			if (copy_to_user(buf, kbuf, sz))
480*4882a593Smuzhiyun 				return -EFAULT;
481*4882a593Smuzhiyun 			buf += sz;
482*4882a593Smuzhiyun 			p += sz;
483*4882a593Smuzhiyun 			read += sz;
484*4882a593Smuzhiyun 			low_count -= sz;
485*4882a593Smuzhiyun 			count -= sz;
486*4882a593Smuzhiyun 			if (should_stop_iteration()) {
487*4882a593Smuzhiyun 				count = 0;
488*4882a593Smuzhiyun 				break;
489*4882a593Smuzhiyun 			}
490*4882a593Smuzhiyun 		}
491*4882a593Smuzhiyun 	}
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 	if (count > 0) {
494*4882a593Smuzhiyun 		kbuf = (char *)__get_free_page(GFP_KERNEL);
495*4882a593Smuzhiyun 		if (!kbuf)
496*4882a593Smuzhiyun 			return -ENOMEM;
497*4882a593Smuzhiyun 		while (count > 0) {
498*4882a593Smuzhiyun 			sz = size_inside_page(p, count);
499*4882a593Smuzhiyun 			if (!is_vmalloc_or_module_addr((void *)p)) {
500*4882a593Smuzhiyun 				err = -ENXIO;
501*4882a593Smuzhiyun 				break;
502*4882a593Smuzhiyun 			}
503*4882a593Smuzhiyun 			sz = vread(kbuf, (char *)p, sz);
504*4882a593Smuzhiyun 			if (!sz)
505*4882a593Smuzhiyun 				break;
506*4882a593Smuzhiyun 			if (copy_to_user(buf, kbuf, sz)) {
507*4882a593Smuzhiyun 				err = -EFAULT;
508*4882a593Smuzhiyun 				break;
509*4882a593Smuzhiyun 			}
510*4882a593Smuzhiyun 			count -= sz;
511*4882a593Smuzhiyun 			buf += sz;
512*4882a593Smuzhiyun 			read += sz;
513*4882a593Smuzhiyun 			p += sz;
514*4882a593Smuzhiyun 			if (should_stop_iteration())
515*4882a593Smuzhiyun 				break;
516*4882a593Smuzhiyun 		}
517*4882a593Smuzhiyun 		free_page((unsigned long)kbuf);
518*4882a593Smuzhiyun 	}
519*4882a593Smuzhiyun 	*ppos = p;
520*4882a593Smuzhiyun 	return read ? read : err;
521*4882a593Smuzhiyun }
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 
do_write_kmem(unsigned long p,const char __user * buf,size_t count,loff_t * ppos)524*4882a593Smuzhiyun static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
525*4882a593Smuzhiyun 				size_t count, loff_t *ppos)
526*4882a593Smuzhiyun {
527*4882a593Smuzhiyun 	ssize_t written, sz;
528*4882a593Smuzhiyun 	unsigned long copied;
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	written = 0;
531*4882a593Smuzhiyun #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
532*4882a593Smuzhiyun 	/* we don't have page 0 mapped on sparc and m68k.. */
533*4882a593Smuzhiyun 	if (p < PAGE_SIZE) {
534*4882a593Smuzhiyun 		sz = size_inside_page(p, count);
535*4882a593Smuzhiyun 		/* Hmm. Do something? */
536*4882a593Smuzhiyun 		buf += sz;
537*4882a593Smuzhiyun 		p += sz;
538*4882a593Smuzhiyun 		count -= sz;
539*4882a593Smuzhiyun 		written += sz;
540*4882a593Smuzhiyun 	}
541*4882a593Smuzhiyun #endif
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	while (count > 0) {
544*4882a593Smuzhiyun 		void *ptr;
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 		sz = size_inside_page(p, count);
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 		/*
549*4882a593Smuzhiyun 		 * On ia64 if a page has been mapped somewhere as uncached, then
550*4882a593Smuzhiyun 		 * it must also be accessed uncached by the kernel or data
551*4882a593Smuzhiyun 		 * corruption may occur.
552*4882a593Smuzhiyun 		 */
553*4882a593Smuzhiyun 		ptr = xlate_dev_kmem_ptr((void *)p);
554*4882a593Smuzhiyun 		if (!virt_addr_valid(ptr))
555*4882a593Smuzhiyun 			return -ENXIO;
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 		copied = copy_from_user(ptr, buf, sz);
558*4882a593Smuzhiyun 		if (copied) {
559*4882a593Smuzhiyun 			written += sz - copied;
560*4882a593Smuzhiyun 			if (written)
561*4882a593Smuzhiyun 				break;
562*4882a593Smuzhiyun 			return -EFAULT;
563*4882a593Smuzhiyun 		}
564*4882a593Smuzhiyun 		buf += sz;
565*4882a593Smuzhiyun 		p += sz;
566*4882a593Smuzhiyun 		count -= sz;
567*4882a593Smuzhiyun 		written += sz;
568*4882a593Smuzhiyun 		if (should_stop_iteration())
569*4882a593Smuzhiyun 			break;
570*4882a593Smuzhiyun 	}
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun 	*ppos += written;
573*4882a593Smuzhiyun 	return written;
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun /*
577*4882a593Smuzhiyun  * This function writes to the *virtual* memory as seen by the kernel.
578*4882a593Smuzhiyun  */
write_kmem(struct file * file,const char __user * buf,size_t count,loff_t * ppos)579*4882a593Smuzhiyun static ssize_t write_kmem(struct file *file, const char __user *buf,
580*4882a593Smuzhiyun 			  size_t count, loff_t *ppos)
581*4882a593Smuzhiyun {
582*4882a593Smuzhiyun 	unsigned long p = *ppos;
583*4882a593Smuzhiyun 	ssize_t wrote = 0;
584*4882a593Smuzhiyun 	ssize_t virtr = 0;
585*4882a593Smuzhiyun 	char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
586*4882a593Smuzhiyun 	int err = 0;
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	if (p < (unsigned long) high_memory) {
589*4882a593Smuzhiyun 		unsigned long to_write = min_t(unsigned long, count,
590*4882a593Smuzhiyun 					       (unsigned long)high_memory - p);
591*4882a593Smuzhiyun 		wrote = do_write_kmem(p, buf, to_write, ppos);
592*4882a593Smuzhiyun 		if (wrote != to_write)
593*4882a593Smuzhiyun 			return wrote;
594*4882a593Smuzhiyun 		p += wrote;
595*4882a593Smuzhiyun 		buf += wrote;
596*4882a593Smuzhiyun 		count -= wrote;
597*4882a593Smuzhiyun 	}
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 	if (count > 0) {
600*4882a593Smuzhiyun 		kbuf = (char *)__get_free_page(GFP_KERNEL);
601*4882a593Smuzhiyun 		if (!kbuf)
602*4882a593Smuzhiyun 			return wrote ? wrote : -ENOMEM;
603*4882a593Smuzhiyun 		while (count > 0) {
604*4882a593Smuzhiyun 			unsigned long sz = size_inside_page(p, count);
605*4882a593Smuzhiyun 			unsigned long n;
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 			if (!is_vmalloc_or_module_addr((void *)p)) {
608*4882a593Smuzhiyun 				err = -ENXIO;
609*4882a593Smuzhiyun 				break;
610*4882a593Smuzhiyun 			}
611*4882a593Smuzhiyun 			n = copy_from_user(kbuf, buf, sz);
612*4882a593Smuzhiyun 			if (n) {
613*4882a593Smuzhiyun 				err = -EFAULT;
614*4882a593Smuzhiyun 				break;
615*4882a593Smuzhiyun 			}
616*4882a593Smuzhiyun 			vwrite(kbuf, (char *)p, sz);
617*4882a593Smuzhiyun 			count -= sz;
618*4882a593Smuzhiyun 			buf += sz;
619*4882a593Smuzhiyun 			virtr += sz;
620*4882a593Smuzhiyun 			p += sz;
621*4882a593Smuzhiyun 			if (should_stop_iteration())
622*4882a593Smuzhiyun 				break;
623*4882a593Smuzhiyun 		}
624*4882a593Smuzhiyun 		free_page((unsigned long)kbuf);
625*4882a593Smuzhiyun 	}
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	*ppos = p;
628*4882a593Smuzhiyun 	return virtr + wrote ? : err;
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun 
read_port(struct file * file,char __user * buf,size_t count,loff_t * ppos)631*4882a593Smuzhiyun static ssize_t read_port(struct file *file, char __user *buf,
632*4882a593Smuzhiyun 			 size_t count, loff_t *ppos)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun 	unsigned long i = *ppos;
635*4882a593Smuzhiyun 	char __user *tmp = buf;
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 	if (!access_ok(buf, count))
638*4882a593Smuzhiyun 		return -EFAULT;
639*4882a593Smuzhiyun 	while (count-- > 0 && i < 65536) {
640*4882a593Smuzhiyun 		if (__put_user(inb(i), tmp) < 0)
641*4882a593Smuzhiyun 			return -EFAULT;
642*4882a593Smuzhiyun 		i++;
643*4882a593Smuzhiyun 		tmp++;
644*4882a593Smuzhiyun 	}
645*4882a593Smuzhiyun 	*ppos = i;
646*4882a593Smuzhiyun 	return tmp-buf;
647*4882a593Smuzhiyun }
648*4882a593Smuzhiyun 
write_port(struct file * file,const char __user * buf,size_t count,loff_t * ppos)649*4882a593Smuzhiyun static ssize_t write_port(struct file *file, const char __user *buf,
650*4882a593Smuzhiyun 			  size_t count, loff_t *ppos)
651*4882a593Smuzhiyun {
652*4882a593Smuzhiyun 	unsigned long i = *ppos;
653*4882a593Smuzhiyun 	const char __user *tmp = buf;
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 	if (!access_ok(buf, count))
656*4882a593Smuzhiyun 		return -EFAULT;
657*4882a593Smuzhiyun 	while (count-- > 0 && i < 65536) {
658*4882a593Smuzhiyun 		char c;
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun 		if (__get_user(c, tmp)) {
661*4882a593Smuzhiyun 			if (tmp > buf)
662*4882a593Smuzhiyun 				break;
663*4882a593Smuzhiyun 			return -EFAULT;
664*4882a593Smuzhiyun 		}
665*4882a593Smuzhiyun 		outb(c, i);
666*4882a593Smuzhiyun 		i++;
667*4882a593Smuzhiyun 		tmp++;
668*4882a593Smuzhiyun 	}
669*4882a593Smuzhiyun 	*ppos = i;
670*4882a593Smuzhiyun 	return tmp-buf;
671*4882a593Smuzhiyun }
672*4882a593Smuzhiyun 
read_null(struct file * file,char __user * buf,size_t count,loff_t * ppos)673*4882a593Smuzhiyun static ssize_t read_null(struct file *file, char __user *buf,
674*4882a593Smuzhiyun 			 size_t count, loff_t *ppos)
675*4882a593Smuzhiyun {
676*4882a593Smuzhiyun 	return 0;
677*4882a593Smuzhiyun }
678*4882a593Smuzhiyun 
write_null(struct file * file,const char __user * buf,size_t count,loff_t * ppos)679*4882a593Smuzhiyun static ssize_t write_null(struct file *file, const char __user *buf,
680*4882a593Smuzhiyun 			  size_t count, loff_t *ppos)
681*4882a593Smuzhiyun {
682*4882a593Smuzhiyun 	return count;
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun 
read_iter_null(struct kiocb * iocb,struct iov_iter * to)685*4882a593Smuzhiyun static ssize_t read_iter_null(struct kiocb *iocb, struct iov_iter *to)
686*4882a593Smuzhiyun {
687*4882a593Smuzhiyun 	return 0;
688*4882a593Smuzhiyun }
689*4882a593Smuzhiyun 
write_iter_null(struct kiocb * iocb,struct iov_iter * from)690*4882a593Smuzhiyun static ssize_t write_iter_null(struct kiocb *iocb, struct iov_iter *from)
691*4882a593Smuzhiyun {
692*4882a593Smuzhiyun 	size_t count = iov_iter_count(from);
693*4882a593Smuzhiyun 	iov_iter_advance(from, count);
694*4882a593Smuzhiyun 	return count;
695*4882a593Smuzhiyun }
696*4882a593Smuzhiyun 
pipe_to_null(struct pipe_inode_info * info,struct pipe_buffer * buf,struct splice_desc * sd)697*4882a593Smuzhiyun static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
698*4882a593Smuzhiyun 			struct splice_desc *sd)
699*4882a593Smuzhiyun {
700*4882a593Smuzhiyun 	return sd->len;
701*4882a593Smuzhiyun }
702*4882a593Smuzhiyun 
splice_write_null(struct pipe_inode_info * pipe,struct file * out,loff_t * ppos,size_t len,unsigned int flags)703*4882a593Smuzhiyun static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
704*4882a593Smuzhiyun 				 loff_t *ppos, size_t len, unsigned int flags)
705*4882a593Smuzhiyun {
706*4882a593Smuzhiyun 	return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
707*4882a593Smuzhiyun }
708*4882a593Smuzhiyun 
read_iter_zero(struct kiocb * iocb,struct iov_iter * iter)709*4882a593Smuzhiyun static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
710*4882a593Smuzhiyun {
711*4882a593Smuzhiyun 	size_t written = 0;
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 	while (iov_iter_count(iter)) {
714*4882a593Smuzhiyun 		size_t chunk = iov_iter_count(iter), n;
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 		if (chunk > PAGE_SIZE)
717*4882a593Smuzhiyun 			chunk = PAGE_SIZE;	/* Just for latency reasons */
718*4882a593Smuzhiyun 		n = iov_iter_zero(chunk, iter);
719*4882a593Smuzhiyun 		if (!n && iov_iter_count(iter))
720*4882a593Smuzhiyun 			return written ? written : -EFAULT;
721*4882a593Smuzhiyun 		written += n;
722*4882a593Smuzhiyun 		if (signal_pending(current))
723*4882a593Smuzhiyun 			return written ? written : -ERESTARTSYS;
724*4882a593Smuzhiyun 		cond_resched();
725*4882a593Smuzhiyun 	}
726*4882a593Smuzhiyun 	return written;
727*4882a593Smuzhiyun }
728*4882a593Smuzhiyun 
read_zero(struct file * file,char __user * buf,size_t count,loff_t * ppos)729*4882a593Smuzhiyun static ssize_t read_zero(struct file *file, char __user *buf,
730*4882a593Smuzhiyun 			 size_t count, loff_t *ppos)
731*4882a593Smuzhiyun {
732*4882a593Smuzhiyun 	size_t cleared = 0;
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	while (count) {
735*4882a593Smuzhiyun 		size_t chunk = min_t(size_t, count, PAGE_SIZE);
736*4882a593Smuzhiyun 		size_t left;
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 		left = clear_user(buf + cleared, chunk);
739*4882a593Smuzhiyun 		if (unlikely(left)) {
740*4882a593Smuzhiyun 			cleared += (chunk - left);
741*4882a593Smuzhiyun 			if (!cleared)
742*4882a593Smuzhiyun 				return -EFAULT;
743*4882a593Smuzhiyun 			break;
744*4882a593Smuzhiyun 		}
745*4882a593Smuzhiyun 		cleared += chunk;
746*4882a593Smuzhiyun 		count -= chunk;
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 		if (signal_pending(current))
749*4882a593Smuzhiyun 			break;
750*4882a593Smuzhiyun 		cond_resched();
751*4882a593Smuzhiyun 	}
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 	return cleared;
754*4882a593Smuzhiyun }
755*4882a593Smuzhiyun 
mmap_zero(struct file * file,struct vm_area_struct * vma)756*4882a593Smuzhiyun static int mmap_zero(struct file *file, struct vm_area_struct *vma)
757*4882a593Smuzhiyun {
758*4882a593Smuzhiyun #ifndef CONFIG_MMU
759*4882a593Smuzhiyun 	return -ENOSYS;
760*4882a593Smuzhiyun #endif
761*4882a593Smuzhiyun 	if (vma->vm_flags & VM_SHARED)
762*4882a593Smuzhiyun 		return shmem_zero_setup(vma);
763*4882a593Smuzhiyun 	vma_set_anonymous(vma);
764*4882a593Smuzhiyun 	return 0;
765*4882a593Smuzhiyun }
766*4882a593Smuzhiyun 
get_unmapped_area_zero(struct file * file,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)767*4882a593Smuzhiyun static unsigned long get_unmapped_area_zero(struct file *file,
768*4882a593Smuzhiyun 				unsigned long addr, unsigned long len,
769*4882a593Smuzhiyun 				unsigned long pgoff, unsigned long flags)
770*4882a593Smuzhiyun {
771*4882a593Smuzhiyun #ifdef CONFIG_MMU
772*4882a593Smuzhiyun 	if (flags & MAP_SHARED) {
773*4882a593Smuzhiyun 		/*
774*4882a593Smuzhiyun 		 * mmap_zero() will call shmem_zero_setup() to create a file,
775*4882a593Smuzhiyun 		 * so use shmem's get_unmapped_area in case it can be huge;
776*4882a593Smuzhiyun 		 * and pass NULL for file as in mmap.c's get_unmapped_area(),
777*4882a593Smuzhiyun 		 * so as not to confuse shmem with our handle on "/dev/zero".
778*4882a593Smuzhiyun 		 */
779*4882a593Smuzhiyun 		return shmem_get_unmapped_area(NULL, addr, len, pgoff, flags);
780*4882a593Smuzhiyun 	}
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	/* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
783*4882a593Smuzhiyun 	return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
784*4882a593Smuzhiyun #else
785*4882a593Smuzhiyun 	return -ENOSYS;
786*4882a593Smuzhiyun #endif
787*4882a593Smuzhiyun }
788*4882a593Smuzhiyun 
write_full(struct file * file,const char __user * buf,size_t count,loff_t * ppos)789*4882a593Smuzhiyun static ssize_t write_full(struct file *file, const char __user *buf,
790*4882a593Smuzhiyun 			  size_t count, loff_t *ppos)
791*4882a593Smuzhiyun {
792*4882a593Smuzhiyun 	return -ENOSPC;
793*4882a593Smuzhiyun }
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun /*
796*4882a593Smuzhiyun  * Special lseek() function for /dev/null and /dev/zero.  Most notably, you
797*4882a593Smuzhiyun  * can fopen() both devices with "a" now.  This was previously impossible.
798*4882a593Smuzhiyun  * -- SRB.
799*4882a593Smuzhiyun  */
null_lseek(struct file * file,loff_t offset,int orig)800*4882a593Smuzhiyun static loff_t null_lseek(struct file *file, loff_t offset, int orig)
801*4882a593Smuzhiyun {
802*4882a593Smuzhiyun 	return file->f_pos = 0;
803*4882a593Smuzhiyun }
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun /*
806*4882a593Smuzhiyun  * The memory devices use the full 32/64 bits of the offset, and so we cannot
807*4882a593Smuzhiyun  * check against negative addresses: they are ok. The return value is weird,
808*4882a593Smuzhiyun  * though, in that case (0).
809*4882a593Smuzhiyun  *
810*4882a593Smuzhiyun  * also note that seeking relative to the "end of file" isn't supported:
811*4882a593Smuzhiyun  * it has no meaning, so it returns -EINVAL.
812*4882a593Smuzhiyun  */
memory_lseek(struct file * file,loff_t offset,int orig)813*4882a593Smuzhiyun static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
814*4882a593Smuzhiyun {
815*4882a593Smuzhiyun 	loff_t ret;
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 	inode_lock(file_inode(file));
818*4882a593Smuzhiyun 	switch (orig) {
819*4882a593Smuzhiyun 	case SEEK_CUR:
820*4882a593Smuzhiyun 		offset += file->f_pos;
821*4882a593Smuzhiyun 		fallthrough;
822*4882a593Smuzhiyun 	case SEEK_SET:
823*4882a593Smuzhiyun 		/* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
824*4882a593Smuzhiyun 		if ((unsigned long long)offset >= -MAX_ERRNO) {
825*4882a593Smuzhiyun 			ret = -EOVERFLOW;
826*4882a593Smuzhiyun 			break;
827*4882a593Smuzhiyun 		}
828*4882a593Smuzhiyun 		file->f_pos = offset;
829*4882a593Smuzhiyun 		ret = file->f_pos;
830*4882a593Smuzhiyun 		force_successful_syscall_return();
831*4882a593Smuzhiyun 		break;
832*4882a593Smuzhiyun 	default:
833*4882a593Smuzhiyun 		ret = -EINVAL;
834*4882a593Smuzhiyun 	}
835*4882a593Smuzhiyun 	inode_unlock(file_inode(file));
836*4882a593Smuzhiyun 	return ret;
837*4882a593Smuzhiyun }
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun static struct inode *devmem_inode;
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun #ifdef CONFIG_IO_STRICT_DEVMEM
revoke_devmem(struct resource * res)842*4882a593Smuzhiyun void revoke_devmem(struct resource *res)
843*4882a593Smuzhiyun {
844*4882a593Smuzhiyun 	/* pairs with smp_store_release() in devmem_init_inode() */
845*4882a593Smuzhiyun 	struct inode *inode = smp_load_acquire(&devmem_inode);
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 	/*
848*4882a593Smuzhiyun 	 * Check that the initialization has completed. Losing the race
849*4882a593Smuzhiyun 	 * is ok because it means drivers are claiming resources before
850*4882a593Smuzhiyun 	 * the fs_initcall level of init and prevent /dev/mem from
851*4882a593Smuzhiyun 	 * establishing mappings.
852*4882a593Smuzhiyun 	 */
853*4882a593Smuzhiyun 	if (!inode)
854*4882a593Smuzhiyun 		return;
855*4882a593Smuzhiyun 
856*4882a593Smuzhiyun 	/*
857*4882a593Smuzhiyun 	 * The expectation is that the driver has successfully marked
858*4882a593Smuzhiyun 	 * the resource busy by this point, so devmem_is_allowed()
859*4882a593Smuzhiyun 	 * should start returning false, however for performance this
860*4882a593Smuzhiyun 	 * does not iterate the entire resource range.
861*4882a593Smuzhiyun 	 */
862*4882a593Smuzhiyun 	if (devmem_is_allowed(PHYS_PFN(res->start)) &&
863*4882a593Smuzhiyun 	    devmem_is_allowed(PHYS_PFN(res->end))) {
864*4882a593Smuzhiyun 		/*
865*4882a593Smuzhiyun 		 * *cringe* iomem=relaxed says "go ahead, what's the
866*4882a593Smuzhiyun 		 * worst that can happen?"
867*4882a593Smuzhiyun 		 */
868*4882a593Smuzhiyun 		return;
869*4882a593Smuzhiyun 	}
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	unmap_mapping_range(inode->i_mapping, res->start, resource_size(res), 1);
872*4882a593Smuzhiyun }
873*4882a593Smuzhiyun #endif
874*4882a593Smuzhiyun 
open_port(struct inode * inode,struct file * filp)875*4882a593Smuzhiyun static int open_port(struct inode *inode, struct file *filp)
876*4882a593Smuzhiyun {
877*4882a593Smuzhiyun 	int rc;
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun 	if (!capable(CAP_SYS_RAWIO))
880*4882a593Smuzhiyun 		return -EPERM;
881*4882a593Smuzhiyun 
882*4882a593Smuzhiyun 	rc = security_locked_down(LOCKDOWN_DEV_MEM);
883*4882a593Smuzhiyun 	if (rc)
884*4882a593Smuzhiyun 		return rc;
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 	if (iminor(inode) != DEVMEM_MINOR)
887*4882a593Smuzhiyun 		return 0;
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun 	/*
890*4882a593Smuzhiyun 	 * Use a unified address space to have a single point to manage
891*4882a593Smuzhiyun 	 * revocations when drivers want to take over a /dev/mem mapped
892*4882a593Smuzhiyun 	 * range.
893*4882a593Smuzhiyun 	 */
894*4882a593Smuzhiyun 	inode->i_mapping = devmem_inode->i_mapping;
895*4882a593Smuzhiyun 	filp->f_mapping = inode->i_mapping;
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 	return 0;
898*4882a593Smuzhiyun }
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun #define zero_lseek	null_lseek
901*4882a593Smuzhiyun #define full_lseek      null_lseek
902*4882a593Smuzhiyun #define write_zero	write_null
903*4882a593Smuzhiyun #define write_iter_zero	write_iter_null
904*4882a593Smuzhiyun #define open_mem	open_port
905*4882a593Smuzhiyun #define open_kmem	open_mem
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun static const struct file_operations __maybe_unused mem_fops = {
908*4882a593Smuzhiyun 	.llseek		= memory_lseek,
909*4882a593Smuzhiyun 	.read		= read_mem,
910*4882a593Smuzhiyun 	.write		= write_mem,
911*4882a593Smuzhiyun 	.mmap		= mmap_mem,
912*4882a593Smuzhiyun 	.open		= open_mem,
913*4882a593Smuzhiyun #ifndef CONFIG_MMU
914*4882a593Smuzhiyun 	.get_unmapped_area = get_unmapped_area_mem,
915*4882a593Smuzhiyun 	.mmap_capabilities = memory_mmap_capabilities,
916*4882a593Smuzhiyun #endif
917*4882a593Smuzhiyun };
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun static const struct file_operations __maybe_unused kmem_fops = {
920*4882a593Smuzhiyun 	.llseek		= memory_lseek,
921*4882a593Smuzhiyun 	.read		= read_kmem,
922*4882a593Smuzhiyun 	.write		= write_kmem,
923*4882a593Smuzhiyun 	.mmap		= mmap_kmem,
924*4882a593Smuzhiyun 	.open		= open_kmem,
925*4882a593Smuzhiyun #ifndef CONFIG_MMU
926*4882a593Smuzhiyun 	.get_unmapped_area = get_unmapped_area_mem,
927*4882a593Smuzhiyun 	.mmap_capabilities = memory_mmap_capabilities,
928*4882a593Smuzhiyun #endif
929*4882a593Smuzhiyun };
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun static const struct file_operations null_fops = {
932*4882a593Smuzhiyun 	.llseek		= null_lseek,
933*4882a593Smuzhiyun 	.read		= read_null,
934*4882a593Smuzhiyun 	.write		= write_null,
935*4882a593Smuzhiyun 	.read_iter	= read_iter_null,
936*4882a593Smuzhiyun 	.write_iter	= write_iter_null,
937*4882a593Smuzhiyun 	.splice_write	= splice_write_null,
938*4882a593Smuzhiyun };
939*4882a593Smuzhiyun 
940*4882a593Smuzhiyun static const struct file_operations __maybe_unused port_fops = {
941*4882a593Smuzhiyun 	.llseek		= memory_lseek,
942*4882a593Smuzhiyun 	.read		= read_port,
943*4882a593Smuzhiyun 	.write		= write_port,
944*4882a593Smuzhiyun 	.open		= open_port,
945*4882a593Smuzhiyun };
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun static const struct file_operations zero_fops = {
948*4882a593Smuzhiyun 	.llseek		= zero_lseek,
949*4882a593Smuzhiyun 	.write		= write_zero,
950*4882a593Smuzhiyun 	.read_iter	= read_iter_zero,
951*4882a593Smuzhiyun 	.read		= read_zero,
952*4882a593Smuzhiyun 	.write_iter	= write_iter_zero,
953*4882a593Smuzhiyun 	.mmap		= mmap_zero,
954*4882a593Smuzhiyun 	.get_unmapped_area = get_unmapped_area_zero,
955*4882a593Smuzhiyun #ifndef CONFIG_MMU
956*4882a593Smuzhiyun 	.mmap_capabilities = zero_mmap_capabilities,
957*4882a593Smuzhiyun #endif
958*4882a593Smuzhiyun };
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun static const struct file_operations full_fops = {
961*4882a593Smuzhiyun 	.llseek		= full_lseek,
962*4882a593Smuzhiyun 	.read_iter	= read_iter_zero,
963*4882a593Smuzhiyun 	.write		= write_full,
964*4882a593Smuzhiyun };
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun static const struct memdev {
967*4882a593Smuzhiyun 	const char *name;
968*4882a593Smuzhiyun 	umode_t mode;
969*4882a593Smuzhiyun 	const struct file_operations *fops;
970*4882a593Smuzhiyun 	fmode_t fmode;
971*4882a593Smuzhiyun } devlist[] = {
972*4882a593Smuzhiyun #ifdef CONFIG_DEVMEM
973*4882a593Smuzhiyun 	 [DEVMEM_MINOR] = { "mem", 0, &mem_fops, FMODE_UNSIGNED_OFFSET },
974*4882a593Smuzhiyun #endif
975*4882a593Smuzhiyun #ifdef CONFIG_DEVKMEM
976*4882a593Smuzhiyun 	 [2] = { "kmem", 0, &kmem_fops, FMODE_UNSIGNED_OFFSET },
977*4882a593Smuzhiyun #endif
978*4882a593Smuzhiyun 	 [3] = { "null", 0666, &null_fops, 0 },
979*4882a593Smuzhiyun #ifdef CONFIG_DEVPORT
980*4882a593Smuzhiyun 	 [4] = { "port", 0, &port_fops, 0 },
981*4882a593Smuzhiyun #endif
982*4882a593Smuzhiyun 	 [5] = { "zero", 0666, &zero_fops, 0 },
983*4882a593Smuzhiyun 	 [7] = { "full", 0666, &full_fops, 0 },
984*4882a593Smuzhiyun 	 [8] = { "random", 0666, &random_fops, FMODE_NOWAIT },
985*4882a593Smuzhiyun 	 [9] = { "urandom", 0666, &urandom_fops, FMODE_NOWAIT },
986*4882a593Smuzhiyun #ifdef CONFIG_PRINTK
987*4882a593Smuzhiyun 	[11] = { "kmsg", 0644, &kmsg_fops, 0 },
988*4882a593Smuzhiyun #endif
989*4882a593Smuzhiyun };
990*4882a593Smuzhiyun 
memory_open(struct inode * inode,struct file * filp)991*4882a593Smuzhiyun static int memory_open(struct inode *inode, struct file *filp)
992*4882a593Smuzhiyun {
993*4882a593Smuzhiyun 	int minor;
994*4882a593Smuzhiyun 	const struct memdev *dev;
995*4882a593Smuzhiyun 
996*4882a593Smuzhiyun 	minor = iminor(inode);
997*4882a593Smuzhiyun 	if (minor >= ARRAY_SIZE(devlist))
998*4882a593Smuzhiyun 		return -ENXIO;
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun 	dev = &devlist[minor];
1001*4882a593Smuzhiyun 	if (!dev->fops)
1002*4882a593Smuzhiyun 		return -ENXIO;
1003*4882a593Smuzhiyun 
1004*4882a593Smuzhiyun 	filp->f_op = dev->fops;
1005*4882a593Smuzhiyun 	filp->f_mode |= dev->fmode;
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 	if (dev->fops->open)
1008*4882a593Smuzhiyun 		return dev->fops->open(inode, filp);
1009*4882a593Smuzhiyun 
1010*4882a593Smuzhiyun 	return 0;
1011*4882a593Smuzhiyun }
1012*4882a593Smuzhiyun 
1013*4882a593Smuzhiyun static const struct file_operations memory_fops = {
1014*4882a593Smuzhiyun 	.open = memory_open,
1015*4882a593Smuzhiyun 	.llseek = noop_llseek,
1016*4882a593Smuzhiyun };
1017*4882a593Smuzhiyun 
mem_devnode(struct device * dev,umode_t * mode)1018*4882a593Smuzhiyun static char *mem_devnode(struct device *dev, umode_t *mode)
1019*4882a593Smuzhiyun {
1020*4882a593Smuzhiyun 	if (mode && devlist[MINOR(dev->devt)].mode)
1021*4882a593Smuzhiyun 		*mode = devlist[MINOR(dev->devt)].mode;
1022*4882a593Smuzhiyun 	return NULL;
1023*4882a593Smuzhiyun }
1024*4882a593Smuzhiyun 
1025*4882a593Smuzhiyun static struct class *mem_class;
1026*4882a593Smuzhiyun 
devmem_fs_init_fs_context(struct fs_context * fc)1027*4882a593Smuzhiyun static int devmem_fs_init_fs_context(struct fs_context *fc)
1028*4882a593Smuzhiyun {
1029*4882a593Smuzhiyun 	return init_pseudo(fc, DEVMEM_MAGIC) ? 0 : -ENOMEM;
1030*4882a593Smuzhiyun }
1031*4882a593Smuzhiyun 
1032*4882a593Smuzhiyun static struct file_system_type devmem_fs_type = {
1033*4882a593Smuzhiyun 	.name		= "devmem",
1034*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
1035*4882a593Smuzhiyun 	.init_fs_context = devmem_fs_init_fs_context,
1036*4882a593Smuzhiyun 	.kill_sb	= kill_anon_super,
1037*4882a593Smuzhiyun };
1038*4882a593Smuzhiyun 
devmem_init_inode(void)1039*4882a593Smuzhiyun static int devmem_init_inode(void)
1040*4882a593Smuzhiyun {
1041*4882a593Smuzhiyun 	static struct vfsmount *devmem_vfs_mount;
1042*4882a593Smuzhiyun 	static int devmem_fs_cnt;
1043*4882a593Smuzhiyun 	struct inode *inode;
1044*4882a593Smuzhiyun 	int rc;
1045*4882a593Smuzhiyun 
1046*4882a593Smuzhiyun 	rc = simple_pin_fs(&devmem_fs_type, &devmem_vfs_mount, &devmem_fs_cnt);
1047*4882a593Smuzhiyun 	if (rc < 0) {
1048*4882a593Smuzhiyun 		pr_err("Cannot mount /dev/mem pseudo filesystem: %d\n", rc);
1049*4882a593Smuzhiyun 		return rc;
1050*4882a593Smuzhiyun 	}
1051*4882a593Smuzhiyun 
1052*4882a593Smuzhiyun 	inode = alloc_anon_inode(devmem_vfs_mount->mnt_sb);
1053*4882a593Smuzhiyun 	if (IS_ERR(inode)) {
1054*4882a593Smuzhiyun 		rc = PTR_ERR(inode);
1055*4882a593Smuzhiyun 		pr_err("Cannot allocate inode for /dev/mem: %d\n", rc);
1056*4882a593Smuzhiyun 		simple_release_fs(&devmem_vfs_mount, &devmem_fs_cnt);
1057*4882a593Smuzhiyun 		return rc;
1058*4882a593Smuzhiyun 	}
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun 	/*
1061*4882a593Smuzhiyun 	 * Publish /dev/mem initialized.
1062*4882a593Smuzhiyun 	 * Pairs with smp_load_acquire() in revoke_devmem().
1063*4882a593Smuzhiyun 	 */
1064*4882a593Smuzhiyun 	smp_store_release(&devmem_inode, inode);
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun 	return 0;
1067*4882a593Smuzhiyun }
1068*4882a593Smuzhiyun 
chr_dev_init(void)1069*4882a593Smuzhiyun static int __init chr_dev_init(void)
1070*4882a593Smuzhiyun {
1071*4882a593Smuzhiyun 	int minor;
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun 	if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
1074*4882a593Smuzhiyun 		printk("unable to get major %d for memory devs\n", MEM_MAJOR);
1075*4882a593Smuzhiyun 
1076*4882a593Smuzhiyun 	mem_class = class_create(THIS_MODULE, "mem");
1077*4882a593Smuzhiyun 	if (IS_ERR(mem_class))
1078*4882a593Smuzhiyun 		return PTR_ERR(mem_class);
1079*4882a593Smuzhiyun 
1080*4882a593Smuzhiyun 	mem_class->devnode = mem_devnode;
1081*4882a593Smuzhiyun 	for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
1082*4882a593Smuzhiyun 		if (!devlist[minor].name)
1083*4882a593Smuzhiyun 			continue;
1084*4882a593Smuzhiyun 
1085*4882a593Smuzhiyun 		/*
1086*4882a593Smuzhiyun 		 * Create /dev/port?
1087*4882a593Smuzhiyun 		 */
1088*4882a593Smuzhiyun 		if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
1089*4882a593Smuzhiyun 			continue;
1090*4882a593Smuzhiyun 		if ((minor == DEVMEM_MINOR) && devmem_init_inode() != 0)
1091*4882a593Smuzhiyun 			continue;
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun 		device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
1094*4882a593Smuzhiyun 			      NULL, devlist[minor].name);
1095*4882a593Smuzhiyun 	}
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun 	return tty_init();
1098*4882a593Smuzhiyun }
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun fs_initcall(chr_dev_init);
1101