blob: e1c860800ff15963566962f5e7f0448e0755b9dd [file] [log] [blame]
Ingo Molnarb4416a12008-01-30 13:33:41 +01001/*
2 * Copyright 2002 Andi Kleen, SuSE Labs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Thanks to Ben LaHaise for precious feedback.
Ingo Molnarb4416a12008-01-30 13:33:41 +01004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/highmem.h>
7#include <linux/module.h>
Ingo Molnarb4416a12008-01-30 13:33:41 +01008#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/slab.h>
Ingo Molnarb4416a12008-01-30 13:33:41 +010010#include <linux/mm.h>
11
Ingo Molnard6ee09a2008-01-30 13:33:59 +010012void clflush_cache_range(void *addr, int size)
13{
14 int i;
15
16 for (i = 0; i < size; i += boot_cpu_data.x86_clflush_size)
17 clflush(addr+i);
18}
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/processor.h>
21#include <asm/tlbflush.h>
Ingo Molnar5e5224a2008-01-30 13:34:00 +010022#include <asm/sections.h>
Ingo Molnarb4416a12008-01-30 13:33:41 +010023#include <asm/uaccess.h>
Ingo Molnar5e5224a2008-01-30 13:34:00 +010024#include <asm/pgalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Ingo Molnarf0646e42008-01-30 13:33:43 +010026pte_t *lookup_address(unsigned long address, int *level)
Ingo Molnarb4416a12008-01-30 13:33:41 +010027{
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 pgd_t *pgd = pgd_offset_k(address);
29 pud_t *pud;
30 pmd_t *pmd;
Ingo Molnarb4416a12008-01-30 13:33:41 +010031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 if (pgd_none(*pgd))
33 return NULL;
34 pud = pud_offset(pgd, address);
Ingo Molnara5f55032008-01-30 13:33:59 +010035 if (pud_none(*pud))
Ingo Molnarb4416a12008-01-30 13:33:41 +010036 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 pmd = pmd_offset(pud, address);
Ingo Molnara5f55032008-01-30 13:33:59 +010038 if (pmd_none(*pmd))
Ingo Molnarb4416a12008-01-30 13:33:41 +010039 return NULL;
Ingo Molnarf0646e42008-01-30 13:33:43 +010040 *level = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 if (pmd_large(*pmd))
42 return (pte_t *)pmd;
Ingo Molnarf0646e42008-01-30 13:33:43 +010043 *level = 4;
Ingo Molnarb4416a12008-01-30 13:33:41 +010044
Ingo Molnara5f55032008-01-30 13:33:59 +010045 return pte_offset_kernel(pmd, address);
Ingo Molnarb4416a12008-01-30 13:33:41 +010046}
47
Ingo Molnar6faa4c52008-01-30 13:34:03 +010048static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnarb4416a12008-01-30 13:33:41 +010049{
Ingo Molnar6faa4c52008-01-30 13:34:03 +010050 /* change init_mm */
51 set_pte_atomic(kpte, pte);
52#ifdef CONFIG_X86_32
53 if (SHARED_KERNEL_PMD)
54 return;
55 {
56 struct page *page;
Ingo Molnarb4416a12008-01-30 13:33:41 +010057
Ingo Molnar6faa4c52008-01-30 13:34:03 +010058 for (page = pgd_list; page; page = (struct page *)page->index) {
59 pgd_t *pgd;
60 pud_t *pud;
61 pmd_t *pmd;
62
63 pgd = (pgd_t *)page_address(page) + pgd_index(address);
64 pud = pud_offset(pgd, address);
65 pmd = pmd_offset(pud, address);
66 set_pte_atomic((pte_t *)pmd, pte);
67 }
68 }
69#endif
70}
71
72static int split_large_page(pte_t *kpte, unsigned long address)
73{
74 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
75 gfp_t gfp_flags = GFP_KERNEL;
76 unsigned long flags;
77 unsigned long addr;
78 pte_t *pbase, *tmp;
79 struct page *base;
80 int i, level;
81
82
83#ifdef CONFIG_DEBUG_PAGEALLOC
84 gfp_flags = GFP_ATOMIC;
85#endif
86 base = alloc_pages(gfp_flags, 0);
Ingo Molnarb4416a12008-01-30 13:33:41 +010087 if (!base)
Ingo Molnar6faa4c52008-01-30 13:34:03 +010088 return -ENOMEM;
89
90 spin_lock_irqsave(&pgd_lock, flags);
91 /*
92 * Check for races, another CPU might have split this page
93 * up for us already:
94 */
95 tmp = lookup_address(address, &level);
96 if (tmp != kpte) {
97 WARN_ON_ONCE(1);
98 goto out_unlock;
99 }
Nick Piggin4fa4f532006-03-22 00:08:33 -0800100
Linus Torvaldse3ebadd2007-05-07 08:44:24 -0700101 address = __pa(address);
Ingo Molnarb4416a12008-01-30 13:33:41 +0100102 addr = address & LARGE_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 pbase = (pte_t *)page_address(base);
Ingo Molnar6faa4c52008-01-30 13:34:03 +0100104#ifdef CONFIG_X86_32
105 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
106#endif
Ingo Molnara5c625142008-01-30 13:34:00 +0100107
Ingo Molnar6faa4c52008-01-30 13:34:03 +0100108 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
109 set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
110
111 /*
112 * Install the new, split up pagetable:
113 */
114 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
115 base = NULL;
116
117out_unlock:
118 spin_unlock_irqrestore(&pgd_lock, flags);
119
120 if (base)
121 __free_pages(base, 0);
122
123 return 0;
Ingo Molnarb4416a12008-01-30 13:33:41 +0100124}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static int
Ingo Molnar0d824942008-01-30 13:34:02 +0100127__change_page_attr(unsigned long address, struct page *page, pgprot_t prot)
Ingo Molnarb4416a12008-01-30 13:33:41 +0100128{
Ingo Molnar6faa4c52008-01-30 13:34:03 +0100129 struct page *kpte_page;
130 int level, err = 0;
131 pte_t *kpte;
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200132
Ingo Molnar674d6722008-01-30 13:34:03 +0100133 BUG_ON(PageHighMem(page));
134
Ingo Molnara5c625142008-01-30 13:34:00 +0100135repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100136 kpte = lookup_address(address, &level);
Ingo Molnarb4416a12008-01-30 13:33:41 +0100137 if (!kpte)
Ingo Molnarf5a50ce2008-01-30 13:34:03 +0100138 return -EINVAL;
Ingo Molnarb4416a12008-01-30 13:33:41 +0100139
Andi Kleena3ae91b2008-01-30 13:33:50 +0100140 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200141 BUG_ON(PageLRU(kpte_page));
142 BUG_ON(PageCompound(kpte_page));
Ingo Molnar674d6722008-01-30 13:34:03 +0100143
144 /*
145 * Better fail early if someone sets the kernel text to NX.
146 * Does not cover __inittext
147 */
148 BUG_ON(address >= (unsigned long)&_text &&
149 address < (unsigned long)&_etext &&
150 (pgprot_val(prot) & _PAGE_NX));
Ingo Molnard6ee09a2008-01-30 13:33:59 +0100151
Ingo Molnar34eff1d2008-01-30 13:34:01 +0100152 if (level == 4) {
Ingo Molnar674d6722008-01-30 13:34:03 +0100153 set_pte_atomic(kpte, mk_pte(page, canon_pgprot(prot)));
Ingo Molnarb4416a12008-01-30 13:33:41 +0100154 } else {
Ingo Molnar6faa4c52008-01-30 13:34:03 +0100155 err = split_large_page(kpte, address);
156 if (!err)
157 goto repeat;
Ingo Molnarb4416a12008-01-30 13:33:41 +0100158 }
Ingo Molnar6faa4c52008-01-30 13:34:03 +0100159 return err;
Ingo Molnarb4416a12008-01-30 13:33:41 +0100160}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Ingo Molnard6ee09a2008-01-30 13:33:59 +0100162/**
163 * change_page_attr_addr - Change page table attributes in linear mapping
164 * @address: Virtual address in linear mapping.
165 * @numpages: Number of pages to change
166 * @prot: New page table attribute (PAGE_*)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 *
Ingo Molnard6ee09a2008-01-30 13:33:59 +0100168 * Change page attributes of a page in the direct mapping. This is a variant
169 * of change_page_attr() that also works on memory holes that do not have
170 * mem_map entry (pfn_valid() is false).
Ingo Molnarb4416a12008-01-30 13:33:41 +0100171 *
Ingo Molnard6ee09a2008-01-30 13:33:59 +0100172 * See change_page_attr() documentation for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 */
Ingo Molnard6ee09a2008-01-30 13:33:59 +0100174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175int change_page_attr_addr(unsigned long address, int numpages, pgprot_t prot)
176{
Ingo Molnarb4416a12008-01-30 13:33:41 +0100177 int err = 0, kernel_map = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Ingo Molnar44136712008-01-30 13:34:03 +0100179#ifdef CONFIG_X86_64
Ingo Molnarb4416a12008-01-30 13:33:41 +0100180 if (address >= __START_KERNEL_map &&
181 address < __START_KERNEL_map + KERNEL_TEXT_SIZE) {
182
Jan Beulichd01ad8d2007-05-02 19:27:10 +0200183 address = (unsigned long)__va(__pa(address));
184 kernel_map = 1;
185 }
Ingo Molnar44136712008-01-30 13:34:03 +0100186#endif
Jan Beulichd01ad8d2007-05-02 19:27:10 +0200187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 for (i = 0; i < numpages; i++, address += PAGE_SIZE) {
189 unsigned long pfn = __pa(address) >> PAGE_SHIFT;
190
Jan Beulichd01ad8d2007-05-02 19:27:10 +0200191 if (!kernel_map || pte_present(pfn_pte(0, prot))) {
Ingo Molnar0d824942008-01-30 13:34:02 +0100192 err = __change_page_attr(address, pfn_to_page(pfn), prot);
Jan Beulichd01ad8d2007-05-02 19:27:10 +0200193 if (err)
194 break;
195 }
Ingo Molnar44136712008-01-30 13:34:03 +0100196#ifdef CONFIG_X86_64
197 /*
198 * Handle kernel mapping too which aliases part of
199 * lowmem:
200 */
Linus Torvaldse3ebadd2007-05-07 08:44:24 -0700201 if (__pa(address) < KERNEL_TEXT_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 unsigned long addr2;
Andi Kleendf9928482006-09-26 10:52:37 +0200203 pgprot_t prot2;
Ingo Molnarb4416a12008-01-30 13:33:41 +0100204
Linus Torvaldse3ebadd2007-05-07 08:44:24 -0700205 addr2 = __START_KERNEL_map + __pa(address);
Andi Kleendf9928482006-09-26 10:52:37 +0200206 /* Make sure the kernel mappings stay executable */
207 prot2 = pte_pgprot(pte_mkexec(pfn_pte(0, prot)));
Ingo Molnar0d824942008-01-30 13:34:02 +0100208 err = __change_page_attr(addr2, pfn_to_page(pfn), prot2);
Ingo Molnarb4416a12008-01-30 13:33:41 +0100209 }
Ingo Molnar44136712008-01-30 13:34:03 +0100210#endif
Ingo Molnarb4416a12008-01-30 13:33:41 +0100211 }
Ingo Molnarb4416a12008-01-30 13:33:41 +0100212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return err;
214}
215
Ingo Molnard6ee09a2008-01-30 13:33:59 +0100216/**
217 * change_page_attr - Change page table attributes in the linear mapping.
218 * @page: First page to change
219 * @numpages: Number of pages to change
220 * @prot: New protection/caching type (PAGE_*)
221 *
222 * Returns 0 on success, otherwise a negated errno.
223 *
224 * This should be used when a page is mapped with a different caching policy
225 * than write-back somewhere - some CPUs do not like it when mappings with
226 * different caching policies exist. This changes the page attributes of the
227 * in kernel linear mapping too.
228 *
229 * Caller must call global_flush_tlb() later to make the changes active.
230 *
231 * The caller needs to ensure that there are no conflicting mappings elsewhere
232 * (e.g. in user space) * This function only deals with the kernel linear map.
233 *
234 * For MMIO areas without mem_map use change_page_attr_addr() instead.
235 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236int change_page_attr(struct page *page, int numpages, pgprot_t prot)
237{
238 unsigned long addr = (unsigned long)page_address(page);
Ingo Molnarb4416a12008-01-30 13:33:41 +0100239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return change_page_attr_addr(addr, numpages, prot);
241}
Ingo Molnarb4416a12008-01-30 13:33:41 +0100242EXPORT_SYMBOL(change_page_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Ingo Molnard6ee09a2008-01-30 13:33:59 +0100244static void flush_kernel_map(void *arg)
245{
246 /*
247 * Flush all to work around Errata in early athlons regarding
248 * large page flushing.
249 */
250 __flush_tlb_all();
251
252 if (boot_cpu_data.x86_model >= 4)
253 wbinvd();
254}
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256void global_flush_tlb(void)
Ingo Molnarb4416a12008-01-30 13:33:41 +0100257{
Ingo Molnard6ee09a2008-01-30 13:33:59 +0100258 BUG_ON(irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Ingo Molnard6ee09a2008-01-30 13:33:59 +0100260 on_each_cpu(flush_kernel_map, NULL, 1, 1);
Ingo Molnarb4416a12008-01-30 13:33:41 +0100261}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262EXPORT_SYMBOL(global_flush_tlb);
Ingo Molnarace63e32008-01-30 13:34:03 +0100263
264#ifdef CONFIG_DEBUG_PAGEALLOC
265void kernel_map_pages(struct page *page, int numpages, int enable)
266{
267 if (PageHighMem(page))
268 return;
269 if (!enable) {
270 debug_check_no_locks_freed(page_address(page),
271 numpages * PAGE_SIZE);
272 }
273
274 /*
275 * If page allocator is not up yet then do not call c_p_a():
276 */
277 if (!debug_pagealloc_enabled)
278 return;
279
280 /*
281 * the return value is ignored - the calls cannot fail,
282 * large pages are disabled at boot time.
283 */
284 change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
285
286 /*
287 * we should perform an IPI and flush all tlbs,
288 * but that can deadlock->flush only current cpu.
289 */
290 __flush_tlb_all();
291}
292#endif