4 * Copyright (C) 2008 - 2009 Paul Mundt
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
12 #include <linux/mman.h>
13 #include <linux/module.h>
15 #include <asm/processor.h>
17 unsigned long shm_align_mask
= PAGE_SIZE
- 1; /* Sane caches */
18 EXPORT_SYMBOL(shm_align_mask
);
22 * To avoid cache aliases, we map the shared page with same color.
24 static inline unsigned long COLOUR_ALIGN(unsigned long addr
,
27 unsigned long base
= (addr
+ shm_align_mask
) & ~shm_align_mask
;
28 unsigned long off
= (pgoff
<< PAGE_SHIFT
) & shm_align_mask
;
33 static inline unsigned long COLOUR_ALIGN_DOWN(unsigned long addr
,
36 unsigned long base
= addr
& ~shm_align_mask
;
37 unsigned long off
= (pgoff
<< PAGE_SHIFT
) & shm_align_mask
;
39 if (base
+ off
<= addr
)
45 unsigned long arch_get_unmapped_area(struct file
*filp
, unsigned long addr
,
46 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
48 struct mm_struct
*mm
= current
->mm
;
49 struct vm_area_struct
*vma
;
50 unsigned long start_addr
;
53 if (flags
& MAP_FIXED
) {
54 /* We do not accept a shared mapping if it would violate
55 * cache aliasing constraints.
57 if ((flags
& MAP_SHARED
) &&
58 ((addr
- (pgoff
<< PAGE_SHIFT
)) & shm_align_mask
))
63 if (unlikely(len
> TASK_SIZE
))
67 if (filp
|| (flags
& MAP_SHARED
))
72 addr
= COLOUR_ALIGN(addr
, pgoff
);
74 addr
= PAGE_ALIGN(addr
);
76 vma
= find_vma(mm
, addr
);
77 if (TASK_SIZE
- len
>= addr
&&
78 (!vma
|| addr
+ len
<= vma
->vm_start
))
82 if (len
> mm
->cached_hole_size
) {
83 start_addr
= addr
= mm
->free_area_cache
;
85 mm
->cached_hole_size
= 0;
86 start_addr
= addr
= TASK_UNMAPPED_BASE
;
91 addr
= COLOUR_ALIGN(addr
, pgoff
);
93 addr
= PAGE_ALIGN(mm
->free_area_cache
);
95 for (vma
= find_vma(mm
, addr
); ; vma
= vma
->vm_next
) {
96 /* At this point: (!vma || addr < vma->vm_end). */
97 if (unlikely(TASK_SIZE
- len
< addr
)) {
99 * Start a new search - just in case we missed
102 if (start_addr
!= TASK_UNMAPPED_BASE
) {
103 start_addr
= addr
= TASK_UNMAPPED_BASE
;
104 mm
->cached_hole_size
= 0;
109 if (likely(!vma
|| addr
+ len
<= vma
->vm_start
)) {
111 * Remember the place where we stopped the search:
113 mm
->free_area_cache
= addr
+ len
;
116 if (addr
+ mm
->cached_hole_size
< vma
->vm_start
)
117 mm
->cached_hole_size
= vma
->vm_start
- addr
;
121 addr
= COLOUR_ALIGN(addr
, pgoff
);
126 arch_get_unmapped_area_topdown(struct file
*filp
, const unsigned long addr0
,
127 const unsigned long len
, const unsigned long pgoff
,
128 const unsigned long flags
)
130 struct vm_area_struct
*vma
;
131 struct mm_struct
*mm
= current
->mm
;
132 unsigned long addr
= addr0
;
135 if (flags
& MAP_FIXED
) {
136 /* We do not accept a shared mapping if it would violate
137 * cache aliasing constraints.
139 if ((flags
& MAP_SHARED
) &&
140 ((addr
- (pgoff
<< PAGE_SHIFT
)) & shm_align_mask
))
145 if (unlikely(len
> TASK_SIZE
))
149 if (filp
|| (flags
& MAP_SHARED
))
152 /* requesting a specific address */
155 addr
= COLOUR_ALIGN(addr
, pgoff
);
157 addr
= PAGE_ALIGN(addr
);
159 vma
= find_vma(mm
, addr
);
160 if (TASK_SIZE
- len
>= addr
&&
161 (!vma
|| addr
+ len
<= vma
->vm_start
))
165 /* check if free_area_cache is useful for us */
166 if (len
<= mm
->cached_hole_size
) {
167 mm
->cached_hole_size
= 0;
168 mm
->free_area_cache
= mm
->mmap_base
;
171 /* either no address requested or can't fit in requested address hole */
172 addr
= mm
->free_area_cache
;
173 if (do_colour_align
) {
174 unsigned long base
= COLOUR_ALIGN_DOWN(addr
-len
, pgoff
);
179 /* make sure it can fit in the remaining address space */
180 if (likely(addr
> len
)) {
181 vma
= find_vma(mm
, addr
-len
);
182 if (!vma
|| addr
<= vma
->vm_start
) {
183 /* remember the address as a hint for next time */
184 return (mm
->free_area_cache
= addr
-len
);
188 if (unlikely(mm
->mmap_base
< len
))
191 addr
= mm
->mmap_base
-len
;
193 addr
= COLOUR_ALIGN_DOWN(addr
, pgoff
);
197 * Lookup failure means no vma is above this address,
198 * else if new region fits below vma->vm_start,
199 * return with success:
201 vma
= find_vma(mm
, addr
);
202 if (likely(!vma
|| addr
+len
<= vma
->vm_start
)) {
203 /* remember the address as a hint for next time */
204 return (mm
->free_area_cache
= addr
);
207 /* remember the largest hole we saw so far */
208 if (addr
+ mm
->cached_hole_size
< vma
->vm_start
)
209 mm
->cached_hole_size
= vma
->vm_start
- addr
;
211 /* try just below the current vma->vm_start */
212 addr
= vma
->vm_start
-len
;
214 addr
= COLOUR_ALIGN_DOWN(addr
, pgoff
);
215 } while (likely(len
< vma
->vm_start
));
219 * A failed mmap() very likely causes application failure,
220 * so fall back to the bottom-up function here. This scenario
221 * can happen with large stack limits and large mmap()
224 mm
->cached_hole_size
= ~0UL;
225 mm
->free_area_cache
= TASK_UNMAPPED_BASE
;
226 addr
= arch_get_unmapped_area(filp
, addr0
, len
, pgoff
, flags
);
228 * Restore the topdown base:
230 mm
->free_area_cache
= mm
->mmap_base
;
231 mm
->cached_hole_size
= ~0UL;
235 #endif /* CONFIG_MMU */
238 * You really shouldn't be using read() or write() on /dev/mem. This
239 * might go away in the future.
241 int valid_phys_addr_range(unsigned long addr
, size_t count
)
243 if (addr
< __MEMORY_START
)
245 if (addr
+ count
> __pa(high_memory
))
251 int valid_mmap_phys_addr_range(unsigned long pfn
, size_t size
)