blob: 14c923b3b07f272b03a8726216d036ab6768157a [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 Molnar9f4c8152008-01-30 13:33:41 +010040static void set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
41{
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 unsigned long flags;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010043 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Ingo Molnar9f4c8152008-01-30 13:33:41 +010045 /* change init_mm */
46 set_pte_atomic(kpte, pte);
Jeremy Fitzhardinge5311ab62007-05-02 19:27:13 +020047 if (SHARED_KERNEL_PMD)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 return;
49
50 spin_lock_irqsave(&pgd_lock, flags);
51 for (page = pgd_list; page; page = (struct page *)page->index) {
52 pgd_t *pgd;
53 pud_t *pud;
54 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 pgd = (pgd_t *)page_address(page) + pgd_index(address);
57 pud = pud_offset(pgd, address);
58 pmd = pmd_offset(pud, address);
59 set_pte_atomic((pte_t *)pmd, pte);
60 }
61 spin_unlock_irqrestore(&pgd_lock, flags);
62}
63
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010064static int
65split_large_page(pte_t *kpte, unsigned long address, pgprot_t ref_prot)
66{
67 int i, level;
68 unsigned long addr;
69 pte_t *pbase, *tmp;
70 struct page *base;
71
72 base = alloc_pages(GFP_KERNEL, 0);
73 if (!base)
74 return -ENOMEM;
75
76 down_write(&init_mm.mmap_sem);
77 /*
78 * Check for races, another CPU might have split this page
79 * up for us already:
80 */
81 tmp = lookup_address(address, &level);
82 if (tmp != kpte)
83 goto out_unlock;
84
85 address = __pa(address);
86 addr = address & LARGE_PAGE_MASK;
87 pbase = (pte_t *)page_address(base);
88 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
89
90 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
91 set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
92
93 /*
94 * Install the new, split up pagetable:
95 */
96 set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
97 base = NULL;
98
99out_unlock:
100 up_write(&init_mm.mmap_sem);
101
102 if (base)
103 __free_pages(base, 0);
104
105 return 0;
106}
107
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100108static int __change_page_attr(struct page *page, pgprot_t prot)
109{
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100110 pgprot_t ref_prot = PAGE_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 struct page *kpte_page;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100112 unsigned long address;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100113 int level, err = 0;
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100114 pgprot_t oldprot;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100115 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 BUG_ON(PageHighMem(page));
118 address = (unsigned long)page_address(page);
119
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100120repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100121 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (!kpte)
123 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100124
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100125 oldprot = pte_pgprot(*kpte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200127 BUG_ON(PageLRU(kpte_page));
128 BUG_ON(PageCompound(kpte_page));
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 /*
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100131 * Better fail early if someone sets the kernel text to NX.
132 * Does not cover __inittext
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100134 BUG_ON(address >= (unsigned long)&_text &&
135 address < (unsigned long)&_etext &&
136 (pgprot_val(prot) & _PAGE_NX));
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200137
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100138 if ((address & LARGE_PAGE_MASK) < (unsigned long)&_etext)
139 ref_prot = PAGE_KERNEL_EXEC;
140
141 ref_prot = canon_pgprot(ref_prot);
142 prot = canon_pgprot(prot);
143
144 if (level == 3) {
145 set_pte_atomic(kpte, mk_pte(page, prot));
146 } else {
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100147 err = split_large_page(kpte, address, ref_prot);
148 if (!err)
149 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100151 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100152}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
155 * Change the page attributes of an page in the linear mapping.
156 *
157 * This should be used when a page is mapped with a different caching policy
158 * than write-back somewhere - some CPUs do not like it when mappings with
159 * different caching policies exist. This changes the page attributes of the
160 * in kernel linear mapping too.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100161 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * The caller needs to ensure that there are no conflicting mappings elsewhere.
163 * This function only deals with the kernel linear map.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100164 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * Caller must call global_flush_tlb() after this.
166 */
167int change_page_attr(struct page *page, int numpages, pgprot_t prot)
168{
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100169 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100171 for (i = 0; i < numpages; i++, page++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 err = __change_page_attr(page, prot);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100173 if (err)
174 break;
175 }
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return err;
178}
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100179EXPORT_SYMBOL(change_page_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100181int change_page_attr_addr(unsigned long addr, int numpages, pgprot_t prot)
182{
183 int i;
184 unsigned long pfn = (addr >> PAGE_SHIFT);
185
186 for (i = 0; i < numpages; i++) {
187 if (!pfn_valid(pfn + i)) {
188 break;
189 } else {
190 int level;
191 pte_t *pte = lookup_address(addr + i*PAGE_SIZE, &level);
192 BUG_ON(pte && !pte_none(*pte));
193 }
194 }
195 return change_page_attr(virt_to_page(addr), i, prot);
196}
197
198static void flush_kernel_map(void *arg)
199{
200 /*
201 * Flush all to work around Errata in early athlons regarding
202 * large page flushing.
203 */
204 __flush_tlb_all();
205
206 if (boot_cpu_data.x86_model >= 4)
207 wbinvd();
208}
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210void global_flush_tlb(void)
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700211{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 BUG_ON(irqs_disabled());
213
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100214 on_each_cpu(flush_kernel_map, NULL, 1, 1);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700215}
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100216EXPORT_SYMBOL(global_flush_tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218#ifdef CONFIG_DEBUG_PAGEALLOC
219void kernel_map_pages(struct page *page, int numpages, int enable)
220{
221 if (PageHighMem(page))
222 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100223 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700224 debug_check_no_locks_freed(page_address(page),
225 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100226 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800227
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100228 /*
229 * the return value is ignored - the calls cannot fail,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 * large pages are disabled at boot time.
231 */
232 change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100233
234 /*
235 * we should perform an IPI and flush all tlbs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * but that can deadlock->flush only current cpu.
237 */
238 __flush_tlb_all();
239}
240#endif