mmap.c 3.5 KB

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