blob: 42ca3d8effad96a045aa537800651acb442a1712 [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
Thomas Gleixner72e458d2008-02-04 16:48:07 +010019struct cpa_data {
20 unsigned long vaddr;
Thomas Gleixner72e458d2008-02-04 16:48:07 +010021 pgprot_t mask_set;
22 pgprot_t mask_clr;
Thomas Gleixner65e074d2008-02-04 16:48:07 +010023 int numpages;
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +010024 int flushtlb;
Thomas Gleixner72e458d2008-02-04 16:48:07 +010025};
26
Thomas Gleixner65e074d2008-02-04 16:48:07 +010027enum {
28 CPA_NO_SPLIT = 0,
29 CPA_SPLIT,
30};
31
Arjan van de Vened724be2008-01-30 13:34:04 +010032static inline int
33within(unsigned long addr, unsigned long start, unsigned long end)
Ingo Molnar687c4822008-01-30 13:34:04 +010034{
Arjan van de Vened724be2008-01-30 13:34:04 +010035 return addr >= start && addr < end;
36}
37
38/*
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010039 * Flushing functions
40 */
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010041
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010042/**
43 * clflush_cache_range - flush a cache range with clflush
44 * @addr: virtual start address
45 * @size: number of bytes to flush
46 *
47 * clflush is an unordered instruction which needs fencing with mfence
48 * to avoid ordering issues.
49 */
Ingo Molnar4c61afc2008-01-30 13:34:09 +010050void clflush_cache_range(void *vaddr, unsigned int size)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010051{
Ingo Molnar4c61afc2008-01-30 13:34:09 +010052 void *vend = vaddr + size - 1;
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010053
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010054 mb();
Ingo Molnar4c61afc2008-01-30 13:34:09 +010055
56 for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
57 clflush(vaddr);
58 /*
59 * Flush any possible final partial cacheline:
60 */
61 clflush(vend);
62
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010063 mb();
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010064}
65
Thomas Gleixneraf1e6842008-01-30 13:34:08 +010066static void __cpa_flush_all(void *arg)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010067{
Andi Kleen6bb83832008-02-04 16:48:06 +010068 unsigned long cache = (unsigned long)arg;
69
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010070 /*
71 * Flush all to work around Errata in early athlons regarding
72 * large page flushing.
73 */
74 __flush_tlb_all();
75
Andi Kleen6bb83832008-02-04 16:48:06 +010076 if (cache && boot_cpu_data.x86_model >= 4)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010077 wbinvd();
78}
79
Andi Kleen6bb83832008-02-04 16:48:06 +010080static void cpa_flush_all(unsigned long cache)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010081{
82 BUG_ON(irqs_disabled());
83
Andi Kleen6bb83832008-02-04 16:48:06 +010084 on_each_cpu(__cpa_flush_all, (void *) cache, 1, 1);
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010085}
86
Thomas Gleixner57a6a462008-01-30 13:34:08 +010087static void __cpa_flush_range(void *arg)
88{
Thomas Gleixner57a6a462008-01-30 13:34:08 +010089 /*
90 * We could optimize that further and do individual per page
91 * tlb invalidates for a low number of pages. Caveat: we must
92 * flush the high aliases on 64bit as well.
93 */
94 __flush_tlb_all();
Thomas Gleixner57a6a462008-01-30 13:34:08 +010095}
96
Andi Kleen6bb83832008-02-04 16:48:06 +010097static void cpa_flush_range(unsigned long start, int numpages, int cache)
Thomas Gleixner57a6a462008-01-30 13:34:08 +010098{
Ingo Molnar4c61afc2008-01-30 13:34:09 +010099 unsigned int i, level;
100 unsigned long addr;
101
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100102 BUG_ON(irqs_disabled());
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100103 WARN_ON(PAGE_ALIGN(start) != start);
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100104
Thomas Gleixner3b233e52008-01-30 13:34:08 +0100105 on_each_cpu(__cpa_flush_range, NULL, 1, 1);
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100106
Andi Kleen6bb83832008-02-04 16:48:06 +0100107 if (!cache)
108 return;
109
Thomas Gleixner3b233e52008-01-30 13:34:08 +0100110 /*
111 * We only need to flush on one CPU,
112 * clflush is a MESI-coherent instruction that
113 * will cause all other CPUs to flush the same
114 * cachelines:
115 */
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100116 for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
117 pte_t *pte = lookup_address(addr, &level);
118
119 /*
120 * Only flush present addresses:
121 */
Thomas Gleixner7bfb72e2008-02-04 16:48:08 +0100122 if (pte && (pte_val(*pte) & _PAGE_PRESENT))
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100123 clflush_cache_range((void *) addr, PAGE_SIZE);
124 }
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100125}
126
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100127#define HIGH_MAP_START __START_KERNEL_map
128#define HIGH_MAP_END (__START_KERNEL_map + KERNEL_TEXT_SIZE)
129
130
131/*
132 * Converts a virtual address to a X86-64 highmap address
133 */
134static unsigned long virt_to_highmap(void *address)
135{
136#ifdef CONFIG_X86_64
137 return __pa((unsigned long)address) + HIGH_MAP_START - phys_base;
138#else
139 return (unsigned long)address;
140#endif
141}
142
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100143/*
Arjan van de Vened724be2008-01-30 13:34:04 +0100144 * Certain areas of memory on x86 require very specific protection flags,
145 * for example the BIOS area or kernel text. Callers don't always get this
146 * right (again, ioremap() on BIOS memory is not uncommon) so this function
147 * checks and fixes these known static required protection bits.
148 */
149static inline pgprot_t static_protections(pgprot_t prot, unsigned long address)
150{
151 pgprot_t forbidden = __pgprot(0);
152
Ingo Molnar687c4822008-01-30 13:34:04 +0100153 /*
Arjan van de Vened724be2008-01-30 13:34:04 +0100154 * The BIOS area between 640k and 1Mb needs to be executable for
155 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
Ingo Molnar687c4822008-01-30 13:34:04 +0100156 */
Arjan van de Vened724be2008-01-30 13:34:04 +0100157 if (within(__pa(address), BIOS_BEGIN, BIOS_END))
158 pgprot_val(forbidden) |= _PAGE_NX;
159
160 /*
161 * The kernel text needs to be executable for obvious reasons
162 * Does not cover __inittext since that is gone later on
163 */
164 if (within(address, (unsigned long)_text, (unsigned long)_etext))
165 pgprot_val(forbidden) |= _PAGE_NX;
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100166 /*
167 * Do the same for the x86-64 high kernel mapping
168 */
169 if (within(address, virt_to_highmap(_text), virt_to_highmap(_etext)))
170 pgprot_val(forbidden) |= _PAGE_NX;
171
Arjan van de Vened724be2008-01-30 13:34:04 +0100172
173#ifdef CONFIG_DEBUG_RODATA
174 /* The .rodata section needs to be read-only */
175 if (within(address, (unsigned long)__start_rodata,
176 (unsigned long)__end_rodata))
177 pgprot_val(forbidden) |= _PAGE_RW;
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100178 /*
179 * Do the same for the x86-64 high kernel mapping
180 */
181 if (within(address, virt_to_highmap(__start_rodata),
182 virt_to_highmap(__end_rodata)))
183 pgprot_val(forbidden) |= _PAGE_RW;
Arjan van de Vened724be2008-01-30 13:34:04 +0100184#endif
185
186 prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
Ingo Molnar687c4822008-01-30 13:34:04 +0100187
188 return prot;
189}
190
Thomas Gleixner9a14aef2008-02-04 16:48:07 +0100191/*
192 * Lookup the page table entry for a virtual address. Return a pointer
193 * to the entry and the level of the mapping.
194 *
195 * Note: We return pud and pmd either when the entry is marked large
196 * or when the present bit is not set. Otherwise we would return a
197 * pointer to a nonexisting mapping.
198 */
Ingo Molnarf0646e42008-01-30 13:33:43 +0100199pte_t *lookup_address(unsigned long address, int *level)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100200{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 pgd_t *pgd = pgd_offset_k(address);
202 pud_t *pud;
203 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100204
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100205 *level = PG_LEVEL_NONE;
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (pgd_none(*pgd))
208 return NULL;
209 pud = pud_offset(pgd, address);
210 if (pud_none(*pud))
211 return NULL;
Andi Kleenc2f71ee2008-02-04 16:48:09 +0100212
213 *level = PG_LEVEL_1G;
214 if (pud_large(*pud) || !pud_present(*pud))
215 return (pte_t *)pud;
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 pmd = pmd_offset(pud, address);
218 if (pmd_none(*pmd))
219 return NULL;
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100220
221 *level = PG_LEVEL_2M;
Thomas Gleixner9a14aef2008-02-04 16:48:07 +0100222 if (pmd_large(*pmd) || !pmd_present(*pmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return (pte_t *)pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100225 *level = PG_LEVEL_4K;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100226 return pte_offset_kernel(pmd, address);
227}
228
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100229static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100230{
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100231 /* change init_mm */
232 set_pte_atomic(kpte, pte);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100233#ifdef CONFIG_X86_32
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100234 if (!SHARED_KERNEL_PMD) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100235 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Jeremy Fitzhardingee3ed9102008-01-30 13:34:11 +0100237 list_for_each_entry(page, &pgd_list, lru) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100238 pgd_t *pgd;
239 pud_t *pud;
240 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100241
Ingo Molnar44af6c42008-01-30 13:34:03 +0100242 pgd = (pgd_t *)page_address(page) + pgd_index(address);
243 pud = pud_offset(pgd, address);
244 pmd = pmd_offset(pud, address);
245 set_pte_atomic((pte_t *)pmd, pte);
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
Ingo Molnar44af6c42008-01-30 13:34:03 +0100248#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100251static int try_preserve_large_page(pte_t *kpte, unsigned long address,
252 struct cpa_data *cpa)
253{
254 unsigned long nextpage_addr, numpages, pmask, psize, flags;
255 pte_t new_pte, old_pte, *tmp;
256 pgprot_t old_prot, new_prot;
257 int level, res = CPA_SPLIT;
258
Ingo Molnar34508f62008-02-04 16:48:07 +0100259 /*
260 * An Athlon 64 X2 showed hard hangs if we tried to preserve
261 * largepages and changed the PSE entry from RW to RO.
262 *
263 * As AMD CPUs have a long series of erratas in this area,
264 * (and none of the known ones seem to explain this hang),
265 * disable this code until the hang can be debugged:
266 */
267 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
268 return res;
269
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100270 spin_lock_irqsave(&pgd_lock, flags);
271 /*
272 * Check for races, another CPU might have split this page
273 * up already:
274 */
275 tmp = lookup_address(address, &level);
276 if (tmp != kpte)
277 goto out_unlock;
278
279 switch (level) {
280 case PG_LEVEL_2M:
Andi Kleen31422c52008-02-04 16:48:08 +0100281 psize = PMD_PAGE_SIZE;
282 pmask = PMD_PAGE_MASK;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100283 break;
Andi Kleenf07333f2008-02-04 16:48:09 +0100284#ifdef CONFIG_X86_64
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100285 case PG_LEVEL_1G:
Andi Kleenf07333f2008-02-04 16:48:09 +0100286 psize = PMD_PAGE_SIZE;
287 pmask = PMD_PAGE_MASK;
288 break;
289#endif
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100290 default:
291 res = -EINVAL;
292 goto out_unlock;
293 }
294
295 /*
296 * Calculate the number of pages, which fit into this large
297 * page starting at address:
298 */
299 nextpage_addr = (address + psize) & pmask;
300 numpages = (nextpage_addr - address) >> PAGE_SHIFT;
301 if (numpages < cpa->numpages)
302 cpa->numpages = numpages;
303
304 /*
305 * We are safe now. Check whether the new pgprot is the same:
306 */
307 old_pte = *kpte;
308 old_prot = new_prot = pte_pgprot(old_pte);
309
310 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
311 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
312 new_prot = static_protections(new_prot, address);
313
314 /*
315 * If there are no changes, return. maxpages has been updated
316 * above:
317 */
318 if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
319 res = CPA_NO_SPLIT;
320 goto out_unlock;
321 }
322
323 /*
324 * We need to change the attributes. Check, whether we can
325 * change the large page in one go. We request a split, when
326 * the address is not aligned and the number of pages is
327 * smaller than the number of pages in the large page. Note
328 * that we limited the number of possible pages already to
329 * the number of pages in the large page.
330 */
331 if (address == (nextpage_addr - psize) && cpa->numpages == numpages) {
332 /*
333 * The address is aligned and the number of pages
334 * covers the full page.
335 */
336 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
337 __set_pmd_pte(kpte, address, new_pte);
338 cpa->flushtlb = 1;
339 res = CPA_NO_SPLIT;
340 }
341
342out_unlock:
343 spin_unlock_irqrestore(&pgd_lock, flags);
344 return res;
345}
346
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100347static int split_large_page(pte_t *kpte, unsigned long address)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100348{
Thomas Gleixner07cf89c2008-02-04 16:48:08 +0100349 pgprot_t ref_prot;
Ingo Molnar12d6f212008-01-30 13:33:58 +0100350 gfp_t gfp_flags = GFP_KERNEL;
Andi Kleenf07333f2008-02-04 16:48:09 +0100351 unsigned long flags, addr, pfn, pfninc = 1;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100352 pte_t *pbase, *tmp;
353 struct page *base;
Ingo Molnar86f03982008-01-30 13:34:09 +0100354 unsigned int i, level;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100355
Ingo Molnar12d6f212008-01-30 13:33:58 +0100356#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnar86f03982008-01-30 13:34:09 +0100357 gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Ingo Molnar12d6f212008-01-30 13:33:58 +0100358#endif
359 base = alloc_pages(gfp_flags, 0);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100360 if (!base)
361 return -ENOMEM;
362
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100363 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100364 /*
365 * Check for races, another CPU might have split this page
366 * up for us already:
367 */
368 tmp = lookup_address(address, &level);
Ingo Molnar6ce9fc12008-02-04 16:48:08 +0100369 if (tmp != kpte)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100370 goto out_unlock;
371
372 address = __pa(address);
Andi Kleen31422c52008-02-04 16:48:08 +0100373 addr = address & PMD_PAGE_MASK;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100374 pbase = (pte_t *)page_address(base);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100375#ifdef CONFIG_X86_32
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100376 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
Ingo Molnar44af6c42008-01-30 13:34:03 +0100377#endif
Thomas Gleixner07cf89c2008-02-04 16:48:08 +0100378 ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100379
Andi Kleenf07333f2008-02-04 16:48:09 +0100380#ifdef CONFIG_X86_64
381 if (level == PG_LEVEL_1G) {
382 pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
383 pgprot_val(ref_prot) |= _PAGE_PSE;
384 addr &= PUD_PAGE_MASK;
385 }
386#endif
387
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100388 /*
389 * Get the target pfn from the original entry:
390 */
391 pfn = pte_pfn(*kpte);
Andi Kleenf07333f2008-02-04 16:48:09 +0100392 for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100393 set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100394
395 /*
Thomas Gleixner07cf89c2008-02-04 16:48:08 +0100396 * Install the new, split up pagetable. Important details here:
Huang, Ying4c881ca2008-01-30 13:34:04 +0100397 *
398 * On Intel the NX bit of all levels must be cleared to make a
399 * page executable. See section 4.13.2 of Intel 64 and IA-32
400 * Architectures Software Developer's Manual).
Thomas Gleixner07cf89c2008-02-04 16:48:08 +0100401 *
402 * Mark the entry present. The current mapping might be
403 * set to not present, which we preserved above.
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100404 */
Huang, Ying4c881ca2008-01-30 13:34:04 +0100405 ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
Thomas Gleixner07cf89c2008-02-04 16:48:08 +0100406 pgprot_val(ref_prot) |= _PAGE_PRESENT;
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100407 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100408 base = NULL;
409
410out_unlock:
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100411 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100412
413 if (base)
414 __free_pages(base, 0);
415
416 return 0;
417}
418
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100419static int __change_page_attr(unsigned long address, struct cpa_data *cpa)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100420{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 struct page *kpte_page;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100422 int level, res;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100423 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100425repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100426 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (!kpte)
428 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200431 BUG_ON(PageLRU(kpte_page));
432 BUG_ON(PageCompound(kpte_page));
433
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100434 if (level == PG_LEVEL_4K) {
Ingo Molnar86f03982008-01-30 13:34:09 +0100435 pte_t new_pte, old_pte = *kpte;
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100436 pgprot_t new_prot = pte_pgprot(old_pte);
437
438 if(!pte_val(old_pte)) {
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100439 printk(KERN_WARNING "CPA: called for zero pte. "
440 "vaddr = %lx cpa->vaddr = %lx\n", address,
441 cpa->vaddr);
442 WARN_ON(1);
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100443 return -EINVAL;
444 }
Thomas Gleixnera72a08a2008-01-30 13:34:07 +0100445
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100446 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
447 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
Ingo Molnar86f03982008-01-30 13:34:09 +0100448
449 new_prot = static_protections(new_prot, address);
450
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100451 /*
452 * We need to keep the pfn from the existing PTE,
453 * after all we're only going to change it's attributes
454 * not the memory it points to
455 */
456 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100457
458 /*
459 * Do we really change anything ?
460 */
461 if (pte_val(old_pte) != pte_val(new_pte)) {
462 set_pte_atomic(kpte, new_pte);
463 cpa->flushtlb = 1;
464 }
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100465 cpa->numpages = 1;
466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100468
469 /*
470 * Check, whether we can keep the large page intact
471 * and just change the pte:
472 */
473 res = try_preserve_large_page(kpte, address, cpa);
474 if (res < 0)
475 return res;
476
477 /*
478 * When the range fits into the existing large page,
479 * return. cp->numpages and cpa->tlbflush have been updated in
480 * try_large_page:
481 */
482 if (res == CPA_NO_SPLIT)
483 return 0;
484
485 /*
486 * We have to split the large page:
487 */
488 res = split_large_page(kpte, address);
489 if (res)
490 return res;
491 cpa->flushtlb = 1;
492 goto repeat;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100493}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Ingo Molnar44af6c42008-01-30 13:34:03 +0100495/**
496 * change_page_attr_addr - Change page table attributes in linear mapping
497 * @address: Virtual address in linear mapping.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100498 * @prot: New page table attribute (PAGE_*)
499 *
500 * Change page attributes of a page in the direct mapping. This is a variant
501 * of change_page_attr() that also works on memory holes that do not have
502 * mem_map entry (pfn_valid() is false).
503 *
504 * See change_page_attr() documentation for more details.
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100505 *
506 * Modules and drivers should use the set_memory_* APIs instead.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100507 */
508
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100509static int change_page_attr_addr(struct cpa_data *cpa)
Ingo Molnar44af6c42008-01-30 13:34:03 +0100510{
Thomas Gleixner08797502008-01-30 13:34:09 +0100511 int err;
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100512 unsigned long address = cpa->vaddr;
Ingo Molnar44af6c42008-01-30 13:34:03 +0100513
Arjan van de Ven488fd992008-01-30 13:34:07 +0100514#ifdef CONFIG_X86_64
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100515 unsigned long phys_addr = __pa(address);
516
Arjan van de Ven488fd992008-01-30 13:34:07 +0100517 /*
Thomas Gleixner08797502008-01-30 13:34:09 +0100518 * If we are inside the high mapped kernel range, then we
519 * fixup the low mapping first. __va() returns the virtual
520 * address in the linear mapping:
Arjan van de Ven488fd992008-01-30 13:34:07 +0100521 */
Thomas Gleixner08797502008-01-30 13:34:09 +0100522 if (within(address, HIGH_MAP_START, HIGH_MAP_END))
523 address = (unsigned long) __va(phys_addr);
Arjan van de Ven488fd992008-01-30 13:34:07 +0100524#endif
525
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100526 err = __change_page_attr(address, cpa);
Thomas Gleixner08797502008-01-30 13:34:09 +0100527 if (err)
528 return err;
529
530#ifdef CONFIG_X86_64
531 /*
532 * If the physical address is inside the kernel map, we need
533 * to touch the high mapped kernel as well:
534 */
535 if (within(phys_addr, 0, KERNEL_TEXT_SIZE)) {
536 /*
537 * Calc the high mapping address. See __phys_addr()
538 * for the non obvious details.
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100539 *
540 * Note that NX and other required permissions are
541 * checked in static_protections().
Thomas Gleixner08797502008-01-30 13:34:09 +0100542 */
543 address = phys_addr + HIGH_MAP_START - phys_base;
Thomas Gleixner08797502008-01-30 13:34:09 +0100544
545 /*
546 * Our high aliases are imprecise, because we check
547 * everything between 0 and KERNEL_TEXT_SIZE, so do
548 * not propagate lookup failures back to users:
549 */
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100550 __change_page_attr(address, cpa);
Thomas Gleixner08797502008-01-30 13:34:09 +0100551 }
552#endif
Ingo Molnar44af6c42008-01-30 13:34:03 +0100553 return err;
554}
555
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100556static int __change_page_attr_set_clr(struct cpa_data *cpa)
Thomas Gleixnerff314522008-01-30 13:34:08 +0100557{
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100558 int ret, numpages = cpa->numpages;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100559
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100560 while (numpages) {
561 /*
562 * Store the remaining nr of pages for the large page
563 * preservation check.
564 */
565 cpa->numpages = numpages;
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100566 ret = change_page_attr_addr(cpa);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100567 if (ret)
568 return ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100569
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100570 /*
571 * Adjust the number of pages with the result of the
572 * CPA operation. Either a large page has been
573 * preserved or a single page update happened.
574 */
575 BUG_ON(cpa->numpages > numpages);
576 numpages -= cpa->numpages;
577 cpa->vaddr += cpa->numpages * PAGE_SIZE;
578 }
Thomas Gleixnerff314522008-01-30 13:34:08 +0100579 return 0;
580}
581
Andi Kleen6bb83832008-02-04 16:48:06 +0100582static inline int cache_attr(pgprot_t attr)
583{
584 return pgprot_val(attr) &
585 (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
586}
587
Thomas Gleixnerff314522008-01-30 13:34:08 +0100588static int change_page_attr_set_clr(unsigned long addr, int numpages,
589 pgprot_t mask_set, pgprot_t mask_clr)
590{
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100591 struct cpa_data cpa;
Andi Kleen6bb83832008-02-04 16:48:06 +0100592 int ret, cache;
Thomas Gleixner331e4062008-02-04 16:48:06 +0100593
594 /*
595 * Check, if we are requested to change a not supported
596 * feature:
597 */
598 mask_set = canon_pgprot(mask_set);
599 mask_clr = canon_pgprot(mask_clr);
600 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr))
601 return 0;
602
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100603 cpa.vaddr = addr;
604 cpa.numpages = numpages;
605 cpa.mask_set = mask_set;
606 cpa.mask_clr = mask_clr;
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100607 cpa.flushtlb = 0;
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100608
609 ret = __change_page_attr_set_clr(&cpa);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100610
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100611 /*
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100612 * Check whether we really changed something:
613 */
614 if (!cpa.flushtlb)
615 return ret;
616
617 /*
Andi Kleen6bb83832008-02-04 16:48:06 +0100618 * No need to flush, when we did not set any of the caching
619 * attributes:
620 */
621 cache = cache_attr(mask_set);
622
623 /*
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100624 * On success we use clflush, when the CPU supports it to
625 * avoid the wbindv. If the CPU does not support it and in the
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100626 * error case we fall back to cpa_flush_all (which uses
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100627 * wbindv):
628 */
629 if (!ret && cpu_has_clflush)
Andi Kleen6bb83832008-02-04 16:48:06 +0100630 cpa_flush_range(addr, numpages, cache);
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100631 else
Andi Kleen6bb83832008-02-04 16:48:06 +0100632 cpa_flush_all(cache);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100633
634 return ret;
635}
636
Thomas Gleixner56744542008-01-30 13:34:08 +0100637static inline int change_page_attr_set(unsigned long addr, int numpages,
638 pgprot_t mask)
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100639{
Thomas Gleixner56744542008-01-30 13:34:08 +0100640 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100641}
642
Thomas Gleixner56744542008-01-30 13:34:08 +0100643static inline int change_page_attr_clear(unsigned long addr, int numpages,
644 pgprot_t mask)
Thomas Gleixner72932c72008-01-30 13:34:08 +0100645{
Huang, Ying58270402008-01-31 22:05:43 +0100646 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100647}
648
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100649int set_memory_uc(unsigned long addr, int numpages)
650{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100651 return change_page_attr_set(addr, numpages,
652 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100653}
654EXPORT_SYMBOL(set_memory_uc);
655
656int set_memory_wb(unsigned long addr, int numpages)
657{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100658 return change_page_attr_clear(addr, numpages,
659 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100660}
661EXPORT_SYMBOL(set_memory_wb);
662
663int set_memory_x(unsigned long addr, int numpages)
664{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100665 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100666}
667EXPORT_SYMBOL(set_memory_x);
668
669int set_memory_nx(unsigned long addr, int numpages)
670{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100671 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100672}
673EXPORT_SYMBOL(set_memory_nx);
674
675int set_memory_ro(unsigned long addr, int numpages)
676{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100677 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100678}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100679
680int set_memory_rw(unsigned long addr, int numpages)
681{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100682 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100683}
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100684
685int set_memory_np(unsigned long addr, int numpages)
686{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100687 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100688}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100689
690int set_pages_uc(struct page *page, int numpages)
691{
692 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100693
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100694 return set_memory_uc(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100695}
696EXPORT_SYMBOL(set_pages_uc);
697
698int set_pages_wb(struct page *page, int numpages)
699{
700 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100701
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100702 return set_memory_wb(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100703}
704EXPORT_SYMBOL(set_pages_wb);
705
706int set_pages_x(struct page *page, int numpages)
707{
708 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100709
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100710 return set_memory_x(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100711}
712EXPORT_SYMBOL(set_pages_x);
713
714int set_pages_nx(struct page *page, int numpages)
715{
716 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100717
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100718 return set_memory_nx(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100719}
720EXPORT_SYMBOL(set_pages_nx);
721
722int set_pages_ro(struct page *page, int numpages)
723{
724 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100725
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100726 return set_memory_ro(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100727}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100728
729int set_pages_rw(struct page *page, int numpages)
730{
731 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100732
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100733 return set_memory_rw(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100734}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100737
738static int __set_pages_p(struct page *page, int numpages)
739{
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100740 struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
741 .numpages = numpages,
742 .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
743 .mask_clr = __pgprot(0)};
Thomas Gleixner72932c72008-01-30 13:34:08 +0100744
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100745 return __change_page_attr_set_clr(&cpa);
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100746}
747
748static int __set_pages_np(struct page *page, int numpages)
749{
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100750 struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
751 .numpages = numpages,
752 .mask_set = __pgprot(0),
753 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW)};
Thomas Gleixner72932c72008-01-30 13:34:08 +0100754
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100755 return __change_page_attr_set_clr(&cpa);
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100756}
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758void kernel_map_pages(struct page *page, int numpages, int enable)
759{
760 if (PageHighMem(page))
761 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100762 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700763 debug_check_no_locks_freed(page_address(page),
764 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100765 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800766
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100767 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +0100768 * If page allocator is not up yet then do not call c_p_a():
769 */
770 if (!debug_pagealloc_enabled)
771 return;
772
773 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100774 * The return value is ignored - the calls cannot fail,
775 * large pages are disabled at boot time:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 */
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100777 if (enable)
778 __set_pages_p(page, numpages);
779 else
780 __set_pages_np(page, numpages);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100781
782 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100783 * We should perform an IPI and flush all tlbs,
784 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 */
786 __flush_tlb_all();
787}
788#endif
Arjan van de Vend1028a12008-01-30 13:34:07 +0100789
790/*
791 * The testcases use internal knowledge of the implementation that shouldn't
792 * be exposed to the rest of the kernel. Include these directly here.
793 */
794#ifdef CONFIG_CPA_DEBUG
795#include "pageattr-test.c"
796#endif