mmap.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  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. #include <linux/personality.h>
  25. #include <linux/mm.h>
  26. #include <linux/mman.h>
  27. #include <linux/module.h>
  28. #include <linux/random.h>
  29. #include <linux/compat.h>
  30. #include <linux/security.h>
  31. #include <asm/pgalloc.h>
  32. unsigned long mmap_rnd_mask;
  33. static unsigned long mmap_align_mask;
  34. static unsigned long stack_maxrandom_size(void)
  35. {
  36. if (!(current->flags & PF_RANDOMIZE))
  37. return 0;
  38. if (current->personality & ADDR_NO_RANDOMIZE)
  39. return 0;
  40. return STACK_RND_MASK << PAGE_SHIFT;
  41. }
  42. /*
  43. * Top of mmap area (just below the process stack).
  44. *
  45. * Leave at least a ~32 MB hole.
  46. */
  47. #define MIN_GAP (32*1024*1024)
  48. #define MAX_GAP (STACK_TOP/6*5)
  49. static inline int mmap_is_legacy(void)
  50. {
  51. if (current->personality & ADDR_COMPAT_LAYOUT)
  52. return 1;
  53. if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
  54. return 1;
  55. return sysctl_legacy_va_layout;
  56. }
  57. unsigned long arch_mmap_rnd(void)
  58. {
  59. if (is_32bit_task())
  60. return (get_random_int() & 0x7ff) << PAGE_SHIFT;
  61. else
  62. return (get_random_int() & mmap_rnd_mask) << PAGE_SHIFT;
  63. }
  64. static unsigned long mmap_base_legacy(unsigned long rnd)
  65. {
  66. return TASK_UNMAPPED_BASE + rnd;
  67. }
  68. static inline unsigned long mmap_base(unsigned long rnd)
  69. {
  70. unsigned long gap = rlimit(RLIMIT_STACK);
  71. if (gap < MIN_GAP)
  72. gap = MIN_GAP;
  73. else if (gap > MAX_GAP)
  74. gap = MAX_GAP;
  75. gap &= PAGE_MASK;
  76. return STACK_TOP - stack_maxrandom_size() - rnd - gap;
  77. }
  78. unsigned long
  79. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  80. unsigned long len, unsigned long pgoff, unsigned long flags)
  81. {
  82. struct mm_struct *mm = current->mm;
  83. struct vm_area_struct *vma;
  84. struct vm_unmapped_area_info info;
  85. int do_color_align;
  86. if (len > TASK_SIZE - mmap_min_addr)
  87. return -ENOMEM;
  88. if (flags & MAP_FIXED)
  89. return addr;
  90. if (addr) {
  91. addr = PAGE_ALIGN(addr);
  92. vma = find_vma(mm, addr);
  93. if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
  94. (!vma || addr + len <= vma->vm_start))
  95. return addr;
  96. }
  97. do_color_align = 0;
  98. if (filp || (flags & MAP_SHARED))
  99. do_color_align = !is_32bit_task();
  100. info.flags = 0;
  101. info.length = len;
  102. info.low_limit = mm->mmap_base;
  103. info.high_limit = TASK_SIZE;
  104. info.align_mask = do_color_align ? (mmap_align_mask << PAGE_SHIFT) : 0;
  105. info.align_offset = pgoff << PAGE_SHIFT;
  106. return vm_unmapped_area(&info);
  107. }
  108. unsigned long
  109. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  110. const unsigned long len, const unsigned long pgoff,
  111. const unsigned long flags)
  112. {
  113. struct vm_area_struct *vma;
  114. struct mm_struct *mm = current->mm;
  115. unsigned long addr = addr0;
  116. struct vm_unmapped_area_info info;
  117. int do_color_align;
  118. /* requested length too big for entire address space */
  119. if (len > TASK_SIZE - mmap_min_addr)
  120. return -ENOMEM;
  121. if (flags & MAP_FIXED)
  122. return addr;
  123. /* requesting a specific address */
  124. if (addr) {
  125. addr = PAGE_ALIGN(addr);
  126. vma = find_vma(mm, addr);
  127. if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
  128. (!vma || addr + len <= vma->vm_start))
  129. return addr;
  130. }
  131. do_color_align = 0;
  132. if (filp || (flags & MAP_SHARED))
  133. do_color_align = !is_32bit_task();
  134. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  135. info.length = len;
  136. info.low_limit = max(PAGE_SIZE, mmap_min_addr);
  137. info.high_limit = mm->mmap_base;
  138. info.align_mask = do_color_align ? (mmap_align_mask << PAGE_SHIFT) : 0;
  139. info.align_offset = pgoff << PAGE_SHIFT;
  140. addr = vm_unmapped_area(&info);
  141. /*
  142. * A failed mmap() very likely causes application failure,
  143. * so fall back to the bottom-up function here. This scenario
  144. * can happen with large stack limits and large mmap()
  145. * allocations.
  146. */
  147. if (addr & ~PAGE_MASK) {
  148. VM_BUG_ON(addr != -ENOMEM);
  149. info.flags = 0;
  150. info.low_limit = TASK_UNMAPPED_BASE;
  151. info.high_limit = TASK_SIZE;
  152. addr = vm_unmapped_area(&info);
  153. }
  154. return addr;
  155. }
  156. int s390_mmap_check(unsigned long addr, unsigned long len, unsigned long flags)
  157. {
  158. if (is_compat_task() || (TASK_SIZE >= (1UL << 53)))
  159. return 0;
  160. if (!(flags & MAP_FIXED))
  161. addr = 0;
  162. if ((addr + len) >= TASK_SIZE)
  163. return crst_table_upgrade(current->mm, 1UL << 53);
  164. return 0;
  165. }
  166. static unsigned long
  167. s390_get_unmapped_area(struct file *filp, unsigned long addr,
  168. unsigned long len, unsigned long pgoff, unsigned long flags)
  169. {
  170. struct mm_struct *mm = current->mm;
  171. unsigned long area;
  172. int rc;
  173. area = arch_get_unmapped_area(filp, addr, len, pgoff, flags);
  174. if (!(area & ~PAGE_MASK))
  175. return area;
  176. if (area == -ENOMEM && !is_compat_task() && TASK_SIZE < (1UL << 53)) {
  177. /* Upgrade the page table to 4 levels and retry. */
  178. rc = crst_table_upgrade(mm, 1UL << 53);
  179. if (rc)
  180. return (unsigned long) rc;
  181. area = arch_get_unmapped_area(filp, addr, len, pgoff, flags);
  182. }
  183. return area;
  184. }
  185. static unsigned long
  186. s390_get_unmapped_area_topdown(struct file *filp, const unsigned long addr,
  187. const unsigned long len, const unsigned long pgoff,
  188. const unsigned long flags)
  189. {
  190. struct mm_struct *mm = current->mm;
  191. unsigned long area;
  192. int rc;
  193. area = arch_get_unmapped_area_topdown(filp, addr, len, pgoff, flags);
  194. if (!(area & ~PAGE_MASK))
  195. return area;
  196. if (area == -ENOMEM && !is_compat_task() && TASK_SIZE < (1UL << 53)) {
  197. /* Upgrade the page table to 4 levels and retry. */
  198. rc = crst_table_upgrade(mm, 1UL << 53);
  199. if (rc)
  200. return (unsigned long) rc;
  201. area = arch_get_unmapped_area_topdown(filp, addr, len,
  202. pgoff, flags);
  203. }
  204. return area;
  205. }
  206. /*
  207. * This function, called very early during the creation of a new
  208. * process VM image, sets up which VM layout function to use:
  209. */
  210. void arch_pick_mmap_layout(struct mm_struct *mm)
  211. {
  212. unsigned long random_factor = 0UL;
  213. if (current->flags & PF_RANDOMIZE)
  214. random_factor = arch_mmap_rnd();
  215. /*
  216. * Fall back to the standard layout if the personality
  217. * bit is set, or if the expected stack growth is unlimited:
  218. */
  219. if (mmap_is_legacy()) {
  220. mm->mmap_base = mmap_base_legacy(random_factor);
  221. mm->get_unmapped_area = s390_get_unmapped_area;
  222. } else {
  223. mm->mmap_base = mmap_base(random_factor);
  224. mm->get_unmapped_area = s390_get_unmapped_area_topdown;
  225. }
  226. }
  227. static int __init setup_mmap_rnd(void)
  228. {
  229. struct cpuid cpu_id;
  230. get_cpu_id(&cpu_id);
  231. switch (cpu_id.machine) {
  232. case 0x9672:
  233. case 0x2064:
  234. case 0x2066:
  235. case 0x2084:
  236. case 0x2086:
  237. case 0x2094:
  238. case 0x2096:
  239. case 0x2097:
  240. case 0x2098:
  241. case 0x2817:
  242. case 0x2818:
  243. case 0x2827:
  244. case 0x2828:
  245. mmap_rnd_mask = 0x7ffUL;
  246. mmap_align_mask = 0UL;
  247. break;
  248. case 0x2964: /* z13 */
  249. default:
  250. mmap_rnd_mask = 0x3ff80UL;
  251. mmap_align_mask = 0x7fUL;
  252. break;
  253. }
  254. return 0;
  255. }
  256. early_initcall(setup_mmap_rnd);