blob: 570a37bf1401bbdd78ced91e3286c128e222218a [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
40static struct page *
41split_large_page(unsigned long address, pgprot_t prot, pgprot_t ref_prot)
42{
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 unsigned long addr;
44 struct page *base;
45 pte_t *pbase;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010046 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 base = alloc_pages(GFP_KERNEL, 0);
Ingo Molnar9f4c8152008-01-30 13:33:41 +010049 if (!base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 return NULL;
51
Nick Piggin84d1c052006-03-22 00:08:31 -080052 /*
53 * page_private is used to track the number of entries in
54 * the page table page that have non standard attributes.
55 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 address = __pa(address);
Ingo Molnar9f4c8152008-01-30 13:33:41 +010057 addr = address & LARGE_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 pbase = (pte_t *)page_address(base);
Jeremy Fitzhardingefdb4c332007-07-17 18:37:03 -070059 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
Ingo Molnar9f4c8152008-01-30 13:33:41 +010060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE) {
Ingo Molnar9f4c8152008-01-30 13:33:41 +010062 set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT,
63 addr == address ? prot : ref_prot));
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
65 return base;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010066}
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Ingo Molnar9f4c8152008-01-30 13:33:41 +010068static void set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
69{
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 unsigned long flags;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010071 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Ingo Molnar9f4c8152008-01-30 13:33:41 +010073 /* change init_mm */
74 set_pte_atomic(kpte, pte);
Jeremy Fitzhardinge5311ab62007-05-02 19:27:13 +020075 if (SHARED_KERNEL_PMD)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return;
77
78 spin_lock_irqsave(&pgd_lock, flags);
79 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 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 }
89 spin_unlock_irqrestore(&pgd_lock, flags);
90}
91
Ingo Molnar9f4c8152008-01-30 13:33:41 +010092static int __change_page_attr(struct page *page, pgprot_t prot)
93{
Ingo Molnar78c94ab2008-01-30 13:33:55 +010094 pgprot_t ref_prot = PAGE_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 struct page *kpte_page;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010096 unsigned long address;
Ingo Molnar78c94ab2008-01-30 13:33:55 +010097 pgprot_t oldprot;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010098 pte_t *kpte;
Ingo Molnarf0646e42008-01-30 13:33:43 +010099 int level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 BUG_ON(PageHighMem(page));
102 address = (unsigned long)page_address(page);
103
Ingo Molnarf0646e42008-01-30 13:33:43 +0100104 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 if (!kpte)
106 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100107
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100108 oldprot = pte_pgprot(*kpte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200110 BUG_ON(PageLRU(kpte_page));
111 BUG_ON(PageCompound(kpte_page));
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 /*
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100114 * Better fail early if someone sets the kernel text to NX.
115 * Does not cover __inittext
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100117 BUG_ON(address >= (unsigned long)&_text &&
118 address < (unsigned long)&_etext &&
119 (pgprot_val(prot) & _PAGE_NX));
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200120
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100121 if ((address & LARGE_PAGE_MASK) < (unsigned long)&_etext)
122 ref_prot = PAGE_KERNEL_EXEC;
123
124 ref_prot = canon_pgprot(ref_prot);
125 prot = canon_pgprot(prot);
126
127 if (level == 3) {
128 set_pte_atomic(kpte, mk_pte(page, prot));
129 } else {
130 struct page *split;
131 split = split_large_page(address, prot, ref_prot);
132 if (!split)
133 return -ENOMEM;
134
135 /*
136 * There's a small window here to waste a bit of RAM:
137 */
138 set_pmd_pte(kpte, address, mk_pte(split, ref_prot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
140 return 0;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100141}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/*
144 * Change the page attributes of an page in the linear mapping.
145 *
146 * This should be used when a page is mapped with a different caching policy
147 * than write-back somewhere - some CPUs do not like it when mappings with
148 * different caching policies exist. This changes the page attributes of the
149 * in kernel linear mapping too.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100150 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 * The caller needs to ensure that there are no conflicting mappings elsewhere.
152 * This function only deals with the kernel linear map.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100153 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * Caller must call global_flush_tlb() after this.
155 */
156int change_page_attr(struct page *page, int numpages, pgprot_t prot)
157{
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100158 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100160 for (i = 0; i < numpages; i++, page++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 err = __change_page_attr(page, prot);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100162 if (err)
163 break;
164 }
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return err;
167}
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100168EXPORT_SYMBOL(change_page_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100170int change_page_attr_addr(unsigned long addr, int numpages, pgprot_t prot)
171{
172 int i;
173 unsigned long pfn = (addr >> PAGE_SHIFT);
174
175 for (i = 0; i < numpages; i++) {
176 if (!pfn_valid(pfn + i)) {
177 break;
178 } else {
179 int level;
180 pte_t *pte = lookup_address(addr + i*PAGE_SIZE, &level);
181 BUG_ON(pte && !pte_none(*pte));
182 }
183 }
184 return change_page_attr(virt_to_page(addr), i, prot);
185}
186
187static void flush_kernel_map(void *arg)
188{
189 /*
190 * Flush all to work around Errata in early athlons regarding
191 * large page flushing.
192 */
193 __flush_tlb_all();
194
195 if (boot_cpu_data.x86_model >= 4)
196 wbinvd();
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199void global_flush_tlb(void)
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700200{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 BUG_ON(irqs_disabled());
202
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100203 on_each_cpu(flush_kernel_map, NULL, 1, 1);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700204}
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100205EXPORT_SYMBOL(global_flush_tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207#ifdef CONFIG_DEBUG_PAGEALLOC
208void kernel_map_pages(struct page *page, int numpages, int enable)
209{
210 if (PageHighMem(page))
211 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100212 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700213 debug_check_no_locks_freed(page_address(page),
214 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100215 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800216
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100217 /*
218 * the return value is ignored - the calls cannot fail,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * large pages are disabled at boot time.
220 */
221 change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100222
223 /*
224 * we should perform an IPI and flush all tlbs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 * but that can deadlock->flush only current cpu.
226 */
227 __flush_tlb_all();
228}
229#endif