2 * Copyright 2002 Andi Kleen, SuSE Labs.
3 * Thanks to Ben LaHaise for precious feedback.
7 #include <linux/sched.h>
8 #include <linux/highmem.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <asm/uaccess.h>
12 #include <asm/processor.h>
13 #include <asm/tlbflush.h>
16 static inline pte_t *lookup_address(unsigned long address)
18 pgd_t *pgd = pgd_offset_k(address);
24 pud = pud_offset(pgd, address);
25 if (!pud_present(*pud))
27 pmd = pmd_offset(pud, address);
28 if (!pmd_present(*pmd))
32 pte = pte_offset_kernel(pmd, address);
33 if (pte && !pte_present(*pte))
38 static struct page *split_large_page(unsigned long address, pgprot_t prot,
43 struct page *base = alloc_pages(GFP_KERNEL, 0);
48 * page_private is used to track the number of entries in
49 * the page table page have non standard attributes.
52 page_private(base) = 0;
54 address = __pa(address);
55 addr = address & LARGE_PAGE_MASK;
56 pbase = (pte_t *)page_address(base);
57 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE) {
58 pbase[i] = pfn_pte(addr >> PAGE_SHIFT,
59 addr == address ? prot : ref_prot);
64 static void cache_flush_page(void *adr)
67 for (i = 0; i < PAGE_SIZE; i += boot_cpu_data.x86_clflush_size)
68 asm volatile("clflush (%0)" :: "r" (adr + i));
71 static void flush_kernel_map(void *arg)
73 struct list_head *l = (struct list_head *)arg;
76 /* When clflush is available always use it because it is
77 much cheaper than WBINVD */
79 asm volatile("wbinvd" ::: "memory");
80 list_for_each_entry(pg, l, lru) {
81 void *adr = page_address(pg);
83 cache_flush_page(adr);
88 static inline void flush_map(struct list_head *l)
90 on_each_cpu(flush_kernel_map, l, 1, 1);
93 static LIST_HEAD(deferred_pages); /* protected by init_mm.mmap_sem */
95 static inline void save_page(struct page *fpage)
97 list_add(&fpage->lru, &deferred_pages);
101 * No more special protections in this 2/4MB area - revert to a
104 static void revert_page(unsigned long address, pgprot_t ref_prot)
111 pgd = pgd_offset_k(address);
112 BUG_ON(pgd_none(*pgd));
113 pud = pud_offset(pgd,address);
114 BUG_ON(pud_none(*pud));
115 pmd = pmd_offset(pud, address);
116 BUG_ON(pmd_val(*pmd) & _PAGE_PSE);
117 large_pte = mk_pte_phys(__pa(address) & LARGE_PAGE_MASK, ref_prot);
118 large_pte = pte_mkhuge(large_pte);
119 set_pte((pte_t *)pmd, large_pte);
123 __change_page_attr(unsigned long address, unsigned long pfn, pgprot_t prot,
127 struct page *kpte_page;
129 kpte = lookup_address(address);
131 kpte_page = virt_to_page(((unsigned long)kpte) & PAGE_MASK);
132 if (pgprot_val(prot) != pgprot_val(ref_prot)) {
133 if (!pte_huge(*kpte)) {
134 set_pte(kpte, pfn_pte(pfn, prot));
137 * split_large_page will take the reference for this
138 * change_page_attr on the split page.
141 ref_prot2 = pte_pgprot(pte_clrhuge(*kpte));
142 split = split_large_page(address, prot, ref_prot2);
145 set_pte(kpte, mk_pte(split, ref_prot2));
148 page_private(kpte_page)++;
149 } else if (!pte_huge(*kpte)) {
150 set_pte(kpte, pfn_pte(pfn, ref_prot));
151 BUG_ON(page_private(kpte_page) == 0);
152 page_private(kpte_page)--;
156 /* on x86-64 the direct mapping set at boot is not using 4k pages */
157 BUG_ON(PageReserved(kpte_page));
159 if (page_private(kpte_page) == 0) {
160 save_page(kpte_page);
161 revert_page(address, ref_prot);
167 * Change the page attributes of an page in the linear mapping.
169 * This should be used when a page is mapped with a different caching policy
170 * than write-back somewhere - some CPUs do not like it when mappings with
171 * different caching policies exist. This changes the page attributes of the
172 * in kernel linear mapping too.
174 * The caller needs to ensure that there are no conflicting mappings elsewhere.
175 * This function only deals with the kernel linear map.
177 * Caller must call global_flush_tlb() after this.
179 int change_page_attr_addr(unsigned long address, int numpages, pgprot_t prot)
184 down_write(&init_mm.mmap_sem);
185 for (i = 0; i < numpages; i++, address += PAGE_SIZE) {
186 unsigned long pfn = __pa(address) >> PAGE_SHIFT;
188 err = __change_page_attr(address, pfn, prot, PAGE_KERNEL);
191 /* Handle kernel mapping too which aliases part of the
193 if (__pa(address) < KERNEL_TEXT_SIZE) {
196 addr2 = __START_KERNEL_map + __pa(address);
197 /* Make sure the kernel mappings stay executable */
198 prot2 = pte_pgprot(pte_mkexec(pfn_pte(0, prot)));
199 err = __change_page_attr(addr2, pfn, prot2,
203 up_write(&init_mm.mmap_sem);
207 /* Don't call this for MMIO areas that may not have a mem_map entry */
208 int change_page_attr(struct page *page, int numpages, pgprot_t prot)
210 unsigned long addr = (unsigned long)page_address(page);
211 return change_page_attr_addr(addr, numpages, prot);
214 void global_flush_tlb(void)
216 struct page *pg, *next;
219 down_read(&init_mm.mmap_sem);
220 list_replace_init(&deferred_pages, &l);
221 up_read(&init_mm.mmap_sem);
225 list_for_each_entry_safe(pg, next, &l, lru) {
226 ClearPagePrivate(pg);
231 EXPORT_SYMBOL(change_page_attr);
232 EXPORT_SYMBOL(global_flush_tlb);