blob: f0701d8d24df5a8edd151055402c0cdf05e8a8e3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/mm/mmap.c
3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/fs.h>
5#include <linux/mm.h>
6#include <linux/mman.h>
7#include <linux/shm.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +01008#include <linux/sched/signal.h>
Ingo Molnar01042602017-02-08 18:51:31 +01009#include <linux/sched/mm.h>
Russell King09d9bae2008-09-05 14:08:44 +010010#include <linux/io.h>
Nicolas Pitredf5419a2011-04-13 04:57:17 +010011#include <linux/personality.h>
Nicolas Pitrecc92c282010-06-14 21:16:19 -040012#include <linux/random.h>
Rob Herring41dfaa92011-11-22 04:01:06 +010013#include <asm/cachetype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#define COLOUR_ALIGN(addr,pgoff) \
16 ((((addr)+SHMLBA-1)&~(SHMLBA-1)) + \
17 (((pgoff)<<PAGE_SHIFT) & (SHMLBA-1)))
18
Rob Herring7dbaa462011-11-22 04:01:07 +010019/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
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;
Rob Herring41dfaa92011-11-22 04:01:06 +010061 int do_align = 0;
62 int aliasing = cache_is_vipt_aliasing();
Michel Lespinasse394ef642012-12-11 16:02:10 -080063 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 /*
66 * We only need to do colour alignment if either the I or D
Rob Herring41dfaa92011-11-22 04:01:06 +010067 * caches alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 */
Rob Herring41dfaa92011-11-22 04:01:06 +010069 if (aliasing)
70 do_align = filp || (flags & MAP_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 /*
Benjamin Herrenschmidtacec0ac2007-05-06 14:50:07 -070073 * We enforce the MAP_FIXED case.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 */
75 if (flags & MAP_FIXED) {
Al Viroe77414e2009-12-05 15:10:44 -050076 if (aliasing && flags & MAP_SHARED &&
77 (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 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 &&
Hugh Dickins1be71072017-06-19 04:03:24 -070093 (!vma || addr + len <= vm_start_gap(vma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return addr;
95 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Michel Lespinasse394ef642012-12-11 16:02:10 -080097 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Rob Herring7dbaa462011-11-22 04:01:07 +0100106unsigned 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();
Michel Lespinasse394ef642012-12-11 16:02:10 -0800116 struct vm_unmapped_area_info info;
Rob Herring7dbaa462011-11-22 04:01:07 +0100117
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 &&
Hugh Dickins1be71072017-06-19 04:03:24 -0700144 (!vma || addr + len <= vm_start_gap(vma)))
Rob Herring7dbaa462011-11-22 04:01:07 +0100145 return addr;
146 }
147
Michel Lespinasse394ef642012-12-11 16:02:10 -0800148 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
149 info.length = len;
Russell Kingd8aa7122013-11-28 21:43:40 +0000150 info.low_limit = FIRST_USER_ADDRESS;
Michel Lespinasse394ef642012-12-11 16:02:10 -0800151 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);
Rob Herring7dbaa462011-11-22 04:01:07 +0100155
Rob Herring7dbaa462011-11-22 04:01:07 +0100156 /*
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 */
Michel Lespinasse394ef642012-12-11 16:02:10 -0800162 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 }
Rob Herring7dbaa462011-11-22 04:01:07 +0100169
170 return addr;
171}
172
Kees Cook2b68f6c2015-04-14 15:48:00 -0700173unsigned long arch_mmap_rnd(void)
Kees Cookfbbc4002015-04-14 15:47:41 -0700174{
175 unsigned long rnd;
176
Daniel Cashman5ef11c32016-02-26 15:19:37 -0800177 rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
Kees Cookfbbc4002015-04-14 15:47:41 -0700178
179 return rnd << PAGE_SHIFT;
180}
181
Rob Herring7dbaa462011-11-22 04:01:07 +0100182void arch_pick_mmap_layout(struct mm_struct *mm)
183{
184 unsigned long random_factor = 0UL;
185
Kees Cookfbbc4002015-04-14 15:47:41 -0700186 if (current->flags & PF_RANDOMIZE)
Kees Cook2b68f6c2015-04-14 15:48:00 -0700187 random_factor = arch_mmap_rnd();
Rob Herring7dbaa462011-11-22 04:01:07 +0100188
189 if (mmap_is_legacy()) {
190 mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
191 mm->get_unmapped_area = arch_get_unmapped_area;
Rob Herring7dbaa462011-11-22 04:01:07 +0100192 } else {
193 mm->mmap_base = mmap_base(random_factor);
194 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
Rob Herring7dbaa462011-11-22 04:01:07 +0100195 }
196}
Lennert Buytenhek51635ad2006-09-16 10:50:22 +0100197
198/*
199 * You really shouldn't be using read() or write() on /dev/mem. This
200 * might go away in the future.
201 */
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400202int valid_phys_addr_range(phys_addr_t addr, size_t size)
Lennert Buytenhek51635ad2006-09-16 10:50:22 +0100203{
Alexandre Rusev9ae3ae02008-02-26 18:42:10 +0100204 if (addr < PHYS_OFFSET)
205 return 0;
Greg Ungerer6806bfe2009-10-02 00:45:28 +0100206 if (addr + size > __pa(high_memory - 1) + 1)
Lennert Buytenhek51635ad2006-09-16 10:50:22 +0100207 return 0;
208
209 return 1;
210}
211
212/*
Sergey Dyasly3159f372013-09-24 16:38:00 +0100213 * Do not allow /dev/mem mappings beyond the supported physical range.
Lennert Buytenhek51635ad2006-09-16 10:50:22 +0100214 */
215int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
216{
Sergey Dyasly3159f372013-09-24 16:38:00 +0100217 return (pfn + (size >> PAGE_SHIFT)) <= (1 + (PHYS_MASK >> PAGE_SHIFT));
Lennert Buytenhek51635ad2006-09-16 10:50:22 +0100218}
Nicolas Pitre087aaff2010-09-22 18:34:36 -0400219
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