pgalloc.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #ifndef _ASM_PGALLOC_H
  2. #define _ASM_PGALLOC_H
  3. #include <linux/gfp.h>
  4. #include <linux/mm.h>
  5. #include <linux/threads.h>
  6. #include <asm/processor.h>
  7. #include <asm/fixmap.h>
  8. #include <asm/cache.h>
  9. /* Allocate the top level pgd (page directory)
  10. *
  11. * Here (for 64 bit kernels) we implement a Hybrid L2/L3 scheme: we
  12. * allocate the first pmd adjacent to the pgd. This means that we can
  13. * subtract a constant offset to get to it. The pmd and pgd sizes are
  14. * arranged so that a single pmd covers 4GB (giving a full 64-bit
  15. * process access to 8TB) so our lookups are effectively L2 for the
  16. * first 4GB of the kernel (i.e. for all ILP32 processes and all the
  17. * kernel for machines with under 4GB of memory) */
  18. static inline pgd_t *pgd_alloc(struct mm_struct *mm)
  19. {
  20. pgd_t *pgd = (pgd_t *)__get_free_pages(GFP_KERNEL,
  21. PGD_ALLOC_ORDER);
  22. pgd_t *actual_pgd = pgd;
  23. if (likely(pgd != NULL)) {
  24. memset(pgd, 0, PAGE_SIZE<<PGD_ALLOC_ORDER);
  25. #if CONFIG_PGTABLE_LEVELS == 3
  26. actual_pgd += PTRS_PER_PGD;
  27. /* Populate first pmd with allocated memory. We mark it
  28. * with PxD_FLAG_ATTACHED as a signal to the system that this
  29. * pmd entry may not be cleared. */
  30. __pgd_val_set(*actual_pgd, (PxD_FLAG_PRESENT |
  31. PxD_FLAG_VALID |
  32. PxD_FLAG_ATTACHED)
  33. + (__u32)(__pa((unsigned long)pgd) >> PxD_VALUE_SHIFT));
  34. /* The first pmd entry also is marked with PxD_FLAG_ATTACHED as
  35. * a signal that this pmd may not be freed */
  36. __pgd_val_set(*pgd, PxD_FLAG_ATTACHED);
  37. #endif
  38. }
  39. return actual_pgd;
  40. }
  41. static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)
  42. {
  43. #if CONFIG_PGTABLE_LEVELS == 3
  44. pgd -= PTRS_PER_PGD;
  45. #endif
  46. free_pages((unsigned long)pgd, PGD_ALLOC_ORDER);
  47. }
  48. #if CONFIG_PGTABLE_LEVELS == 3
  49. /* Three Level Page Table Support for pmd's */
  50. static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgd, pmd_t *pmd)
  51. {
  52. __pgd_val_set(*pgd, (PxD_FLAG_PRESENT | PxD_FLAG_VALID) +
  53. (__u32)(__pa((unsigned long)pmd) >> PxD_VALUE_SHIFT));
  54. }
  55. static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long address)
  56. {
  57. pmd_t *pmd = (pmd_t *)__get_free_pages(GFP_KERNEL|__GFP_REPEAT,
  58. PMD_ORDER);
  59. if (pmd)
  60. memset(pmd, 0, PAGE_SIZE<<PMD_ORDER);
  61. return pmd;
  62. }
  63. static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd)
  64. {
  65. if (pmd_flag(*pmd) & PxD_FLAG_ATTACHED) {
  66. /*
  67. * This is the permanent pmd attached to the pgd;
  68. * cannot free it.
  69. * Increment the counter to compensate for the decrement
  70. * done by generic mm code.
  71. */
  72. mm_inc_nr_pmds(mm);
  73. return;
  74. }
  75. free_pages((unsigned long)pmd, PMD_ORDER);
  76. }
  77. #else
  78. /* Two Level Page Table Support for pmd's */
  79. /*
  80. * allocating and freeing a pmd is trivial: the 1-entry pmd is
  81. * inside the pgd, so has no extra memory associated with it.
  82. */
  83. #define pmd_alloc_one(mm, addr) ({ BUG(); ((pmd_t *)2); })
  84. #define pmd_free(mm, x) do { } while (0)
  85. #define pgd_populate(mm, pmd, pte) BUG()
  86. #endif
  87. static inline void
  88. pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd, pte_t *pte)
  89. {
  90. #if CONFIG_PGTABLE_LEVELS == 3
  91. /* preserve the gateway marker if this is the beginning of
  92. * the permanent pmd */
  93. if(pmd_flag(*pmd) & PxD_FLAG_ATTACHED)
  94. __pmd_val_set(*pmd, (PxD_FLAG_PRESENT |
  95. PxD_FLAG_VALID |
  96. PxD_FLAG_ATTACHED)
  97. + (__u32)(__pa((unsigned long)pte) >> PxD_VALUE_SHIFT));
  98. else
  99. #endif
  100. __pmd_val_set(*pmd, (PxD_FLAG_PRESENT | PxD_FLAG_VALID)
  101. + (__u32)(__pa((unsigned long)pte) >> PxD_VALUE_SHIFT));
  102. }
  103. #define pmd_populate(mm, pmd, pte_page) \
  104. pmd_populate_kernel(mm, pmd, page_address(pte_page))
  105. #define pmd_pgtable(pmd) pmd_page(pmd)
  106. static inline pgtable_t
  107. pte_alloc_one(struct mm_struct *mm, unsigned long address)
  108. {
  109. struct page *page = alloc_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
  110. if (!page)
  111. return NULL;
  112. if (!pgtable_page_ctor(page)) {
  113. __free_page(page);
  114. return NULL;
  115. }
  116. return page;
  117. }
  118. static inline pte_t *
  119. pte_alloc_one_kernel(struct mm_struct *mm, unsigned long addr)
  120. {
  121. pte_t *pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
  122. return pte;
  123. }
  124. static inline void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
  125. {
  126. free_page((unsigned long)pte);
  127. }
  128. static inline void pte_free(struct mm_struct *mm, struct page *pte)
  129. {
  130. pgtable_page_dtor(pte);
  131. pte_free_kernel(mm, page_address(pte));
  132. }
  133. #define check_pgt_cache() do { } while (0)
  134. #endif