blob: a270f9ccebfb004bd98238df6405943b10865ed7 [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#include <linux/highmem.h>
6#include <linux/module.h>
Ingo Molnar9f4c8152008-01-30 13:33:41 +01007#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/slab.h>
Ingo Molnar9f4c8152008-01-30 13:33:41 +01009#include <linux/mm.h>
10
Ingo Molnar4554ab92008-01-30 13:34:03 +010011void clflush_cache_range(void *addr, int size)
12{
13 int i;
14
15 for (i = 0; i < size; i += boot_cpu_data.x86_clflush_size)
16 clflush(addr+i);
17}
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/processor.h>
20#include <asm/tlbflush.h>
Dave Jonesf8af0952006-01-06 00:12:10 -080021#include <asm/sections.h>
Ingo Molnar9f4c8152008-01-30 13:33:41 +010022#include <asm/uaccess.h>
23#include <asm/pgalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Ingo Molnarf0646e42008-01-30 13:33:43 +010025pte_t *lookup_address(unsigned long address, int *level)
Ingo Molnar9f4c8152008-01-30 13:33:41 +010026{
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 pgd_t *pgd = pgd_offset_k(address);
28 pud_t *pud;
29 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 if (pgd_none(*pgd))
32 return NULL;
33 pud = pud_offset(pgd, address);
34 if (pud_none(*pud))
35 return NULL;
36 pmd = pmd_offset(pud, address);
37 if (pmd_none(*pmd))
38 return NULL;
Ingo Molnarbbb09f52008-01-30 13:33:59 +010039 *level = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 if (pmd_large(*pmd))
41 return (pte_t *)pmd;
Ingo Molnarbbb09f52008-01-30 13:33:59 +010042 *level = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Ingo Molnar9f4c8152008-01-30 13:33:41 +010044 return pte_offset_kernel(pmd, address);
45}
46
Ingo Molnar9a3dc782008-01-30 13:33:57 +010047static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnar9f4c8152008-01-30 13:33:41 +010048{
Ingo Molnar9f4c8152008-01-30 13:33:41 +010049 /* change init_mm */
50 set_pte_atomic(kpte, pte);
Ingo Molnar44af6c42008-01-30 13:34:03 +010051#ifdef CONFIG_X86_32
Ingo Molnare4b71dc2008-01-30 13:34:04 +010052 if (!SHARED_KERNEL_PMD) {
Ingo Molnar44af6c42008-01-30 13:34:03 +010053 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Ingo Molnar44af6c42008-01-30 13:34:03 +010055 for (page = pgd_list; page; page = (struct page *)page->index) {
56 pgd_t *pgd;
57 pud_t *pud;
58 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010059
Ingo Molnar44af6c42008-01-30 13:34:03 +010060 pgd = (pgd_t *)page_address(page) + pgd_index(address);
61 pud = pud_offset(pgd, address);
62 pmd = pmd_offset(pud, address);
63 set_pte_atomic((pte_t *)pmd, pte);
64 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 }
Ingo Molnar44af6c42008-01-30 13:34:03 +010066#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
Ingo Molnar7afe15b2008-01-30 13:33:57 +010069static int split_large_page(pte_t *kpte, unsigned long address)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010070{
Ingo Molnar7afe15b2008-01-30 13:33:57 +010071 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnar12d6f212008-01-30 13:33:58 +010072 gfp_t gfp_flags = GFP_KERNEL;
Ingo Molnar9a3dc782008-01-30 13:33:57 +010073 unsigned long flags;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010074 unsigned long addr;
75 pte_t *pbase, *tmp;
76 struct page *base;
Ingo Molnar7afe15b2008-01-30 13:33:57 +010077 int i, level;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010078
Ingo Molnar12d6f212008-01-30 13:33:58 +010079#ifdef CONFIG_DEBUG_PAGEALLOC
80 gfp_flags = GFP_ATOMIC;
81#endif
82 base = alloc_pages(gfp_flags, 0);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010083 if (!base)
84 return -ENOMEM;
85
Ingo Molnar9a3dc782008-01-30 13:33:57 +010086 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010087 /*
88 * Check for races, another CPU might have split this page
89 * up for us already:
90 */
91 tmp = lookup_address(address, &level);
Ingo Molnar5508a7482008-01-30 13:33:56 +010092 if (tmp != kpte) {
93 WARN_ON_ONCE(1);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010094 goto out_unlock;
Ingo Molnar5508a7482008-01-30 13:33:56 +010095 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010096
97 address = __pa(address);
98 addr = address & LARGE_PAGE_MASK;
99 pbase = (pte_t *)page_address(base);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100100#ifdef CONFIG_X86_32
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100101 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
Ingo Molnar44af6c42008-01-30 13:34:03 +0100102#endif
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100103
104 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
105 set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
106
107 /*
108 * Install the new, split up pagetable:
109 */
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100110 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100111 base = NULL;
112
113out_unlock:
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100114 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100115
116 if (base)
117 __free_pages(base, 0);
118
119 return 0;
120}
121
Ingo Molnar44af6c42008-01-30 13:34:03 +0100122static int
123__change_page_attr(unsigned long address, struct page *page, pgprot_t prot)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100124{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 struct page *kpte_page;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100126 int level, err = 0;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100127 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 BUG_ON(PageHighMem(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100131repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100132 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (!kpte)
134 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200137 BUG_ON(PageLRU(kpte_page));
138 BUG_ON(PageCompound(kpte_page));
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 /*
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100141 * Better fail early if someone sets the kernel text to NX.
142 * Does not cover __inittext
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 */
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100144 BUG_ON(address >= (unsigned long)&_text &&
145 address < (unsigned long)&_etext &&
146 (pgprot_val(prot) & _PAGE_NX));
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200147
Ingo Molnarbbb09f52008-01-30 13:33:59 +0100148 if (level == 4) {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100149 set_pte_atomic(kpte, mk_pte(page, canon_pgprot(prot)));
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100150 } else {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100151 err = split_large_page(kpte, address);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100152 if (!err)
153 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100155 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100156}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Ingo Molnar44af6c42008-01-30 13:34:03 +0100158/**
159 * change_page_attr_addr - Change page table attributes in linear mapping
160 * @address: Virtual address in linear mapping.
161 * @numpages: Number of pages to change
162 * @prot: New page table attribute (PAGE_*)
163 *
164 * Change page attributes of a page in the direct mapping. This is a variant
165 * of change_page_attr() that also works on memory holes that do not have
166 * mem_map entry (pfn_valid() is false).
167 *
168 * See change_page_attr() documentation for more details.
169 */
170
171int change_page_attr_addr(unsigned long address, int numpages, pgprot_t prot)
172{
173 int err = 0, kernel_map = 0, i;
174
175#ifdef CONFIG_X86_64
176 if (address >= __START_KERNEL_map &&
177 address < __START_KERNEL_map + KERNEL_TEXT_SIZE) {
178
179 address = (unsigned long)__va(__pa(address));
180 kernel_map = 1;
181 }
182#endif
183
184 for (i = 0; i < numpages; i++, address += PAGE_SIZE) {
185 unsigned long pfn = __pa(address) >> PAGE_SHIFT;
186
187 if (!kernel_map || pte_present(pfn_pte(0, prot))) {
188 err = __change_page_attr(address, pfn_to_page(pfn), prot);
189 if (err)
190 break;
191 }
192#ifdef CONFIG_X86_64
193 /*
194 * Handle kernel mapping too which aliases part of
195 * lowmem:
196 */
197 if (__pa(address) < KERNEL_TEXT_SIZE) {
198 unsigned long addr2;
199 pgprot_t prot2;
200
201 addr2 = __START_KERNEL_map + __pa(address);
202 /* Make sure the kernel mappings stay executable */
203 prot2 = pte_pgprot(pte_mkexec(pfn_pte(0, prot)));
204 err = __change_page_attr(addr2, pfn_to_page(pfn), prot2);
205 }
206#endif
207 }
208
209 return err;
210}
211
212/**
213 * change_page_attr - Change page table attributes in the linear mapping.
214 * @page: First page to change
215 * @numpages: Number of pages to change
216 * @prot: New protection/caching type (PAGE_*)
217 *
218 * Returns 0 on success, otherwise a negated errno.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 *
220 * This should be used when a page is mapped with a different caching policy
221 * than write-back somewhere - some CPUs do not like it when mappings with
222 * different caching policies exist. This changes the page attributes of the
223 * in kernel linear mapping too.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100224 *
Ingo Molnar44af6c42008-01-30 13:34:03 +0100225 * Caller must call global_flush_tlb() later to make the changes active.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100226 *
Ingo Molnar44af6c42008-01-30 13:34:03 +0100227 * The caller needs to ensure that there are no conflicting mappings elsewhere
228 * (e.g. in user space) * This function only deals with the kernel linear map.
229 *
230 * For MMIO areas without mem_map use change_page_attr_addr() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
232int change_page_attr(struct page *page, int numpages, pgprot_t prot)
233{
Ingo Molnar44af6c42008-01-30 13:34:03 +0100234 unsigned long addr = (unsigned long)page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Ingo Molnar44af6c42008-01-30 13:34:03 +0100236 return change_page_attr_addr(addr, numpages, prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100238EXPORT_SYMBOL(change_page_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100240static void flush_kernel_map(void *arg)
241{
242 /*
243 * Flush all to work around Errata in early athlons regarding
244 * large page flushing.
245 */
246 __flush_tlb_all();
247
248 if (boot_cpu_data.x86_model >= 4)
249 wbinvd();
250}
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252void global_flush_tlb(void)
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700253{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 BUG_ON(irqs_disabled());
255
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100256 on_each_cpu(flush_kernel_map, NULL, 1, 1);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700257}
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100258EXPORT_SYMBOL(global_flush_tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260#ifdef CONFIG_DEBUG_PAGEALLOC
261void kernel_map_pages(struct page *page, int numpages, int enable)
262{
263 if (PageHighMem(page))
264 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100265 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700266 debug_check_no_locks_freed(page_address(page),
267 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100268 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800269
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100270 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +0100271 * If page allocator is not up yet then do not call c_p_a():
272 */
273 if (!debug_pagealloc_enabled)
274 return;
275
276 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100277 * The return value is ignored - the calls cannot fail,
278 * large pages are disabled at boot time:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 */
280 change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100281
282 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100283 * We should perform an IPI and flush all tlbs,
284 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 */
286 __flush_tlb_all();
287}
288#endif