xref: /openbmc/linux/arch/arm64/mm/mmap.c (revision 6d8e62c3)
1 /*
2  * Based on arch/arm/mm/mmap.c
3  *
4  * Copyright (C) 2012 ARM Ltd.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <linux/elf.h>
20 #include <linux/fs.h>
21 #include <linux/mm.h>
22 #include <linux/mman.h>
23 #include <linux/export.h>
24 #include <linux/shm.h>
25 #include <linux/sched.h>
26 #include <linux/io.h>
27 #include <linux/personality.h>
28 #include <linux/random.h>
29 
30 #include <asm/cputype.h>
31 
32 /*
33  * Leave enough space between the mmap area and the stack to honour ulimit in
34  * the face of randomisation.
35  */
36 #define MIN_GAP (SZ_128M + ((STACK_RND_MASK << PAGE_SHIFT) + 1))
37 #define MAX_GAP	(STACK_TOP/6*5)
38 
39 static int mmap_is_legacy(void)
40 {
41 	if (current->personality & ADDR_COMPAT_LAYOUT)
42 		return 1;
43 
44 	if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
45 		return 1;
46 
47 	return sysctl_legacy_va_layout;
48 }
49 
50 static unsigned long mmap_rnd(void)
51 {
52 	unsigned long rnd = 0;
53 
54 	if (current->flags & PF_RANDOMIZE)
55 		rnd = (long)get_random_int() & STACK_RND_MASK;
56 
57 	return rnd << PAGE_SHIFT;
58 }
59 
60 static unsigned long mmap_base(void)
61 {
62 	unsigned long gap = rlimit(RLIMIT_STACK);
63 
64 	if (gap < MIN_GAP)
65 		gap = MIN_GAP;
66 	else if (gap > MAX_GAP)
67 		gap = MAX_GAP;
68 
69 	return PAGE_ALIGN(STACK_TOP - gap - mmap_rnd());
70 }
71 
72 /*
73  * This function, called very early during the creation of a new process VM
74  * image, sets up which VM layout function to use:
75  */
76 void arch_pick_mmap_layout(struct mm_struct *mm)
77 {
78 	/*
79 	 * Fall back to the standard layout if the personality bit is set, or
80 	 * if the expected stack growth is unlimited:
81 	 */
82 	if (mmap_is_legacy()) {
83 		mm->mmap_base = TASK_UNMAPPED_BASE;
84 		mm->get_unmapped_area = arch_get_unmapped_area;
85 	} else {
86 		mm->mmap_base = mmap_base();
87 		mm->get_unmapped_area = arch_get_unmapped_area_topdown;
88 	}
89 }
90 EXPORT_SYMBOL_GPL(arch_pick_mmap_layout);
91 
92 
93 /*
94  * You really shouldn't be using read() or write() on /dev/mem.  This might go
95  * away in the future.
96  */
97 int valid_phys_addr_range(phys_addr_t addr, size_t size)
98 {
99 	if (addr < PHYS_OFFSET)
100 		return 0;
101 	if (addr + size > __pa(high_memory - 1) + 1)
102 		return 0;
103 
104 	return 1;
105 }
106 
107 /*
108  * Do not allow /dev/mem mappings beyond the supported physical range.
109  */
110 int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
111 {
112 	return !(((pfn << PAGE_SHIFT) + size) & ~PHYS_MASK);
113 }
114 
115 #ifdef CONFIG_STRICT_DEVMEM
116 
117 #include <linux/ioport.h>
118 
119 /*
120  * devmem_is_allowed() checks to see if /dev/mem access to a certain address
121  * is valid. The argument is a physical page number.  We mimic x86 here by
122  * disallowing access to system RAM as well as device-exclusive MMIO regions.
123  * This effectively disable read()/write() on /dev/mem.
124  */
125 int devmem_is_allowed(unsigned long pfn)
126 {
127 	if (iomem_is_exclusive(pfn << PAGE_SHIFT))
128 		return 0;
129 	if (!page_is_ram(pfn))
130 		return 1;
131 	return 0;
132 }
133 
134 #endif
135