blob: bbfc8e2466abb67b47535068b6d0bc83a7bf8865 [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
29
30/**
31 * clflush_cache_range - flush a cache range with clflush
32 * @addr: virtual start address
33 * @size: number of bytes to flush
34 *
35 * clflush is an unordered instruction which needs fencing with mfence
36 * to avoid ordering issues.
37 */
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010038void clflush_cache_range(void *addr, int size)
39{
40 int i;
41
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010042 mb();
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010043 for (i = 0; i < size; i += boot_cpu_data.x86_clflush_size)
44 clflush(addr+i);
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010045 mb();
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010046}
47
Thomas Gleixneraf1e6842008-01-30 13:34:08 +010048static void __cpa_flush_all(void *arg)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010049{
50 /*
51 * Flush all to work around Errata in early athlons regarding
52 * large page flushing.
53 */
54 __flush_tlb_all();
55
56 if (boot_cpu_data.x86_model >= 4)
57 wbinvd();
58}
59
Thomas Gleixneraf1e6842008-01-30 13:34:08 +010060static void cpa_flush_all(void)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010061{
62 BUG_ON(irqs_disabled());
63
Thomas Gleixneraf1e6842008-01-30 13:34:08 +010064 on_each_cpu(__cpa_flush_all, NULL, 1, 1);
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010065}
66
Thomas Gleixner57a6a462008-01-30 13:34:08 +010067static void __cpa_flush_range(void *arg)
68{
Thomas Gleixner57a6a462008-01-30 13:34:08 +010069 /*
70 * We could optimize that further and do individual per page
71 * tlb invalidates for a low number of pages. Caveat: we must
72 * flush the high aliases on 64bit as well.
73 */
74 __flush_tlb_all();
Thomas Gleixner57a6a462008-01-30 13:34:08 +010075}
76
77static void cpa_flush_range(unsigned long addr, int numpages)
78{
Thomas Gleixner57a6a462008-01-30 13:34:08 +010079 BUG_ON(irqs_disabled());
80
Thomas Gleixner3b233e52008-01-30 13:34:08 +010081 on_each_cpu(__cpa_flush_range, NULL, 1, 1);
Thomas Gleixner57a6a462008-01-30 13:34:08 +010082
Thomas Gleixner3b233e52008-01-30 13:34:08 +010083 /*
84 * We only need to flush on one CPU,
85 * clflush is a MESI-coherent instruction that
86 * will cause all other CPUs to flush the same
87 * cachelines:
88 */
89 clflush_cache_range((void *) addr, numpages * PAGE_SIZE);
Thomas Gleixner57a6a462008-01-30 13:34:08 +010090}
91
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010092/*
Arjan van de Vened724be2008-01-30 13:34:04 +010093 * Certain areas of memory on x86 require very specific protection flags,
94 * for example the BIOS area or kernel text. Callers don't always get this
95 * right (again, ioremap() on BIOS memory is not uncommon) so this function
96 * checks and fixes these known static required protection bits.
97 */
98static inline pgprot_t static_protections(pgprot_t prot, unsigned long address)
99{
100 pgprot_t forbidden = __pgprot(0);
101
Ingo Molnar687c4822008-01-30 13:34:04 +0100102 /*
Arjan van de Vened724be2008-01-30 13:34:04 +0100103 * The BIOS area between 640k and 1Mb needs to be executable for
104 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
Ingo Molnar687c4822008-01-30 13:34:04 +0100105 */
Arjan van de Vened724be2008-01-30 13:34:04 +0100106 if (within(__pa(address), BIOS_BEGIN, BIOS_END))
107 pgprot_val(forbidden) |= _PAGE_NX;
108
109 /*
110 * The kernel text needs to be executable for obvious reasons
111 * Does not cover __inittext since that is gone later on
112 */
113 if (within(address, (unsigned long)_text, (unsigned long)_etext))
114 pgprot_val(forbidden) |= _PAGE_NX;
115
116#ifdef CONFIG_DEBUG_RODATA
117 /* The .rodata section needs to be read-only */
118 if (within(address, (unsigned long)__start_rodata,
119 (unsigned long)__end_rodata))
120 pgprot_val(forbidden) |= _PAGE_RW;
121#endif
122
123 prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
Ingo Molnar687c4822008-01-30 13:34:04 +0100124
125 return prot;
126}
127
Ingo Molnarf0646e42008-01-30 13:33:43 +0100128pte_t *lookup_address(unsigned long address, int *level)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100129{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 pgd_t *pgd = pgd_offset_k(address);
131 pud_t *pud;
132 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100133
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100134 *level = PG_LEVEL_NONE;
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (pgd_none(*pgd))
137 return NULL;
138 pud = pud_offset(pgd, address);
139 if (pud_none(*pud))
140 return NULL;
141 pmd = pmd_offset(pud, address);
142 if (pmd_none(*pmd))
143 return NULL;
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100144
145 *level = PG_LEVEL_2M;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (pmd_large(*pmd))
147 return (pte_t *)pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100149 *level = PG_LEVEL_4K;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100150 return pte_offset_kernel(pmd, address);
151}
152
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100153static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100154{
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100155 /* change init_mm */
156 set_pte_atomic(kpte, pte);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100157#ifdef CONFIG_X86_32
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100158 if (!SHARED_KERNEL_PMD) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100159 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Ingo Molnar44af6c42008-01-30 13:34:03 +0100161 for (page = pgd_list; page; page = (struct page *)page->index) {
162 pgd_t *pgd;
163 pud_t *pud;
164 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100165
Ingo Molnar44af6c42008-01-30 13:34:03 +0100166 pgd = (pgd_t *)page_address(page) + pgd_index(address);
167 pud = pud_offset(pgd, address);
168 pmd = pmd_offset(pud, address);
169 set_pte_atomic((pte_t *)pmd, pte);
170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
Ingo Molnar44af6c42008-01-30 13:34:03 +0100172#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100175static int split_large_page(pte_t *kpte, unsigned long address)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100176{
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100177 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnar12d6f212008-01-30 13:33:58 +0100178 gfp_t gfp_flags = GFP_KERNEL;
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100179 unsigned long flags;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100180 unsigned long addr;
181 pte_t *pbase, *tmp;
182 struct page *base;
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100183 int i, level;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100184
Ingo Molnar12d6f212008-01-30 13:33:58 +0100185#ifdef CONFIG_DEBUG_PAGEALLOC
186 gfp_flags = GFP_ATOMIC;
187#endif
188 base = alloc_pages(gfp_flags, 0);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100189 if (!base)
190 return -ENOMEM;
191
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100192 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100193 /*
194 * Check for races, another CPU might have split this page
195 * up for us already:
196 */
197 tmp = lookup_address(address, &level);
Ingo Molnar5508a742008-01-30 13:33:56 +0100198 if (tmp != kpte) {
199 WARN_ON_ONCE(1);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100200 goto out_unlock;
Ingo Molnar5508a742008-01-30 13:33:56 +0100201 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100202
203 address = __pa(address);
204 addr = address & LARGE_PAGE_MASK;
205 pbase = (pte_t *)page_address(base);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100206#ifdef CONFIG_X86_32
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100207 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
Ingo Molnar44af6c42008-01-30 13:34:03 +0100208#endif
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100209
210 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
211 set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
212
213 /*
Huang, Ying4c881ca2008-01-30 13:34:04 +0100214 * Install the new, split up pagetable. Important detail here:
215 *
216 * On Intel the NX bit of all levels must be cleared to make a
217 * page executable. See section 4.13.2 of Intel 64 and IA-32
218 * Architectures Software Developer's Manual).
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100219 */
Huang, Ying4c881ca2008-01-30 13:34:04 +0100220 ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100221 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100222 base = NULL;
223
224out_unlock:
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100225 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100226
227 if (base)
228 __free_pages(base, 0);
229
230 return 0;
231}
232
Ingo Molnar44af6c42008-01-30 13:34:03 +0100233static int
Ingo Molnar81922062008-01-30 13:34:04 +0100234__change_page_attr(unsigned long address, unsigned long pfn, pgprot_t prot)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100235{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 struct page *kpte_page;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100237 int level, err = 0;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100238 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Ingo Molnar81922062008-01-30 13:34:04 +0100240#ifdef CONFIG_X86_32
241 BUG_ON(pfn > max_low_pfn);
242#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100244repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100245 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (!kpte)
247 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200250 BUG_ON(PageLRU(kpte_page));
251 BUG_ON(PageCompound(kpte_page));
252
Arjan van de Vened724be2008-01-30 13:34:04 +0100253 prot = static_protections(prot, address);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200254
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100255 if (level == PG_LEVEL_4K) {
Thomas Gleixnera72a08a2008-01-30 13:34:07 +0100256 WARN_ON_ONCE(pgprot_val(prot) & _PAGE_PSE);
Ingo Molnar81922062008-01-30 13:34:04 +0100257 set_pte_atomic(kpte, pfn_pte(pfn, canon_pgprot(prot)));
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100258 } else {
Thomas Gleixnera72a08a2008-01-30 13:34:07 +0100259 /* Clear the PSE bit for the 4k level pages ! */
260 pgprot_val(prot) = pgprot_val(prot) & ~_PAGE_PSE;
261
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100262 err = split_large_page(kpte, address);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100263 if (!err)
264 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100266 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100267}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Ingo Molnar44af6c42008-01-30 13:34:03 +0100269/**
270 * change_page_attr_addr - Change page table attributes in linear mapping
271 * @address: Virtual address in linear mapping.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100272 * @prot: New page table attribute (PAGE_*)
273 *
274 * Change page attributes of a page in the direct mapping. This is a variant
275 * of change_page_attr() that also works on memory holes that do not have
276 * mem_map entry (pfn_valid() is false).
277 *
278 * See change_page_attr() documentation for more details.
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100279 *
280 * Modules and drivers should use the set_memory_* APIs instead.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100281 */
282
Arjan van de Ven488fd992008-01-30 13:34:07 +0100283static int change_page_attr_addr(unsigned long address, pgprot_t prot)
Ingo Molnar44af6c42008-01-30 13:34:03 +0100284{
Arjan van de Ven488fd992008-01-30 13:34:07 +0100285 int err = 0, kernel_map = 0;
286 unsigned long pfn = __pa(address) >> PAGE_SHIFT;
Ingo Molnar44af6c42008-01-30 13:34:03 +0100287
288#ifdef CONFIG_X86_64
289 if (address >= __START_KERNEL_map &&
290 address < __START_KERNEL_map + KERNEL_TEXT_SIZE) {
291
292 address = (unsigned long)__va(__pa(address));
293 kernel_map = 1;
294 }
295#endif
296
Arjan van de Ven488fd992008-01-30 13:34:07 +0100297 if (!kernel_map || pte_present(pfn_pte(0, prot))) {
298 err = __change_page_attr(address, pfn, prot);
299 if (err)
300 return err;
Ingo Molnar44af6c42008-01-30 13:34:03 +0100301 }
302
Arjan van de Ven488fd992008-01-30 13:34:07 +0100303#ifdef CONFIG_X86_64
304 /*
305 * Handle kernel mapping too which aliases part of
306 * lowmem:
307 */
308 if (__pa(address) < KERNEL_TEXT_SIZE) {
309 unsigned long addr2;
310 pgprot_t prot2;
311
312 addr2 = __START_KERNEL_map + __pa(address);
313 /* Make sure the kernel mappings stay executable */
314 prot2 = pte_pgprot(pte_mkexec(pfn_pte(0, prot)));
315 err = __change_page_attr(addr2, pfn, prot2);
316 }
317#endif
318
Ingo Molnar44af6c42008-01-30 13:34:03 +0100319 return err;
320}
321
Thomas Gleixnerff314522008-01-30 13:34:08 +0100322static int __change_page_attr_set_clr(unsigned long addr, int numpages,
323 pgprot_t mask_set, pgprot_t mask_clr)
324{
325 pgprot_t new_prot;
326 int level;
327 pte_t *pte;
328 int i, ret;
329
330 for (i = 0; i < numpages ; i++) {
331
332 pte = lookup_address(addr, &level);
333 if (!pte)
334 return -EINVAL;
335
336 new_prot = pte_pgprot(*pte);
337
338 pgprot_val(new_prot) &= ~pgprot_val(mask_clr);
339 pgprot_val(new_prot) |= pgprot_val(mask_set);
340
341 ret = change_page_attr_addr(addr, new_prot);
342 if (ret)
343 return ret;
344 addr += PAGE_SIZE;
345 }
346
347 return 0;
348}
349
350static int change_page_attr_set_clr(unsigned long addr, int numpages,
351 pgprot_t mask_set, pgprot_t mask_clr)
352{
353 int ret = __change_page_attr_set_clr(addr, numpages, mask_set,
354 mask_clr);
355
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100356 /*
357 * On success we use clflush, when the CPU supports it to
358 * avoid the wbindv. If the CPU does not support it and in the
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100359 * error case we fall back to cpa_flush_all (which uses
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100360 * wbindv):
361 */
362 if (!ret && cpu_has_clflush)
363 cpa_flush_range(addr, numpages);
364 else
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100365 cpa_flush_all();
Thomas Gleixnerff314522008-01-30 13:34:08 +0100366
367 return ret;
368}
369
Thomas Gleixner56744542008-01-30 13:34:08 +0100370static inline int change_page_attr_set(unsigned long addr, int numpages,
371 pgprot_t mask)
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100372{
Thomas Gleixner56744542008-01-30 13:34:08 +0100373 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100374}
375
Thomas Gleixner56744542008-01-30 13:34:08 +0100376static inline int change_page_attr_clear(unsigned long addr, int numpages,
377 pgprot_t mask)
Thomas Gleixner72932c72008-01-30 13:34:08 +0100378{
Thomas Gleixner56744542008-01-30 13:34:08 +0100379 return __change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100380
381}
382
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100383int set_memory_uc(unsigned long addr, int numpages)
384{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100385 return change_page_attr_set(addr, numpages,
386 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100387}
388EXPORT_SYMBOL(set_memory_uc);
389
390int set_memory_wb(unsigned long addr, int numpages)
391{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100392 return change_page_attr_clear(addr, numpages,
393 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100394}
395EXPORT_SYMBOL(set_memory_wb);
396
397int set_memory_x(unsigned long addr, int numpages)
398{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100399 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100400}
401EXPORT_SYMBOL(set_memory_x);
402
403int set_memory_nx(unsigned long addr, int numpages)
404{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100405 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100406}
407EXPORT_SYMBOL(set_memory_nx);
408
409int set_memory_ro(unsigned long addr, int numpages)
410{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100411 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100412}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100413
414int set_memory_rw(unsigned long addr, int numpages)
415{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100416 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100417}
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100418
419int set_memory_np(unsigned long addr, int numpages)
420{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100421 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100422}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100423
424int set_pages_uc(struct page *page, int numpages)
425{
426 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100427
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100428 return set_memory_uc(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100429}
430EXPORT_SYMBOL(set_pages_uc);
431
432int set_pages_wb(struct page *page, int numpages)
433{
434 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100435
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100436 return set_memory_wb(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100437}
438EXPORT_SYMBOL(set_pages_wb);
439
440int set_pages_x(struct page *page, int numpages)
441{
442 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100443
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100444 return set_memory_x(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100445}
446EXPORT_SYMBOL(set_pages_x);
447
448int set_pages_nx(struct page *page, int numpages)
449{
450 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100451
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100452 return set_memory_nx(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100453}
454EXPORT_SYMBOL(set_pages_nx);
455
456int set_pages_ro(struct page *page, int numpages)
457{
458 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100459
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100460 return set_memory_ro(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100461}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100462
463int set_pages_rw(struct page *page, int numpages)
464{
465 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100466
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100467 return set_memory_rw(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100468}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Thomas Gleixner56744542008-01-30 13:34:08 +0100471#if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_CPA_DEBUG)
472static inline int __change_page_attr_set(unsigned long addr, int numpages,
473 pgprot_t mask)
474{
475 return __change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
476}
477
478static inline int __change_page_attr_clear(unsigned long addr, int numpages,
479 pgprot_t mask)
480{
481 return __change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
482}
483#endif
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100486
487static int __set_pages_p(struct page *page, int numpages)
488{
489 unsigned long addr = (unsigned long)page_address(page);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100490
491 return __change_page_attr_set(addr, numpages,
492 __pgprot(_PAGE_PRESENT | _PAGE_RW));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100493}
494
495static int __set_pages_np(struct page *page, int numpages)
496{
497 unsigned long addr = (unsigned long)page_address(page);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100498
499 return __change_page_attr_clear(addr, numpages,
500 __pgprot(_PAGE_PRESENT));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503void kernel_map_pages(struct page *page, int numpages, int enable)
504{
505 if (PageHighMem(page))
506 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100507 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700508 debug_check_no_locks_freed(page_address(page),
509 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100510 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800511
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100512 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +0100513 * If page allocator is not up yet then do not call c_p_a():
514 */
515 if (!debug_pagealloc_enabled)
516 return;
517
518 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100519 * The return value is ignored - the calls cannot fail,
520 * large pages are disabled at boot time:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 */
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100522 if (enable)
523 __set_pages_p(page, numpages);
524 else
525 __set_pages_np(page, numpages);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100526
527 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100528 * We should perform an IPI and flush all tlbs,
529 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 */
531 __flush_tlb_all();
532}
533#endif
Arjan van de Vend1028a12008-01-30 13:34:07 +0100534
535/*
536 * The testcases use internal knowledge of the implementation that shouldn't
537 * be exposed to the rest of the kernel. Include these directly here.
538 */
539#ifdef CONFIG_CPA_DEBUG
540#include "pageattr-test.c"
541#endif