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