mmap.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Based on arch/arm/mm/mmap.c
  3. *
  4. * Copyright (C) 2012 ARM Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/elf.h>
  19. #include <linux/fs.h>
  20. #include <linux/mm.h>
  21. #include <linux/mman.h>
  22. #include <linux/export.h>
  23. #include <linux/shm.h>
  24. #include <linux/sched.h>
  25. #include <linux/io.h>
  26. #include <linux/personality.h>
  27. #include <linux/random.h>
  28. #include <asm/cputype.h>
  29. /*
  30. * Leave enough space between the mmap area and the stack to honour ulimit in
  31. * the face of randomisation.
  32. */
  33. #define MIN_GAP (SZ_128M + ((STACK_RND_MASK << PAGE_SHIFT) + 1))
  34. #define MAX_GAP (STACK_TOP/6*5)
  35. static int mmap_is_legacy(void)
  36. {
  37. if (current->personality & ADDR_COMPAT_LAYOUT)
  38. return 1;
  39. if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
  40. return 1;
  41. return sysctl_legacy_va_layout;
  42. }
  43. /*
  44. * Since get_random_int() returns the same value within a 1 jiffy window, we
  45. * will almost always get the same randomisation for the stack and mmap
  46. * region. This will mean the relative distance between stack and mmap will be
  47. * the same.
  48. *
  49. * To avoid this we can shift the randomness by 1 bit.
  50. */
  51. static unsigned long mmap_rnd(void)
  52. {
  53. unsigned long rnd = 0;
  54. if (current->flags & PF_RANDOMIZE)
  55. rnd = (long)get_random_int() & (STACK_RND_MASK >> 1);
  56. return rnd << (PAGE_SHIFT + 1);
  57. }
  58. static unsigned long mmap_base(void)
  59. {
  60. unsigned long gap = rlimit(RLIMIT_STACK);
  61. if (gap < MIN_GAP)
  62. gap = MIN_GAP;
  63. else if (gap > MAX_GAP)
  64. gap = MAX_GAP;
  65. return PAGE_ALIGN(STACK_TOP - gap - mmap_rnd());
  66. }
  67. /*
  68. * This function, called very early during the creation of a new process VM
  69. * image, sets up which VM layout function to use:
  70. */
  71. void arch_pick_mmap_layout(struct mm_struct *mm)
  72. {
  73. /*
  74. * Fall back to the standard layout if the personality bit is set, or
  75. * if the expected stack growth is unlimited:
  76. */
  77. if (mmap_is_legacy()) {
  78. mm->mmap_base = TASK_UNMAPPED_BASE;
  79. mm->get_unmapped_area = arch_get_unmapped_area;
  80. mm->unmap_area = arch_unmap_area;
  81. } else {
  82. mm->mmap_base = mmap_base();
  83. mm->get_unmapped_area = arch_get_unmapped_area_topdown;
  84. mm->unmap_area = arch_unmap_area_topdown;
  85. }
  86. }
  87. EXPORT_SYMBOL_GPL(arch_pick_mmap_layout);
  88. /*
  89. * You really shouldn't be using read() or write() on /dev/mem. This might go
  90. * away in the future.
  91. */
  92. int valid_phys_addr_range(unsigned long addr, size_t size)
  93. {
  94. if (addr < PHYS_OFFSET)
  95. return 0;
  96. if (addr + size > __pa(high_memory - 1) + 1)
  97. return 0;
  98. return 1;
  99. }
  100. /*
  101. * Do not allow /dev/mem mappings beyond the supported physical range.
  102. */
  103. int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
  104. {
  105. return !(((pfn << PAGE_SHIFT) + size) & ~PHYS_MASK);
  106. }
  107. #ifdef CONFIG_STRICT_DEVMEM
  108. #include <linux/ioport.h>
  109. /*
  110. * devmem_is_allowed() checks to see if /dev/mem access to a certain address
  111. * is valid. The argument is a physical page number. We mimic x86 here by
  112. * disallowing access to system RAM as well as device-exclusive MMIO regions.
  113. * This effectively disable read()/write() on /dev/mem.
  114. */
  115. int devmem_is_allowed(unsigned long pfn)
  116. {
  117. if (iomem_is_exclusive(pfn << PAGE_SHIFT))
  118. return 0;
  119. if (!page_is_ram(pfn))
  120. return 1;
  121. return 0;
  122. }
  123. #endif