mmap.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 should enforce the MAP_FIXED case. However, currently
  48. * the generic kernel code doesn't allow us to handle this.
  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. }