mmap.c 3.4 KB

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