hugetlbpage.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * IA-64 Huge TLB Page Support for Kernel.
  3. *
  4. * Copyright (C) 2002-2004 Rohit Seth <rohit.seth@intel.com>
  5. * Copyright (C) 2003-2004 Ken Chen <kenneth.w.chen@intel.com>
  6. *
  7. * Sep, 2003: add numa support
  8. * Feb, 2004: dynamic hugetlb page size via boot parameter
  9. */
  10. #include <linux/init.h>
  11. #include <linux/fs.h>
  12. #include <linux/mm.h>
  13. #include <linux/hugetlb.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/module.h>
  16. #include <linux/sysctl.h>
  17. #include <linux/log2.h>
  18. #include <asm/mman.h>
  19. #include <asm/pgalloc.h>
  20. #include <asm/tlb.h>
  21. #include <asm/tlbflush.h>
  22. unsigned int hpage_shift = HPAGE_SHIFT_DEFAULT;
  23. EXPORT_SYMBOL(hpage_shift);
  24. pte_t *
  25. huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz)
  26. {
  27. unsigned long taddr = htlbpage_to_page(addr);
  28. pgd_t *pgd;
  29. pud_t *pud;
  30. pmd_t *pmd;
  31. pte_t *pte = NULL;
  32. pgd = pgd_offset(mm, taddr);
  33. pud = pud_alloc(mm, pgd, taddr);
  34. if (pud) {
  35. pmd = pmd_alloc(mm, pud, taddr);
  36. if (pmd)
  37. pte = pte_alloc_map(mm, NULL, pmd, taddr);
  38. }
  39. return pte;
  40. }
  41. pte_t *
  42. huge_pte_offset (struct mm_struct *mm, unsigned long addr)
  43. {
  44. unsigned long taddr = htlbpage_to_page(addr);
  45. pgd_t *pgd;
  46. pud_t *pud;
  47. pmd_t *pmd;
  48. pte_t *pte = NULL;
  49. pgd = pgd_offset(mm, taddr);
  50. if (pgd_present(*pgd)) {
  51. pud = pud_offset(pgd, taddr);
  52. if (pud_present(*pud)) {
  53. pmd = pmd_offset(pud, taddr);
  54. if (pmd_present(*pmd))
  55. pte = pte_offset_map(pmd, taddr);
  56. }
  57. }
  58. return pte;
  59. }
  60. #define mk_pte_huge(entry) { pte_val(entry) |= _PAGE_P; }
  61. /*
  62. * Don't actually need to do any preparation, but need to make sure
  63. * the address is in the right region.
  64. */
  65. int prepare_hugepage_range(struct file *file,
  66. unsigned long addr, unsigned long len)
  67. {
  68. if (len & ~HPAGE_MASK)
  69. return -EINVAL;
  70. if (addr & ~HPAGE_MASK)
  71. return -EINVAL;
  72. if (REGION_NUMBER(addr) != RGN_HPAGE)
  73. return -EINVAL;
  74. return 0;
  75. }
  76. struct page *follow_huge_addr(struct mm_struct *mm, unsigned long addr, int write)
  77. {
  78. struct page *page;
  79. pte_t *ptep;
  80. if (REGION_NUMBER(addr) != RGN_HPAGE)
  81. return ERR_PTR(-EINVAL);
  82. ptep = huge_pte_offset(mm, addr);
  83. if (!ptep || pte_none(*ptep))
  84. return NULL;
  85. page = pte_page(*ptep);
  86. page += ((addr & ~HPAGE_MASK) >> PAGE_SHIFT);
  87. return page;
  88. }
  89. int pmd_huge(pmd_t pmd)
  90. {
  91. return 0;
  92. }
  93. int pud_huge(pud_t pud)
  94. {
  95. return 0;
  96. }
  97. void hugetlb_free_pgd_range(struct mmu_gather *tlb,
  98. unsigned long addr, unsigned long end,
  99. unsigned long floor, unsigned long ceiling)
  100. {
  101. /*
  102. * This is called to free hugetlb page tables.
  103. *
  104. * The offset of these addresses from the base of the hugetlb
  105. * region must be scaled down by HPAGE_SIZE/PAGE_SIZE so that
  106. * the standard free_pgd_range will free the right page tables.
  107. *
  108. * If floor and ceiling are also in the hugetlb region, they
  109. * must likewise be scaled down; but if outside, left unchanged.
  110. */
  111. addr = htlbpage_to_page(addr);
  112. end = htlbpage_to_page(end);
  113. if (REGION_NUMBER(floor) == RGN_HPAGE)
  114. floor = htlbpage_to_page(floor);
  115. if (REGION_NUMBER(ceiling) == RGN_HPAGE)
  116. ceiling = htlbpage_to_page(ceiling);
  117. free_pgd_range(tlb, addr, end, floor, ceiling);
  118. }
  119. unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
  120. unsigned long pgoff, unsigned long flags)
  121. {
  122. struct vm_unmapped_area_info info;
  123. if (len > RGN_MAP_LIMIT)
  124. return -ENOMEM;
  125. if (len & ~HPAGE_MASK)
  126. return -EINVAL;
  127. /* Handle MAP_FIXED */
  128. if (flags & MAP_FIXED) {
  129. if (prepare_hugepage_range(file, addr, len))
  130. return -EINVAL;
  131. return addr;
  132. }
  133. /* This code assumes that RGN_HPAGE != 0. */
  134. if ((REGION_NUMBER(addr) != RGN_HPAGE) || (addr & (HPAGE_SIZE - 1)))
  135. addr = HPAGE_REGION_BASE;
  136. info.flags = 0;
  137. info.length = len;
  138. info.low_limit = addr;
  139. info.high_limit = HPAGE_REGION_BASE + RGN_MAP_LIMIT;
  140. info.align_mask = PAGE_MASK & (HPAGE_SIZE - 1);
  141. info.align_offset = 0;
  142. return vm_unmapped_area(&info);
  143. }
  144. static int __init hugetlb_setup_sz(char *str)
  145. {
  146. u64 tr_pages;
  147. unsigned long long size;
  148. if (ia64_pal_vm_page_size(&tr_pages, NULL) != 0)
  149. /*
  150. * shouldn't happen, but just in case.
  151. */
  152. tr_pages = 0x15557000UL;
  153. size = memparse(str, &str);
  154. if (*str || !is_power_of_2(size) || !(tr_pages & size) ||
  155. size <= PAGE_SIZE ||
  156. size >= (1UL << PAGE_SHIFT << MAX_ORDER)) {
  157. printk(KERN_WARNING "Invalid huge page size specified\n");
  158. return 1;
  159. }
  160. hpage_shift = __ffs(size);
  161. /*
  162. * boot cpu already executed ia64_mmu_init, and has HPAGE_SHIFT_DEFAULT
  163. * override here with new page shift.
  164. */
  165. ia64_set_rr(HPAGE_REGION_BASE, hpage_shift << 2);
  166. return 0;
  167. }
  168. early_param("hugepagesz", hugetlb_setup_sz);