mmap.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. /*
  19. * We need to ensure that shared mappings are correctly aligned to
  20. * avoid aliasing issues with VIPT caches. We need to ensure that
  21. * a specific page of an object is always mapped at a multiple of
  22. * SHMLBA bytes.
  23. *
  24. * We unconditionally provide this function for all cases, however
  25. * in the VIVT case, we optimise out the alignment rules.
  26. */
  27. unsigned long
  28. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  29. unsigned long len, unsigned long pgoff, unsigned long flags)
  30. {
  31. struct mm_struct *mm = current->mm;
  32. struct vm_area_struct *vma;
  33. int do_align = 0;
  34. int aliasing = cache_is_vipt_aliasing();
  35. struct vm_unmapped_area_info info;
  36. /*
  37. * We only need to do colour alignment if either the I or D
  38. * caches alias.
  39. */
  40. if (aliasing)
  41. do_align = filp || (flags & MAP_SHARED);
  42. /*
  43. * We enforce the MAP_FIXED case.
  44. */
  45. if (flags & MAP_FIXED) {
  46. if (aliasing && flags & MAP_SHARED &&
  47. (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1))
  48. return -EINVAL;
  49. return addr;
  50. }
  51. if (len > TASK_SIZE)
  52. return -ENOMEM;
  53. if (addr) {
  54. if (do_align)
  55. addr = COLOUR_ALIGN(addr, pgoff);
  56. else
  57. addr = PAGE_ALIGN(addr);
  58. vma = find_vma(mm, addr);
  59. if (TASK_SIZE - len >= addr &&
  60. (!vma || addr + len <= vm_start_gap(vma)))
  61. return addr;
  62. }
  63. info.flags = 0;
  64. info.length = len;
  65. info.low_limit = mm->mmap_base;
  66. info.high_limit = TASK_SIZE;
  67. info.align_mask = do_align ? (PAGE_MASK & (SHMLBA - 1)) : 0;
  68. info.align_offset = pgoff << PAGE_SHIFT;
  69. return vm_unmapped_area(&info);
  70. }
  71. unsigned long
  72. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  73. const unsigned long len, const unsigned long pgoff,
  74. const unsigned long flags)
  75. {
  76. struct vm_area_struct *vma;
  77. struct mm_struct *mm = current->mm;
  78. unsigned long addr = addr0;
  79. int do_align = 0;
  80. int aliasing = cache_is_vipt_aliasing();
  81. struct vm_unmapped_area_info info;
  82. /*
  83. * We only need to do colour alignment if either the I or D
  84. * caches alias.
  85. */
  86. if (aliasing)
  87. do_align = filp || (flags & MAP_SHARED);
  88. /* requested length too big for entire address space */
  89. if (len > TASK_SIZE)
  90. return -ENOMEM;
  91. if (flags & MAP_FIXED) {
  92. if (aliasing && flags & MAP_SHARED &&
  93. (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1))
  94. return -EINVAL;
  95. return addr;
  96. }
  97. /* requesting a specific address */
  98. if (addr) {
  99. if (do_align)
  100. addr = COLOUR_ALIGN(addr, pgoff);
  101. else
  102. addr = PAGE_ALIGN(addr);
  103. vma = find_vma(mm, addr);
  104. if (TASK_SIZE - len >= addr &&
  105. (!vma || addr + len <= vm_start_gap(vma)))
  106. return addr;
  107. }
  108. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  109. info.length = len;
  110. info.low_limit = FIRST_USER_ADDRESS;
  111. info.high_limit = mm->mmap_base;
  112. info.align_mask = do_align ? (PAGE_MASK & (SHMLBA - 1)) : 0;
  113. info.align_offset = pgoff << PAGE_SHIFT;
  114. addr = vm_unmapped_area(&info);
  115. /*
  116. * A failed mmap() very likely causes application failure,
  117. * so fall back to the bottom-up function here. This scenario
  118. * can happen with large stack limits and large mmap()
  119. * allocations.
  120. */
  121. if (addr & ~PAGE_MASK) {
  122. VM_BUG_ON(addr != -ENOMEM);
  123. info.flags = 0;
  124. info.low_limit = mm->mmap_base;
  125. info.high_limit = TASK_SIZE;
  126. addr = vm_unmapped_area(&info);
  127. }
  128. return addr;
  129. }
  130. /*
  131. * You really shouldn't be using read() or write() on /dev/mem. This
  132. * might go away in the future.
  133. */
  134. int valid_phys_addr_range(phys_addr_t addr, size_t size)
  135. {
  136. if (addr < PHYS_OFFSET)
  137. return 0;
  138. if (addr + size > __pa(high_memory - 1) + 1)
  139. return 0;
  140. return 1;
  141. }
  142. /*
  143. * Do not allow /dev/mem mappings beyond the supported physical range.
  144. */
  145. int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
  146. {
  147. return (pfn + (size >> PAGE_SHIFT)) <= (1 + (PHYS_MASK >> PAGE_SHIFT));
  148. }