blob: 029fb07b3f0368c023c3f149b4a43401aa06b107 [file] [log] [blame]
Ingo Molnar9f4c8152008-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 Molnar9f4c8152008-01-30 13:33:41 +01004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/highmem.h>
Ingo Molnar81922062008-01-30 13:34:04 +01006#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
Ingo Molnar9f4c8152008-01-30 13:33:41 +01008#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/slab.h>
Ingo Molnar9f4c8152008-01-30 13:33:41 +010010#include <linux/mm.h>
11
Thomas Gleixner950f9d92008-01-30 13:34:06 +010012#include <asm/e820.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/processor.h>
14#include <asm/tlbflush.h>
Dave Jonesf8af0952006-01-06 00:12:10 -080015#include <asm/sections.h>
Ingo Molnar9f4c8152008-01-30 13:33:41 +010016#include <asm/uaccess.h>
17#include <asm/pgalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Ingo Molnar9df84992008-02-04 16:48:09 +010019/*
20 * The current flushing context - we pass it instead of 5 arguments:
21 */
Thomas Gleixner72e458d2008-02-04 16:48:07 +010022struct cpa_data {
23 unsigned long vaddr;
Thomas Gleixner72e458d2008-02-04 16:48:07 +010024 pgprot_t mask_set;
25 pgprot_t mask_clr;
Thomas Gleixner65e074d2008-02-04 16:48:07 +010026 int numpages;
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +010027 int flushtlb;
Thomas Gleixner72e458d2008-02-04 16:48:07 +010028};
29
Thomas Gleixner65e074d2008-02-04 16:48:07 +010030enum {
31 CPA_NO_SPLIT = 0,
32 CPA_SPLIT,
33};
34
Arjan van de Vened724be2008-01-30 13:34:04 +010035static inline int
36within(unsigned long addr, unsigned long start, unsigned long end)
Ingo Molnar687c4822008-01-30 13:34:04 +010037{
Arjan van de Vened724be2008-01-30 13:34:04 +010038 return addr >= start && addr < end;
39}
40
41/*
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010042 * Flushing functions
43 */
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010044
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010045/**
46 * clflush_cache_range - flush a cache range with clflush
47 * @addr: virtual start address
48 * @size: number of bytes to flush
49 *
50 * clflush is an unordered instruction which needs fencing with mfence
51 * to avoid ordering issues.
52 */
Ingo Molnar4c61afc2008-01-30 13:34:09 +010053void clflush_cache_range(void *vaddr, unsigned int size)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010054{
Ingo Molnar4c61afc2008-01-30 13:34:09 +010055 void *vend = vaddr + size - 1;
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010056
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010057 mb();
Ingo Molnar4c61afc2008-01-30 13:34:09 +010058
59 for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
60 clflush(vaddr);
61 /*
62 * Flush any possible final partial cacheline:
63 */
64 clflush(vend);
65
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010066 mb();
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010067}
68
Thomas Gleixneraf1e6842008-01-30 13:34:08 +010069static void __cpa_flush_all(void *arg)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010070{
Andi Kleen6bb83832008-02-04 16:48:06 +010071 unsigned long cache = (unsigned long)arg;
72
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010073 /*
74 * Flush all to work around Errata in early athlons regarding
75 * large page flushing.
76 */
77 __flush_tlb_all();
78
Andi Kleen6bb83832008-02-04 16:48:06 +010079 if (cache && boot_cpu_data.x86_model >= 4)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010080 wbinvd();
81}
82
Andi Kleen6bb83832008-02-04 16:48:06 +010083static void cpa_flush_all(unsigned long cache)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010084{
85 BUG_ON(irqs_disabled());
86
Andi Kleen6bb83832008-02-04 16:48:06 +010087 on_each_cpu(__cpa_flush_all, (void *) cache, 1, 1);
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010088}
89
Thomas Gleixner57a6a462008-01-30 13:34:08 +010090static void __cpa_flush_range(void *arg)
91{
Thomas Gleixner57a6a462008-01-30 13:34:08 +010092 /*
93 * We could optimize that further and do individual per page
94 * tlb invalidates for a low number of pages. Caveat: we must
95 * flush the high aliases on 64bit as well.
96 */
97 __flush_tlb_all();
Thomas Gleixner57a6a462008-01-30 13:34:08 +010098}
99
Andi Kleen6bb83832008-02-04 16:48:06 +0100100static void cpa_flush_range(unsigned long start, int numpages, int cache)
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100101{
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100102 unsigned int i, level;
103 unsigned long addr;
104
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100105 BUG_ON(irqs_disabled());
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100106 WARN_ON(PAGE_ALIGN(start) != start);
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100107
Thomas Gleixner3b233e52008-01-30 13:34:08 +0100108 on_each_cpu(__cpa_flush_range, NULL, 1, 1);
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100109
Andi Kleen6bb83832008-02-04 16:48:06 +0100110 if (!cache)
111 return;
112
Thomas Gleixner3b233e52008-01-30 13:34:08 +0100113 /*
114 * We only need to flush on one CPU,
115 * clflush is a MESI-coherent instruction that
116 * will cause all other CPUs to flush the same
117 * cachelines:
118 */
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100119 for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
120 pte_t *pte = lookup_address(addr, &level);
121
122 /*
123 * Only flush present addresses:
124 */
Thomas Gleixner7bfb72e2008-02-04 16:48:08 +0100125 if (pte && (pte_val(*pte) & _PAGE_PRESENT))
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100126 clflush_cache_range((void *) addr, PAGE_SIZE);
127 }
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100128}
129
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100130#define HIGH_MAP_START __START_KERNEL_map
131#define HIGH_MAP_END (__START_KERNEL_map + KERNEL_TEXT_SIZE)
132
133
134/*
135 * Converts a virtual address to a X86-64 highmap address
136 */
137static unsigned long virt_to_highmap(void *address)
138{
139#ifdef CONFIG_X86_64
140 return __pa((unsigned long)address) + HIGH_MAP_START - phys_base;
141#else
142 return (unsigned long)address;
143#endif
144}
145
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100146/*
Arjan van de Vened724be2008-01-30 13:34:04 +0100147 * Certain areas of memory on x86 require very specific protection flags,
148 * for example the BIOS area or kernel text. Callers don't always get this
149 * right (again, ioremap() on BIOS memory is not uncommon) so this function
150 * checks and fixes these known static required protection bits.
151 */
152static inline pgprot_t static_protections(pgprot_t prot, unsigned long address)
153{
154 pgprot_t forbidden = __pgprot(0);
155
Ingo Molnar687c4822008-01-30 13:34:04 +0100156 /*
Arjan van de Vened724be2008-01-30 13:34:04 +0100157 * The BIOS area between 640k and 1Mb needs to be executable for
158 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
Ingo Molnar687c4822008-01-30 13:34:04 +0100159 */
Arjan van de Vened724be2008-01-30 13:34:04 +0100160 if (within(__pa(address), BIOS_BEGIN, BIOS_END))
161 pgprot_val(forbidden) |= _PAGE_NX;
162
163 /*
164 * The kernel text needs to be executable for obvious reasons
165 * Does not cover __inittext since that is gone later on
166 */
167 if (within(address, (unsigned long)_text, (unsigned long)_etext))
168 pgprot_val(forbidden) |= _PAGE_NX;
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100169 /*
170 * Do the same for the x86-64 high kernel mapping
171 */
172 if (within(address, virt_to_highmap(_text), virt_to_highmap(_etext)))
173 pgprot_val(forbidden) |= _PAGE_NX;
174
Arjan van de Vened724be2008-01-30 13:34:04 +0100175
176#ifdef CONFIG_DEBUG_RODATA
177 /* The .rodata section needs to be read-only */
178 if (within(address, (unsigned long)__start_rodata,
179 (unsigned long)__end_rodata))
180 pgprot_val(forbidden) |= _PAGE_RW;
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100181 /*
182 * Do the same for the x86-64 high kernel mapping
183 */
184 if (within(address, virt_to_highmap(__start_rodata),
185 virt_to_highmap(__end_rodata)))
186 pgprot_val(forbidden) |= _PAGE_RW;
Arjan van de Vened724be2008-01-30 13:34:04 +0100187#endif
188
189 prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
Ingo Molnar687c4822008-01-30 13:34:04 +0100190
191 return prot;
192}
193
Thomas Gleixner9a14aef2008-02-04 16:48:07 +0100194/*
195 * Lookup the page table entry for a virtual address. Return a pointer
196 * to the entry and the level of the mapping.
197 *
198 * Note: We return pud and pmd either when the entry is marked large
199 * or when the present bit is not set. Otherwise we would return a
200 * pointer to a nonexisting mapping.
201 */
Ingo Molnarf0646e42008-01-30 13:33:43 +0100202pte_t *lookup_address(unsigned long address, int *level)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100203{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 pgd_t *pgd = pgd_offset_k(address);
205 pud_t *pud;
206 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100207
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100208 *level = PG_LEVEL_NONE;
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (pgd_none(*pgd))
211 return NULL;
Ingo Molnar9df84992008-02-04 16:48:09 +0100212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 pud = pud_offset(pgd, address);
214 if (pud_none(*pud))
215 return NULL;
Andi Kleenc2f71ee2008-02-04 16:48:09 +0100216
217 *level = PG_LEVEL_1G;
218 if (pud_large(*pud) || !pud_present(*pud))
219 return (pte_t *)pud;
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 pmd = pmd_offset(pud, address);
222 if (pmd_none(*pmd))
223 return NULL;
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100224
225 *level = PG_LEVEL_2M;
Thomas Gleixner9a14aef2008-02-04 16:48:07 +0100226 if (pmd_large(*pmd) || !pmd_present(*pmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return (pte_t *)pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100229 *level = PG_LEVEL_4K;
Ingo Molnar9df84992008-02-04 16:48:09 +0100230
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100231 return pte_offset_kernel(pmd, address);
232}
233
Ingo Molnar9df84992008-02-04 16:48:09 +0100234/*
235 * Set the new pmd in all the pgds we know about:
236 */
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100237static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100238{
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100239 /* change init_mm */
240 set_pte_atomic(kpte, pte);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100241#ifdef CONFIG_X86_32
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100242 if (!SHARED_KERNEL_PMD) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100243 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Jeremy Fitzhardingee3ed9102008-01-30 13:34:11 +0100245 list_for_each_entry(page, &pgd_list, lru) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100246 pgd_t *pgd;
247 pud_t *pud;
248 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100249
Ingo Molnar44af6c42008-01-30 13:34:03 +0100250 pgd = (pgd_t *)page_address(page) + pgd_index(address);
251 pud = pud_offset(pgd, address);
252 pmd = pmd_offset(pud, address);
253 set_pte_atomic((pte_t *)pmd, pte);
254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
Ingo Molnar44af6c42008-01-30 13:34:03 +0100256#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Ingo Molnar9df84992008-02-04 16:48:09 +0100259static int
260try_preserve_large_page(pte_t *kpte, unsigned long address,
261 struct cpa_data *cpa)
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100262{
263 unsigned long nextpage_addr, numpages, pmask, psize, flags;
264 pte_t new_pte, old_pte, *tmp;
265 pgprot_t old_prot, new_prot;
266 int level, res = CPA_SPLIT;
267
Ingo Molnar34508f62008-02-04 16:48:07 +0100268 /*
269 * An Athlon 64 X2 showed hard hangs if we tried to preserve
270 * largepages and changed the PSE entry from RW to RO.
271 *
272 * As AMD CPUs have a long series of erratas in this area,
273 * (and none of the known ones seem to explain this hang),
274 * disable this code until the hang can be debugged:
275 */
276 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
277 return res;
278
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100279 spin_lock_irqsave(&pgd_lock, flags);
280 /*
281 * Check for races, another CPU might have split this page
282 * up already:
283 */
284 tmp = lookup_address(address, &level);
285 if (tmp != kpte)
286 goto out_unlock;
287
288 switch (level) {
289 case PG_LEVEL_2M:
Andi Kleen31422c52008-02-04 16:48:08 +0100290 psize = PMD_PAGE_SIZE;
291 pmask = PMD_PAGE_MASK;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100292 break;
Andi Kleenf07333f2008-02-04 16:48:09 +0100293#ifdef CONFIG_X86_64
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100294 case PG_LEVEL_1G:
Andi Kleenf07333f2008-02-04 16:48:09 +0100295 psize = PMD_PAGE_SIZE;
296 pmask = PMD_PAGE_MASK;
297 break;
298#endif
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100299 default:
300 res = -EINVAL;
301 goto out_unlock;
302 }
303
304 /*
305 * Calculate the number of pages, which fit into this large
306 * page starting at address:
307 */
308 nextpage_addr = (address + psize) & pmask;
309 numpages = (nextpage_addr - address) >> PAGE_SHIFT;
310 if (numpages < cpa->numpages)
311 cpa->numpages = numpages;
312
313 /*
314 * We are safe now. Check whether the new pgprot is the same:
315 */
316 old_pte = *kpte;
317 old_prot = new_prot = pte_pgprot(old_pte);
318
319 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
320 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
321 new_prot = static_protections(new_prot, address);
322
323 /*
324 * If there are no changes, return. maxpages has been updated
325 * above:
326 */
327 if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
328 res = CPA_NO_SPLIT;
329 goto out_unlock;
330 }
331
332 /*
333 * We need to change the attributes. Check, whether we can
334 * change the large page in one go. We request a split, when
335 * the address is not aligned and the number of pages is
336 * smaller than the number of pages in the large page. Note
337 * that we limited the number of possible pages already to
338 * the number of pages in the large page.
339 */
340 if (address == (nextpage_addr - psize) && cpa->numpages == numpages) {
341 /*
342 * The address is aligned and the number of pages
343 * covers the full page.
344 */
345 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
346 __set_pmd_pte(kpte, address, new_pte);
347 cpa->flushtlb = 1;
348 res = CPA_NO_SPLIT;
349 }
350
351out_unlock:
352 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnar9df84992008-02-04 16:48:09 +0100353
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100354 return res;
355}
356
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100357static int split_large_page(pte_t *kpte, unsigned long address)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100358{
Andi Kleenf07333f2008-02-04 16:48:09 +0100359 unsigned long flags, addr, pfn, pfninc = 1;
Ingo Molnar9df84992008-02-04 16:48:09 +0100360 gfp_t gfp_flags = GFP_KERNEL;
Ingo Molnar86f03982008-01-30 13:34:09 +0100361 unsigned int i, level;
Ingo Molnar9df84992008-02-04 16:48:09 +0100362 pte_t *pbase, *tmp;
363 pgprot_t ref_prot;
364 struct page *base;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100365
Ingo Molnar12d6f212008-01-30 13:33:58 +0100366#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnar86f03982008-01-30 13:34:09 +0100367 gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Ingo Molnar12d6f212008-01-30 13:33:58 +0100368#endif
369 base = alloc_pages(gfp_flags, 0);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100370 if (!base)
371 return -ENOMEM;
372
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100373 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100374 /*
375 * Check for races, another CPU might have split this page
376 * up for us already:
377 */
378 tmp = lookup_address(address, &level);
Ingo Molnar6ce9fc12008-02-04 16:48:08 +0100379 if (tmp != kpte)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100380 goto out_unlock;
381
382 address = __pa(address);
Andi Kleen31422c52008-02-04 16:48:08 +0100383 addr = address & PMD_PAGE_MASK;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100384 pbase = (pte_t *)page_address(base);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100385#ifdef CONFIG_X86_32
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100386 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
Ingo Molnar44af6c42008-01-30 13:34:03 +0100387#endif
Thomas Gleixner07cf89c2008-02-04 16:48:08 +0100388 ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100389
Andi Kleenf07333f2008-02-04 16:48:09 +0100390#ifdef CONFIG_X86_64
391 if (level == PG_LEVEL_1G) {
392 pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
393 pgprot_val(ref_prot) |= _PAGE_PSE;
394 addr &= PUD_PAGE_MASK;
395 }
396#endif
397
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100398 /*
399 * Get the target pfn from the original entry:
400 */
401 pfn = pte_pfn(*kpte);
Andi Kleenf07333f2008-02-04 16:48:09 +0100402 for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100403 set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100404
405 /*
Thomas Gleixner07cf89c2008-02-04 16:48:08 +0100406 * Install the new, split up pagetable. Important details here:
Huang, Ying4c881ca2008-01-30 13:34:04 +0100407 *
408 * On Intel the NX bit of all levels must be cleared to make a
409 * page executable. See section 4.13.2 of Intel 64 and IA-32
410 * Architectures Software Developer's Manual).
Thomas Gleixner07cf89c2008-02-04 16:48:08 +0100411 *
412 * Mark the entry present. The current mapping might be
413 * set to not present, which we preserved above.
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100414 */
Huang, Ying4c881ca2008-01-30 13:34:04 +0100415 ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
Thomas Gleixner07cf89c2008-02-04 16:48:08 +0100416 pgprot_val(ref_prot) |= _PAGE_PRESENT;
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100417 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100418 base = NULL;
419
420out_unlock:
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100421 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100422
423 if (base)
424 __free_pages(base, 0);
425
426 return 0;
427}
428
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100429static int __change_page_attr(unsigned long address, struct cpa_data *cpa)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100430{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 struct page *kpte_page;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100432 int level, res;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100433 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100435repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100436 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (!kpte)
438 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200441 BUG_ON(PageLRU(kpte_page));
442 BUG_ON(PageCompound(kpte_page));
443
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100444 if (level == PG_LEVEL_4K) {
Ingo Molnar86f03982008-01-30 13:34:09 +0100445 pte_t new_pte, old_pte = *kpte;
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100446 pgprot_t new_prot = pte_pgprot(old_pte);
447
448 if(!pte_val(old_pte)) {
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100449 printk(KERN_WARNING "CPA: called for zero pte. "
450 "vaddr = %lx cpa->vaddr = %lx\n", address,
451 cpa->vaddr);
452 WARN_ON(1);
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100453 return -EINVAL;
454 }
Thomas Gleixnera72a08a2008-01-30 13:34:07 +0100455
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100456 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
457 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
Ingo Molnar86f03982008-01-30 13:34:09 +0100458
459 new_prot = static_protections(new_prot, address);
460
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100461 /*
462 * We need to keep the pfn from the existing PTE,
463 * after all we're only going to change it's attributes
464 * not the memory it points to
465 */
466 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100467
468 /*
469 * Do we really change anything ?
470 */
471 if (pte_val(old_pte) != pte_val(new_pte)) {
472 set_pte_atomic(kpte, new_pte);
473 cpa->flushtlb = 1;
474 }
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100475 cpa->numpages = 1;
476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100478
479 /*
480 * Check, whether we can keep the large page intact
481 * and just change the pte:
482 */
483 res = try_preserve_large_page(kpte, address, cpa);
484 if (res < 0)
485 return res;
486
487 /*
488 * When the range fits into the existing large page,
489 * return. cp->numpages and cpa->tlbflush have been updated in
490 * try_large_page:
491 */
492 if (res == CPA_NO_SPLIT)
493 return 0;
494
495 /*
496 * We have to split the large page:
497 */
498 res = split_large_page(kpte, address);
499 if (res)
500 return res;
501 cpa->flushtlb = 1;
502 goto repeat;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100503}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Ingo Molnar44af6c42008-01-30 13:34:03 +0100505/**
506 * change_page_attr_addr - Change page table attributes in linear mapping
507 * @address: Virtual address in linear mapping.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100508 * @prot: New page table attribute (PAGE_*)
509 *
510 * Change page attributes of a page in the direct mapping. This is a variant
511 * of change_page_attr() that also works on memory holes that do not have
512 * mem_map entry (pfn_valid() is false).
513 *
514 * See change_page_attr() documentation for more details.
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100515 *
516 * Modules and drivers should use the set_memory_* APIs instead.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100517 */
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100518static int change_page_attr_addr(struct cpa_data *cpa)
Ingo Molnar44af6c42008-01-30 13:34:03 +0100519{
Thomas Gleixner08797502008-01-30 13:34:09 +0100520 int err;
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100521 unsigned long address = cpa->vaddr;
Ingo Molnar44af6c42008-01-30 13:34:03 +0100522
Arjan van de Ven488fd992008-01-30 13:34:07 +0100523#ifdef CONFIG_X86_64
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100524 unsigned long phys_addr = __pa(address);
525
Arjan van de Ven488fd992008-01-30 13:34:07 +0100526 /*
Thomas Gleixner08797502008-01-30 13:34:09 +0100527 * If we are inside the high mapped kernel range, then we
528 * fixup the low mapping first. __va() returns the virtual
529 * address in the linear mapping:
Arjan van de Ven488fd992008-01-30 13:34:07 +0100530 */
Thomas Gleixner08797502008-01-30 13:34:09 +0100531 if (within(address, HIGH_MAP_START, HIGH_MAP_END))
532 address = (unsigned long) __va(phys_addr);
Arjan van de Ven488fd992008-01-30 13:34:07 +0100533#endif
534
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100535 err = __change_page_attr(address, cpa);
Thomas Gleixner08797502008-01-30 13:34:09 +0100536 if (err)
537 return err;
538
539#ifdef CONFIG_X86_64
540 /*
541 * If the physical address is inside the kernel map, we need
542 * to touch the high mapped kernel as well:
543 */
544 if (within(phys_addr, 0, KERNEL_TEXT_SIZE)) {
545 /*
546 * Calc the high mapping address. See __phys_addr()
547 * for the non obvious details.
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100548 *
549 * Note that NX and other required permissions are
550 * checked in static_protections().
Thomas Gleixner08797502008-01-30 13:34:09 +0100551 */
552 address = phys_addr + HIGH_MAP_START - phys_base;
Thomas Gleixner08797502008-01-30 13:34:09 +0100553
554 /*
555 * Our high aliases are imprecise, because we check
556 * everything between 0 and KERNEL_TEXT_SIZE, so do
557 * not propagate lookup failures back to users:
558 */
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100559 __change_page_attr(address, cpa);
Thomas Gleixner08797502008-01-30 13:34:09 +0100560 }
561#endif
Ingo Molnar44af6c42008-01-30 13:34:03 +0100562 return err;
563}
564
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100565static int __change_page_attr_set_clr(struct cpa_data *cpa)
Thomas Gleixnerff314522008-01-30 13:34:08 +0100566{
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100567 int ret, numpages = cpa->numpages;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100568
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100569 while (numpages) {
570 /*
571 * Store the remaining nr of pages for the large page
572 * preservation check.
573 */
574 cpa->numpages = numpages;
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100575 ret = change_page_attr_addr(cpa);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100576 if (ret)
577 return ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100578
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100579 /*
580 * Adjust the number of pages with the result of the
581 * CPA operation. Either a large page has been
582 * preserved or a single page update happened.
583 */
584 BUG_ON(cpa->numpages > numpages);
585 numpages -= cpa->numpages;
586 cpa->vaddr += cpa->numpages * PAGE_SIZE;
587 }
Thomas Gleixnerff314522008-01-30 13:34:08 +0100588 return 0;
589}
590
Andi Kleen6bb83832008-02-04 16:48:06 +0100591static inline int cache_attr(pgprot_t attr)
592{
593 return pgprot_val(attr) &
594 (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
595}
596
Thomas Gleixnerff314522008-01-30 13:34:08 +0100597static int change_page_attr_set_clr(unsigned long addr, int numpages,
598 pgprot_t mask_set, pgprot_t mask_clr)
599{
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100600 struct cpa_data cpa;
Andi Kleen6bb83832008-02-04 16:48:06 +0100601 int ret, cache;
Thomas Gleixner331e4062008-02-04 16:48:06 +0100602
603 /*
604 * Check, if we are requested to change a not supported
605 * feature:
606 */
607 mask_set = canon_pgprot(mask_set);
608 mask_clr = canon_pgprot(mask_clr);
609 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr))
610 return 0;
611
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100612 cpa.vaddr = addr;
613 cpa.numpages = numpages;
614 cpa.mask_set = mask_set;
615 cpa.mask_clr = mask_clr;
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100616 cpa.flushtlb = 0;
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100617
618 ret = __change_page_attr_set_clr(&cpa);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100619
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100620 /*
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100621 * Check whether we really changed something:
622 */
623 if (!cpa.flushtlb)
624 return ret;
625
626 /*
Andi Kleen6bb83832008-02-04 16:48:06 +0100627 * No need to flush, when we did not set any of the caching
628 * attributes:
629 */
630 cache = cache_attr(mask_set);
631
632 /*
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100633 * On success we use clflush, when the CPU supports it to
634 * avoid the wbindv. If the CPU does not support it and in the
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100635 * error case we fall back to cpa_flush_all (which uses
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100636 * wbindv):
637 */
638 if (!ret && cpu_has_clflush)
Andi Kleen6bb83832008-02-04 16:48:06 +0100639 cpa_flush_range(addr, numpages, cache);
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100640 else
Andi Kleen6bb83832008-02-04 16:48:06 +0100641 cpa_flush_all(cache);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100642
643 return ret;
644}
645
Thomas Gleixner56744542008-01-30 13:34:08 +0100646static inline int change_page_attr_set(unsigned long addr, int numpages,
647 pgprot_t mask)
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100648{
Thomas Gleixner56744542008-01-30 13:34:08 +0100649 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100650}
651
Thomas Gleixner56744542008-01-30 13:34:08 +0100652static inline int change_page_attr_clear(unsigned long addr, int numpages,
653 pgprot_t mask)
Thomas Gleixner72932c72008-01-30 13:34:08 +0100654{
Huang, Ying58270402008-01-31 22:05:43 +0100655 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100656}
657
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100658int set_memory_uc(unsigned long addr, int numpages)
659{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100660 return change_page_attr_set(addr, numpages,
661 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100662}
663EXPORT_SYMBOL(set_memory_uc);
664
665int set_memory_wb(unsigned long addr, int numpages)
666{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100667 return change_page_attr_clear(addr, numpages,
668 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100669}
670EXPORT_SYMBOL(set_memory_wb);
671
672int set_memory_x(unsigned long addr, int numpages)
673{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100674 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100675}
676EXPORT_SYMBOL(set_memory_x);
677
678int set_memory_nx(unsigned long addr, int numpages)
679{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100680 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100681}
682EXPORT_SYMBOL(set_memory_nx);
683
684int set_memory_ro(unsigned long addr, int numpages)
685{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100686 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100687}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100688
689int set_memory_rw(unsigned long addr, int numpages)
690{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100691 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100692}
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100693
694int set_memory_np(unsigned long addr, int numpages)
695{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100696 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100697}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100698
699int set_pages_uc(struct page *page, int numpages)
700{
701 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100702
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100703 return set_memory_uc(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100704}
705EXPORT_SYMBOL(set_pages_uc);
706
707int set_pages_wb(struct page *page, int numpages)
708{
709 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100710
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100711 return set_memory_wb(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100712}
713EXPORT_SYMBOL(set_pages_wb);
714
715int set_pages_x(struct page *page, int numpages)
716{
717 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100718
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100719 return set_memory_x(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100720}
721EXPORT_SYMBOL(set_pages_x);
722
723int set_pages_nx(struct page *page, int numpages)
724{
725 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100726
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100727 return set_memory_nx(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100728}
729EXPORT_SYMBOL(set_pages_nx);
730
731int set_pages_ro(struct page *page, int numpages)
732{
733 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100734
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100735 return set_memory_ro(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100736}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100737
738int set_pages_rw(struct page *page, int numpages)
739{
740 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100741
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100742 return set_memory_rw(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100743}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100746
747static int __set_pages_p(struct page *page, int numpages)
748{
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100749 struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
750 .numpages = numpages,
751 .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
752 .mask_clr = __pgprot(0)};
Thomas Gleixner72932c72008-01-30 13:34:08 +0100753
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100754 return __change_page_attr_set_clr(&cpa);
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100755}
756
757static int __set_pages_np(struct page *page, int numpages)
758{
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100759 struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
760 .numpages = numpages,
761 .mask_set = __pgprot(0),
762 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW)};
Thomas Gleixner72932c72008-01-30 13:34:08 +0100763
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100764 return __change_page_attr_set_clr(&cpa);
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100765}
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767void kernel_map_pages(struct page *page, int numpages, int enable)
768{
769 if (PageHighMem(page))
770 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100771 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700772 debug_check_no_locks_freed(page_address(page),
773 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100774 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800775
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100776 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +0100777 * If page allocator is not up yet then do not call c_p_a():
778 */
779 if (!debug_pagealloc_enabled)
780 return;
781
782 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100783 * The return value is ignored - the calls cannot fail,
784 * large pages are disabled at boot time:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 */
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100786 if (enable)
787 __set_pages_p(page, numpages);
788 else
789 __set_pages_np(page, numpages);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100790
791 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100792 * We should perform an IPI and flush all tlbs,
793 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 */
795 __flush_tlb_all();
796}
797#endif
Arjan van de Vend1028a12008-01-30 13:34:07 +0100798
799/*
800 * The testcases use internal knowledge of the implementation that shouldn't
801 * be exposed to the rest of the kernel. Include these directly here.
802 */
803#ifdef CONFIG_CPA_DEBUG
804#include "pageattr-test.c"
805#endif