blob: 40b7ac58e671ff8f39f70a2e051671f1564d4de5 [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 */
122 if (pte && pte_present(*pte))
123 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
Ingo Molnarf0646e42008-01-30 13:33:43 +0100191pte_t *lookup_address(unsigned long address, int *level)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100192{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 pgd_t *pgd = pgd_offset_k(address);
194 pud_t *pud;
195 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100196
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100197 *level = PG_LEVEL_NONE;
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (pgd_none(*pgd))
200 return NULL;
201 pud = pud_offset(pgd, address);
202 if (pud_none(*pud))
203 return NULL;
204 pmd = pmd_offset(pud, address);
205 if (pmd_none(*pmd))
206 return NULL;
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100207
208 *level = PG_LEVEL_2M;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (pmd_large(*pmd))
210 return (pte_t *)pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100212 *level = PG_LEVEL_4K;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100213 return pte_offset_kernel(pmd, address);
214}
215
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100216static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100217{
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100218 /* change init_mm */
219 set_pte_atomic(kpte, pte);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100220#ifdef CONFIG_X86_32
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100221 if (!SHARED_KERNEL_PMD) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100222 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Jeremy Fitzhardingee3ed9102008-01-30 13:34:11 +0100224 list_for_each_entry(page, &pgd_list, lru) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100225 pgd_t *pgd;
226 pud_t *pud;
227 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100228
Ingo Molnar44af6c42008-01-30 13:34:03 +0100229 pgd = (pgd_t *)page_address(page) + pgd_index(address);
230 pud = pud_offset(pgd, address);
231 pmd = pmd_offset(pud, address);
232 set_pte_atomic((pte_t *)pmd, pte);
233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
Ingo Molnar44af6c42008-01-30 13:34:03 +0100235#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100238static int try_preserve_large_page(pte_t *kpte, unsigned long address,
239 struct cpa_data *cpa)
240{
241 unsigned long nextpage_addr, numpages, pmask, psize, flags;
242 pte_t new_pte, old_pte, *tmp;
243 pgprot_t old_prot, new_prot;
244 int level, res = CPA_SPLIT;
245
246 spin_lock_irqsave(&pgd_lock, flags);
247 /*
248 * Check for races, another CPU might have split this page
249 * up already:
250 */
251 tmp = lookup_address(address, &level);
252 if (tmp != kpte)
253 goto out_unlock;
254
255 switch (level) {
256 case PG_LEVEL_2M:
257 psize = LARGE_PAGE_SIZE;
258 pmask = LARGE_PAGE_MASK;
259 break;
260 case PG_LEVEL_1G:
261 default:
262 res = -EINVAL;
263 goto out_unlock;
264 }
265
266 /*
267 * Calculate the number of pages, which fit into this large
268 * page starting at address:
269 */
270 nextpage_addr = (address + psize) & pmask;
271 numpages = (nextpage_addr - address) >> PAGE_SHIFT;
272 if (numpages < cpa->numpages)
273 cpa->numpages = numpages;
274
275 /*
276 * We are safe now. Check whether the new pgprot is the same:
277 */
278 old_pte = *kpte;
279 old_prot = new_prot = pte_pgprot(old_pte);
280
281 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
282 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
283 new_prot = static_protections(new_prot, address);
284
285 /*
286 * If there are no changes, return. maxpages has been updated
287 * above:
288 */
289 if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
290 res = CPA_NO_SPLIT;
291 goto out_unlock;
292 }
293
294 /*
295 * We need to change the attributes. Check, whether we can
296 * change the large page in one go. We request a split, when
297 * the address is not aligned and the number of pages is
298 * smaller than the number of pages in the large page. Note
299 * that we limited the number of possible pages already to
300 * the number of pages in the large page.
301 */
302 if (address == (nextpage_addr - psize) && cpa->numpages == numpages) {
303 /*
304 * The address is aligned and the number of pages
305 * covers the full page.
306 */
307 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
308 __set_pmd_pte(kpte, address, new_pte);
309 cpa->flushtlb = 1;
310 res = CPA_NO_SPLIT;
311 }
312
313out_unlock:
314 spin_unlock_irqrestore(&pgd_lock, flags);
315 return res;
316}
317
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100318static int split_large_page(pte_t *kpte, unsigned long address)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100319{
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100320 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnar12d6f212008-01-30 13:33:58 +0100321 gfp_t gfp_flags = GFP_KERNEL;
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100322 unsigned long flags, addr, pfn;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100323 pte_t *pbase, *tmp;
324 struct page *base;
Ingo Molnar86f03982008-01-30 13:34:09 +0100325 unsigned int i, level;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100326
Ingo Molnar12d6f212008-01-30 13:33:58 +0100327#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnar86f03982008-01-30 13:34:09 +0100328 gfp_flags = __GFP_HIGH | __GFP_NOFAIL | __GFP_NOWARN;
329 gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Ingo Molnar12d6f212008-01-30 13:33:58 +0100330#endif
331 base = alloc_pages(gfp_flags, 0);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100332 if (!base)
333 return -ENOMEM;
334
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100335 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100336 /*
337 * Check for races, another CPU might have split this page
338 * up for us already:
339 */
340 tmp = lookup_address(address, &level);
Ingo Molnar5508a742008-01-30 13:33:56 +0100341 if (tmp != kpte) {
342 WARN_ON_ONCE(1);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100343 goto out_unlock;
Ingo Molnar5508a742008-01-30 13:33:56 +0100344 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100345
346 address = __pa(address);
347 addr = address & LARGE_PAGE_MASK;
348 pbase = (pte_t *)page_address(base);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100349#ifdef CONFIG_X86_32
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100350 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
Ingo Molnar44af6c42008-01-30 13:34:03 +0100351#endif
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100352
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100353 /*
354 * Get the target pfn from the original entry:
355 */
356 pfn = pte_pfn(*kpte);
357 for (i = 0; i < PTRS_PER_PTE; i++, pfn++)
358 set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100359
360 /*
Huang, Ying4c881ca2008-01-30 13:34:04 +0100361 * Install the new, split up pagetable. Important detail here:
362 *
363 * On Intel the NX bit of all levels must be cleared to make a
364 * page executable. See section 4.13.2 of Intel 64 and IA-32
365 * Architectures Software Developer's Manual).
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100366 */
Huang, Ying4c881ca2008-01-30 13:34:04 +0100367 ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100368 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100369 base = NULL;
370
371out_unlock:
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100372 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100373
374 if (base)
375 __free_pages(base, 0);
376
377 return 0;
378}
379
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100380static int __change_page_attr(unsigned long address, struct cpa_data *cpa)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100381{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 struct page *kpte_page;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100383 int level, res;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100384 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100386repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100387 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (!kpte)
389 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200392 BUG_ON(PageLRU(kpte_page));
393 BUG_ON(PageCompound(kpte_page));
394
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100395 if (level == PG_LEVEL_4K) {
Ingo Molnar86f03982008-01-30 13:34:09 +0100396 pte_t new_pte, old_pte = *kpte;
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100397 pgprot_t new_prot = pte_pgprot(old_pte);
398
399 if(!pte_val(old_pte)) {
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100400 printk(KERN_WARNING "CPA: called for zero pte. "
401 "vaddr = %lx cpa->vaddr = %lx\n", address,
402 cpa->vaddr);
403 WARN_ON(1);
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100404 return -EINVAL;
405 }
Thomas Gleixnera72a08a2008-01-30 13:34:07 +0100406
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100407 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
408 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
Ingo Molnar86f03982008-01-30 13:34:09 +0100409
410 new_prot = static_protections(new_prot, address);
411
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100412 /*
413 * We need to keep the pfn from the existing PTE,
414 * after all we're only going to change it's attributes
415 * not the memory it points to
416 */
417 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100418
419 /*
420 * Do we really change anything ?
421 */
422 if (pte_val(old_pte) != pte_val(new_pte)) {
423 set_pte_atomic(kpte, new_pte);
424 cpa->flushtlb = 1;
425 }
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100426 cpa->numpages = 1;
427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100429
430 /*
431 * Check, whether we can keep the large page intact
432 * and just change the pte:
433 */
434 res = try_preserve_large_page(kpte, address, cpa);
435 if (res < 0)
436 return res;
437
438 /*
439 * When the range fits into the existing large page,
440 * return. cp->numpages and cpa->tlbflush have been updated in
441 * try_large_page:
442 */
443 if (res == CPA_NO_SPLIT)
444 return 0;
445
446 /*
447 * We have to split the large page:
448 */
449 res = split_large_page(kpte, address);
450 if (res)
451 return res;
452 cpa->flushtlb = 1;
453 goto repeat;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100454}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Ingo Molnar44af6c42008-01-30 13:34:03 +0100456/**
457 * change_page_attr_addr - Change page table attributes in linear mapping
458 * @address: Virtual address in linear mapping.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100459 * @prot: New page table attribute (PAGE_*)
460 *
461 * Change page attributes of a page in the direct mapping. This is a variant
462 * of change_page_attr() that also works on memory holes that do not have
463 * mem_map entry (pfn_valid() is false).
464 *
465 * See change_page_attr() documentation for more details.
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100466 *
467 * Modules and drivers should use the set_memory_* APIs instead.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100468 */
469
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100470static int change_page_attr_addr(struct cpa_data *cpa)
Ingo Molnar44af6c42008-01-30 13:34:03 +0100471{
Thomas Gleixner08797502008-01-30 13:34:09 +0100472 int err;
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100473 unsigned long address = cpa->vaddr;
Ingo Molnar44af6c42008-01-30 13:34:03 +0100474
Arjan van de Ven488fd992008-01-30 13:34:07 +0100475#ifdef CONFIG_X86_64
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100476 unsigned long phys_addr = __pa(address);
477
Arjan van de Ven488fd992008-01-30 13:34:07 +0100478 /*
Thomas Gleixner08797502008-01-30 13:34:09 +0100479 * If we are inside the high mapped kernel range, then we
480 * fixup the low mapping first. __va() returns the virtual
481 * address in the linear mapping:
Arjan van de Ven488fd992008-01-30 13:34:07 +0100482 */
Thomas Gleixner08797502008-01-30 13:34:09 +0100483 if (within(address, HIGH_MAP_START, HIGH_MAP_END))
484 address = (unsigned long) __va(phys_addr);
Arjan van de Ven488fd992008-01-30 13:34:07 +0100485#endif
486
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100487 err = __change_page_attr(address, cpa);
Thomas Gleixner08797502008-01-30 13:34:09 +0100488 if (err)
489 return err;
490
491#ifdef CONFIG_X86_64
492 /*
493 * If the physical address is inside the kernel map, we need
494 * to touch the high mapped kernel as well:
495 */
496 if (within(phys_addr, 0, KERNEL_TEXT_SIZE)) {
497 /*
498 * Calc the high mapping address. See __phys_addr()
499 * for the non obvious details.
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100500 *
501 * Note that NX and other required permissions are
502 * checked in static_protections().
Thomas Gleixner08797502008-01-30 13:34:09 +0100503 */
504 address = phys_addr + HIGH_MAP_START - phys_base;
Thomas Gleixner08797502008-01-30 13:34:09 +0100505
506 /*
507 * Our high aliases are imprecise, because we check
508 * everything between 0 and KERNEL_TEXT_SIZE, so do
509 * not propagate lookup failures back to users:
510 */
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100511 __change_page_attr(address, cpa);
Thomas Gleixner08797502008-01-30 13:34:09 +0100512 }
513#endif
Ingo Molnar44af6c42008-01-30 13:34:03 +0100514 return err;
515}
516
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100517static int __change_page_attr_set_clr(struct cpa_data *cpa)
Thomas Gleixnerff314522008-01-30 13:34:08 +0100518{
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100519 int ret, numpages = cpa->numpages;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100520
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100521 while (numpages) {
522 /*
523 * Store the remaining nr of pages for the large page
524 * preservation check.
525 */
526 cpa->numpages = numpages;
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100527 ret = change_page_attr_addr(cpa);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100528 if (ret)
529 return ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100530
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100531 /*
532 * Adjust the number of pages with the result of the
533 * CPA operation. Either a large page has been
534 * preserved or a single page update happened.
535 */
536 BUG_ON(cpa->numpages > numpages);
537 numpages -= cpa->numpages;
538 cpa->vaddr += cpa->numpages * PAGE_SIZE;
539 }
Thomas Gleixnerff314522008-01-30 13:34:08 +0100540 return 0;
541}
542
Andi Kleen6bb83832008-02-04 16:48:06 +0100543static inline int cache_attr(pgprot_t attr)
544{
545 return pgprot_val(attr) &
546 (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
547}
548
Thomas Gleixnerff314522008-01-30 13:34:08 +0100549static int change_page_attr_set_clr(unsigned long addr, int numpages,
550 pgprot_t mask_set, pgprot_t mask_clr)
551{
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100552 struct cpa_data cpa;
Andi Kleen6bb83832008-02-04 16:48:06 +0100553 int ret, cache;
Thomas Gleixner331e4062008-02-04 16:48:06 +0100554
555 /*
556 * Check, if we are requested to change a not supported
557 * feature:
558 */
559 mask_set = canon_pgprot(mask_set);
560 mask_clr = canon_pgprot(mask_clr);
561 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr))
562 return 0;
563
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100564 cpa.vaddr = addr;
565 cpa.numpages = numpages;
566 cpa.mask_set = mask_set;
567 cpa.mask_clr = mask_clr;
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100568 cpa.flushtlb = 0;
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100569
570 ret = __change_page_attr_set_clr(&cpa);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100571
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100572 /*
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100573 * Check whether we really changed something:
574 */
575 if (!cpa.flushtlb)
576 return ret;
577
578 /*
Andi Kleen6bb83832008-02-04 16:48:06 +0100579 * No need to flush, when we did not set any of the caching
580 * attributes:
581 */
582 cache = cache_attr(mask_set);
583
584 /*
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100585 * On success we use clflush, when the CPU supports it to
586 * avoid the wbindv. If the CPU does not support it and in the
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100587 * error case we fall back to cpa_flush_all (which uses
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100588 * wbindv):
589 */
590 if (!ret && cpu_has_clflush)
Andi Kleen6bb83832008-02-04 16:48:06 +0100591 cpa_flush_range(addr, numpages, cache);
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100592 else
Andi Kleen6bb83832008-02-04 16:48:06 +0100593 cpa_flush_all(cache);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100594
595 return ret;
596}
597
Thomas Gleixner56744542008-01-30 13:34:08 +0100598static inline int change_page_attr_set(unsigned long addr, int numpages,
599 pgprot_t mask)
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100600{
Thomas Gleixner56744542008-01-30 13:34:08 +0100601 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100602}
603
Thomas Gleixner56744542008-01-30 13:34:08 +0100604static inline int change_page_attr_clear(unsigned long addr, int numpages,
605 pgprot_t mask)
Thomas Gleixner72932c72008-01-30 13:34:08 +0100606{
Huang, Ying58270402008-01-31 22:05:43 +0100607 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100608}
609
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100610int set_memory_uc(unsigned long addr, int numpages)
611{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100612 return change_page_attr_set(addr, numpages,
613 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100614}
615EXPORT_SYMBOL(set_memory_uc);
616
617int set_memory_wb(unsigned long addr, int numpages)
618{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100619 return change_page_attr_clear(addr, numpages,
620 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100621}
622EXPORT_SYMBOL(set_memory_wb);
623
624int set_memory_x(unsigned long addr, int numpages)
625{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100626 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100627}
628EXPORT_SYMBOL(set_memory_x);
629
630int set_memory_nx(unsigned long addr, int numpages)
631{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100632 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100633}
634EXPORT_SYMBOL(set_memory_nx);
635
636int set_memory_ro(unsigned long addr, int numpages)
637{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100638 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100639}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100640
641int set_memory_rw(unsigned long addr, int numpages)
642{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100643 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100644}
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100645
646int set_memory_np(unsigned long addr, int numpages)
647{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100648 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100649}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100650
651int set_pages_uc(struct page *page, int numpages)
652{
653 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100654
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100655 return set_memory_uc(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100656}
657EXPORT_SYMBOL(set_pages_uc);
658
659int set_pages_wb(struct page *page, int numpages)
660{
661 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100662
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100663 return set_memory_wb(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100664}
665EXPORT_SYMBOL(set_pages_wb);
666
667int set_pages_x(struct page *page, int numpages)
668{
669 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100670
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100671 return set_memory_x(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100672}
673EXPORT_SYMBOL(set_pages_x);
674
675int set_pages_nx(struct page *page, int numpages)
676{
677 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100678
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100679 return set_memory_nx(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100680}
681EXPORT_SYMBOL(set_pages_nx);
682
683int set_pages_ro(struct page *page, int numpages)
684{
685 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100686
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100687 return set_memory_ro(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100688}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100689
690int set_pages_rw(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_rw(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100695}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100698
699static int __set_pages_p(struct page *page, int numpages)
700{
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100701 struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
702 .numpages = numpages,
703 .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
704 .mask_clr = __pgprot(0)};
Thomas Gleixner72932c72008-01-30 13:34:08 +0100705
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100706 return __change_page_attr_set_clr(&cpa);
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100707}
708
709static int __set_pages_np(struct page *page, int numpages)
710{
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100711 struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
712 .numpages = numpages,
713 .mask_set = __pgprot(0),
714 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW)};
Thomas Gleixner72932c72008-01-30 13:34:08 +0100715
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100716 return __change_page_attr_set_clr(&cpa);
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100717}
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719void kernel_map_pages(struct page *page, int numpages, int enable)
720{
721 if (PageHighMem(page))
722 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100723 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700724 debug_check_no_locks_freed(page_address(page),
725 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100726 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800727
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100728 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +0100729 * If page allocator is not up yet then do not call c_p_a():
730 */
731 if (!debug_pagealloc_enabled)
732 return;
733
734 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100735 * The return value is ignored - the calls cannot fail,
736 * large pages are disabled at boot time:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 */
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100738 if (enable)
739 __set_pages_p(page, numpages);
740 else
741 __set_pages_np(page, numpages);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100742
743 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100744 * We should perform an IPI and flush all tlbs,
745 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 */
747 __flush_tlb_all();
748}
749#endif
Arjan van de Vend1028a12008-01-30 13:34:07 +0100750
751/*
752 * The testcases use internal knowledge of the implementation that shouldn't
753 * be exposed to the rest of the kernel. Include these directly here.
754 */
755#ifdef CONFIG_CPA_DEBUG
756#include "pageattr-test.c"
757#endif