pgalloc.h 4.0 KB

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