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 | |
| 14 | #define COLOUR_ALIGN(addr,pgoff) \ |
| 15 | ((((addr)+SHMLBA-1)&~(SHMLBA-1)) + \ |
| 16 | (((pgoff)<<PAGE_SHIFT) & (SHMLBA-1))) |
| 17 | |
Rob Herring | 7dbaa46 | 2011-11-22 04:01:07 +0100 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Rob Herring | 41dfaa9 | 2011-11-22 04:01:06 +0100 | [diff] [blame] | 60 | int do_align = 0; |
| 61 | int aliasing = cache_is_vipt_aliasing(); |
Michel Lespinasse | 394ef64 | 2012-12-11 16:02:10 -0800 | [diff] [blame] | 62 | struct vm_unmapped_area_info info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | /* |
| 65 | * 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] | 66 | * caches alias. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | */ |
Rob Herring | 41dfaa9 | 2011-11-22 04:01:06 +0100 | [diff] [blame] | 68 | if (aliasing) |
| 69 | do_align = filp || (flags & MAP_SHARED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | /* |
Benjamin Herrenschmidt | acec0ac | 2007-05-06 14:50:07 -0700 | [diff] [blame] | 72 | * We enforce the MAP_FIXED case. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | */ |
| 74 | if (flags & MAP_FIXED) { |
Al Viro | e77414e | 2009-12-05 15:10:44 -0500 | [diff] [blame] | 75 | if (aliasing && flags & MAP_SHARED && |
| 76 | (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Michel Lespinasse | 394ef64 | 2012-12-11 16:02:10 -0800 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Rob Herring | 7dbaa46 | 2011-11-22 04:01:07 +0100 | [diff] [blame] | 105 | unsigned long |
| 106 | arch_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(); |
Michel Lespinasse | 394ef64 | 2012-12-11 16:02:10 -0800 | [diff] [blame] | 115 | struct vm_unmapped_area_info info; |
Rob Herring | 7dbaa46 | 2011-11-22 04:01:07 +0100 | [diff] [blame] | 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 | |
Michel Lespinasse | 394ef64 | 2012-12-11 16:02:10 -0800 | [diff] [blame] | 147 | info.flags = VM_UNMAPPED_AREA_TOPDOWN; |
| 148 | info.length = len; |
Russell King | d8aa712 | 2013-11-28 21:43:40 +0000 | [diff] [blame] | 149 | info.low_limit = FIRST_USER_ADDRESS; |
Michel Lespinasse | 394ef64 | 2012-12-11 16:02:10 -0800 | [diff] [blame] | 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); |
Rob Herring | 7dbaa46 | 2011-11-22 04:01:07 +0100 | [diff] [blame] | 154 | |
Rob Herring | 7dbaa46 | 2011-11-22 04:01:07 +0100 | [diff] [blame] | 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 | */ |
Michel Lespinasse | 394ef64 | 2012-12-11 16:02:10 -0800 | [diff] [blame] | 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 | } |
Rob Herring | 7dbaa46 | 2011-11-22 04:01:07 +0100 | [diff] [blame] | 168 | |
| 169 | return addr; |
| 170 | } |
| 171 | |
Kees Cook | 2b68f6c | 2015-04-14 15:48:00 -0700 | [diff] [blame] | 172 | unsigned long arch_mmap_rnd(void) |
Kees Cook | fbbc400 | 2015-04-14 15:47:41 -0700 | [diff] [blame] | 173 | { |
| 174 | unsigned long rnd; |
| 175 | |
| 176 | /* 8 bits of randomness in 20 address space bits */ |
| 177 | rnd = (unsigned long)get_random_int() % (1 << 8); |
| 178 | |
| 179 | return rnd << PAGE_SHIFT; |
| 180 | } |
| 181 | |
Rob Herring | 7dbaa46 | 2011-11-22 04:01:07 +0100 | [diff] [blame] | 182 | void arch_pick_mmap_layout(struct mm_struct *mm) |
| 183 | { |
| 184 | unsigned long random_factor = 0UL; |
| 185 | |
Kees Cook | fbbc400 | 2015-04-14 15:47:41 -0700 | [diff] [blame] | 186 | if (current->flags & PF_RANDOMIZE) |
Kees Cook | 2b68f6c | 2015-04-14 15:48:00 -0700 | [diff] [blame] | 187 | random_factor = arch_mmap_rnd(); |
Rob Herring | 7dbaa46 | 2011-11-22 04:01:07 +0100 | [diff] [blame] | 188 | |
| 189 | if (mmap_is_legacy()) { |
| 190 | mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; |
| 191 | mm->get_unmapped_area = arch_get_unmapped_area; |
Rob Herring | 7dbaa46 | 2011-11-22 04:01:07 +0100 | [diff] [blame] | 192 | } else { |
| 193 | mm->mmap_base = mmap_base(random_factor); |
| 194 | mm->get_unmapped_area = arch_get_unmapped_area_topdown; |
Rob Herring | 7dbaa46 | 2011-11-22 04:01:07 +0100 | [diff] [blame] | 195 | } |
| 196 | } |
Lennert Buytenhek | 51635ad | 2006-09-16 10:50:22 +0100 | [diff] [blame] | 197 | |
| 198 | /* |
| 199 | * You really shouldn't be using read() or write() on /dev/mem. This |
| 200 | * might go away in the future. |
| 201 | */ |
Cyril Chemparathy | 7e6735c | 2012-09-12 14:05:58 -0400 | [diff] [blame] | 202 | int valid_phys_addr_range(phys_addr_t addr, size_t size) |
Lennert Buytenhek | 51635ad | 2006-09-16 10:50:22 +0100 | [diff] [blame] | 203 | { |
Alexandre Rusev | 9ae3ae0 | 2008-02-26 18:42:10 +0100 | [diff] [blame] | 204 | if (addr < PHYS_OFFSET) |
| 205 | return 0; |
Greg Ungerer | 6806bfe | 2009-10-02 00:45:28 +0100 | [diff] [blame] | 206 | if (addr + size > __pa(high_memory - 1) + 1) |
Lennert Buytenhek | 51635ad | 2006-09-16 10:50:22 +0100 | [diff] [blame] | 207 | return 0; |
| 208 | |
| 209 | return 1; |
| 210 | } |
| 211 | |
| 212 | /* |
Sergey Dyasly | 3159f37 | 2013-09-24 16:38:00 +0100 | [diff] [blame] | 213 | * Do not allow /dev/mem mappings beyond the supported physical range. |
Lennert Buytenhek | 51635ad | 2006-09-16 10:50:22 +0100 | [diff] [blame] | 214 | */ |
| 215 | int valid_mmap_phys_addr_range(unsigned long pfn, size_t size) |
| 216 | { |
Sergey Dyasly | 3159f37 | 2013-09-24 16:38:00 +0100 | [diff] [blame] | 217 | return (pfn + (size >> PAGE_SHIFT)) <= (1 + (PHYS_MASK >> PAGE_SHIFT)); |
Lennert Buytenhek | 51635ad | 2006-09-16 10:50:22 +0100 | [diff] [blame] | 218 | } |
Nicolas Pitre | 087aaff | 2010-09-22 18:34:36 -0400 | [diff] [blame] | 219 | |
| 220 | #ifdef CONFIG_STRICT_DEVMEM |
| 221 | |
| 222 | #include <linux/ioport.h> |
| 223 | |
| 224 | /* |
| 225 | * devmem_is_allowed() checks to see if /dev/mem access to a certain |
| 226 | * address is valid. The argument is a physical page number. |
| 227 | * We mimic x86 here by disallowing access to system RAM as well as |
| 228 | * device-exclusive MMIO regions. This effectively disable read()/write() |
| 229 | * on /dev/mem. |
| 230 | */ |
| 231 | int devmem_is_allowed(unsigned long pfn) |
| 232 | { |
| 233 | if (iomem_is_exclusive(pfn << PAGE_SHIFT)) |
| 234 | return 0; |
| 235 | if (!page_is_ram(pfn)) |
| 236 | return 1; |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | #endif |