mmap.c 4.2 KB

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