mmap.c 3.3 KB

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