mmap.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /*
  17. * We need to ensure that shared mappings are correctly aligned to
  18. * avoid aliasing issues with VIPT caches. We need to ensure that
  19. * a specific page of an object is always mapped at a multiple of
  20. * SHMLBA bytes.
  21. *
  22. * We unconditionally provide this function for all cases, however
  23. * in the VIVT case, we optimise out the alignment rules.
  24. */
  25. unsigned long
  26. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  27. unsigned long len, unsigned long pgoff, unsigned long flags)
  28. {
  29. struct mm_struct *mm = current->mm;
  30. struct vm_area_struct *vma;
  31. unsigned long start_addr;
  32. int do_align = 0;
  33. int aliasing = cache_is_vipt_aliasing();
  34. /*
  35. * We only need to do colour alignment if either the I or D
  36. * caches alias.
  37. */
  38. if (aliasing)
  39. do_align = filp || (flags & MAP_SHARED);
  40. /*
  41. * We enforce the MAP_FIXED case.
  42. */
  43. if (flags & MAP_FIXED) {
  44. if (aliasing && flags & MAP_SHARED &&
  45. (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1))
  46. return -EINVAL;
  47. return addr;
  48. }
  49. if (len > TASK_SIZE)
  50. return -ENOMEM;
  51. if (addr) {
  52. if (do_align)
  53. addr = COLOUR_ALIGN(addr, pgoff);
  54. else
  55. addr = PAGE_ALIGN(addr);
  56. vma = find_vma(mm, addr);
  57. if (TASK_SIZE - len >= addr &&
  58. (!vma || addr + len <= vma->vm_start))
  59. return addr;
  60. }
  61. if (len > mm->cached_hole_size) {
  62. start_addr = addr = mm->free_area_cache;
  63. } else {
  64. start_addr = addr = TASK_UNMAPPED_BASE;
  65. mm->cached_hole_size = 0;
  66. }
  67. /* 8 bits of randomness in 20 address space bits */
  68. if ((current->flags & PF_RANDOMIZE) &&
  69. !(current->personality & ADDR_NO_RANDOMIZE))
  70. addr += (get_random_int() % (1 << 8)) << PAGE_SHIFT;
  71. full_search:
  72. if (do_align)
  73. addr = COLOUR_ALIGN(addr, pgoff);
  74. else
  75. addr = PAGE_ALIGN(addr);
  76. for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
  77. /* At this point: (!vma || addr < vma->vm_end). */
  78. if (TASK_SIZE - len < addr) {
  79. /*
  80. * Start a new search - just in case we missed
  81. * some holes.
  82. */
  83. if (start_addr != TASK_UNMAPPED_BASE) {
  84. start_addr = addr = TASK_UNMAPPED_BASE;
  85. mm->cached_hole_size = 0;
  86. goto full_search;
  87. }
  88. return -ENOMEM;
  89. }
  90. if (!vma || addr + len <= vma->vm_start) {
  91. /*
  92. * Remember the place where we stopped the search:
  93. */
  94. mm->free_area_cache = addr + len;
  95. return addr;
  96. }
  97. if (addr + mm->cached_hole_size < vma->vm_start)
  98. mm->cached_hole_size = vma->vm_start - addr;
  99. addr = vma->vm_end;
  100. if (do_align)
  101. addr = COLOUR_ALIGN(addr, pgoff);
  102. }
  103. }
  104. /*
  105. * You really shouldn't be using read() or write() on /dev/mem. This
  106. * might go away in the future.
  107. */
  108. int valid_phys_addr_range(unsigned long addr, size_t size)
  109. {
  110. if (addr < PHYS_OFFSET)
  111. return 0;
  112. if (addr + size > __pa(high_memory - 1) + 1)
  113. return 0;
  114. return 1;
  115. }
  116. /*
  117. * We don't use supersection mappings for mmap() on /dev/mem, which
  118. * means that we can't map the memory area above the 4G barrier into
  119. * userspace.
  120. */
  121. int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
  122. {
  123. return !(pfn + (size >> PAGE_SHIFT) > 0x00100000);
  124. }
  125. #ifdef CONFIG_STRICT_DEVMEM
  126. #include <linux/ioport.h>
  127. /*
  128. * devmem_is_allowed() checks to see if /dev/mem access to a certain
  129. * address is valid. The argument is a physical page number.
  130. * We mimic x86 here by disallowing access to system RAM as well as
  131. * device-exclusive MMIO regions. This effectively disable read()/write()
  132. * on /dev/mem.
  133. */
  134. int devmem_is_allowed(unsigned long pfn)
  135. {
  136. if (iomem_is_exclusive(pfn << PAGE_SHIFT))
  137. return 0;
  138. if (!page_is_ram(pfn))
  139. return 1;
  140. return 0;
  141. }
  142. #endif