blob: dd49b16b3a0ec08941990c75c61c367064110e01 [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 Molnarf0646e42008-01-30 13:33:43 +010032 *level = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 if (pmd_large(*pmd))
34 return (pte_t *)pmd;
Ingo Molnarf0646e42008-01-30 13:33:43 +010035 *level = 3;
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 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Ingo Molnar9f4c8152008-01-30 13:33:41 +010044 /* change init_mm */
45 set_pte_atomic(kpte, pte);
Jeremy Fitzhardinge5311ab62007-05-02 19:27:13 +020046 if (SHARED_KERNEL_PMD)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 return;
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 for (page = pgd_list; page; page = (struct page *)page->index) {
50 pgd_t *pgd;
51 pud_t *pud;
52 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 pgd = (pgd_t *)page_address(page) + pgd_index(address);
55 pud = pud_offset(pgd, address);
56 pmd = pmd_offset(pud, address);
57 set_pte_atomic((pte_t *)pmd, pte);
58 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Ingo Molnar7afe15b2008-01-30 13:33:57 +010061static int split_large_page(pte_t *kpte, unsigned long address)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010062{
Ingo Molnar7afe15b2008-01-30 13:33:57 +010063 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnar12d6f212008-01-30 13:33:58 +010064 gfp_t gfp_flags = GFP_KERNEL;
Ingo Molnar9a3dc782008-01-30 13:33:57 +010065 unsigned long flags;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010066 unsigned long addr;
67 pte_t *pbase, *tmp;
68 struct page *base;
Ingo Molnar7afe15b2008-01-30 13:33:57 +010069 int i, level;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010070
Ingo Molnar12d6f212008-01-30 13:33:58 +010071#ifdef CONFIG_DEBUG_PAGEALLOC
72 gfp_flags = GFP_ATOMIC;
73#endif
74 base = alloc_pages(gfp_flags, 0);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010075 if (!base)
76 return -ENOMEM;
77
Ingo Molnar9a3dc782008-01-30 13:33:57 +010078 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010079 /*
80 * Check for races, another CPU might have split this page
81 * up for us already:
82 */
83 tmp = lookup_address(address, &level);
Ingo Molnar5508a7482008-01-30 13:33:56 +010084 if (tmp != kpte) {
85 WARN_ON_ONCE(1);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010086 goto out_unlock;
Ingo Molnar5508a7482008-01-30 13:33:56 +010087 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010088
89 address = __pa(address);
90 addr = address & LARGE_PAGE_MASK;
91 pbase = (pte_t *)page_address(base);
92 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
93
94 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
95 set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
96
97 /*
98 * Install the new, split up pagetable:
99 */
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100100 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100101 base = NULL;
102
103out_unlock:
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100104 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100105
106 if (base)
107 __free_pages(base, 0);
108
109 return 0;
110}
111
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100112static int __change_page_attr(struct page *page, pgprot_t prot)
113{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 struct page *kpte_page;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100115 unsigned long address;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100116 int level, err = 0;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100117 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 BUG_ON(PageHighMem(page));
120 address = (unsigned long)page_address(page);
121
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100122repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100123 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (!kpte)
125 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200128 BUG_ON(PageLRU(kpte_page));
129 BUG_ON(PageCompound(kpte_page));
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 /*
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100132 * Better fail early if someone sets the kernel text to NX.
133 * Does not cover __inittext
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 */
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100135 BUG_ON(address >= (unsigned long)&_text &&
136 address < (unsigned long)&_etext &&
137 (pgprot_val(prot) & _PAGE_NX));
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200138
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100139 if (level == 3) {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100140 set_pte_atomic(kpte, mk_pte(page, canon_pgprot(prot)));
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100141 } else {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100142 err = split_large_page(kpte, address);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100143 if (!err)
144 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100146 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100147}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/*
150 * Change the page attributes of an page in the linear mapping.
151 *
152 * This should be used when a page is mapped with a different caching policy
153 * than write-back somewhere - some CPUs do not like it when mappings with
154 * different caching policies exist. This changes the page attributes of the
155 * in kernel linear mapping too.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100156 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * The caller needs to ensure that there are no conflicting mappings elsewhere.
158 * This function only deals with the kernel linear map.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100159 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 * Caller must call global_flush_tlb() after this.
161 */
162int change_page_attr(struct page *page, int numpages, pgprot_t prot)
163{
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100164 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100166 for (i = 0; i < numpages; i++, page++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 err = __change_page_attr(page, prot);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100168 if (err)
169 break;
170 }
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return err;
173}
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100174EXPORT_SYMBOL(change_page_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100176int change_page_attr_addr(unsigned long addr, int numpages, pgprot_t prot)
177{
178 int i;
Ingo Molnar5508a7482008-01-30 13:33:56 +0100179 unsigned long pfn = (__pa(addr) >> PAGE_SHIFT);
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100180
181 for (i = 0; i < numpages; i++) {
182 if (!pfn_valid(pfn + i)) {
Ingo Molnar5508a7482008-01-30 13:33:56 +0100183 WARN_ON_ONCE(1);
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100184 break;
185 } else {
186 int level;
187 pte_t *pte = lookup_address(addr + i*PAGE_SIZE, &level);
Ingo Molnar5508a7482008-01-30 13:33:56 +0100188 BUG_ON(pte && pte_none(*pte));
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100189 }
190 }
Ingo Molnar5508a7482008-01-30 13:33:56 +0100191
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100192 return change_page_attr(virt_to_page(addr), i, prot);
193}
194
195static void flush_kernel_map(void *arg)
196{
197 /*
198 * Flush all to work around Errata in early athlons regarding
199 * large page flushing.
200 */
201 __flush_tlb_all();
202
203 if (boot_cpu_data.x86_model >= 4)
204 wbinvd();
205}
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207void global_flush_tlb(void)
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700208{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 BUG_ON(irqs_disabled());
210
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100211 on_each_cpu(flush_kernel_map, NULL, 1, 1);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700212}
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100213EXPORT_SYMBOL(global_flush_tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215#ifdef CONFIG_DEBUG_PAGEALLOC
216void kernel_map_pages(struct page *page, int numpages, int enable)
217{
218 if (PageHighMem(page))
219 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100220 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700221 debug_check_no_locks_freed(page_address(page),
222 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100223 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800224
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100225 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +0100226 * If page allocator is not up yet then do not call c_p_a():
227 */
228 if (!debug_pagealloc_enabled)
229 return;
230
231 /*
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100232 * the return value is ignored - the calls cannot fail,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 * large pages are disabled at boot time.
234 */
235 change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100236
237 /*
238 * we should perform an IPI and flush all tlbs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * but that can deadlock->flush only current cpu.
240 */
241 __flush_tlb_all();
242}
243#endif