blob: d1c08308ecbbba955d4b29c266f01ff96f8b55aa [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;
21 int numpages;
22 pgprot_t mask_set;
23 pgprot_t mask_clr;
24};
25
Arjan van de Vened724be2008-01-30 13:34:04 +010026static inline int
27within(unsigned long addr, unsigned long start, unsigned long end)
Ingo Molnar687c4822008-01-30 13:34:04 +010028{
Arjan van de Vened724be2008-01-30 13:34:04 +010029 return addr >= start && addr < end;
30}
31
32/*
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010033 * Flushing functions
34 */
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010035
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010036/**
37 * clflush_cache_range - flush a cache range with clflush
38 * @addr: virtual start address
39 * @size: number of bytes to flush
40 *
41 * clflush is an unordered instruction which needs fencing with mfence
42 * to avoid ordering issues.
43 */
Ingo Molnar4c61afc2008-01-30 13:34:09 +010044void clflush_cache_range(void *vaddr, unsigned int size)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010045{
Ingo Molnar4c61afc2008-01-30 13:34:09 +010046 void *vend = vaddr + size - 1;
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010047
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010048 mb();
Ingo Molnar4c61afc2008-01-30 13:34:09 +010049
50 for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
51 clflush(vaddr);
52 /*
53 * Flush any possible final partial cacheline:
54 */
55 clflush(vend);
56
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010057 mb();
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010058}
59
Thomas Gleixneraf1e6842008-01-30 13:34:08 +010060static void __cpa_flush_all(void *arg)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010061{
Andi Kleen6bb83832008-02-04 16:48:06 +010062 unsigned long cache = (unsigned long)arg;
63
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010064 /*
65 * Flush all to work around Errata in early athlons regarding
66 * large page flushing.
67 */
68 __flush_tlb_all();
69
Andi Kleen6bb83832008-02-04 16:48:06 +010070 if (cache && boot_cpu_data.x86_model >= 4)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010071 wbinvd();
72}
73
Andi Kleen6bb83832008-02-04 16:48:06 +010074static void cpa_flush_all(unsigned long cache)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010075{
76 BUG_ON(irqs_disabled());
77
Andi Kleen6bb83832008-02-04 16:48:06 +010078 on_each_cpu(__cpa_flush_all, (void *) cache, 1, 1);
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010079}
80
Thomas Gleixner57a6a462008-01-30 13:34:08 +010081static void __cpa_flush_range(void *arg)
82{
Thomas Gleixner57a6a462008-01-30 13:34:08 +010083 /*
84 * We could optimize that further and do individual per page
85 * tlb invalidates for a low number of pages. Caveat: we must
86 * flush the high aliases on 64bit as well.
87 */
88 __flush_tlb_all();
Thomas Gleixner57a6a462008-01-30 13:34:08 +010089}
90
Andi Kleen6bb83832008-02-04 16:48:06 +010091static void cpa_flush_range(unsigned long start, int numpages, int cache)
Thomas Gleixner57a6a462008-01-30 13:34:08 +010092{
Ingo Molnar4c61afc2008-01-30 13:34:09 +010093 unsigned int i, level;
94 unsigned long addr;
95
Thomas Gleixner57a6a462008-01-30 13:34:08 +010096 BUG_ON(irqs_disabled());
Ingo Molnar4c61afc2008-01-30 13:34:09 +010097 WARN_ON(PAGE_ALIGN(start) != start);
Thomas Gleixner57a6a462008-01-30 13:34:08 +010098
Thomas Gleixner3b233e52008-01-30 13:34:08 +010099 on_each_cpu(__cpa_flush_range, NULL, 1, 1);
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100100
Andi Kleen6bb83832008-02-04 16:48:06 +0100101 if (!cache)
102 return;
103
Thomas Gleixner3b233e52008-01-30 13:34:08 +0100104 /*
105 * We only need to flush on one CPU,
106 * clflush is a MESI-coherent instruction that
107 * will cause all other CPUs to flush the same
108 * cachelines:
109 */
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100110 for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
111 pte_t *pte = lookup_address(addr, &level);
112
113 /*
114 * Only flush present addresses:
115 */
116 if (pte && pte_present(*pte))
117 clflush_cache_range((void *) addr, PAGE_SIZE);
118 }
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100119}
120
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100121#define HIGH_MAP_START __START_KERNEL_map
122#define HIGH_MAP_END (__START_KERNEL_map + KERNEL_TEXT_SIZE)
123
124
125/*
126 * Converts a virtual address to a X86-64 highmap address
127 */
128static unsigned long virt_to_highmap(void *address)
129{
130#ifdef CONFIG_X86_64
131 return __pa((unsigned long)address) + HIGH_MAP_START - phys_base;
132#else
133 return (unsigned long)address;
134#endif
135}
136
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100137/*
Arjan van de Vened724be2008-01-30 13:34:04 +0100138 * Certain areas of memory on x86 require very specific protection flags,
139 * for example the BIOS area or kernel text. Callers don't always get this
140 * right (again, ioremap() on BIOS memory is not uncommon) so this function
141 * checks and fixes these known static required protection bits.
142 */
143static inline pgprot_t static_protections(pgprot_t prot, unsigned long address)
144{
145 pgprot_t forbidden = __pgprot(0);
146
Ingo Molnar687c4822008-01-30 13:34:04 +0100147 /*
Arjan van de Vened724be2008-01-30 13:34:04 +0100148 * The BIOS area between 640k and 1Mb needs to be executable for
149 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
Ingo Molnar687c4822008-01-30 13:34:04 +0100150 */
Arjan van de Vened724be2008-01-30 13:34:04 +0100151 if (within(__pa(address), BIOS_BEGIN, BIOS_END))
152 pgprot_val(forbidden) |= _PAGE_NX;
153
154 /*
155 * The kernel text needs to be executable for obvious reasons
156 * Does not cover __inittext since that is gone later on
157 */
158 if (within(address, (unsigned long)_text, (unsigned long)_etext))
159 pgprot_val(forbidden) |= _PAGE_NX;
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(_text), virt_to_highmap(_etext)))
164 pgprot_val(forbidden) |= _PAGE_NX;
165
Arjan van de Vened724be2008-01-30 13:34:04 +0100166
167#ifdef CONFIG_DEBUG_RODATA
168 /* The .rodata section needs to be read-only */
169 if (within(address, (unsigned long)__start_rodata,
170 (unsigned long)__end_rodata))
171 pgprot_val(forbidden) |= _PAGE_RW;
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100172 /*
173 * Do the same for the x86-64 high kernel mapping
174 */
175 if (within(address, virt_to_highmap(__start_rodata),
176 virt_to_highmap(__end_rodata)))
177 pgprot_val(forbidden) |= _PAGE_RW;
Arjan van de Vened724be2008-01-30 13:34:04 +0100178#endif
179
180 prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
Ingo Molnar687c4822008-01-30 13:34:04 +0100181
182 return prot;
183}
184
Ingo Molnarf0646e42008-01-30 13:33:43 +0100185pte_t *lookup_address(unsigned long address, int *level)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100186{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 pgd_t *pgd = pgd_offset_k(address);
188 pud_t *pud;
189 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100190
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100191 *level = PG_LEVEL_NONE;
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (pgd_none(*pgd))
194 return NULL;
195 pud = pud_offset(pgd, address);
196 if (pud_none(*pud))
197 return NULL;
198 pmd = pmd_offset(pud, address);
199 if (pmd_none(*pmd))
200 return NULL;
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100201
202 *level = PG_LEVEL_2M;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (pmd_large(*pmd))
204 return (pte_t *)pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100206 *level = PG_LEVEL_4K;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100207 return pte_offset_kernel(pmd, address);
208}
209
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100210static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100211{
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100212 /* change init_mm */
213 set_pte_atomic(kpte, pte);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100214#ifdef CONFIG_X86_32
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100215 if (!SHARED_KERNEL_PMD) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100216 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Jeremy Fitzhardingee3ed9102008-01-30 13:34:11 +0100218 list_for_each_entry(page, &pgd_list, lru) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100219 pgd_t *pgd;
220 pud_t *pud;
221 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100222
Ingo Molnar44af6c42008-01-30 13:34:03 +0100223 pgd = (pgd_t *)page_address(page) + pgd_index(address);
224 pud = pud_offset(pgd, address);
225 pmd = pmd_offset(pud, address);
226 set_pte_atomic((pte_t *)pmd, pte);
227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
Ingo Molnar44af6c42008-01-30 13:34:03 +0100229#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100232static int split_large_page(pte_t *kpte, unsigned long address)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100233{
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100234 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnar12d6f212008-01-30 13:33:58 +0100235 gfp_t gfp_flags = GFP_KERNEL;
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100236 unsigned long flags, addr, pfn;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100237 pte_t *pbase, *tmp;
238 struct page *base;
Ingo Molnar86f03982008-01-30 13:34:09 +0100239 unsigned int i, level;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100240
Ingo Molnar12d6f212008-01-30 13:33:58 +0100241#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnar86f03982008-01-30 13:34:09 +0100242 gfp_flags = __GFP_HIGH | __GFP_NOFAIL | __GFP_NOWARN;
243 gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Ingo Molnar12d6f212008-01-30 13:33:58 +0100244#endif
245 base = alloc_pages(gfp_flags, 0);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100246 if (!base)
247 return -ENOMEM;
248
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100249 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100250 /*
251 * Check for races, another CPU might have split this page
252 * up for us already:
253 */
254 tmp = lookup_address(address, &level);
Ingo Molnar5508a742008-01-30 13:33:56 +0100255 if (tmp != kpte) {
256 WARN_ON_ONCE(1);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100257 goto out_unlock;
Ingo Molnar5508a742008-01-30 13:33:56 +0100258 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100259
260 address = __pa(address);
261 addr = address & LARGE_PAGE_MASK;
262 pbase = (pte_t *)page_address(base);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100263#ifdef CONFIG_X86_32
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100264 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
Ingo Molnar44af6c42008-01-30 13:34:03 +0100265#endif
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100266
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100267 /*
268 * Get the target pfn from the original entry:
269 */
270 pfn = pte_pfn(*kpte);
271 for (i = 0; i < PTRS_PER_PTE; i++, pfn++)
272 set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100273
274 /*
Huang, Ying4c881ca2008-01-30 13:34:04 +0100275 * Install the new, split up pagetable. Important detail here:
276 *
277 * On Intel the NX bit of all levels must be cleared to make a
278 * page executable. See section 4.13.2 of Intel 64 and IA-32
279 * Architectures Software Developer's Manual).
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100280 */
Huang, Ying4c881ca2008-01-30 13:34:04 +0100281 ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100282 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100283 base = NULL;
284
285out_unlock:
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100286 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100287
288 if (base)
289 __free_pages(base, 0);
290
291 return 0;
292}
293
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100294static int __change_page_attr(unsigned long address, struct cpa_data *cpa)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100295{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 struct page *kpte_page;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100297 int level, err = 0;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100298 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100300repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100301 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (!kpte)
303 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200306 BUG_ON(PageLRU(kpte_page));
307 BUG_ON(PageCompound(kpte_page));
308
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100309 if (level == PG_LEVEL_4K) {
Ingo Molnar86f03982008-01-30 13:34:09 +0100310 pte_t new_pte, old_pte = *kpte;
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100311 pgprot_t new_prot = pte_pgprot(old_pte);
312
313 if(!pte_val(old_pte)) {
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100314 printk(KERN_WARNING "CPA: called for zero pte. "
315 "vaddr = %lx cpa->vaddr = %lx\n", address,
316 cpa->vaddr);
317 WARN_ON(1);
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100318 return -EINVAL;
319 }
Thomas Gleixnera72a08a2008-01-30 13:34:07 +0100320
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100321 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
322 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
Ingo Molnar86f03982008-01-30 13:34:09 +0100323
324 new_prot = static_protections(new_prot, address);
325
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100326 /*
327 * We need to keep the pfn from the existing PTE,
328 * after all we're only going to change it's attributes
329 * not the memory it points to
330 */
331 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
Ingo Molnar86f03982008-01-30 13:34:09 +0100332 set_pte_atomic(kpte, new_pte);
333 } else {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100334 err = split_large_page(kpte, address);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100335 if (!err)
336 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100338 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100339}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Ingo Molnar44af6c42008-01-30 13:34:03 +0100341/**
342 * change_page_attr_addr - Change page table attributes in linear mapping
343 * @address: Virtual address in linear mapping.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100344 * @prot: New page table attribute (PAGE_*)
345 *
346 * Change page attributes of a page in the direct mapping. This is a variant
347 * of change_page_attr() that also works on memory holes that do not have
348 * mem_map entry (pfn_valid() is false).
349 *
350 * See change_page_attr() documentation for more details.
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100351 *
352 * Modules and drivers should use the set_memory_* APIs instead.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100353 */
354
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100355static int change_page_attr_addr(struct cpa_data *cpa)
Ingo Molnar44af6c42008-01-30 13:34:03 +0100356{
Thomas Gleixner08797502008-01-30 13:34:09 +0100357 int err;
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100358 unsigned long address = cpa->vaddr;
Ingo Molnar44af6c42008-01-30 13:34:03 +0100359
Arjan van de Ven488fd992008-01-30 13:34:07 +0100360#ifdef CONFIG_X86_64
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100361 unsigned long phys_addr = __pa(address);
362
Arjan van de Ven488fd992008-01-30 13:34:07 +0100363 /*
Thomas Gleixner08797502008-01-30 13:34:09 +0100364 * If we are inside the high mapped kernel range, then we
365 * fixup the low mapping first. __va() returns the virtual
366 * address in the linear mapping:
Arjan van de Ven488fd992008-01-30 13:34:07 +0100367 */
Thomas Gleixner08797502008-01-30 13:34:09 +0100368 if (within(address, HIGH_MAP_START, HIGH_MAP_END))
369 address = (unsigned long) __va(phys_addr);
Arjan van de Ven488fd992008-01-30 13:34:07 +0100370#endif
371
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100372 err = __change_page_attr(address, cpa);
Thomas Gleixner08797502008-01-30 13:34:09 +0100373 if (err)
374 return err;
375
376#ifdef CONFIG_X86_64
377 /*
378 * If the physical address is inside the kernel map, we need
379 * to touch the high mapped kernel as well:
380 */
381 if (within(phys_addr, 0, KERNEL_TEXT_SIZE)) {
382 /*
383 * Calc the high mapping address. See __phys_addr()
384 * for the non obvious details.
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100385 *
386 * Note that NX and other required permissions are
387 * checked in static_protections().
Thomas Gleixner08797502008-01-30 13:34:09 +0100388 */
389 address = phys_addr + HIGH_MAP_START - phys_base;
Thomas Gleixner08797502008-01-30 13:34:09 +0100390
391 /*
392 * Our high aliases are imprecise, because we check
393 * everything between 0 and KERNEL_TEXT_SIZE, so do
394 * not propagate lookup failures back to users:
395 */
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100396 __change_page_attr(address, cpa);
Thomas Gleixner08797502008-01-30 13:34:09 +0100397 }
398#endif
Ingo Molnar44af6c42008-01-30 13:34:03 +0100399 return err;
400}
401
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100402static int __change_page_attr_set_clr(struct cpa_data *cpa)
Thomas Gleixnerff314522008-01-30 13:34:08 +0100403{
Ingo Molnar86f03982008-01-30 13:34:09 +0100404 unsigned int i;
405 int ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100406
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100407 for (i = 0; i < cpa->numpages ; i++, cpa->vaddr += PAGE_SIZE) {
408 ret = change_page_attr_addr(cpa);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100409 if (ret)
410 return ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100411 }
412
413 return 0;
414}
415
Andi Kleen6bb83832008-02-04 16:48:06 +0100416static inline int cache_attr(pgprot_t attr)
417{
418 return pgprot_val(attr) &
419 (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
420}
421
Thomas Gleixnerff314522008-01-30 13:34:08 +0100422static int change_page_attr_set_clr(unsigned long addr, int numpages,
423 pgprot_t mask_set, pgprot_t mask_clr)
424{
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100425 struct cpa_data cpa;
Andi Kleen6bb83832008-02-04 16:48:06 +0100426 int ret, cache;
Thomas Gleixner331e4062008-02-04 16:48:06 +0100427
428 /*
429 * Check, if we are requested to change a not supported
430 * feature:
431 */
432 mask_set = canon_pgprot(mask_set);
433 mask_clr = canon_pgprot(mask_clr);
434 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr))
435 return 0;
436
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100437 cpa.vaddr = addr;
438 cpa.numpages = numpages;
439 cpa.mask_set = mask_set;
440 cpa.mask_clr = mask_clr;
441
442 ret = __change_page_attr_set_clr(&cpa);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100443
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100444 /*
Andi Kleen6bb83832008-02-04 16:48:06 +0100445 * No need to flush, when we did not set any of the caching
446 * attributes:
447 */
448 cache = cache_attr(mask_set);
449
450 /*
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100451 * On success we use clflush, when the CPU supports it to
452 * avoid the wbindv. If the CPU does not support it and in the
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100453 * error case we fall back to cpa_flush_all (which uses
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100454 * wbindv):
455 */
456 if (!ret && cpu_has_clflush)
Andi Kleen6bb83832008-02-04 16:48:06 +0100457 cpa_flush_range(addr, numpages, cache);
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100458 else
Andi Kleen6bb83832008-02-04 16:48:06 +0100459 cpa_flush_all(cache);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100460
461 return ret;
462}
463
Thomas Gleixner56744542008-01-30 13:34:08 +0100464static inline int change_page_attr_set(unsigned long addr, int numpages,
465 pgprot_t mask)
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100466{
Thomas Gleixner56744542008-01-30 13:34:08 +0100467 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100468}
469
Thomas Gleixner56744542008-01-30 13:34:08 +0100470static inline int change_page_attr_clear(unsigned long addr, int numpages,
471 pgprot_t mask)
Thomas Gleixner72932c72008-01-30 13:34:08 +0100472{
Huang, Ying58270402008-01-31 22:05:43 +0100473 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100474}
475
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100476int set_memory_uc(unsigned long addr, int numpages)
477{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100478 return change_page_attr_set(addr, numpages,
479 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100480}
481EXPORT_SYMBOL(set_memory_uc);
482
483int set_memory_wb(unsigned long addr, int numpages)
484{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100485 return change_page_attr_clear(addr, numpages,
486 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100487}
488EXPORT_SYMBOL(set_memory_wb);
489
490int set_memory_x(unsigned long addr, int numpages)
491{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100492 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100493}
494EXPORT_SYMBOL(set_memory_x);
495
496int set_memory_nx(unsigned long addr, int numpages)
497{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100498 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100499}
500EXPORT_SYMBOL(set_memory_nx);
501
502int set_memory_ro(unsigned long addr, int numpages)
503{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100504 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100505}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100506
507int set_memory_rw(unsigned long addr, int numpages)
508{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100509 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100510}
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100511
512int set_memory_np(unsigned long addr, int numpages)
513{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100514 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100515}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100516
517int set_pages_uc(struct page *page, int numpages)
518{
519 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100520
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100521 return set_memory_uc(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100522}
523EXPORT_SYMBOL(set_pages_uc);
524
525int set_pages_wb(struct page *page, int numpages)
526{
527 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100528
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100529 return set_memory_wb(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100530}
531EXPORT_SYMBOL(set_pages_wb);
532
533int set_pages_x(struct page *page, int numpages)
534{
535 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100536
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100537 return set_memory_x(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100538}
539EXPORT_SYMBOL(set_pages_x);
540
541int set_pages_nx(struct page *page, int numpages)
542{
543 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100544
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100545 return set_memory_nx(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100546}
547EXPORT_SYMBOL(set_pages_nx);
548
549int set_pages_ro(struct page *page, int numpages)
550{
551 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100552
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100553 return set_memory_ro(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100554}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100555
556int set_pages_rw(struct page *page, int numpages)
557{
558 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100559
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100560 return set_memory_rw(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100561}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100564
565static int __set_pages_p(struct page *page, int numpages)
566{
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100567 struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
568 .numpages = numpages,
569 .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
570 .mask_clr = __pgprot(0)};
Thomas Gleixner72932c72008-01-30 13:34:08 +0100571
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100572 return __change_page_attr_set_clr(&cpa);
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100573}
574
575static int __set_pages_np(struct page *page, int numpages)
576{
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100577 struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
578 .numpages = numpages,
579 .mask_set = __pgprot(0),
580 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW)};
Thomas Gleixner72932c72008-01-30 13:34:08 +0100581
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100582 return __change_page_attr_set_clr(&cpa);
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585void kernel_map_pages(struct page *page, int numpages, int enable)
586{
587 if (PageHighMem(page))
588 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100589 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700590 debug_check_no_locks_freed(page_address(page),
591 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100592 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800593
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100594 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +0100595 * If page allocator is not up yet then do not call c_p_a():
596 */
597 if (!debug_pagealloc_enabled)
598 return;
599
600 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100601 * The return value is ignored - the calls cannot fail,
602 * large pages are disabled at boot time:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 */
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100604 if (enable)
605 __set_pages_p(page, numpages);
606 else
607 __set_pages_np(page, numpages);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100608
609 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100610 * We should perform an IPI and flush all tlbs,
611 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 */
613 __flush_tlb_all();
614}
615#endif
Arjan van de Vend1028a12008-01-30 13:34:07 +0100616
617/*
618 * The testcases use internal knowledge of the implementation that shouldn't
619 * be exposed to the rest of the kernel. Include these directly here.
620 */
621#ifdef CONFIG_CPA_DEBUG
622#include "pageattr-test.c"
623#endif