]>
Commit | Line | Data |
---|---|---|
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> | |
e8edc6e0 | 8 | #include <linux/sched.h> |
09d9bae0 | 9 | #include <linux/io.h> |
df5419a9 | 10 | #include <linux/personality.h> |
cc92c28b | 11 | #include <linux/random.h> |
41dfaa93 | 12 | #include <asm/cachetype.h> |
1da177e4 LT |
13 | |
14 | #define COLOUR_ALIGN(addr,pgoff) \ | |
15 | ((((addr)+SHMLBA-1)&~(SHMLBA-1)) + \ | |
16 | (((pgoff)<<PAGE_SHIFT) & (SHMLBA-1))) | |
17 | ||
7dbaa466 RH |
18 | /* gap between mmap and stack */ |
19 | #define MIN_GAP (128*1024*1024UL) | |
20 | #define MAX_GAP ((TASK_SIZE)/6*5) | |
21 | ||
22 | static 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 | ||
33 | static 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 | ||
1da177e4 LT |
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 | */ | |
54 | unsigned long | |
55 | arch_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; | |
41dfaa93 RH |
60 | int do_align = 0; |
61 | int aliasing = cache_is_vipt_aliasing(); | |
63d9c273 | 62 | unsigned long offset = gr_rand_threadstack_offset(mm, filp, flags); |
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 | ||
63d9c273 MT |
85 | #ifdef CONFIG_PAX_RANDMMAP |
86 | if (!(mm->pax_flags & MF_PAX_RANDMMAP)) | |
87 | #endif | |
88 | ||
1da177e4 LT |
89 | if (addr) { |
90 | if (do_align) | |
91 | addr = COLOUR_ALIGN(addr, pgoff); | |
92 | else | |
93 | addr = PAGE_ALIGN(addr); | |
94 | ||
95 | vma = find_vma(mm, addr); | |
63d9c273 | 96 | if (TASK_SIZE - len >= addr && check_heap_stack_gap(vma, addr, len, offset)) |
1da177e4 LT |
97 | return addr; |
98 | } | |
1da177e4 | 99 | |
394ef640 ML |
100 | info.flags = 0; |
101 | info.length = len; | |
102 | info.low_limit = mm->mmap_base; | |
103 | info.high_limit = TASK_SIZE; | |
104 | info.align_mask = do_align ? (PAGE_MASK & (SHMLBA - 1)) : 0; | |
105 | info.align_offset = pgoff << PAGE_SHIFT; | |
63d9c273 | 106 | info.threadstack_offset = offset; |
394ef640 | 107 | return vm_unmapped_area(&info); |
1da177e4 LT |
108 | } |
109 | ||
7dbaa466 RH |
110 | unsigned long |
111 | arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, | |
112 | const unsigned long len, const unsigned long pgoff, | |
113 | const unsigned long flags) | |
114 | { | |
115 | struct vm_area_struct *vma; | |
116 | struct mm_struct *mm = current->mm; | |
117 | unsigned long addr = addr0; | |
118 | int do_align = 0; | |
119 | int aliasing = cache_is_vipt_aliasing(); | |
63d9c273 | 120 | unsigned long offset = gr_rand_threadstack_offset(mm, filp, flags); |
394ef640 | 121 | struct vm_unmapped_area_info info; |
7dbaa466 RH |
122 | |
123 | /* | |
124 | * We only need to do colour alignment if either the I or D | |
125 | * caches alias. | |
126 | */ | |
127 | if (aliasing) | |
128 | do_align = filp || (flags & MAP_SHARED); | |
129 | ||
130 | /* requested length too big for entire address space */ | |
131 | if (len > TASK_SIZE) | |
132 | return -ENOMEM; | |
133 | ||
134 | if (flags & MAP_FIXED) { | |
135 | if (aliasing && flags & MAP_SHARED && | |
136 | (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)) | |
137 | return -EINVAL; | |
138 | return addr; | |
139 | } | |
140 | ||
63d9c273 MT |
141 | #ifdef CONFIG_PAX_RANDMMAP |
142 | if (!(mm->pax_flags & MF_PAX_RANDMMAP)) | |
143 | #endif | |
144 | ||
7dbaa466 RH |
145 | /* requesting a specific address */ |
146 | if (addr) { | |
147 | if (do_align) | |
148 | addr = COLOUR_ALIGN(addr, pgoff); | |
149 | else | |
150 | addr = PAGE_ALIGN(addr); | |
151 | vma = find_vma(mm, addr); | |
63d9c273 | 152 | if (TASK_SIZE - len >= addr && check_heap_stack_gap(vma, addr, len, offset)) |
7dbaa466 RH |
153 | return addr; |
154 | } | |
155 | ||
394ef640 ML |
156 | info.flags = VM_UNMAPPED_AREA_TOPDOWN; |
157 | info.length = len; | |
d8aa712c | 158 | info.low_limit = FIRST_USER_ADDRESS; |
394ef640 ML |
159 | info.high_limit = mm->mmap_base; |
160 | info.align_mask = do_align ? (PAGE_MASK & (SHMLBA - 1)) : 0; | |
161 | info.align_offset = pgoff << PAGE_SHIFT; | |
63d9c273 | 162 | info.threadstack_offset = offset; |
394ef640 | 163 | addr = vm_unmapped_area(&info); |
7dbaa466 | 164 | |
7dbaa466 RH |
165 | /* |
166 | * A failed mmap() very likely causes application failure, | |
167 | * so fall back to the bottom-up function here. This scenario | |
168 | * can happen with large stack limits and large mmap() | |
169 | * allocations. | |
170 | */ | |
394ef640 ML |
171 | if (addr & ~PAGE_MASK) { |
172 | VM_BUG_ON(addr != -ENOMEM); | |
173 | info.flags = 0; | |
174 | info.low_limit = mm->mmap_base; | |
175 | info.high_limit = TASK_SIZE; | |
176 | addr = vm_unmapped_area(&info); | |
177 | } | |
7dbaa466 RH |
178 | |
179 | return addr; | |
180 | } | |
181 | ||
182 | void arch_pick_mmap_layout(struct mm_struct *mm) | |
183 | { | |
184 | unsigned long random_factor = 0UL; | |
185 | ||
63d9c273 MT |
186 | #ifdef CONFIG_PAX_RANDMMAP |
187 | if (!(mm->pax_flags & MF_PAX_RANDMMAP)) | |
188 | #endif | |
189 | ||
7dbaa466 RH |
190 | /* 8 bits of randomness in 20 address space bits */ |
191 | if ((current->flags & PF_RANDOMIZE) && | |
192 | !(current->personality & ADDR_NO_RANDOMIZE)) | |
193 | random_factor = (get_random_int() % (1 << 8)) << PAGE_SHIFT; | |
194 | ||
195 | if (mmap_is_legacy()) { | |
196 | mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; | |
63d9c273 MT |
197 | |
198 | #ifdef CONFIG_PAX_RANDMMAP | |
199 | if (mm->pax_flags & MF_PAX_RANDMMAP) | |
200 | mm->mmap_base += mm->delta_mmap; | |
201 | #endif | |
202 | ||
7dbaa466 | 203 | mm->get_unmapped_area = arch_get_unmapped_area; |
7dbaa466 RH |
204 | } else { |
205 | mm->mmap_base = mmap_base(random_factor); | |
63d9c273 MT |
206 | |
207 | #ifdef CONFIG_PAX_RANDMMAP | |
208 | if (mm->pax_flags & MF_PAX_RANDMMAP) | |
209 | mm->mmap_base -= mm->delta_mmap + mm->delta_stack; | |
210 | #endif | |
211 | ||
7dbaa466 | 212 | mm->get_unmapped_area = arch_get_unmapped_area_topdown; |
7dbaa466 RH |
213 | } |
214 | } | |
51635ad2 LB |
215 | |
216 | /* | |
217 | * You really shouldn't be using read() or write() on /dev/mem. This | |
218 | * might go away in the future. | |
219 | */ | |
7e6735c3 | 220 | int valid_phys_addr_range(phys_addr_t addr, size_t size) |
51635ad2 | 221 | { |
9ae3ae0b AR |
222 | if (addr < PHYS_OFFSET) |
223 | return 0; | |
6806bfe1 | 224 | if (addr + size > __pa(high_memory - 1) + 1) |
51635ad2 LB |
225 | return 0; |
226 | ||
227 | return 1; | |
228 | } | |
229 | ||
230 | /* | |
3159f372 | 231 | * Do not allow /dev/mem mappings beyond the supported physical range. |
51635ad2 LB |
232 | */ |
233 | int valid_mmap_phys_addr_range(unsigned long pfn, size_t size) | |
234 | { | |
3159f372 | 235 | return (pfn + (size >> PAGE_SHIFT)) <= (1 + (PHYS_MASK >> PAGE_SHIFT)); |
51635ad2 | 236 | } |
087aaffc NP |
237 | |
238 | #ifdef CONFIG_STRICT_DEVMEM | |
239 | ||
240 | #include <linux/ioport.h> | |
241 | ||
242 | /* | |
243 | * devmem_is_allowed() checks to see if /dev/mem access to a certain | |
244 | * address is valid. The argument is a physical page number. | |
245 | * We mimic x86 here by disallowing access to system RAM as well as | |
246 | * device-exclusive MMIO regions. This effectively disable read()/write() | |
247 | * on /dev/mem. | |
248 | */ | |
249 | int devmem_is_allowed(unsigned long pfn) | |
250 | { | |
251 | if (iomem_is_exclusive(pfn << PAGE_SHIFT)) | |
252 | return 0; | |
253 | if (!page_is_ram(pfn)) | |
254 | return 1; | |
255 | return 0; | |
256 | } | |
257 | ||
258 | #endif |