blob: f60b93dc2e579794f8a7783a2fb2a3a5fb660c66 [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;
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100224 unsigned long flags, addr, pfn;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100225 pte_t *pbase, *tmp;
226 struct page *base;
Ingo Molnar86f03982008-01-30 13:34:09 +0100227 unsigned int i, level;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100228
Ingo Molnar12d6f212008-01-30 13:33:58 +0100229#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnar86f03982008-01-30 13:34:09 +0100230 gfp_flags = __GFP_HIGH | __GFP_NOFAIL | __GFP_NOWARN;
231 gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Ingo Molnar12d6f212008-01-30 13:33:58 +0100232#endif
233 base = alloc_pages(gfp_flags, 0);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100234 if (!base)
235 return -ENOMEM;
236
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100237 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100238 /*
239 * Check for races, another CPU might have split this page
240 * up for us already:
241 */
242 tmp = lookup_address(address, &level);
Ingo Molnar5508a7482008-01-30 13:33:56 +0100243 if (tmp != kpte) {
244 WARN_ON_ONCE(1);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100245 goto out_unlock;
Ingo Molnar5508a7482008-01-30 13:33:56 +0100246 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100247
248 address = __pa(address);
249 addr = address & LARGE_PAGE_MASK;
250 pbase = (pte_t *)page_address(base);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100251#ifdef CONFIG_X86_32
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100252 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
Ingo Molnar44af6c42008-01-30 13:34:03 +0100253#endif
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100254
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100255 /*
256 * Get the target pfn from the original entry:
257 */
258 pfn = pte_pfn(*kpte);
259 for (i = 0; i < PTRS_PER_PTE; i++, pfn++)
260 set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100261
262 /*
Huang, Ying4c881ca2008-01-30 13:34:04 +0100263 * Install the new, split up pagetable. Important detail here:
264 *
265 * On Intel the NX bit of all levels must be cleared to make a
266 * page executable. See section 4.13.2 of Intel 64 and IA-32
267 * Architectures Software Developer's Manual).
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100268 */
Huang, Ying4c881ca2008-01-30 13:34:04 +0100269 ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100270 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100271 base = NULL;
272
273out_unlock:
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100274 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100275
276 if (base)
277 __free_pages(base, 0);
278
279 return 0;
280}
281
Ingo Molnar44af6c42008-01-30 13:34:03 +0100282static int
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100283__change_page_attr(unsigned long address, pgprot_t mask_set, pgprot_t mask_clr)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100284{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct page *kpte_page;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100286 int level, err = 0;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100287 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100289repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100290 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (!kpte)
292 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200295 BUG_ON(PageLRU(kpte_page));
296 BUG_ON(PageCompound(kpte_page));
297
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100298 if (level == PG_LEVEL_4K) {
Ingo Molnar86f03982008-01-30 13:34:09 +0100299 pte_t new_pte, old_pte = *kpte;
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100300 pgprot_t new_prot = pte_pgprot(old_pte);
301
302 if(!pte_val(old_pte)) {
303 WARN_ON_ONCE(1);
304 return -EINVAL;
305 }
Thomas Gleixnera72a08a2008-01-30 13:34:07 +0100306
Ingo Molnar86f03982008-01-30 13:34:09 +0100307 pgprot_val(new_prot) &= ~pgprot_val(mask_clr);
308 pgprot_val(new_prot) |= pgprot_val(mask_set);
309
310 new_prot = static_protections(new_prot, address);
311
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100312 /*
313 * We need to keep the pfn from the existing PTE,
314 * after all we're only going to change it's attributes
315 * not the memory it points to
316 */
317 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
Ingo Molnar86f03982008-01-30 13:34:09 +0100318 set_pte_atomic(kpte, new_pte);
319 } else {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100320 err = split_large_page(kpte, address);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100321 if (!err)
322 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100324 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100325}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Ingo Molnar44af6c42008-01-30 13:34:03 +0100327/**
328 * change_page_attr_addr - Change page table attributes in linear mapping
329 * @address: Virtual address in linear mapping.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100330 * @prot: New page table attribute (PAGE_*)
331 *
332 * Change page attributes of a page in the direct mapping. This is a variant
333 * of change_page_attr() that also works on memory holes that do not have
334 * mem_map entry (pfn_valid() is false).
335 *
336 * See change_page_attr() documentation for more details.
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100337 *
338 * Modules and drivers should use the set_memory_* APIs instead.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100339 */
340
Thomas Gleixner08797502008-01-30 13:34:09 +0100341
Ingo Molnar86f03982008-01-30 13:34:09 +0100342static int
343change_page_attr_addr(unsigned long address, pgprot_t mask_set,
Thomas Gleixner08797502008-01-30 13:34:09 +0100344 pgprot_t mask_clr)
Ingo Molnar44af6c42008-01-30 13:34:03 +0100345{
Thomas Gleixner08797502008-01-30 13:34:09 +0100346 int err;
Ingo Molnar44af6c42008-01-30 13:34:03 +0100347
Arjan van de Ven488fd992008-01-30 13:34:07 +0100348#ifdef CONFIG_X86_64
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100349 unsigned long phys_addr = __pa(address);
350
Arjan van de Ven488fd992008-01-30 13:34:07 +0100351 /*
Thomas Gleixner08797502008-01-30 13:34:09 +0100352 * If we are inside the high mapped kernel range, then we
353 * fixup the low mapping first. __va() returns the virtual
354 * address in the linear mapping:
Arjan van de Ven488fd992008-01-30 13:34:07 +0100355 */
Thomas Gleixner08797502008-01-30 13:34:09 +0100356 if (within(address, HIGH_MAP_START, HIGH_MAP_END))
357 address = (unsigned long) __va(phys_addr);
Arjan van de Ven488fd992008-01-30 13:34:07 +0100358#endif
359
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100360 err = __change_page_attr(address, mask_set, mask_clr);
Thomas Gleixner08797502008-01-30 13:34:09 +0100361 if (err)
362 return err;
363
364#ifdef CONFIG_X86_64
365 /*
366 * If the physical address is inside the kernel map, we need
367 * to touch the high mapped kernel as well:
368 */
369 if (within(phys_addr, 0, KERNEL_TEXT_SIZE)) {
370 /*
371 * Calc the high mapping address. See __phys_addr()
372 * for the non obvious details.
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100373 *
374 * Note that NX and other required permissions are
375 * checked in static_protections().
Thomas Gleixner08797502008-01-30 13:34:09 +0100376 */
377 address = phys_addr + HIGH_MAP_START - phys_base;
Thomas Gleixner08797502008-01-30 13:34:09 +0100378
379 /*
380 * Our high aliases are imprecise, because we check
381 * everything between 0 and KERNEL_TEXT_SIZE, so do
382 * not propagate lookup failures back to users:
383 */
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100384 __change_page_attr(address, mask_set, mask_clr);
Thomas Gleixner08797502008-01-30 13:34:09 +0100385 }
386#endif
Ingo Molnar44af6c42008-01-30 13:34:03 +0100387 return err;
388}
389
Thomas Gleixnerff314522008-01-30 13:34:08 +0100390static int __change_page_attr_set_clr(unsigned long addr, int numpages,
391 pgprot_t mask_set, pgprot_t mask_clr)
392{
Ingo Molnar86f03982008-01-30 13:34:09 +0100393 unsigned int i;
394 int ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100395
Ingo Molnar86f03982008-01-30 13:34:09 +0100396 for (i = 0; i < numpages ; i++, addr += PAGE_SIZE) {
397 ret = change_page_attr_addr(addr, mask_set, mask_clr);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100398 if (ret)
399 return ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100400 }
401
402 return 0;
403}
404
405static int change_page_attr_set_clr(unsigned long addr, int numpages,
406 pgprot_t mask_set, pgprot_t mask_clr)
407{
Thomas Gleixner331e4062008-02-04 16:48:06 +0100408 int ret;
409
410 /*
411 * Check, if we are requested to change a not supported
412 * feature:
413 */
414 mask_set = canon_pgprot(mask_set);
415 mask_clr = canon_pgprot(mask_clr);
416 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr))
417 return 0;
418
419 ret = __change_page_attr_set_clr(addr, numpages, mask_set, mask_clr);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100420
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100421 /*
422 * On success we use clflush, when the CPU supports it to
423 * avoid the wbindv. If the CPU does not support it and in the
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100424 * error case we fall back to cpa_flush_all (which uses
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100425 * wbindv):
426 */
427 if (!ret && cpu_has_clflush)
428 cpa_flush_range(addr, numpages);
429 else
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100430 cpa_flush_all();
Thomas Gleixnerff314522008-01-30 13:34:08 +0100431
432 return ret;
433}
434
Thomas Gleixner56744542008-01-30 13:34:08 +0100435static inline int change_page_attr_set(unsigned long addr, int numpages,
436 pgprot_t mask)
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100437{
Thomas Gleixner56744542008-01-30 13:34:08 +0100438 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100439}
440
Thomas Gleixner56744542008-01-30 13:34:08 +0100441static inline int change_page_attr_clear(unsigned long addr, int numpages,
442 pgprot_t mask)
Thomas Gleixner72932c72008-01-30 13:34:08 +0100443{
Huang, Ying58270402008-01-31 22:05:43 +0100444 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100445}
446
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100447int set_memory_uc(unsigned long addr, int numpages)
448{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100449 return change_page_attr_set(addr, numpages,
450 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100451}
452EXPORT_SYMBOL(set_memory_uc);
453
454int set_memory_wb(unsigned long addr, int numpages)
455{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100456 return change_page_attr_clear(addr, numpages,
457 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100458}
459EXPORT_SYMBOL(set_memory_wb);
460
461int set_memory_x(unsigned long addr, int numpages)
462{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100463 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100464}
465EXPORT_SYMBOL(set_memory_x);
466
467int set_memory_nx(unsigned long addr, int numpages)
468{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100469 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100470}
471EXPORT_SYMBOL(set_memory_nx);
472
473int set_memory_ro(unsigned long addr, int numpages)
474{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100475 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100476}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100477
478int set_memory_rw(unsigned long addr, int numpages)
479{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100480 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100481}
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100482
483int set_memory_np(unsigned long addr, int numpages)
484{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100485 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100486}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100487
488int set_pages_uc(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_uc(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100493}
494EXPORT_SYMBOL(set_pages_uc);
495
496int set_pages_wb(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_wb(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100501}
502EXPORT_SYMBOL(set_pages_wb);
503
504int set_pages_x(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_x(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100509}
510EXPORT_SYMBOL(set_pages_x);
511
512int set_pages_nx(struct page *page, int numpages)
513{
514 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100515
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100516 return set_memory_nx(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100517}
518EXPORT_SYMBOL(set_pages_nx);
519
520int set_pages_ro(struct page *page, int numpages)
521{
522 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100523
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100524 return set_memory_ro(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100525}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100526
527int set_pages_rw(struct page *page, int numpages)
528{
529 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100530
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100531 return set_memory_rw(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100532}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Thomas Gleixner56744542008-01-30 13:34:08 +0100535#if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_CPA_DEBUG)
536static inline int __change_page_attr_set(unsigned long addr, int numpages,
537 pgprot_t mask)
538{
539 return __change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
540}
541
542static inline int __change_page_attr_clear(unsigned long addr, int numpages,
543 pgprot_t mask)
544{
545 return __change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
546}
547#endif
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100550
551static int __set_pages_p(struct page *page, int numpages)
552{
553 unsigned long addr = (unsigned long)page_address(page);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100554
555 return __change_page_attr_set(addr, numpages,
556 __pgprot(_PAGE_PRESENT | _PAGE_RW));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100557}
558
559static int __set_pages_np(struct page *page, int numpages)
560{
561 unsigned long addr = (unsigned long)page_address(page);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100562
563 return __change_page_attr_clear(addr, numpages,
564 __pgprot(_PAGE_PRESENT));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100565}
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567void kernel_map_pages(struct page *page, int numpages, int enable)
568{
569 if (PageHighMem(page))
570 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100571 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700572 debug_check_no_locks_freed(page_address(page),
573 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100574 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800575
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100576 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +0100577 * If page allocator is not up yet then do not call c_p_a():
578 */
579 if (!debug_pagealloc_enabled)
580 return;
581
582 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100583 * The return value is ignored - the calls cannot fail,
584 * large pages are disabled at boot time:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 */
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100586 if (enable)
587 __set_pages_p(page, numpages);
588 else
589 __set_pages_np(page, numpages);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100590
591 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100592 * We should perform an IPI and flush all tlbs,
593 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 */
595 __flush_tlb_all();
596}
597#endif
Arjan van de Vend1028a12008-01-30 13:34:07 +0100598
599/*
600 * The testcases use internal knowledge of the implementation that shouldn't
601 * be exposed to the rest of the kernel. Include these directly here.
602 */
603#ifdef CONFIG_CPA_DEBUG
604#include "pageattr-test.c"
605#endif