pgd.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * linux/arch/arm/mm/pgd.c
  3. *
  4. * Copyright (C) 1998-2005 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/mm.h>
  11. #include <linux/gfp.h>
  12. #include <linux/highmem.h>
  13. #include <linux/slab.h>
  14. #include <asm/cp15.h>
  15. #include <asm/pgalloc.h>
  16. #include <asm/page.h>
  17. #include <asm/tlbflush.h>
  18. #include "mm.h"
  19. #ifdef CONFIG_ARM_LPAE
  20. #define __pgd_alloc() kmalloc_array(PTRS_PER_PGD, sizeof(pgd_t), GFP_KERNEL)
  21. #define __pgd_free(pgd) kfree(pgd)
  22. #else
  23. #define __pgd_alloc() (pgd_t *)__get_free_pages(GFP_KERNEL, 2)
  24. #define __pgd_free(pgd) free_pages((unsigned long)pgd, 2)
  25. #endif
  26. /*
  27. * need to get a 16k page for level 1
  28. */
  29. pgd_t *pgd_alloc(struct mm_struct *mm)
  30. {
  31. pgd_t *new_pgd, *init_pgd;
  32. pud_t *new_pud, *init_pud;
  33. pmd_t *new_pmd, *init_pmd;
  34. pte_t *new_pte, *init_pte;
  35. new_pgd = __pgd_alloc();
  36. if (!new_pgd)
  37. goto no_pgd;
  38. memset(new_pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
  39. /*
  40. * Copy over the kernel and IO PGD entries
  41. */
  42. init_pgd = pgd_offset_k(0);
  43. memcpy(new_pgd + USER_PTRS_PER_PGD, init_pgd + USER_PTRS_PER_PGD,
  44. (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
  45. clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t));
  46. #ifdef CONFIG_ARM_LPAE
  47. /*
  48. * Allocate PMD table for modules and pkmap mappings.
  49. */
  50. new_pud = pud_alloc(mm, new_pgd + pgd_index(MODULES_VADDR),
  51. MODULES_VADDR);
  52. if (!new_pud)
  53. goto no_pud;
  54. new_pmd = pmd_alloc(mm, new_pud, 0);
  55. if (!new_pmd)
  56. goto no_pmd;
  57. #endif
  58. if (!vectors_high()) {
  59. /*
  60. * On ARM, first page must always be allocated since it
  61. * contains the machine vectors. The vectors are always high
  62. * with LPAE.
  63. */
  64. new_pud = pud_alloc(mm, new_pgd, 0);
  65. if (!new_pud)
  66. goto no_pud;
  67. new_pmd = pmd_alloc(mm, new_pud, 0);
  68. if (!new_pmd)
  69. goto no_pmd;
  70. new_pte = pte_alloc_map(mm, new_pmd, 0);
  71. if (!new_pte)
  72. goto no_pte;
  73. #ifndef CONFIG_ARM_LPAE
  74. /*
  75. * Modify the PTE pointer to have the correct domain. This
  76. * needs to be the vectors domain to avoid the low vectors
  77. * being unmapped.
  78. */
  79. pmd_val(*new_pmd) &= ~PMD_DOMAIN_MASK;
  80. pmd_val(*new_pmd) |= PMD_DOMAIN(DOMAIN_VECTORS);
  81. #endif
  82. init_pud = pud_offset(init_pgd, 0);
  83. init_pmd = pmd_offset(init_pud, 0);
  84. init_pte = pte_offset_map(init_pmd, 0);
  85. set_pte_ext(new_pte + 0, init_pte[0], 0);
  86. set_pte_ext(new_pte + 1, init_pte[1], 0);
  87. pte_unmap(init_pte);
  88. pte_unmap(new_pte);
  89. }
  90. return new_pgd;
  91. no_pte:
  92. pmd_free(mm, new_pmd);
  93. mm_dec_nr_pmds(mm);
  94. no_pmd:
  95. pud_free(mm, new_pud);
  96. no_pud:
  97. __pgd_free(new_pgd);
  98. no_pgd:
  99. return NULL;
  100. }
  101. void pgd_free(struct mm_struct *mm, pgd_t *pgd_base)
  102. {
  103. pgd_t *pgd;
  104. pud_t *pud;
  105. pmd_t *pmd;
  106. pgtable_t pte;
  107. if (!pgd_base)
  108. return;
  109. pgd = pgd_base + pgd_index(0);
  110. if (pgd_none_or_clear_bad(pgd))
  111. goto no_pgd;
  112. pud = pud_offset(pgd, 0);
  113. if (pud_none_or_clear_bad(pud))
  114. goto no_pud;
  115. pmd = pmd_offset(pud, 0);
  116. if (pmd_none_or_clear_bad(pmd))
  117. goto no_pmd;
  118. pte = pmd_pgtable(*pmd);
  119. pmd_clear(pmd);
  120. pte_free(mm, pte);
  121. mm_dec_nr_ptes(mm);
  122. no_pmd:
  123. pud_clear(pud);
  124. pmd_free(mm, pmd);
  125. mm_dec_nr_pmds(mm);
  126. no_pud:
  127. pgd_clear(pgd);
  128. pud_free(mm, pud);
  129. no_pgd:
  130. #ifdef CONFIG_ARM_LPAE
  131. /*
  132. * Free modules/pkmap or identity pmd tables.
  133. */
  134. for (pgd = pgd_base; pgd < pgd_base + PTRS_PER_PGD; pgd++) {
  135. if (pgd_none_or_clear_bad(pgd))
  136. continue;
  137. if (pgd_val(*pgd) & L_PGD_SWAPPER)
  138. continue;
  139. pud = pud_offset(pgd, 0);
  140. if (pud_none_or_clear_bad(pud))
  141. continue;
  142. pmd = pmd_offset(pud, 0);
  143. pud_clear(pud);
  144. pmd_free(mm, pmd);
  145. mm_dec_nr_pmds(mm);
  146. pgd_clear(pgd);
  147. pud_free(mm, pud);
  148. }
  149. #endif
  150. __pgd_free(pgd_base);
  151. }