Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm/mm/mmap.c |
| 3 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | #include <linux/fs.h> |
| 5 | #include <linux/mm.h> |
| 6 | #include <linux/mman.h> |
| 7 | #include <linux/shm.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 8 | #include <linux/sched.h> |
Russell King | 09d9bae | 2008-09-05 14:08:44 +0100 | [diff] [blame] | 9 | #include <linux/io.h> |
Nicolas Pitre | df5419a | 2011-04-13 04:57:17 +0100 | [diff] [blame] | 10 | #include <linux/personality.h> |
Nicolas Pitre | cc92c28 | 2010-06-14 21:16:19 -0400 | [diff] [blame] | 11 | #include <linux/random.h> |
Rob Herring | 41dfaa9 | 2011-11-22 04:01:06 +0100 | [diff] [blame] | 12 | #include <asm/cachetype.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
Rob Herring | 7dbaa46 | 2011-11-22 04:01:07 +0100 | [diff] [blame] | 14 | static inline unsigned long COLOUR_ALIGN_DOWN(unsigned long addr, |
| 15 | unsigned long pgoff) |
| 16 | { |
| 17 | unsigned long base = addr & ~(SHMLBA-1); |
| 18 | unsigned long off = (pgoff << PAGE_SHIFT) & (SHMLBA-1); |
| 19 | |
| 20 | if (base + off <= addr) |
| 21 | return base + off; |
| 22 | |
| 23 | return base - off; |
| 24 | } |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #define COLOUR_ALIGN(addr,pgoff) \ |
| 27 | ((((addr)+SHMLBA-1)&~(SHMLBA-1)) + \ |
| 28 | (((pgoff)<<PAGE_SHIFT) & (SHMLBA-1))) |
| 29 | |
Rob Herring | 7dbaa46 | 2011-11-22 04:01:07 +0100 | [diff] [blame] | 30 | /* gap between mmap and stack */ |
| 31 | #define MIN_GAP (128*1024*1024UL) |
| 32 | #define MAX_GAP ((TASK_SIZE)/6*5) |
| 33 | |
| 34 | static int mmap_is_legacy(void) |
| 35 | { |
| 36 | if (current->personality & ADDR_COMPAT_LAYOUT) |
| 37 | return 1; |
| 38 | |
| 39 | if (rlimit(RLIMIT_STACK) == RLIM_INFINITY) |
| 40 | return 1; |
| 41 | |
| 42 | return sysctl_legacy_va_layout; |
| 43 | } |
| 44 | |
| 45 | static unsigned long mmap_base(unsigned long rnd) |
| 46 | { |
| 47 | unsigned long gap = rlimit(RLIMIT_STACK); |
| 48 | |
| 49 | if (gap < MIN_GAP) |
| 50 | gap = MIN_GAP; |
| 51 | else if (gap > MAX_GAP) |
| 52 | gap = MAX_GAP; |
| 53 | |
| 54 | return PAGE_ALIGN(TASK_SIZE - gap - rnd); |
| 55 | } |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* |
| 58 | * We need to ensure that shared mappings are correctly aligned to |
| 59 | * avoid aliasing issues with VIPT caches. We need to ensure that |
| 60 | * a specific page of an object is always mapped at a multiple of |
| 61 | * SHMLBA bytes. |
| 62 | * |
| 63 | * We unconditionally provide this function for all cases, however |
| 64 | * in the VIVT case, we optimise out the alignment rules. |
| 65 | */ |
| 66 | unsigned long |
| 67 | arch_get_unmapped_area(struct file *filp, unsigned long addr, |
| 68 | unsigned long len, unsigned long pgoff, unsigned long flags) |
| 69 | { |
| 70 | struct mm_struct *mm = current->mm; |
| 71 | struct vm_area_struct *vma; |
| 72 | unsigned long start_addr; |
Rob Herring | 41dfaa9 | 2011-11-22 04:01:06 +0100 | [diff] [blame] | 73 | int do_align = 0; |
| 74 | int aliasing = cache_is_vipt_aliasing(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | * We only need to do colour alignment if either the I or D |
Rob Herring | 41dfaa9 | 2011-11-22 04:01:06 +0100 | [diff] [blame] | 78 | * caches alias. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | */ |
Rob Herring | 41dfaa9 | 2011-11-22 04:01:06 +0100 | [diff] [blame] | 80 | if (aliasing) |
| 81 | do_align = filp || (flags & MAP_SHARED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | /* |
Benjamin Herrenschmidt | acec0ac | 2007-05-06 14:50:07 -0700 | [diff] [blame] | 84 | * We enforce the MAP_FIXED case. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | */ |
| 86 | if (flags & MAP_FIXED) { |
Al Viro | e77414e | 2009-12-05 15:10:44 -0500 | [diff] [blame] | 87 | if (aliasing && flags & MAP_SHARED && |
| 88 | (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | return -EINVAL; |
| 90 | return addr; |
| 91 | } |
| 92 | |
| 93 | if (len > TASK_SIZE) |
| 94 | return -ENOMEM; |
| 95 | |
| 96 | if (addr) { |
| 97 | if (do_align) |
| 98 | addr = COLOUR_ALIGN(addr, pgoff); |
| 99 | else |
| 100 | addr = PAGE_ALIGN(addr); |
| 101 | |
| 102 | vma = find_vma(mm, addr); |
| 103 | if (TASK_SIZE - len >= addr && |
| 104 | (!vma || addr + len <= vma->vm_start)) |
| 105 | return addr; |
| 106 | } |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 107 | if (len > mm->cached_hole_size) { |
| 108 | start_addr = addr = mm->free_area_cache; |
| 109 | } else { |
Rob Herring | 7dbaa46 | 2011-11-22 04:01:07 +0100 | [diff] [blame] | 110 | start_addr = addr = mm->mmap_base; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 111 | mm->cached_hole_size = 0; |
| 112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | full_search: |
| 115 | if (do_align) |
| 116 | addr = COLOUR_ALIGN(addr, pgoff); |
| 117 | else |
| 118 | addr = PAGE_ALIGN(addr); |
| 119 | |
| 120 | for (vma = find_vma(mm, addr); ; vma = vma->vm_next) { |
| 121 | /* At this point: (!vma || addr < vma->vm_end). */ |
| 122 | if (TASK_SIZE - len < addr) { |
| 123 | /* |
| 124 | * Start a new search - just in case we missed |
| 125 | * some holes. |
| 126 | */ |
| 127 | if (start_addr != TASK_UNMAPPED_BASE) { |
| 128 | start_addr = addr = TASK_UNMAPPED_BASE; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 129 | mm->cached_hole_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | goto full_search; |
| 131 | } |
| 132 | return -ENOMEM; |
| 133 | } |
| 134 | if (!vma || addr + len <= vma->vm_start) { |
| 135 | /* |
| 136 | * Remember the place where we stopped the search: |
| 137 | */ |
| 138 | mm->free_area_cache = addr + len; |
| 139 | return addr; |
| 140 | } |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 141 | if (addr + mm->cached_hole_size < vma->vm_start) |
| 142 | mm->cached_hole_size = vma->vm_start - addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | addr = vma->vm_end; |
| 144 | if (do_align) |
| 145 | addr = COLOUR_ALIGN(addr, pgoff); |
| 146 | } |
| 147 | } |
| 148 | |
Rob Herring | 7dbaa46 | 2011-11-22 04:01:07 +0100 | [diff] [blame] | 149 | unsigned long |
| 150 | arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, |
| 151 | const unsigned long len, const unsigned long pgoff, |
| 152 | const unsigned long flags) |
| 153 | { |
| 154 | struct vm_area_struct *vma; |
| 155 | struct mm_struct *mm = current->mm; |
| 156 | unsigned long addr = addr0; |
| 157 | int do_align = 0; |
| 158 | int aliasing = cache_is_vipt_aliasing(); |
| 159 | |
| 160 | /* |
| 161 | * We only need to do colour alignment if either the I or D |
| 162 | * caches alias. |
| 163 | */ |
| 164 | if (aliasing) |
| 165 | do_align = filp || (flags & MAP_SHARED); |
| 166 | |
| 167 | /* requested length too big for entire address space */ |
| 168 | if (len > TASK_SIZE) |
| 169 | return -ENOMEM; |
| 170 | |
| 171 | if (flags & MAP_FIXED) { |
| 172 | if (aliasing && flags & MAP_SHARED && |
| 173 | (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)) |
| 174 | return -EINVAL; |
| 175 | return addr; |
| 176 | } |
| 177 | |
| 178 | /* requesting a specific address */ |
| 179 | if (addr) { |
| 180 | if (do_align) |
| 181 | addr = COLOUR_ALIGN(addr, pgoff); |
| 182 | else |
| 183 | addr = PAGE_ALIGN(addr); |
| 184 | vma = find_vma(mm, addr); |
| 185 | if (TASK_SIZE - len >= addr && |
| 186 | (!vma || addr + len <= vma->vm_start)) |
| 187 | return addr; |
| 188 | } |
| 189 | |
| 190 | /* check if free_area_cache is useful for us */ |
| 191 | if (len <= mm->cached_hole_size) { |
| 192 | mm->cached_hole_size = 0; |
| 193 | mm->free_area_cache = mm->mmap_base; |
| 194 | } |
| 195 | |
| 196 | /* either no address requested or can't fit in requested address hole */ |
| 197 | addr = mm->free_area_cache; |
| 198 | if (do_align) { |
| 199 | unsigned long base = COLOUR_ALIGN_DOWN(addr - len, pgoff); |
| 200 | addr = base + len; |
| 201 | } |
| 202 | |
| 203 | /* make sure it can fit in the remaining address space */ |
| 204 | if (addr > len) { |
| 205 | vma = find_vma(mm, addr-len); |
| 206 | if (!vma || addr <= vma->vm_start) |
| 207 | /* remember the address as a hint for next time */ |
| 208 | return (mm->free_area_cache = addr-len); |
| 209 | } |
| 210 | |
| 211 | if (mm->mmap_base < len) |
| 212 | goto bottomup; |
| 213 | |
| 214 | addr = mm->mmap_base - len; |
| 215 | if (do_align) |
| 216 | addr = COLOUR_ALIGN_DOWN(addr, pgoff); |
| 217 | |
| 218 | do { |
| 219 | /* |
| 220 | * Lookup failure means no vma is above this address, |
| 221 | * else if new region fits below vma->vm_start, |
| 222 | * return with success: |
| 223 | */ |
| 224 | vma = find_vma(mm, addr); |
| 225 | if (!vma || addr+len <= vma->vm_start) |
| 226 | /* remember the address as a hint for next time */ |
| 227 | return (mm->free_area_cache = addr); |
| 228 | |
| 229 | /* remember the largest hole we saw so far */ |
| 230 | if (addr + mm->cached_hole_size < vma->vm_start) |
| 231 | mm->cached_hole_size = vma->vm_start - addr; |
| 232 | |
| 233 | /* try just below the current vma->vm_start */ |
| 234 | addr = vma->vm_start - len; |
| 235 | if (do_align) |
| 236 | addr = COLOUR_ALIGN_DOWN(addr, pgoff); |
| 237 | } while (len < vma->vm_start); |
| 238 | |
| 239 | bottomup: |
| 240 | /* |
| 241 | * A failed mmap() very likely causes application failure, |
| 242 | * so fall back to the bottom-up function here. This scenario |
| 243 | * can happen with large stack limits and large mmap() |
| 244 | * allocations. |
| 245 | */ |
| 246 | mm->cached_hole_size = ~0UL; |
| 247 | mm->free_area_cache = TASK_UNMAPPED_BASE; |
| 248 | addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags); |
| 249 | /* |
| 250 | * Restore the topdown base: |
| 251 | */ |
| 252 | mm->free_area_cache = mm->mmap_base; |
| 253 | mm->cached_hole_size = ~0UL; |
| 254 | |
| 255 | return addr; |
| 256 | } |
| 257 | |
| 258 | void arch_pick_mmap_layout(struct mm_struct *mm) |
| 259 | { |
| 260 | unsigned long random_factor = 0UL; |
| 261 | |
| 262 | /* 8 bits of randomness in 20 address space bits */ |
| 263 | if ((current->flags & PF_RANDOMIZE) && |
| 264 | !(current->personality & ADDR_NO_RANDOMIZE)) |
| 265 | random_factor = (get_random_int() % (1 << 8)) << PAGE_SHIFT; |
| 266 | |
| 267 | if (mmap_is_legacy()) { |
| 268 | mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; |
| 269 | mm->get_unmapped_area = arch_get_unmapped_area; |
| 270 | mm->unmap_area = arch_unmap_area; |
| 271 | } else { |
| 272 | mm->mmap_base = mmap_base(random_factor); |
| 273 | mm->get_unmapped_area = arch_get_unmapped_area_topdown; |
| 274 | mm->unmap_area = arch_unmap_area_topdown; |
| 275 | } |
| 276 | } |
Lennert Buytenhek | 51635ad | 2006-09-16 10:50:22 +0100 | [diff] [blame] | 277 | |
| 278 | /* |
| 279 | * You really shouldn't be using read() or write() on /dev/mem. This |
| 280 | * might go away in the future. |
| 281 | */ |
| 282 | int valid_phys_addr_range(unsigned long addr, size_t size) |
| 283 | { |
Alexandre Rusev | 9ae3ae0 | 2008-02-26 18:42:10 +0100 | [diff] [blame] | 284 | if (addr < PHYS_OFFSET) |
| 285 | return 0; |
Greg Ungerer | 6806bfe | 2009-10-02 00:45:28 +0100 | [diff] [blame] | 286 | if (addr + size > __pa(high_memory - 1) + 1) |
Lennert Buytenhek | 51635ad | 2006-09-16 10:50:22 +0100 | [diff] [blame] | 287 | return 0; |
| 288 | |
| 289 | return 1; |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * We don't use supersection mappings for mmap() on /dev/mem, which |
| 294 | * means that we can't map the memory area above the 4G barrier into |
| 295 | * userspace. |
| 296 | */ |
| 297 | int valid_mmap_phys_addr_range(unsigned long pfn, size_t size) |
| 298 | { |
| 299 | return !(pfn + (size >> PAGE_SHIFT) > 0x00100000); |
| 300 | } |
Nicolas Pitre | 087aaff | 2010-09-22 18:34:36 -0400 | [diff] [blame] | 301 | |
| 302 | #ifdef CONFIG_STRICT_DEVMEM |
| 303 | |
| 304 | #include <linux/ioport.h> |
| 305 | |
| 306 | /* |
| 307 | * devmem_is_allowed() checks to see if /dev/mem access to a certain |
| 308 | * address is valid. The argument is a physical page number. |
| 309 | * We mimic x86 here by disallowing access to system RAM as well as |
| 310 | * device-exclusive MMIO regions. This effectively disable read()/write() |
| 311 | * on /dev/mem. |
| 312 | */ |
| 313 | int devmem_is_allowed(unsigned long pfn) |
| 314 | { |
| 315 | if (iomem_is_exclusive(pfn << PAGE_SHIFT)) |
| 316 | return 0; |
| 317 | if (!page_is_ram(pfn)) |
| 318 | return 1; |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | #endif |