pgalloc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* pgalloc.c: page directory & page table allocation
  2. *
  3. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/sched.h>
  12. #include <linux/gfp.h>
  13. #include <linux/mm.h>
  14. #include <linux/highmem.h>
  15. #include <linux/quicklist.h>
  16. #include <asm/pgalloc.h>
  17. #include <asm/page.h>
  18. #include <asm/cacheflush.h>
  19. pgd_t swapper_pg_dir[PTRS_PER_PGD] __attribute__((aligned(PAGE_SIZE)));
  20. pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
  21. {
  22. pte_t *pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT);
  23. if (pte)
  24. clear_page(pte);
  25. return pte;
  26. }
  27. pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
  28. {
  29. struct page *page;
  30. #ifdef CONFIG_HIGHPTE
  31. page = alloc_pages(GFP_KERNEL|__GFP_HIGHMEM|__GFP_REPEAT, 0);
  32. #else
  33. page = alloc_pages(GFP_KERNEL|__GFP_REPEAT, 0);
  34. #endif
  35. if (page) {
  36. clear_highpage(page);
  37. pgtable_page_ctor(page);
  38. flush_dcache_page(page);
  39. }
  40. return page;
  41. }
  42. void __set_pmd(pmd_t *pmdptr, unsigned long pmd)
  43. {
  44. unsigned long *__ste_p = pmdptr->ste;
  45. int loop;
  46. if (!pmd) {
  47. memset(__ste_p, 0, PME_SIZE);
  48. }
  49. else {
  50. BUG_ON(pmd & (0x3f00 | xAMPRx_SS | 0xe));
  51. for (loop = PME_SIZE; loop > 0; loop -= 4) {
  52. *__ste_p++ = pmd;
  53. pmd += __frv_PT_SIZE;
  54. }
  55. }
  56. frv_dcache_writeback((unsigned long) pmdptr, (unsigned long) (pmdptr + 1));
  57. }
  58. /*
  59. * List of all pgd's needed for non-PAE so it can invalidate entries
  60. * in both cached and uncached pgd's; not needed for PAE since the
  61. * kernel pmd is shared. If PAE were not to share the pmd a similar
  62. * tactic would be needed. This is essentially codepath-based locking
  63. * against pageattr.c; it is the unique case in which a valid change
  64. * of kernel pagetables can't be lazily synchronized by vmalloc faults.
  65. * vmalloc faults work because attached pagetables are never freed.
  66. * If the locking proves to be non-performant, a ticketing scheme with
  67. * checks at dup_mmap(), exec(), and other mmlist addition points
  68. * could be used. The locking scheme was chosen on the basis of
  69. * manfred's recommendations and having no core impact whatsoever.
  70. * -- wli
  71. */
  72. DEFINE_SPINLOCK(pgd_lock);
  73. struct page *pgd_list;
  74. static inline void pgd_list_add(pgd_t *pgd)
  75. {
  76. struct page *page = virt_to_page(pgd);
  77. page->index = (unsigned long) pgd_list;
  78. if (pgd_list)
  79. set_page_private(pgd_list, (unsigned long) &page->index);
  80. pgd_list = page;
  81. set_page_private(page, (unsigned long)&pgd_list);
  82. }
  83. static inline void pgd_list_del(pgd_t *pgd)
  84. {
  85. struct page *next, **pprev, *page = virt_to_page(pgd);
  86. next = (struct page *) page->index;
  87. pprev = (struct page **) page_private(page);
  88. *pprev = next;
  89. if (next)
  90. set_page_private(next, (unsigned long) pprev);
  91. }
  92. void pgd_ctor(void *pgd)
  93. {
  94. unsigned long flags;
  95. if (PTRS_PER_PMD == 1)
  96. spin_lock_irqsave(&pgd_lock, flags);
  97. memcpy((pgd_t *) pgd + USER_PGDS_IN_LAST_PML4,
  98. swapper_pg_dir + USER_PGDS_IN_LAST_PML4,
  99. (PTRS_PER_PGD - USER_PGDS_IN_LAST_PML4) * sizeof(pgd_t));
  100. if (PTRS_PER_PMD > 1)
  101. return;
  102. pgd_list_add(pgd);
  103. spin_unlock_irqrestore(&pgd_lock, flags);
  104. memset(pgd, 0, USER_PGDS_IN_LAST_PML4 * sizeof(pgd_t));
  105. }
  106. /* never called when PTRS_PER_PMD > 1 */
  107. void pgd_dtor(void *pgd)
  108. {
  109. unsigned long flags; /* can be called from interrupt context */
  110. spin_lock_irqsave(&pgd_lock, flags);
  111. pgd_list_del(pgd);
  112. spin_unlock_irqrestore(&pgd_lock, flags);
  113. }
  114. pgd_t *pgd_alloc(struct mm_struct *mm)
  115. {
  116. pgd_t *pgd;
  117. pgd = quicklist_alloc(0, GFP_KERNEL, pgd_ctor);
  118. if (!pgd)
  119. return pgd;
  120. return pgd;
  121. }
  122. void pgd_free(struct mm_struct *mm, pgd_t *pgd)
  123. {
  124. /* in the non-PAE case, clear_page_tables() clears user pgd entries */
  125. quicklist_free(0, pgd_dtor, pgd);
  126. }
  127. void __init pgtable_cache_init(void)
  128. {
  129. }
  130. void check_pgt_cache(void)
  131. {
  132. quicklist_trim(0, pgd_dtor, 25, 16);
  133. }