mmap.c 4.7 KB

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