mmap.c 3.9 KB

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