x86: AMD Athlon X2 hard hang fix
[wrt350n-kernel.git] / arch / x86 / mm / pageattr.c
blob3810f7a83b1da62caee23d5a8d8c564264e2ede7
1 /*
2 * Copyright 2002 Andi Kleen, SuSE Labs.
3 * Thanks to Ben LaHaise for precious feedback.
4 */
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>
10 #include <linux/mm.h>
12 #include <asm/e820.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>
19 struct cpa_data {
20 unsigned long vaddr;
21 pgprot_t mask_set;
22 pgprot_t mask_clr;
23 int numpages;
24 int flushtlb;
27 enum {
28 CPA_NO_SPLIT = 0,
29 CPA_SPLIT,
32 static inline int
33 within(unsigned long addr, unsigned long start, unsigned long end)
35 return addr >= start && addr < end;
39 * Flushing functions
42 /**
43 * clflush_cache_range - flush a cache range with clflush
44 * @addr: virtual start address
45 * @size: number of bytes to flush
47 * clflush is an unordered instruction which needs fencing with mfence
48 * to avoid ordering issues.
50 void clflush_cache_range(void *vaddr, unsigned int size)
52 void *vend = vaddr + size - 1;
54 mb();
56 for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
57 clflush(vaddr);
59 * Flush any possible final partial cacheline:
61 clflush(vend);
63 mb();
66 static void __cpa_flush_all(void *arg)
68 unsigned long cache = (unsigned long)arg;
71 * Flush all to work around Errata in early athlons regarding
72 * large page flushing.
74 __flush_tlb_all();
76 if (cache && boot_cpu_data.x86_model >= 4)
77 wbinvd();
80 static void cpa_flush_all(unsigned long cache)
82 BUG_ON(irqs_disabled());
84 on_each_cpu(__cpa_flush_all, (void *) cache, 1, 1);
87 static void __cpa_flush_range(void *arg)
90 * We could optimize that further and do individual per page
91 * tlb invalidates for a low number of pages. Caveat: we must
92 * flush the high aliases on 64bit as well.
94 __flush_tlb_all();
97 static void cpa_flush_range(unsigned long start, int numpages, int cache)
99 unsigned int i, level;
100 unsigned long addr;
102 BUG_ON(irqs_disabled());
103 WARN_ON(PAGE_ALIGN(start) != start);
105 on_each_cpu(__cpa_flush_range, NULL, 1, 1);
107 if (!cache)
108 return;
111 * We only need to flush on one CPU,
112 * clflush is a MESI-coherent instruction that
113 * will cause all other CPUs to flush the same
114 * cachelines:
116 for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
117 pte_t *pte = lookup_address(addr, &level);
120 * Only flush present addresses:
122 if (pte && pte_present(*pte))
123 clflush_cache_range((void *) addr, PAGE_SIZE);
127 #define HIGH_MAP_START __START_KERNEL_map
128 #define HIGH_MAP_END (__START_KERNEL_map + KERNEL_TEXT_SIZE)
132 * Converts a virtual address to a X86-64 highmap address
134 static unsigned long virt_to_highmap(void *address)
136 #ifdef CONFIG_X86_64
137 return __pa((unsigned long)address) + HIGH_MAP_START - phys_base;
138 #else
139 return (unsigned long)address;
140 #endif
144 * Certain areas of memory on x86 require very specific protection flags,
145 * for example the BIOS area or kernel text. Callers don't always get this
146 * right (again, ioremap() on BIOS memory is not uncommon) so this function
147 * checks and fixes these known static required protection bits.
149 static inline pgprot_t static_protections(pgprot_t prot, unsigned long address)
151 pgprot_t forbidden = __pgprot(0);
154 * The BIOS area between 640k and 1Mb needs to be executable for
155 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
157 if (within(__pa(address), BIOS_BEGIN, BIOS_END))
158 pgprot_val(forbidden) |= _PAGE_NX;
161 * The kernel text needs to be executable for obvious reasons
162 * Does not cover __inittext since that is gone later on
164 if (within(address, (unsigned long)_text, (unsigned long)_etext))
165 pgprot_val(forbidden) |= _PAGE_NX;
167 * Do the same for the x86-64 high kernel mapping
169 if (within(address, virt_to_highmap(_text), virt_to_highmap(_etext)))
170 pgprot_val(forbidden) |= _PAGE_NX;
173 #ifdef CONFIG_DEBUG_RODATA
174 /* The .rodata section needs to be read-only */
175 if (within(address, (unsigned long)__start_rodata,
176 (unsigned long)__end_rodata))
177 pgprot_val(forbidden) |= _PAGE_RW;
179 * Do the same for the x86-64 high kernel mapping
181 if (within(address, virt_to_highmap(__start_rodata),
182 virt_to_highmap(__end_rodata)))
183 pgprot_val(forbidden) |= _PAGE_RW;
184 #endif
186 prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
188 return prot;
191 pte_t *lookup_address(unsigned long address, int *level)
193 pgd_t *pgd = pgd_offset_k(address);
194 pud_t *pud;
195 pmd_t *pmd;
197 *level = PG_LEVEL_NONE;
199 if (pgd_none(*pgd))
200 return NULL;
201 pud = pud_offset(pgd, address);
202 if (pud_none(*pud))
203 return NULL;
204 pmd = pmd_offset(pud, address);
205 if (pmd_none(*pmd))
206 return NULL;
208 *level = PG_LEVEL_2M;
209 if (pmd_large(*pmd))
210 return (pte_t *)pmd;
212 *level = PG_LEVEL_4K;
213 return pte_offset_kernel(pmd, address);
216 static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
218 /* change init_mm */
219 set_pte_atomic(kpte, pte);
220 #ifdef CONFIG_X86_32
221 if (!SHARED_KERNEL_PMD) {
222 struct page *page;
224 list_for_each_entry(page, &pgd_list, lru) {
225 pgd_t *pgd;
226 pud_t *pud;
227 pmd_t *pmd;
229 pgd = (pgd_t *)page_address(page) + pgd_index(address);
230 pud = pud_offset(pgd, address);
231 pmd = pmd_offset(pud, address);
232 set_pte_atomic((pte_t *)pmd, pte);
235 #endif
238 static int try_preserve_large_page(pte_t *kpte, unsigned long address,
239 struct cpa_data *cpa)
241 unsigned long nextpage_addr, numpages, pmask, psize, flags;
242 pte_t new_pte, old_pte, *tmp;
243 pgprot_t old_prot, new_prot;
244 int level, res = CPA_SPLIT;
247 * An Athlon 64 X2 showed hard hangs if we tried to preserve
248 * largepages and changed the PSE entry from RW to RO.
250 * As AMD CPUs have a long series of erratas in this area,
251 * (and none of the known ones seem to explain this hang),
252 * disable this code until the hang can be debugged:
254 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
255 return res;
257 spin_lock_irqsave(&pgd_lock, flags);
259 * Check for races, another CPU might have split this page
260 * up already:
262 tmp = lookup_address(address, &level);
263 if (tmp != kpte)
264 goto out_unlock;
266 switch (level) {
267 case PG_LEVEL_2M:
268 psize = LARGE_PAGE_SIZE;
269 pmask = LARGE_PAGE_MASK;
270 break;
271 case PG_LEVEL_1G:
272 default:
273 res = -EINVAL;
274 goto out_unlock;
278 * Calculate the number of pages, which fit into this large
279 * page starting at address:
281 nextpage_addr = (address + psize) & pmask;
282 numpages = (nextpage_addr - address) >> PAGE_SHIFT;
283 if (numpages < cpa->numpages)
284 cpa->numpages = numpages;
287 * We are safe now. Check whether the new pgprot is the same:
289 old_pte = *kpte;
290 old_prot = new_prot = pte_pgprot(old_pte);
292 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
293 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
294 new_prot = static_protections(new_prot, address);
297 * If there are no changes, return. maxpages has been updated
298 * above:
300 if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
301 res = CPA_NO_SPLIT;
302 goto out_unlock;
306 * We need to change the attributes. Check, whether we can
307 * change the large page in one go. We request a split, when
308 * the address is not aligned and the number of pages is
309 * smaller than the number of pages in the large page. Note
310 * that we limited the number of possible pages already to
311 * the number of pages in the large page.
313 if (address == (nextpage_addr - psize) && cpa->numpages == numpages) {
315 * The address is aligned and the number of pages
316 * covers the full page.
318 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
319 __set_pmd_pte(kpte, address, new_pte);
320 cpa->flushtlb = 1;
321 res = CPA_NO_SPLIT;
324 out_unlock:
325 spin_unlock_irqrestore(&pgd_lock, flags);
326 return res;
329 static int split_large_page(pte_t *kpte, unsigned long address)
331 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
332 gfp_t gfp_flags = GFP_KERNEL;
333 unsigned long flags, addr, pfn;
334 pte_t *pbase, *tmp;
335 struct page *base;
336 unsigned int i, level;
338 #ifdef CONFIG_DEBUG_PAGEALLOC
339 gfp_flags = __GFP_HIGH | __GFP_NOFAIL | __GFP_NOWARN;
340 gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
341 #endif
342 base = alloc_pages(gfp_flags, 0);
343 if (!base)
344 return -ENOMEM;
346 spin_lock_irqsave(&pgd_lock, flags);
348 * Check for races, another CPU might have split this page
349 * up for us already:
351 tmp = lookup_address(address, &level);
352 if (tmp != kpte) {
353 WARN_ON_ONCE(1);
354 goto out_unlock;
357 address = __pa(address);
358 addr = address & LARGE_PAGE_MASK;
359 pbase = (pte_t *)page_address(base);
360 #ifdef CONFIG_X86_32
361 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
362 #endif
365 * Get the target pfn from the original entry:
367 pfn = pte_pfn(*kpte);
368 for (i = 0; i < PTRS_PER_PTE; i++, pfn++)
369 set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
372 * Install the new, split up pagetable. Important detail here:
374 * On Intel the NX bit of all levels must be cleared to make a
375 * page executable. See section 4.13.2 of Intel 64 and IA-32
376 * Architectures Software Developer's Manual).
378 ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
379 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
380 base = NULL;
382 out_unlock:
383 spin_unlock_irqrestore(&pgd_lock, flags);
385 if (base)
386 __free_pages(base, 0);
388 return 0;
391 static int __change_page_attr(unsigned long address, struct cpa_data *cpa)
393 struct page *kpte_page;
394 int level, res;
395 pte_t *kpte;
397 repeat:
398 kpte = lookup_address(address, &level);
399 if (!kpte)
400 return -EINVAL;
402 kpte_page = virt_to_page(kpte);
403 BUG_ON(PageLRU(kpte_page));
404 BUG_ON(PageCompound(kpte_page));
406 if (level == PG_LEVEL_4K) {
407 pte_t new_pte, old_pte = *kpte;
408 pgprot_t new_prot = pte_pgprot(old_pte);
410 if(!pte_val(old_pte)) {
411 printk(KERN_WARNING "CPA: called for zero pte. "
412 "vaddr = %lx cpa->vaddr = %lx\n", address,
413 cpa->vaddr);
414 WARN_ON(1);
415 return -EINVAL;
418 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
419 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
421 new_prot = static_protections(new_prot, address);
424 * We need to keep the pfn from the existing PTE,
425 * after all we're only going to change it's attributes
426 * not the memory it points to
428 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
431 * Do we really change anything ?
433 if (pte_val(old_pte) != pte_val(new_pte)) {
434 set_pte_atomic(kpte, new_pte);
435 cpa->flushtlb = 1;
437 cpa->numpages = 1;
438 return 0;
442 * Check, whether we can keep the large page intact
443 * and just change the pte:
445 res = try_preserve_large_page(kpte, address, cpa);
446 if (res < 0)
447 return res;
450 * When the range fits into the existing large page,
451 * return. cp->numpages and cpa->tlbflush have been updated in
452 * try_large_page:
454 if (res == CPA_NO_SPLIT)
455 return 0;
458 * We have to split the large page:
460 res = split_large_page(kpte, address);
461 if (res)
462 return res;
463 cpa->flushtlb = 1;
464 goto repeat;
468 * change_page_attr_addr - Change page table attributes in linear mapping
469 * @address: Virtual address in linear mapping.
470 * @prot: New page table attribute (PAGE_*)
472 * Change page attributes of a page in the direct mapping. This is a variant
473 * of change_page_attr() that also works on memory holes that do not have
474 * mem_map entry (pfn_valid() is false).
476 * See change_page_attr() documentation for more details.
478 * Modules and drivers should use the set_memory_* APIs instead.
481 static int change_page_attr_addr(struct cpa_data *cpa)
483 int err;
484 unsigned long address = cpa->vaddr;
486 #ifdef CONFIG_X86_64
487 unsigned long phys_addr = __pa(address);
490 * If we are inside the high mapped kernel range, then we
491 * fixup the low mapping first. __va() returns the virtual
492 * address in the linear mapping:
494 if (within(address, HIGH_MAP_START, HIGH_MAP_END))
495 address = (unsigned long) __va(phys_addr);
496 #endif
498 err = __change_page_attr(address, cpa);
499 if (err)
500 return err;
502 #ifdef CONFIG_X86_64
504 * If the physical address is inside the kernel map, we need
505 * to touch the high mapped kernel as well:
507 if (within(phys_addr, 0, KERNEL_TEXT_SIZE)) {
509 * Calc the high mapping address. See __phys_addr()
510 * for the non obvious details.
512 * Note that NX and other required permissions are
513 * checked in static_protections().
515 address = phys_addr + HIGH_MAP_START - phys_base;
518 * Our high aliases are imprecise, because we check
519 * everything between 0 and KERNEL_TEXT_SIZE, so do
520 * not propagate lookup failures back to users:
522 __change_page_attr(address, cpa);
524 #endif
525 return err;
528 static int __change_page_attr_set_clr(struct cpa_data *cpa)
530 int ret, numpages = cpa->numpages;
532 while (numpages) {
534 * Store the remaining nr of pages for the large page
535 * preservation check.
537 cpa->numpages = numpages;
538 ret = change_page_attr_addr(cpa);
539 if (ret)
540 return ret;
543 * Adjust the number of pages with the result of the
544 * CPA operation. Either a large page has been
545 * preserved or a single page update happened.
547 BUG_ON(cpa->numpages > numpages);
548 numpages -= cpa->numpages;
549 cpa->vaddr += cpa->numpages * PAGE_SIZE;
551 return 0;
554 static inline int cache_attr(pgprot_t attr)
556 return pgprot_val(attr) &
557 (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
560 static int change_page_attr_set_clr(unsigned long addr, int numpages,
561 pgprot_t mask_set, pgprot_t mask_clr)
563 struct cpa_data cpa;
564 int ret, cache;
567 * Check, if we are requested to change a not supported
568 * feature:
570 mask_set = canon_pgprot(mask_set);
571 mask_clr = canon_pgprot(mask_clr);
572 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr))
573 return 0;
575 cpa.vaddr = addr;
576 cpa.numpages = numpages;
577 cpa.mask_set = mask_set;
578 cpa.mask_clr = mask_clr;
579 cpa.flushtlb = 0;
581 ret = __change_page_attr_set_clr(&cpa);
584 * Check whether we really changed something:
586 if (!cpa.flushtlb)
587 return ret;
590 * No need to flush, when we did not set any of the caching
591 * attributes:
593 cache = cache_attr(mask_set);
596 * On success we use clflush, when the CPU supports it to
597 * avoid the wbindv. If the CPU does not support it and in the
598 * error case we fall back to cpa_flush_all (which uses
599 * wbindv):
601 if (!ret && cpu_has_clflush)
602 cpa_flush_range(addr, numpages, cache);
603 else
604 cpa_flush_all(cache);
606 return ret;
609 static inline int change_page_attr_set(unsigned long addr, int numpages,
610 pgprot_t mask)
612 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
615 static inline int change_page_attr_clear(unsigned long addr, int numpages,
616 pgprot_t mask)
618 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
621 int set_memory_uc(unsigned long addr, int numpages)
623 return change_page_attr_set(addr, numpages,
624 __pgprot(_PAGE_PCD | _PAGE_PWT));
626 EXPORT_SYMBOL(set_memory_uc);
628 int set_memory_wb(unsigned long addr, int numpages)
630 return change_page_attr_clear(addr, numpages,
631 __pgprot(_PAGE_PCD | _PAGE_PWT));
633 EXPORT_SYMBOL(set_memory_wb);
635 int set_memory_x(unsigned long addr, int numpages)
637 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
639 EXPORT_SYMBOL(set_memory_x);
641 int set_memory_nx(unsigned long addr, int numpages)
643 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
645 EXPORT_SYMBOL(set_memory_nx);
647 int set_memory_ro(unsigned long addr, int numpages)
649 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
652 int set_memory_rw(unsigned long addr, int numpages)
654 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
657 int set_memory_np(unsigned long addr, int numpages)
659 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
662 int set_pages_uc(struct page *page, int numpages)
664 unsigned long addr = (unsigned long)page_address(page);
666 return set_memory_uc(addr, numpages);
668 EXPORT_SYMBOL(set_pages_uc);
670 int set_pages_wb(struct page *page, int numpages)
672 unsigned long addr = (unsigned long)page_address(page);
674 return set_memory_wb(addr, numpages);
676 EXPORT_SYMBOL(set_pages_wb);
678 int set_pages_x(struct page *page, int numpages)
680 unsigned long addr = (unsigned long)page_address(page);
682 return set_memory_x(addr, numpages);
684 EXPORT_SYMBOL(set_pages_x);
686 int set_pages_nx(struct page *page, int numpages)
688 unsigned long addr = (unsigned long)page_address(page);
690 return set_memory_nx(addr, numpages);
692 EXPORT_SYMBOL(set_pages_nx);
694 int set_pages_ro(struct page *page, int numpages)
696 unsigned long addr = (unsigned long)page_address(page);
698 return set_memory_ro(addr, numpages);
701 int set_pages_rw(struct page *page, int numpages)
703 unsigned long addr = (unsigned long)page_address(page);
705 return set_memory_rw(addr, numpages);
708 #ifdef CONFIG_DEBUG_PAGEALLOC
710 static int __set_pages_p(struct page *page, int numpages)
712 struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
713 .numpages = numpages,
714 .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
715 .mask_clr = __pgprot(0)};
717 return __change_page_attr_set_clr(&cpa);
720 static int __set_pages_np(struct page *page, int numpages)
722 struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
723 .numpages = numpages,
724 .mask_set = __pgprot(0),
725 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW)};
727 return __change_page_attr_set_clr(&cpa);
730 void kernel_map_pages(struct page *page, int numpages, int enable)
732 if (PageHighMem(page))
733 return;
734 if (!enable) {
735 debug_check_no_locks_freed(page_address(page),
736 numpages * PAGE_SIZE);
740 * If page allocator is not up yet then do not call c_p_a():
742 if (!debug_pagealloc_enabled)
743 return;
746 * The return value is ignored - the calls cannot fail,
747 * large pages are disabled at boot time:
749 if (enable)
750 __set_pages_p(page, numpages);
751 else
752 __set_pages_np(page, numpages);
755 * We should perform an IPI and flush all tlbs,
756 * but that can deadlock->flush only current cpu:
758 __flush_tlb_all();
760 #endif
763 * The testcases use internal knowledge of the implementation that shouldn't
764 * be exposed to the rest of the kernel. Include these directly here.
766 #ifdef CONFIG_CPA_DEBUG
767 #include "pageattr-test.c"
768 #endif