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