mmap.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
  30. unsigned long len, unsigned long pgoff, unsigned long flags)
  31. {
  32. struct mm_struct *mm = current->mm;
  33. struct vm_area_struct *vma;
  34. int do_colour_align;
  35. struct vm_unmapped_area_info info;
  36. if (flags & MAP_FIXED) {
  37. /* We do not accept a shared mapping if it would violate
  38. * cache aliasing constraints.
  39. */
  40. if ((flags & MAP_SHARED) &&
  41. ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
  42. return -EINVAL;
  43. return addr;
  44. }
  45. if (unlikely(len > TASK_SIZE))
  46. return -ENOMEM;
  47. do_colour_align = 0;
  48. if (filp || (flags & MAP_SHARED))
  49. do_colour_align = 1;
  50. if (addr) {
  51. if (do_colour_align)
  52. addr = COLOUR_ALIGN(addr, pgoff);
  53. else
  54. addr = PAGE_ALIGN(addr);
  55. vma = find_vma(mm, addr);
  56. if (TASK_SIZE - len >= addr &&
  57. (!vma || addr + len <= vm_start_gap(vma)))
  58. return addr;
  59. }
  60. info.flags = 0;
  61. info.length = len;
  62. info.low_limit = TASK_UNMAPPED_BASE;
  63. info.high_limit = TASK_SIZE;
  64. info.align_mask = do_colour_align ? (PAGE_MASK & shm_align_mask) : 0;
  65. info.align_offset = pgoff << PAGE_SHIFT;
  66. return vm_unmapped_area(&info);
  67. }
  68. unsigned long
  69. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  70. const unsigned long len, const unsigned long pgoff,
  71. const unsigned long flags)
  72. {
  73. struct vm_area_struct *vma;
  74. struct mm_struct *mm = current->mm;
  75. unsigned long addr = addr0;
  76. int do_colour_align;
  77. struct vm_unmapped_area_info info;
  78. if (flags & MAP_FIXED) {
  79. /* We do not accept a shared mapping if it would violate
  80. * cache aliasing constraints.
  81. */
  82. if ((flags & MAP_SHARED) &&
  83. ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
  84. return -EINVAL;
  85. return addr;
  86. }
  87. if (unlikely(len > TASK_SIZE))
  88. return -ENOMEM;
  89. do_colour_align = 0;
  90. if (filp || (flags & MAP_SHARED))
  91. do_colour_align = 1;
  92. /* requesting a specific address */
  93. if (addr) {
  94. if (do_colour_align)
  95. addr = COLOUR_ALIGN(addr, pgoff);
  96. else
  97. addr = PAGE_ALIGN(addr);
  98. vma = find_vma(mm, addr);
  99. if (TASK_SIZE - len >= addr &&
  100. (!vma || addr + len <= vm_start_gap(vma)))
  101. return addr;
  102. }
  103. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  104. info.length = len;
  105. info.low_limit = PAGE_SIZE;
  106. info.high_limit = mm->mmap_base;
  107. info.align_mask = do_colour_align ? (PAGE_MASK & shm_align_mask) : 0;
  108. info.align_offset = pgoff << PAGE_SHIFT;
  109. addr = vm_unmapped_area(&info);
  110. /*
  111. * A failed mmap() very likely causes application failure,
  112. * so fall back to the bottom-up function here. This scenario
  113. * can happen with large stack limits and large mmap()
  114. * allocations.
  115. */
  116. if (addr & ~PAGE_MASK) {
  117. VM_BUG_ON(addr != -ENOMEM);
  118. info.flags = 0;
  119. info.low_limit = TASK_UNMAPPED_BASE;
  120. info.high_limit = TASK_SIZE;
  121. addr = vm_unmapped_area(&info);
  122. }
  123. return addr;
  124. }
  125. #endif /* CONFIG_MMU */
  126. /*
  127. * You really shouldn't be using read() or write() on /dev/mem. This
  128. * might go away in the future.
  129. */
  130. int valid_phys_addr_range(phys_addr_t addr, size_t count)
  131. {
  132. if (addr < __MEMORY_START)
  133. return 0;
  134. if (addr + count > __pa(high_memory))
  135. return 0;
  136. return 1;
  137. }
  138. int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
  139. {
  140. return 1;
  141. }