blob: cbe8e9223bee9fa9d40759a5c3ad02b0e7a08a3c [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>
Ingo Molnar81922062008-01-30 13:34:04 +01006#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#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
Ingo Molnar4554ab92008-01-30 13:34:03 +010012void clflush_cache_range(void *addr, int size)
13{
14 int i;
15
16 for (i = 0; i < size; i += boot_cpu_data.x86_clflush_size)
17 clflush(addr+i);
18}
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/processor.h>
21#include <asm/tlbflush.h>
Dave Jonesf8af0952006-01-06 00:12:10 -080022#include <asm/sections.h>
Ingo Molnar9f4c8152008-01-30 13:33:41 +010023#include <asm/uaccess.h>
24#include <asm/pgalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Ingo Molnar687c4822008-01-30 13:34:04 +010026/*
27 * We allow the BIOS range to be executable:
28 */
29#define BIOS_BEGIN 0x000a0000
30#define BIOS_END 0x00100000
31
32static inline pgprot_t check_exec(pgprot_t prot, unsigned long address)
33{
34 if (__pa(address) >= BIOS_BEGIN && __pa(address) < BIOS_END)
35 pgprot_val(prot) &= ~_PAGE_NX;
36 /*
37 * Better fail early if someone sets the kernel text to NX.
38 * Does not cover __inittext
39 */
40 BUG_ON(address >= (unsigned long)&_text &&
41 address < (unsigned long)&_etext &&
42 (pgprot_val(prot) & _PAGE_NX));
43
44 return prot;
45}
46
Ingo Molnarf0646e42008-01-30 13:33:43 +010047pte_t *lookup_address(unsigned long address, int *level)
Ingo Molnar9f4c8152008-01-30 13:33:41 +010048{
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 pgd_t *pgd = pgd_offset_k(address);
50 pud_t *pud;
51 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010052
Thomas Gleixner30551bb2008-01-30 13:34:04 +010053 *level = PG_LEVEL_NONE;
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (pgd_none(*pgd))
56 return NULL;
57 pud = pud_offset(pgd, address);
58 if (pud_none(*pud))
59 return NULL;
60 pmd = pmd_offset(pud, address);
61 if (pmd_none(*pmd))
62 return NULL;
Thomas Gleixner30551bb2008-01-30 13:34:04 +010063
64 *level = PG_LEVEL_2M;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if (pmd_large(*pmd))
66 return (pte_t *)pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Thomas Gleixner30551bb2008-01-30 13:34:04 +010068 *level = PG_LEVEL_4K;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010069 return pte_offset_kernel(pmd, address);
70}
71
Ingo Molnar9a3dc782008-01-30 13:33:57 +010072static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnar9f4c8152008-01-30 13:33:41 +010073{
Ingo Molnar9f4c8152008-01-30 13:33:41 +010074 /* change init_mm */
75 set_pte_atomic(kpte, pte);
Ingo Molnar44af6c42008-01-30 13:34:03 +010076#ifdef CONFIG_X86_32
Ingo Molnare4b71dc2008-01-30 13:34:04 +010077 if (!SHARED_KERNEL_PMD) {
Ingo Molnar44af6c42008-01-30 13:34:03 +010078 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Ingo Molnar44af6c42008-01-30 13:34:03 +010080 for (page = pgd_list; page; page = (struct page *)page->index) {
81 pgd_t *pgd;
82 pud_t *pud;
83 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010084
Ingo Molnar44af6c42008-01-30 13:34:03 +010085 pgd = (pgd_t *)page_address(page) + pgd_index(address);
86 pud = pud_offset(pgd, address);
87 pmd = pmd_offset(pud, address);
88 set_pte_atomic((pte_t *)pmd, pte);
89 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
Ingo Molnar44af6c42008-01-30 13:34:03 +010091#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Ingo Molnar7afe15b2008-01-30 13:33:57 +010094static int split_large_page(pte_t *kpte, unsigned long address)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010095{
Ingo Molnar7afe15b2008-01-30 13:33:57 +010096 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnar12d6f212008-01-30 13:33:58 +010097 gfp_t gfp_flags = GFP_KERNEL;
Ingo Molnar9a3dc782008-01-30 13:33:57 +010098 unsigned long flags;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010099 unsigned long addr;
100 pte_t *pbase, *tmp;
101 struct page *base;
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100102 int i, level;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100103
Ingo Molnar12d6f212008-01-30 13:33:58 +0100104#ifdef CONFIG_DEBUG_PAGEALLOC
105 gfp_flags = GFP_ATOMIC;
106#endif
107 base = alloc_pages(gfp_flags, 0);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100108 if (!base)
109 return -ENOMEM;
110
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100111 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100112 /*
113 * Check for races, another CPU might have split this page
114 * up for us already:
115 */
116 tmp = lookup_address(address, &level);
Ingo Molnar5508a7482008-01-30 13:33:56 +0100117 if (tmp != kpte) {
118 WARN_ON_ONCE(1);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100119 goto out_unlock;
Ingo Molnar5508a7482008-01-30 13:33:56 +0100120 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100121
122 address = __pa(address);
123 addr = address & LARGE_PAGE_MASK;
124 pbase = (pte_t *)page_address(base);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100125#ifdef CONFIG_X86_32
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100126 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
Ingo Molnar44af6c42008-01-30 13:34:03 +0100127#endif
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100128
129 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
130 set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
131
132 /*
Huang, Ying4c881ca2008-01-30 13:34:04 +0100133 * Install the new, split up pagetable. Important detail here:
134 *
135 * On Intel the NX bit of all levels must be cleared to make a
136 * page executable. See section 4.13.2 of Intel 64 and IA-32
137 * Architectures Software Developer's Manual).
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100138 */
Huang, Ying4c881ca2008-01-30 13:34:04 +0100139 ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100140 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100141 base = NULL;
142
143out_unlock:
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100144 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100145
146 if (base)
147 __free_pages(base, 0);
148
149 return 0;
150}
151
Ingo Molnar44af6c42008-01-30 13:34:03 +0100152static int
Ingo Molnar81922062008-01-30 13:34:04 +0100153__change_page_attr(unsigned long address, unsigned long pfn, pgprot_t prot)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100154{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct page *kpte_page;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100156 int level, err = 0;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100157 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Ingo Molnar81922062008-01-30 13:34:04 +0100159#ifdef CONFIG_X86_32
160 BUG_ON(pfn > max_low_pfn);
161#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100163repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100164 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (!kpte)
166 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200169 BUG_ON(PageLRU(kpte_page));
170 BUG_ON(PageCompound(kpte_page));
171
Ingo Molnar687c4822008-01-30 13:34:04 +0100172 prot = check_exec(prot, address);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200173
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100174 if (level == PG_LEVEL_4K) {
Ingo Molnar81922062008-01-30 13:34:04 +0100175 set_pte_atomic(kpte, pfn_pte(pfn, canon_pgprot(prot)));
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100176 } else {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100177 err = split_large_page(kpte, address);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100178 if (!err)
179 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100181 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100182}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Ingo Molnar44af6c42008-01-30 13:34:03 +0100184/**
185 * change_page_attr_addr - Change page table attributes in linear mapping
186 * @address: Virtual address in linear mapping.
187 * @numpages: Number of pages to change
188 * @prot: New page table attribute (PAGE_*)
189 *
190 * Change page attributes of a page in the direct mapping. This is a variant
191 * of change_page_attr() that also works on memory holes that do not have
192 * mem_map entry (pfn_valid() is false).
193 *
194 * See change_page_attr() documentation for more details.
195 */
196
197int change_page_attr_addr(unsigned long address, int numpages, pgprot_t prot)
198{
199 int err = 0, kernel_map = 0, i;
200
201#ifdef CONFIG_X86_64
202 if (address >= __START_KERNEL_map &&
203 address < __START_KERNEL_map + KERNEL_TEXT_SIZE) {
204
205 address = (unsigned long)__va(__pa(address));
206 kernel_map = 1;
207 }
208#endif
209
210 for (i = 0; i < numpages; i++, address += PAGE_SIZE) {
211 unsigned long pfn = __pa(address) >> PAGE_SHIFT;
212
213 if (!kernel_map || pte_present(pfn_pte(0, prot))) {
Ingo Molnar81922062008-01-30 13:34:04 +0100214 err = __change_page_attr(address, pfn, prot);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100215 if (err)
216 break;
217 }
218#ifdef CONFIG_X86_64
219 /*
220 * Handle kernel mapping too which aliases part of
221 * lowmem:
222 */
223 if (__pa(address) < KERNEL_TEXT_SIZE) {
224 unsigned long addr2;
225 pgprot_t prot2;
226
227 addr2 = __START_KERNEL_map + __pa(address);
228 /* Make sure the kernel mappings stay executable */
229 prot2 = pte_pgprot(pte_mkexec(pfn_pte(0, prot)));
Ingo Molnar81922062008-01-30 13:34:04 +0100230 err = __change_page_attr(addr2, pfn, prot2);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100231 }
232#endif
233 }
234
235 return err;
236}
237
238/**
239 * change_page_attr - Change page table attributes in the linear mapping.
240 * @page: First page to change
241 * @numpages: Number of pages to change
242 * @prot: New protection/caching type (PAGE_*)
243 *
244 * Returns 0 on success, otherwise a negated errno.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 *
246 * This should be used when a page is mapped with a different caching policy
247 * than write-back somewhere - some CPUs do not like it when mappings with
248 * different caching policies exist. This changes the page attributes of the
249 * in kernel linear mapping too.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100250 *
Ingo Molnar44af6c42008-01-30 13:34:03 +0100251 * Caller must call global_flush_tlb() later to make the changes active.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100252 *
Ingo Molnar44af6c42008-01-30 13:34:03 +0100253 * The caller needs to ensure that there are no conflicting mappings elsewhere
254 * (e.g. in user space) * This function only deals with the kernel linear map.
255 *
256 * For MMIO areas without mem_map use change_page_attr_addr() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
258int change_page_attr(struct page *page, int numpages, pgprot_t prot)
259{
Ingo Molnar44af6c42008-01-30 13:34:03 +0100260 unsigned long addr = (unsigned long)page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Ingo Molnar44af6c42008-01-30 13:34:03 +0100262 return change_page_attr_addr(addr, numpages, prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100264EXPORT_SYMBOL(change_page_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100266static void flush_kernel_map(void *arg)
267{
268 /*
269 * Flush all to work around Errata in early athlons regarding
270 * large page flushing.
271 */
272 __flush_tlb_all();
273
274 if (boot_cpu_data.x86_model >= 4)
275 wbinvd();
276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278void global_flush_tlb(void)
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700279{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 BUG_ON(irqs_disabled());
281
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100282 on_each_cpu(flush_kernel_map, NULL, 1, 1);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700283}
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100284EXPORT_SYMBOL(global_flush_tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286#ifdef CONFIG_DEBUG_PAGEALLOC
287void kernel_map_pages(struct page *page, int numpages, int enable)
288{
289 if (PageHighMem(page))
290 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100291 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700292 debug_check_no_locks_freed(page_address(page),
293 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100294 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800295
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100296 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +0100297 * If page allocator is not up yet then do not call c_p_a():
298 */
299 if (!debug_pagealloc_enabled)
300 return;
301
302 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100303 * The return value is ignored - the calls cannot fail,
304 * large pages are disabled at boot time:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 */
306 change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100307
308 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100309 * We should perform an IPI and flush all tlbs,
310 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 */
312 __flush_tlb_all();
313}
314#endif