pgd.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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(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, NULL, new_pmd, 0);
  71. if (!new_pte)
  72. goto no_pte;
  73. init_pud = pud_offset(init_pgd, 0);
  74. init_pmd = pmd_offset(init_pud, 0);
  75. init_pte = pte_offset_map(init_pmd, 0);
  76. set_pte_ext(new_pte, *init_pte, 0);
  77. pte_unmap(init_pte);
  78. pte_unmap(new_pte);
  79. }
  80. return new_pgd;
  81. no_pte:
  82. pmd_free(mm, new_pmd);
  83. no_pmd:
  84. pud_free(mm, new_pud);
  85. no_pud:
  86. __pgd_free(new_pgd);
  87. no_pgd:
  88. return NULL;
  89. }
  90. void pgd_free(struct mm_struct *mm, pgd_t *pgd_base)
  91. {
  92. pgd_t *pgd;
  93. pud_t *pud;
  94. pmd_t *pmd;
  95. pgtable_t pte;
  96. if (!pgd_base)
  97. return;
  98. pgd = pgd_base + pgd_index(0);
  99. if (pgd_none_or_clear_bad(pgd))
  100. goto no_pgd;
  101. pud = pud_offset(pgd, 0);
  102. if (pud_none_or_clear_bad(pud))
  103. goto no_pud;
  104. pmd = pmd_offset(pud, 0);
  105. if (pmd_none_or_clear_bad(pmd))
  106. goto no_pmd;
  107. pte = pmd_pgtable(*pmd);
  108. pmd_clear(pmd);
  109. pte_free(mm, pte);
  110. no_pmd:
  111. pud_clear(pud);
  112. pmd_free(mm, pmd);
  113. no_pud:
  114. pgd_clear(pgd);
  115. pud_free(mm, pud);
  116. no_pgd:
  117. #ifdef CONFIG_ARM_LPAE
  118. /*
  119. * Free modules/pkmap or identity pmd tables.
  120. */
  121. for (pgd = pgd_base; pgd < pgd_base + PTRS_PER_PGD; pgd++) {
  122. if (pgd_none_or_clear_bad(pgd))
  123. continue;
  124. if (pgd_val(*pgd) & L_PGD_SWAPPER)
  125. continue;
  126. pud = pud_offset(pgd, 0);
  127. if (pud_none_or_clear_bad(pud))
  128. continue;
  129. pmd = pmd_offset(pud, 0);
  130. pud_clear(pud);
  131. pmd_free(mm, pmd);
  132. pgd_clear(pgd);
  133. pud_free(mm, pud);
  134. }
  135. #endif
  136. __pgd_free(pgd_base);
  137. }