mmap.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * arch/sh/mm/mmap.c
  3. *
  4. * Copyright (C) 2008 - 2009 Paul Mundt
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/io.h>
  11. #include <linux/mm.h>
  12. #include <linux/mman.h>
  13. #include <linux/module.h>
  14. #include <asm/page.h>
  15. #include <asm/processor.h>
  16. unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
  17. EXPORT_SYMBOL(shm_align_mask);
  18. #ifdef CONFIG_MMU
  19. /*
  20. * To avoid cache aliases, we map the shared page with same color.
  21. */
  22. static inline unsigned long COLOUR_ALIGN(unsigned long addr,
  23. unsigned long pgoff)
  24. {
  25. unsigned long base = (addr + shm_align_mask) & ~shm_align_mask;
  26. unsigned long off = (pgoff << PAGE_SHIFT) & shm_align_mask;
  27. return base + off;
  28. }
  29. static inline unsigned long COLOUR_ALIGN_DOWN(unsigned long addr,
  30. unsigned long pgoff)
  31. {
  32. unsigned long base = addr & ~shm_align_mask;
  33. unsigned long off = (pgoff << PAGE_SHIFT) & shm_align_mask;
  34. if (base + off <= addr)
  35. return base + off;
  36. return base - off;
  37. }
  38. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
  39. unsigned long len, unsigned long pgoff, unsigned long flags)
  40. {
  41. struct mm_struct *mm = current->mm;
  42. struct vm_area_struct *vma;
  43. unsigned long start_addr;
  44. int do_colour_align;
  45. if (flags & MAP_FIXED) {
  46. /* We do not accept a shared mapping if it would violate
  47. * cache aliasing constraints.
  48. */
  49. if ((flags & MAP_SHARED) &&
  50. ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
  51. return -EINVAL;
  52. return addr;
  53. }
  54. if (unlikely(len > TASK_SIZE))
  55. return -ENOMEM;
  56. do_colour_align = 0;
  57. if (filp || (flags & MAP_SHARED))
  58. do_colour_align = 1;
  59. if (addr) {
  60. if (do_colour_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 <= vm_start_gap(vma)))
  67. return addr;
  68. }
  69. if (len > mm->cached_hole_size) {
  70. start_addr = addr = mm->free_area_cache;
  71. } else {
  72. mm->cached_hole_size = 0;
  73. start_addr = addr = TASK_UNMAPPED_BASE;
  74. }
  75. full_search:
  76. if (do_colour_align)
  77. addr = COLOUR_ALIGN(addr, pgoff);
  78. else
  79. addr = PAGE_ALIGN(mm->free_area_cache);
  80. for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
  81. /* At this point: (!vma || addr < vma->vm_end). */
  82. if (unlikely(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 (likely(!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_colour_align)
  105. addr = COLOUR_ALIGN(addr, pgoff);
  106. }
  107. }
  108. unsigned long
  109. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  110. const unsigned long len, const unsigned long pgoff,
  111. const unsigned long flags)
  112. {
  113. struct vm_area_struct *vma;
  114. struct mm_struct *mm = current->mm;
  115. unsigned long addr = addr0;
  116. int do_colour_align;
  117. if (flags & MAP_FIXED) {
  118. /* We do not accept a shared mapping if it would violate
  119. * cache aliasing constraints.
  120. */
  121. if ((flags & MAP_SHARED) &&
  122. ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
  123. return -EINVAL;
  124. return addr;
  125. }
  126. if (unlikely(len > TASK_SIZE))
  127. return -ENOMEM;
  128. do_colour_align = 0;
  129. if (filp || (flags & MAP_SHARED))
  130. do_colour_align = 1;
  131. /* requesting a specific address */
  132. if (addr) {
  133. if (do_colour_align)
  134. addr = COLOUR_ALIGN(addr, pgoff);
  135. else
  136. addr = PAGE_ALIGN(addr);
  137. vma = find_vma(mm, addr);
  138. if (TASK_SIZE - len >= addr &&
  139. (!vma || addr + len <= vm_start_gap(vma)))
  140. return addr;
  141. }
  142. /* check if free_area_cache is useful for us */
  143. if (len <= mm->cached_hole_size) {
  144. mm->cached_hole_size = 0;
  145. mm->free_area_cache = mm->mmap_base;
  146. }
  147. /* either no address requested or can't fit in requested address hole */
  148. addr = mm->free_area_cache;
  149. if (do_colour_align) {
  150. unsigned long base = COLOUR_ALIGN_DOWN(addr-len, pgoff);
  151. addr = base + len;
  152. }
  153. /* make sure it can fit in the remaining address space */
  154. if (likely(addr > len)) {
  155. vma = find_vma(mm, addr-len);
  156. if (!vma || addr <= vma->vm_start) {
  157. /* remember the address as a hint for next time */
  158. return (mm->free_area_cache = addr-len);
  159. }
  160. }
  161. if (unlikely(mm->mmap_base < len))
  162. goto bottomup;
  163. addr = mm->mmap_base-len;
  164. if (do_colour_align)
  165. addr = COLOUR_ALIGN_DOWN(addr, pgoff);
  166. do {
  167. /*
  168. * Lookup failure means no vma is above this address,
  169. * else if new region fits below vma->vm_start,
  170. * return with success:
  171. */
  172. vma = find_vma(mm, addr);
  173. if (likely(!vma || addr+len <= vma->vm_start)) {
  174. /* remember the address as a hint for next time */
  175. return (mm->free_area_cache = addr);
  176. }
  177. /* remember the largest hole we saw so far */
  178. if (addr + mm->cached_hole_size < vma->vm_start)
  179. mm->cached_hole_size = vma->vm_start - addr;
  180. /* try just below the current vma->vm_start */
  181. addr = vma->vm_start-len;
  182. if (do_colour_align)
  183. addr = COLOUR_ALIGN_DOWN(addr, pgoff);
  184. } while (likely(len < vma->vm_start));
  185. bottomup:
  186. /*
  187. * A failed mmap() very likely causes application failure,
  188. * so fall back to the bottom-up function here. This scenario
  189. * can happen with large stack limits and large mmap()
  190. * allocations.
  191. */
  192. mm->cached_hole_size = ~0UL;
  193. mm->free_area_cache = TASK_UNMAPPED_BASE;
  194. addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  195. /*
  196. * Restore the topdown base:
  197. */
  198. mm->free_area_cache = mm->mmap_base;
  199. mm->cached_hole_size = ~0UL;
  200. return addr;
  201. }
  202. #endif /* CONFIG_MMU */
  203. /*
  204. * You really shouldn't be using read() or write() on /dev/mem. This
  205. * might go away in the future.
  206. */
  207. int valid_phys_addr_range(unsigned long addr, size_t count)
  208. {
  209. if (addr < __MEMORY_START)
  210. return 0;
  211. if (addr + count > __pa(high_memory))
  212. return 0;
  213. return 1;
  214. }
  215. int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
  216. {
  217. return 1;
  218. }