blob: 532a40bc0e7e8aca565b64af6044f1efa5cec449 [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
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100109/*
Arjan van de Vened724be2008-01-30 13:34:04 +0100110 * Certain areas of memory on x86 require very specific protection flags,
111 * for example the BIOS area or kernel text. Callers don't always get this
112 * right (again, ioremap() on BIOS memory is not uncommon) so this function
113 * checks and fixes these known static required protection bits.
114 */
115static inline pgprot_t static_protections(pgprot_t prot, unsigned long address)
116{
117 pgprot_t forbidden = __pgprot(0);
118
Ingo Molnar687c4822008-01-30 13:34:04 +0100119 /*
Arjan van de Vened724be2008-01-30 13:34:04 +0100120 * The BIOS area between 640k and 1Mb needs to be executable for
121 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
Ingo Molnar687c4822008-01-30 13:34:04 +0100122 */
Arjan van de Vened724be2008-01-30 13:34:04 +0100123 if (within(__pa(address), BIOS_BEGIN, BIOS_END))
124 pgprot_val(forbidden) |= _PAGE_NX;
125
126 /*
127 * The kernel text needs to be executable for obvious reasons
128 * Does not cover __inittext since that is gone later on
129 */
130 if (within(address, (unsigned long)_text, (unsigned long)_etext))
131 pgprot_val(forbidden) |= _PAGE_NX;
132
133#ifdef CONFIG_DEBUG_RODATA
134 /* The .rodata section needs to be read-only */
135 if (within(address, (unsigned long)__start_rodata,
136 (unsigned long)__end_rodata))
137 pgprot_val(forbidden) |= _PAGE_RW;
138#endif
139
140 prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
Ingo Molnar687c4822008-01-30 13:34:04 +0100141
142 return prot;
143}
144
Ingo Molnarf0646e42008-01-30 13:33:43 +0100145pte_t *lookup_address(unsigned long address, int *level)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100146{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 pgd_t *pgd = pgd_offset_k(address);
148 pud_t *pud;
149 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100150
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100151 *level = PG_LEVEL_NONE;
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (pgd_none(*pgd))
154 return NULL;
155 pud = pud_offset(pgd, address);
156 if (pud_none(*pud))
157 return NULL;
158 pmd = pmd_offset(pud, address);
159 if (pmd_none(*pmd))
160 return NULL;
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100161
162 *level = PG_LEVEL_2M;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (pmd_large(*pmd))
164 return (pte_t *)pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100166 *level = PG_LEVEL_4K;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100167 return pte_offset_kernel(pmd, address);
168}
169
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100170static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100171{
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100172 /* change init_mm */
173 set_pte_atomic(kpte, pte);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100174#ifdef CONFIG_X86_32
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100175 if (!SHARED_KERNEL_PMD) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100176 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Ingo Molnar44af6c42008-01-30 13:34:03 +0100178 for (page = pgd_list; page; page = (struct page *)page->index) {
179 pgd_t *pgd;
180 pud_t *pud;
181 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100182
Ingo Molnar44af6c42008-01-30 13:34:03 +0100183 pgd = (pgd_t *)page_address(page) + pgd_index(address);
184 pud = pud_offset(pgd, address);
185 pmd = pmd_offset(pud, address);
186 set_pte_atomic((pte_t *)pmd, pte);
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
Ingo Molnar44af6c42008-01-30 13:34:03 +0100189#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100192static int split_large_page(pte_t *kpte, unsigned long address)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100193{
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100194 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnar12d6f212008-01-30 13:33:58 +0100195 gfp_t gfp_flags = GFP_KERNEL;
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100196 unsigned long flags;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100197 unsigned long addr;
198 pte_t *pbase, *tmp;
199 struct page *base;
Ingo Molnar86f03982008-01-30 13:34:09 +0100200 unsigned int i, level;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100201
Ingo Molnar12d6f212008-01-30 13:33:58 +0100202#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnar86f03982008-01-30 13:34:09 +0100203 gfp_flags = __GFP_HIGH | __GFP_NOFAIL | __GFP_NOWARN;
204 gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Ingo Molnar12d6f212008-01-30 13:33:58 +0100205#endif
206 base = alloc_pages(gfp_flags, 0);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100207 if (!base)
208 return -ENOMEM;
209
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100210 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100211 /*
212 * Check for races, another CPU might have split this page
213 * up for us already:
214 */
215 tmp = lookup_address(address, &level);
Ingo Molnar5508a7482008-01-30 13:33:56 +0100216 if (tmp != kpte) {
217 WARN_ON_ONCE(1);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100218 goto out_unlock;
Ingo Molnar5508a7482008-01-30 13:33:56 +0100219 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100220
221 address = __pa(address);
222 addr = address & LARGE_PAGE_MASK;
223 pbase = (pte_t *)page_address(base);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100224#ifdef CONFIG_X86_32
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100225 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
Ingo Molnar44af6c42008-01-30 13:34:03 +0100226#endif
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100227
Ingo Molnar86f03982008-01-30 13:34:09 +0100228 pgprot_val(ref_prot) &= ~_PAGE_NX;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100229 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
230 set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
231
232 /*
Huang, Ying4c881ca2008-01-30 13:34:04 +0100233 * Install the new, split up pagetable. Important detail here:
234 *
235 * On Intel the NX bit of all levels must be cleared to make a
236 * page executable. See section 4.13.2 of Intel 64 and IA-32
237 * Architectures Software Developer's Manual).
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100238 */
Huang, Ying4c881ca2008-01-30 13:34:04 +0100239 ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100240 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100241 base = NULL;
242
243out_unlock:
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100244 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100245
246 if (base)
247 __free_pages(base, 0);
248
249 return 0;
250}
251
Ingo Molnar44af6c42008-01-30 13:34:03 +0100252static int
Ingo Molnar86f03982008-01-30 13:34:09 +0100253__change_page_attr(unsigned long address, unsigned long pfn,
254 pgprot_t mask_set, pgprot_t mask_clr)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100255{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 struct page *kpte_page;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100257 int level, err = 0;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100258 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Ingo Molnar81922062008-01-30 13:34:04 +0100260#ifdef CONFIG_X86_32
261 BUG_ON(pfn > max_low_pfn);
262#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100264repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100265 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (!kpte)
267 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200270 BUG_ON(PageLRU(kpte_page));
271 BUG_ON(PageCompound(kpte_page));
272
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100273 if (level == PG_LEVEL_4K) {
Ingo Molnar86f03982008-01-30 13:34:09 +0100274 pgprot_t new_prot = pte_pgprot(*kpte);
275 pte_t new_pte, old_pte = *kpte;
Thomas Gleixnera72a08a2008-01-30 13:34:07 +0100276
Ingo Molnar86f03982008-01-30 13:34:09 +0100277 pgprot_val(new_prot) &= ~pgprot_val(mask_clr);
278 pgprot_val(new_prot) |= pgprot_val(mask_set);
279
280 new_prot = static_protections(new_prot, address);
281
282 new_pte = pfn_pte(pfn, canon_pgprot(new_prot));
283 BUG_ON(pte_pfn(new_pte) != pte_pfn(old_pte));
284
285 set_pte_atomic(kpte, new_pte);
286 } else {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100287 err = split_large_page(kpte, address);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100288 if (!err)
289 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100291 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100292}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Ingo Molnar44af6c42008-01-30 13:34:03 +0100294/**
295 * change_page_attr_addr - Change page table attributes in linear mapping
296 * @address: Virtual address in linear mapping.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100297 * @prot: New page table attribute (PAGE_*)
298 *
299 * Change page attributes of a page in the direct mapping. This is a variant
300 * of change_page_attr() that also works on memory holes that do not have
301 * mem_map entry (pfn_valid() is false).
302 *
303 * See change_page_attr() documentation for more details.
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100304 *
305 * Modules and drivers should use the set_memory_* APIs instead.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100306 */
307
Ingo Molnar86f03982008-01-30 13:34:09 +0100308static int
309change_page_attr_addr(unsigned long address, pgprot_t mask_set,
310 pgprot_t mask_clr)
Ingo Molnar44af6c42008-01-30 13:34:03 +0100311{
Arjan van de Ven488fd992008-01-30 13:34:07 +0100312 int err = 0, kernel_map = 0;
Ingo Molnar86f03982008-01-30 13:34:09 +0100313 unsigned long pfn;
Ingo Molnar44af6c42008-01-30 13:34:03 +0100314
315#ifdef CONFIG_X86_64
316 if (address >= __START_KERNEL_map &&
317 address < __START_KERNEL_map + KERNEL_TEXT_SIZE) {
318
Ingo Molnar86f03982008-01-30 13:34:09 +0100319 address = (unsigned long)__va(__pa((void *)address));
Ingo Molnar44af6c42008-01-30 13:34:03 +0100320 kernel_map = 1;
321 }
322#endif
323
Ingo Molnar86f03982008-01-30 13:34:09 +0100324 pfn = __pa(address) >> PAGE_SHIFT;
325
326 if (!kernel_map || 1) {
327 err = __change_page_attr(address, pfn, mask_set, mask_clr);
Arjan van de Ven488fd992008-01-30 13:34:07 +0100328 if (err)
329 return err;
Ingo Molnar44af6c42008-01-30 13:34:03 +0100330 }
331
Arjan van de Ven488fd992008-01-30 13:34:07 +0100332#ifdef CONFIG_X86_64
333 /*
334 * Handle kernel mapping too which aliases part of
335 * lowmem:
336 */
337 if (__pa(address) < KERNEL_TEXT_SIZE) {
338 unsigned long addr2;
Arjan van de Ven488fd992008-01-30 13:34:07 +0100339
Ingo Molnar86f03982008-01-30 13:34:09 +0100340 addr2 = __pa(address) + __START_KERNEL_map - phys_base;
Arjan van de Ven488fd992008-01-30 13:34:07 +0100341 /* Make sure the kernel mappings stay executable */
Ingo Molnar86f03982008-01-30 13:34:09 +0100342 pgprot_val(mask_clr) |= _PAGE_NX;
343 /*
344 * Our high aliases are imprecise, so do not propagate
345 * failures back to users:
346 */
347 __change_page_attr(addr2, pfn, mask_set, mask_clr);
Arjan van de Ven488fd992008-01-30 13:34:07 +0100348 }
349#endif
350
Ingo Molnar44af6c42008-01-30 13:34:03 +0100351 return err;
352}
353
Thomas Gleixnerff314522008-01-30 13:34:08 +0100354static int __change_page_attr_set_clr(unsigned long addr, int numpages,
355 pgprot_t mask_set, pgprot_t mask_clr)
356{
Ingo Molnar86f03982008-01-30 13:34:09 +0100357 unsigned int i;
358 int ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100359
Ingo Molnar86f03982008-01-30 13:34:09 +0100360 for (i = 0; i < numpages ; i++, addr += PAGE_SIZE) {
361 ret = change_page_attr_addr(addr, mask_set, mask_clr);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100362 if (ret)
363 return ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100364 }
365
366 return 0;
367}
368
369static int change_page_attr_set_clr(unsigned long addr, int numpages,
370 pgprot_t mask_set, pgprot_t mask_clr)
371{
372 int ret = __change_page_attr_set_clr(addr, numpages, mask_set,
373 mask_clr);
374
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100375 /*
376 * On success we use clflush, when the CPU supports it to
377 * avoid the wbindv. If the CPU does not support it and in the
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100378 * error case we fall back to cpa_flush_all (which uses
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100379 * wbindv):
380 */
381 if (!ret && cpu_has_clflush)
382 cpa_flush_range(addr, numpages);
383 else
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100384 cpa_flush_all();
Thomas Gleixnerff314522008-01-30 13:34:08 +0100385
386 return ret;
387}
388
Thomas Gleixner56744542008-01-30 13:34:08 +0100389static inline int change_page_attr_set(unsigned long addr, int numpages,
390 pgprot_t mask)
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100391{
Thomas Gleixner56744542008-01-30 13:34:08 +0100392 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100393}
394
Thomas Gleixner56744542008-01-30 13:34:08 +0100395static inline int change_page_attr_clear(unsigned long addr, int numpages,
396 pgprot_t mask)
Thomas Gleixner72932c72008-01-30 13:34:08 +0100397{
Thomas Gleixner56744542008-01-30 13:34:08 +0100398 return __change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100399
400}
401
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100402int set_memory_uc(unsigned long addr, int numpages)
403{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100404 return change_page_attr_set(addr, numpages,
405 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100406}
407EXPORT_SYMBOL(set_memory_uc);
408
409int set_memory_wb(unsigned long addr, int numpages)
410{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100411 return change_page_attr_clear(addr, numpages,
412 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100413}
414EXPORT_SYMBOL(set_memory_wb);
415
416int set_memory_x(unsigned long addr, int numpages)
417{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100418 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100419}
420EXPORT_SYMBOL(set_memory_x);
421
422int set_memory_nx(unsigned long addr, int numpages)
423{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100424 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100425}
426EXPORT_SYMBOL(set_memory_nx);
427
428int set_memory_ro(unsigned long addr, int numpages)
429{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100430 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100431}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100432
433int set_memory_rw(unsigned long addr, int numpages)
434{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100435 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100436}
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100437
438int set_memory_np(unsigned long addr, int numpages)
439{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100440 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100441}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100442
443int set_pages_uc(struct page *page, int numpages)
444{
445 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100446
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100447 return set_memory_uc(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100448}
449EXPORT_SYMBOL(set_pages_uc);
450
451int set_pages_wb(struct page *page, int numpages)
452{
453 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100454
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100455 return set_memory_wb(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100456}
457EXPORT_SYMBOL(set_pages_wb);
458
459int set_pages_x(struct page *page, int numpages)
460{
461 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100462
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100463 return set_memory_x(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100464}
465EXPORT_SYMBOL(set_pages_x);
466
467int set_pages_nx(struct page *page, int numpages)
468{
469 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100470
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100471 return set_memory_nx(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100472}
473EXPORT_SYMBOL(set_pages_nx);
474
475int set_pages_ro(struct page *page, int numpages)
476{
477 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100478
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100479 return set_memory_ro(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100480}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100481
482int set_pages_rw(struct page *page, int numpages)
483{
484 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100485
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100486 return set_memory_rw(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100487}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Thomas Gleixner56744542008-01-30 13:34:08 +0100490#if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_CPA_DEBUG)
491static inline int __change_page_attr_set(unsigned long addr, int numpages,
492 pgprot_t mask)
493{
494 return __change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
495}
496
497static inline int __change_page_attr_clear(unsigned long addr, int numpages,
498 pgprot_t mask)
499{
500 return __change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
501}
502#endif
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100505
506static int __set_pages_p(struct page *page, int numpages)
507{
508 unsigned long addr = (unsigned long)page_address(page);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100509
510 return __change_page_attr_set(addr, numpages,
511 __pgprot(_PAGE_PRESENT | _PAGE_RW));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100512}
513
514static int __set_pages_np(struct page *page, int numpages)
515{
516 unsigned long addr = (unsigned long)page_address(page);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100517
518 return __change_page_attr_clear(addr, numpages,
519 __pgprot(_PAGE_PRESENT));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522void kernel_map_pages(struct page *page, int numpages, int enable)
523{
524 if (PageHighMem(page))
525 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100526 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700527 debug_check_no_locks_freed(page_address(page),
528 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100529 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800530
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100531 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +0100532 * If page allocator is not up yet then do not call c_p_a():
533 */
534 if (!debug_pagealloc_enabled)
535 return;
536
537 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100538 * The return value is ignored - the calls cannot fail,
539 * large pages are disabled at boot time:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 */
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100541 if (enable)
542 __set_pages_p(page, numpages);
543 else
544 __set_pages_np(page, numpages);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100545
546 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100547 * We should perform an IPI and flush all tlbs,
548 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 */
550 __flush_tlb_all();
551}
552#endif
Arjan van de Vend1028a12008-01-30 13:34:07 +0100553
554/*
555 * The testcases use internal knowledge of the implementation that shouldn't
556 * be exposed to the rest of the kernel. Include these directly here.
557 */
558#ifdef CONFIG_CPA_DEBUG
559#include "pageattr-test.c"
560#endif