/*1* flexible mmap layout support2*3* Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.4* All Rights Reserved.5*6* This program is free software; you can redistribute it and/or modify7* it under the terms of the GNU General Public License as published by8* the Free Software Foundation; either version 2 of the License, or9* (at your option) any later version.10*11* This program is distributed in the hope that it will be useful,12* but WITHOUT ANY WARRANTY; without even the implied warranty of13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14* GNU General Public License for more details.15*16* You should have received a copy of the GNU General Public License17* along with this program; if not, write to the Free Software18* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA19*20*21* Started by Ingo Molnar <[email protected]>22*/2324#include <linux/personality.h>25#include <linux/mm.h>26#include <linux/random.h>27#include <linux/sched.h>2829/*30* Top of mmap area (just below the process stack).31*32* Leave at least a ~128 MB hole on 32bit applications.33*34* On 64bit applications we randomise the stack by 1GB so we need to35* space our mmap start address by a further 1GB, otherwise there is a36* chance the mmap area will end up closer to the stack than our ulimit37* requires.38*/39#define MIN_GAP32 (128*1024*1024)40#define MIN_GAP64 ((128 + 1024)*1024*1024UL)41#define MIN_GAP ((is_32bit_task()) ? MIN_GAP32 : MIN_GAP64)42#define MAX_GAP (TASK_SIZE/6*5)4344static inline int mmap_is_legacy(void)45{46if (current->personality & ADDR_COMPAT_LAYOUT)47return 1;4849if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)50return 1;5152return sysctl_legacy_va_layout;53}5455/*56* Since get_random_int() returns the same value within a 1 jiffy window,57* we will almost always get the same randomisation for the stack and mmap58* region. This will mean the relative distance between stack and mmap will59* be the same.60*61* To avoid this we can shift the randomness by 1 bit.62*/63static unsigned long mmap_rnd(void)64{65unsigned long rnd = 0;6667if (current->flags & PF_RANDOMIZE) {68/* 8MB for 32bit, 1GB for 64bit */69if (is_32bit_task())70rnd = (long)(get_random_int() % (1<<(22-PAGE_SHIFT)));71else72rnd = (long)(get_random_int() % (1<<(29-PAGE_SHIFT)));73}74return (rnd << PAGE_SHIFT) * 2;75}7677static inline unsigned long mmap_base(void)78{79unsigned long gap = rlimit(RLIMIT_STACK);8081if (gap < MIN_GAP)82gap = MIN_GAP;83else if (gap > MAX_GAP)84gap = MAX_GAP;8586return PAGE_ALIGN(TASK_SIZE - gap - mmap_rnd());87}8889/*90* This function, called very early during the creation of a new91* process VM image, sets up which VM layout function to use:92*/93void arch_pick_mmap_layout(struct mm_struct *mm)94{95/*96* Fall back to the standard layout if the personality97* bit is set, or if the expected stack growth is unlimited:98*/99if (mmap_is_legacy()) {100mm->mmap_base = TASK_UNMAPPED_BASE;101mm->get_unmapped_area = arch_get_unmapped_area;102mm->unmap_area = arch_unmap_area;103} else {104mm->mmap_base = mmap_base();105mm->get_unmapped_area = arch_get_unmapped_area_topdown;106mm->unmap_area = arch_unmap_area_topdown;107}108}109110111