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 #include <asm/processor.h>
13 #include <asm/tlbflush.h>
14 #include <asm/sections.h>
15 #include <asm/uaccess.h>
16 #include <asm/pgalloc.h>
18 pte_t *lookup_address(unsigned long address, int *level)
20 pgd_t *pgd = pgd_offset_k(address);
26 pud = pud_offset(pgd, address);
29 pmd = pmd_offset(pud, address);
37 return pte_offset_kernel(pmd, address);
40 static void set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
46 set_pte_atomic(kpte, pte);
47 if (SHARED_KERNEL_PMD)
50 spin_lock_irqsave(&pgd_lock, flags);
51 for (page = pgd_list; page; page = (struct page *)page->index) {
56 pgd = (pgd_t *)page_address(page) + pgd_index(address);
57 pud = pud_offset(pgd, address);
58 pmd = pmd_offset(pud, address);
59 set_pte_atomic((pte_t *)pmd, pte);
61 spin_unlock_irqrestore(&pgd_lock, flags);
64 static int split_large_page(pte_t *kpte, unsigned long address)
66 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
72 base = alloc_pages(GFP_KERNEL, 0);
76 down_write(&init_mm.mmap_sem);
78 * Check for races, another CPU might have split this page
81 tmp = lookup_address(address, &level);
87 address = __pa(address);
88 addr = address & LARGE_PAGE_MASK;
89 pbase = (pte_t *)page_address(base);
90 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
92 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
93 set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
96 * Install the new, split up pagetable:
98 set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
102 up_write(&init_mm.mmap_sem);
105 __free_pages(base, 0);
110 static int __change_page_attr(struct page *page, pgprot_t prot)
112 struct page *kpte_page;
113 unsigned long address;
117 BUG_ON(PageHighMem(page));
118 address = (unsigned long)page_address(page);
121 kpte = lookup_address(address, &level);
125 kpte_page = virt_to_page(kpte);
126 BUG_ON(PageLRU(kpte_page));
127 BUG_ON(PageCompound(kpte_page));
130 * Better fail early if someone sets the kernel text to NX.
131 * Does not cover __inittext
133 BUG_ON(address >= (unsigned long)&_text &&
134 address < (unsigned long)&_etext &&
135 (pgprot_val(prot) & _PAGE_NX));
138 set_pte_atomic(kpte, mk_pte(page, canon_pgprot(prot)));
140 err = split_large_page(kpte, address);
148 * Change the page attributes of an page in the linear mapping.
150 * This should be used when a page is mapped with a different caching policy
151 * than write-back somewhere - some CPUs do not like it when mappings with
152 * different caching policies exist. This changes the page attributes of the
153 * in kernel linear mapping too.
155 * The caller needs to ensure that there are no conflicting mappings elsewhere.
156 * This function only deals with the kernel linear map.
158 * Caller must call global_flush_tlb() after this.
160 int change_page_attr(struct page *page, int numpages, pgprot_t prot)
164 for (i = 0; i < numpages; i++, page++) {
165 err = __change_page_attr(page, prot);
172 EXPORT_SYMBOL(change_page_attr);
174 int change_page_attr_addr(unsigned long addr, int numpages, pgprot_t prot)
177 unsigned long pfn = (__pa(addr) >> PAGE_SHIFT);
179 for (i = 0; i < numpages; i++) {
180 if (!pfn_valid(pfn + i)) {
185 pte_t *pte = lookup_address(addr + i*PAGE_SIZE, &level);
186 BUG_ON(pte && pte_none(*pte));
190 return change_page_attr(virt_to_page(addr), i, prot);
193 static void flush_kernel_map(void *arg)
196 * Flush all to work around Errata in early athlons regarding
197 * large page flushing.
201 if (boot_cpu_data.x86_model >= 4)
205 void global_flush_tlb(void)
207 BUG_ON(irqs_disabled());
209 on_each_cpu(flush_kernel_map, NULL, 1, 1);
211 EXPORT_SYMBOL(global_flush_tlb);
213 #ifdef CONFIG_DEBUG_PAGEALLOC
214 void kernel_map_pages(struct page *page, int numpages, int enable)
216 if (PageHighMem(page))
219 debug_check_no_locks_freed(page_address(page),
220 numpages * PAGE_SIZE);
224 * the return value is ignored - the calls cannot fail,
225 * large pages are disabled at boot time.
227 change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
230 * we should perform an IPI and flush all tlbs,
231 * but that can deadlock->flush only current cpu.