blob: 6c55fbdbd7e8f42f0a2ca75c7f91623a8db9d69c [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
Arjan van de Vened724be2008-01-30 13:34:04 +010019static inline int
20within(unsigned long addr, unsigned long start, unsigned long end)
Ingo Molnar687c4822008-01-30 13:34:04 +010021{
Arjan van de Vened724be2008-01-30 13:34:04 +010022 return addr >= start && addr < end;
23}
24
25/*
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010026 * Flushing functions
27 */
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010028
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010029/**
30 * clflush_cache_range - flush a cache range with clflush
31 * @addr: virtual start address
32 * @size: number of bytes to flush
33 *
34 * clflush is an unordered instruction which needs fencing with mfence
35 * to avoid ordering issues.
36 */
Ingo Molnar4c61afc2008-01-30 13:34:09 +010037void clflush_cache_range(void *vaddr, unsigned int size)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010038{
Ingo Molnar4c61afc2008-01-30 13:34:09 +010039 void *vend = vaddr + size - 1;
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010040
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010041 mb();
Ingo Molnar4c61afc2008-01-30 13:34:09 +010042
43 for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
44 clflush(vaddr);
45 /*
46 * Flush any possible final partial cacheline:
47 */
48 clflush(vend);
49
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010050 mb();
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010051}
52
Thomas Gleixneraf1e6842008-01-30 13:34:08 +010053static void __cpa_flush_all(void *arg)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010054{
55 /*
56 * Flush all to work around Errata in early athlons regarding
57 * large page flushing.
58 */
59 __flush_tlb_all();
60
61 if (boot_cpu_data.x86_model >= 4)
62 wbinvd();
63}
64
Thomas Gleixneraf1e6842008-01-30 13:34:08 +010065static void cpa_flush_all(void)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010066{
67 BUG_ON(irqs_disabled());
68
Thomas Gleixneraf1e6842008-01-30 13:34:08 +010069 on_each_cpu(__cpa_flush_all, NULL, 1, 1);
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010070}
71
Thomas Gleixner57a6a462008-01-30 13:34:08 +010072static void __cpa_flush_range(void *arg)
73{
Thomas Gleixner57a6a462008-01-30 13:34:08 +010074 /*
75 * We could optimize that further and do individual per page
76 * tlb invalidates for a low number of pages. Caveat: we must
77 * flush the high aliases on 64bit as well.
78 */
79 __flush_tlb_all();
Thomas Gleixner57a6a462008-01-30 13:34:08 +010080}
81
Ingo Molnar4c61afc2008-01-30 13:34:09 +010082static void cpa_flush_range(unsigned long start, int numpages)
Thomas Gleixner57a6a462008-01-30 13:34:08 +010083{
Ingo Molnar4c61afc2008-01-30 13:34:09 +010084 unsigned int i, level;
85 unsigned long addr;
86
Thomas Gleixner57a6a462008-01-30 13:34:08 +010087 BUG_ON(irqs_disabled());
Ingo Molnar4c61afc2008-01-30 13:34:09 +010088 WARN_ON(PAGE_ALIGN(start) != start);
Thomas Gleixner57a6a462008-01-30 13:34:08 +010089
Thomas Gleixner3b233e52008-01-30 13:34:08 +010090 on_each_cpu(__cpa_flush_range, NULL, 1, 1);
Thomas Gleixner57a6a462008-01-30 13:34:08 +010091
Thomas Gleixner3b233e52008-01-30 13:34:08 +010092 /*
93 * We only need to flush on one CPU,
94 * clflush is a MESI-coherent instruction that
95 * will cause all other CPUs to flush the same
96 * cachelines:
97 */
Ingo Molnar4c61afc2008-01-30 13:34:09 +010098 for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
99 pte_t *pte = lookup_address(addr, &level);
100
101 /*
102 * Only flush present addresses:
103 */
104 if (pte && pte_present(*pte))
105 clflush_cache_range((void *) addr, PAGE_SIZE);
106 }
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100107}
108
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100109#define HIGH_MAP_START __START_KERNEL_map
110#define HIGH_MAP_END (__START_KERNEL_map + KERNEL_TEXT_SIZE)
111
112
113/*
114 * Converts a virtual address to a X86-64 highmap address
115 */
116static unsigned long virt_to_highmap(void *address)
117{
118#ifdef CONFIG_X86_64
119 return __pa((unsigned long)address) + HIGH_MAP_START - phys_base;
120#else
121 return (unsigned long)address;
122#endif
123}
124
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100125/*
Arjan van de Vened724be2008-01-30 13:34:04 +0100126 * Certain areas of memory on x86 require very specific protection flags,
127 * for example the BIOS area or kernel text. Callers don't always get this
128 * right (again, ioremap() on BIOS memory is not uncommon) so this function
129 * checks and fixes these known static required protection bits.
130 */
131static inline pgprot_t static_protections(pgprot_t prot, unsigned long address)
132{
133 pgprot_t forbidden = __pgprot(0);
134
Ingo Molnar687c4822008-01-30 13:34:04 +0100135 /*
Arjan van de Vened724be2008-01-30 13:34:04 +0100136 * The BIOS area between 640k and 1Mb needs to be executable for
137 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
Ingo Molnar687c4822008-01-30 13:34:04 +0100138 */
Arjan van de Vened724be2008-01-30 13:34:04 +0100139 if (within(__pa(address), BIOS_BEGIN, BIOS_END))
140 pgprot_val(forbidden) |= _PAGE_NX;
141
142 /*
143 * The kernel text needs to be executable for obvious reasons
144 * Does not cover __inittext since that is gone later on
145 */
146 if (within(address, (unsigned long)_text, (unsigned long)_etext))
147 pgprot_val(forbidden) |= _PAGE_NX;
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100148 /*
149 * Do the same for the x86-64 high kernel mapping
150 */
151 if (within(address, virt_to_highmap(_text), virt_to_highmap(_etext)))
152 pgprot_val(forbidden) |= _PAGE_NX;
153
Arjan van de Vened724be2008-01-30 13:34:04 +0100154
155#ifdef CONFIG_DEBUG_RODATA
156 /* The .rodata section needs to be read-only */
157 if (within(address, (unsigned long)__start_rodata,
158 (unsigned long)__end_rodata))
159 pgprot_val(forbidden) |= _PAGE_RW;
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100160 /*
161 * Do the same for the x86-64 high kernel mapping
162 */
163 if (within(address, virt_to_highmap(__start_rodata),
164 virt_to_highmap(__end_rodata)))
165 pgprot_val(forbidden) |= _PAGE_RW;
Arjan van de Vened724be2008-01-30 13:34:04 +0100166#endif
167
168 prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
Ingo Molnar687c4822008-01-30 13:34:04 +0100169
170 return prot;
171}
172
Ingo Molnarf0646e42008-01-30 13:33:43 +0100173pte_t *lookup_address(unsigned long address, int *level)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100174{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 pgd_t *pgd = pgd_offset_k(address);
176 pud_t *pud;
177 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100178
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100179 *level = PG_LEVEL_NONE;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (pgd_none(*pgd))
182 return NULL;
183 pud = pud_offset(pgd, address);
184 if (pud_none(*pud))
185 return NULL;
186 pmd = pmd_offset(pud, address);
187 if (pmd_none(*pmd))
188 return NULL;
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100189
190 *level = PG_LEVEL_2M;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (pmd_large(*pmd))
192 return (pte_t *)pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100194 *level = PG_LEVEL_4K;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100195 return pte_offset_kernel(pmd, address);
196}
197
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100198static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100199{
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100200 /* change init_mm */
201 set_pte_atomic(kpte, pte);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100202#ifdef CONFIG_X86_32
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100203 if (!SHARED_KERNEL_PMD) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100204 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Jeremy Fitzhardingee3ed9102008-01-30 13:34:11 +0100206 list_for_each_entry(page, &pgd_list, lru) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100207 pgd_t *pgd;
208 pud_t *pud;
209 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100210
Ingo Molnar44af6c42008-01-30 13:34:03 +0100211 pgd = (pgd_t *)page_address(page) + pgd_index(address);
212 pud = pud_offset(pgd, address);
213 pmd = pmd_offset(pud, address);
214 set_pte_atomic((pte_t *)pmd, pte);
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
Ingo Molnar44af6c42008-01-30 13:34:03 +0100217#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100220static int split_large_page(pte_t *kpte, unsigned long address)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100221{
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100222 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnar12d6f212008-01-30 13:33:58 +0100223 gfp_t gfp_flags = GFP_KERNEL;
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100224 unsigned long flags;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100225 unsigned long addr;
226 pte_t *pbase, *tmp;
227 struct page *base;
Ingo Molnar86f03982008-01-30 13:34:09 +0100228 unsigned int i, level;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100229
Ingo Molnar12d6f212008-01-30 13:33:58 +0100230#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnar86f03982008-01-30 13:34:09 +0100231 gfp_flags = __GFP_HIGH | __GFP_NOFAIL | __GFP_NOWARN;
232 gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Ingo Molnar12d6f212008-01-30 13:33:58 +0100233#endif
234 base = alloc_pages(gfp_flags, 0);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100235 if (!base)
236 return -ENOMEM;
237
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100238 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100239 /*
240 * Check for races, another CPU might have split this page
241 * up for us already:
242 */
243 tmp = lookup_address(address, &level);
Ingo Molnar5508a742008-01-30 13:33:56 +0100244 if (tmp != kpte) {
245 WARN_ON_ONCE(1);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100246 goto out_unlock;
Ingo Molnar5508a742008-01-30 13:33:56 +0100247 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100248
249 address = __pa(address);
250 addr = address & LARGE_PAGE_MASK;
251 pbase = (pte_t *)page_address(base);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100252#ifdef CONFIG_X86_32
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100253 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
Ingo Molnar44af6c42008-01-30 13:34:03 +0100254#endif
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100255
256 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
257 set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
258
259 /*
Huang, Ying4c881ca2008-01-30 13:34:04 +0100260 * Install the new, split up pagetable. Important detail here:
261 *
262 * On Intel the NX bit of all levels must be cleared to make a
263 * page executable. See section 4.13.2 of Intel 64 and IA-32
264 * Architectures Software Developer's Manual).
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100265 */
Huang, Ying4c881ca2008-01-30 13:34:04 +0100266 ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100267 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100268 base = NULL;
269
270out_unlock:
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100271 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100272
273 if (base)
274 __free_pages(base, 0);
275
276 return 0;
277}
278
Ingo Molnar44af6c42008-01-30 13:34:03 +0100279static int
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100280__change_page_attr(unsigned long address, pgprot_t mask_set, pgprot_t mask_clr)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100281{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 struct page *kpte_page;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100283 int level, err = 0;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100284 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100286repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100287 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (!kpte)
289 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200292 BUG_ON(PageLRU(kpte_page));
293 BUG_ON(PageCompound(kpte_page));
294
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100295 if (level == PG_LEVEL_4K) {
Ingo Molnar86f03982008-01-30 13:34:09 +0100296 pte_t new_pte, old_pte = *kpte;
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100297 pgprot_t new_prot = pte_pgprot(old_pte);
298
299 if(!pte_val(old_pte)) {
300 WARN_ON_ONCE(1);
301 return -EINVAL;
302 }
Thomas Gleixnera72a08a2008-01-30 13:34:07 +0100303
Ingo Molnar86f03982008-01-30 13:34:09 +0100304 pgprot_val(new_prot) &= ~pgprot_val(mask_clr);
305 pgprot_val(new_prot) |= pgprot_val(mask_set);
306
307 new_prot = static_protections(new_prot, address);
308
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100309 /*
310 * We need to keep the pfn from the existing PTE,
311 * after all we're only going to change it's attributes
312 * not the memory it points to
313 */
314 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
Ingo Molnar86f03982008-01-30 13:34:09 +0100315 set_pte_atomic(kpte, new_pte);
316 } else {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100317 err = split_large_page(kpte, address);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100318 if (!err)
319 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100321 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100322}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Ingo Molnar44af6c42008-01-30 13:34:03 +0100324/**
325 * change_page_attr_addr - Change page table attributes in linear mapping
326 * @address: Virtual address in linear mapping.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100327 * @prot: New page table attribute (PAGE_*)
328 *
329 * Change page attributes of a page in the direct mapping. This is a variant
330 * of change_page_attr() that also works on memory holes that do not have
331 * mem_map entry (pfn_valid() is false).
332 *
333 * See change_page_attr() documentation for more details.
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100334 *
335 * Modules and drivers should use the set_memory_* APIs instead.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100336 */
337
Thomas Gleixner08797502008-01-30 13:34:09 +0100338
Ingo Molnar86f03982008-01-30 13:34:09 +0100339static int
340change_page_attr_addr(unsigned long address, pgprot_t mask_set,
Thomas Gleixner08797502008-01-30 13:34:09 +0100341 pgprot_t mask_clr)
Ingo Molnar44af6c42008-01-30 13:34:03 +0100342{
Thomas Gleixner08797502008-01-30 13:34:09 +0100343 int err;
Ingo Molnar44af6c42008-01-30 13:34:03 +0100344
Arjan van de Ven488fd992008-01-30 13:34:07 +0100345#ifdef CONFIG_X86_64
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100346 unsigned long phys_addr = __pa(address);
347
Arjan van de Ven488fd992008-01-30 13:34:07 +0100348 /*
Thomas Gleixner08797502008-01-30 13:34:09 +0100349 * If we are inside the high mapped kernel range, then we
350 * fixup the low mapping first. __va() returns the virtual
351 * address in the linear mapping:
Arjan van de Ven488fd992008-01-30 13:34:07 +0100352 */
Thomas Gleixner08797502008-01-30 13:34:09 +0100353 if (within(address, HIGH_MAP_START, HIGH_MAP_END))
354 address = (unsigned long) __va(phys_addr);
Arjan van de Ven488fd992008-01-30 13:34:07 +0100355#endif
356
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100357 err = __change_page_attr(address, mask_set, mask_clr);
Thomas Gleixner08797502008-01-30 13:34:09 +0100358 if (err)
359 return err;
360
361#ifdef CONFIG_X86_64
362 /*
363 * If the physical address is inside the kernel map, we need
364 * to touch the high mapped kernel as well:
365 */
366 if (within(phys_addr, 0, KERNEL_TEXT_SIZE)) {
367 /*
368 * Calc the high mapping address. See __phys_addr()
369 * for the non obvious details.
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100370 *
371 * Note that NX and other required permissions are
372 * checked in static_protections().
Thomas Gleixner08797502008-01-30 13:34:09 +0100373 */
374 address = phys_addr + HIGH_MAP_START - phys_base;
Thomas Gleixner08797502008-01-30 13:34:09 +0100375
376 /*
377 * Our high aliases are imprecise, because we check
378 * everything between 0 and KERNEL_TEXT_SIZE, so do
379 * not propagate lookup failures back to users:
380 */
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100381 __change_page_attr(address, mask_set, mask_clr);
Thomas Gleixner08797502008-01-30 13:34:09 +0100382 }
383#endif
Ingo Molnar44af6c42008-01-30 13:34:03 +0100384 return err;
385}
386
Thomas Gleixnerff314522008-01-30 13:34:08 +0100387static int __change_page_attr_set_clr(unsigned long addr, int numpages,
388 pgprot_t mask_set, pgprot_t mask_clr)
389{
Ingo Molnar86f03982008-01-30 13:34:09 +0100390 unsigned int i;
391 int ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100392
Ingo Molnar86f03982008-01-30 13:34:09 +0100393 for (i = 0; i < numpages ; i++, addr += PAGE_SIZE) {
394 ret = change_page_attr_addr(addr, mask_set, mask_clr);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100395 if (ret)
396 return ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100397 }
398
399 return 0;
400}
401
402static int change_page_attr_set_clr(unsigned long addr, int numpages,
403 pgprot_t mask_set, pgprot_t mask_clr)
404{
405 int ret = __change_page_attr_set_clr(addr, numpages, mask_set,
406 mask_clr);
407
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100408 /*
409 * On success we use clflush, when the CPU supports it to
410 * avoid the wbindv. If the CPU does not support it and in the
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100411 * error case we fall back to cpa_flush_all (which uses
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100412 * wbindv):
413 */
414 if (!ret && cpu_has_clflush)
415 cpa_flush_range(addr, numpages);
416 else
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100417 cpa_flush_all();
Thomas Gleixnerff314522008-01-30 13:34:08 +0100418
419 return ret;
420}
421
Thomas Gleixner56744542008-01-30 13:34:08 +0100422static inline int change_page_attr_set(unsigned long addr, int numpages,
423 pgprot_t mask)
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100424{
Thomas Gleixner56744542008-01-30 13:34:08 +0100425 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100426}
427
Thomas Gleixner56744542008-01-30 13:34:08 +0100428static inline int change_page_attr_clear(unsigned long addr, int numpages,
429 pgprot_t mask)
Thomas Gleixner72932c72008-01-30 13:34:08 +0100430{
Huang, Ying58270402008-01-31 22:05:43 +0100431 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100432}
433
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100434int set_memory_uc(unsigned long addr, int numpages)
435{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100436 return change_page_attr_set(addr, numpages,
437 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100438}
439EXPORT_SYMBOL(set_memory_uc);
440
441int set_memory_wb(unsigned long addr, int numpages)
442{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100443 return change_page_attr_clear(addr, numpages,
444 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100445}
446EXPORT_SYMBOL(set_memory_wb);
447
448int set_memory_x(unsigned long addr, int numpages)
449{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100450 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100451}
452EXPORT_SYMBOL(set_memory_x);
453
454int set_memory_nx(unsigned long addr, int numpages)
455{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100456 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100457}
458EXPORT_SYMBOL(set_memory_nx);
459
460int set_memory_ro(unsigned long addr, int numpages)
461{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100462 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100463}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100464
465int set_memory_rw(unsigned long addr, int numpages)
466{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100467 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100468}
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100469
470int set_memory_np(unsigned long addr, int numpages)
471{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100472 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100473}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100474
475int set_pages_uc(struct page *page, int numpages)
476{
477 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100478
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100479 return set_memory_uc(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100480}
481EXPORT_SYMBOL(set_pages_uc);
482
483int set_pages_wb(struct page *page, int numpages)
484{
485 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100486
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100487 return set_memory_wb(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100488}
489EXPORT_SYMBOL(set_pages_wb);
490
491int set_pages_x(struct page *page, int numpages)
492{
493 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100494
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100495 return set_memory_x(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100496}
497EXPORT_SYMBOL(set_pages_x);
498
499int set_pages_nx(struct page *page, int numpages)
500{
501 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100502
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100503 return set_memory_nx(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100504}
505EXPORT_SYMBOL(set_pages_nx);
506
507int set_pages_ro(struct page *page, int numpages)
508{
509 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100510
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100511 return set_memory_ro(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100512}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100513
514int set_pages_rw(struct page *page, int numpages)
515{
516 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100517
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100518 return set_memory_rw(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100519}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Thomas Gleixner56744542008-01-30 13:34:08 +0100522#if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_CPA_DEBUG)
523static inline int __change_page_attr_set(unsigned long addr, int numpages,
524 pgprot_t mask)
525{
526 return __change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
527}
528
529static inline int __change_page_attr_clear(unsigned long addr, int numpages,
530 pgprot_t mask)
531{
532 return __change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
533}
534#endif
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100537
538static int __set_pages_p(struct page *page, int numpages)
539{
540 unsigned long addr = (unsigned long)page_address(page);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100541
542 return __change_page_attr_set(addr, numpages,
543 __pgprot(_PAGE_PRESENT | _PAGE_RW));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100544}
545
546static int __set_pages_np(struct page *page, int numpages)
547{
548 unsigned long addr = (unsigned long)page_address(page);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100549
550 return __change_page_attr_clear(addr, numpages,
551 __pgprot(_PAGE_PRESENT));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554void kernel_map_pages(struct page *page, int numpages, int enable)
555{
556 if (PageHighMem(page))
557 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100558 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700559 debug_check_no_locks_freed(page_address(page),
560 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100561 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800562
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100563 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +0100564 * If page allocator is not up yet then do not call c_p_a():
565 */
566 if (!debug_pagealloc_enabled)
567 return;
568
569 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100570 * The return value is ignored - the calls cannot fail,
571 * large pages are disabled at boot time:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 */
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100573 if (enable)
574 __set_pages_p(page, numpages);
575 else
576 __set_pages_np(page, numpages);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100577
578 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100579 * We should perform an IPI and flush all tlbs,
580 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 */
582 __flush_tlb_all();
583}
584#endif
Arjan van de Vend1028a12008-01-30 13:34:07 +0100585
586/*
587 * The testcases use internal knowledge of the implementation that shouldn't
588 * be exposed to the rest of the kernel. Include these directly here.
589 */
590#ifdef CONFIG_CPA_DEBUG
591#include "pageattr-test.c"
592#endif