blob: f866870db749c4bf2b0e5ff03f687cda5569e651 [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/arch/arm/mm/mmap.c
4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/fs.h>
6#include <linux/mm.h>
7#include <linux/mman.h>
8#include <linux/shm.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +01009#include <linux/sched/signal.h>
Ingo Molnar01042602017-02-08 18:51:31 +010010#include <linux/sched/mm.h>
Russell King09d9bae2008-09-05 14:08:44 +010011#include <linux/io.h>
Nicolas Pitredf5419a2011-04-13 04:57:17 +010012#include <linux/personality.h>
Nicolas Pitrecc92c282010-06-14 21:16:19 -040013#include <linux/random.h>
Rob Herring41dfaa92011-11-22 04:01:06 +010014#include <asm/cachetype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#define COLOUR_ALIGN(addr,pgoff) \
17 ((((addr)+SHMLBA-1)&~(SHMLBA-1)) + \
18 (((pgoff)<<PAGE_SHIFT) & (SHMLBA-1)))
19
Rob Herring7dbaa462011-11-22 04:01:07 +010020/* gap between mmap and stack */
21#define MIN_GAP (128*1024*1024UL)
22#define MAX_GAP ((TASK_SIZE)/6*5)
23
Kees Cook8f2af152018-04-10 16:34:53 -070024static int mmap_is_legacy(struct rlimit *rlim_stack)
Rob Herring7dbaa462011-11-22 04:01:07 +010025{
26 if (current->personality & ADDR_COMPAT_LAYOUT)
27 return 1;
28
Kees Cook8f2af152018-04-10 16:34:53 -070029 if (rlim_stack->rlim_cur == RLIM_INFINITY)
Rob Herring7dbaa462011-11-22 04:01:07 +010030 return 1;
31
32 return sysctl_legacy_va_layout;
33}
34
Kees Cook8f2af152018-04-10 16:34:53 -070035static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack)
Rob Herring7dbaa462011-11-22 04:01:07 +010036{
Kees Cook8f2af152018-04-10 16:34:53 -070037 unsigned long gap = rlim_stack->rlim_cur;
Rob Herring7dbaa462011-11-22 04:01:07 +010038
39 if (gap < MIN_GAP)
40 gap = MIN_GAP;
41 else if (gap > MAX_GAP)
42 gap = MAX_GAP;
43
44 return PAGE_ALIGN(TASK_SIZE - gap - rnd);
45}
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 * We need to ensure that shared mappings are correctly aligned to
49 * avoid aliasing issues with VIPT caches. We need to ensure that
50 * a specific page of an object is always mapped at a multiple of
51 * SHMLBA bytes.
52 *
53 * We unconditionally provide this function for all cases, however
54 * in the VIVT case, we optimise out the alignment rules.
55 */
56unsigned long
57arch_get_unmapped_area(struct file *filp, unsigned long addr,
58 unsigned long len, unsigned long pgoff, unsigned long flags)
59{
60 struct mm_struct *mm = current->mm;
61 struct vm_area_struct *vma;
Rob Herring41dfaa92011-11-22 04:01:06 +010062 int do_align = 0;
63 int aliasing = cache_is_vipt_aliasing();
Michel Lespinasse394ef642012-12-11 16:02:10 -080064 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 /*
67 * We only need to do colour alignment if either the I or D
Rob Herring41dfaa92011-11-22 04:01:06 +010068 * caches alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 */
Rob Herring41dfaa92011-11-22 04:01:06 +010070 if (aliasing)
71 do_align = filp || (flags & MAP_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 /*
Benjamin Herrenschmidtacec0ac2007-05-06 14:50:07 -070074 * We enforce the MAP_FIXED case.
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 */
76 if (flags & MAP_FIXED) {
Al Viroe77414e2009-12-05 15:10:44 -050077 if (aliasing && flags & MAP_SHARED &&
78 (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return -EINVAL;
80 return addr;
81 }
82
83 if (len > TASK_SIZE)
84 return -ENOMEM;
85
86 if (addr) {
87 if (do_align)
88 addr = COLOUR_ALIGN(addr, pgoff);
89 else
90 addr = PAGE_ALIGN(addr);
91
92 vma = find_vma(mm, addr);
93 if (TASK_SIZE - len >= addr &&
Hugh Dickins1be71072017-06-19 04:03:24 -070094 (!vma || addr + len <= vm_start_gap(vma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return addr;
96 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Michel Lespinasse394ef642012-12-11 16:02:10 -080098 info.flags = 0;
99 info.length = len;
100 info.low_limit = mm->mmap_base;
101 info.high_limit = TASK_SIZE;
102 info.align_mask = do_align ? (PAGE_MASK & (SHMLBA - 1)) : 0;
103 info.align_offset = pgoff << PAGE_SHIFT;
104 return vm_unmapped_area(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Rob Herring7dbaa462011-11-22 04:01:07 +0100107unsigned long
108arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
109 const unsigned long len, const unsigned long pgoff,
110 const unsigned long flags)
111{
112 struct vm_area_struct *vma;
113 struct mm_struct *mm = current->mm;
114 unsigned long addr = addr0;
115 int do_align = 0;
116 int aliasing = cache_is_vipt_aliasing();
Michel Lespinasse394ef642012-12-11 16:02:10 -0800117 struct vm_unmapped_area_info info;
Rob Herring7dbaa462011-11-22 04:01:07 +0100118
119 /*
120 * We only need to do colour alignment if either the I or D
121 * caches alias.
122 */
123 if (aliasing)
124 do_align = filp || (flags & MAP_SHARED);
125
126 /* requested length too big for entire address space */
127 if (len > TASK_SIZE)
128 return -ENOMEM;
129
130 if (flags & MAP_FIXED) {
131 if (aliasing && flags & MAP_SHARED &&
132 (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1))
133 return -EINVAL;
134 return addr;
135 }
136
137 /* requesting a specific address */
138 if (addr) {
139 if (do_align)
140 addr = COLOUR_ALIGN(addr, pgoff);
141 else
142 addr = PAGE_ALIGN(addr);
143 vma = find_vma(mm, addr);
144 if (TASK_SIZE - len >= addr &&
Hugh Dickins1be71072017-06-19 04:03:24 -0700145 (!vma || addr + len <= vm_start_gap(vma)))
Rob Herring7dbaa462011-11-22 04:01:07 +0100146 return addr;
147 }
148
Michel Lespinasse394ef642012-12-11 16:02:10 -0800149 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
150 info.length = len;
Russell Kingd8aa7122013-11-28 21:43:40 +0000151 info.low_limit = FIRST_USER_ADDRESS;
Michel Lespinasse394ef642012-12-11 16:02:10 -0800152 info.high_limit = mm->mmap_base;
153 info.align_mask = do_align ? (PAGE_MASK & (SHMLBA - 1)) : 0;
154 info.align_offset = pgoff << PAGE_SHIFT;
155 addr = vm_unmapped_area(&info);
Rob Herring7dbaa462011-11-22 04:01:07 +0100156
Rob Herring7dbaa462011-11-22 04:01:07 +0100157 /*
158 * A failed mmap() very likely causes application failure,
159 * so fall back to the bottom-up function here. This scenario
160 * can happen with large stack limits and large mmap()
161 * allocations.
162 */
Michel Lespinasse394ef642012-12-11 16:02:10 -0800163 if (addr & ~PAGE_MASK) {
164 VM_BUG_ON(addr != -ENOMEM);
165 info.flags = 0;
166 info.low_limit = mm->mmap_base;
167 info.high_limit = TASK_SIZE;
168 addr = vm_unmapped_area(&info);
169 }
Rob Herring7dbaa462011-11-22 04:01:07 +0100170
171 return addr;
172}
173
Kees Cook2b68f6c2015-04-14 15:48:00 -0700174unsigned long arch_mmap_rnd(void)
Kees Cookfbbc4002015-04-14 15:47:41 -0700175{
176 unsigned long rnd;
177
Daniel Cashman5ef11c32016-02-26 15:19:37 -0800178 rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
Kees Cookfbbc4002015-04-14 15:47:41 -0700179
180 return rnd << PAGE_SHIFT;
181}
182
Kees Cook8f2af152018-04-10 16:34:53 -0700183void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
Rob Herring7dbaa462011-11-22 04:01:07 +0100184{
185 unsigned long random_factor = 0UL;
186
Kees Cookfbbc4002015-04-14 15:47:41 -0700187 if (current->flags & PF_RANDOMIZE)
Kees Cook2b68f6c2015-04-14 15:48:00 -0700188 random_factor = arch_mmap_rnd();
Rob Herring7dbaa462011-11-22 04:01:07 +0100189
Kees Cook8f2af152018-04-10 16:34:53 -0700190 if (mmap_is_legacy(rlim_stack)) {
Rob Herring7dbaa462011-11-22 04:01:07 +0100191 mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
192 mm->get_unmapped_area = arch_get_unmapped_area;
Rob Herring7dbaa462011-11-22 04:01:07 +0100193 } else {
Kees Cook8f2af152018-04-10 16:34:53 -0700194 mm->mmap_base = mmap_base(random_factor, rlim_stack);
Rob Herring7dbaa462011-11-22 04:01:07 +0100195 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
Rob Herring7dbaa462011-11-22 04:01:07 +0100196 }
197}
Lennert Buytenhek51635ad2006-09-16 10:50:22 +0100198
199/*
200 * You really shouldn't be using read() or write() on /dev/mem. This
201 * might go away in the future.
202 */
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400203int valid_phys_addr_range(phys_addr_t addr, size_t size)
Lennert Buytenhek51635ad2006-09-16 10:50:22 +0100204{
Alexandre Rusev9ae3ae02008-02-26 18:42:10 +0100205 if (addr < PHYS_OFFSET)
206 return 0;
Greg Ungerer6806bfe2009-10-02 00:45:28 +0100207 if (addr + size > __pa(high_memory - 1) + 1)
Lennert Buytenhek51635ad2006-09-16 10:50:22 +0100208 return 0;
209
210 return 1;
211}
212
213/*
Sergey Dyasly3159f372013-09-24 16:38:00 +0100214 * Do not allow /dev/mem mappings beyond the supported physical range.
Lennert Buytenhek51635ad2006-09-16 10:50:22 +0100215 */
216int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
217{
Sergey Dyasly3159f372013-09-24 16:38:00 +0100218 return (pfn + (size >> PAGE_SHIFT)) <= (1 + (PHYS_MASK >> PAGE_SHIFT));
Lennert Buytenhek51635ad2006-09-16 10:50:22 +0100219}
Nicolas Pitre087aaff2010-09-22 18:34:36 -0400220
221#ifdef CONFIG_STRICT_DEVMEM
222
223#include <linux/ioport.h>
224
225/*
226 * devmem_is_allowed() checks to see if /dev/mem access to a certain
227 * address is valid. The argument is a physical page number.
228 * We mimic x86 here by disallowing access to system RAM as well as
229 * device-exclusive MMIO regions. This effectively disable read()/write()
230 * on /dev/mem.
231 */
232int devmem_is_allowed(unsigned long pfn)
233{
234 if (iomem_is_exclusive(pfn << PAGE_SHIFT))
235 return 0;
236 if (!page_is_ram(pfn))
237 return 1;
238 return 0;
239}
240
241#endif