]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/arm/mm/mmap.c
Merge remote-tracking branch 'regulator/fix/max77802' into regulator-linus
[mirror_ubuntu-artful-kernel.git] / arch / arm / mm / mmap.c
CommitLineData
1da177e4
LT
1/*
2 * linux/arch/arm/mm/mmap.c
3 */
1da177e4
LT
4#include <linux/fs.h>
5#include <linux/mm.h>
6#include <linux/mman.h>
7#include <linux/shm.h>
3f07c014 8#include <linux/sched/signal.h>
01042607 9#include <linux/sched/mm.h>
09d9bae0 10#include <linux/io.h>
df5419a9 11#include <linux/personality.h>
cc92c28b 12#include <linux/random.h>
41dfaa93 13#include <asm/cachetype.h>
1da177e4
LT
14
15#define COLOUR_ALIGN(addr,pgoff) \
16 ((((addr)+SHMLBA-1)&~(SHMLBA-1)) + \
17 (((pgoff)<<PAGE_SHIFT) & (SHMLBA-1)))
18
7dbaa466
RH
19/* gap between mmap and stack */
20#define MIN_GAP (128*1024*1024UL)
21#define MAX_GAP ((TASK_SIZE)/6*5)
22
23static int mmap_is_legacy(void)
24{
25 if (current->personality & ADDR_COMPAT_LAYOUT)
26 return 1;
27
28 if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
29 return 1;
30
31 return sysctl_legacy_va_layout;
32}
33
34static unsigned long mmap_base(unsigned long rnd)
35{
36 unsigned long gap = rlimit(RLIMIT_STACK);
37
38 if (gap < MIN_GAP)
39 gap = MIN_GAP;
40 else if (gap > MAX_GAP)
41 gap = MAX_GAP;
42
43 return PAGE_ALIGN(TASK_SIZE - gap - rnd);
44}
45
1da177e4
LT
46/*
47 * We need to ensure that shared mappings are correctly aligned to
48 * avoid aliasing issues with VIPT caches. We need to ensure that
49 * a specific page of an object is always mapped at a multiple of
50 * SHMLBA bytes.
51 *
52 * We unconditionally provide this function for all cases, however
53 * in the VIVT case, we optimise out the alignment rules.
54 */
55unsigned long
56arch_get_unmapped_area(struct file *filp, unsigned long addr,
57 unsigned long len, unsigned long pgoff, unsigned long flags)
58{
59 struct mm_struct *mm = current->mm;
60 struct vm_area_struct *vma;
41dfaa93
RH
61 int do_align = 0;
62 int aliasing = cache_is_vipt_aliasing();
394ef640 63 struct vm_unmapped_area_info info;
1da177e4
LT
64
65 /*
66 * We only need to do colour alignment if either the I or D
41dfaa93 67 * caches alias.
1da177e4 68 */
41dfaa93
RH
69 if (aliasing)
70 do_align = filp || (flags & MAP_SHARED);
1da177e4
LT
71
72 /*
acec0ac0 73 * We enforce the MAP_FIXED case.
1da177e4
LT
74 */
75 if (flags & MAP_FIXED) {
e77414e0
AV
76 if (aliasing && flags & MAP_SHARED &&
77 (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1))
1da177e4
LT
78 return -EINVAL;
79 return addr;
80 }
81
82 if (len > TASK_SIZE)
83 return -ENOMEM;
84
85 if (addr) {
86 if (do_align)
87 addr = COLOUR_ALIGN(addr, pgoff);
88 else
89 addr = PAGE_ALIGN(addr);
90
91 vma = find_vma(mm, addr);
92 if (TASK_SIZE - len >= addr &&
1be7107f 93 (!vma || addr + len <= vm_start_gap(vma)))
1da177e4
LT
94 return addr;
95 }
1da177e4 96
394ef640
ML
97 info.flags = 0;
98 info.length = len;
99 info.low_limit = mm->mmap_base;
100 info.high_limit = TASK_SIZE;
101 info.align_mask = do_align ? (PAGE_MASK & (SHMLBA - 1)) : 0;
102 info.align_offset = pgoff << PAGE_SHIFT;
103 return vm_unmapped_area(&info);
1da177e4
LT
104}
105
7dbaa466
RH
106unsigned long
107arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
108 const unsigned long len, const unsigned long pgoff,
109 const unsigned long flags)
110{
111 struct vm_area_struct *vma;
112 struct mm_struct *mm = current->mm;
113 unsigned long addr = addr0;
114 int do_align = 0;
115 int aliasing = cache_is_vipt_aliasing();
394ef640 116 struct vm_unmapped_area_info info;
7dbaa466
RH
117
118 /*
119 * We only need to do colour alignment if either the I or D
120 * caches alias.
121 */
122 if (aliasing)
123 do_align = filp || (flags & MAP_SHARED);
124
125 /* requested length too big for entire address space */
126 if (len > TASK_SIZE)
127 return -ENOMEM;
128
129 if (flags & MAP_FIXED) {
130 if (aliasing && flags & MAP_SHARED &&
131 (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1))
132 return -EINVAL;
133 return addr;
134 }
135
136 /* requesting a specific address */
137 if (addr) {
138 if (do_align)
139 addr = COLOUR_ALIGN(addr, pgoff);
140 else
141 addr = PAGE_ALIGN(addr);
142 vma = find_vma(mm, addr);
143 if (TASK_SIZE - len >= addr &&
1be7107f 144 (!vma || addr + len <= vm_start_gap(vma)))
7dbaa466
RH
145 return addr;
146 }
147
394ef640
ML
148 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
149 info.length = len;
d8aa712c 150 info.low_limit = FIRST_USER_ADDRESS;
394ef640
ML
151 info.high_limit = mm->mmap_base;
152 info.align_mask = do_align ? (PAGE_MASK & (SHMLBA - 1)) : 0;
153 info.align_offset = pgoff << PAGE_SHIFT;
154 addr = vm_unmapped_area(&info);
7dbaa466 155
7dbaa466
RH
156 /*
157 * A failed mmap() very likely causes application failure,
158 * so fall back to the bottom-up function here. This scenario
159 * can happen with large stack limits and large mmap()
160 * allocations.
161 */
394ef640
ML
162 if (addr & ~PAGE_MASK) {
163 VM_BUG_ON(addr != -ENOMEM);
164 info.flags = 0;
165 info.low_limit = mm->mmap_base;
166 info.high_limit = TASK_SIZE;
167 addr = vm_unmapped_area(&info);
168 }
7dbaa466
RH
169
170 return addr;
171}
172
2b68f6ca 173unsigned long arch_mmap_rnd(void)
fbbc400f
KC
174{
175 unsigned long rnd;
176
5ef11c35 177 rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
fbbc400f
KC
178
179 return rnd << PAGE_SHIFT;
180}
181
7dbaa466
RH
182void arch_pick_mmap_layout(struct mm_struct *mm)
183{
184 unsigned long random_factor = 0UL;
185
fbbc400f 186 if (current->flags & PF_RANDOMIZE)
2b68f6ca 187 random_factor = arch_mmap_rnd();
7dbaa466
RH
188
189 if (mmap_is_legacy()) {
190 mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
191 mm->get_unmapped_area = arch_get_unmapped_area;
7dbaa466
RH
192 } else {
193 mm->mmap_base = mmap_base(random_factor);
194 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
7dbaa466
RH
195 }
196}
51635ad2
LB
197
198/*
199 * You really shouldn't be using read() or write() on /dev/mem. This
200 * might go away in the future.
201 */
7e6735c3 202int valid_phys_addr_range(phys_addr_t addr, size_t size)
51635ad2 203{
9ae3ae0b
AR
204 if (addr < PHYS_OFFSET)
205 return 0;
6806bfe1 206 if (addr + size > __pa(high_memory - 1) + 1)
51635ad2
LB
207 return 0;
208
209 return 1;
210}
211
212/*
3159f372 213 * Do not allow /dev/mem mappings beyond the supported physical range.
51635ad2
LB
214 */
215int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
216{
3159f372 217 return (pfn + (size >> PAGE_SHIFT)) <= (1 + (PHYS_MASK >> PAGE_SHIFT));
51635ad2 218}
087aaffc
NP
219
220#ifdef CONFIG_STRICT_DEVMEM
221
222#include <linux/ioport.h>
223
224/*
225 * devmem_is_allowed() checks to see if /dev/mem access to a certain
226 * address is valid. The argument is a physical page number.
227 * We mimic x86 here by disallowing access to system RAM as well as
228 * device-exclusive MMIO regions. This effectively disable read()/write()
229 * on /dev/mem.
230 */
231int devmem_is_allowed(unsigned long pfn)
232{
233 if (iomem_is_exclusive(pfn << PAGE_SHIFT))
234 return 0;
235 if (!page_is_ram(pfn))
236 return 1;
237 return 0;
238}
239
240#endif