mmap.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. #ifdef CONFIG_MMU
  17. unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
  18. EXPORT_SYMBOL(shm_align_mask);
  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) && (addr & shm_align_mask))
  50. return -EINVAL;
  51. return addr;
  52. }
  53. if (unlikely(len > TASK_SIZE))
  54. return -ENOMEM;
  55. do_colour_align = 0;
  56. if (filp || (flags & MAP_SHARED))
  57. do_colour_align = 1;
  58. if (addr) {
  59. if (do_colour_align)
  60. addr = COLOUR_ALIGN(addr, pgoff);
  61. else
  62. addr = PAGE_ALIGN(addr);
  63. vma = find_vma(mm, addr);
  64. if (TASK_SIZE - len >= addr &&
  65. (!vma || addr + len <= vma->vm_start))
  66. return addr;
  67. }
  68. if (len > mm->cached_hole_size) {
  69. start_addr = addr = mm->free_area_cache;
  70. } else {
  71. mm->cached_hole_size = 0;
  72. start_addr = addr = TASK_UNMAPPED_BASE;
  73. }
  74. full_search:
  75. if (do_colour_align)
  76. addr = COLOUR_ALIGN(addr, pgoff);
  77. else
  78. addr = PAGE_ALIGN(mm->free_area_cache);
  79. for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
  80. /* At this point: (!vma || addr < vma->vm_end). */
  81. if (unlikely(TASK_SIZE - len < addr)) {
  82. /*
  83. * Start a new search - just in case we missed
  84. * some holes.
  85. */
  86. if (start_addr != TASK_UNMAPPED_BASE) {
  87. start_addr = addr = TASK_UNMAPPED_BASE;
  88. mm->cached_hole_size = 0;
  89. goto full_search;
  90. }
  91. return -ENOMEM;
  92. }
  93. if (likely(!vma || addr + len <= vma->vm_start)) {
  94. /*
  95. * Remember the place where we stopped the search:
  96. */
  97. mm->free_area_cache = addr + len;
  98. return addr;
  99. }
  100. if (addr + mm->cached_hole_size < vma->vm_start)
  101. mm->cached_hole_size = vma->vm_start - addr;
  102. addr = vma->vm_end;
  103. if (do_colour_align)
  104. addr = COLOUR_ALIGN(addr, pgoff);
  105. }
  106. }
  107. unsigned long
  108. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  109. const unsigned long len, const unsigned long pgoff,
  110. const unsigned long flags)
  111. {
  112. struct vm_area_struct *vma;
  113. struct mm_struct *mm = current->mm;
  114. unsigned long addr = addr0;
  115. int do_colour_align;
  116. if (flags & MAP_FIXED) {
  117. /* We do not accept a shared mapping if it would violate
  118. * cache aliasing constraints.
  119. */
  120. if ((flags & MAP_SHARED) &&
  121. ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
  122. return -EINVAL;
  123. return addr;
  124. }
  125. if (unlikely(len > TASK_SIZE))
  126. return -ENOMEM;
  127. do_colour_align = 0;
  128. if (filp || (flags & MAP_SHARED))
  129. do_colour_align = 1;
  130. /* requesting a specific address */
  131. if (addr) {
  132. if (do_colour_align)
  133. addr = COLOUR_ALIGN(addr, pgoff);
  134. else
  135. addr = PAGE_ALIGN(addr);
  136. vma = find_vma(mm, addr);
  137. if (TASK_SIZE - len >= addr &&
  138. (!vma || addr + len <= vma->vm_start))
  139. return addr;
  140. }
  141. /* check if free_area_cache is useful for us */
  142. if (len <= mm->cached_hole_size) {
  143. mm->cached_hole_size = 0;
  144. mm->free_area_cache = mm->mmap_base;
  145. }
  146. /* either no address requested or can't fit in requested address hole */
  147. addr = mm->free_area_cache;
  148. if (do_colour_align) {
  149. unsigned long base = COLOUR_ALIGN_DOWN(addr-len, pgoff);
  150. addr = base + len;
  151. }
  152. /* make sure it can fit in the remaining address space */
  153. if (likely(addr > len)) {
  154. vma = find_vma(mm, addr-len);
  155. if (!vma || addr <= vma->vm_start) {
  156. /* remember the address as a hint for next time */
  157. return (mm->free_area_cache = addr-len);
  158. }
  159. }
  160. if (unlikely(mm->mmap_base < len))
  161. goto bottomup;
  162. addr = mm->mmap_base-len;
  163. if (do_colour_align)
  164. addr = COLOUR_ALIGN_DOWN(addr, pgoff);
  165. do {
  166. /*
  167. * Lookup failure means no vma is above this address,
  168. * else if new region fits below vma->vm_start,
  169. * return with success:
  170. */
  171. vma = find_vma(mm, addr);
  172. if (likely(!vma || addr+len <= vma->vm_start)) {
  173. /* remember the address as a hint for next time */
  174. return (mm->free_area_cache = addr);
  175. }
  176. /* remember the largest hole we saw so far */
  177. if (addr + mm->cached_hole_size < vma->vm_start)
  178. mm->cached_hole_size = vma->vm_start - addr;
  179. /* try just below the current vma->vm_start */
  180. addr = vma->vm_start-len;
  181. if (do_colour_align)
  182. addr = COLOUR_ALIGN_DOWN(addr, pgoff);
  183. } while (likely(len < vma->vm_start));
  184. bottomup:
  185. /*
  186. * A failed mmap() very likely causes application failure,
  187. * so fall back to the bottom-up function here. This scenario
  188. * can happen with large stack limits and large mmap()
  189. * allocations.
  190. */
  191. mm->cached_hole_size = ~0UL;
  192. mm->free_area_cache = TASK_UNMAPPED_BASE;
  193. addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  194. /*
  195. * Restore the topdown base:
  196. */
  197. mm->free_area_cache = mm->mmap_base;
  198. mm->cached_hole_size = ~0UL;
  199. return addr;
  200. }
  201. #endif /* CONFIG_MMU */
  202. /*
  203. * You really shouldn't be using read() or write() on /dev/mem. This
  204. * might go away in the future.
  205. */
  206. int valid_phys_addr_range(unsigned long addr, size_t count)
  207. {
  208. if (addr < __MEMORY_START)
  209. return 0;
  210. if (addr + count > __pa(high_memory))
  211. return 0;
  212. return 1;
  213. }
  214. int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
  215. {
  216. return 1;
  217. }