blob: 456ad0ab9c7e07933fcaf5b4bcc95abfee6a5d04 [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{
Andi Kleen6bb83832008-02-04 16:48:06 +010055 unsigned long cache = (unsigned long)arg;
56
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010057 /*
58 * Flush all to work around Errata in early athlons regarding
59 * large page flushing.
60 */
61 __flush_tlb_all();
62
Andi Kleen6bb83832008-02-04 16:48:06 +010063 if (cache && boot_cpu_data.x86_model >= 4)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010064 wbinvd();
65}
66
Andi Kleen6bb83832008-02-04 16:48:06 +010067static void cpa_flush_all(unsigned long cache)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010068{
69 BUG_ON(irqs_disabled());
70
Andi Kleen6bb83832008-02-04 16:48:06 +010071 on_each_cpu(__cpa_flush_all, (void *) cache, 1, 1);
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010072}
73
Thomas Gleixner57a6a462008-01-30 13:34:08 +010074static void __cpa_flush_range(void *arg)
75{
Thomas Gleixner57a6a462008-01-30 13:34:08 +010076 /*
77 * We could optimize that further and do individual per page
78 * tlb invalidates for a low number of pages. Caveat: we must
79 * flush the high aliases on 64bit as well.
80 */
81 __flush_tlb_all();
Thomas Gleixner57a6a462008-01-30 13:34:08 +010082}
83
Andi Kleen6bb83832008-02-04 16:48:06 +010084static void cpa_flush_range(unsigned long start, int numpages, int cache)
Thomas Gleixner57a6a462008-01-30 13:34:08 +010085{
Ingo Molnar4c61afc2008-01-30 13:34:09 +010086 unsigned int i, level;
87 unsigned long addr;
88
Thomas Gleixner57a6a462008-01-30 13:34:08 +010089 BUG_ON(irqs_disabled());
Ingo Molnar4c61afc2008-01-30 13:34:09 +010090 WARN_ON(PAGE_ALIGN(start) != start);
Thomas Gleixner57a6a462008-01-30 13:34:08 +010091
Thomas Gleixner3b233e52008-01-30 13:34:08 +010092 on_each_cpu(__cpa_flush_range, NULL, 1, 1);
Thomas Gleixner57a6a462008-01-30 13:34:08 +010093
Andi Kleen6bb83832008-02-04 16:48:06 +010094 if (!cache)
95 return;
96
Thomas Gleixner3b233e52008-01-30 13:34:08 +010097 /*
98 * We only need to flush on one CPU,
99 * clflush is a MESI-coherent instruction that
100 * will cause all other CPUs to flush the same
101 * cachelines:
102 */
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100103 for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
104 pte_t *pte = lookup_address(addr, &level);
105
106 /*
107 * Only flush present addresses:
108 */
109 if (pte && pte_present(*pte))
110 clflush_cache_range((void *) addr, PAGE_SIZE);
111 }
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100112}
113
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100114#define HIGH_MAP_START __START_KERNEL_map
115#define HIGH_MAP_END (__START_KERNEL_map + KERNEL_TEXT_SIZE)
116
117
118/*
119 * Converts a virtual address to a X86-64 highmap address
120 */
121static unsigned long virt_to_highmap(void *address)
122{
123#ifdef CONFIG_X86_64
124 return __pa((unsigned long)address) + HIGH_MAP_START - phys_base;
125#else
126 return (unsigned long)address;
127#endif
128}
129
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100130/*
Arjan van de Vened724be2008-01-30 13:34:04 +0100131 * Certain areas of memory on x86 require very specific protection flags,
132 * for example the BIOS area or kernel text. Callers don't always get this
133 * right (again, ioremap() on BIOS memory is not uncommon) so this function
134 * checks and fixes these known static required protection bits.
135 */
136static inline pgprot_t static_protections(pgprot_t prot, unsigned long address)
137{
138 pgprot_t forbidden = __pgprot(0);
139
Ingo Molnar687c4822008-01-30 13:34:04 +0100140 /*
Arjan van de Vened724be2008-01-30 13:34:04 +0100141 * The BIOS area between 640k and 1Mb needs to be executable for
142 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
Ingo Molnar687c4822008-01-30 13:34:04 +0100143 */
Arjan van de Vened724be2008-01-30 13:34:04 +0100144 if (within(__pa(address), BIOS_BEGIN, BIOS_END))
145 pgprot_val(forbidden) |= _PAGE_NX;
146
147 /*
148 * The kernel text needs to be executable for obvious reasons
149 * Does not cover __inittext since that is gone later on
150 */
151 if (within(address, (unsigned long)_text, (unsigned long)_etext))
152 pgprot_val(forbidden) |= _PAGE_NX;
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100153 /*
154 * Do the same for the x86-64 high kernel mapping
155 */
156 if (within(address, virt_to_highmap(_text), virt_to_highmap(_etext)))
157 pgprot_val(forbidden) |= _PAGE_NX;
158
Arjan van de Vened724be2008-01-30 13:34:04 +0100159
160#ifdef CONFIG_DEBUG_RODATA
161 /* The .rodata section needs to be read-only */
162 if (within(address, (unsigned long)__start_rodata,
163 (unsigned long)__end_rodata))
164 pgprot_val(forbidden) |= _PAGE_RW;
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100165 /*
166 * Do the same for the x86-64 high kernel mapping
167 */
168 if (within(address, virt_to_highmap(__start_rodata),
169 virt_to_highmap(__end_rodata)))
170 pgprot_val(forbidden) |= _PAGE_RW;
Arjan van de Vened724be2008-01-30 13:34:04 +0100171#endif
172
173 prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
Ingo Molnar687c4822008-01-30 13:34:04 +0100174
175 return prot;
176}
177
Ingo Molnarf0646e42008-01-30 13:33:43 +0100178pte_t *lookup_address(unsigned long address, int *level)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100179{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 pgd_t *pgd = pgd_offset_k(address);
181 pud_t *pud;
182 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100183
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100184 *level = PG_LEVEL_NONE;
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (pgd_none(*pgd))
187 return NULL;
188 pud = pud_offset(pgd, address);
189 if (pud_none(*pud))
190 return NULL;
191 pmd = pmd_offset(pud, address);
192 if (pmd_none(*pmd))
193 return NULL;
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100194
195 *level = PG_LEVEL_2M;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (pmd_large(*pmd))
197 return (pte_t *)pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100199 *level = PG_LEVEL_4K;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100200 return pte_offset_kernel(pmd, address);
201}
202
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100203static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100204{
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100205 /* change init_mm */
206 set_pte_atomic(kpte, pte);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100207#ifdef CONFIG_X86_32
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100208 if (!SHARED_KERNEL_PMD) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100209 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Jeremy Fitzhardingee3ed9102008-01-30 13:34:11 +0100211 list_for_each_entry(page, &pgd_list, lru) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100212 pgd_t *pgd;
213 pud_t *pud;
214 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100215
Ingo Molnar44af6c42008-01-30 13:34:03 +0100216 pgd = (pgd_t *)page_address(page) + pgd_index(address);
217 pud = pud_offset(pgd, address);
218 pmd = pmd_offset(pud, address);
219 set_pte_atomic((pte_t *)pmd, pte);
220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
Ingo Molnar44af6c42008-01-30 13:34:03 +0100222#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100225static int split_large_page(pte_t *kpte, unsigned long address)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100226{
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100227 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnar12d6f212008-01-30 13:33:58 +0100228 gfp_t gfp_flags = GFP_KERNEL;
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100229 unsigned long flags, addr, pfn;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100230 pte_t *pbase, *tmp;
231 struct page *base;
Ingo Molnar86f03982008-01-30 13:34:09 +0100232 unsigned int i, level;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100233
Ingo Molnar12d6f212008-01-30 13:33:58 +0100234#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnar86f03982008-01-30 13:34:09 +0100235 gfp_flags = __GFP_HIGH | __GFP_NOFAIL | __GFP_NOWARN;
236 gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Ingo Molnar12d6f212008-01-30 13:33:58 +0100237#endif
238 base = alloc_pages(gfp_flags, 0);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100239 if (!base)
240 return -ENOMEM;
241
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100242 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100243 /*
244 * Check for races, another CPU might have split this page
245 * up for us already:
246 */
247 tmp = lookup_address(address, &level);
Ingo Molnar5508a742008-01-30 13:33:56 +0100248 if (tmp != kpte) {
249 WARN_ON_ONCE(1);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100250 goto out_unlock;
Ingo Molnar5508a742008-01-30 13:33:56 +0100251 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100252
253 address = __pa(address);
254 addr = address & LARGE_PAGE_MASK;
255 pbase = (pte_t *)page_address(base);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100256#ifdef CONFIG_X86_32
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100257 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
Ingo Molnar44af6c42008-01-30 13:34:03 +0100258#endif
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100259
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100260 /*
261 * Get the target pfn from the original entry:
262 */
263 pfn = pte_pfn(*kpte);
264 for (i = 0; i < PTRS_PER_PTE; i++, pfn++)
265 set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100266
267 /*
Huang, Ying4c881ca2008-01-30 13:34:04 +0100268 * Install the new, split up pagetable. Important detail here:
269 *
270 * On Intel the NX bit of all levels must be cleared to make a
271 * page executable. See section 4.13.2 of Intel 64 and IA-32
272 * Architectures Software Developer's Manual).
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100273 */
Huang, Ying4c881ca2008-01-30 13:34:04 +0100274 ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100275 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100276 base = NULL;
277
278out_unlock:
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100279 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100280
281 if (base)
282 __free_pages(base, 0);
283
284 return 0;
285}
286
Ingo Molnar44af6c42008-01-30 13:34:03 +0100287static int
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100288__change_page_attr(unsigned long address, pgprot_t mask_set, pgprot_t mask_clr)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100289{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 struct page *kpte_page;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100291 int level, err = 0;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100292 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100294repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100295 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (!kpte)
297 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200300 BUG_ON(PageLRU(kpte_page));
301 BUG_ON(PageCompound(kpte_page));
302
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100303 if (level == PG_LEVEL_4K) {
Ingo Molnar86f03982008-01-30 13:34:09 +0100304 pte_t new_pte, old_pte = *kpte;
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100305 pgprot_t new_prot = pte_pgprot(old_pte);
306
307 if(!pte_val(old_pte)) {
308 WARN_ON_ONCE(1);
309 return -EINVAL;
310 }
Thomas Gleixnera72a08a2008-01-30 13:34:07 +0100311
Ingo Molnar86f03982008-01-30 13:34:09 +0100312 pgprot_val(new_prot) &= ~pgprot_val(mask_clr);
313 pgprot_val(new_prot) |= pgprot_val(mask_set);
314
315 new_prot = static_protections(new_prot, address);
316
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100317 /*
318 * We need to keep the pfn from the existing PTE,
319 * after all we're only going to change it's attributes
320 * not the memory it points to
321 */
322 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
Ingo Molnar86f03982008-01-30 13:34:09 +0100323 set_pte_atomic(kpte, new_pte);
324 } else {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100325 err = split_large_page(kpte, address);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100326 if (!err)
327 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100329 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100330}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Ingo Molnar44af6c42008-01-30 13:34:03 +0100332/**
333 * change_page_attr_addr - Change page table attributes in linear mapping
334 * @address: Virtual address in linear mapping.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100335 * @prot: New page table attribute (PAGE_*)
336 *
337 * Change page attributes of a page in the direct mapping. This is a variant
338 * of change_page_attr() that also works on memory holes that do not have
339 * mem_map entry (pfn_valid() is false).
340 *
341 * See change_page_attr() documentation for more details.
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100342 *
343 * Modules and drivers should use the set_memory_* APIs instead.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100344 */
345
Thomas Gleixner08797502008-01-30 13:34:09 +0100346
Ingo Molnar86f03982008-01-30 13:34:09 +0100347static int
348change_page_attr_addr(unsigned long address, pgprot_t mask_set,
Thomas Gleixner08797502008-01-30 13:34:09 +0100349 pgprot_t mask_clr)
Ingo Molnar44af6c42008-01-30 13:34:03 +0100350{
Thomas Gleixner08797502008-01-30 13:34:09 +0100351 int err;
Ingo Molnar44af6c42008-01-30 13:34:03 +0100352
Arjan van de Ven488fd992008-01-30 13:34:07 +0100353#ifdef CONFIG_X86_64
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100354 unsigned long phys_addr = __pa(address);
355
Arjan van de Ven488fd992008-01-30 13:34:07 +0100356 /*
Thomas Gleixner08797502008-01-30 13:34:09 +0100357 * If we are inside the high mapped kernel range, then we
358 * fixup the low mapping first. __va() returns the virtual
359 * address in the linear mapping:
Arjan van de Ven488fd992008-01-30 13:34:07 +0100360 */
Thomas Gleixner08797502008-01-30 13:34:09 +0100361 if (within(address, HIGH_MAP_START, HIGH_MAP_END))
362 address = (unsigned long) __va(phys_addr);
Arjan van de Ven488fd992008-01-30 13:34:07 +0100363#endif
364
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100365 err = __change_page_attr(address, mask_set, mask_clr);
Thomas Gleixner08797502008-01-30 13:34:09 +0100366 if (err)
367 return err;
368
369#ifdef CONFIG_X86_64
370 /*
371 * If the physical address is inside the kernel map, we need
372 * to touch the high mapped kernel as well:
373 */
374 if (within(phys_addr, 0, KERNEL_TEXT_SIZE)) {
375 /*
376 * Calc the high mapping address. See __phys_addr()
377 * for the non obvious details.
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100378 *
379 * Note that NX and other required permissions are
380 * checked in static_protections().
Thomas Gleixner08797502008-01-30 13:34:09 +0100381 */
382 address = phys_addr + HIGH_MAP_START - phys_base;
Thomas Gleixner08797502008-01-30 13:34:09 +0100383
384 /*
385 * Our high aliases are imprecise, because we check
386 * everything between 0 and KERNEL_TEXT_SIZE, so do
387 * not propagate lookup failures back to users:
388 */
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100389 __change_page_attr(address, mask_set, mask_clr);
Thomas Gleixner08797502008-01-30 13:34:09 +0100390 }
391#endif
Ingo Molnar44af6c42008-01-30 13:34:03 +0100392 return err;
393}
394
Thomas Gleixnerff314522008-01-30 13:34:08 +0100395static int __change_page_attr_set_clr(unsigned long addr, int numpages,
396 pgprot_t mask_set, pgprot_t mask_clr)
397{
Ingo Molnar86f03982008-01-30 13:34:09 +0100398 unsigned int i;
399 int ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100400
Ingo Molnar86f03982008-01-30 13:34:09 +0100401 for (i = 0; i < numpages ; i++, addr += PAGE_SIZE) {
402 ret = change_page_attr_addr(addr, mask_set, mask_clr);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100403 if (ret)
404 return ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100405 }
406
407 return 0;
408}
409
Andi Kleen6bb83832008-02-04 16:48:06 +0100410static inline int cache_attr(pgprot_t attr)
411{
412 return pgprot_val(attr) &
413 (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
414}
415
Thomas Gleixnerff314522008-01-30 13:34:08 +0100416static int change_page_attr_set_clr(unsigned long addr, int numpages,
417 pgprot_t mask_set, pgprot_t mask_clr)
418{
Andi Kleen6bb83832008-02-04 16:48:06 +0100419 int ret, cache;
Thomas Gleixner331e4062008-02-04 16:48:06 +0100420
421 /*
422 * Check, if we are requested to change a not supported
423 * feature:
424 */
425 mask_set = canon_pgprot(mask_set);
426 mask_clr = canon_pgprot(mask_clr);
427 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr))
428 return 0;
429
430 ret = __change_page_attr_set_clr(addr, numpages, mask_set, mask_clr);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100431
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100432 /*
Andi Kleen6bb83832008-02-04 16:48:06 +0100433 * No need to flush, when we did not set any of the caching
434 * attributes:
435 */
436 cache = cache_attr(mask_set);
437
438 /*
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100439 * On success we use clflush, when the CPU supports it to
440 * avoid the wbindv. If the CPU does not support it and in the
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100441 * error case we fall back to cpa_flush_all (which uses
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100442 * wbindv):
443 */
444 if (!ret && cpu_has_clflush)
Andi Kleen6bb83832008-02-04 16:48:06 +0100445 cpa_flush_range(addr, numpages, cache);
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100446 else
Andi Kleen6bb83832008-02-04 16:48:06 +0100447 cpa_flush_all(cache);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100448
449 return ret;
450}
451
Thomas Gleixner56744542008-01-30 13:34:08 +0100452static inline int change_page_attr_set(unsigned long addr, int numpages,
453 pgprot_t mask)
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100454{
Thomas Gleixner56744542008-01-30 13:34:08 +0100455 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100456}
457
Thomas Gleixner56744542008-01-30 13:34:08 +0100458static inline int change_page_attr_clear(unsigned long addr, int numpages,
459 pgprot_t mask)
Thomas Gleixner72932c72008-01-30 13:34:08 +0100460{
Huang, Ying58270402008-01-31 22:05:43 +0100461 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100462}
463
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100464int set_memory_uc(unsigned long addr, int numpages)
465{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100466 return change_page_attr_set(addr, numpages,
467 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100468}
469EXPORT_SYMBOL(set_memory_uc);
470
471int set_memory_wb(unsigned long addr, int numpages)
472{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100473 return change_page_attr_clear(addr, numpages,
474 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100475}
476EXPORT_SYMBOL(set_memory_wb);
477
478int set_memory_x(unsigned long addr, int numpages)
479{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100480 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100481}
482EXPORT_SYMBOL(set_memory_x);
483
484int set_memory_nx(unsigned long addr, int numpages)
485{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100486 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100487}
488EXPORT_SYMBOL(set_memory_nx);
489
490int set_memory_ro(unsigned long addr, int numpages)
491{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100492 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100493}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100494
495int set_memory_rw(unsigned long addr, int numpages)
496{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100497 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100498}
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100499
500int set_memory_np(unsigned long addr, int numpages)
501{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100502 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100503}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100504
505int set_pages_uc(struct page *page, int numpages)
506{
507 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100508
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100509 return set_memory_uc(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100510}
511EXPORT_SYMBOL(set_pages_uc);
512
513int set_pages_wb(struct page *page, int numpages)
514{
515 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100516
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100517 return set_memory_wb(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100518}
519EXPORT_SYMBOL(set_pages_wb);
520
521int set_pages_x(struct page *page, int numpages)
522{
523 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100524
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100525 return set_memory_x(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100526}
527EXPORT_SYMBOL(set_pages_x);
528
529int set_pages_nx(struct page *page, int numpages)
530{
531 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100532
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100533 return set_memory_nx(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100534}
535EXPORT_SYMBOL(set_pages_nx);
536
537int set_pages_ro(struct page *page, int numpages)
538{
539 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100540
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100541 return set_memory_ro(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100542}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100543
544int set_pages_rw(struct page *page, int numpages)
545{
546 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100547
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100548 return set_memory_rw(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100549}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Thomas Gleixner56744542008-01-30 13:34:08 +0100552#if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_CPA_DEBUG)
553static inline int __change_page_attr_set(unsigned long addr, int numpages,
554 pgprot_t mask)
555{
556 return __change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
557}
558
559static inline int __change_page_attr_clear(unsigned long addr, int numpages,
560 pgprot_t mask)
561{
562 return __change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
563}
564#endif
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100567
568static int __set_pages_p(struct page *page, int numpages)
569{
570 unsigned long addr = (unsigned long)page_address(page);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100571
572 return __change_page_attr_set(addr, numpages,
573 __pgprot(_PAGE_PRESENT | _PAGE_RW));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100574}
575
576static int __set_pages_np(struct page *page, int numpages)
577{
578 unsigned long addr = (unsigned long)page_address(page);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100579
580 return __change_page_attr_clear(addr, numpages,
581 __pgprot(_PAGE_PRESENT));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100582}
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584void kernel_map_pages(struct page *page, int numpages, int enable)
585{
586 if (PageHighMem(page))
587 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100588 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700589 debug_check_no_locks_freed(page_address(page),
590 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100591 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800592
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100593 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +0100594 * If page allocator is not up yet then do not call c_p_a():
595 */
596 if (!debug_pagealloc_enabled)
597 return;
598
599 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100600 * The return value is ignored - the calls cannot fail,
601 * large pages are disabled at boot time:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100603 if (enable)
604 __set_pages_p(page, numpages);
605 else
606 __set_pages_np(page, numpages);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100607
608 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100609 * We should perform an IPI and flush all tlbs,
610 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 */
612 __flush_tlb_all();
613}
614#endif
Arjan van de Vend1028a12008-01-30 13:34:07 +0100615
616/*
617 * The testcases use internal knowledge of the implementation that shouldn't
618 * be exposed to the rest of the kernel. Include these directly here.
619 */
620#ifdef CONFIG_CPA_DEBUG
621#include "pageattr-test.c"
622#endif