blob: 4208571334dbdded11fe00c25fa4a80011d5b1f7 [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
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 Molnar687c4822008-01-30 13:34:04 +010018/*
Arjan van de Vened724be2008-01-30 13:34:04 +010019 * We must allow the BIOS range to be executable:
Ingo Molnar687c4822008-01-30 13:34:04 +010020 */
21#define BIOS_BEGIN 0x000a0000
22#define BIOS_END 0x00100000
23
Arjan van de Vened724be2008-01-30 13:34:04 +010024static inline int
25within(unsigned long addr, unsigned long start, unsigned long end)
Ingo Molnar687c4822008-01-30 13:34:04 +010026{
Arjan van de Vened724be2008-01-30 13:34:04 +010027 return addr >= start && addr < end;
28}
29
30/*
31 * Certain areas of memory on x86 require very specific protection flags,
32 * for example the BIOS area or kernel text. Callers don't always get this
33 * right (again, ioremap() on BIOS memory is not uncommon) so this function
34 * checks and fixes these known static required protection bits.
35 */
36static inline pgprot_t static_protections(pgprot_t prot, unsigned long address)
37{
38 pgprot_t forbidden = __pgprot(0);
39
Ingo Molnar687c4822008-01-30 13:34:04 +010040 /*
Arjan van de Vened724be2008-01-30 13:34:04 +010041 * The BIOS area between 640k and 1Mb needs to be executable for
42 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
Ingo Molnar687c4822008-01-30 13:34:04 +010043 */
Arjan van de Vened724be2008-01-30 13:34:04 +010044 if (within(__pa(address), BIOS_BEGIN, BIOS_END))
45 pgprot_val(forbidden) |= _PAGE_NX;
46
47 /*
48 * The kernel text needs to be executable for obvious reasons
49 * Does not cover __inittext since that is gone later on
50 */
51 if (within(address, (unsigned long)_text, (unsigned long)_etext))
52 pgprot_val(forbidden) |= _PAGE_NX;
53
54#ifdef CONFIG_DEBUG_RODATA
55 /* The .rodata section needs to be read-only */
56 if (within(address, (unsigned long)__start_rodata,
57 (unsigned long)__end_rodata))
58 pgprot_val(forbidden) |= _PAGE_RW;
59#endif
60
61 prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
Ingo Molnar687c4822008-01-30 13:34:04 +010062
63 return prot;
64}
65
Ingo Molnarf0646e42008-01-30 13:33:43 +010066pte_t *lookup_address(unsigned long address, int *level)
Ingo Molnar9f4c8152008-01-30 13:33:41 +010067{
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 pgd_t *pgd = pgd_offset_k(address);
69 pud_t *pud;
70 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010071
Thomas Gleixner30551bb2008-01-30 13:34:04 +010072 *level = PG_LEVEL_NONE;
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (pgd_none(*pgd))
75 return NULL;
76 pud = pud_offset(pgd, address);
77 if (pud_none(*pud))
78 return NULL;
79 pmd = pmd_offset(pud, address);
80 if (pmd_none(*pmd))
81 return NULL;
Thomas Gleixner30551bb2008-01-30 13:34:04 +010082
83 *level = PG_LEVEL_2M;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (pmd_large(*pmd))
85 return (pte_t *)pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Thomas Gleixner30551bb2008-01-30 13:34:04 +010087 *level = PG_LEVEL_4K;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010088 return pte_offset_kernel(pmd, address);
89}
90
Ingo Molnar9a3dc782008-01-30 13:33:57 +010091static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnar9f4c8152008-01-30 13:33:41 +010092{
Ingo Molnar9f4c8152008-01-30 13:33:41 +010093 /* change init_mm */
94 set_pte_atomic(kpte, pte);
Ingo Molnar44af6c42008-01-30 13:34:03 +010095#ifdef CONFIG_X86_32
Ingo Molnare4b71dc2008-01-30 13:34:04 +010096 if (!SHARED_KERNEL_PMD) {
Ingo Molnar44af6c42008-01-30 13:34:03 +010097 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Ingo Molnar44af6c42008-01-30 13:34:03 +010099 for (page = pgd_list; page; page = (struct page *)page->index) {
100 pgd_t *pgd;
101 pud_t *pud;
102 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100103
Ingo Molnar44af6c42008-01-30 13:34:03 +0100104 pgd = (pgd_t *)page_address(page) + pgd_index(address);
105 pud = pud_offset(pgd, address);
106 pmd = pmd_offset(pud, address);
107 set_pte_atomic((pte_t *)pmd, pte);
108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
Ingo Molnar44af6c42008-01-30 13:34:03 +0100110#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100113static int split_large_page(pte_t *kpte, unsigned long address)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100114{
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100115 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnar12d6f212008-01-30 13:33:58 +0100116 gfp_t gfp_flags = GFP_KERNEL;
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100117 unsigned long flags;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100118 unsigned long addr;
119 pte_t *pbase, *tmp;
120 struct page *base;
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100121 int i, level;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100122
Ingo Molnar12d6f212008-01-30 13:33:58 +0100123#ifdef CONFIG_DEBUG_PAGEALLOC
124 gfp_flags = GFP_ATOMIC;
125#endif
126 base = alloc_pages(gfp_flags, 0);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100127 if (!base)
128 return -ENOMEM;
129
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100130 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100131 /*
132 * Check for races, another CPU might have split this page
133 * up for us already:
134 */
135 tmp = lookup_address(address, &level);
Ingo Molnar5508a742008-01-30 13:33:56 +0100136 if (tmp != kpte) {
137 WARN_ON_ONCE(1);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100138 goto out_unlock;
Ingo Molnar5508a742008-01-30 13:33:56 +0100139 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100140
141 address = __pa(address);
142 addr = address & LARGE_PAGE_MASK;
143 pbase = (pte_t *)page_address(base);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100144#ifdef CONFIG_X86_32
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100145 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
Ingo Molnar44af6c42008-01-30 13:34:03 +0100146#endif
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100147
148 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
149 set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
150
151 /*
Huang, Ying4c881ca2008-01-30 13:34:04 +0100152 * Install the new, split up pagetable. Important detail here:
153 *
154 * On Intel the NX bit of all levels must be cleared to make a
155 * page executable. See section 4.13.2 of Intel 64 and IA-32
156 * Architectures Software Developer's Manual).
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100157 */
Huang, Ying4c881ca2008-01-30 13:34:04 +0100158 ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100159 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100160 base = NULL;
161
162out_unlock:
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100163 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100164
165 if (base)
166 __free_pages(base, 0);
167
168 return 0;
169}
170
Ingo Molnar44af6c42008-01-30 13:34:03 +0100171static int
Ingo Molnar81922062008-01-30 13:34:04 +0100172__change_page_attr(unsigned long address, unsigned long pfn, pgprot_t prot)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100173{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 struct page *kpte_page;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100175 int level, err = 0;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100176 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Ingo Molnar81922062008-01-30 13:34:04 +0100178#ifdef CONFIG_X86_32
179 BUG_ON(pfn > max_low_pfn);
180#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100182repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100183 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (!kpte)
185 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200188 BUG_ON(PageLRU(kpte_page));
189 BUG_ON(PageCompound(kpte_page));
190
Arjan van de Vened724be2008-01-30 13:34:04 +0100191 prot = static_protections(prot, address);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200192
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100193 if (level == PG_LEVEL_4K) {
Ingo Molnar81922062008-01-30 13:34:04 +0100194 set_pte_atomic(kpte, pfn_pte(pfn, canon_pgprot(prot)));
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100195 } else {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100196 err = split_large_page(kpte, address);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100197 if (!err)
198 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100200 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100201}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Ingo Molnar44af6c42008-01-30 13:34:03 +0100203/**
204 * change_page_attr_addr - Change page table attributes in linear mapping
205 * @address: Virtual address in linear mapping.
206 * @numpages: Number of pages to change
207 * @prot: New page table attribute (PAGE_*)
208 *
209 * Change page attributes of a page in the direct mapping. This is a variant
210 * of change_page_attr() that also works on memory holes that do not have
211 * mem_map entry (pfn_valid() is false).
212 *
213 * See change_page_attr() documentation for more details.
214 */
215
216int change_page_attr_addr(unsigned long address, int numpages, pgprot_t prot)
217{
218 int err = 0, kernel_map = 0, i;
219
220#ifdef CONFIG_X86_64
221 if (address >= __START_KERNEL_map &&
222 address < __START_KERNEL_map + KERNEL_TEXT_SIZE) {
223
224 address = (unsigned long)__va(__pa(address));
225 kernel_map = 1;
226 }
227#endif
228
229 for (i = 0; i < numpages; i++, address += PAGE_SIZE) {
230 unsigned long pfn = __pa(address) >> PAGE_SHIFT;
231
232 if (!kernel_map || pte_present(pfn_pte(0, prot))) {
Ingo Molnar81922062008-01-30 13:34:04 +0100233 err = __change_page_attr(address, pfn, prot);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100234 if (err)
235 break;
236 }
237#ifdef CONFIG_X86_64
238 /*
239 * Handle kernel mapping too which aliases part of
240 * lowmem:
241 */
242 if (__pa(address) < KERNEL_TEXT_SIZE) {
243 unsigned long addr2;
244 pgprot_t prot2;
245
246 addr2 = __START_KERNEL_map + __pa(address);
247 /* Make sure the kernel mappings stay executable */
248 prot2 = pte_pgprot(pte_mkexec(pfn_pte(0, prot)));
Ingo Molnar81922062008-01-30 13:34:04 +0100249 err = __change_page_attr(addr2, pfn, prot2);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100250 }
251#endif
252 }
253
254 return err;
255}
256
257/**
258 * change_page_attr - Change page table attributes in the linear mapping.
259 * @page: First page to change
260 * @numpages: Number of pages to change
261 * @prot: New protection/caching type (PAGE_*)
262 *
263 * Returns 0 on success, otherwise a negated errno.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 *
265 * This should be used when a page is mapped with a different caching policy
266 * than write-back somewhere - some CPUs do not like it when mappings with
267 * different caching policies exist. This changes the page attributes of the
268 * in kernel linear mapping too.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100269 *
Ingo Molnar44af6c42008-01-30 13:34:03 +0100270 * Caller must call global_flush_tlb() later to make the changes active.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100271 *
Ingo Molnar44af6c42008-01-30 13:34:03 +0100272 * The caller needs to ensure that there are no conflicting mappings elsewhere
273 * (e.g. in user space) * This function only deals with the kernel linear map.
274 *
275 * For MMIO areas without mem_map use change_page_attr_addr() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 */
277int change_page_attr(struct page *page, int numpages, pgprot_t prot)
278{
Ingo Molnar44af6c42008-01-30 13:34:03 +0100279 unsigned long addr = (unsigned long)page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Ingo Molnar44af6c42008-01-30 13:34:03 +0100281 return change_page_attr_addr(addr, numpages, prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100283EXPORT_SYMBOL(change_page_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Ingo Molnare81d5dc2008-01-30 13:34:06 +0100285void clflush_cache_range(void *addr, int size)
286{
287 int i;
288
289 for (i = 0; i < size; i += boot_cpu_data.x86_clflush_size)
290 clflush(addr+i);
291}
292
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100293static void flush_kernel_map(void *arg)
294{
295 /*
296 * Flush all to work around Errata in early athlons regarding
297 * large page flushing.
298 */
299 __flush_tlb_all();
300
301 if (boot_cpu_data.x86_model >= 4)
302 wbinvd();
303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305void global_flush_tlb(void)
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700306{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 BUG_ON(irqs_disabled());
308
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100309 on_each_cpu(flush_kernel_map, NULL, 1, 1);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700310}
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100311EXPORT_SYMBOL(global_flush_tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313#ifdef CONFIG_DEBUG_PAGEALLOC
314void kernel_map_pages(struct page *page, int numpages, int enable)
315{
316 if (PageHighMem(page))
317 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100318 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700319 debug_check_no_locks_freed(page_address(page),
320 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100321 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800322
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100323 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +0100324 * If page allocator is not up yet then do not call c_p_a():
325 */
326 if (!debug_pagealloc_enabled)
327 return;
328
329 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100330 * The return value is ignored - the calls cannot fail,
331 * large pages are disabled at boot time:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 */
333 change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100334
335 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100336 * We should perform an IPI and flush all tlbs,
337 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
339 __flush_tlb_all();
340}
341#endif