mmap.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 && addr & (SHMLBA - 1))
  54. return -EINVAL;
  55. return addr;
  56. }
  57. if (len > TASK_SIZE)
  58. return -ENOMEM;
  59. if (addr) {
  60. if (do_align)
  61. addr = COLOUR_ALIGN(addr, pgoff);
  62. else
  63. addr = PAGE_ALIGN(addr);
  64. vma = find_vma(mm, addr);
  65. if (TASK_SIZE - len >= addr &&
  66. (!vma || addr + len <= vma->vm_start))
  67. return addr;
  68. }
  69. if (len > mm->cached_hole_size) {
  70. start_addr = addr = mm->free_area_cache;
  71. } else {
  72. start_addr = addr = TASK_UNMAPPED_BASE;
  73. mm->cached_hole_size = 0;
  74. }
  75. full_search:
  76. if (do_align)
  77. addr = COLOUR_ALIGN(addr, pgoff);
  78. else
  79. addr = PAGE_ALIGN(addr);
  80. for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
  81. /* At this point: (!vma || addr < vma->vm_end). */
  82. if (TASK_SIZE - len < addr) {
  83. /*
  84. * Start a new search - just in case we missed
  85. * some holes.
  86. */
  87. if (start_addr != TASK_UNMAPPED_BASE) {
  88. start_addr = addr = TASK_UNMAPPED_BASE;
  89. mm->cached_hole_size = 0;
  90. goto full_search;
  91. }
  92. return -ENOMEM;
  93. }
  94. if (!vma || addr + len <= vma->vm_start) {
  95. /*
  96. * Remember the place where we stopped the search:
  97. */
  98. mm->free_area_cache = addr + len;
  99. return addr;
  100. }
  101. if (addr + mm->cached_hole_size < vma->vm_start)
  102. mm->cached_hole_size = vma->vm_start - addr;
  103. addr = vma->vm_end;
  104. if (do_align)
  105. addr = COLOUR_ALIGN(addr, pgoff);
  106. }
  107. }
  108. /*
  109. * You really shouldn't be using read() or write() on /dev/mem. This
  110. * might go away in the future.
  111. */
  112. int valid_phys_addr_range(unsigned long addr, size_t size)
  113. {
  114. if (addr < PHYS_OFFSET)
  115. return 0;
  116. if (addr + size >= __pa(high_memory - 1))
  117. return 0;
  118. return 1;
  119. }
  120. /*
  121. * We don't use supersection mappings for mmap() on /dev/mem, which
  122. * means that we can't map the memory area above the 4G barrier into
  123. * userspace.
  124. */
  125. int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
  126. {
  127. return !(pfn + (size >> PAGE_SHIFT) > 0x00100000);
  128. }