mmap.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/arch/arm/mm/mmap.c
  4. */
  5. #include <linux/fs.h>
  6. #include <linux/mm.h>
  7. #include <linux/mman.h>
  8. #include <linux/shm.h>
  9. #include <linux/sched/signal.h>
  10. #include <linux/sched/mm.h>
  11. #include <linux/io.h>
  12. #include <linux/personality.h>
  13. #include <linux/random.h>
  14. #include <asm/cachetype.h>
  15. #define COLOUR_ALIGN(addr,pgoff) \
  16. ((((addr)+SHMLBA-1)&~(SHMLBA-1)) + \
  17. (((pgoff)<<PAGE_SHIFT) & (SHMLBA-1)))
  18. /* gap between mmap and stack */
  19. #define MIN_GAP (128*1024*1024UL)
  20. #define MAX_GAP ((TASK_SIZE)/6*5)
  21. static int mmap_is_legacy(struct rlimit *rlim_stack)
  22. {
  23. if (current->personality & ADDR_COMPAT_LAYOUT)
  24. return 1;
  25. if (rlim_stack->rlim_cur == RLIM_INFINITY)
  26. return 1;
  27. return sysctl_legacy_va_layout;
  28. }
  29. static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack)
  30. {
  31. unsigned long gap = rlim_stack->rlim_cur;
  32. if (gap < MIN_GAP)
  33. gap = MIN_GAP;
  34. else if (gap > MAX_GAP)
  35. gap = MAX_GAP;
  36. return PAGE_ALIGN(TASK_SIZE - gap - rnd);
  37. }
  38. /*
  39. * We need to ensure that shared mappings are correctly aligned to
  40. * avoid aliasing issues with VIPT caches. We need to ensure that
  41. * a specific page of an object is always mapped at a multiple of
  42. * SHMLBA bytes.
  43. *
  44. * We unconditionally provide this function for all cases, however
  45. * in the VIVT case, we optimise out the alignment rules.
  46. */
  47. unsigned long
  48. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  49. unsigned long len, unsigned long pgoff, unsigned long flags)
  50. {
  51. struct mm_struct *mm = current->mm;
  52. struct vm_area_struct *vma;
  53. int do_align = 0;
  54. int aliasing = cache_is_vipt_aliasing();
  55. struct vm_unmapped_area_info info;
  56. /*
  57. * We only need to do colour alignment if either the I or D
  58. * caches alias.
  59. */
  60. if (aliasing)
  61. do_align = filp || (flags & MAP_SHARED);
  62. /*
  63. * We enforce the MAP_FIXED case.
  64. */
  65. if (flags & MAP_FIXED) {
  66. if (aliasing && flags & MAP_SHARED &&
  67. (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1))
  68. return -EINVAL;
  69. return addr;
  70. }
  71. if (len > TASK_SIZE)
  72. return -ENOMEM;
  73. if (addr) {
  74. if (do_align)
  75. addr = COLOUR_ALIGN(addr, pgoff);
  76. else
  77. addr = PAGE_ALIGN(addr);
  78. vma = find_vma(mm, addr);
  79. if (TASK_SIZE - len >= addr &&
  80. (!vma || addr + len <= vm_start_gap(vma)))
  81. return addr;
  82. }
  83. info.flags = 0;
  84. info.length = len;
  85. info.low_limit = mm->mmap_base;
  86. info.high_limit = TASK_SIZE;
  87. info.align_mask = do_align ? (PAGE_MASK & (SHMLBA - 1)) : 0;
  88. info.align_offset = pgoff << PAGE_SHIFT;
  89. return vm_unmapped_area(&info);
  90. }
  91. unsigned long
  92. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  93. const unsigned long len, const unsigned long pgoff,
  94. const unsigned long flags)
  95. {
  96. struct vm_area_struct *vma;
  97. struct mm_struct *mm = current->mm;
  98. unsigned long addr = addr0;
  99. int do_align = 0;
  100. int aliasing = cache_is_vipt_aliasing();
  101. struct vm_unmapped_area_info info;
  102. /*
  103. * We only need to do colour alignment if either the I or D
  104. * caches alias.
  105. */
  106. if (aliasing)
  107. do_align = filp || (flags & MAP_SHARED);
  108. /* requested length too big for entire address space */
  109. if (len > TASK_SIZE)
  110. return -ENOMEM;
  111. if (flags & MAP_FIXED) {
  112. if (aliasing && flags & MAP_SHARED &&
  113. (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1))
  114. return -EINVAL;
  115. return addr;
  116. }
  117. /* requesting a specific address */
  118. if (addr) {
  119. if (do_align)
  120. addr = COLOUR_ALIGN(addr, pgoff);
  121. else
  122. addr = PAGE_ALIGN(addr);
  123. vma = find_vma(mm, addr);
  124. if (TASK_SIZE - len >= addr &&
  125. (!vma || addr + len <= vm_start_gap(vma)))
  126. return addr;
  127. }
  128. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  129. info.length = len;
  130. info.low_limit = FIRST_USER_ADDRESS;
  131. info.high_limit = mm->mmap_base;
  132. info.align_mask = do_align ? (PAGE_MASK & (SHMLBA - 1)) : 0;
  133. info.align_offset = pgoff << PAGE_SHIFT;
  134. addr = vm_unmapped_area(&info);
  135. /*
  136. * A failed mmap() very likely causes application failure,
  137. * so fall back to the bottom-up function here. This scenario
  138. * can happen with large stack limits and large mmap()
  139. * allocations.
  140. */
  141. if (addr & ~PAGE_MASK) {
  142. VM_BUG_ON(addr != -ENOMEM);
  143. info.flags = 0;
  144. info.low_limit = mm->mmap_base;
  145. info.high_limit = TASK_SIZE;
  146. addr = vm_unmapped_area(&info);
  147. }
  148. return addr;
  149. }
  150. unsigned long arch_mmap_rnd(void)
  151. {
  152. unsigned long rnd;
  153. rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
  154. return rnd << PAGE_SHIFT;
  155. }
  156. void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
  157. {
  158. unsigned long random_factor = 0UL;
  159. if (current->flags & PF_RANDOMIZE)
  160. random_factor = arch_mmap_rnd();
  161. if (mmap_is_legacy(rlim_stack)) {
  162. mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
  163. mm->get_unmapped_area = arch_get_unmapped_area;
  164. } else {
  165. mm->mmap_base = mmap_base(random_factor, rlim_stack);
  166. mm->get_unmapped_area = arch_get_unmapped_area_topdown;
  167. }
  168. }
  169. /*
  170. * You really shouldn't be using read() or write() on /dev/mem. This
  171. * might go away in the future.
  172. */
  173. int valid_phys_addr_range(phys_addr_t addr, size_t size)
  174. {
  175. if (addr < PHYS_OFFSET)
  176. return 0;
  177. if (addr + size > __pa(high_memory - 1) + 1)
  178. return 0;
  179. return 1;
  180. }
  181. /*
  182. * Do not allow /dev/mem mappings beyond the supported physical range.
  183. */
  184. int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
  185. {
  186. return (pfn + (size >> PAGE_SHIFT)) <= (1 + (PHYS_MASK >> PAGE_SHIFT));
  187. }
  188. #ifdef CONFIG_STRICT_DEVMEM
  189. #include <linux/ioport.h>
  190. /*
  191. * devmem_is_allowed() checks to see if /dev/mem access to a certain
  192. * address is valid. The argument is a physical page number.
  193. * We mimic x86 here by disallowing access to system RAM as well as
  194. * device-exclusive MMIO regions. This effectively disable read()/write()
  195. * on /dev/mem.
  196. */
  197. int devmem_is_allowed(unsigned long pfn)
  198. {
  199. if (iomem_is_exclusive(pfn << PAGE_SHIFT))
  200. return 0;
  201. if (!page_is_ram(pfn))
  202. return 1;
  203. return 0;
  204. }
  205. #endif