2 * Copyright 2002 Andi Kleen, SuSE Labs.
3 * Thanks to Ben LaHaise for precious feedback.
6 #include <linux/config.h>
8 #include <linux/sched.h>
9 #include <linux/highmem.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <asm/uaccess.h>
13 #include <asm/processor.h>
14 #include <asm/tlbflush.h>
15 #include <asm/pgalloc.h>
18 void pte_free(struct page *pte)
22 ptep = pfn_to_kaddr(page_to_pfn(pte));
24 xen_pte_unpin(__pa(ptep));
25 make_page_writable(ptep);
29 static inline pte_t *lookup_address(unsigned long address)
31 pgd_t *pgd = pgd_offset_k(address);
37 pud = pud_offset(pgd, address);
38 if (!pud_present(*pud))
40 pmd = pmd_offset(pud, address);
41 if (!pmd_present(*pmd))
45 pte = pte_offset_kernel(pmd, address);
46 if (pte && !pte_present(*pte))
51 static struct page *split_large_page(unsigned long address, pgprot_t prot,
56 struct page *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,
65 addr == address ? prot : ref_prot);
71 static void flush_kernel_map(void *address)
73 if (0 && address && cpu_has_clflush) {
74 /* is this worth it? */
76 for (i = 0; i < PAGE_SIZE; i += boot_cpu_data.x86_clflush_size)
77 asm volatile("clflush (%0)" :: "r" (address + i));
79 asm volatile("wbinvd":::"memory");
81 __flush_tlb_one((unsigned long) address);
87 static inline void flush_map(unsigned long address)
89 on_each_cpu(flush_kernel_map, (void *)address, 1, 1);
92 struct deferred_page {
93 struct deferred_page *next;
95 unsigned long address;
97 static struct deferred_page *df_list; /* protected by init_mm.mmap_sem */
99 static inline void save_page(unsigned long address, struct page *fpage)
101 struct deferred_page *df;
102 df = kmalloc(sizeof(struct deferred_page), GFP_KERNEL);
109 df->address = address;
115 * No more special protections in this 2/4MB area - revert to a
118 static void revert_page(unsigned long address, pgprot_t ref_prot)
125 pgd = pgd_offset_k(address);
126 BUG_ON(pgd_none(*pgd));
127 pud = pud_offset(pgd,address);
128 BUG_ON(pud_none(*pud));
129 pmd = pmd_offset(pud, address);
130 BUG_ON(pmd_val(*pmd) & _PAGE_PSE);
131 pgprot_val(ref_prot) |= _PAGE_PSE;
132 large_pte = mk_pte_phys(__pa(address) & LARGE_PAGE_MASK, ref_prot);
133 set_pte((pte_t *)pmd, large_pte);
137 __change_page_attr(unsigned long address, unsigned long pfn, pgprot_t prot,
141 struct page *kpte_page;
143 kpte = lookup_address(address);
145 kpte_page = virt_to_page(((unsigned long)kpte) & PAGE_MASK);
146 kpte_flags = pte_val(*kpte);
147 if (pgprot_val(prot) != pgprot_val(ref_prot)) {
148 if ((kpte_flags & _PAGE_PSE) == 0) {
149 set_pte(kpte, pfn_pte(pfn, prot));
152 * split_large_page will take the reference for this change_page_attr
155 struct page *split = split_large_page(address, prot, ref_prot);
158 set_pte(kpte,mk_pte(split, ref_prot));
162 } else if ((kpte_flags & _PAGE_PSE) == 0) {
163 set_pte(kpte, pfn_pte(pfn, ref_prot));
164 __put_page(kpte_page);
168 /* on x86-64 the direct mapping set at boot is not using 4k pages */
169 BUG_ON(PageReserved(kpte_page));
171 switch (page_count(kpte_page)) {
173 save_page(address, kpte_page);
174 revert_page(address, ref_prot);
177 BUG(); /* memleak and failed 2M page regeneration */
183 * Change the page attributes of an page in the linear mapping.
185 * This should be used when a page is mapped with a different caching policy
186 * than write-back somewhere - some CPUs do not like it when mappings with
187 * different caching policies exist. This changes the page attributes of the
188 * in kernel linear mapping too.
190 * The caller needs to ensure that there are no conflicting mappings elsewhere.
191 * This function only deals with the kernel linear map.
193 * Caller must call global_flush_tlb() after this.
195 int change_page_attr_addr(unsigned long address, int numpages, pgprot_t prot)
200 down_write(&init_mm.mmap_sem);
201 for (i = 0; i < numpages; i++, address += PAGE_SIZE) {
202 unsigned long pfn = __pa(address) >> PAGE_SHIFT;
204 err = __change_page_attr(address, pfn, prot, PAGE_KERNEL);
207 /* Handle kernel mapping too which aliases part of the
209 if (__pa(address) < KERNEL_TEXT_SIZE) {
211 pgprot_t prot2 = prot;
212 addr2 = __START_KERNEL_map + __pa(address);
213 pgprot_val(prot2) &= ~_PAGE_NX;
214 err = __change_page_attr(addr2, pfn, prot2, PAGE_KERNEL_EXEC);
217 up_write(&init_mm.mmap_sem);
221 /* Don't call this for MMIO areas that may not have a mem_map entry */
222 int change_page_attr(struct page *page, int numpages, pgprot_t prot)
224 unsigned long addr = (unsigned long)page_address(page);
225 return change_page_attr_addr(addr, numpages, prot);
228 void global_flush_tlb(void)
230 struct deferred_page *df, *next_df;
232 down_read(&init_mm.mmap_sem);
233 df = xchg(&df_list, NULL);
234 up_read(&init_mm.mmap_sem);
237 flush_map((df && !df->next) ? df->address : 0);
238 for (; df; df = next_df) {
241 __free_page(df->fpage);
246 EXPORT_SYMBOL(change_page_attr);
247 EXPORT_SYMBOL(global_flush_tlb);