mmap.c 5.6 KB

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