Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * flexible mmap layout support |
| 3 | * |
| 4 | * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina. |
| 5 | * All Rights Reserved. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | * |
| 22 | * Started by Ingo Molnar <mingo@elte.hu> |
| 23 | */ |
| 24 | |
| 25 | #include <linux/personality.h> |
| 26 | #include <linux/mm.h> |
Martin Schwidefsky | 638ad34 | 2011-10-30 15:17:13 +0100 | [diff] [blame] | 27 | #include <linux/mman.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/module.h> |
Heiko Carstens | df1ca53 | 2011-01-12 09:55:27 +0100 | [diff] [blame] | 29 | #include <linux/random.h> |
Heiko Carstens | 048cd4e | 2012-02-27 10:01:52 +0100 | [diff] [blame] | 30 | #include <linux/compat.h> |
Martin Schwidefsky | 1f6b83e | 2015-01-14 17:51:17 +0100 | [diff] [blame] | 31 | #include <linux/security.h> |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 32 | #include <asm/pgalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Martin Schwidefsky | 1f6b83e | 2015-01-14 17:51:17 +0100 | [diff] [blame] | 34 | unsigned long mmap_rnd_mask; |
Heiko Carstens | 3ddb1b7 | 2015-03-16 12:44:10 +0100 | [diff] [blame^] | 35 | static unsigned long mmap_align_mask; |
Martin Schwidefsky | 1f6b83e | 2015-01-14 17:51:17 +0100 | [diff] [blame] | 36 | |
Heiko Carstens | 9046e40 | 2011-01-12 09:55:22 +0100 | [diff] [blame] | 37 | static unsigned long stack_maxrandom_size(void) |
| 38 | { |
| 39 | if (!(current->flags & PF_RANDOMIZE)) |
| 40 | return 0; |
| 41 | if (current->personality & ADDR_NO_RANDOMIZE) |
| 42 | return 0; |
| 43 | return STACK_RND_MASK << PAGE_SHIFT; |
| 44 | } |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | /* |
| 47 | * Top of mmap area (just below the process stack). |
| 48 | * |
Heiko Carstens | 9e78a13 | 2011-01-12 09:55:23 +0100 | [diff] [blame] | 49 | * Leave at least a ~32 MB hole. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | */ |
Heiko Carstens | 9e78a13 | 2011-01-12 09:55:23 +0100 | [diff] [blame] | 51 | #define MIN_GAP (32*1024*1024) |
Martin Schwidefsky | f481bfa | 2009-03-18 13:27:36 +0100 | [diff] [blame] | 52 | #define MAX_GAP (STACK_TOP/6*5) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Heiko Carstens | 1060f62 | 2011-01-12 09:55:26 +0100 | [diff] [blame] | 54 | static inline int mmap_is_legacy(void) |
| 55 | { |
| 56 | if (current->personality & ADDR_COMPAT_LAYOUT) |
| 57 | return 1; |
| 58 | if (rlimit(RLIMIT_STACK) == RLIM_INFINITY) |
| 59 | return 1; |
| 60 | return sysctl_legacy_va_layout; |
| 61 | } |
| 62 | |
Heiko Carstens | df1ca53 | 2011-01-12 09:55:27 +0100 | [diff] [blame] | 63 | static unsigned long mmap_rnd(void) |
| 64 | { |
| 65 | if (!(current->flags & PF_RANDOMIZE)) |
| 66 | return 0; |
Martin Schwidefsky | 1f6b83e | 2015-01-14 17:51:17 +0100 | [diff] [blame] | 67 | if (is_32bit_task()) |
| 68 | return (get_random_int() & 0x7ff) << PAGE_SHIFT; |
| 69 | else |
| 70 | return (get_random_int() & mmap_rnd_mask) << PAGE_SHIFT; |
Heiko Carstens | df1ca53 | 2011-01-12 09:55:27 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Heiko Carstens | 7aba842 | 2013-11-12 15:07:55 -0800 | [diff] [blame] | 73 | static unsigned long mmap_base_legacy(void) |
| 74 | { |
| 75 | return TASK_UNMAPPED_BASE + mmap_rnd(); |
| 76 | } |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | static inline unsigned long mmap_base(void) |
| 79 | { |
Jiri Slaby | a58c26b | 2010-01-13 20:44:33 +0100 | [diff] [blame] | 80 | unsigned long gap = rlimit(RLIMIT_STACK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
| 82 | if (gap < MIN_GAP) |
| 83 | gap = MIN_GAP; |
| 84 | else if (gap > MAX_GAP) |
| 85 | gap = MAX_GAP; |
Heiko Carstens | df1ca53 | 2011-01-12 09:55:27 +0100 | [diff] [blame] | 86 | gap &= PAGE_MASK; |
| 87 | return STACK_TOP - stack_maxrandom_size() - mmap_rnd() - gap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Martin Schwidefsky | 1f6b83e | 2015-01-14 17:51:17 +0100 | [diff] [blame] | 90 | unsigned long |
| 91 | arch_get_unmapped_area(struct file *filp, unsigned long addr, |
| 92 | unsigned long len, unsigned long pgoff, unsigned long flags) |
| 93 | { |
| 94 | struct mm_struct *mm = current->mm; |
| 95 | struct vm_area_struct *vma; |
| 96 | struct vm_unmapped_area_info info; |
| 97 | int do_color_align; |
| 98 | |
| 99 | if (len > TASK_SIZE - mmap_min_addr) |
| 100 | return -ENOMEM; |
| 101 | |
| 102 | if (flags & MAP_FIXED) |
| 103 | return addr; |
| 104 | |
| 105 | if (addr) { |
| 106 | addr = PAGE_ALIGN(addr); |
| 107 | vma = find_vma(mm, addr); |
| 108 | if (TASK_SIZE - len >= addr && addr >= mmap_min_addr && |
| 109 | (!vma || addr + len <= vma->vm_start)) |
| 110 | return addr; |
| 111 | } |
| 112 | |
| 113 | do_color_align = 0; |
| 114 | if (filp || (flags & MAP_SHARED)) |
| 115 | do_color_align = !is_32bit_task(); |
| 116 | |
| 117 | info.flags = 0; |
| 118 | info.length = len; |
| 119 | info.low_limit = mm->mmap_base; |
| 120 | info.high_limit = TASK_SIZE; |
| 121 | info.align_mask = do_color_align ? (mmap_align_mask << PAGE_SHIFT) : 0; |
| 122 | info.align_offset = pgoff << PAGE_SHIFT; |
| 123 | return vm_unmapped_area(&info); |
| 124 | } |
| 125 | |
| 126 | unsigned long |
| 127 | arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, |
| 128 | const unsigned long len, const unsigned long pgoff, |
| 129 | const unsigned long flags) |
| 130 | { |
| 131 | struct vm_area_struct *vma; |
| 132 | struct mm_struct *mm = current->mm; |
| 133 | unsigned long addr = addr0; |
| 134 | struct vm_unmapped_area_info info; |
| 135 | int do_color_align; |
| 136 | |
| 137 | /* requested length too big for entire address space */ |
| 138 | if (len > TASK_SIZE - mmap_min_addr) |
| 139 | return -ENOMEM; |
| 140 | |
| 141 | if (flags & MAP_FIXED) |
| 142 | return addr; |
| 143 | |
| 144 | /* requesting a specific address */ |
| 145 | if (addr) { |
| 146 | addr = PAGE_ALIGN(addr); |
| 147 | vma = find_vma(mm, addr); |
| 148 | if (TASK_SIZE - len >= addr && addr >= mmap_min_addr && |
| 149 | (!vma || addr + len <= vma->vm_start)) |
| 150 | return addr; |
| 151 | } |
| 152 | |
| 153 | do_color_align = 0; |
| 154 | if (filp || (flags & MAP_SHARED)) |
| 155 | do_color_align = !is_32bit_task(); |
| 156 | |
| 157 | info.flags = VM_UNMAPPED_AREA_TOPDOWN; |
| 158 | info.length = len; |
| 159 | info.low_limit = max(PAGE_SIZE, mmap_min_addr); |
| 160 | info.high_limit = mm->mmap_base; |
| 161 | info.align_mask = do_color_align ? (mmap_align_mask << PAGE_SHIFT) : 0; |
| 162 | info.align_offset = pgoff << PAGE_SHIFT; |
| 163 | addr = vm_unmapped_area(&info); |
| 164 | |
| 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 | */ |
| 171 | if (addr & ~PAGE_MASK) { |
| 172 | VM_BUG_ON(addr != -ENOMEM); |
| 173 | info.flags = 0; |
| 174 | info.low_limit = TASK_UNMAPPED_BASE; |
| 175 | info.high_limit = TASK_SIZE; |
| 176 | addr = vm_unmapped_area(&info); |
| 177 | } |
| 178 | |
| 179 | return addr; |
| 180 | } |
| 181 | |
| 182 | unsigned long randomize_et_dyn(void) |
| 183 | { |
| 184 | unsigned long base; |
| 185 | |
Martin Schwidefsky | 4ba2815 | 2015-02-12 14:17:52 +0100 | [diff] [blame] | 186 | base = STACK_TOP / 3 * 2; |
| 187 | if (!is_32bit_task()) |
| 188 | /* Align to 4GB */ |
| 189 | base &= ~((1UL << 32) - 1); |
Martin Schwidefsky | 1f6b83e | 2015-01-14 17:51:17 +0100 | [diff] [blame] | 190 | return base + mmap_rnd(); |
| 191 | } |
| 192 | |
Hendrik Brueckner | 486c0a0 | 2013-02-11 14:29:49 +0100 | [diff] [blame] | 193 | int s390_mmap_check(unsigned long addr, unsigned long len, unsigned long flags) |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 194 | { |
Hendrik Brueckner | 486c0a0 | 2013-02-11 14:29:49 +0100 | [diff] [blame] | 195 | if (is_compat_task() || (TASK_SIZE >= (1UL << 53))) |
| 196 | return 0; |
| 197 | if (!(flags & MAP_FIXED)) |
| 198 | addr = 0; |
Martin Schwidefsky | 1060786 | 2013-10-28 14:48:30 +0100 | [diff] [blame] | 199 | if ((addr + len) >= TASK_SIZE) |
| 200 | return crst_table_upgrade(current->mm, 1UL << 53); |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 201 | return 0; |
| 202 | } |
| 203 | |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 204 | static unsigned long |
| 205 | s390_get_unmapped_area(struct file *filp, unsigned long addr, |
| 206 | unsigned long len, unsigned long pgoff, unsigned long flags) |
| 207 | { |
| 208 | struct mm_struct *mm = current->mm; |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 209 | unsigned long area; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 210 | int rc; |
| 211 | |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 212 | area = arch_get_unmapped_area(filp, addr, len, pgoff, flags); |
| 213 | if (!(area & ~PAGE_MASK)) |
| 214 | return area; |
Heiko Carstens | 7757591 | 2009-06-12 10:26:25 +0200 | [diff] [blame] | 215 | if (area == -ENOMEM && !is_compat_task() && TASK_SIZE < (1UL << 53)) { |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 216 | /* Upgrade the page table to 4 levels and retry. */ |
| 217 | rc = crst_table_upgrade(mm, 1UL << 53); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 218 | if (rc) |
| 219 | return (unsigned long) rc; |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 220 | area = arch_get_unmapped_area(filp, addr, len, pgoff, flags); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 221 | } |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 222 | return area; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static unsigned long |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 226 | s390_get_unmapped_area_topdown(struct file *filp, const unsigned long addr, |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 227 | const unsigned long len, const unsigned long pgoff, |
| 228 | const unsigned long flags) |
| 229 | { |
| 230 | struct mm_struct *mm = current->mm; |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 231 | unsigned long area; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 232 | int rc; |
| 233 | |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 234 | area = arch_get_unmapped_area_topdown(filp, addr, len, pgoff, flags); |
| 235 | if (!(area & ~PAGE_MASK)) |
| 236 | return area; |
Heiko Carstens | 7757591 | 2009-06-12 10:26:25 +0200 | [diff] [blame] | 237 | if (area == -ENOMEM && !is_compat_task() && TASK_SIZE < (1UL << 53)) { |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 238 | /* Upgrade the page table to 4 levels and retry. */ |
| 239 | rc = crst_table_upgrade(mm, 1UL << 53); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 240 | if (rc) |
| 241 | return (unsigned long) rc; |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 242 | area = arch_get_unmapped_area_topdown(filp, addr, len, |
| 243 | pgoff, flags); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 244 | } |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 245 | return area; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 246 | } |
| 247 | /* |
| 248 | * This function, called very early during the creation of a new |
| 249 | * process VM image, sets up which VM layout function to use: |
| 250 | */ |
| 251 | void arch_pick_mmap_layout(struct mm_struct *mm) |
| 252 | { |
| 253 | /* |
| 254 | * Fall back to the standard layout if the personality |
| 255 | * bit is set, or if the expected stack growth is unlimited: |
| 256 | */ |
| 257 | if (mmap_is_legacy()) { |
Heiko Carstens | 7aba842 | 2013-11-12 15:07:55 -0800 | [diff] [blame] | 258 | mm->mmap_base = mmap_base_legacy(); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 259 | mm->get_unmapped_area = s390_get_unmapped_area; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 260 | } else { |
| 261 | mm->mmap_base = mmap_base(); |
| 262 | mm->get_unmapped_area = s390_get_unmapped_area_topdown; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 263 | } |
| 264 | } |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 265 | |
Martin Schwidefsky | 1f6b83e | 2015-01-14 17:51:17 +0100 | [diff] [blame] | 266 | static int __init setup_mmap_rnd(void) |
| 267 | { |
| 268 | struct cpuid cpu_id; |
| 269 | |
| 270 | get_cpu_id(&cpu_id); |
| 271 | switch (cpu_id.machine) { |
| 272 | case 0x9672: |
| 273 | case 0x2064: |
| 274 | case 0x2066: |
| 275 | case 0x2084: |
| 276 | case 0x2086: |
| 277 | case 0x2094: |
| 278 | case 0x2096: |
| 279 | case 0x2097: |
| 280 | case 0x2098: |
| 281 | case 0x2817: |
| 282 | case 0x2818: |
| 283 | case 0x2827: |
| 284 | case 0x2828: |
| 285 | mmap_rnd_mask = 0x7ffUL; |
| 286 | mmap_align_mask = 0UL; |
| 287 | break; |
| 288 | case 0x2964: /* z13 */ |
| 289 | default: |
| 290 | mmap_rnd_mask = 0x3ff80UL; |
| 291 | mmap_align_mask = 0x7fUL; |
| 292 | break; |
| 293 | } |
| 294 | return 0; |
| 295 | } |
| 296 | early_initcall(setup_mmap_rnd); |