/*1* Flexible mmap layout support2*3* Based on code by Ingo Molnar and Andi Kleen, copyrighted4* as follows:5*6* Copyright 2003-2009 Red Hat Inc.7* All Rights Reserved.8* Copyright 2005 Andi Kleen, SUSE Labs.9* Copyright 2007 Jiri Kosina, SUSE Labs.10*11* This program is free software; you can redistribute it and/or modify12* it under the terms of the GNU General Public License as published by13* the Free Software Foundation; either version 2 of the License, or14* (at your option) any later version.15*16* This program is distributed in the hope that it will be useful,17* but WITHOUT ANY WARRANTY; without even the implied warranty of18* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the19* GNU General Public License for more details.20*21* You should have received a copy of the GNU General Public License22* along with this program; if not, write to the Free Software23* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA24*/2526#include <linux/personality.h>27#include <linux/mm.h>28#include <linux/random.h>29#include <linux/limits.h>30#include <linux/sched.h>31#include <asm/elf.h>3233static unsigned int stack_maxrandom_size(void)34{35unsigned int max = 0;36if ((current->flags & PF_RANDOMIZE) &&37!(current->personality & ADDR_NO_RANDOMIZE)) {38max = ((-1U) & STACK_RND_MASK) << PAGE_SHIFT;39}4041return max;42}434445/*46* Top of mmap area (just below the process stack).47*48* Leave an at least ~128 MB hole with possible stack randomization.49*/50#define MIN_GAP (128*1024*1024UL + stack_maxrandom_size())51#define MAX_GAP (TASK_SIZE/6*5)5253/*54* True on X86_32 or when emulating IA32 on X86_6455*/56static int mmap_is_ia32(void)57{58#ifdef CONFIG_X86_3259return 1;60#endif61#ifdef CONFIG_IA32_EMULATION62if (test_thread_flag(TIF_IA32))63return 1;64#endif65return 0;66}6768static int mmap_is_legacy(void)69{70if (current->personality & ADDR_COMPAT_LAYOUT)71return 1;7273if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)74return 1;7576return sysctl_legacy_va_layout;77}7879static unsigned long mmap_rnd(void)80{81unsigned long rnd = 0;8283/*84* 8 bits of randomness in 32bit mmaps, 20 address space bits85* 28 bits of randomness in 64bit mmaps, 40 address space bits86*/87if (current->flags & PF_RANDOMIZE) {88if (mmap_is_ia32())89rnd = (long)get_random_int() % (1<<8);90else91rnd = (long)(get_random_int() % (1<<28));92}93return rnd << PAGE_SHIFT;94}9596static unsigned long mmap_base(void)97{98unsigned long gap = rlimit(RLIMIT_STACK);99100if (gap < MIN_GAP)101gap = MIN_GAP;102else if (gap > MAX_GAP)103gap = MAX_GAP;104105return PAGE_ALIGN(TASK_SIZE - gap - mmap_rnd());106}107108/*109* Bottom-up (legacy) layout on X86_32 did not support randomization, X86_64110* does, but not when emulating X86_32111*/112static unsigned long mmap_legacy_base(void)113{114if (mmap_is_ia32())115return TASK_UNMAPPED_BASE;116else117return TASK_UNMAPPED_BASE + mmap_rnd();118}119120/*121* This function, called very early during the creation of a new122* process VM image, sets up which VM layout function to use:123*/124void arch_pick_mmap_layout(struct mm_struct *mm)125{126if (mmap_is_legacy()) {127mm->mmap_base = mmap_legacy_base();128mm->get_unmapped_area = arch_get_unmapped_area;129mm->unmap_area = arch_unmap_area;130} else {131mm->mmap_base = mmap_base();132mm->get_unmapped_area = arch_get_unmapped_area_topdown;133mm->unmap_area = arch_unmap_area_topdown;134}135}136137138