blob: 79a9f1b42ddd60c5d0f24290b691eaba5021514d [file] [log] [blame]
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001/*
2 * Copyright 2002 Andi Kleen, SuSE Labs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Thanks to Ben LaHaise for precious feedback.
Ingo Molnar9f4c8152008-01-30 13:33:41 +01004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/highmem.h>
Ingo Molnar81922062008-01-30 13:34:04 +01006#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
Ingo Molnar9f4c8152008-01-30 13:33:41 +01008#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/slab.h>
Ingo Molnar9f4c8152008-01-30 13:33:41 +010010#include <linux/mm.h>
11
Thomas Gleixner950f9d92008-01-30 13:34:06 +010012#include <asm/e820.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/processor.h>
14#include <asm/tlbflush.h>
Dave Jonesf8af0952006-01-06 00:12:10 -080015#include <asm/sections.h>
Ingo Molnar9f4c8152008-01-30 13:33:41 +010016#include <asm/uaccess.h>
17#include <asm/pgalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Thomas Gleixner72e458d2008-02-04 16:48:07 +010019struct cpa_data {
20 unsigned long vaddr;
21 int numpages;
22 pgprot_t mask_set;
23 pgprot_t mask_clr;
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +010024 int flushtlb;
Thomas Gleixner72e458d2008-02-04 16:48:07 +010025};
26
Arjan van de Vened724be2008-01-30 13:34:04 +010027static inline int
28within(unsigned long addr, unsigned long start, unsigned long end)
Ingo Molnar687c4822008-01-30 13:34:04 +010029{
Arjan van de Vened724be2008-01-30 13:34:04 +010030 return addr >= start && addr < end;
31}
32
33/*
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010034 * Flushing functions
35 */
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010036
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010037/**
38 * clflush_cache_range - flush a cache range with clflush
39 * @addr: virtual start address
40 * @size: number of bytes to flush
41 *
42 * clflush is an unordered instruction which needs fencing with mfence
43 * to avoid ordering issues.
44 */
Ingo Molnar4c61afc2008-01-30 13:34:09 +010045void clflush_cache_range(void *vaddr, unsigned int size)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010046{
Ingo Molnar4c61afc2008-01-30 13:34:09 +010047 void *vend = vaddr + size - 1;
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010048
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010049 mb();
Ingo Molnar4c61afc2008-01-30 13:34:09 +010050
51 for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
52 clflush(vaddr);
53 /*
54 * Flush any possible final partial cacheline:
55 */
56 clflush(vend);
57
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010058 mb();
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010059}
60
Thomas Gleixneraf1e6842008-01-30 13:34:08 +010061static void __cpa_flush_all(void *arg)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010062{
Andi Kleen6bb83832008-02-04 16:48:06 +010063 unsigned long cache = (unsigned long)arg;
64
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010065 /*
66 * Flush all to work around Errata in early athlons regarding
67 * large page flushing.
68 */
69 __flush_tlb_all();
70
Andi Kleen6bb83832008-02-04 16:48:06 +010071 if (cache && boot_cpu_data.x86_model >= 4)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010072 wbinvd();
73}
74
Andi Kleen6bb83832008-02-04 16:48:06 +010075static void cpa_flush_all(unsigned long cache)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010076{
77 BUG_ON(irqs_disabled());
78
Andi Kleen6bb83832008-02-04 16:48:06 +010079 on_each_cpu(__cpa_flush_all, (void *) cache, 1, 1);
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010080}
81
Thomas Gleixner57a6a462008-01-30 13:34:08 +010082static void __cpa_flush_range(void *arg)
83{
Thomas Gleixner57a6a462008-01-30 13:34:08 +010084 /*
85 * We could optimize that further and do individual per page
86 * tlb invalidates for a low number of pages. Caveat: we must
87 * flush the high aliases on 64bit as well.
88 */
89 __flush_tlb_all();
Thomas Gleixner57a6a462008-01-30 13:34:08 +010090}
91
Andi Kleen6bb83832008-02-04 16:48:06 +010092static void cpa_flush_range(unsigned long start, int numpages, int cache)
Thomas Gleixner57a6a462008-01-30 13:34:08 +010093{
Ingo Molnar4c61afc2008-01-30 13:34:09 +010094 unsigned int i, level;
95 unsigned long addr;
96
Thomas Gleixner57a6a462008-01-30 13:34:08 +010097 BUG_ON(irqs_disabled());
Ingo Molnar4c61afc2008-01-30 13:34:09 +010098 WARN_ON(PAGE_ALIGN(start) != start);
Thomas Gleixner57a6a462008-01-30 13:34:08 +010099
Thomas Gleixner3b233e52008-01-30 13:34:08 +0100100 on_each_cpu(__cpa_flush_range, NULL, 1, 1);
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100101
Andi Kleen6bb83832008-02-04 16:48:06 +0100102 if (!cache)
103 return;
104
Thomas Gleixner3b233e52008-01-30 13:34:08 +0100105 /*
106 * We only need to flush on one CPU,
107 * clflush is a MESI-coherent instruction that
108 * will cause all other CPUs to flush the same
109 * cachelines:
110 */
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100111 for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
112 pte_t *pte = lookup_address(addr, &level);
113
114 /*
115 * Only flush present addresses:
116 */
117 if (pte && pte_present(*pte))
118 clflush_cache_range((void *) addr, PAGE_SIZE);
119 }
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100120}
121
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100122#define HIGH_MAP_START __START_KERNEL_map
123#define HIGH_MAP_END (__START_KERNEL_map + KERNEL_TEXT_SIZE)
124
125
126/*
127 * Converts a virtual address to a X86-64 highmap address
128 */
129static unsigned long virt_to_highmap(void *address)
130{
131#ifdef CONFIG_X86_64
132 return __pa((unsigned long)address) + HIGH_MAP_START - phys_base;
133#else
134 return (unsigned long)address;
135#endif
136}
137
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100138/*
Arjan van de Vened724be2008-01-30 13:34:04 +0100139 * Certain areas of memory on x86 require very specific protection flags,
140 * for example the BIOS area or kernel text. Callers don't always get this
141 * right (again, ioremap() on BIOS memory is not uncommon) so this function
142 * checks and fixes these known static required protection bits.
143 */
144static inline pgprot_t static_protections(pgprot_t prot, unsigned long address)
145{
146 pgprot_t forbidden = __pgprot(0);
147
Ingo Molnar687c4822008-01-30 13:34:04 +0100148 /*
Arjan van de Vened724be2008-01-30 13:34:04 +0100149 * The BIOS area between 640k and 1Mb needs to be executable for
150 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
Ingo Molnar687c4822008-01-30 13:34:04 +0100151 */
Arjan van de Vened724be2008-01-30 13:34:04 +0100152 if (within(__pa(address), BIOS_BEGIN, BIOS_END))
153 pgprot_val(forbidden) |= _PAGE_NX;
154
155 /*
156 * The kernel text needs to be executable for obvious reasons
157 * Does not cover __inittext since that is gone later on
158 */
159 if (within(address, (unsigned long)_text, (unsigned long)_etext))
160 pgprot_val(forbidden) |= _PAGE_NX;
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100161 /*
162 * Do the same for the x86-64 high kernel mapping
163 */
164 if (within(address, virt_to_highmap(_text), virt_to_highmap(_etext)))
165 pgprot_val(forbidden) |= _PAGE_NX;
166
Arjan van de Vened724be2008-01-30 13:34:04 +0100167
168#ifdef CONFIG_DEBUG_RODATA
169 /* The .rodata section needs to be read-only */
170 if (within(address, (unsigned long)__start_rodata,
171 (unsigned long)__end_rodata))
172 pgprot_val(forbidden) |= _PAGE_RW;
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100173 /*
174 * Do the same for the x86-64 high kernel mapping
175 */
176 if (within(address, virt_to_highmap(__start_rodata),
177 virt_to_highmap(__end_rodata)))
178 pgprot_val(forbidden) |= _PAGE_RW;
Arjan van de Vened724be2008-01-30 13:34:04 +0100179#endif
180
181 prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
Ingo Molnar687c4822008-01-30 13:34:04 +0100182
183 return prot;
184}
185
Ingo Molnarf0646e42008-01-30 13:33:43 +0100186pte_t *lookup_address(unsigned long address, int *level)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100187{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 pgd_t *pgd = pgd_offset_k(address);
189 pud_t *pud;
190 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100191
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100192 *level = PG_LEVEL_NONE;
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (pgd_none(*pgd))
195 return NULL;
196 pud = pud_offset(pgd, address);
197 if (pud_none(*pud))
198 return NULL;
199 pmd = pmd_offset(pud, address);
200 if (pmd_none(*pmd))
201 return NULL;
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100202
203 *level = PG_LEVEL_2M;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (pmd_large(*pmd))
205 return (pte_t *)pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100207 *level = PG_LEVEL_4K;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100208 return pte_offset_kernel(pmd, address);
209}
210
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100211static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100212{
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100213 /* change init_mm */
214 set_pte_atomic(kpte, pte);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100215#ifdef CONFIG_X86_32
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100216 if (!SHARED_KERNEL_PMD) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100217 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Jeremy Fitzhardingee3ed9102008-01-30 13:34:11 +0100219 list_for_each_entry(page, &pgd_list, lru) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100220 pgd_t *pgd;
221 pud_t *pud;
222 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100223
Ingo Molnar44af6c42008-01-30 13:34:03 +0100224 pgd = (pgd_t *)page_address(page) + pgd_index(address);
225 pud = pud_offset(pgd, address);
226 pmd = pmd_offset(pud, address);
227 set_pte_atomic((pte_t *)pmd, pte);
228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
Ingo Molnar44af6c42008-01-30 13:34:03 +0100230#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100233static int split_large_page(pte_t *kpte, unsigned long address)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100234{
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100235 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnar12d6f212008-01-30 13:33:58 +0100236 gfp_t gfp_flags = GFP_KERNEL;
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100237 unsigned long flags, addr, pfn;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100238 pte_t *pbase, *tmp;
239 struct page *base;
Ingo Molnar86f03982008-01-30 13:34:09 +0100240 unsigned int i, level;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100241
Ingo Molnar12d6f212008-01-30 13:33:58 +0100242#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnar86f03982008-01-30 13:34:09 +0100243 gfp_flags = __GFP_HIGH | __GFP_NOFAIL | __GFP_NOWARN;
244 gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Ingo Molnar12d6f212008-01-30 13:33:58 +0100245#endif
246 base = alloc_pages(gfp_flags, 0);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100247 if (!base)
248 return -ENOMEM;
249
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100250 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100251 /*
252 * Check for races, another CPU might have split this page
253 * up for us already:
254 */
255 tmp = lookup_address(address, &level);
Ingo Molnar5508a742008-01-30 13:33:56 +0100256 if (tmp != kpte) {
257 WARN_ON_ONCE(1);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100258 goto out_unlock;
Ingo Molnar5508a742008-01-30 13:33:56 +0100259 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100260
261 address = __pa(address);
262 addr = address & LARGE_PAGE_MASK;
263 pbase = (pte_t *)page_address(base);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100264#ifdef CONFIG_X86_32
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100265 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
Ingo Molnar44af6c42008-01-30 13:34:03 +0100266#endif
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100267
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100268 /*
269 * Get the target pfn from the original entry:
270 */
271 pfn = pte_pfn(*kpte);
272 for (i = 0; i < PTRS_PER_PTE; i++, pfn++)
273 set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100274
275 /*
Huang, Ying4c881ca2008-01-30 13:34:04 +0100276 * Install the new, split up pagetable. Important detail here:
277 *
278 * On Intel the NX bit of all levels must be cleared to make a
279 * page executable. See section 4.13.2 of Intel 64 and IA-32
280 * Architectures Software Developer's Manual).
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100281 */
Huang, Ying4c881ca2008-01-30 13:34:04 +0100282 ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100283 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100284 base = NULL;
285
286out_unlock:
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100287 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100288
289 if (base)
290 __free_pages(base, 0);
291
292 return 0;
293}
294
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100295static int __change_page_attr(unsigned long address, struct cpa_data *cpa)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100296{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 struct page *kpte_page;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100298 int level, err = 0;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100299 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100301repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100302 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (!kpte)
304 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200307 BUG_ON(PageLRU(kpte_page));
308 BUG_ON(PageCompound(kpte_page));
309
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100310 if (level == PG_LEVEL_4K) {
Ingo Molnar86f03982008-01-30 13:34:09 +0100311 pte_t new_pte, old_pte = *kpte;
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100312 pgprot_t new_prot = pte_pgprot(old_pte);
313
314 if(!pte_val(old_pte)) {
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100315 printk(KERN_WARNING "CPA: called for zero pte. "
316 "vaddr = %lx cpa->vaddr = %lx\n", address,
317 cpa->vaddr);
318 WARN_ON(1);
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100319 return -EINVAL;
320 }
Thomas Gleixnera72a08a2008-01-30 13:34:07 +0100321
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100322 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
323 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
Ingo Molnar86f03982008-01-30 13:34:09 +0100324
325 new_prot = static_protections(new_prot, address);
326
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100327 /*
328 * We need to keep the pfn from the existing PTE,
329 * after all we're only going to change it's attributes
330 * not the memory it points to
331 */
332 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100333
334 /*
335 * Do we really change anything ?
336 */
337 if (pte_val(old_pte) != pte_val(new_pte)) {
338 set_pte_atomic(kpte, new_pte);
339 cpa->flushtlb = 1;
340 }
Ingo Molnar86f03982008-01-30 13:34:09 +0100341 } else {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100342 err = split_large_page(kpte, address);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100343 if (!err)
344 goto repeat;
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100345 cpa->flushtlb = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100347 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100348}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Ingo Molnar44af6c42008-01-30 13:34:03 +0100350/**
351 * change_page_attr_addr - Change page table attributes in linear mapping
352 * @address: Virtual address in linear mapping.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100353 * @prot: New page table attribute (PAGE_*)
354 *
355 * Change page attributes of a page in the direct mapping. This is a variant
356 * of change_page_attr() that also works on memory holes that do not have
357 * mem_map entry (pfn_valid() is false).
358 *
359 * See change_page_attr() documentation for more details.
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100360 *
361 * Modules and drivers should use the set_memory_* APIs instead.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100362 */
363
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100364static int change_page_attr_addr(struct cpa_data *cpa)
Ingo Molnar44af6c42008-01-30 13:34:03 +0100365{
Thomas Gleixner08797502008-01-30 13:34:09 +0100366 int err;
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100367 unsigned long address = cpa->vaddr;
Ingo Molnar44af6c42008-01-30 13:34:03 +0100368
Arjan van de Ven488fd992008-01-30 13:34:07 +0100369#ifdef CONFIG_X86_64
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100370 unsigned long phys_addr = __pa(address);
371
Arjan van de Ven488fd992008-01-30 13:34:07 +0100372 /*
Thomas Gleixner08797502008-01-30 13:34:09 +0100373 * If we are inside the high mapped kernel range, then we
374 * fixup the low mapping first. __va() returns the virtual
375 * address in the linear mapping:
Arjan van de Ven488fd992008-01-30 13:34:07 +0100376 */
Thomas Gleixner08797502008-01-30 13:34:09 +0100377 if (within(address, HIGH_MAP_START, HIGH_MAP_END))
378 address = (unsigned long) __va(phys_addr);
Arjan van de Ven488fd992008-01-30 13:34:07 +0100379#endif
380
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100381 err = __change_page_attr(address, cpa);
Thomas Gleixner08797502008-01-30 13:34:09 +0100382 if (err)
383 return err;
384
385#ifdef CONFIG_X86_64
386 /*
387 * If the physical address is inside the kernel map, we need
388 * to touch the high mapped kernel as well:
389 */
390 if (within(phys_addr, 0, KERNEL_TEXT_SIZE)) {
391 /*
392 * Calc the high mapping address. See __phys_addr()
393 * for the non obvious details.
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100394 *
395 * Note that NX and other required permissions are
396 * checked in static_protections().
Thomas Gleixner08797502008-01-30 13:34:09 +0100397 */
398 address = phys_addr + HIGH_MAP_START - phys_base;
Thomas Gleixner08797502008-01-30 13:34:09 +0100399
400 /*
401 * Our high aliases are imprecise, because we check
402 * everything between 0 and KERNEL_TEXT_SIZE, so do
403 * not propagate lookup failures back to users:
404 */
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100405 __change_page_attr(address, cpa);
Thomas Gleixner08797502008-01-30 13:34:09 +0100406 }
407#endif
Ingo Molnar44af6c42008-01-30 13:34:03 +0100408 return err;
409}
410
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100411static int __change_page_attr_set_clr(struct cpa_data *cpa)
Thomas Gleixnerff314522008-01-30 13:34:08 +0100412{
Ingo Molnar86f03982008-01-30 13:34:09 +0100413 unsigned int i;
414 int ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100415
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100416 for (i = 0; i < cpa->numpages ; i++, cpa->vaddr += PAGE_SIZE) {
417 ret = change_page_attr_addr(cpa);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100418 if (ret)
419 return ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100420 }
421
422 return 0;
423}
424
Andi Kleen6bb83832008-02-04 16:48:06 +0100425static inline int cache_attr(pgprot_t attr)
426{
427 return pgprot_val(attr) &
428 (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
429}
430
Thomas Gleixnerff314522008-01-30 13:34:08 +0100431static int change_page_attr_set_clr(unsigned long addr, int numpages,
432 pgprot_t mask_set, pgprot_t mask_clr)
433{
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100434 struct cpa_data cpa;
Andi Kleen6bb83832008-02-04 16:48:06 +0100435 int ret, cache;
Thomas Gleixner331e4062008-02-04 16:48:06 +0100436
437 /*
438 * Check, if we are requested to change a not supported
439 * feature:
440 */
441 mask_set = canon_pgprot(mask_set);
442 mask_clr = canon_pgprot(mask_clr);
443 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr))
444 return 0;
445
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100446 cpa.vaddr = addr;
447 cpa.numpages = numpages;
448 cpa.mask_set = mask_set;
449 cpa.mask_clr = mask_clr;
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100450 cpa.flushtlb = 0;
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100451
452 ret = __change_page_attr_set_clr(&cpa);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100453
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100454 /*
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100455 * Check whether we really changed something:
456 */
457 if (!cpa.flushtlb)
458 return ret;
459
460 /*
Andi Kleen6bb83832008-02-04 16:48:06 +0100461 * No need to flush, when we did not set any of the caching
462 * attributes:
463 */
464 cache = cache_attr(mask_set);
465
466 /*
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100467 * On success we use clflush, when the CPU supports it to
468 * avoid the wbindv. If the CPU does not support it and in the
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100469 * error case we fall back to cpa_flush_all (which uses
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100470 * wbindv):
471 */
472 if (!ret && cpu_has_clflush)
Andi Kleen6bb83832008-02-04 16:48:06 +0100473 cpa_flush_range(addr, numpages, cache);
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100474 else
Andi Kleen6bb83832008-02-04 16:48:06 +0100475 cpa_flush_all(cache);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100476
477 return ret;
478}
479
Thomas Gleixner56744542008-01-30 13:34:08 +0100480static inline int change_page_attr_set(unsigned long addr, int numpages,
481 pgprot_t mask)
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100482{
Thomas Gleixner56744542008-01-30 13:34:08 +0100483 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100484}
485
Thomas Gleixner56744542008-01-30 13:34:08 +0100486static inline int change_page_attr_clear(unsigned long addr, int numpages,
487 pgprot_t mask)
Thomas Gleixner72932c72008-01-30 13:34:08 +0100488{
Huang, Ying58270402008-01-31 22:05:43 +0100489 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100490}
491
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100492int set_memory_uc(unsigned long addr, int numpages)
493{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100494 return change_page_attr_set(addr, numpages,
495 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100496}
497EXPORT_SYMBOL(set_memory_uc);
498
499int set_memory_wb(unsigned long addr, int numpages)
500{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100501 return change_page_attr_clear(addr, numpages,
502 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100503}
504EXPORT_SYMBOL(set_memory_wb);
505
506int set_memory_x(unsigned long addr, int numpages)
507{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100508 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100509}
510EXPORT_SYMBOL(set_memory_x);
511
512int set_memory_nx(unsigned long addr, int numpages)
513{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100514 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100515}
516EXPORT_SYMBOL(set_memory_nx);
517
518int set_memory_ro(unsigned long addr, int numpages)
519{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100520 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100521}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100522
523int set_memory_rw(unsigned long addr, int numpages)
524{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100525 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100526}
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100527
528int set_memory_np(unsigned long addr, int numpages)
529{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100530 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100531}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100532
533int set_pages_uc(struct page *page, int numpages)
534{
535 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100536
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100537 return set_memory_uc(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100538}
539EXPORT_SYMBOL(set_pages_uc);
540
541int set_pages_wb(struct page *page, int numpages)
542{
543 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100544
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100545 return set_memory_wb(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100546}
547EXPORT_SYMBOL(set_pages_wb);
548
549int set_pages_x(struct page *page, int numpages)
550{
551 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100552
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100553 return set_memory_x(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100554}
555EXPORT_SYMBOL(set_pages_x);
556
557int set_pages_nx(struct page *page, int numpages)
558{
559 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100560
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100561 return set_memory_nx(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100562}
563EXPORT_SYMBOL(set_pages_nx);
564
565int set_pages_ro(struct page *page, int numpages)
566{
567 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100568
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100569 return set_memory_ro(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100570}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100571
572int set_pages_rw(struct page *page, int numpages)
573{
574 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100575
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100576 return set_memory_rw(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100577}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100580
581static int __set_pages_p(struct page *page, int numpages)
582{
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100583 struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
584 .numpages = numpages,
585 .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
586 .mask_clr = __pgprot(0)};
Thomas Gleixner72932c72008-01-30 13:34:08 +0100587
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100588 return __change_page_attr_set_clr(&cpa);
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100589}
590
591static int __set_pages_np(struct page *page, int numpages)
592{
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100593 struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
594 .numpages = numpages,
595 .mask_set = __pgprot(0),
596 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW)};
Thomas Gleixner72932c72008-01-30 13:34:08 +0100597
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100598 return __change_page_attr_set_clr(&cpa);
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601void kernel_map_pages(struct page *page, int numpages, int enable)
602{
603 if (PageHighMem(page))
604 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100605 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700606 debug_check_no_locks_freed(page_address(page),
607 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100608 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800609
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100610 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +0100611 * If page allocator is not up yet then do not call c_p_a():
612 */
613 if (!debug_pagealloc_enabled)
614 return;
615
616 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100617 * The return value is ignored - the calls cannot fail,
618 * large pages are disabled at boot time:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100620 if (enable)
621 __set_pages_p(page, numpages);
622 else
623 __set_pages_np(page, numpages);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100624
625 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100626 * We should perform an IPI and flush all tlbs,
627 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 */
629 __flush_tlb_all();
630}
631#endif
Arjan van de Vend1028a12008-01-30 13:34:07 +0100632
633/*
634 * The testcases use internal knowledge of the implementation that shouldn't
635 * be exposed to the rest of the kernel. Include these directly here.
636 */
637#ifdef CONFIG_CPA_DEBUG
638#include "pageattr-test.c"
639#endif