hugetlbpage.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * SPARC64 Huge TLB page support.
  3. *
  4. * Copyright (C) 2002, 2003, 2006 David S. Miller (davem@davemloft.net)
  5. */
  6. #include <linux/init.h>
  7. #include <linux/fs.h>
  8. #include <linux/mm.h>
  9. #include <linux/hugetlb.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/sysctl.h>
  12. #include <asm/mman.h>
  13. #include <asm/pgalloc.h>
  14. #include <asm/tlb.h>
  15. #include <asm/tlbflush.h>
  16. #include <asm/cacheflush.h>
  17. #include <asm/mmu_context.h>
  18. /* Slightly simplified from the non-hugepage variant because by
  19. * definition we don't have to worry about any page coloring stuff
  20. */
  21. #define VA_EXCLUDE_START (0x0000080000000000UL - (1UL << 32UL))
  22. #define VA_EXCLUDE_END (0xfffff80000000000UL + (1UL << 32UL))
  23. static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *filp,
  24. unsigned long addr,
  25. unsigned long len,
  26. unsigned long pgoff,
  27. unsigned long flags)
  28. {
  29. unsigned long task_size = TASK_SIZE;
  30. struct vm_unmapped_area_info info;
  31. if (test_thread_flag(TIF_32BIT))
  32. task_size = STACK_TOP32;
  33. info.flags = 0;
  34. info.length = len;
  35. info.low_limit = TASK_UNMAPPED_BASE;
  36. info.high_limit = min(task_size, VA_EXCLUDE_START);
  37. info.align_mask = PAGE_MASK & ~HPAGE_MASK;
  38. info.align_offset = 0;
  39. addr = vm_unmapped_area(&info);
  40. if ((addr & ~PAGE_MASK) && task_size > VA_EXCLUDE_END) {
  41. VM_BUG_ON(addr != -ENOMEM);
  42. info.low_limit = VA_EXCLUDE_END;
  43. info.high_limit = task_size;
  44. addr = vm_unmapped_area(&info);
  45. }
  46. return addr;
  47. }
  48. static unsigned long
  49. hugetlb_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  50. const unsigned long len,
  51. const unsigned long pgoff,
  52. const unsigned long flags)
  53. {
  54. struct mm_struct *mm = current->mm;
  55. unsigned long addr = addr0;
  56. struct vm_unmapped_area_info info;
  57. /* This should only ever run for 32-bit processes. */
  58. BUG_ON(!test_thread_flag(TIF_32BIT));
  59. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  60. info.length = len;
  61. info.low_limit = PAGE_SIZE;
  62. info.high_limit = mm->mmap_base;
  63. info.align_mask = PAGE_MASK & ~HPAGE_MASK;
  64. info.align_offset = 0;
  65. addr = vm_unmapped_area(&info);
  66. /*
  67. * A failed mmap() very likely causes application failure,
  68. * so fall back to the bottom-up function here. This scenario
  69. * can happen with large stack limits and large mmap()
  70. * allocations.
  71. */
  72. if (addr & ~PAGE_MASK) {
  73. VM_BUG_ON(addr != -ENOMEM);
  74. info.flags = 0;
  75. info.low_limit = TASK_UNMAPPED_BASE;
  76. info.high_limit = STACK_TOP32;
  77. addr = vm_unmapped_area(&info);
  78. }
  79. return addr;
  80. }
  81. unsigned long
  82. hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  83. unsigned long len, unsigned long pgoff, unsigned long flags)
  84. {
  85. struct mm_struct *mm = current->mm;
  86. struct vm_area_struct *vma;
  87. unsigned long task_size = TASK_SIZE;
  88. if (test_thread_flag(TIF_32BIT))
  89. task_size = STACK_TOP32;
  90. if (len & ~HPAGE_MASK)
  91. return -EINVAL;
  92. if (len > task_size)
  93. return -ENOMEM;
  94. if (flags & MAP_FIXED) {
  95. if (prepare_hugepage_range(file, addr, len))
  96. return -EINVAL;
  97. return addr;
  98. }
  99. if (addr) {
  100. addr = ALIGN(addr, HPAGE_SIZE);
  101. vma = find_vma(mm, addr);
  102. if (task_size - len >= addr &&
  103. (!vma || addr + len <= vma->vm_start))
  104. return addr;
  105. }
  106. if (mm->get_unmapped_area == arch_get_unmapped_area)
  107. return hugetlb_get_unmapped_area_bottomup(file, addr, len,
  108. pgoff, flags);
  109. else
  110. return hugetlb_get_unmapped_area_topdown(file, addr, len,
  111. pgoff, flags);
  112. }
  113. pte_t *huge_pte_alloc(struct mm_struct *mm,
  114. unsigned long addr, unsigned long sz)
  115. {
  116. pgd_t *pgd;
  117. pud_t *pud;
  118. pmd_t *pmd;
  119. pte_t *pte = NULL;
  120. /* We must align the address, because our caller will run
  121. * set_huge_pte_at() on whatever we return, which writes out
  122. * all of the sub-ptes for the hugepage range. So we have
  123. * to give it the first such sub-pte.
  124. */
  125. addr &= HPAGE_MASK;
  126. pgd = pgd_offset(mm, addr);
  127. pud = pud_alloc(mm, pgd, addr);
  128. if (pud) {
  129. pmd = pmd_alloc(mm, pud, addr);
  130. if (pmd)
  131. pte = pte_alloc_map(mm, NULL, pmd, addr);
  132. }
  133. return pte;
  134. }
  135. pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  136. {
  137. pgd_t *pgd;
  138. pud_t *pud;
  139. pmd_t *pmd;
  140. pte_t *pte = NULL;
  141. addr &= HPAGE_MASK;
  142. pgd = pgd_offset(mm, addr);
  143. if (!pgd_none(*pgd)) {
  144. pud = pud_offset(pgd, addr);
  145. if (!pud_none(*pud)) {
  146. pmd = pmd_offset(pud, addr);
  147. if (!pmd_none(*pmd))
  148. pte = pte_offset_map(pmd, addr);
  149. }
  150. }
  151. return pte;
  152. }
  153. int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
  154. {
  155. return 0;
  156. }
  157. void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
  158. pte_t *ptep, pte_t entry)
  159. {
  160. int i;
  161. if (!pte_present(*ptep) && pte_present(entry))
  162. mm->context.huge_pte_count++;
  163. addr &= HPAGE_MASK;
  164. for (i = 0; i < (1 << HUGETLB_PAGE_ORDER); i++) {
  165. set_pte_at(mm, addr, ptep, entry);
  166. ptep++;
  167. addr += PAGE_SIZE;
  168. pte_val(entry) += PAGE_SIZE;
  169. }
  170. }
  171. pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
  172. pte_t *ptep)
  173. {
  174. pte_t entry;
  175. int i;
  176. entry = *ptep;
  177. if (pte_present(entry))
  178. mm->context.huge_pte_count--;
  179. addr &= HPAGE_MASK;
  180. for (i = 0; i < (1 << HUGETLB_PAGE_ORDER); i++) {
  181. pte_clear(mm, addr, ptep);
  182. addr += PAGE_SIZE;
  183. ptep++;
  184. }
  185. return entry;
  186. }
  187. struct page *follow_huge_addr(struct mm_struct *mm,
  188. unsigned long address, int write)
  189. {
  190. return ERR_PTR(-EINVAL);
  191. }
  192. int pmd_huge(pmd_t pmd)
  193. {
  194. return 0;
  195. }
  196. int pud_huge(pud_t pud)
  197. {
  198. return 0;
  199. }
  200. struct page *follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  201. pmd_t *pmd, int write)
  202. {
  203. return NULL;
  204. }