blob: e8a53552b13df147753efed1084b8a96744d667f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2002 Andi Kleen, SuSE Labs.
3 * Thanks to Ben LaHaise for precious feedback.
4 */
5
6#include <linux/config.h>
7#include <linux/mm.h>
8#include <linux/sched.h>
9#include <linux/highmem.h>
10#include <linux/module.h>
11#include <linux/slab.h>
12#include <asm/uaccess.h>
13#include <asm/processor.h>
14#include <asm/tlbflush.h>
Zachary Amsdenc9b02a22005-09-03 15:56:40 -070015#include <asm/pgalloc.h>
Dave Jonesf8af0952006-01-06 00:12:10 -080016#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18static DEFINE_SPINLOCK(cpa_lock);
19static struct list_head df_list = LIST_HEAD_INIT(df_list);
20
21
22pte_t *lookup_address(unsigned long address)
23{
24 pgd_t *pgd = pgd_offset_k(address);
25 pud_t *pud;
26 pmd_t *pmd;
27 if (pgd_none(*pgd))
28 return NULL;
29 pud = pud_offset(pgd, address);
30 if (pud_none(*pud))
31 return NULL;
32 pmd = pmd_offset(pud, address);
33 if (pmd_none(*pmd))
34 return NULL;
35 if (pmd_large(*pmd))
36 return (pte_t *)pmd;
37 return pte_offset_kernel(pmd, address);
38}
39
Dave Jonesf8af0952006-01-06 00:12:10 -080040static struct page *split_large_page(unsigned long address, pgprot_t prot,
41 pgprot_t ref_prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 int i;
44 unsigned long addr;
45 struct page *base;
46 pte_t *pbase;
47
48 spin_unlock_irq(&cpa_lock);
49 base = alloc_pages(GFP_KERNEL, 0);
50 spin_lock_irq(&cpa_lock);
51 if (!base)
52 return NULL;
53
54 address = __pa(address);
55 addr = address & LARGE_PAGE_MASK;
56 pbase = (pte_t *)page_address(base);
57 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE) {
Zachary Amsdenc9b02a22005-09-03 15:56:40 -070058 set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT,
Dave Jonesf8af0952006-01-06 00:12:10 -080059 addr == address ? prot : ref_prot));
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 }
61 return base;
62}
63
64static void flush_kernel_map(void *dummy)
65{
66 /* Could use CLFLUSH here if the CPU supports it (Hammer,P4) */
67 if (boot_cpu_data.x86_model >= 4)
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -070068 wbinvd();
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 /* Flush all to work around Errata in early athlons regarding
70 * large page flushing.
71 */
72 __flush_tlb_all();
73}
74
75static void set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
76{
77 struct page *page;
78 unsigned long flags;
79
80 set_pte_atomic(kpte, pte); /* change init_mm */
81 if (PTRS_PER_PMD > 1)
82 return;
83
84 spin_lock_irqsave(&pgd_lock, flags);
85 for (page = pgd_list; page; page = (struct page *)page->index) {
86 pgd_t *pgd;
87 pud_t *pud;
88 pmd_t *pmd;
89 pgd = (pgd_t *)page_address(page) + pgd_index(address);
90 pud = pud_offset(pgd, address);
91 pmd = pmd_offset(pud, address);
92 set_pte_atomic((pte_t *)pmd, pte);
93 }
94 spin_unlock_irqrestore(&pgd_lock, flags);
95}
96
97/*
98 * No more special protections in this 2/4MB area - revert to a
99 * large page again.
100 */
101static inline void revert_page(struct page *kpte_page, unsigned long address)
102{
Dave Jonesf8af0952006-01-06 00:12:10 -0800103 pgprot_t ref_prot;
104 pte_t *linear;
105
106 ref_prot =
107 ((address & LARGE_PAGE_MASK) < (unsigned long)&_etext)
108 ? PAGE_KERNEL_LARGE_EXEC : PAGE_KERNEL_LARGE;
109
110 linear = (pte_t *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 pmd_offset(pud_offset(pgd_offset_k(address), address), address);
112 set_pmd_pte(linear, address,
113 pfn_pte((__pa(address) & LARGE_PAGE_MASK) >> PAGE_SHIFT,
Dave Jonesf8af0952006-01-06 00:12:10 -0800114 ref_prot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117static int
118__change_page_attr(struct page *page, pgprot_t prot)
119{
120 pte_t *kpte;
121 unsigned long address;
122 struct page *kpte_page;
123
124 BUG_ON(PageHighMem(page));
125 address = (unsigned long)page_address(page);
126
127 kpte = lookup_address(address);
128 if (!kpte)
129 return -EINVAL;
130 kpte_page = virt_to_page(kpte);
131 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL)) {
132 if ((pte_val(*kpte) & _PAGE_PSE) == 0) {
133 set_pte_atomic(kpte, mk_pte(page, prot));
134 } else {
Dave Jonesf8af0952006-01-06 00:12:10 -0800135 pgprot_t ref_prot;
136 struct page *split;
137
138 ref_prot =
139 ((address & LARGE_PAGE_MASK) < (unsigned long)&_etext)
140 ? PAGE_KERNEL_EXEC : PAGE_KERNEL;
141 split = split_large_page(address, prot, ref_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (!split)
143 return -ENOMEM;
Dave Jonesf8af0952006-01-06 00:12:10 -0800144 set_pmd_pte(kpte,address,mk_pte(split, ref_prot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 kpte_page = split;
146 }
147 get_page(kpte_page);
148 } else if ((pte_val(*kpte) & _PAGE_PSE) == 0) {
149 set_pte_atomic(kpte, mk_pte(page, PAGE_KERNEL));
150 __put_page(kpte_page);
151 } else
152 BUG();
153
154 /*
155 * If the pte was reserved, it means it was created at boot
156 * time (not via split_large_page) and in turn we must not
157 * replace it with a largepage.
158 */
159 if (!PageReserved(kpte_page)) {
160 /* memleak and potential failed 2M page regeneration */
161 BUG_ON(!page_count(kpte_page));
162
163 if (cpu_has_pse && (page_count(kpte_page) == 1)) {
164 list_add(&kpte_page->lru, &df_list);
165 revert_page(kpte_page, address);
166 }
167 }
168 return 0;
169}
170
171static inline void flush_map(void)
172{
173 on_each_cpu(flush_kernel_map, NULL, 1, 1);
174}
175
176/*
177 * Change the page attributes of an page in the linear mapping.
178 *
179 * This should be used when a page is mapped with a different caching policy
180 * than write-back somewhere - some CPUs do not like it when mappings with
181 * different caching policies exist. This changes the page attributes of the
182 * in kernel linear mapping too.
183 *
184 * The caller needs to ensure that there are no conflicting mappings elsewhere.
185 * This function only deals with the kernel linear map.
186 *
187 * Caller must call global_flush_tlb() after this.
188 */
189int change_page_attr(struct page *page, int numpages, pgprot_t prot)
190{
191 int err = 0;
192 int i;
193 unsigned long flags;
194
195 spin_lock_irqsave(&cpa_lock, flags);
196 for (i = 0; i < numpages; i++, page++) {
197 err = __change_page_attr(page, prot);
198 if (err)
199 break;
200 }
201 spin_unlock_irqrestore(&cpa_lock, flags);
202 return err;
203}
204
205void global_flush_tlb(void)
206{
207 LIST_HEAD(l);
208 struct page *pg, *next;
209
210 BUG_ON(irqs_disabled());
211
212 spin_lock_irq(&cpa_lock);
213 list_splice_init(&df_list, &l);
214 spin_unlock_irq(&cpa_lock);
215 flush_map();
216 list_for_each_entry_safe(pg, next, &l, lru)
217 __free_page(pg);
218}
219
220#ifdef CONFIG_DEBUG_PAGEALLOC
221void kernel_map_pages(struct page *page, int numpages, int enable)
222{
223 if (PageHighMem(page))
224 return;
Ingo Molnarde5097c2006-01-09 15:59:21 -0800225 if (!enable)
226 mutex_debug_check_no_locks_freed(page_address(page),
227 page_address(page+numpages));
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* the return value is ignored - the calls cannot fail,
230 * large pages are disabled at boot time.
231 */
232 change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
233 /* we should perform an IPI and flush all tlbs,
234 * but that can deadlock->flush only current cpu.
235 */
236 __flush_tlb_all();
237}
238#endif
239
240EXPORT_SYMBOL(change_page_attr);
241EXPORT_SYMBOL(global_flush_tlb);