blob: 251613449dd6baefdbd4ffa0709e16b3429d3866 [file] [log] [blame]
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001/*
2 * Copyright 2002 Andi Kleen, SuSE Labs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Thanks to Ben LaHaise for precious feedback.
Ingo Molnar9f4c8152008-01-30 13:33:41 +01004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/highmem.h>
7#include <linux/module.h>
Ingo Molnar9f4c8152008-01-30 13:33:41 +01008#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/slab.h>
Ingo Molnar9f4c8152008-01-30 13:33:41 +010010#include <linux/mm.h>
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <asm/processor.h>
13#include <asm/tlbflush.h>
Dave Jonesf8af0952006-01-06 00:12:10 -080014#include <asm/sections.h>
Ingo Molnar9f4c8152008-01-30 13:33:41 +010015#include <asm/uaccess.h>
16#include <asm/pgalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Ingo Molnarf0646e42008-01-30 13:33:43 +010018pte_t *lookup_address(unsigned long address, int *level)
Ingo Molnar9f4c8152008-01-30 13:33:41 +010019{
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 pgd_t *pgd = pgd_offset_k(address);
21 pud_t *pud;
22 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 if (pgd_none(*pgd))
25 return NULL;
26 pud = pud_offset(pgd, address);
27 if (pud_none(*pud))
28 return NULL;
29 pmd = pmd_offset(pud, address);
30 if (pmd_none(*pmd))
31 return NULL;
Ingo Molnarbbb09f52008-01-30 13:33:59 +010032 *level = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 if (pmd_large(*pmd))
34 return (pte_t *)pmd;
Ingo Molnarbbb09f52008-01-30 13:33:59 +010035 *level = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Ingo Molnar9f4c8152008-01-30 13:33:41 +010037 return pte_offset_kernel(pmd, address);
38}
39
Ingo Molnar9a3dc782008-01-30 13:33:57 +010040static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnar9f4c8152008-01-30 13:33:41 +010041{
Ingo Molnar9f4c8152008-01-30 13:33:41 +010042 /* change init_mm */
43 set_pte_atomic(kpte, pte);
Ingo Molnar44af6c42008-01-30 13:34:03 +010044#ifdef CONFIG_X86_32
Jeremy Fitzhardinge5311ab62007-05-02 19:27:13 +020045 if (SHARED_KERNEL_PMD)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 return;
Ingo Molnar44af6c42008-01-30 13:34:03 +010047 {
48 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Ingo Molnar44af6c42008-01-30 13:34:03 +010050 for (page = pgd_list; page; page = (struct page *)page->index) {
51 pgd_t *pgd;
52 pud_t *pud;
53 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010054
Ingo Molnar44af6c42008-01-30 13:34:03 +010055 pgd = (pgd_t *)page_address(page) + pgd_index(address);
56 pud = pud_offset(pgd, address);
57 pmd = pmd_offset(pud, address);
58 set_pte_atomic((pte_t *)pmd, pte);
59 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 }
Ingo Molnar44af6c42008-01-30 13:34:03 +010061#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
Ingo Molnar7afe15b2008-01-30 13:33:57 +010064static int split_large_page(pte_t *kpte, unsigned long address)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010065{
Ingo Molnar7afe15b2008-01-30 13:33:57 +010066 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnar12d6f212008-01-30 13:33:58 +010067 gfp_t gfp_flags = GFP_KERNEL;
Ingo Molnar9a3dc782008-01-30 13:33:57 +010068 unsigned long flags;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010069 unsigned long addr;
70 pte_t *pbase, *tmp;
71 struct page *base;
Ingo Molnar7afe15b2008-01-30 13:33:57 +010072 int i, level;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010073
Ingo Molnar12d6f212008-01-30 13:33:58 +010074#ifdef CONFIG_DEBUG_PAGEALLOC
75 gfp_flags = GFP_ATOMIC;
76#endif
77 base = alloc_pages(gfp_flags, 0);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010078 if (!base)
79 return -ENOMEM;
80
Ingo Molnar9a3dc782008-01-30 13:33:57 +010081 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010082 /*
83 * Check for races, another CPU might have split this page
84 * up for us already:
85 */
86 tmp = lookup_address(address, &level);
Ingo Molnar5508a742008-01-30 13:33:56 +010087 if (tmp != kpte) {
88 WARN_ON_ONCE(1);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010089 goto out_unlock;
Ingo Molnar5508a742008-01-30 13:33:56 +010090 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010091
92 address = __pa(address);
93 addr = address & LARGE_PAGE_MASK;
94 pbase = (pte_t *)page_address(base);
Ingo Molnar44af6c42008-01-30 13:34:03 +010095#ifdef CONFIG_X86_32
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010096 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
Ingo Molnar44af6c42008-01-30 13:34:03 +010097#endif
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010098
99 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
100 set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
101
102 /*
103 * Install the new, split up pagetable:
104 */
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100105 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100106 base = NULL;
107
108out_unlock:
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100109 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100110
111 if (base)
112 __free_pages(base, 0);
113
114 return 0;
115}
116
Ingo Molnar44af6c42008-01-30 13:34:03 +0100117static int
118__change_page_attr(unsigned long address, struct page *page, pgprot_t prot)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100119{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 struct page *kpte_page;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100121 int level, err = 0;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100122 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 BUG_ON(PageHighMem(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100126repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100127 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (!kpte)
129 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200132 BUG_ON(PageLRU(kpte_page));
133 BUG_ON(PageCompound(kpte_page));
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 /*
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100136 * Better fail early if someone sets the kernel text to NX.
137 * Does not cover __inittext
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 */
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100139 BUG_ON(address >= (unsigned long)&_text &&
140 address < (unsigned long)&_etext &&
141 (pgprot_val(prot) & _PAGE_NX));
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200142
Ingo Molnarbbb09f52008-01-30 13:33:59 +0100143 if (level == 4) {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100144 set_pte_atomic(kpte, mk_pte(page, canon_pgprot(prot)));
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100145 } else {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100146 err = split_large_page(kpte, address);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100147 if (!err)
148 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100150 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100151}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Ingo Molnar44af6c42008-01-30 13:34:03 +0100153/**
154 * change_page_attr_addr - Change page table attributes in linear mapping
155 * @address: Virtual address in linear mapping.
156 * @numpages: Number of pages to change
157 * @prot: New page table attribute (PAGE_*)
158 *
159 * Change page attributes of a page in the direct mapping. This is a variant
160 * of change_page_attr() that also works on memory holes that do not have
161 * mem_map entry (pfn_valid() is false).
162 *
163 * See change_page_attr() documentation for more details.
164 */
165
166int change_page_attr_addr(unsigned long address, int numpages, pgprot_t prot)
167{
168 int err = 0, kernel_map = 0, i;
169
170#ifdef CONFIG_X86_64
171 if (address >= __START_KERNEL_map &&
172 address < __START_KERNEL_map + KERNEL_TEXT_SIZE) {
173
174 address = (unsigned long)__va(__pa(address));
175 kernel_map = 1;
176 }
177#endif
178
179 for (i = 0; i < numpages; i++, address += PAGE_SIZE) {
180 unsigned long pfn = __pa(address) >> PAGE_SHIFT;
181
182 if (!kernel_map || pte_present(pfn_pte(0, prot))) {
183 err = __change_page_attr(address, pfn_to_page(pfn), prot);
184 if (err)
185 break;
186 }
187#ifdef CONFIG_X86_64
188 /*
189 * Handle kernel mapping too which aliases part of
190 * lowmem:
191 */
192 if (__pa(address) < KERNEL_TEXT_SIZE) {
193 unsigned long addr2;
194 pgprot_t prot2;
195
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_to_page(pfn), prot2);
200 }
201#endif
202 }
203
204 return err;
205}
206
207/**
208 * change_page_attr - Change page table attributes in the linear mapping.
209 * @page: First page to change
210 * @numpages: Number of pages to change
211 * @prot: New protection/caching type (PAGE_*)
212 *
213 * Returns 0 on success, otherwise a negated errno.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 *
215 * This should be used when a page is mapped with a different caching policy
216 * than write-back somewhere - some CPUs do not like it when mappings with
217 * different caching policies exist. This changes the page attributes of the
218 * in kernel linear mapping too.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100219 *
Ingo Molnar44af6c42008-01-30 13:34:03 +0100220 * Caller must call global_flush_tlb() later to make the changes active.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100221 *
Ingo Molnar44af6c42008-01-30 13:34:03 +0100222 * The caller needs to ensure that there are no conflicting mappings elsewhere
223 * (e.g. in user space) * This function only deals with the kernel linear map.
224 *
225 * For MMIO areas without mem_map use change_page_attr_addr() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 */
227int change_page_attr(struct page *page, int numpages, pgprot_t prot)
228{
Ingo Molnar44af6c42008-01-30 13:34:03 +0100229 unsigned long addr = (unsigned long)page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Ingo Molnar44af6c42008-01-30 13:34:03 +0100231 return change_page_attr_addr(addr, numpages, prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100233EXPORT_SYMBOL(change_page_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100235static void flush_kernel_map(void *arg)
236{
237 /*
238 * Flush all to work around Errata in early athlons regarding
239 * large page flushing.
240 */
241 __flush_tlb_all();
242
243 if (boot_cpu_data.x86_model >= 4)
244 wbinvd();
245}
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247void global_flush_tlb(void)
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700248{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 BUG_ON(irqs_disabled());
250
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100251 on_each_cpu(flush_kernel_map, NULL, 1, 1);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700252}
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100253EXPORT_SYMBOL(global_flush_tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255#ifdef CONFIG_DEBUG_PAGEALLOC
256void kernel_map_pages(struct page *page, int numpages, int enable)
257{
258 if (PageHighMem(page))
259 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100260 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700261 debug_check_no_locks_freed(page_address(page),
262 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100263 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800264
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100265 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +0100266 * If page allocator is not up yet then do not call c_p_a():
267 */
268 if (!debug_pagealloc_enabled)
269 return;
270
271 /*
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100272 * the return value is ignored - the calls cannot fail,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * large pages are disabled at boot time.
274 */
275 change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100276
277 /*
278 * we should perform an IPI and flush all tlbs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * but that can deadlock->flush only current cpu.
280 */
281 __flush_tlb_all();
282}
283#endif