blob: effcd78d5f4039761bd3598f197031e2940640a4 [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 Molnar687c4822008-01-30 13:34:04 +010025/*
26 * We allow the BIOS range to be executable:
27 */
28#define BIOS_BEGIN 0x000a0000
29#define BIOS_END 0x00100000
30
31static inline pgprot_t check_exec(pgprot_t prot, unsigned long address)
32{
33 if (__pa(address) >= BIOS_BEGIN && __pa(address) < BIOS_END)
34 pgprot_val(prot) &= ~_PAGE_NX;
35 /*
36 * Better fail early if someone sets the kernel text to NX.
37 * Does not cover __inittext
38 */
39 BUG_ON(address >= (unsigned long)&_text &&
40 address < (unsigned long)&_etext &&
41 (pgprot_val(prot) & _PAGE_NX));
42
43 return prot;
44}
45
Ingo Molnarf0646e42008-01-30 13:33:43 +010046pte_t *lookup_address(unsigned long address, int *level)
Ingo Molnar9f4c8152008-01-30 13:33:41 +010047{
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 pgd_t *pgd = pgd_offset_k(address);
49 pud_t *pud;
50 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010051
Thomas Gleixner30551bb2008-01-30 13:34:04 +010052 *level = PG_LEVEL_NONE;
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (pgd_none(*pgd))
55 return NULL;
56 pud = pud_offset(pgd, address);
57 if (pud_none(*pud))
58 return NULL;
59 pmd = pmd_offset(pud, address);
60 if (pmd_none(*pmd))
61 return NULL;
Thomas Gleixner30551bb2008-01-30 13:34:04 +010062
63 *level = PG_LEVEL_2M;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (pmd_large(*pmd))
65 return (pte_t *)pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Thomas Gleixner30551bb2008-01-30 13:34:04 +010067 *level = PG_LEVEL_4K;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010068 return pte_offset_kernel(pmd, address);
69}
70
Ingo Molnar9a3dc782008-01-30 13:33:57 +010071static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnar9f4c8152008-01-30 13:33:41 +010072{
Ingo Molnar9f4c8152008-01-30 13:33:41 +010073 /* change init_mm */
74 set_pte_atomic(kpte, pte);
Ingo Molnar44af6c42008-01-30 13:34:03 +010075#ifdef CONFIG_X86_32
Ingo Molnare4b71dc2008-01-30 13:34:04 +010076 if (!SHARED_KERNEL_PMD) {
Ingo Molnar44af6c42008-01-30 13:34:03 +010077 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Ingo Molnar44af6c42008-01-30 13:34:03 +010079 for (page = pgd_list; page; page = (struct page *)page->index) {
80 pgd_t *pgd;
81 pud_t *pud;
82 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010083
Ingo Molnar44af6c42008-01-30 13:34:03 +010084 pgd = (pgd_t *)page_address(page) + pgd_index(address);
85 pud = pud_offset(pgd, address);
86 pmd = pmd_offset(pud, address);
87 set_pte_atomic((pte_t *)pmd, pte);
88 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
Ingo Molnar44af6c42008-01-30 13:34:03 +010090#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Ingo Molnar7afe15b2008-01-30 13:33:57 +010093static int split_large_page(pte_t *kpte, unsigned long address)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010094{
Ingo Molnar7afe15b2008-01-30 13:33:57 +010095 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnar12d6f212008-01-30 13:33:58 +010096 gfp_t gfp_flags = GFP_KERNEL;
Ingo Molnar9a3dc782008-01-30 13:33:57 +010097 unsigned long flags;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010098 unsigned long addr;
99 pte_t *pbase, *tmp;
100 struct page *base;
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100101 int i, level;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100102
Ingo Molnar12d6f212008-01-30 13:33:58 +0100103#ifdef CONFIG_DEBUG_PAGEALLOC
104 gfp_flags = GFP_ATOMIC;
105#endif
106 base = alloc_pages(gfp_flags, 0);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100107 if (!base)
108 return -ENOMEM;
109
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100110 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100111 /*
112 * Check for races, another CPU might have split this page
113 * up for us already:
114 */
115 tmp = lookup_address(address, &level);
Ingo Molnar5508a742008-01-30 13:33:56 +0100116 if (tmp != kpte) {
117 WARN_ON_ONCE(1);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100118 goto out_unlock;
Ingo Molnar5508a742008-01-30 13:33:56 +0100119 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100120
121 address = __pa(address);
122 addr = address & LARGE_PAGE_MASK;
123 pbase = (pte_t *)page_address(base);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100124#ifdef CONFIG_X86_32
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100125 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
Ingo Molnar44af6c42008-01-30 13:34:03 +0100126#endif
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100127
128 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
129 set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
130
131 /*
132 * Install the new, split up pagetable:
133 */
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100134 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100135 base = NULL;
136
137out_unlock:
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100138 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100139
140 if (base)
141 __free_pages(base, 0);
142
143 return 0;
144}
145
Ingo Molnar44af6c42008-01-30 13:34:03 +0100146static int
147__change_page_attr(unsigned long address, struct page *page, pgprot_t prot)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100148{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 struct page *kpte_page;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100150 int level, err = 0;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100151 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 BUG_ON(PageHighMem(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100155repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100156 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (!kpte)
158 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200161 BUG_ON(PageLRU(kpte_page));
162 BUG_ON(PageCompound(kpte_page));
163
Ingo Molnar687c4822008-01-30 13:34:04 +0100164 prot = check_exec(prot, address);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200165
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100166 if (level == PG_LEVEL_4K) {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100167 set_pte_atomic(kpte, mk_pte(page, canon_pgprot(prot)));
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100168 } else {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100169 err = split_large_page(kpte, address);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100170 if (!err)
171 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100173 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100174}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Ingo Molnar44af6c42008-01-30 13:34:03 +0100176/**
177 * change_page_attr_addr - Change page table attributes in linear mapping
178 * @address: Virtual address in linear mapping.
179 * @numpages: Number of pages to change
180 * @prot: New page table attribute (PAGE_*)
181 *
182 * Change page attributes of a page in the direct mapping. This is a variant
183 * of change_page_attr() that also works on memory holes that do not have
184 * mem_map entry (pfn_valid() is false).
185 *
186 * See change_page_attr() documentation for more details.
187 */
188
189int change_page_attr_addr(unsigned long address, int numpages, pgprot_t prot)
190{
191 int err = 0, kernel_map = 0, i;
192
193#ifdef CONFIG_X86_64
194 if (address >= __START_KERNEL_map &&
195 address < __START_KERNEL_map + KERNEL_TEXT_SIZE) {
196
197 address = (unsigned long)__va(__pa(address));
198 kernel_map = 1;
199 }
200#endif
201
202 for (i = 0; i < numpages; i++, address += PAGE_SIZE) {
203 unsigned long pfn = __pa(address) >> PAGE_SHIFT;
204
205 if (!kernel_map || pte_present(pfn_pte(0, prot))) {
206 err = __change_page_attr(address, pfn_to_page(pfn), prot);
207 if (err)
208 break;
209 }
210#ifdef CONFIG_X86_64
211 /*
212 * Handle kernel mapping too which aliases part of
213 * lowmem:
214 */
215 if (__pa(address) < KERNEL_TEXT_SIZE) {
216 unsigned long addr2;
217 pgprot_t prot2;
218
219 addr2 = __START_KERNEL_map + __pa(address);
220 /* Make sure the kernel mappings stay executable */
221 prot2 = pte_pgprot(pte_mkexec(pfn_pte(0, prot)));
222 err = __change_page_attr(addr2, pfn_to_page(pfn), prot2);
223 }
224#endif
225 }
226
227 return err;
228}
229
230/**
231 * change_page_attr - Change page table attributes in the linear mapping.
232 * @page: First page to change
233 * @numpages: Number of pages to change
234 * @prot: New protection/caching type (PAGE_*)
235 *
236 * Returns 0 on success, otherwise a negated errno.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 *
238 * This should be used when a page is mapped with a different caching policy
239 * than write-back somewhere - some CPUs do not like it when mappings with
240 * different caching policies exist. This changes the page attributes of the
241 * in kernel linear mapping too.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100242 *
Ingo Molnar44af6c42008-01-30 13:34:03 +0100243 * Caller must call global_flush_tlb() later to make the changes active.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100244 *
Ingo Molnar44af6c42008-01-30 13:34:03 +0100245 * The caller needs to ensure that there are no conflicting mappings elsewhere
246 * (e.g. in user space) * This function only deals with the kernel linear map.
247 *
248 * For MMIO areas without mem_map use change_page_attr_addr() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 */
250int change_page_attr(struct page *page, int numpages, pgprot_t prot)
251{
Ingo Molnar44af6c42008-01-30 13:34:03 +0100252 unsigned long addr = (unsigned long)page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Ingo Molnar44af6c42008-01-30 13:34:03 +0100254 return change_page_attr_addr(addr, numpages, prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100256EXPORT_SYMBOL(change_page_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100258static void flush_kernel_map(void *arg)
259{
260 /*
261 * Flush all to work around Errata in early athlons regarding
262 * large page flushing.
263 */
264 __flush_tlb_all();
265
266 if (boot_cpu_data.x86_model >= 4)
267 wbinvd();
268}
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270void global_flush_tlb(void)
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700271{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 BUG_ON(irqs_disabled());
273
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100274 on_each_cpu(flush_kernel_map, NULL, 1, 1);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700275}
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100276EXPORT_SYMBOL(global_flush_tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278#ifdef CONFIG_DEBUG_PAGEALLOC
279void kernel_map_pages(struct page *page, int numpages, int enable)
280{
281 if (PageHighMem(page))
282 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100283 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700284 debug_check_no_locks_freed(page_address(page),
285 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100286 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800287
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100288 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +0100289 * If page allocator is not up yet then do not call c_p_a():
290 */
291 if (!debug_pagealloc_enabled)
292 return;
293
294 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100295 * The return value is ignored - the calls cannot fail,
296 * large pages are disabled at boot time:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 */
298 change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100299
300 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100301 * We should perform an IPI and flush all tlbs,
302 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 */
304 __flush_tlb_all();
305}
306#endif