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 _PAGE_GATEWAY 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. free_pages((unsigned long)pmd, PMD_ORDER);
  75. }
  76. #else
  77. /* Two Level Page Table Support for pmd's */
  78. /*
  79. * allocating and freeing a pmd is trivial: the 1-entry pmd is
  80. * inside the pgd, so has no extra memory associated with it.
  81. */
  82. #define pmd_alloc_one(mm, addr) ({ BUG(); ((pmd_t *)2); })
  83. #define pmd_free(mm, x) do { } while (0)
  84. #define pgd_populate(mm, pmd, pte) BUG()
  85. #endif
  86. static inline void
  87. pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd, pte_t *pte)
  88. {
  89. #if CONFIG_PGTABLE_LEVELS == 3
  90. /* preserve the gateway marker if this is the beginning of
  91. * the permanent pmd */
  92. if(pmd_flag(*pmd) & PxD_FLAG_ATTACHED)
  93. __pmd_val_set(*pmd, (PxD_FLAG_PRESENT |
  94. PxD_FLAG_VALID |
  95. PxD_FLAG_ATTACHED)
  96. + (__u32)(__pa((unsigned long)pte) >> PxD_VALUE_SHIFT));
  97. else
  98. #endif
  99. __pmd_val_set(*pmd, (PxD_FLAG_PRESENT | PxD_FLAG_VALID)
  100. + (__u32)(__pa((unsigned long)pte) >> PxD_VALUE_SHIFT));
  101. }
  102. #define pmd_populate(mm, pmd, pte_page) \
  103. pmd_populate_kernel(mm, pmd, page_address(pte_page))
  104. #define pmd_pgtable(pmd) pmd_page(pmd)
  105. static inline pgtable_t
  106. pte_alloc_one(struct mm_struct *mm, unsigned long address)
  107. {
  108. struct page *page = alloc_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
  109. if (!page)
  110. return NULL;
  111. if (!pgtable_page_ctor(page)) {
  112. __free_page(page);
  113. return NULL;
  114. }
  115. return page;
  116. }
  117. static inline pte_t *
  118. pte_alloc_one_kernel(struct mm_struct *mm, unsigned long addr)
  119. {
  120. pte_t *pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
  121. return pte;
  122. }
  123. static inline void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
  124. {
  125. free_page((unsigned long)pte);
  126. }
  127. static inline void pte_free(struct mm_struct *mm, struct page *pte)
  128. {
  129. pgtable_page_dtor(pte);
  130. pte_free_kernel(mm, page_address(pte));
  131. }
  132. #define check_pgt_cache() do { } while (0)
  133. #endif