blob: eb2a54415a77a12c99d13bf8b03a5aa42c377bf6 [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
Ingo Molnar9df84992008-02-04 16:48:09 +010019/*
20 * The current flushing context - we pass it instead of 5 arguments:
21 */
Thomas Gleixner72e458d2008-02-04 16:48:07 +010022struct cpa_data {
23 unsigned long vaddr;
Thomas Gleixner72e458d2008-02-04 16:48:07 +010024 pgprot_t mask_set;
25 pgprot_t mask_clr;
Thomas Gleixner65e074d2008-02-04 16:48:07 +010026 int numpages;
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +010027 int flushtlb;
Thomas Gleixner72e458d2008-02-04 16:48:07 +010028};
29
Arjan van de Vened724be2008-01-30 13:34:04 +010030static inline int
31within(unsigned long addr, unsigned long start, unsigned long end)
Ingo Molnar687c4822008-01-30 13:34:04 +010032{
Arjan van de Vened724be2008-01-30 13:34:04 +010033 return addr >= start && addr < end;
34}
35
36/*
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010037 * Flushing functions
38 */
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010039
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010040/**
41 * clflush_cache_range - flush a cache range with clflush
42 * @addr: virtual start address
43 * @size: number of bytes to flush
44 *
45 * clflush is an unordered instruction which needs fencing with mfence
46 * to avoid ordering issues.
47 */
Ingo Molnar4c61afc2008-01-30 13:34:09 +010048void clflush_cache_range(void *vaddr, unsigned int size)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010049{
Ingo Molnar4c61afc2008-01-30 13:34:09 +010050 void *vend = vaddr + size - 1;
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010051
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010052 mb();
Ingo Molnar4c61afc2008-01-30 13:34:09 +010053
54 for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
55 clflush(vaddr);
56 /*
57 * Flush any possible final partial cacheline:
58 */
59 clflush(vend);
60
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +010061 mb();
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010062}
63
Thomas Gleixneraf1e6842008-01-30 13:34:08 +010064static void __cpa_flush_all(void *arg)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010065{
Andi Kleen6bb83832008-02-04 16:48:06 +010066 unsigned long cache = (unsigned long)arg;
67
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010068 /*
69 * Flush all to work around Errata in early athlons regarding
70 * large page flushing.
71 */
72 __flush_tlb_all();
73
Andi Kleen6bb83832008-02-04 16:48:06 +010074 if (cache && boot_cpu_data.x86_model >= 4)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010075 wbinvd();
76}
77
Andi Kleen6bb83832008-02-04 16:48:06 +010078static void cpa_flush_all(unsigned long cache)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010079{
80 BUG_ON(irqs_disabled());
81
Andi Kleen6bb83832008-02-04 16:48:06 +010082 on_each_cpu(__cpa_flush_all, (void *) cache, 1, 1);
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +010083}
84
Thomas Gleixner57a6a462008-01-30 13:34:08 +010085static void __cpa_flush_range(void *arg)
86{
Thomas Gleixner57a6a462008-01-30 13:34:08 +010087 /*
88 * We could optimize that further and do individual per page
89 * tlb invalidates for a low number of pages. Caveat: we must
90 * flush the high aliases on 64bit as well.
91 */
92 __flush_tlb_all();
Thomas Gleixner57a6a462008-01-30 13:34:08 +010093}
94
Andi Kleen6bb83832008-02-04 16:48:06 +010095static void cpa_flush_range(unsigned long start, int numpages, int cache)
Thomas Gleixner57a6a462008-01-30 13:34:08 +010096{
Ingo Molnar4c61afc2008-01-30 13:34:09 +010097 unsigned int i, level;
98 unsigned long addr;
99
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100100 BUG_ON(irqs_disabled());
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100101 WARN_ON(PAGE_ALIGN(start) != start);
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100102
Thomas Gleixner3b233e52008-01-30 13:34:08 +0100103 on_each_cpu(__cpa_flush_range, NULL, 1, 1);
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100104
Andi Kleen6bb83832008-02-04 16:48:06 +0100105 if (!cache)
106 return;
107
Thomas Gleixner3b233e52008-01-30 13:34:08 +0100108 /*
109 * We only need to flush on one CPU,
110 * clflush is a MESI-coherent instruction that
111 * will cause all other CPUs to flush the same
112 * cachelines:
113 */
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100114 for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
115 pte_t *pte = lookup_address(addr, &level);
116
117 /*
118 * Only flush present addresses:
119 */
Thomas Gleixner7bfb72e2008-02-04 16:48:08 +0100120 if (pte && (pte_val(*pte) & _PAGE_PRESENT))
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100121 clflush_cache_range((void *) addr, PAGE_SIZE);
122 }
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100123}
124
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100125#define HIGH_MAP_START __START_KERNEL_map
126#define HIGH_MAP_END (__START_KERNEL_map + KERNEL_TEXT_SIZE)
127
128
129/*
130 * Converts a virtual address to a X86-64 highmap address
131 */
132static unsigned long virt_to_highmap(void *address)
133{
134#ifdef CONFIG_X86_64
135 return __pa((unsigned long)address) + HIGH_MAP_START - phys_base;
136#else
137 return (unsigned long)address;
138#endif
139}
140
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100141/*
Arjan van de Vened724be2008-01-30 13:34:04 +0100142 * Certain areas of memory on x86 require very specific protection flags,
143 * for example the BIOS area or kernel text. Callers don't always get this
144 * right (again, ioremap() on BIOS memory is not uncommon) so this function
145 * checks and fixes these known static required protection bits.
146 */
147static inline pgprot_t static_protections(pgprot_t prot, unsigned long address)
148{
149 pgprot_t forbidden = __pgprot(0);
150
Ingo Molnar687c4822008-01-30 13:34:04 +0100151 /*
Arjan van de Vened724be2008-01-30 13:34:04 +0100152 * The BIOS area between 640k and 1Mb needs to be executable for
153 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
Ingo Molnar687c4822008-01-30 13:34:04 +0100154 */
Arjan van de Vened724be2008-01-30 13:34:04 +0100155 if (within(__pa(address), BIOS_BEGIN, BIOS_END))
156 pgprot_val(forbidden) |= _PAGE_NX;
157
158 /*
159 * The kernel text needs to be executable for obvious reasons
160 * Does not cover __inittext since that is gone later on
161 */
162 if (within(address, (unsigned long)_text, (unsigned long)_etext))
163 pgprot_val(forbidden) |= _PAGE_NX;
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100164 /*
165 * Do the same for the x86-64 high kernel mapping
166 */
167 if (within(address, virt_to_highmap(_text), virt_to_highmap(_etext)))
168 pgprot_val(forbidden) |= _PAGE_NX;
169
Arjan van de Vened724be2008-01-30 13:34:04 +0100170 /* The .rodata section needs to be read-only */
171 if (within(address, (unsigned long)__start_rodata,
172 (unsigned long)__end_rodata))
173 pgprot_val(forbidden) |= _PAGE_RW;
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100174 /*
175 * Do the same for the x86-64 high kernel mapping
176 */
177 if (within(address, virt_to_highmap(__start_rodata),
178 virt_to_highmap(__end_rodata)))
179 pgprot_val(forbidden) |= _PAGE_RW;
Arjan van de Vened724be2008-01-30 13:34:04 +0100180
181 prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
Ingo Molnar687c4822008-01-30 13:34:04 +0100182
183 return prot;
184}
185
Thomas Gleixner9a14aef2008-02-04 16:48:07 +0100186/*
187 * Lookup the page table entry for a virtual address. Return a pointer
188 * to the entry and the level of the mapping.
189 *
190 * Note: We return pud and pmd either when the entry is marked large
191 * or when the present bit is not set. Otherwise we would return a
192 * pointer to a nonexisting mapping.
193 */
Harvey Harrisonda7bfc52008-02-09 23:24:08 +0100194pte_t *lookup_address(unsigned long address, unsigned int *level)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100195{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 pgd_t *pgd = pgd_offset_k(address);
197 pud_t *pud;
198 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100199
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100200 *level = PG_LEVEL_NONE;
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (pgd_none(*pgd))
203 return NULL;
Ingo Molnar9df84992008-02-04 16:48:09 +0100204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 pud = pud_offset(pgd, address);
206 if (pud_none(*pud))
207 return NULL;
Andi Kleenc2f71ee2008-02-04 16:48:09 +0100208
209 *level = PG_LEVEL_1G;
210 if (pud_large(*pud) || !pud_present(*pud))
211 return (pte_t *)pud;
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 pmd = pmd_offset(pud, address);
214 if (pmd_none(*pmd))
215 return NULL;
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100216
217 *level = PG_LEVEL_2M;
Thomas Gleixner9a14aef2008-02-04 16:48:07 +0100218 if (pmd_large(*pmd) || !pmd_present(*pmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return (pte_t *)pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100221 *level = PG_LEVEL_4K;
Ingo Molnar9df84992008-02-04 16:48:09 +0100222
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100223 return pte_offset_kernel(pmd, address);
224}
225
Ingo Molnar9df84992008-02-04 16:48:09 +0100226/*
227 * Set the new pmd in all the pgds we know about:
228 */
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100229static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100230{
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100231 /* change init_mm */
232 set_pte_atomic(kpte, pte);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100233#ifdef CONFIG_X86_32
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100234 if (!SHARED_KERNEL_PMD) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100235 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Jeremy Fitzhardingee3ed9102008-01-30 13:34:11 +0100237 list_for_each_entry(page, &pgd_list, lru) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100238 pgd_t *pgd;
239 pud_t *pud;
240 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100241
Ingo Molnar44af6c42008-01-30 13:34:03 +0100242 pgd = (pgd_t *)page_address(page) + pgd_index(address);
243 pud = pud_offset(pgd, address);
244 pmd = pmd_offset(pud, address);
245 set_pte_atomic((pte_t *)pmd, pte);
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
Ingo Molnar44af6c42008-01-30 13:34:03 +0100248#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Ingo Molnar9df84992008-02-04 16:48:09 +0100251static int
252try_preserve_large_page(pte_t *kpte, unsigned long address,
253 struct cpa_data *cpa)
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100254{
255 unsigned long nextpage_addr, numpages, pmask, psize, flags;
256 pte_t new_pte, old_pte, *tmp;
257 pgprot_t old_prot, new_prot;
Harvey Harrisonda7bfc52008-02-09 23:24:08 +0100258 int do_split = 1;
259 unsigned int level;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100260
261 spin_lock_irqsave(&pgd_lock, flags);
262 /*
263 * Check for races, another CPU might have split this page
264 * up already:
265 */
266 tmp = lookup_address(address, &level);
267 if (tmp != kpte)
268 goto out_unlock;
269
270 switch (level) {
271 case PG_LEVEL_2M:
Andi Kleen31422c52008-02-04 16:48:08 +0100272 psize = PMD_PAGE_SIZE;
273 pmask = PMD_PAGE_MASK;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100274 break;
Andi Kleenf07333f2008-02-04 16:48:09 +0100275#ifdef CONFIG_X86_64
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100276 case PG_LEVEL_1G:
Andi Kleenf07333f2008-02-04 16:48:09 +0100277 psize = PMD_PAGE_SIZE;
278 pmask = PMD_PAGE_MASK;
279 break;
280#endif
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100281 default:
Ingo Molnarbeaff632008-02-04 16:48:09 +0100282 do_split = -EINVAL;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100283 goto out_unlock;
284 }
285
286 /*
287 * Calculate the number of pages, which fit into this large
288 * page starting at address:
289 */
290 nextpage_addr = (address + psize) & pmask;
291 numpages = (nextpage_addr - address) >> PAGE_SHIFT;
292 if (numpages < cpa->numpages)
293 cpa->numpages = numpages;
294
295 /*
296 * We are safe now. Check whether the new pgprot is the same:
297 */
298 old_pte = *kpte;
299 old_prot = new_prot = pte_pgprot(old_pte);
300
301 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
302 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
303 new_prot = static_protections(new_prot, address);
304
305 /*
306 * If there are no changes, return. maxpages has been updated
307 * above:
308 */
309 if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
Ingo Molnarbeaff632008-02-04 16:48:09 +0100310 do_split = 0;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100311 goto out_unlock;
312 }
313
314 /*
315 * We need to change the attributes. Check, whether we can
316 * change the large page in one go. We request a split, when
317 * the address is not aligned and the number of pages is
318 * smaller than the number of pages in the large page. Note
319 * that we limited the number of possible pages already to
320 * the number of pages in the large page.
321 */
322 if (address == (nextpage_addr - psize) && cpa->numpages == numpages) {
323 /*
324 * The address is aligned and the number of pages
325 * covers the full page.
326 */
327 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
328 __set_pmd_pte(kpte, address, new_pte);
329 cpa->flushtlb = 1;
Ingo Molnarbeaff632008-02-04 16:48:09 +0100330 do_split = 0;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100331 }
332
333out_unlock:
334 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnar9df84992008-02-04 16:48:09 +0100335
Ingo Molnarbeaff632008-02-04 16:48:09 +0100336 return do_split;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100337}
338
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100339static int split_large_page(pte_t *kpte, unsigned long address)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100340{
Thomas Gleixner7b610ee2008-02-04 16:48:10 +0100341 unsigned long flags, pfn, pfninc = 1;
Ingo Molnar9df84992008-02-04 16:48:09 +0100342 gfp_t gfp_flags = GFP_KERNEL;
Ingo Molnar86f03982008-01-30 13:34:09 +0100343 unsigned int i, level;
Ingo Molnar9df84992008-02-04 16:48:09 +0100344 pte_t *pbase, *tmp;
345 pgprot_t ref_prot;
346 struct page *base;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100347
Ingo Molnar12d6f212008-01-30 13:33:58 +0100348#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnar86f03982008-01-30 13:34:09 +0100349 gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
Ingo Molnar12d6f212008-01-30 13:33:58 +0100350#endif
351 base = alloc_pages(gfp_flags, 0);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100352 if (!base)
353 return -ENOMEM;
354
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100355 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100356 /*
357 * Check for races, another CPU might have split this page
358 * up for us already:
359 */
360 tmp = lookup_address(address, &level);
Ingo Molnar6ce9fc12008-02-04 16:48:08 +0100361 if (tmp != kpte)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100362 goto out_unlock;
363
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100364 pbase = (pte_t *)page_address(base);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100365#ifdef CONFIG_X86_32
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100366 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
Ingo Molnar44af6c42008-01-30 13:34:03 +0100367#endif
Thomas Gleixner07cf89c2008-02-04 16:48:08 +0100368 ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100369
Andi Kleenf07333f2008-02-04 16:48:09 +0100370#ifdef CONFIG_X86_64
371 if (level == PG_LEVEL_1G) {
372 pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
373 pgprot_val(ref_prot) |= _PAGE_PSE;
Andi Kleenf07333f2008-02-04 16:48:09 +0100374 }
375#endif
376
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100377 /*
378 * Get the target pfn from the original entry:
379 */
380 pfn = pte_pfn(*kpte);
Andi Kleenf07333f2008-02-04 16:48:09 +0100381 for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100382 set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100383
384 /*
Thomas Gleixner07cf89c2008-02-04 16:48:08 +0100385 * Install the new, split up pagetable. Important details here:
Huang, Ying4c881ca2008-01-30 13:34:04 +0100386 *
387 * On Intel the NX bit of all levels must be cleared to make a
388 * page executable. See section 4.13.2 of Intel 64 and IA-32
389 * Architectures Software Developer's Manual).
Thomas Gleixner07cf89c2008-02-04 16:48:08 +0100390 *
391 * Mark the entry present. The current mapping might be
392 * set to not present, which we preserved above.
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100393 */
Huang, Ying4c881ca2008-01-30 13:34:04 +0100394 ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
Thomas Gleixner07cf89c2008-02-04 16:48:08 +0100395 pgprot_val(ref_prot) |= _PAGE_PRESENT;
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100396 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100397 base = NULL;
398
399out_unlock:
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100400 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100401
402 if (base)
403 __free_pages(base, 0);
404
405 return 0;
406}
407
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100408static int __change_page_attr(unsigned long address, struct cpa_data *cpa)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100409{
Harvey Harrisonda7bfc52008-02-09 23:24:08 +0100410 int do_split, err;
411 unsigned int level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 struct page *kpte_page;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100413 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100415repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100416 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (!kpte)
418 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200421 BUG_ON(PageLRU(kpte_page));
422 BUG_ON(PageCompound(kpte_page));
423
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100424 if (level == PG_LEVEL_4K) {
Ingo Molnar86f03982008-01-30 13:34:09 +0100425 pte_t new_pte, old_pte = *kpte;
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100426 pgprot_t new_prot = pte_pgprot(old_pte);
427
428 if(!pte_val(old_pte)) {
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100429 printk(KERN_WARNING "CPA: called for zero pte. "
430 "vaddr = %lx cpa->vaddr = %lx\n", address,
431 cpa->vaddr);
432 WARN_ON(1);
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100433 return -EINVAL;
434 }
Thomas Gleixnera72a08a2008-01-30 13:34:07 +0100435
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100436 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
437 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
Ingo Molnar86f03982008-01-30 13:34:09 +0100438
439 new_prot = static_protections(new_prot, address);
440
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100441 /*
442 * We need to keep the pfn from the existing PTE,
443 * after all we're only going to change it's attributes
444 * not the memory it points to
445 */
446 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100447
448 /*
449 * Do we really change anything ?
450 */
451 if (pte_val(old_pte) != pte_val(new_pte)) {
452 set_pte_atomic(kpte, new_pte);
453 cpa->flushtlb = 1;
454 }
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100455 cpa->numpages = 1;
456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100458
459 /*
460 * Check, whether we can keep the large page intact
461 * and just change the pte:
462 */
Ingo Molnarbeaff632008-02-04 16:48:09 +0100463 do_split = try_preserve_large_page(kpte, address, cpa);
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100464 /*
465 * When the range fits into the existing large page,
466 * return. cp->numpages and cpa->tlbflush have been updated in
467 * try_large_page:
468 */
Ingo Molnar87f7f8f2008-02-04 16:48:10 +0100469 if (do_split <= 0)
470 return do_split;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100471
472 /*
473 * We have to split the large page:
474 */
Ingo Molnar87f7f8f2008-02-04 16:48:10 +0100475 err = split_large_page(kpte, address);
476 if (!err) {
477 cpa->flushtlb = 1;
478 goto repeat;
479 }
Ingo Molnarbeaff632008-02-04 16:48:09 +0100480
Ingo Molnar87f7f8f2008-02-04 16:48:10 +0100481 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100482}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Ingo Molnar44af6c42008-01-30 13:34:03 +0100484/**
485 * change_page_attr_addr - Change page table attributes in linear mapping
486 * @address: Virtual address in linear mapping.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100487 * @prot: New page table attribute (PAGE_*)
488 *
489 * Change page attributes of a page in the direct mapping. This is a variant
490 * of change_page_attr() that also works on memory holes that do not have
491 * mem_map entry (pfn_valid() is false).
492 *
493 * See change_page_attr() documentation for more details.
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100494 *
495 * Modules and drivers should use the set_memory_* APIs instead.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100496 */
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100497static int change_page_attr_addr(struct cpa_data *cpa)
Ingo Molnar44af6c42008-01-30 13:34:03 +0100498{
Thomas Gleixner08797502008-01-30 13:34:09 +0100499 int err;
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100500 unsigned long address = cpa->vaddr;
Ingo Molnar44af6c42008-01-30 13:34:03 +0100501
Arjan van de Ven488fd992008-01-30 13:34:07 +0100502#ifdef CONFIG_X86_64
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100503 unsigned long phys_addr = __pa(address);
504
Arjan van de Ven488fd992008-01-30 13:34:07 +0100505 /*
Thomas Gleixner08797502008-01-30 13:34:09 +0100506 * If we are inside the high mapped kernel range, then we
507 * fixup the low mapping first. __va() returns the virtual
508 * address in the linear mapping:
Arjan van de Ven488fd992008-01-30 13:34:07 +0100509 */
Thomas Gleixner08797502008-01-30 13:34:09 +0100510 if (within(address, HIGH_MAP_START, HIGH_MAP_END))
511 address = (unsigned long) __va(phys_addr);
Arjan van de Ven488fd992008-01-30 13:34:07 +0100512#endif
513
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100514 err = __change_page_attr(address, cpa);
Thomas Gleixner08797502008-01-30 13:34:09 +0100515 if (err)
516 return err;
517
518#ifdef CONFIG_X86_64
519 /*
520 * If the physical address is inside the kernel map, we need
521 * to touch the high mapped kernel as well:
522 */
523 if (within(phys_addr, 0, KERNEL_TEXT_SIZE)) {
524 /*
525 * Calc the high mapping address. See __phys_addr()
526 * for the non obvious details.
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100527 *
528 * Note that NX and other required permissions are
529 * checked in static_protections().
Thomas Gleixner08797502008-01-30 13:34:09 +0100530 */
531 address = phys_addr + HIGH_MAP_START - phys_base;
Thomas Gleixner08797502008-01-30 13:34:09 +0100532
533 /*
534 * Our high aliases are imprecise, because we check
535 * everything between 0 and KERNEL_TEXT_SIZE, so do
536 * not propagate lookup failures back to users:
537 */
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100538 __change_page_attr(address, cpa);
Thomas Gleixner08797502008-01-30 13:34:09 +0100539 }
540#endif
Ingo Molnar44af6c42008-01-30 13:34:03 +0100541 return err;
542}
543
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100544static int __change_page_attr_set_clr(struct cpa_data *cpa)
Thomas Gleixnerff314522008-01-30 13:34:08 +0100545{
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100546 int ret, numpages = cpa->numpages;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100547
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100548 while (numpages) {
549 /*
550 * Store the remaining nr of pages for the large page
551 * preservation check.
552 */
553 cpa->numpages = numpages;
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100554 ret = change_page_attr_addr(cpa);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100555 if (ret)
556 return ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100557
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100558 /*
559 * Adjust the number of pages with the result of the
560 * CPA operation. Either a large page has been
561 * preserved or a single page update happened.
562 */
563 BUG_ON(cpa->numpages > numpages);
564 numpages -= cpa->numpages;
565 cpa->vaddr += cpa->numpages * PAGE_SIZE;
566 }
Thomas Gleixnerff314522008-01-30 13:34:08 +0100567 return 0;
568}
569
Andi Kleen6bb83832008-02-04 16:48:06 +0100570static inline int cache_attr(pgprot_t attr)
571{
572 return pgprot_val(attr) &
573 (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
574}
575
Thomas Gleixnerff314522008-01-30 13:34:08 +0100576static int change_page_attr_set_clr(unsigned long addr, int numpages,
577 pgprot_t mask_set, pgprot_t mask_clr)
578{
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100579 struct cpa_data cpa;
Andi Kleen6bb83832008-02-04 16:48:06 +0100580 int ret, cache;
Thomas Gleixner331e4062008-02-04 16:48:06 +0100581
582 /*
583 * Check, if we are requested to change a not supported
584 * feature:
585 */
586 mask_set = canon_pgprot(mask_set);
587 mask_clr = canon_pgprot(mask_clr);
588 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr))
589 return 0;
590
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100591 cpa.vaddr = addr;
592 cpa.numpages = numpages;
593 cpa.mask_set = mask_set;
594 cpa.mask_clr = mask_clr;
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100595 cpa.flushtlb = 0;
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100596
597 ret = __change_page_attr_set_clr(&cpa);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100598
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100599 /*
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100600 * Check whether we really changed something:
601 */
602 if (!cpa.flushtlb)
603 return ret;
604
605 /*
Andi Kleen6bb83832008-02-04 16:48:06 +0100606 * No need to flush, when we did not set any of the caching
607 * attributes:
608 */
609 cache = cache_attr(mask_set);
610
611 /*
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100612 * On success we use clflush, when the CPU supports it to
613 * avoid the wbindv. If the CPU does not support it and in the
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100614 * error case we fall back to cpa_flush_all (which uses
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100615 * wbindv):
616 */
617 if (!ret && cpu_has_clflush)
Andi Kleen6bb83832008-02-04 16:48:06 +0100618 cpa_flush_range(addr, numpages, cache);
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100619 else
Andi Kleen6bb83832008-02-04 16:48:06 +0100620 cpa_flush_all(cache);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100621
622 return ret;
623}
624
Thomas Gleixner56744542008-01-30 13:34:08 +0100625static inline int change_page_attr_set(unsigned long addr, int numpages,
626 pgprot_t mask)
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100627{
Thomas Gleixner56744542008-01-30 13:34:08 +0100628 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100629}
630
Thomas Gleixner56744542008-01-30 13:34:08 +0100631static inline int change_page_attr_clear(unsigned long addr, int numpages,
632 pgprot_t mask)
Thomas Gleixner72932c72008-01-30 13:34:08 +0100633{
Huang, Ying58270402008-01-31 22:05:43 +0100634 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100635}
636
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100637int set_memory_uc(unsigned long addr, int numpages)
638{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100639 return change_page_attr_set(addr, numpages,
640 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100641}
642EXPORT_SYMBOL(set_memory_uc);
643
644int set_memory_wb(unsigned long addr, int numpages)
645{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100646 return change_page_attr_clear(addr, numpages,
647 __pgprot(_PAGE_PCD | _PAGE_PWT));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100648}
649EXPORT_SYMBOL(set_memory_wb);
650
651int set_memory_x(unsigned long addr, int numpages)
652{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100653 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100654}
655EXPORT_SYMBOL(set_memory_x);
656
657int set_memory_nx(unsigned long addr, int numpages)
658{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100659 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100660}
661EXPORT_SYMBOL(set_memory_nx);
662
663int set_memory_ro(unsigned long addr, int numpages)
664{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100665 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100666}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100667
668int set_memory_rw(unsigned long addr, int numpages)
669{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100670 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100671}
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100672
673int set_memory_np(unsigned long addr, int numpages)
674{
Thomas Gleixner72932c72008-01-30 13:34:08 +0100675 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100676}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100677
678int set_pages_uc(struct page *page, int numpages)
679{
680 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100681
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100682 return set_memory_uc(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100683}
684EXPORT_SYMBOL(set_pages_uc);
685
686int set_pages_wb(struct page *page, int numpages)
687{
688 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100689
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100690 return set_memory_wb(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100691}
692EXPORT_SYMBOL(set_pages_wb);
693
694int set_pages_x(struct page *page, int numpages)
695{
696 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100697
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100698 return set_memory_x(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100699}
700EXPORT_SYMBOL(set_pages_x);
701
702int set_pages_nx(struct page *page, int numpages)
703{
704 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100705
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100706 return set_memory_nx(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100707}
708EXPORT_SYMBOL(set_pages_nx);
709
710int set_pages_ro(struct page *page, int numpages)
711{
712 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100713
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100714 return set_memory_ro(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100715}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100716
717int set_pages_rw(struct page *page, int numpages)
718{
719 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100720
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100721 return set_memory_rw(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100722}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100725
726static int __set_pages_p(struct page *page, int numpages)
727{
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100728 struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
729 .numpages = numpages,
730 .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
731 .mask_clr = __pgprot(0)};
Thomas Gleixner72932c72008-01-30 13:34:08 +0100732
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100733 return __change_page_attr_set_clr(&cpa);
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100734}
735
736static int __set_pages_np(struct page *page, int numpages)
737{
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100738 struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
739 .numpages = numpages,
740 .mask_set = __pgprot(0),
741 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW)};
Thomas Gleixner72932c72008-01-30 13:34:08 +0100742
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100743 return __change_page_attr_set_clr(&cpa);
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100744}
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746void kernel_map_pages(struct page *page, int numpages, int enable)
747{
748 if (PageHighMem(page))
749 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100750 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700751 debug_check_no_locks_freed(page_address(page),
752 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100753 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800754
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100755 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +0100756 * If page allocator is not up yet then do not call c_p_a():
757 */
758 if (!debug_pagealloc_enabled)
759 return;
760
761 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100762 * The return value is ignored - the calls cannot fail,
763 * large pages are disabled at boot time:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 */
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100765 if (enable)
766 __set_pages_p(page, numpages);
767 else
768 __set_pages_np(page, numpages);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100769
770 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100771 * We should perform an IPI and flush all tlbs,
772 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 */
774 __flush_tlb_all();
775}
776#endif
Arjan van de Vend1028a12008-01-30 13:34:07 +0100777
778/*
779 * The testcases use internal knowledge of the implementation that shouldn't
780 * be exposed to the rest of the kernel. Include these directly here.
781 */
782#ifdef CONFIG_CPA_DEBUG
783#include "pageattr-test.c"
784#endif