2 #include <asm/pgalloc.h>
5 pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
7 return (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
10 pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
15 pte = alloc_pages(GFP_KERNEL|__GFP_HIGHMEM|__GFP_REPEAT|__GFP_ZERO, 0);
17 pte = alloc_pages(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO, 0);
20 pgtable_page_ctor(pte);
24 void __pte_free_tlb(struct mmu_gather *tlb, struct page *pte)
26 pgtable_page_dtor(pte);
27 paravirt_release_pte(page_to_pfn(pte));
28 tlb_remove_page(tlb, pte);
31 #if PAGETABLE_LEVELS > 2
32 void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd)
34 paravirt_release_pmd(__pa(pmd) >> PAGE_SHIFT);
35 tlb_remove_page(tlb, virt_to_page(pmd));
38 #if PAGETABLE_LEVELS > 3
39 void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pud)
41 paravirt_release_pud(__pa(pud) >> PAGE_SHIFT);
42 tlb_remove_page(tlb, virt_to_page(pud));
44 #endif /* PAGETABLE_LEVELS > 3 */
45 #endif /* PAGETABLE_LEVELS > 2 */
47 static inline void pgd_list_add(pgd_t *pgd)
49 struct page *page = virt_to_page(pgd);
51 list_add(&page->lru, &pgd_list);
54 static inline void pgd_list_del(pgd_t *pgd)
56 struct page *page = virt_to_page(pgd);
62 pgd_t *pgd_alloc(struct mm_struct *mm)
65 pgd_t *pgd = (pgd_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT);
69 spin_lock_irqsave(&pgd_lock, flags);
71 spin_unlock_irqrestore(&pgd_lock, flags);
73 * Copy kernel pointers in from init.
74 * Could keep a freelist or slab cache of those because the kernel
77 boundary = pgd_index(__PAGE_OFFSET);
78 memset(pgd, 0, boundary * sizeof(pgd_t));
79 memcpy(pgd + boundary,
80 init_level4_pgt + boundary,
81 (PTRS_PER_PGD - boundary) * sizeof(pgd_t));
85 void pgd_free(struct mm_struct *mm, pgd_t *pgd)
88 BUG_ON((unsigned long)pgd & (PAGE_SIZE-1));
89 spin_lock_irqsave(&pgd_lock, flags);
91 spin_unlock_irqrestore(&pgd_lock, flags);
92 free_page((unsigned long)pgd);
96 * List of all pgd's needed for non-PAE so it can invalidate entries
97 * in both cached and uncached pgd's; not needed for PAE since the
98 * kernel pmd is shared. If PAE were not to share the pmd a similar
99 * tactic would be needed. This is essentially codepath-based locking
100 * against pageattr.c; it is the unique case in which a valid change
101 * of kernel pagetables can't be lazily synchronized by vmalloc faults.
102 * vmalloc faults work because attached pagetables are never freed.
105 #define UNSHARED_PTRS_PER_PGD \
106 (SHARED_KERNEL_PMD ? USER_PTRS_PER_PGD : PTRS_PER_PGD)
108 static void pgd_ctor(void *p)
113 /* Clear usermode parts of PGD */
114 memset(pgd, 0, USER_PTRS_PER_PGD*sizeof(pgd_t));
116 spin_lock_irqsave(&pgd_lock, flags);
118 /* If the pgd points to a shared pagetable level (either the
119 ptes in non-PAE, or shared PMD in PAE), then just copy the
120 references from swapper_pg_dir. */
121 if (PAGETABLE_LEVELS == 2 ||
122 (PAGETABLE_LEVELS == 3 && SHARED_KERNEL_PMD)) {
123 clone_pgd_range(pgd + USER_PTRS_PER_PGD,
124 swapper_pg_dir + USER_PTRS_PER_PGD,
126 paravirt_alloc_pmd_clone(__pa(pgd) >> PAGE_SHIFT,
127 __pa(swapper_pg_dir) >> PAGE_SHIFT,
132 /* list required to sync kernel mapping updates */
133 if (!SHARED_KERNEL_PMD)
136 spin_unlock_irqrestore(&pgd_lock, flags);
139 static void pgd_dtor(void *pgd)
141 unsigned long flags; /* can be called from interrupt context */
143 if (SHARED_KERNEL_PMD)
146 spin_lock_irqsave(&pgd_lock, flags);
148 spin_unlock_irqrestore(&pgd_lock, flags);
151 #ifdef CONFIG_X86_PAE
153 * Mop up any pmd pages which may still be attached to the pgd.
154 * Normally they will be freed by munmap/exit_mmap, but any pmd we
155 * preallocate which never got a corresponding vma will need to be
158 static void pgd_mop_up_pmds(struct mm_struct *mm, pgd_t *pgdp)
162 for(i = 0; i < UNSHARED_PTRS_PER_PGD; i++) {
165 if (pgd_val(pgd) != 0) {
166 pmd_t *pmd = (pmd_t *)pgd_page_vaddr(pgd);
168 pgdp[i] = native_make_pgd(0);
170 paravirt_release_pmd(pgd_val(pgd) >> PAGE_SHIFT);
177 * In PAE mode, we need to do a cr3 reload (=tlb flush) when
178 * updating the top-level pagetable entries to guarantee the
179 * processor notices the update. Since this is expensive, and
180 * all 4 top-level entries are used almost immediately in a
181 * new process's life, we just pre-populate them here.
183 * Also, if we're in a paravirt environment where the kernel pmd is
184 * not shared between pagetables (!SHARED_KERNEL_PMDS), we allocate
185 * and initialize the kernel pmds here.
187 static int pgd_prepopulate_pmd(struct mm_struct *mm, pgd_t *pgd)
193 pud = pud_offset(pgd, 0);
194 for (addr = i = 0; i < UNSHARED_PTRS_PER_PGD;
195 i++, pud++, addr += PUD_SIZE) {
196 pmd_t *pmd = pmd_alloc_one(mm, addr);
199 pgd_mop_up_pmds(mm, pgd);
203 if (i >= USER_PTRS_PER_PGD)
204 memcpy(pmd, (pmd_t *)pgd_page_vaddr(swapper_pg_dir[i]),
205 sizeof(pmd_t) * PTRS_PER_PMD);
207 pud_populate(mm, pud, pmd);
213 void pud_populate(struct mm_struct *mm, pud_t *pudp, pmd_t *pmd)
215 paravirt_alloc_pmd(mm, __pa(pmd) >> PAGE_SHIFT);
217 /* Note: almost everything apart from _PAGE_PRESENT is
218 reserved at the pmd (PDPT) level. */
219 set_pud(pudp, __pud(__pa(pmd) | _PAGE_PRESENT));
222 * According to Intel App note "TLBs, Paging-Structure Caches,
223 * and Their Invalidation", April 2007, document 317080-001,
224 * section 8.1: in PAE mode we explicitly have to flush the
225 * TLB via cr3 if the top-level pgd is changed...
227 if (mm == current->active_mm)
228 write_cr3(read_cr3());
230 #else /* !CONFIG_X86_PAE */
231 /* No need to prepopulate any pagetable entries in non-PAE modes. */
232 static int pgd_prepopulate_pmd(struct mm_struct *mm, pgd_t *pgd)
237 static void pgd_mop_up_pmds(struct mm_struct *mm, pgd_t *pgd)
240 #endif /* CONFIG_X86_PAE */
242 pgd_t *pgd_alloc(struct mm_struct *mm)
244 pgd_t *pgd = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
246 /* so that alloc_pmd can use it */
251 if (pgd && !pgd_prepopulate_pmd(mm, pgd)) {
253 free_page((unsigned long)pgd);
260 void pgd_free(struct mm_struct *mm, pgd_t *pgd)
262 pgd_mop_up_pmds(mm, pgd);
264 free_page((unsigned long)pgd);