blob: 0966023dfd70393002130858f3c11c1a1864ca8b [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 Molnar7afe15b2008-01-30 13:33:57 +010064static int split_large_page(pte_t *kpte, unsigned long address)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010065{
Ingo Molnar7afe15b2008-01-30 13:33:57 +010066 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010067 unsigned long addr;
68 pte_t *pbase, *tmp;
69 struct page *base;
Ingo Molnar7afe15b2008-01-30 13:33:57 +010070 int i, level;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010071
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);
Ingo Molnar5508a742008-01-30 13:33:56 +010082 if (tmp != kpte) {
83 WARN_ON_ONCE(1);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010084 goto out_unlock;
Ingo Molnar5508a742008-01-30 13:33:56 +010085 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010086
87 address = __pa(address);
88 addr = address & LARGE_PAGE_MASK;
89 pbase = (pte_t *)page_address(base);
90 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
91
92 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
93 set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
94
95 /*
96 * Install the new, split up pagetable:
97 */
98 set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
99 base = NULL;
100
101out_unlock:
102 up_write(&init_mm.mmap_sem);
103
104 if (base)
105 __free_pages(base, 0);
106
107 return 0;
108}
109
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100110static int __change_page_attr(struct page *page, pgprot_t prot)
111{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 struct page *kpte_page;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100113 unsigned long address;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100114 int level, err = 0;
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200126 BUG_ON(PageLRU(kpte_page));
127 BUG_ON(PageCompound(kpte_page));
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 /*
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100130 * Better fail early if someone sets the kernel text to NX.
131 * Does not cover __inittext
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 */
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100133 BUG_ON(address >= (unsigned long)&_text &&
134 address < (unsigned long)&_etext &&
135 (pgprot_val(prot) & _PAGE_NX));
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200136
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100137 if (level == 3) {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100138 set_pte_atomic(kpte, mk_pte(page, canon_pgprot(prot)));
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100139 } else {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100140 err = split_large_page(kpte, address);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100141 if (!err)
142 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100144 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100145}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/*
148 * Change the page attributes of an page in the linear mapping.
149 *
150 * This should be used when a page is mapped with a different caching policy
151 * than write-back somewhere - some CPUs do not like it when mappings with
152 * different caching policies exist. This changes the page attributes of the
153 * in kernel linear mapping too.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100154 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 * The caller needs to ensure that there are no conflicting mappings elsewhere.
156 * This function only deals with the kernel linear map.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100157 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 * Caller must call global_flush_tlb() after this.
159 */
160int change_page_attr(struct page *page, int numpages, pgprot_t prot)
161{
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100162 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100164 for (i = 0; i < numpages; i++, page++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 err = __change_page_attr(page, prot);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100166 if (err)
167 break;
168 }
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return err;
171}
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100172EXPORT_SYMBOL(change_page_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100174int change_page_attr_addr(unsigned long addr, int numpages, pgprot_t prot)
175{
176 int i;
Ingo Molnar5508a742008-01-30 13:33:56 +0100177 unsigned long pfn = (__pa(addr) >> PAGE_SHIFT);
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100178
179 for (i = 0; i < numpages; i++) {
180 if (!pfn_valid(pfn + i)) {
Ingo Molnar5508a742008-01-30 13:33:56 +0100181 WARN_ON_ONCE(1);
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100182 break;
183 } else {
184 int level;
185 pte_t *pte = lookup_address(addr + i*PAGE_SIZE, &level);
Ingo Molnar5508a742008-01-30 13:33:56 +0100186 BUG_ON(pte && pte_none(*pte));
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100187 }
188 }
Ingo Molnar5508a742008-01-30 13:33:56 +0100189
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100190 return change_page_attr(virt_to_page(addr), i, prot);
191}
192
193static void flush_kernel_map(void *arg)
194{
195 /*
196 * Flush all to work around Errata in early athlons regarding
197 * large page flushing.
198 */
199 __flush_tlb_all();
200
201 if (boot_cpu_data.x86_model >= 4)
202 wbinvd();
203}
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205void global_flush_tlb(void)
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700206{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 BUG_ON(irqs_disabled());
208
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100209 on_each_cpu(flush_kernel_map, NULL, 1, 1);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700210}
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100211EXPORT_SYMBOL(global_flush_tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213#ifdef CONFIG_DEBUG_PAGEALLOC
214void kernel_map_pages(struct page *page, int numpages, int enable)
215{
216 if (PageHighMem(page))
217 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100218 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700219 debug_check_no_locks_freed(page_address(page),
220 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100221 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800222
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100223 /*
224 * the return value is ignored - the calls cannot fail,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 * large pages are disabled at boot time.
226 */
227 change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100228
229 /*
230 * we should perform an IPI and flush all tlbs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 * but that can deadlock->flush only current cpu.
232 */
233 __flush_tlb_all();
234}
235#endif