pgd.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. pmd_t *new_pmd, *init_pmd;
  24. pte_t *new_pte, *init_pte;
  25. new_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL, 2);
  26. if (!new_pgd)
  27. goto no_pgd;
  28. memset(new_pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
  29. /*
  30. * Copy over the kernel and IO PGD entries
  31. */
  32. init_pgd = pgd_offset_k(0);
  33. memcpy(new_pgd + USER_PTRS_PER_PGD, init_pgd + USER_PTRS_PER_PGD,
  34. (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
  35. clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t));
  36. if (!vectors_high()) {
  37. /*
  38. * On ARM, first page must always be allocated since it
  39. * contains the machine vectors.
  40. */
  41. new_pmd = pmd_alloc(mm, new_pgd, 0);
  42. if (!new_pmd)
  43. goto no_pmd;
  44. new_pte = pte_alloc_map(mm, NULL, new_pmd, 0);
  45. if (!new_pte)
  46. goto no_pte;
  47. init_pmd = pmd_offset(init_pgd, 0);
  48. init_pte = pte_offset_map(init_pmd, 0);
  49. set_pte_ext(new_pte, *init_pte, 0);
  50. pte_unmap(init_pte);
  51. pte_unmap(new_pte);
  52. }
  53. return new_pgd;
  54. no_pte:
  55. pmd_free(mm, new_pmd);
  56. no_pmd:
  57. free_pages((unsigned long)new_pgd, 2);
  58. no_pgd:
  59. return NULL;
  60. }
  61. void pgd_free(struct mm_struct *mm, pgd_t *pgd_base)
  62. {
  63. pgd_t *pgd;
  64. pmd_t *pmd;
  65. pgtable_t pte;
  66. if (!pgd_base)
  67. return;
  68. pgd = pgd_base + pgd_index(0);
  69. if (pgd_none_or_clear_bad(pgd))
  70. goto no_pgd;
  71. pmd = pmd_offset(pgd, 0);
  72. if (pmd_none_or_clear_bad(pmd))
  73. goto no_pmd;
  74. pte = pmd_pgtable(*pmd);
  75. pmd_clear(pmd);
  76. pte_free(mm, pte);
  77. no_pmd:
  78. pgd_clear(pgd);
  79. pmd_free(mm, pmd);
  80. no_pgd:
  81. free_pages((unsigned long) pgd_base, 2);
  82. }