2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2011 Wind River Systems,
7 * written by Ralf Baechle <ralf@linux-mips.org>
9 #include <linux/compiler.h>
10 #include <linux/elf-randomize.h>
11 #include <linux/errno.h>
13 #include <linux/mman.h>
14 #include <linux/export.h>
15 #include <linux/personality.h>
16 #include <linux/random.h>
17 #include <linux/sched/signal.h>
18 #include <linux/sched/mm.h>
20 unsigned long shm_align_mask
= PAGE_SIZE
- 1; /* Sane caches */
21 EXPORT_SYMBOL(shm_align_mask
);
23 /* gap between mmap and stack */
24 #define MIN_GAP (128*1024*1024UL)
25 #define MAX_GAP ((TASK_SIZE)/6*5)
27 static int mmap_is_legacy(struct rlimit
*rlim_stack
)
29 if (current
->personality
& ADDR_COMPAT_LAYOUT
)
32 if (rlim_stack
->rlim_cur
== RLIM_INFINITY
)
35 return sysctl_legacy_va_layout
;
38 static unsigned long mmap_base(unsigned long rnd
, struct rlimit
*rlim_stack
)
40 unsigned long gap
= rlim_stack
->rlim_cur
;
44 else if (gap
> MAX_GAP
)
47 return PAGE_ALIGN(TASK_SIZE
- gap
- rnd
);
50 #define COLOUR_ALIGN(addr, pgoff) \
51 ((((addr) + shm_align_mask) & ~shm_align_mask) + \
52 (((pgoff) << PAGE_SHIFT) & shm_align_mask))
54 enum mmap_allocation_direction
{UP
, DOWN
};
56 static unsigned long arch_get_unmapped_area_common(struct file
*filp
,
57 unsigned long addr0
, unsigned long len
, unsigned long pgoff
,
58 unsigned long flags
, enum mmap_allocation_direction dir
)
60 struct mm_struct
*mm
= current
->mm
;
61 struct vm_area_struct
*vma
;
62 unsigned long addr
= addr0
;
64 struct vm_unmapped_area_info info
;
66 if (unlikely(len
> TASK_SIZE
))
69 if (flags
& MAP_FIXED
) {
70 /* Even MAP_FIXED mappings must reside within TASK_SIZE */
71 if (TASK_SIZE
- len
< addr
)
75 * We do not accept a shared mapping if it would violate
76 * cache aliasing constraints.
78 if ((flags
& MAP_SHARED
) &&
79 ((addr
- (pgoff
<< PAGE_SHIFT
)) & shm_align_mask
))
85 if (filp
|| (flags
& MAP_SHARED
))
88 /* requesting a specific address */
91 addr
= COLOUR_ALIGN(addr
, pgoff
);
93 addr
= PAGE_ALIGN(addr
);
95 vma
= find_vma(mm
, addr
);
96 if (TASK_SIZE
- len
>= addr
&&
97 (!vma
|| addr
+ len
<= vm_start_gap(vma
)))
102 info
.align_mask
= do_color_align
? (PAGE_MASK
& shm_align_mask
) : 0;
103 info
.align_offset
= pgoff
<< PAGE_SHIFT
;
106 info
.flags
= VM_UNMAPPED_AREA_TOPDOWN
;
107 info
.low_limit
= PAGE_SIZE
;
108 info
.high_limit
= mm
->mmap_base
;
109 addr
= vm_unmapped_area(&info
);
111 if (!(addr
& ~PAGE_MASK
))
115 * A failed mmap() very likely causes application failure,
116 * so fall back to the bottom-up function here. This scenario
117 * can happen with large stack limits and large mmap()
123 info
.low_limit
= mm
->mmap_base
;
124 info
.high_limit
= TASK_SIZE
;
125 return vm_unmapped_area(&info
);
128 unsigned long arch_get_unmapped_area(struct file
*filp
, unsigned long addr0
,
129 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
131 return arch_get_unmapped_area_common(filp
,
132 addr0
, len
, pgoff
, flags
, UP
);
136 * There is no need to export this but sched.h declares the function as
137 * extern so making it static here results in an error.
139 unsigned long arch_get_unmapped_area_topdown(struct file
*filp
,
140 unsigned long addr0
, unsigned long len
, unsigned long pgoff
,
143 return arch_get_unmapped_area_common(filp
,
144 addr0
, len
, pgoff
, flags
, DOWN
);
147 unsigned long arch_mmap_rnd(void)
152 if (TASK_IS_32BIT_ADDR
)
153 rnd
= get_random_long() & ((1UL << mmap_rnd_compat_bits
) - 1);
155 #endif /* CONFIG_COMPAT */
156 rnd
= get_random_long() & ((1UL << mmap_rnd_bits
) - 1);
158 return rnd
<< PAGE_SHIFT
;
161 void arch_pick_mmap_layout(struct mm_struct
*mm
, struct rlimit
*rlim_stack
)
163 unsigned long random_factor
= 0UL;
165 if (current
->flags
& PF_RANDOMIZE
)
166 random_factor
= arch_mmap_rnd();
168 if (mmap_is_legacy(rlim_stack
)) {
169 mm
->mmap_base
= TASK_UNMAPPED_BASE
+ random_factor
;
170 mm
->get_unmapped_area
= arch_get_unmapped_area
;
172 mm
->mmap_base
= mmap_base(random_factor
, rlim_stack
);
173 mm
->get_unmapped_area
= arch_get_unmapped_area_topdown
;
177 static inline unsigned long brk_rnd(void)
179 unsigned long rnd
= get_random_long();
181 rnd
= rnd
<< PAGE_SHIFT
;
182 /* 8MB for 32bit, 256MB for 64bit */
183 if (TASK_IS_32BIT_ADDR
)
184 rnd
= rnd
& 0x7ffffful
;
186 rnd
= rnd
& 0xffffffful
;
191 unsigned long arch_randomize_brk(struct mm_struct
*mm
)
193 unsigned long base
= mm
->brk
;
196 ret
= PAGE_ALIGN(base
+ brk_rnd());
204 int __virt_addr_valid(const volatile void *kaddr
)
206 return pfn_valid(PFN_DOWN(virt_to_phys(kaddr
)));
208 EXPORT_SYMBOL_GPL(__virt_addr_valid
);