blob: bf5e33f6a32235c58b71f2073d5781865f5ddf6c [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 Molnar5508a7482008-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 Molnar5508a7482008-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
Ingo Molnar86f03982008-01-30 13:34:09 +0100280__change_page_attr(unsigned long address, unsigned long pfn,
281 pgprot_t mask_set, pgprot_t mask_clr)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100282{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 struct page *kpte_page;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100284 int level, err = 0;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100285 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Ingo Molnar81922062008-01-30 13:34:04 +0100287#ifdef CONFIG_X86_32
288 BUG_ON(pfn > max_low_pfn);
289#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100291repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100292 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (!kpte)
294 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200297 BUG_ON(PageLRU(kpte_page));
298 BUG_ON(PageCompound(kpte_page));
299
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100300 if (level == PG_LEVEL_4K) {
Ingo Molnar86f03982008-01-30 13:34:09 +0100301 pgprot_t new_prot = pte_pgprot(*kpte);
302 pte_t new_pte, old_pte = *kpte;
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
309 new_pte = pfn_pte(pfn, canon_pgprot(new_prot));
310 BUG_ON(pte_pfn(new_pte) != pte_pfn(old_pte));
311
312 set_pte_atomic(kpte, new_pte);
313 } else {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100314 err = split_large_page(kpte, address);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100315 if (!err)
316 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100318 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100319}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Ingo Molnar44af6c42008-01-30 13:34:03 +0100321/**
322 * change_page_attr_addr - Change page table attributes in linear mapping
323 * @address: Virtual address in linear mapping.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100324 * @prot: New page table attribute (PAGE_*)
325 *
326 * Change page attributes of a page in the direct mapping. This is a variant
327 * of change_page_attr() that also works on memory holes that do not have
328 * mem_map entry (pfn_valid() is false).
329 *
330 * See change_page_attr() documentation for more details.
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100331 *
332 * Modules and drivers should use the set_memory_* APIs instead.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100333 */
334
Thomas Gleixner08797502008-01-30 13:34:09 +0100335
Ingo Molnar86f03982008-01-30 13:34:09 +0100336static int
337change_page_attr_addr(unsigned long address, pgprot_t mask_set,
Thomas Gleixner08797502008-01-30 13:34:09 +0100338 pgprot_t mask_clr)
Ingo Molnar44af6c42008-01-30 13:34:03 +0100339{
Thomas Gleixner08797502008-01-30 13:34:09 +0100340 unsigned long phys_addr = __pa(address);
341 unsigned long pfn = phys_addr >> PAGE_SHIFT;
342 int err;
Ingo Molnar44af6c42008-01-30 13:34:03 +0100343
Arjan van de Ven488fd992008-01-30 13:34:07 +0100344#ifdef CONFIG_X86_64
345 /*
Thomas Gleixner08797502008-01-30 13:34:09 +0100346 * If we are inside the high mapped kernel range, then we
347 * fixup the low mapping first. __va() returns the virtual
348 * address in the linear mapping:
Arjan van de Ven488fd992008-01-30 13:34:07 +0100349 */
Thomas Gleixner08797502008-01-30 13:34:09 +0100350 if (within(address, HIGH_MAP_START, HIGH_MAP_END))
351 address = (unsigned long) __va(phys_addr);
Arjan van de Ven488fd992008-01-30 13:34:07 +0100352#endif
353
Thomas Gleixner08797502008-01-30 13:34:09 +0100354 err = __change_page_attr(address, pfn, mask_set, mask_clr);
355 if (err)
356 return err;
357
358#ifdef CONFIG_X86_64
359 /*
360 * If the physical address is inside the kernel map, we need
361 * to touch the high mapped kernel as well:
362 */
363 if (within(phys_addr, 0, KERNEL_TEXT_SIZE)) {
364 /*
365 * Calc the high mapping address. See __phys_addr()
366 * for the non obvious details.
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100367 *
368 * Note that NX and other required permissions are
369 * checked in static_protections().
Thomas Gleixner08797502008-01-30 13:34:09 +0100370 */
371 address = phys_addr + HIGH_MAP_START - phys_base;
Thomas Gleixner08797502008-01-30 13:34:09 +0100372
373 /*
374 * Our high aliases are imprecise, because we check
375 * everything between 0 and KERNEL_TEXT_SIZE, so do
376 * not propagate lookup failures back to users:
377 */
378 __change_page_attr(address, pfn, mask_set, mask_clr);
379 }
380#endif
Ingo Molnar44af6c42008-01-30 13:34:03 +0100381 return err;
382}
383
Thomas Gleixnerff314522008-01-30 13:34:08 +0100384static int __change_page_attr_set_clr(unsigned long addr, int numpages,
385 pgprot_t mask_set, pgprot_t mask_clr)
386{
Ingo Molnar86f03982008-01-30 13:34:09 +0100387 unsigned int i;
388 int ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100389
Ingo Molnar86f03982008-01-30 13:34:09 +0100390 for (i = 0; i < numpages ; i++, addr += PAGE_SIZE) {
391 ret = change_page_attr_addr(addr, mask_set, mask_clr);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100392 if (ret)
393 return ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100394 }
395
396 return 0;
397}
398
399static int change_page_attr_set_clr(unsigned long addr, int numpages,
400 pgprot_t mask_set, pgprot_t mask_clr)
401{
402 int ret = __change_page_attr_set_clr(addr, numpages, mask_set,
403 mask_clr);
404
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100405 /*
406 * On success we use clflush, when the CPU supports it to
407 * avoid the wbindv. If the CPU does not support it and in the
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100408 * error case we fall back to cpa_flush_all (which uses
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100409 * wbindv):
410 */
411 if (!ret && cpu_has_clflush)
412 cpa_flush_range(addr, numpages);
413 else
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100414 cpa_flush_all();
Thomas Gleixnerff314522008-01-30 13:34:08 +0100415
416 return ret;
417}
418
Thomas Gleixner56744542008-01-30 13:34:08 +0100419static inline int change_page_attr_set(unsigned long addr, int numpages,
420 pgprot_t mask)
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100421{
Thomas Gleixner56744542008-01-30 13:34:08 +0100422 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100423}
424
Thomas Gleixner56744542008-01-30 13:34:08 +0100425static inline int change_page_attr_clear(unsigned long addr, int numpages,
426 pgprot_t mask)
Thomas Gleixner72932c72008-01-30 13:34:08 +0100427{
Huang, Ying58270402008-01-31 22:05:43 +0100428 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100429}
430
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100431int set_memory_uc(unsigned long addr, int numpages)
432{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100433 return change_page_attr_set(addr, numpages,
434 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100435}
436EXPORT_SYMBOL(set_memory_uc);
437
438int set_memory_wb(unsigned long addr, int numpages)
439{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100440 return change_page_attr_clear(addr, numpages,
441 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100442}
443EXPORT_SYMBOL(set_memory_wb);
444
445int set_memory_x(unsigned long addr, int numpages)
446{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100447 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100448}
449EXPORT_SYMBOL(set_memory_x);
450
451int set_memory_nx(unsigned long addr, int numpages)
452{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100453 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100454}
455EXPORT_SYMBOL(set_memory_nx);
456
457int set_memory_ro(unsigned long addr, int numpages)
458{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100459 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100460}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100461
462int set_memory_rw(unsigned long addr, int numpages)
463{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100464 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100465}
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100466
467int set_memory_np(unsigned long addr, int numpages)
468{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100469 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100470}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100471
472int set_pages_uc(struct page *page, int numpages)
473{
474 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100475
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100476 return set_memory_uc(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100477}
478EXPORT_SYMBOL(set_pages_uc);
479
480int set_pages_wb(struct page *page, int numpages)
481{
482 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100483
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100484 return set_memory_wb(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100485}
486EXPORT_SYMBOL(set_pages_wb);
487
488int set_pages_x(struct page *page, int numpages)
489{
490 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100491
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100492 return set_memory_x(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100493}
494EXPORT_SYMBOL(set_pages_x);
495
496int set_pages_nx(struct page *page, int numpages)
497{
498 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100499
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100500 return set_memory_nx(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100501}
502EXPORT_SYMBOL(set_pages_nx);
503
504int set_pages_ro(struct page *page, int numpages)
505{
506 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100507
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100508 return set_memory_ro(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100509}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100510
511int set_pages_rw(struct page *page, int numpages)
512{
513 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100514
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100515 return set_memory_rw(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100516}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Thomas Gleixner56744542008-01-30 13:34:08 +0100519#if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_CPA_DEBUG)
520static inline int __change_page_attr_set(unsigned long addr, int numpages,
521 pgprot_t mask)
522{
523 return __change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
524}
525
526static inline int __change_page_attr_clear(unsigned long addr, int numpages,
527 pgprot_t mask)
528{
529 return __change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
530}
531#endif
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100534
535static int __set_pages_p(struct page *page, int numpages)
536{
537 unsigned long addr = (unsigned long)page_address(page);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100538
539 return __change_page_attr_set(addr, numpages,
540 __pgprot(_PAGE_PRESENT | _PAGE_RW));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100541}
542
543static int __set_pages_np(struct page *page, int numpages)
544{
545 unsigned long addr = (unsigned long)page_address(page);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100546
547 return __change_page_attr_clear(addr, numpages,
548 __pgprot(_PAGE_PRESENT));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100549}
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551void kernel_map_pages(struct page *page, int numpages, int enable)
552{
553 if (PageHighMem(page))
554 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100555 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700556 debug_check_no_locks_freed(page_address(page),
557 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100558 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800559
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100560 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +0100561 * If page allocator is not up yet then do not call c_p_a():
562 */
563 if (!debug_pagealloc_enabled)
564 return;
565
566 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100567 * The return value is ignored - the calls cannot fail,
568 * large pages are disabled at boot time:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 */
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100570 if (enable)
571 __set_pages_p(page, numpages);
572 else
573 __set_pages_np(page, numpages);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100574
575 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100576 * We should perform an IPI and flush all tlbs,
577 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 */
579 __flush_tlb_all();
580}
581#endif
Arjan van de Vend1028a12008-01-30 13:34:07 +0100582
583/*
584 * The testcases use internal knowledge of the implementation that shouldn't
585 * be exposed to the rest of the kernel. Include these directly here.
586 */
587#ifdef CONFIG_CPA_DEBUG
588#include "pageattr-test.c"
589#endif