]> err.no Git - linux-2.6/blob - arch/x86/mm/pgtable.c
x86: move pmd functions into common asm/pgalloc.h
[linux-2.6] / arch / x86 / mm / pgtable.c
1 #include <linux/mm.h>
2 #include <asm/pgalloc.h>
3 #include <asm/tlb.h>
4
5 pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
6 {
7         return (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
8 }
9
10 pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
11 {
12         struct page *pte;
13
14 #ifdef CONFIG_HIGHPTE
15         pte = alloc_pages(GFP_KERNEL|__GFP_HIGHMEM|__GFP_REPEAT|__GFP_ZERO, 0);
16 #else
17         pte = alloc_pages(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO, 0);
18 #endif
19         if (pte)
20                 pgtable_page_ctor(pte);
21         return pte;
22 }
23
24 void __pte_free_tlb(struct mmu_gather *tlb, struct page *pte)
25 {
26         pgtable_page_dtor(pte);
27         paravirt_release_pt(page_to_pfn(pte));
28         tlb_remove_page(tlb, pte);
29 }
30
31 #if PAGETABLE_LEVELS > 2
32 void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd)
33 {
34         paravirt_release_pd(__pa(pmd) >> PAGE_SHIFT);
35         tlb_remove_page(tlb, virt_to_page(pmd));
36 }
37 #endif  /* PAGETABLE_LEVELS > 2 */
38
39 #ifdef CONFIG_X86_64
40 static inline void pgd_list_add(pgd_t *pgd)
41 {
42         struct page *page = virt_to_page(pgd);
43         unsigned long flags;
44
45         spin_lock_irqsave(&pgd_lock, flags);
46         list_add(&page->lru, &pgd_list);
47         spin_unlock_irqrestore(&pgd_lock, flags);
48 }
49
50 static inline void pgd_list_del(pgd_t *pgd)
51 {
52         struct page *page = virt_to_page(pgd);
53         unsigned long flags;
54
55         spin_lock_irqsave(&pgd_lock, flags);
56         list_del(&page->lru);
57         spin_unlock_irqrestore(&pgd_lock, flags);
58 }
59
60 pgd_t *pgd_alloc(struct mm_struct *mm)
61 {
62         unsigned boundary;
63         pgd_t *pgd = (pgd_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT);
64         if (!pgd)
65                 return NULL;
66         pgd_list_add(pgd);
67         /*
68          * Copy kernel pointers in from init.
69          * Could keep a freelist or slab cache of those because the kernel
70          * part never changes.
71          */
72         boundary = pgd_index(__PAGE_OFFSET);
73         memset(pgd, 0, boundary * sizeof(pgd_t));
74         memcpy(pgd + boundary,
75                init_level4_pgt + boundary,
76                (PTRS_PER_PGD - boundary) * sizeof(pgd_t));
77         return pgd;
78 }
79
80 void pgd_free(struct mm_struct *mm, pgd_t *pgd)
81 {
82         BUG_ON((unsigned long)pgd & (PAGE_SIZE-1));
83         pgd_list_del(pgd);
84         free_page((unsigned long)pgd);
85 }
86 #else
87 /*
88  * List of all pgd's needed for non-PAE so it can invalidate entries
89  * in both cached and uncached pgd's; not needed for PAE since the
90  * kernel pmd is shared. If PAE were not to share the pmd a similar
91  * tactic would be needed. This is essentially codepath-based locking
92  * against pageattr.c; it is the unique case in which a valid change
93  * of kernel pagetables can't be lazily synchronized by vmalloc faults.
94  * vmalloc faults work because attached pagetables are never freed.
95  * -- wli
96  */
97 static inline void pgd_list_add(pgd_t *pgd)
98 {
99         struct page *page = virt_to_page(pgd);
100
101         list_add(&page->lru, &pgd_list);
102 }
103
104 static inline void pgd_list_del(pgd_t *pgd)
105 {
106         struct page *page = virt_to_page(pgd);
107
108         list_del(&page->lru);
109 }
110
111 #define UNSHARED_PTRS_PER_PGD                           \
112         (SHARED_KERNEL_PMD ? USER_PTRS_PER_PGD : PTRS_PER_PGD)
113
114 static void pgd_ctor(void *p)
115 {
116         pgd_t *pgd = p;
117         unsigned long flags;
118
119         /* Clear usermode parts of PGD */
120         memset(pgd, 0, USER_PTRS_PER_PGD*sizeof(pgd_t));
121
122         spin_lock_irqsave(&pgd_lock, flags);
123
124         /* If the pgd points to a shared pagetable level (either the
125            ptes in non-PAE, or shared PMD in PAE), then just copy the
126            references from swapper_pg_dir. */
127         if (PAGETABLE_LEVELS == 2 ||
128             (PAGETABLE_LEVELS == 3 && SHARED_KERNEL_PMD)) {
129                 clone_pgd_range(pgd + USER_PTRS_PER_PGD,
130                                 swapper_pg_dir + USER_PTRS_PER_PGD,
131                                 KERNEL_PGD_PTRS);
132                 paravirt_alloc_pd_clone(__pa(pgd) >> PAGE_SHIFT,
133                                         __pa(swapper_pg_dir) >> PAGE_SHIFT,
134                                         USER_PTRS_PER_PGD,
135                                         KERNEL_PGD_PTRS);
136         }
137
138         /* list required to sync kernel mapping updates */
139         if (!SHARED_KERNEL_PMD)
140                 pgd_list_add(pgd);
141
142         spin_unlock_irqrestore(&pgd_lock, flags);
143 }
144
145 static void pgd_dtor(void *pgd)
146 {
147         unsigned long flags; /* can be called from interrupt context */
148
149         if (SHARED_KERNEL_PMD)
150                 return;
151
152         spin_lock_irqsave(&pgd_lock, flags);
153         pgd_list_del(pgd);
154         spin_unlock_irqrestore(&pgd_lock, flags);
155 }
156
157 #ifdef CONFIG_X86_PAE
158 /*
159  * Mop up any pmd pages which may still be attached to the pgd.
160  * Normally they will be freed by munmap/exit_mmap, but any pmd we
161  * preallocate which never got a corresponding vma will need to be
162  * freed manually.
163  */
164 static void pgd_mop_up_pmds(struct mm_struct *mm, pgd_t *pgdp)
165 {
166         int i;
167
168         for(i = 0; i < UNSHARED_PTRS_PER_PGD; i++) {
169                 pgd_t pgd = pgdp[i];
170
171                 if (pgd_val(pgd) != 0) {
172                         pmd_t *pmd = (pmd_t *)pgd_page_vaddr(pgd);
173
174                         pgdp[i] = native_make_pgd(0);
175
176                         paravirt_release_pd(pgd_val(pgd) >> PAGE_SHIFT);
177                         pmd_free(mm, pmd);
178                 }
179         }
180 }
181
182 /*
183  * In PAE mode, we need to do a cr3 reload (=tlb flush) when
184  * updating the top-level pagetable entries to guarantee the
185  * processor notices the update.  Since this is expensive, and
186  * all 4 top-level entries are used almost immediately in a
187  * new process's life, we just pre-populate them here.
188  *
189  * Also, if we're in a paravirt environment where the kernel pmd is
190  * not shared between pagetables (!SHARED_KERNEL_PMDS), we allocate
191  * and initialize the kernel pmds here.
192  */
193 static int pgd_prepopulate_pmd(struct mm_struct *mm, pgd_t *pgd)
194 {
195         pud_t *pud;
196         unsigned long addr;
197         int i;
198
199         pud = pud_offset(pgd, 0);
200         for (addr = i = 0; i < UNSHARED_PTRS_PER_PGD;
201              i++, pud++, addr += PUD_SIZE) {
202                 pmd_t *pmd = pmd_alloc_one(mm, addr);
203
204                 if (!pmd) {
205                         pgd_mop_up_pmds(mm, pgd);
206                         return 0;
207                 }
208
209                 if (i >= USER_PTRS_PER_PGD)
210                         memcpy(pmd, (pmd_t *)pgd_page_vaddr(swapper_pg_dir[i]),
211                                sizeof(pmd_t) * PTRS_PER_PMD);
212
213                 pud_populate(mm, pud, pmd);
214         }
215
216         return 1;
217 }
218
219 void pud_populate(struct mm_struct *mm, pud_t *pudp, pmd_t *pmd)
220 {
221         paravirt_alloc_pd(mm, __pa(pmd) >> PAGE_SHIFT);
222
223         /* Note: almost everything apart from _PAGE_PRESENT is
224            reserved at the pmd (PDPT) level. */
225         set_pud(pudp, __pud(__pa(pmd) | _PAGE_PRESENT));
226
227         /*
228          * According to Intel App note "TLBs, Paging-Structure Caches,
229          * and Their Invalidation", April 2007, document 317080-001,
230          * section 8.1: in PAE mode we explicitly have to flush the
231          * TLB via cr3 if the top-level pgd is changed...
232          */
233         if (mm == current->active_mm)
234                 write_cr3(read_cr3());
235 }
236 #else  /* !CONFIG_X86_PAE */
237 /* No need to prepopulate any pagetable entries in non-PAE modes. */
238 static int pgd_prepopulate_pmd(struct mm_struct *mm, pgd_t *pgd)
239 {
240         return 1;
241 }
242
243 static void pgd_mop_up_pmds(struct mm_struct *mm, pgd_t *pgd)
244 {
245 }
246 #endif  /* CONFIG_X86_PAE */
247
248 pgd_t *pgd_alloc(struct mm_struct *mm)
249 {
250         pgd_t *pgd = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
251
252         /* so that alloc_pd can use it */
253         mm->pgd = pgd;
254         if (pgd)
255                 pgd_ctor(pgd);
256
257         if (pgd && !pgd_prepopulate_pmd(mm, pgd)) {
258                 pgd_dtor(pgd);
259                 free_page((unsigned long)pgd);
260                 pgd = NULL;
261         }
262
263         return pgd;
264 }
265
266 void pgd_free(struct mm_struct *mm, pgd_t *pgd)
267 {
268         pgd_mop_up_pmds(mm, pgd);
269         pgd_dtor(pgd);
270         free_page((unsigned long)pgd);
271 }
272 #endif