1 /* pgalloc.c: page directory & page table allocation
3 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/sched.h>
13 #include <linux/gfp.h>
15 #include <linux/highmem.h>
16 #include <linux/quicklist.h>
17 #include <asm/pgalloc.h>
19 #include <asm/cacheflush.h>
21 pgd_t swapper_pg_dir
[PTRS_PER_PGD
] __attribute__((aligned(PAGE_SIZE
)));
23 pte_t
*pte_alloc_one_kernel(struct mm_struct
*mm
, unsigned long address
)
25 pte_t
*pte
= (pte_t
*)__get_free_page(GFP_KERNEL
);
31 pgtable_t
pte_alloc_one(struct mm_struct
*mm
, unsigned long address
)
36 page
= alloc_pages(GFP_KERNEL
|__GFP_HIGHMEM
, 0);
38 page
= alloc_pages(GFP_KERNEL
, 0);
44 if (!pgtable_page_ctor(page
)) {
48 flush_dcache_page(page
);
52 void __set_pmd(pmd_t
*pmdptr
, unsigned long pmd
)
54 unsigned long *__ste_p
= pmdptr
->ste
;
58 memset(__ste_p
, 0, PME_SIZE
);
61 BUG_ON(pmd
& (0x3f00 | xAMPRx_SS
| 0xe));
63 for (loop
= PME_SIZE
; loop
> 0; loop
-= 4) {
69 frv_dcache_writeback((unsigned long) pmdptr
, (unsigned long) (pmdptr
+ 1));
73 * List of all pgd's needed for non-PAE so it can invalidate entries
74 * in both cached and uncached pgd's; not needed for PAE since the
75 * kernel pmd is shared. If PAE were not to share the pmd a similar
76 * tactic would be needed. This is essentially codepath-based locking
77 * against pageattr.c; it is the unique case in which a valid change
78 * of kernel pagetables can't be lazily synchronized by vmalloc faults.
79 * vmalloc faults work because attached pagetables are never freed.
80 * If the locking proves to be non-performant, a ticketing scheme with
81 * checks at dup_mmap(), exec(), and other mmlist addition points
82 * could be used. The locking scheme was chosen on the basis of
83 * manfred's recommendations and having no core impact whatsoever.
86 DEFINE_SPINLOCK(pgd_lock
);
87 struct page
*pgd_list
;
89 static inline void pgd_list_add(pgd_t
*pgd
)
91 struct page
*page
= virt_to_page(pgd
);
92 page
->index
= (unsigned long) pgd_list
;
94 set_page_private(pgd_list
, (unsigned long) &page
->index
);
96 set_page_private(page
, (unsigned long)&pgd_list
);
99 static inline void pgd_list_del(pgd_t
*pgd
)
101 struct page
*next
, **pprev
, *page
= virt_to_page(pgd
);
102 next
= (struct page
*) page
->index
;
103 pprev
= (struct page
**) page_private(page
);
106 set_page_private(next
, (unsigned long) pprev
);
109 void pgd_ctor(void *pgd
)
113 if (PTRS_PER_PMD
== 1)
114 spin_lock_irqsave(&pgd_lock
, flags
);
116 memcpy((pgd_t
*) pgd
+ USER_PGDS_IN_LAST_PML4
,
117 swapper_pg_dir
+ USER_PGDS_IN_LAST_PML4
,
118 (PTRS_PER_PGD
- USER_PGDS_IN_LAST_PML4
) * sizeof(pgd_t
));
120 if (PTRS_PER_PMD
> 1)
124 spin_unlock_irqrestore(&pgd_lock
, flags
);
125 memset(pgd
, 0, USER_PGDS_IN_LAST_PML4
* sizeof(pgd_t
));
128 /* never called when PTRS_PER_PMD > 1 */
129 void pgd_dtor(void *pgd
)
131 unsigned long flags
; /* can be called from interrupt context */
133 spin_lock_irqsave(&pgd_lock
, flags
);
135 spin_unlock_irqrestore(&pgd_lock
, flags
);
138 pgd_t
*pgd_alloc(struct mm_struct
*mm
)
140 return quicklist_alloc(0, GFP_KERNEL
, pgd_ctor
);
143 void pgd_free(struct mm_struct
*mm
, pgd_t
*pgd
)
145 /* in the non-PAE case, clear_page_tables() clears user pgd entries */
146 quicklist_free(0, pgd_dtor
, pgd
);
149 void __init
pgtable_cache_init(void)
153 void check_pgt_cache(void)
155 quicklist_trim(0, pgd_dtor
, 25, 16);