2 * Copyright 2002 Andi Kleen, SuSE Labs.
3 * Thanks to Ben LaHaise for precious feedback.
5 #include <linux/highmem.h>
6 #include <linux/bootmem.h>
7 #include <linux/module.h>
8 #include <linux/sched.h>
9 #include <linux/slab.h>
13 #include <asm/processor.h>
14 #include <asm/tlbflush.h>
15 #include <asm/sections.h>
16 #include <asm/uaccess.h>
17 #include <asm/pgalloc.h>
20 * The current flushing context - we pass it instead of 5 arguments:
31 within(unsigned long addr, unsigned long start, unsigned long end)
33 return addr >= start && addr < end;
41 * clflush_cache_range - flush a cache range with clflush
42 * @addr: virtual start address
43 * @size: number of bytes to flush
45 * clflush is an unordered instruction which needs fencing with mfence
46 * to avoid ordering issues.
48 void clflush_cache_range(void *vaddr, unsigned int size)
50 void *vend = vaddr + size - 1;
54 for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
57 * Flush any possible final partial cacheline:
64 static void __cpa_flush_all(void *arg)
66 unsigned long cache = (unsigned long)arg;
69 * Flush all to work around Errata in early athlons regarding
70 * large page flushing.
74 if (cache && boot_cpu_data.x86_model >= 4)
78 static void cpa_flush_all(unsigned long cache)
80 BUG_ON(irqs_disabled());
82 on_each_cpu(__cpa_flush_all, (void *) cache, 1, 1);
85 static void __cpa_flush_range(void *arg)
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.
95 static void cpa_flush_range(unsigned long start, int numpages, int cache)
97 unsigned int i, level;
100 BUG_ON(irqs_disabled());
101 WARN_ON(PAGE_ALIGN(start) != start);
103 on_each_cpu(__cpa_flush_range, NULL, 1, 1);
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
114 for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
115 pte_t *pte = lookup_address(addr, &level);
118 * Only flush present addresses:
120 if (pte && (pte_val(*pte) & _PAGE_PRESENT))
121 clflush_cache_range((void *) addr, PAGE_SIZE);
125 #define HIGH_MAP_START __START_KERNEL_map
126 #define HIGH_MAP_END (__START_KERNEL_map + KERNEL_TEXT_SIZE)
130 * Converts a virtual address to a X86-64 highmap address
132 static unsigned long virt_to_highmap(void *address)
135 return __pa((unsigned long)address) + HIGH_MAP_START - phys_base;
137 return (unsigned long)address;
142 * 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.
147 static inline pgprot_t static_protections(pgprot_t prot, unsigned long address)
149 pgprot_t forbidden = __pgprot(0);
152 * The BIOS area between 640k and 1Mb needs to be executable for
153 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
155 if (within(__pa(address), BIOS_BEGIN, BIOS_END))
156 pgprot_val(forbidden) |= _PAGE_NX;
159 * The kernel text needs to be executable for obvious reasons
160 * Does not cover __inittext since that is gone later on
162 if (within(address, (unsigned long)_text, (unsigned long)_etext))
163 pgprot_val(forbidden) |= _PAGE_NX;
165 * Do the same for the x86-64 high kernel mapping
167 if (within(address, virt_to_highmap(_text), virt_to_highmap(_etext)))
168 pgprot_val(forbidden) |= _PAGE_NX;
170 /* 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;
175 * Do the same for the x86-64 high kernel mapping
177 if (within(address, virt_to_highmap(__start_rodata),
178 virt_to_highmap(__end_rodata)))
179 pgprot_val(forbidden) |= _PAGE_RW;
181 prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
187 * Lookup the page table entry for a virtual address. Return a pointer
188 * to the entry and the level of the mapping.
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.
194 pte_t *lookup_address(unsigned long address, unsigned int *level)
196 pgd_t *pgd = pgd_offset_k(address);
200 *level = PG_LEVEL_NONE;
205 pud = pud_offset(pgd, address);
209 *level = PG_LEVEL_1G;
210 if (pud_large(*pud) || !pud_present(*pud))
213 pmd = pmd_offset(pud, address);
217 *level = PG_LEVEL_2M;
218 if (pmd_large(*pmd) || !pmd_present(*pmd))
221 *level = PG_LEVEL_4K;
223 return pte_offset_kernel(pmd, address);
227 * Set the new pmd in all the pgds we know about:
229 static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
232 set_pte_atomic(kpte, pte);
234 if (!SHARED_KERNEL_PMD) {
237 list_for_each_entry(page, &pgd_list, lru) {
242 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);
252 try_preserve_large_page(pte_t *kpte, unsigned long address,
253 struct cpa_data *cpa)
255 unsigned long nextpage_addr, numpages, pmask, psize, flags;
256 pte_t new_pte, old_pte, *tmp;
257 pgprot_t old_prot, new_prot;
261 spin_lock_irqsave(&pgd_lock, flags);
263 * Check for races, another CPU might have split this page
266 tmp = lookup_address(address, &level);
272 psize = PMD_PAGE_SIZE;
273 pmask = PMD_PAGE_MASK;
277 psize = PMD_PAGE_SIZE;
278 pmask = PMD_PAGE_MASK;
287 * Calculate the number of pages, which fit into this large
288 * page starting at address:
290 nextpage_addr = (address + psize) & pmask;
291 numpages = (nextpage_addr - address) >> PAGE_SHIFT;
292 if (numpages < cpa->numpages)
293 cpa->numpages = numpages;
296 * We are safe now. Check whether the new pgprot is the same:
299 old_prot = new_prot = pte_pgprot(old_pte);
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);
306 * If there are no changes, return. maxpages has been updated
309 if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
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.
322 if (address == (nextpage_addr - psize) && cpa->numpages == numpages) {
324 * The address is aligned and the number of pages
325 * covers the full page.
327 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
328 __set_pmd_pte(kpte, address, new_pte);
334 spin_unlock_irqrestore(&pgd_lock, flags);
339 static int split_large_page(pte_t *kpte, unsigned long address)
341 unsigned long flags, pfn, pfninc = 1;
342 gfp_t gfp_flags = GFP_KERNEL;
343 unsigned int i, level;
348 #ifdef CONFIG_DEBUG_PAGEALLOC
349 gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
351 base = alloc_pages(gfp_flags, 0);
355 spin_lock_irqsave(&pgd_lock, flags);
357 * Check for races, another CPU might have split this page
360 tmp = lookup_address(address, &level);
364 pbase = (pte_t *)page_address(base);
366 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
368 ref_prot = pte_pgprot(pte_clrhuge(*kpte));
371 if (level == PG_LEVEL_1G) {
372 pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
373 pgprot_val(ref_prot) |= _PAGE_PSE;
378 * Get the target pfn from the original entry:
380 pfn = pte_pfn(*kpte);
381 for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
382 set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
385 * Install the new, split up pagetable. Important details here:
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).
391 * Mark the entry present. The current mapping might be
392 * set to not present, which we preserved above.
394 ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
395 pgprot_val(ref_prot) |= _PAGE_PRESENT;
396 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
400 spin_unlock_irqrestore(&pgd_lock, flags);
403 __free_pages(base, 0);
408 static int __change_page_attr(unsigned long address, struct cpa_data *cpa)
412 struct page *kpte_page;
416 kpte = lookup_address(address, &level);
420 kpte_page = virt_to_page(kpte);
421 BUG_ON(PageLRU(kpte_page));
422 BUG_ON(PageCompound(kpte_page));
424 if (level == PG_LEVEL_4K) {
425 pte_t new_pte, old_pte = *kpte;
426 pgprot_t new_prot = pte_pgprot(old_pte);
428 if(!pte_val(old_pte)) {
429 printk(KERN_WARNING "CPA: called for zero pte. "
430 "vaddr = %lx cpa->vaddr = %lx\n", address,
436 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
437 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
439 new_prot = static_protections(new_prot, address);
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
446 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
449 * Do we really change anything ?
451 if (pte_val(old_pte) != pte_val(new_pte)) {
452 set_pte_atomic(kpte, new_pte);
460 * Check, whether we can keep the large page intact
461 * and just change the pte:
463 do_split = try_preserve_large_page(kpte, address, cpa);
465 * When the range fits into the existing large page,
466 * return. cp->numpages and cpa->tlbflush have been updated in
473 * We have to split the large page:
475 err = split_large_page(kpte, address);
485 * change_page_attr_addr - Change page table attributes in linear mapping
486 * @address: Virtual address in linear mapping.
487 * @prot: New page table attribute (PAGE_*)
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).
493 * See change_page_attr() documentation for more details.
495 * Modules and drivers should use the set_memory_* APIs instead.
497 static int change_page_attr_addr(struct cpa_data *cpa)
500 unsigned long address = cpa->vaddr;
503 unsigned long phys_addr = __pa(address);
506 * 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:
510 if (within(address, HIGH_MAP_START, HIGH_MAP_END))
511 address = (unsigned long) __va(phys_addr);
514 err = __change_page_attr(address, cpa);
520 * If the physical address is inside the kernel map, we need
521 * to touch the high mapped kernel as well:
523 if (within(phys_addr, 0, KERNEL_TEXT_SIZE)) {
525 * Calc the high mapping address. See __phys_addr()
526 * for the non obvious details.
528 * Note that NX and other required permissions are
529 * checked in static_protections().
531 address = phys_addr + HIGH_MAP_START - phys_base;
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:
538 __change_page_attr(address, cpa);
544 static int __change_page_attr_set_clr(struct cpa_data *cpa)
546 int ret, numpages = cpa->numpages;
550 * Store the remaining nr of pages for the large page
551 * preservation check.
553 cpa->numpages = numpages;
554 ret = change_page_attr_addr(cpa);
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.
563 BUG_ON(cpa->numpages > numpages);
564 numpages -= cpa->numpages;
565 cpa->vaddr += cpa->numpages * PAGE_SIZE;
570 static inline int cache_attr(pgprot_t attr)
572 return pgprot_val(attr) &
573 (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
576 static int change_page_attr_set_clr(unsigned long addr, int numpages,
577 pgprot_t mask_set, pgprot_t mask_clr)
583 * Check, if we are requested to change a not supported
586 mask_set = canon_pgprot(mask_set);
587 mask_clr = canon_pgprot(mask_clr);
588 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr))
592 cpa.numpages = numpages;
593 cpa.mask_set = mask_set;
594 cpa.mask_clr = mask_clr;
597 ret = __change_page_attr_set_clr(&cpa);
600 * Check whether we really changed something:
606 * No need to flush, when we did not set any of the caching
609 cache = cache_attr(mask_set);
612 * 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
614 * error case we fall back to cpa_flush_all (which uses
617 if (!ret && cpu_has_clflush)
618 cpa_flush_range(addr, numpages, cache);
620 cpa_flush_all(cache);
625 static inline int change_page_attr_set(unsigned long addr, int numpages,
628 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
631 static inline int change_page_attr_clear(unsigned long addr, int numpages,
634 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
637 int set_memory_uc(unsigned long addr, int numpages)
639 return change_page_attr_set(addr, numpages,
640 __pgprot(_PAGE_PCD | _PAGE_PWT));
642 EXPORT_SYMBOL(set_memory_uc);
644 int set_memory_wb(unsigned long addr, int numpages)
646 return change_page_attr_clear(addr, numpages,
647 __pgprot(_PAGE_PCD | _PAGE_PWT));
649 EXPORT_SYMBOL(set_memory_wb);
651 int set_memory_x(unsigned long addr, int numpages)
653 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
655 EXPORT_SYMBOL(set_memory_x);
657 int set_memory_nx(unsigned long addr, int numpages)
659 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
661 EXPORT_SYMBOL(set_memory_nx);
663 int set_memory_ro(unsigned long addr, int numpages)
665 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
668 int set_memory_rw(unsigned long addr, int numpages)
670 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
673 int set_memory_np(unsigned long addr, int numpages)
675 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
678 int set_pages_uc(struct page *page, int numpages)
680 unsigned long addr = (unsigned long)page_address(page);
682 return set_memory_uc(addr, numpages);
684 EXPORT_SYMBOL(set_pages_uc);
686 int set_pages_wb(struct page *page, int numpages)
688 unsigned long addr = (unsigned long)page_address(page);
690 return set_memory_wb(addr, numpages);
692 EXPORT_SYMBOL(set_pages_wb);
694 int set_pages_x(struct page *page, int numpages)
696 unsigned long addr = (unsigned long)page_address(page);
698 return set_memory_x(addr, numpages);
700 EXPORT_SYMBOL(set_pages_x);
702 int set_pages_nx(struct page *page, int numpages)
704 unsigned long addr = (unsigned long)page_address(page);
706 return set_memory_nx(addr, numpages);
708 EXPORT_SYMBOL(set_pages_nx);
710 int set_pages_ro(struct page *page, int numpages)
712 unsigned long addr = (unsigned long)page_address(page);
714 return set_memory_ro(addr, numpages);
717 int set_pages_rw(struct page *page, int numpages)
719 unsigned long addr = (unsigned long)page_address(page);
721 return set_memory_rw(addr, numpages);
724 #ifdef CONFIG_DEBUG_PAGEALLOC
726 static int __set_pages_p(struct page *page, int numpages)
728 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)};
733 return __change_page_attr_set_clr(&cpa);
736 static int __set_pages_np(struct page *page, int numpages)
738 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)};
743 return __change_page_attr_set_clr(&cpa);
746 void kernel_map_pages(struct page *page, int numpages, int enable)
748 if (PageHighMem(page))
751 debug_check_no_locks_freed(page_address(page),
752 numpages * PAGE_SIZE);
756 * If page allocator is not up yet then do not call c_p_a():
758 if (!debug_pagealloc_enabled)
762 * The return value is ignored - the calls cannot fail,
763 * large pages are disabled at boot time:
766 __set_pages_p(page, numpages);
768 __set_pages_np(page, numpages);
771 * We should perform an IPI and flush all tlbs,
772 * but that can deadlock->flush only current cpu:
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.
782 #ifdef CONFIG_CPA_DEBUG
783 #include "pageattr-test.c"