mmap.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * linux/arch/s390/mm/mmap.c
  3. *
  4. * flexible mmap layout support
  5. *
  6. * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
  7. * All Rights Reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. *
  24. * Started by Ingo Molnar <mingo@elte.hu>
  25. */
  26. #include <linux/personality.h>
  27. #include <linux/mm.h>
  28. #include <linux/module.h>
  29. #include <linux/random.h>
  30. #include <linux/compat.h>
  31. #include <asm/pgalloc.h>
  32. static unsigned long stack_maxrandom_size(void)
  33. {
  34. if (!(current->flags & PF_RANDOMIZE))
  35. return 0;
  36. if (current->personality & ADDR_NO_RANDOMIZE)
  37. return 0;
  38. return STACK_RND_MASK << PAGE_SHIFT;
  39. }
  40. /*
  41. * Top of mmap area (just below the process stack).
  42. *
  43. * Leave at least a ~32 MB hole.
  44. */
  45. #define MIN_GAP (32*1024*1024)
  46. #define MAX_GAP (STACK_TOP/6*5)
  47. static inline int mmap_is_legacy(void)
  48. {
  49. if (current->personality & ADDR_COMPAT_LAYOUT)
  50. return 1;
  51. if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
  52. return 1;
  53. return sysctl_legacy_va_layout;
  54. }
  55. static unsigned long mmap_rnd(void)
  56. {
  57. if (!(current->flags & PF_RANDOMIZE))
  58. return 0;
  59. /* 8MB randomization for mmap_base */
  60. return (get_random_int() & 0x7ffUL) << PAGE_SHIFT;
  61. }
  62. static inline unsigned long mmap_base(void)
  63. {
  64. unsigned long gap = rlimit(RLIMIT_STACK);
  65. if (gap < MIN_GAP)
  66. gap = MIN_GAP;
  67. else if (gap > MAX_GAP)
  68. gap = MAX_GAP;
  69. gap &= PAGE_MASK;
  70. return STACK_TOP - stack_maxrandom_size() - mmap_rnd() - gap;
  71. }
  72. #ifndef CONFIG_64BIT
  73. /*
  74. * This function, called very early during the creation of a new
  75. * process VM image, sets up which VM layout function to use:
  76. */
  77. void arch_pick_mmap_layout(struct mm_struct *mm)
  78. {
  79. /*
  80. * Fall back to the standard layout if the personality
  81. * bit is set, or if the expected stack growth is unlimited:
  82. */
  83. if (mmap_is_legacy()) {
  84. mm->mmap_base = TASK_UNMAPPED_BASE;
  85. mm->get_unmapped_area = arch_get_unmapped_area;
  86. mm->unmap_area = arch_unmap_area;
  87. } else {
  88. mm->mmap_base = mmap_base();
  89. mm->get_unmapped_area = arch_get_unmapped_area_topdown;
  90. mm->unmap_area = arch_unmap_area_topdown;
  91. }
  92. }
  93. EXPORT_SYMBOL_GPL(arch_pick_mmap_layout);
  94. #else
  95. int s390_mmap_check(unsigned long addr, unsigned long len)
  96. {
  97. if (!is_compat_task() &&
  98. len >= TASK_SIZE && TASK_SIZE < (1UL << 53))
  99. return crst_table_upgrade(current->mm, 1UL << 53);
  100. return 0;
  101. }
  102. static unsigned long
  103. s390_get_unmapped_area(struct file *filp, unsigned long addr,
  104. unsigned long len, unsigned long pgoff, unsigned long flags)
  105. {
  106. struct mm_struct *mm = current->mm;
  107. unsigned long area;
  108. int rc;
  109. area = arch_get_unmapped_area(filp, addr, len, pgoff, flags);
  110. if (!(area & ~PAGE_MASK))
  111. return area;
  112. if (area == -ENOMEM && !is_compat_task() && TASK_SIZE < (1UL << 53)) {
  113. /* Upgrade the page table to 4 levels and retry. */
  114. rc = crst_table_upgrade(mm, 1UL << 53);
  115. if (rc)
  116. return (unsigned long) rc;
  117. area = arch_get_unmapped_area(filp, addr, len, pgoff, flags);
  118. }
  119. return area;
  120. }
  121. static unsigned long
  122. s390_get_unmapped_area_topdown(struct file *filp, const unsigned long addr,
  123. const unsigned long len, const unsigned long pgoff,
  124. const unsigned long flags)
  125. {
  126. struct mm_struct *mm = current->mm;
  127. unsigned long area;
  128. int rc;
  129. area = arch_get_unmapped_area_topdown(filp, addr, len, pgoff, flags);
  130. if (!(area & ~PAGE_MASK))
  131. return area;
  132. if (area == -ENOMEM && !is_compat_task() && TASK_SIZE < (1UL << 53)) {
  133. /* Upgrade the page table to 4 levels and retry. */
  134. rc = crst_table_upgrade(mm, 1UL << 53);
  135. if (rc)
  136. return (unsigned long) rc;
  137. area = arch_get_unmapped_area_topdown(filp, addr, len,
  138. pgoff, flags);
  139. }
  140. return area;
  141. }
  142. /*
  143. * This function, called very early during the creation of a new
  144. * process VM image, sets up which VM layout function to use:
  145. */
  146. void arch_pick_mmap_layout(struct mm_struct *mm)
  147. {
  148. /*
  149. * Fall back to the standard layout if the personality
  150. * bit is set, or if the expected stack growth is unlimited:
  151. */
  152. if (mmap_is_legacy()) {
  153. mm->mmap_base = TASK_UNMAPPED_BASE;
  154. mm->get_unmapped_area = s390_get_unmapped_area;
  155. mm->unmap_area = arch_unmap_area;
  156. } else {
  157. mm->mmap_base = mmap_base();
  158. mm->get_unmapped_area = s390_get_unmapped_area_topdown;
  159. mm->unmap_area = arch_unmap_area_topdown;
  160. }
  161. }
  162. EXPORT_SYMBOL_GPL(arch_pick_mmap_layout);
  163. #endif