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;
31 * clflush_cache_range - flush a cache range with clflush
32 * @addr: virtual start address
33 * @size: number of bytes to flush
35 * clflush is an unordered instruction which needs fencing with mfence
36 * to avoid ordering issues.
38 void clflush_cache_range(void *addr, int size)
43 for (i = 0; i < size; i += boot_cpu_data.x86_clflush_size)
48 static void __cpa_flush_all(void *arg)
51 * Flush all to work around Errata in early athlons regarding
52 * large page flushing.
56 if (boot_cpu_data.x86_model >= 4)
60 static void cpa_flush_all(void)
62 BUG_ON(irqs_disabled());
64 on_each_cpu(__cpa_flush_all, NULL, 1, 1);
67 static void __cpa_flush_range(void *arg)
70 * We could optimize that further and do individual per page
71 * tlb invalidates for a low number of pages. Caveat: we must
72 * flush the high aliases on 64bit as well.
77 static void cpa_flush_range(unsigned long addr, int numpages)
79 BUG_ON(irqs_disabled());
81 on_each_cpu(__cpa_flush_range, NULL, 1, 1);
84 * We only need to flush on one CPU,
85 * clflush is a MESI-coherent instruction that
86 * will cause all other CPUs to flush the same
89 clflush_cache_range((void *) addr, numpages * PAGE_SIZE);
93 * Certain areas of memory on x86 require very specific protection flags,
94 * for example the BIOS area or kernel text. Callers don't always get this
95 * right (again, ioremap() on BIOS memory is not uncommon) so this function
96 * checks and fixes these known static required protection bits.
98 static inline pgprot_t static_protections(pgprot_t prot, unsigned long address)
100 pgprot_t forbidden = __pgprot(0);
103 * The BIOS area between 640k and 1Mb needs to be executable for
104 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
106 if (within(__pa(address), BIOS_BEGIN, BIOS_END))
107 pgprot_val(forbidden) |= _PAGE_NX;
110 * The kernel text needs to be executable for obvious reasons
111 * Does not cover __inittext since that is gone later on
113 if (within(address, (unsigned long)_text, (unsigned long)_etext))
114 pgprot_val(forbidden) |= _PAGE_NX;
116 #ifdef CONFIG_DEBUG_RODATA
117 /* The .rodata section needs to be read-only */
118 if (within(address, (unsigned long)__start_rodata,
119 (unsigned long)__end_rodata))
120 pgprot_val(forbidden) |= _PAGE_RW;
123 prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
128 pte_t *lookup_address(unsigned long address, int *level)
130 pgd_t *pgd = pgd_offset_k(address);
134 *level = PG_LEVEL_NONE;
138 pud = pud_offset(pgd, address);
141 pmd = pmd_offset(pud, address);
145 *level = PG_LEVEL_2M;
149 *level = PG_LEVEL_4K;
150 return pte_offset_kernel(pmd, address);
153 static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
156 set_pte_atomic(kpte, pte);
158 if (!SHARED_KERNEL_PMD) {
161 for (page = pgd_list; page; page = (struct page *)page->index) {
166 pgd = (pgd_t *)page_address(page) + pgd_index(address);
167 pud = pud_offset(pgd, address);
168 pmd = pmd_offset(pud, address);
169 set_pte_atomic((pte_t *)pmd, pte);
175 static int split_large_page(pte_t *kpte, unsigned long address)
177 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
178 gfp_t gfp_flags = GFP_KERNEL;
185 #ifdef CONFIG_DEBUG_PAGEALLOC
186 gfp_flags = GFP_ATOMIC;
188 base = alloc_pages(gfp_flags, 0);
192 spin_lock_irqsave(&pgd_lock, flags);
194 * Check for races, another CPU might have split this page
197 tmp = lookup_address(address, &level);
203 address = __pa(address);
204 addr = address & LARGE_PAGE_MASK;
205 pbase = (pte_t *)page_address(base);
207 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
210 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
211 set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
214 * Install the new, split up pagetable. Important detail here:
216 * On Intel the NX bit of all levels must be cleared to make a
217 * page executable. See section 4.13.2 of Intel 64 and IA-32
218 * Architectures Software Developer's Manual).
220 ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
221 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
225 spin_unlock_irqrestore(&pgd_lock, flags);
228 __free_pages(base, 0);
234 __change_page_attr(unsigned long address, unsigned long pfn, pgprot_t prot)
236 struct page *kpte_page;
241 BUG_ON(pfn > max_low_pfn);
245 kpte = lookup_address(address, &level);
249 kpte_page = virt_to_page(kpte);
250 BUG_ON(PageLRU(kpte_page));
251 BUG_ON(PageCompound(kpte_page));
253 prot = static_protections(prot, address);
255 if (level == PG_LEVEL_4K) {
256 WARN_ON_ONCE(pgprot_val(prot) & _PAGE_PSE);
257 set_pte_atomic(kpte, pfn_pte(pfn, canon_pgprot(prot)));
259 /* Clear the PSE bit for the 4k level pages ! */
260 pgprot_val(prot) = pgprot_val(prot) & ~_PAGE_PSE;
262 err = split_large_page(kpte, address);
270 * change_page_attr_addr - Change page table attributes in linear mapping
271 * @address: Virtual address in linear mapping.
272 * @prot: New page table attribute (PAGE_*)
274 * Change page attributes of a page in the direct mapping. This is a variant
275 * of change_page_attr() that also works on memory holes that do not have
276 * mem_map entry (pfn_valid() is false).
278 * See change_page_attr() documentation for more details.
280 * Modules and drivers should use the set_memory_* APIs instead.
283 static int change_page_attr_addr(unsigned long address, pgprot_t prot)
285 int err = 0, kernel_map = 0;
286 unsigned long pfn = __pa(address) >> PAGE_SHIFT;
289 if (address >= __START_KERNEL_map &&
290 address < __START_KERNEL_map + KERNEL_TEXT_SIZE) {
292 address = (unsigned long)__va(__pa(address));
297 if (!kernel_map || pte_present(pfn_pte(0, prot))) {
298 err = __change_page_attr(address, pfn, prot);
305 * Handle kernel mapping too which aliases part of
308 if (__pa(address) < KERNEL_TEXT_SIZE) {
312 addr2 = __START_KERNEL_map + __pa(address);
313 /* Make sure the kernel mappings stay executable */
314 prot2 = pte_pgprot(pte_mkexec(pfn_pte(0, prot)));
315 err = __change_page_attr(addr2, pfn, prot2);
322 static int __change_page_attr_set_clr(unsigned long addr, int numpages,
323 pgprot_t mask_set, pgprot_t mask_clr)
330 for (i = 0; i < numpages ; i++) {
332 pte = lookup_address(addr, &level);
336 new_prot = pte_pgprot(*pte);
338 pgprot_val(new_prot) &= ~pgprot_val(mask_clr);
339 pgprot_val(new_prot) |= pgprot_val(mask_set);
341 ret = change_page_attr_addr(addr, new_prot);
350 static int change_page_attr_set_clr(unsigned long addr, int numpages,
351 pgprot_t mask_set, pgprot_t mask_clr)
353 int ret = __change_page_attr_set_clr(addr, numpages, mask_set,
357 * On success we use clflush, when the CPU supports it to
358 * avoid the wbindv. If the CPU does not support it and in the
359 * error case we fall back to cpa_flush_all (which uses
362 if (!ret && cpu_has_clflush)
363 cpa_flush_range(addr, numpages);
370 static inline int change_page_attr_set(unsigned long addr, int numpages,
373 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
376 static inline int change_page_attr_clear(unsigned long addr, int numpages,
379 return __change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
383 int set_memory_uc(unsigned long addr, int numpages)
385 return change_page_attr_set(addr, numpages,
386 __pgprot(_PAGE_PCD | _PAGE_PWT));
388 EXPORT_SYMBOL(set_memory_uc);
390 int set_memory_wb(unsigned long addr, int numpages)
392 return change_page_attr_clear(addr, numpages,
393 __pgprot(_PAGE_PCD | _PAGE_PWT));
395 EXPORT_SYMBOL(set_memory_wb);
397 int set_memory_x(unsigned long addr, int numpages)
399 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
401 EXPORT_SYMBOL(set_memory_x);
403 int set_memory_nx(unsigned long addr, int numpages)
405 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
407 EXPORT_SYMBOL(set_memory_nx);
409 int set_memory_ro(unsigned long addr, int numpages)
411 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
414 int set_memory_rw(unsigned long addr, int numpages)
416 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
419 int set_memory_np(unsigned long addr, int numpages)
421 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
424 int set_pages_uc(struct page *page, int numpages)
426 unsigned long addr = (unsigned long)page_address(page);
428 return set_memory_uc(addr, numpages);
430 EXPORT_SYMBOL(set_pages_uc);
432 int set_pages_wb(struct page *page, int numpages)
434 unsigned long addr = (unsigned long)page_address(page);
436 return set_memory_wb(addr, numpages);
438 EXPORT_SYMBOL(set_pages_wb);
440 int set_pages_x(struct page *page, int numpages)
442 unsigned long addr = (unsigned long)page_address(page);
444 return set_memory_x(addr, numpages);
446 EXPORT_SYMBOL(set_pages_x);
448 int set_pages_nx(struct page *page, int numpages)
450 unsigned long addr = (unsigned long)page_address(page);
452 return set_memory_nx(addr, numpages);
454 EXPORT_SYMBOL(set_pages_nx);
456 int set_pages_ro(struct page *page, int numpages)
458 unsigned long addr = (unsigned long)page_address(page);
460 return set_memory_ro(addr, numpages);
463 int set_pages_rw(struct page *page, int numpages)
465 unsigned long addr = (unsigned long)page_address(page);
467 return set_memory_rw(addr, numpages);
471 #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_CPA_DEBUG)
472 static inline int __change_page_attr_set(unsigned long addr, int numpages,
475 return __change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
478 static inline int __change_page_attr_clear(unsigned long addr, int numpages,
481 return __change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
485 #ifdef CONFIG_DEBUG_PAGEALLOC
487 static int __set_pages_p(struct page *page, int numpages)
489 unsigned long addr = (unsigned long)page_address(page);
491 return __change_page_attr_set(addr, numpages,
492 __pgprot(_PAGE_PRESENT | _PAGE_RW));
495 static int __set_pages_np(struct page *page, int numpages)
497 unsigned long addr = (unsigned long)page_address(page);
499 return __change_page_attr_clear(addr, numpages,
500 __pgprot(_PAGE_PRESENT));
503 void kernel_map_pages(struct page *page, int numpages, int enable)
505 if (PageHighMem(page))
508 debug_check_no_locks_freed(page_address(page),
509 numpages * PAGE_SIZE);
513 * If page allocator is not up yet then do not call c_p_a():
515 if (!debug_pagealloc_enabled)
519 * The return value is ignored - the calls cannot fail,
520 * large pages are disabled at boot time:
523 __set_pages_p(page, numpages);
525 __set_pages_np(page, numpages);
528 * We should perform an IPI and flush all tlbs,
529 * but that can deadlock->flush only current cpu:
536 * The testcases use internal knowledge of the implementation that shouldn't
537 * be exposed to the rest of the kernel. Include these directly here.
539 #ifdef CONFIG_CPA_DEBUG
540 #include "pageattr-test.c"