blob: bd98768d8764f52b0693a4413d32003bd911daed [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/i386/mm/pgtable.c
3 */
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/sched.h>
6#include <linux/kernel.h>
7#include <linux/errno.h>
8#include <linux/mm.h>
9#include <linux/swap.h>
10#include <linux/smp.h>
11#include <linux/highmem.h>
12#include <linux/slab.h>
13#include <linux/pagemap.h>
14#include <linux/spinlock.h>
15
16#include <asm/system.h>
17#include <asm/pgtable.h>
18#include <asm/pgalloc.h>
19#include <asm/fixmap.h>
20#include <asm/e820.h>
21#include <asm/tlb.h>
22#include <asm/tlbflush.h>
23
24void show_mem(void)
25{
26 int total = 0, reserved = 0;
27 int shared = 0, cached = 0;
28 int highmem = 0;
29 struct page *page;
30 pg_data_t *pgdat;
31 unsigned long i;
Dave Hansen208d54e2005-10-29 18:16:52 -070032 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Christophe Lucasf90e7182005-06-25 14:59:24 -070034 printk(KERN_INFO "Mem-info:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 show_free_areas();
Christophe Lucasf90e7182005-06-25 14:59:24 -070036 printk(KERN_INFO "Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
KAMEZAWA Hiroyukiec936fc2006-03-27 01:15:59 -080037 for_each_online_pgdat(pgdat) {
Dave Hansen208d54e2005-10-29 18:16:52 -070038 pgdat_resize_lock(pgdat, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 for (i = 0; i < pgdat->node_spanned_pages; ++i) {
Dave Hansen408fde82005-06-23 00:07:37 -070040 page = pgdat_page_nr(pgdat, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 total++;
42 if (PageHighMem(page))
43 highmem++;
44 if (PageReserved(page))
45 reserved++;
46 else if (PageSwapCache(page))
47 cached++;
48 else if (page_count(page))
49 shared += page_count(page) - 1;
50 }
Dave Hansen208d54e2005-10-29 18:16:52 -070051 pgdat_resize_unlock(pgdat, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 }
Christophe Lucasf90e7182005-06-25 14:59:24 -070053 printk(KERN_INFO "%d pages of RAM\n", total);
54 printk(KERN_INFO "%d pages of HIGHMEM\n", highmem);
55 printk(KERN_INFO "%d reserved pages\n", reserved);
56 printk(KERN_INFO "%d pages shared\n", shared);
57 printk(KERN_INFO "%d pages swap cached\n", cached);
Martin J. Bligh6f4e1e52005-06-23 00:08:08 -070058
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -070059 printk(KERN_INFO "%lu pages dirty\n", global_page_state(NR_FILE_DIRTY));
Christoph Lameterce866b32006-06-30 01:55:40 -070060 printk(KERN_INFO "%lu pages writeback\n",
61 global_page_state(NR_WRITEBACK));
Christoph Lameter65ba55f2006-06-30 01:55:34 -070062 printk(KERN_INFO "%lu pages mapped\n", global_page_state(NR_FILE_MAPPED));
Christoph Lameter9a865ff2006-06-30 01:55:38 -070063 printk(KERN_INFO "%lu pages slab\n", global_page_state(NR_SLAB));
Christoph Lameterdf849a12006-06-30 01:55:38 -070064 printk(KERN_INFO "%lu pages pagetables\n",
65 global_page_state(NR_PAGETABLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
68/*
69 * Associate a virtual page frame with a given physical page frame
70 * and protection flags for that frame.
71 */
72static void set_pte_pfn(unsigned long vaddr, unsigned long pfn, pgprot_t flags)
73{
74 pgd_t *pgd;
75 pud_t *pud;
76 pmd_t *pmd;
77 pte_t *pte;
78
79 pgd = swapper_pg_dir + pgd_index(vaddr);
80 if (pgd_none(*pgd)) {
81 BUG();
82 return;
83 }
84 pud = pud_offset(pgd, vaddr);
85 if (pud_none(*pud)) {
86 BUG();
87 return;
88 }
89 pmd = pmd_offset(pud, vaddr);
90 if (pmd_none(*pmd)) {
91 BUG();
92 return;
93 }
94 pte = pte_offset_kernel(pmd, vaddr);
95 /* <pfn,flags> stored as-is, to permit clearing entries */
96 set_pte(pte, pfn_pte(pfn, flags));
97
98 /*
99 * It's enough to flush this one mapping.
100 * (PGE mappings get flushed as well)
101 */
102 __flush_tlb_one(vaddr);
103}
104
105/*
106 * Associate a large virtual page frame with a given physical page frame
107 * and protection flags for that frame. pfn is for the base of the page,
108 * vaddr is what the page gets mapped to - both must be properly aligned.
109 * The pmd must already be instantiated. Assumes PAE mode.
110 */
111void set_pmd_pfn(unsigned long vaddr, unsigned long pfn, pgprot_t flags)
112{
113 pgd_t *pgd;
114 pud_t *pud;
115 pmd_t *pmd;
116
117 if (vaddr & (PMD_SIZE-1)) { /* vaddr is misaligned */
Christophe Lucasf90e7182005-06-25 14:59:24 -0700118 printk(KERN_WARNING "set_pmd_pfn: vaddr misaligned\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return; /* BUG(); */
120 }
121 if (pfn & (PTRS_PER_PTE-1)) { /* pfn is misaligned */
Christophe Lucasf90e7182005-06-25 14:59:24 -0700122 printk(KERN_WARNING "set_pmd_pfn: pfn misaligned\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return; /* BUG(); */
124 }
125 pgd = swapper_pg_dir + pgd_index(vaddr);
126 if (pgd_none(*pgd)) {
Christophe Lucasf90e7182005-06-25 14:59:24 -0700127 printk(KERN_WARNING "set_pmd_pfn: pgd_none\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return; /* BUG(); */
129 }
130 pud = pud_offset(pgd, vaddr);
131 pmd = pmd_offset(pud, vaddr);
132 set_pmd(pmd, pfn_pmd(pfn, flags));
133 /*
134 * It's enough to flush this one mapping.
135 * (PGE mappings get flushed as well)
136 */
137 __flush_tlb_one(vaddr);
138}
139
140void __set_fixmap (enum fixed_addresses idx, unsigned long phys, pgprot_t flags)
141{
142 unsigned long address = __fix_to_virt(idx);
143
144 if (idx >= __end_of_fixed_addresses) {
145 BUG();
146 return;
147 }
148 set_pte_pfn(address, phys >> PAGE_SHIFT, flags);
149}
150
151pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
152{
153 return (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
154}
155
156struct page *pte_alloc_one(struct mm_struct *mm, unsigned long address)
157{
158 struct page *pte;
159
160#ifdef CONFIG_HIGHPTE
161 pte = alloc_pages(GFP_KERNEL|__GFP_HIGHMEM|__GFP_REPEAT|__GFP_ZERO, 0);
162#else
163 pte = alloc_pages(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO, 0);
164#endif
165 return pte;
166}
167
168void pmd_ctor(void *pmd, kmem_cache_t *cache, unsigned long flags)
169{
170 memset(pmd, 0, PTRS_PER_PMD*sizeof(pmd_t));
171}
172
173/*
174 * List of all pgd's needed for non-PAE so it can invalidate entries
175 * in both cached and uncached pgd's; not needed for PAE since the
176 * kernel pmd is shared. If PAE were not to share the pmd a similar
177 * tactic would be needed. This is essentially codepath-based locking
178 * against pageattr.c; it is the unique case in which a valid change
179 * of kernel pagetables can't be lazily synchronized by vmalloc faults.
180 * vmalloc faults work because attached pagetables are never freed.
181 * The locking scheme was chosen on the basis of manfred's
182 * recommendations and having no core impact whatsoever.
183 * -- wli
184 */
185DEFINE_SPINLOCK(pgd_lock);
186struct page *pgd_list;
187
188static inline void pgd_list_add(pgd_t *pgd)
189{
190 struct page *page = virt_to_page(pgd);
191 page->index = (unsigned long)pgd_list;
192 if (pgd_list)
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700193 set_page_private(pgd_list, (unsigned long)&page->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 pgd_list = page;
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700195 set_page_private(page, (unsigned long)&pgd_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198static inline void pgd_list_del(pgd_t *pgd)
199{
200 struct page *next, **pprev, *page = virt_to_page(pgd);
201 next = (struct page *)page->index;
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700202 pprev = (struct page **)page_private(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 *pprev = next;
204 if (next)
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700205 set_page_private(next, (unsigned long)pprev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208void pgd_ctor(void *pgd, kmem_cache_t *cache, unsigned long unused)
209{
210 unsigned long flags;
211
Zachary Amsdend7271b12005-09-03 15:56:50 -0700212 if (PTRS_PER_PMD == 1) {
213 memset(pgd, 0, USER_PTRS_PER_PGD*sizeof(pgd_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 spin_lock_irqsave(&pgd_lock, flags);
Zachary Amsdend7271b12005-09-03 15:56:50 -0700215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Zachary Amsdend7271b12005-09-03 15:56:50 -0700217 clone_pgd_range((pgd_t *)pgd + USER_PTRS_PER_PGD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 swapper_pg_dir + USER_PTRS_PER_PGD,
Zachary Amsdend7271b12005-09-03 15:56:50 -0700219 KERNEL_PGD_PTRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (PTRS_PER_PMD > 1)
221 return;
222
223 pgd_list_add(pgd);
224 spin_unlock_irqrestore(&pgd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227/* never called when PTRS_PER_PMD > 1 */
228void pgd_dtor(void *pgd, kmem_cache_t *cache, unsigned long unused)
229{
230 unsigned long flags; /* can be called from interrupt context */
231
232 spin_lock_irqsave(&pgd_lock, flags);
233 pgd_list_del(pgd);
234 spin_unlock_irqrestore(&pgd_lock, flags);
235}
236
237pgd_t *pgd_alloc(struct mm_struct *mm)
238{
239 int i;
240 pgd_t *pgd = kmem_cache_alloc(pgd_cache, GFP_KERNEL);
241
242 if (PTRS_PER_PMD == 1 || !pgd)
243 return pgd;
244
245 for (i = 0; i < USER_PTRS_PER_PGD; ++i) {
246 pmd_t *pmd = kmem_cache_alloc(pmd_cache, GFP_KERNEL);
247 if (!pmd)
248 goto out_oom;
249 set_pgd(&pgd[i], __pgd(1 + __pa(pmd)));
250 }
251 return pgd;
252
253out_oom:
254 for (i--; i >= 0; i--)
255 kmem_cache_free(pmd_cache, (void *)__va(pgd_val(pgd[i])-1));
256 kmem_cache_free(pgd_cache, pgd);
257 return NULL;
258}
259
260void pgd_free(pgd_t *pgd)
261{
262 int i;
263
264 /* in the PAE case user pgd entries are overwritten before usage */
265 if (PTRS_PER_PMD > 1)
266 for (i = 0; i < USER_PTRS_PER_PGD; ++i)
267 kmem_cache_free(pmd_cache, (void *)__va(pgd_val(pgd[i])-1));
Hugh Dickinse0da3822005-04-19 13:29:15 -0700268 /* in the non-PAE case, free_pgtables() clears user pgd entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 kmem_cache_free(pgd_cache, pgd);
270}