mmap.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2011 Wind River Systems,
  7. * written by Ralf Baechle <[email protected]>
  8. */
  9. #include <linux/compiler.h>
  10. #include <linux/elf-randomize.h>
  11. #include <linux/errno.h>
  12. #include <linux/mm.h>
  13. #include <linux/mman.h>
  14. #include <linux/export.h>
  15. #include <linux/personality.h>
  16. #include <linux/random.h>
  17. #include <linux/sched/signal.h>
  18. #include <linux/sched/mm.h>
  19. unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
  20. EXPORT_SYMBOL(shm_align_mask);
  21. #define COLOUR_ALIGN(addr, pgoff) \
  22. ((((addr) + shm_align_mask) & ~shm_align_mask) + \
  23. (((pgoff) << PAGE_SHIFT) & shm_align_mask))
  24. enum mmap_allocation_direction {UP, DOWN};
  25. static unsigned long arch_get_unmapped_area_common(struct file *filp,
  26. unsigned long addr0, unsigned long len, unsigned long pgoff,
  27. unsigned long flags, enum mmap_allocation_direction dir)
  28. {
  29. struct mm_struct *mm = current->mm;
  30. struct vm_area_struct *vma;
  31. unsigned long addr = addr0;
  32. int do_color_align;
  33. struct vm_unmapped_area_info info;
  34. if (unlikely(len > TASK_SIZE))
  35. return -ENOMEM;
  36. if (flags & MAP_FIXED) {
  37. /* Even MAP_FIXED mappings must reside within TASK_SIZE */
  38. if (TASK_SIZE - len < addr)
  39. return -EINVAL;
  40. /*
  41. * We do not accept a shared mapping if it would violate
  42. * cache aliasing constraints.
  43. */
  44. if ((flags & MAP_SHARED) &&
  45. ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
  46. return -EINVAL;
  47. return addr;
  48. }
  49. do_color_align = 0;
  50. if (filp || (flags & MAP_SHARED))
  51. do_color_align = 1;
  52. /* requesting a specific address */
  53. if (addr) {
  54. if (do_color_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.length = len;
  64. info.align_mask = do_color_align ? (PAGE_MASK & shm_align_mask) : 0;
  65. info.align_offset = pgoff << PAGE_SHIFT;
  66. if (dir == DOWN) {
  67. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  68. info.low_limit = PAGE_SIZE;
  69. info.high_limit = mm->mmap_base;
  70. addr = vm_unmapped_area(&info);
  71. if (!(addr & ~PAGE_MASK))
  72. return addr;
  73. /*
  74. * A failed mmap() very likely causes application failure,
  75. * so fall back to the bottom-up function here. This scenario
  76. * can happen with large stack limits and large mmap()
  77. * allocations.
  78. */
  79. }
  80. info.flags = 0;
  81. info.low_limit = mm->mmap_base;
  82. info.high_limit = TASK_SIZE;
  83. return vm_unmapped_area(&info);
  84. }
  85. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr0,
  86. unsigned long len, unsigned long pgoff, unsigned long flags)
  87. {
  88. return arch_get_unmapped_area_common(filp,
  89. addr0, len, pgoff, flags, UP);
  90. }
  91. /*
  92. * There is no need to export this but sched.h declares the function as
  93. * extern so making it static here results in an error.
  94. */
  95. unsigned long arch_get_unmapped_area_topdown(struct file *filp,
  96. unsigned long addr0, unsigned long len, unsigned long pgoff,
  97. unsigned long flags)
  98. {
  99. return arch_get_unmapped_area_common(filp,
  100. addr0, len, pgoff, flags, DOWN);
  101. }
  102. bool __virt_addr_valid(const volatile void *kaddr)
  103. {
  104. unsigned long vaddr = (unsigned long)kaddr;
  105. if ((vaddr < PAGE_OFFSET) || (vaddr >= MAP_BASE))
  106. return false;
  107. return pfn_valid(PFN_DOWN(virt_to_phys(kaddr)));
  108. }
  109. EXPORT_SYMBOL_GPL(__virt_addr_valid);