pgd.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <asm/pgalloc.h>
  14. #include <asm/page.h>
  15. #include <asm/tlbflush.h>
  16. #include "mm.h"
  17. /*
  18. * need to get a 16k page for level 1
  19. */
  20. pgd_t *pgd_alloc(struct mm_struct *mm)
  21. {
  22. pgd_t *new_pgd, *init_pgd;
  23. pud_t *new_pud, *init_pud;
  24. pmd_t *new_pmd, *init_pmd;
  25. pte_t *new_pte, *init_pte;
  26. new_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL, 2);
  27. if (!new_pgd)
  28. goto no_pgd;
  29. memset(new_pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
  30. /*
  31. * Copy over the kernel and IO PGD entries
  32. */
  33. init_pgd = pgd_offset_k(0);
  34. memcpy(new_pgd + USER_PTRS_PER_PGD, init_pgd + USER_PTRS_PER_PGD,
  35. (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
  36. clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t));
  37. if (!vectors_high()) {
  38. /*
  39. * On ARM, first page must always be allocated since it
  40. * contains the machine vectors.
  41. */
  42. new_pud = pud_alloc(mm, new_pgd, 0);
  43. if (!new_pud)
  44. goto no_pud;
  45. new_pmd = pmd_alloc(mm, new_pud, 0);
  46. if (!new_pmd)
  47. goto no_pmd;
  48. new_pte = pte_alloc_map(mm, NULL, new_pmd, 0);
  49. if (!new_pte)
  50. goto no_pte;
  51. init_pud = pud_offset(init_pgd, 0);
  52. init_pmd = pmd_offset(init_pud, 0);
  53. init_pte = pte_offset_map(init_pmd, 0);
  54. set_pte_ext(new_pte, *init_pte, 0);
  55. pte_unmap(init_pte);
  56. pte_unmap(new_pte);
  57. }
  58. return new_pgd;
  59. no_pte:
  60. pmd_free(mm, new_pmd);
  61. no_pmd:
  62. pud_free(mm, new_pud);
  63. no_pud:
  64. free_pages((unsigned long)new_pgd, 2);
  65. no_pgd:
  66. return NULL;
  67. }
  68. void pgd_free(struct mm_struct *mm, pgd_t *pgd_base)
  69. {
  70. pgd_t *pgd;
  71. pud_t *pud;
  72. pmd_t *pmd;
  73. pgtable_t pte;
  74. if (!pgd_base)
  75. return;
  76. pgd = pgd_base + pgd_index(0);
  77. if (pgd_none_or_clear_bad(pgd))
  78. goto no_pgd;
  79. pud = pud_offset(pgd, 0);
  80. if (pud_none_or_clear_bad(pud))
  81. goto no_pud;
  82. pmd = pmd_offset(pud, 0);
  83. if (pmd_none_or_clear_bad(pmd))
  84. goto no_pmd;
  85. pte = pmd_pgtable(*pmd);
  86. pmd_clear(pmd);
  87. pte_free(mm, pte);
  88. no_pmd:
  89. pud_clear(pud);
  90. pmd_free(mm, pmd);
  91. no_pud:
  92. pgd_clear(pgd);
  93. pud_free(mm, pud);
  94. no_pgd:
  95. free_pages((unsigned long) pgd_base, 2);
  96. }