mmap.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/mman.h>
  29. #include <linux/module.h>
  30. #include <linux/random.h>
  31. #include <linux/compat.h>
  32. #include <asm/pgalloc.h>
  33. static unsigned long stack_maxrandom_size(void)
  34. {
  35. if (!(current->flags & PF_RANDOMIZE))
  36. return 0;
  37. if (current->personality & ADDR_NO_RANDOMIZE)
  38. return 0;
  39. return STACK_RND_MASK << PAGE_SHIFT;
  40. }
  41. /*
  42. * Top of mmap area (just below the process stack).
  43. *
  44. * Leave at least a ~32 MB hole.
  45. */
  46. #define MIN_GAP (32*1024*1024)
  47. #define MAX_GAP (STACK_TOP/6*5)
  48. static inline int mmap_is_legacy(void)
  49. {
  50. if (current->personality & ADDR_COMPAT_LAYOUT)
  51. return 1;
  52. if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
  53. return 1;
  54. return sysctl_legacy_va_layout;
  55. }
  56. static unsigned long mmap_rnd(void)
  57. {
  58. if (!(current->flags & PF_RANDOMIZE))
  59. return 0;
  60. /* 8MB randomization for mmap_base */
  61. return (get_random_int() & 0x7ffUL) << PAGE_SHIFT;
  62. }
  63. static inline unsigned long mmap_base(void)
  64. {
  65. unsigned long gap = rlimit(RLIMIT_STACK);
  66. if (gap < MIN_GAP)
  67. gap = MIN_GAP;
  68. else if (gap > MAX_GAP)
  69. gap = MAX_GAP;
  70. gap &= PAGE_MASK;
  71. return STACK_TOP - stack_maxrandom_size() - mmap_rnd() - gap;
  72. }
  73. #ifndef CONFIG_64BIT
  74. /*
  75. * This function, called very early during the creation of a new
  76. * process VM image, sets up which VM layout function to use:
  77. */
  78. void arch_pick_mmap_layout(struct mm_struct *mm)
  79. {
  80. /*
  81. * Fall back to the standard layout if the personality
  82. * bit is set, or if the expected stack growth is unlimited:
  83. */
  84. if (mmap_is_legacy()) {
  85. mm->mmap_base = TASK_UNMAPPED_BASE;
  86. mm->get_unmapped_area = arch_get_unmapped_area;
  87. mm->unmap_area = arch_unmap_area;
  88. } else {
  89. mm->mmap_base = mmap_base();
  90. mm->get_unmapped_area = arch_get_unmapped_area_topdown;
  91. mm->unmap_area = arch_unmap_area_topdown;
  92. }
  93. }
  94. #else
  95. int s390_mmap_check(unsigned long addr, unsigned long len)
  96. {
  97. int rc;
  98. if (!is_compat_task() &&
  99. len >= TASK_SIZE && TASK_SIZE < (1UL << 53)) {
  100. rc = crst_table_upgrade(current->mm, 1UL << 53);
  101. if (rc)
  102. return rc;
  103. update_mm(current->mm, current);
  104. }
  105. return 0;
  106. }
  107. static unsigned long
  108. s390_get_unmapped_area(struct file *filp, unsigned long addr,
  109. unsigned long len, unsigned long pgoff, unsigned long flags)
  110. {
  111. struct mm_struct *mm = current->mm;
  112. unsigned long area;
  113. int rc;
  114. area = arch_get_unmapped_area(filp, addr, len, pgoff, flags);
  115. if (!(area & ~PAGE_MASK))
  116. return area;
  117. if (area == -ENOMEM && !is_compat_task() && TASK_SIZE < (1UL << 53)) {
  118. /* Upgrade the page table to 4 levels and retry. */
  119. rc = crst_table_upgrade(mm, 1UL << 53);
  120. if (rc)
  121. return (unsigned long) rc;
  122. update_mm(mm, current);
  123. area = arch_get_unmapped_area(filp, addr, len, pgoff, flags);
  124. }
  125. return area;
  126. }
  127. static unsigned long
  128. s390_get_unmapped_area_topdown(struct file *filp, const unsigned long addr,
  129. const unsigned long len, const unsigned long pgoff,
  130. const unsigned long flags)
  131. {
  132. struct mm_struct *mm = current->mm;
  133. unsigned long area;
  134. int rc;
  135. area = arch_get_unmapped_area_topdown(filp, addr, len, pgoff, flags);
  136. if (!(area & ~PAGE_MASK))
  137. return area;
  138. if (area == -ENOMEM && !is_compat_task() && TASK_SIZE < (1UL << 53)) {
  139. /* Upgrade the page table to 4 levels and retry. */
  140. rc = crst_table_upgrade(mm, 1UL << 53);
  141. if (rc)
  142. return (unsigned long) rc;
  143. update_mm(mm, current);
  144. area = arch_get_unmapped_area_topdown(filp, addr, len,
  145. pgoff, flags);
  146. }
  147. return area;
  148. }
  149. /*
  150. * This function, called very early during the creation of a new
  151. * process VM image, sets up which VM layout function to use:
  152. */
  153. void arch_pick_mmap_layout(struct mm_struct *mm)
  154. {
  155. /*
  156. * Fall back to the standard layout if the personality
  157. * bit is set, or if the expected stack growth is unlimited:
  158. */
  159. if (mmap_is_legacy()) {
  160. mm->mmap_base = TASK_UNMAPPED_BASE;
  161. mm->get_unmapped_area = s390_get_unmapped_area;
  162. mm->unmap_area = arch_unmap_area;
  163. } else {
  164. mm->mmap_base = mmap_base();
  165. mm->get_unmapped_area = s390_get_unmapped_area_topdown;
  166. mm->unmap_area = arch_unmap_area_topdown;
  167. }
  168. }
  169. #endif