blob: 23f0aa3d01c1476c41f22f7fd7470b41f33deced [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 */
28void clflush_cache_range(void *addr, int size)
29{
30 int i;
31
32 for (i = 0; i < size; i += boot_cpu_data.x86_clflush_size)
33 clflush(addr+i);
34}
35
36static void flush_kernel_map(void *arg)
37{
38 /*
39 * Flush all to work around Errata in early athlons regarding
40 * large page flushing.
41 */
42 __flush_tlb_all();
43
44 if (boot_cpu_data.x86_model >= 4)
45 wbinvd();
46}
47
48static void global_flush_tlb(void)
49{
50 BUG_ON(irqs_disabled());
51
52 on_each_cpu(flush_kernel_map, NULL, 1, 1);
53}
54
55/*
Arjan van de Vened724be2008-01-30 13:34:04 +010056 * Certain areas of memory on x86 require very specific protection flags,
57 * for example the BIOS area or kernel text. Callers don't always get this
58 * right (again, ioremap() on BIOS memory is not uncommon) so this function
59 * checks and fixes these known static required protection bits.
60 */
61static inline pgprot_t static_protections(pgprot_t prot, unsigned long address)
62{
63 pgprot_t forbidden = __pgprot(0);
64
Ingo Molnar687c4822008-01-30 13:34:04 +010065 /*
Arjan van de Vened724be2008-01-30 13:34:04 +010066 * The BIOS area between 640k and 1Mb needs to be executable for
67 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
Ingo Molnar687c4822008-01-30 13:34:04 +010068 */
Arjan van de Vened724be2008-01-30 13:34:04 +010069 if (within(__pa(address), BIOS_BEGIN, BIOS_END))
70 pgprot_val(forbidden) |= _PAGE_NX;
71
72 /*
73 * The kernel text needs to be executable for obvious reasons
74 * Does not cover __inittext since that is gone later on
75 */
76 if (within(address, (unsigned long)_text, (unsigned long)_etext))
77 pgprot_val(forbidden) |= _PAGE_NX;
78
79#ifdef CONFIG_DEBUG_RODATA
80 /* The .rodata section needs to be read-only */
81 if (within(address, (unsigned long)__start_rodata,
82 (unsigned long)__end_rodata))
83 pgprot_val(forbidden) |= _PAGE_RW;
84#endif
85
86 prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
Ingo Molnar687c4822008-01-30 13:34:04 +010087
88 return prot;
89}
90
Ingo Molnarf0646e42008-01-30 13:33:43 +010091pte_t *lookup_address(unsigned long address, int *level)
Ingo Molnar9f4c8152008-01-30 13:33:41 +010092{
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 pgd_t *pgd = pgd_offset_k(address);
94 pud_t *pud;
95 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010096
Thomas Gleixner30551bb2008-01-30 13:34:04 +010097 *level = PG_LEVEL_NONE;
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (pgd_none(*pgd))
100 return NULL;
101 pud = pud_offset(pgd, address);
102 if (pud_none(*pud))
103 return NULL;
104 pmd = pmd_offset(pud, address);
105 if (pmd_none(*pmd))
106 return NULL;
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100107
108 *level = PG_LEVEL_2M;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (pmd_large(*pmd))
110 return (pte_t *)pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100112 *level = PG_LEVEL_4K;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100113 return pte_offset_kernel(pmd, address);
114}
115
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100116static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100117{
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100118 /* change init_mm */
119 set_pte_atomic(kpte, pte);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100120#ifdef CONFIG_X86_32
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100121 if (!SHARED_KERNEL_PMD) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100122 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Ingo Molnar44af6c42008-01-30 13:34:03 +0100124 for (page = pgd_list; page; page = (struct page *)page->index) {
125 pgd_t *pgd;
126 pud_t *pud;
127 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100128
Ingo Molnar44af6c42008-01-30 13:34:03 +0100129 pgd = (pgd_t *)page_address(page) + pgd_index(address);
130 pud = pud_offset(pgd, address);
131 pmd = pmd_offset(pud, address);
132 set_pte_atomic((pte_t *)pmd, pte);
133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
Ingo Molnar44af6c42008-01-30 13:34:03 +0100135#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100138static int split_large_page(pte_t *kpte, unsigned long address)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100139{
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100140 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnar12d6f212008-01-30 13:33:58 +0100141 gfp_t gfp_flags = GFP_KERNEL;
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100142 unsigned long flags;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100143 unsigned long addr;
144 pte_t *pbase, *tmp;
145 struct page *base;
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100146 int i, level;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100147
Ingo Molnar12d6f212008-01-30 13:33:58 +0100148#ifdef CONFIG_DEBUG_PAGEALLOC
149 gfp_flags = GFP_ATOMIC;
150#endif
151 base = alloc_pages(gfp_flags, 0);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100152 if (!base)
153 return -ENOMEM;
154
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100155 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100156 /*
157 * Check for races, another CPU might have split this page
158 * up for us already:
159 */
160 tmp = lookup_address(address, &level);
Ingo Molnar5508a7482008-01-30 13:33:56 +0100161 if (tmp != kpte) {
162 WARN_ON_ONCE(1);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100163 goto out_unlock;
Ingo Molnar5508a7482008-01-30 13:33:56 +0100164 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100165
166 address = __pa(address);
167 addr = address & LARGE_PAGE_MASK;
168 pbase = (pte_t *)page_address(base);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100169#ifdef CONFIG_X86_32
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100170 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
Ingo Molnar44af6c42008-01-30 13:34:03 +0100171#endif
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100172
173 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
174 set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
175
176 /*
Huang, Ying4c881ca2008-01-30 13:34:04 +0100177 * Install the new, split up pagetable. Important detail here:
178 *
179 * On Intel the NX bit of all levels must be cleared to make a
180 * page executable. See section 4.13.2 of Intel 64 and IA-32
181 * Architectures Software Developer's Manual).
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100182 */
Huang, Ying4c881ca2008-01-30 13:34:04 +0100183 ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100184 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100185 base = NULL;
186
187out_unlock:
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100188 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100189
190 if (base)
191 __free_pages(base, 0);
192
193 return 0;
194}
195
Ingo Molnar44af6c42008-01-30 13:34:03 +0100196static int
Ingo Molnar81922062008-01-30 13:34:04 +0100197__change_page_attr(unsigned long address, unsigned long pfn, pgprot_t prot)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100198{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 struct page *kpte_page;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100200 int level, err = 0;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100201 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Ingo Molnar81922062008-01-30 13:34:04 +0100203#ifdef CONFIG_X86_32
204 BUG_ON(pfn > max_low_pfn);
205#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100207repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100208 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (!kpte)
210 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200213 BUG_ON(PageLRU(kpte_page));
214 BUG_ON(PageCompound(kpte_page));
215
Arjan van de Vened724be2008-01-30 13:34:04 +0100216 prot = static_protections(prot, address);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200217
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100218 if (level == PG_LEVEL_4K) {
Ingo Molnar81922062008-01-30 13:34:04 +0100219 set_pte_atomic(kpte, pfn_pte(pfn, canon_pgprot(prot)));
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100220 } else {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100221 err = split_large_page(kpte, address);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100222 if (!err)
223 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100225 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100226}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Ingo Molnar44af6c42008-01-30 13:34:03 +0100228/**
229 * change_page_attr_addr - Change page table attributes in linear mapping
230 * @address: Virtual address in linear mapping.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100231 * @prot: New page table attribute (PAGE_*)
232 *
233 * Change page attributes of a page in the direct mapping. This is a variant
234 * of change_page_attr() that also works on memory holes that do not have
235 * mem_map entry (pfn_valid() is false).
236 *
237 * See change_page_attr() documentation for more details.
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100238 *
239 * Modules and drivers should use the set_memory_* APIs instead.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100240 */
241
Arjan van de Ven488fd992008-01-30 13:34:07 +0100242static int change_page_attr_addr(unsigned long address, pgprot_t prot)
Ingo Molnar44af6c42008-01-30 13:34:03 +0100243{
Arjan van de Ven488fd992008-01-30 13:34:07 +0100244 int err = 0, kernel_map = 0;
245 unsigned long pfn = __pa(address) >> PAGE_SHIFT;
Ingo Molnar44af6c42008-01-30 13:34:03 +0100246
247#ifdef CONFIG_X86_64
248 if (address >= __START_KERNEL_map &&
249 address < __START_KERNEL_map + KERNEL_TEXT_SIZE) {
250
251 address = (unsigned long)__va(__pa(address));
252 kernel_map = 1;
253 }
254#endif
255
Arjan van de Ven488fd992008-01-30 13:34:07 +0100256 if (!kernel_map || pte_present(pfn_pte(0, prot))) {
257 err = __change_page_attr(address, pfn, prot);
258 if (err)
259 return err;
Ingo Molnar44af6c42008-01-30 13:34:03 +0100260 }
261
Arjan van de Ven488fd992008-01-30 13:34:07 +0100262#ifdef CONFIG_X86_64
263 /*
264 * Handle kernel mapping too which aliases part of
265 * lowmem:
266 */
267 if (__pa(address) < KERNEL_TEXT_SIZE) {
268 unsigned long addr2;
269 pgprot_t prot2;
270
271 addr2 = __START_KERNEL_map + __pa(address);
272 /* Make sure the kernel mappings stay executable */
273 prot2 = pte_pgprot(pte_mkexec(pfn_pte(0, prot)));
274 err = __change_page_attr(addr2, pfn, prot2);
275 }
276#endif
277
Ingo Molnar44af6c42008-01-30 13:34:03 +0100278 return err;
279}
280
281/**
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100282 * change_page_attr_set - Change page table attributes in the linear mapping.
283 * @addr: Virtual address in linear mapping.
284 * @numpages: Number of pages to change
285 * @prot: Protection/caching type bits to set (PAGE_*)
286 *
287 * Returns 0 on success, otherwise a negated errno.
288 *
289 * This should be used when a page is mapped with a different caching policy
290 * than write-back somewhere - some CPUs do not like it when mappings with
291 * different caching policies exist. This changes the page attributes of the
292 * in kernel linear mapping too.
293 *
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100294 * The caller needs to ensure that there are no conflicting mappings elsewhere
295 * (e.g. in user space) * This function only deals with the kernel linear map.
296 *
297 * This function is different from change_page_attr() in that only selected bits
298 * are impacted, all other bits remain as is.
299 */
Arjan van de Vend1028a12008-01-30 13:34:07 +0100300static int change_page_attr_set(unsigned long addr, int numpages,
301 pgprot_t prot)
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100302{
303 pgprot_t current_prot;
304 int level;
305 pte_t *pte;
Arjan van de Ven488fd992008-01-30 13:34:07 +0100306 int i, ret;
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100307
Arjan van de Ven488fd992008-01-30 13:34:07 +0100308 for (i = 0; i < numpages ; i++) {
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100309
Arjan van de Ven488fd992008-01-30 13:34:07 +0100310 pte = lookup_address(addr, &level);
311 if (pte)
312 current_prot = pte_pgprot(*pte);
313 else
314 pgprot_val(current_prot) = 0;
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100315
Arjan van de Ven488fd992008-01-30 13:34:07 +0100316 pgprot_val(prot) = pgprot_val(current_prot) | pgprot_val(prot);
317
318 ret = change_page_attr_addr(addr, prot);
319 if (ret)
320 return ret;
321 addr += PAGE_SIZE;
322 }
323 return 0;
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100324}
325
326/**
327 * change_page_attr_clear - Change page table attributes in the linear mapping.
328 * @addr: Virtual address in linear mapping.
329 * @numpages: Number of pages to change
330 * @prot: Protection/caching type bits to clear (PAGE_*)
331 *
332 * Returns 0 on success, otherwise a negated errno.
333 *
334 * This should be used when a page is mapped with a different caching policy
335 * than write-back somewhere - some CPUs do not like it when mappings with
336 * different caching policies exist. This changes the page attributes of the
337 * in kernel linear mapping too.
338 *
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100339 * The caller needs to ensure that there are no conflicting mappings elsewhere
340 * (e.g. in user space) * This function only deals with the kernel linear map.
341 *
342 * This function is different from change_page_attr() in that only selected bits
343 * are impacted, all other bits remain as is.
344 */
Arjan van de Vend1028a12008-01-30 13:34:07 +0100345static int change_page_attr_clear(unsigned long addr, int numpages,
346 pgprot_t prot)
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100347{
348 pgprot_t current_prot;
349 int level;
350 pte_t *pte;
Arjan van de Ven488fd992008-01-30 13:34:07 +0100351 int i, ret;
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100352
Arjan van de Ven488fd992008-01-30 13:34:07 +0100353 for (i = 0; i < numpages; i++) {
354 pte = lookup_address(addr, &level);
355 if (pte)
356 current_prot = pte_pgprot(*pte);
357 else
358 pgprot_val(current_prot) = 0;
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100359
Arjan van de Ven488fd992008-01-30 13:34:07 +0100360 pgprot_val(prot) =
361 pgprot_val(current_prot) & ~pgprot_val(prot);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100362
Arjan van de Ven488fd992008-01-30 13:34:07 +0100363 ret = change_page_attr_addr(addr, prot);
364 if (ret)
365 return ret;
366 addr += PAGE_SIZE;
367 }
368 return 0;
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100369}
370
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100371int set_memory_uc(unsigned long addr, int numpages)
372{
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100373 int err;
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100374
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100375 err = change_page_attr_set(addr, numpages,
376 __pgprot(_PAGE_PCD | _PAGE_PWT));
377 global_flush_tlb();
378 return err;
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100379}
380EXPORT_SYMBOL(set_memory_uc);
381
382int set_memory_wb(unsigned long addr, int numpages)
383{
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100384 int err;
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100385
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100386 err = change_page_attr_clear(addr, numpages,
387 __pgprot(_PAGE_PCD | _PAGE_PWT));
388 global_flush_tlb();
389 return err;
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100390}
391EXPORT_SYMBOL(set_memory_wb);
392
393int set_memory_x(unsigned long addr, int numpages)
394{
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100395 int err;
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100396
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100397 err = change_page_attr_clear(addr, numpages,
398 __pgprot(_PAGE_NX));
399 global_flush_tlb();
400 return err;
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100401}
402EXPORT_SYMBOL(set_memory_x);
403
404int set_memory_nx(unsigned long addr, int numpages)
405{
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100406 int err;
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100407
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100408 err = change_page_attr_set(addr, numpages,
409 __pgprot(_PAGE_NX));
410 global_flush_tlb();
411 return err;
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100412}
413EXPORT_SYMBOL(set_memory_nx);
414
415int set_memory_ro(unsigned long addr, int numpages)
416{
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100417 int err;
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100418
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100419 err = change_page_attr_clear(addr, numpages,
420 __pgprot(_PAGE_RW));
421 global_flush_tlb();
422 return err;
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100423}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100424
425int set_memory_rw(unsigned long addr, int numpages)
426{
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100427 int err;
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100428
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100429 err = change_page_attr_set(addr, numpages,
430 __pgprot(_PAGE_RW));
431 global_flush_tlb();
432 return err;
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100433}
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100434
435int set_memory_np(unsigned long addr, int numpages)
436{
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100437 int err;
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100438
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100439 err = change_page_attr_clear(addr, numpages,
440 __pgprot(_PAGE_PRESENT));
441 global_flush_tlb();
442 return err;
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100443}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100444
445int set_pages_uc(struct page *page, int numpages)
446{
447 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100448
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100449 return set_memory_uc(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100450}
451EXPORT_SYMBOL(set_pages_uc);
452
453int set_pages_wb(struct page *page, int numpages)
454{
455 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100456
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100457 return set_memory_wb(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100458}
459EXPORT_SYMBOL(set_pages_wb);
460
461int set_pages_x(struct page *page, int numpages)
462{
463 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100464
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100465 return set_memory_x(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100466}
467EXPORT_SYMBOL(set_pages_x);
468
469int set_pages_nx(struct page *page, int numpages)
470{
471 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100472
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100473 return set_memory_nx(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100474}
475EXPORT_SYMBOL(set_pages_nx);
476
477int set_pages_ro(struct page *page, int numpages)
478{
479 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100480
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100481 return set_memory_ro(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100482}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100483
484int set_pages_rw(struct page *page, int numpages)
485{
486 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100487
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100488 return set_memory_rw(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100489}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100493
494static int __set_pages_p(struct page *page, int numpages)
495{
496 unsigned long addr = (unsigned long)page_address(page);
497 return change_page_attr_set(addr, numpages,
498 __pgprot(_PAGE_PRESENT | _PAGE_RW));
499}
500
501static int __set_pages_np(struct page *page, int numpages)
502{
503 unsigned long addr = (unsigned long)page_address(page);
504 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507void kernel_map_pages(struct page *page, int numpages, int enable)
508{
509 if (PageHighMem(page))
510 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100511 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700512 debug_check_no_locks_freed(page_address(page),
513 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100514 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800515
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100516 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +0100517 * If page allocator is not up yet then do not call c_p_a():
518 */
519 if (!debug_pagealloc_enabled)
520 return;
521
522 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100523 * The return value is ignored - the calls cannot fail,
524 * large pages are disabled at boot time:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 */
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100526 if (enable)
527 __set_pages_p(page, numpages);
528 else
529 __set_pages_np(page, numpages);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100530
531 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100532 * We should perform an IPI and flush all tlbs,
533 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 */
535 __flush_tlb_all();
536}
537#endif
Arjan van de Vend1028a12008-01-30 13:34:07 +0100538
539/*
540 * The testcases use internal knowledge of the implementation that shouldn't
541 * be exposed to the rest of the kernel. Include these directly here.
542 */
543#ifdef CONFIG_CPA_DEBUG
544#include "pageattr-test.c"
545#endif