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 within(unsigned long addr, unsigned long start, unsigned long end)
22 return addr >= start && addr < end;
28 void clflush_cache_range(void *addr, int size)
32 for (i = 0; i < size; i += boot_cpu_data.x86_clflush_size)
36 static void __cpa_flush_all(void *arg)
39 * Flush all to work around Errata in early athlons regarding
40 * large page flushing.
44 if (boot_cpu_data.x86_model >= 4)
48 static void cpa_flush_all(void)
50 BUG_ON(irqs_disabled());
52 on_each_cpu(__cpa_flush_all, NULL, 1, 1);
60 static void __cpa_flush_range(void *arg)
62 struct clflush_data *cld = arg;
65 * We could optimize that further and do individual per page
66 * tlb invalidates for a low number of pages. Caveat: we must
67 * flush the high aliases on 64bit as well.
71 clflush_cache_range((void *) cld->addr, cld->numpages * PAGE_SIZE);
74 static void cpa_flush_range(unsigned long addr, int numpages)
76 struct clflush_data cld;
78 BUG_ON(irqs_disabled());
81 cld.numpages = numpages;
83 on_each_cpu(__cpa_flush_range, &cld, 1, 1);
87 * Certain areas of memory on x86 require very specific protection flags,
88 * for example the BIOS area or kernel text. Callers don't always get this
89 * right (again, ioremap() on BIOS memory is not uncommon) so this function
90 * checks and fixes these known static required protection bits.
92 static inline pgprot_t static_protections(pgprot_t prot, unsigned long address)
94 pgprot_t forbidden = __pgprot(0);
97 * The BIOS area between 640k and 1Mb needs to be executable for
98 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
100 if (within(__pa(address), BIOS_BEGIN, BIOS_END))
101 pgprot_val(forbidden) |= _PAGE_NX;
104 * The kernel text needs to be executable for obvious reasons
105 * Does not cover __inittext since that is gone later on
107 if (within(address, (unsigned long)_text, (unsigned long)_etext))
108 pgprot_val(forbidden) |= _PAGE_NX;
110 #ifdef CONFIG_DEBUG_RODATA
111 /* The .rodata section needs to be read-only */
112 if (within(address, (unsigned long)__start_rodata,
113 (unsigned long)__end_rodata))
114 pgprot_val(forbidden) |= _PAGE_RW;
117 prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
122 pte_t *lookup_address(unsigned long address, int *level)
124 pgd_t *pgd = pgd_offset_k(address);
128 *level = PG_LEVEL_NONE;
132 pud = pud_offset(pgd, address);
135 pmd = pmd_offset(pud, address);
139 *level = PG_LEVEL_2M;
143 *level = PG_LEVEL_4K;
144 return pte_offset_kernel(pmd, address);
147 static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
150 set_pte_atomic(kpte, pte);
152 if (!SHARED_KERNEL_PMD) {
155 for (page = pgd_list; page; page = (struct page *)page->index) {
160 pgd = (pgd_t *)page_address(page) + pgd_index(address);
161 pud = pud_offset(pgd, address);
162 pmd = pmd_offset(pud, address);
163 set_pte_atomic((pte_t *)pmd, pte);
169 static int split_large_page(pte_t *kpte, unsigned long address)
171 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
172 gfp_t gfp_flags = GFP_KERNEL;
179 #ifdef CONFIG_DEBUG_PAGEALLOC
180 gfp_flags = GFP_ATOMIC;
182 base = alloc_pages(gfp_flags, 0);
186 spin_lock_irqsave(&pgd_lock, flags);
188 * Check for races, another CPU might have split this page
191 tmp = lookup_address(address, &level);
197 address = __pa(address);
198 addr = address & LARGE_PAGE_MASK;
199 pbase = (pte_t *)page_address(base);
201 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
204 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
205 set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
208 * Install the new, split up pagetable. Important detail here:
210 * On Intel the NX bit of all levels must be cleared to make a
211 * page executable. See section 4.13.2 of Intel 64 and IA-32
212 * Architectures Software Developer's Manual).
214 ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
215 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
219 spin_unlock_irqrestore(&pgd_lock, flags);
222 __free_pages(base, 0);
228 __change_page_attr(unsigned long address, unsigned long pfn, pgprot_t prot)
230 struct page *kpte_page;
235 BUG_ON(pfn > max_low_pfn);
239 kpte = lookup_address(address, &level);
243 kpte_page = virt_to_page(kpte);
244 BUG_ON(PageLRU(kpte_page));
245 BUG_ON(PageCompound(kpte_page));
247 prot = static_protections(prot, address);
249 if (level == PG_LEVEL_4K) {
250 WARN_ON_ONCE(pgprot_val(prot) & _PAGE_PSE);
251 set_pte_atomic(kpte, pfn_pte(pfn, canon_pgprot(prot)));
253 /* Clear the PSE bit for the 4k level pages ! */
254 pgprot_val(prot) = pgprot_val(prot) & ~_PAGE_PSE;
256 err = split_large_page(kpte, address);
264 * change_page_attr_addr - Change page table attributes in linear mapping
265 * @address: Virtual address in linear mapping.
266 * @prot: New page table attribute (PAGE_*)
268 * Change page attributes of a page in the direct mapping. This is a variant
269 * of change_page_attr() that also works on memory holes that do not have
270 * mem_map entry (pfn_valid() is false).
272 * See change_page_attr() documentation for more details.
274 * Modules and drivers should use the set_memory_* APIs instead.
277 static int change_page_attr_addr(unsigned long address, pgprot_t prot)
279 int err = 0, kernel_map = 0;
280 unsigned long pfn = __pa(address) >> PAGE_SHIFT;
283 if (address >= __START_KERNEL_map &&
284 address < __START_KERNEL_map + KERNEL_TEXT_SIZE) {
286 address = (unsigned long)__va(__pa(address));
291 if (!kernel_map || pte_present(pfn_pte(0, prot))) {
292 err = __change_page_attr(address, pfn, prot);
299 * Handle kernel mapping too which aliases part of
302 if (__pa(address) < KERNEL_TEXT_SIZE) {
306 addr2 = __START_KERNEL_map + __pa(address);
307 /* Make sure the kernel mappings stay executable */
308 prot2 = pte_pgprot(pte_mkexec(pfn_pte(0, prot)));
309 err = __change_page_attr(addr2, pfn, prot2);
316 static int __change_page_attr_set_clr(unsigned long addr, int numpages,
317 pgprot_t mask_set, pgprot_t mask_clr)
324 for (i = 0; i < numpages ; i++) {
326 pte = lookup_address(addr, &level);
330 new_prot = pte_pgprot(*pte);
332 pgprot_val(new_prot) &= ~pgprot_val(mask_clr);
333 pgprot_val(new_prot) |= pgprot_val(mask_set);
335 ret = change_page_attr_addr(addr, new_prot);
344 static int change_page_attr_set_clr(unsigned long addr, int numpages,
345 pgprot_t mask_set, pgprot_t mask_clr)
347 int ret = __change_page_attr_set_clr(addr, numpages, mask_set,
351 * On success we use clflush, when the CPU supports it to
352 * avoid the wbindv. If the CPU does not support it and in the
353 * error case we fall back to cpa_flush_all (which uses
356 if (!ret && cpu_has_clflush)
357 cpa_flush_range(addr, numpages);
364 static inline int change_page_attr_set(unsigned long addr, int numpages,
367 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
370 static inline int change_page_attr_clear(unsigned long addr, int numpages,
373 return __change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
377 int set_memory_uc(unsigned long addr, int numpages)
379 return change_page_attr_set(addr, numpages,
380 __pgprot(_PAGE_PCD | _PAGE_PWT));
382 EXPORT_SYMBOL(set_memory_uc);
384 int set_memory_wb(unsigned long addr, int numpages)
386 return change_page_attr_clear(addr, numpages,
387 __pgprot(_PAGE_PCD | _PAGE_PWT));
389 EXPORT_SYMBOL(set_memory_wb);
391 int set_memory_x(unsigned long addr, int numpages)
393 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
395 EXPORT_SYMBOL(set_memory_x);
397 int set_memory_nx(unsigned long addr, int numpages)
399 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
401 EXPORT_SYMBOL(set_memory_nx);
403 int set_memory_ro(unsigned long addr, int numpages)
405 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
408 int set_memory_rw(unsigned long addr, int numpages)
410 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
413 int set_memory_np(unsigned long addr, int numpages)
415 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
418 int set_pages_uc(struct page *page, int numpages)
420 unsigned long addr = (unsigned long)page_address(page);
422 return set_memory_uc(addr, numpages);
424 EXPORT_SYMBOL(set_pages_uc);
426 int set_pages_wb(struct page *page, int numpages)
428 unsigned long addr = (unsigned long)page_address(page);
430 return set_memory_wb(addr, numpages);
432 EXPORT_SYMBOL(set_pages_wb);
434 int set_pages_x(struct page *page, int numpages)
436 unsigned long addr = (unsigned long)page_address(page);
438 return set_memory_x(addr, numpages);
440 EXPORT_SYMBOL(set_pages_x);
442 int set_pages_nx(struct page *page, int numpages)
444 unsigned long addr = (unsigned long)page_address(page);
446 return set_memory_nx(addr, numpages);
448 EXPORT_SYMBOL(set_pages_nx);
450 int set_pages_ro(struct page *page, int numpages)
452 unsigned long addr = (unsigned long)page_address(page);
454 return set_memory_ro(addr, numpages);
457 int set_pages_rw(struct page *page, int numpages)
459 unsigned long addr = (unsigned long)page_address(page);
461 return set_memory_rw(addr, numpages);
465 #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_CPA_DEBUG)
466 static inline int __change_page_attr_set(unsigned long addr, int numpages,
469 return __change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
472 static inline int __change_page_attr_clear(unsigned long addr, int numpages,
475 return __change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
479 #ifdef CONFIG_DEBUG_PAGEALLOC
481 static int __set_pages_p(struct page *page, int numpages)
483 unsigned long addr = (unsigned long)page_address(page);
485 return __change_page_attr_set(addr, numpages,
486 __pgprot(_PAGE_PRESENT | _PAGE_RW));
489 static int __set_pages_np(struct page *page, int numpages)
491 unsigned long addr = (unsigned long)page_address(page);
493 return __change_page_attr_clear(addr, numpages,
494 __pgprot(_PAGE_PRESENT));
497 void kernel_map_pages(struct page *page, int numpages, int enable)
499 if (PageHighMem(page))
502 debug_check_no_locks_freed(page_address(page),
503 numpages * PAGE_SIZE);
507 * If page allocator is not up yet then do not call c_p_a():
509 if (!debug_pagealloc_enabled)
513 * The return value is ignored - the calls cannot fail,
514 * large pages are disabled at boot time:
517 __set_pages_p(page, numpages);
519 __set_pages_np(page, numpages);
522 * We should perform an IPI and flush all tlbs,
523 * but that can deadlock->flush only current cpu:
530 * The testcases use internal knowledge of the implementation that shouldn't
531 * be exposed to the rest of the kernel. Include these directly here.
533 #ifdef CONFIG_CPA_DEBUG
534 #include "pageattr-test.c"