2 * Copyright 2002 Andi Kleen, SuSE Labs.
3 * Thanks to Ben LaHaise for precious feedback.
6 #include <linux/highmem.h>
7 #include <linux/module.h>
8 #include <linux/sched.h>
9 #include <linux/slab.h>
12 void clflush_cache_range(void *addr, int size)
16 for (i = 0; i < size; i += boot_cpu_data.x86_clflush_size)
20 #include <asm/processor.h>
21 #include <asm/tlbflush.h>
22 #include <asm/sections.h>
23 #include <asm/uaccess.h>
24 #include <asm/pgalloc.h>
26 pte_t *lookup_address(unsigned long address, int *level)
28 pgd_t *pgd = pgd_offset_k(address);
34 pud = pud_offset(pgd, address);
37 pmd = pmd_offset(pud, address);
45 return pte_offset_kernel(pmd, address);
49 split_large_page(unsigned long address, pgprot_t ref_prot)
56 base = alloc_pages(GFP_KERNEL, 0);
60 address = __pa(address);
61 addr = address & LARGE_PAGE_MASK;
62 pbase = (pte_t *)page_address(base);
63 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
64 pbase[i] = pfn_pte(addr >> PAGE_SHIFT, ref_prot);
70 __change_page_attr(unsigned long address, unsigned long pfn, pgprot_t prot,
73 struct page *kpte_page;
75 pgprot_t ref_prot2, oldprot;
79 kpte = lookup_address(address, &level);
83 kpte_page = virt_to_page(kpte);
84 oldprot = pte_pgprot(*kpte);
85 BUG_ON(PageLRU(kpte_page));
86 BUG_ON(PageCompound(kpte_page));
87 ref_prot = canon_pgprot(ref_prot);
88 prot = canon_pgprot(prot);
90 if (pgprot_val(prot) != pgprot_val(ref_prot)) {
92 set_pte(kpte, pfn_pte(pfn, prot));
95 * split_large_page will take the reference for this
96 * change_page_attr on the split page.
100 ref_prot2 = pte_pgprot(pte_clrhuge(*kpte));
101 split = split_large_page(address, ref_prot2);
104 pgprot_val(ref_prot2) &= ~_PAGE_NX;
105 set_pte(kpte, mk_pte(split, ref_prot2));
110 set_pte(kpte, pfn_pte(pfn, ref_prot));
119 * change_page_attr_addr - Change page table attributes in linear mapping
120 * @address: Virtual address in linear mapping.
121 * @numpages: Number of pages to change
122 * @prot: New page table attribute (PAGE_*)
124 * Change page attributes of a page in the direct mapping. This is a variant
125 * of change_page_attr() that also works on memory holes that do not have
126 * mem_map entry (pfn_valid() is false).
128 * See change_page_attr() documentation for more details.
131 int change_page_attr_addr(unsigned long address, int numpages, pgprot_t prot)
133 int err = 0, kernel_map = 0, i;
135 if (address >= __START_KERNEL_map &&
136 address < __START_KERNEL_map + KERNEL_TEXT_SIZE) {
138 address = (unsigned long)__va(__pa(address));
142 down_write(&init_mm.mmap_sem);
143 for (i = 0; i < numpages; i++, address += PAGE_SIZE) {
144 unsigned long pfn = __pa(address) >> PAGE_SHIFT;
146 if (!kernel_map || pte_present(pfn_pte(0, prot))) {
147 err = __change_page_attr(address, pfn, prot,
152 /* Handle kernel mapping too which aliases part of the
154 if (__pa(address) < KERNEL_TEXT_SIZE) {
158 addr2 = __START_KERNEL_map + __pa(address);
159 /* Make sure the kernel mappings stay executable */
160 prot2 = pte_pgprot(pte_mkexec(pfn_pte(0, prot)));
161 err = __change_page_attr(addr2, pfn, prot2,
165 up_write(&init_mm.mmap_sem);
171 * change_page_attr - Change page table attributes in the linear mapping.
172 * @page: First page to change
173 * @numpages: Number of pages to change
174 * @prot: New protection/caching type (PAGE_*)
176 * Returns 0 on success, otherwise a negated errno.
178 * This should be used when a page is mapped with a different caching policy
179 * than write-back somewhere - some CPUs do not like it when mappings with
180 * different caching policies exist. This changes the page attributes of the
181 * in kernel linear mapping too.
183 * Caller must call global_flush_tlb() later to make the changes active.
185 * The caller needs to ensure that there are no conflicting mappings elsewhere
186 * (e.g. in user space) * This function only deals with the kernel linear map.
188 * For MMIO areas without mem_map use change_page_attr_addr() instead.
190 int change_page_attr(struct page *page, int numpages, pgprot_t prot)
192 unsigned long addr = (unsigned long)page_address(page);
194 return change_page_attr_addr(addr, numpages, prot);
196 EXPORT_SYMBOL(change_page_attr);
198 static void flush_kernel_map(void *arg)
201 * Flush all to work around Errata in early athlons regarding
202 * large page flushing.
206 if (boot_cpu_data.x86_model >= 4)
210 void global_flush_tlb(void)
212 BUG_ON(irqs_disabled());
214 on_each_cpu(flush_kernel_map, NULL, 1, 1);
216 EXPORT_SYMBOL(global_flush_tlb);