blob: 9cf2fea54eb5085ea44be387b03ac0b07010cab9 [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 Molnar9a3dc782008-01-30 13:33:57 +010064 unsigned long flags;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010065 unsigned long addr;
66 pte_t *pbase, *tmp;
67 struct page *base;
Ingo Molnar7afe15b2008-01-30 13:33:57 +010068 int i, level;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010069
70 base = alloc_pages(GFP_KERNEL, 0);
71 if (!base)
72 return -ENOMEM;
73
Ingo Molnar9a3dc782008-01-30 13:33:57 +010074 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010075 /*
76 * Check for races, another CPU might have split this page
77 * up for us already:
78 */
79 tmp = lookup_address(address, &level);
Ingo Molnar5508a742008-01-30 13:33:56 +010080 if (tmp != kpte) {
81 WARN_ON_ONCE(1);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010082 goto out_unlock;
Ingo Molnar5508a742008-01-30 13:33:56 +010083 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010084
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 */
Ingo Molnar9a3dc782008-01-30 13:33:57 +010096 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +010097 base = NULL;
98
99out_unlock:
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100100 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100101
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{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 struct page *kpte_page;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100111 unsigned long address;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100112 int level, err = 0;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100113 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 BUG_ON(PageHighMem(page));
116 address = (unsigned long)page_address(page);
117
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100118repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100119 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (!kpte)
121 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200124 BUG_ON(PageLRU(kpte_page));
125 BUG_ON(PageCompound(kpte_page));
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 /*
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100128 * Better fail early if someone sets the kernel text to NX.
129 * Does not cover __inittext
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 */
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100131 BUG_ON(address >= (unsigned long)&_text &&
132 address < (unsigned long)&_etext &&
133 (pgprot_val(prot) & _PAGE_NX));
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200134
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100135 if (level == 3) {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100136 set_pte_atomic(kpte, mk_pte(page, canon_pgprot(prot)));
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100137 } else {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100138 err = split_large_page(kpte, address);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100139 if (!err)
140 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100142 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100143}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*
146 * Change the page attributes of an page in the linear mapping.
147 *
148 * This should be used when a page is mapped with a different caching policy
149 * than write-back somewhere - some CPUs do not like it when mappings with
150 * different caching policies exist. This changes the page attributes of the
151 * in kernel linear mapping too.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100152 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * The caller needs to ensure that there are no conflicting mappings elsewhere.
154 * This function only deals with the kernel linear map.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100155 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * Caller must call global_flush_tlb() after this.
157 */
158int change_page_attr(struct page *page, int numpages, pgprot_t prot)
159{
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100160 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100162 for (i = 0; i < numpages; i++, page++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 err = __change_page_attr(page, prot);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100164 if (err)
165 break;
166 }
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return err;
169}
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100170EXPORT_SYMBOL(change_page_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100172int change_page_attr_addr(unsigned long addr, int numpages, pgprot_t prot)
173{
174 int i;
Ingo Molnar5508a742008-01-30 13:33:56 +0100175 unsigned long pfn = (__pa(addr) >> PAGE_SHIFT);
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100176
177 for (i = 0; i < numpages; i++) {
178 if (!pfn_valid(pfn + i)) {
Ingo Molnar5508a742008-01-30 13:33:56 +0100179 WARN_ON_ONCE(1);
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100180 break;
181 } else {
182 int level;
183 pte_t *pte = lookup_address(addr + i*PAGE_SIZE, &level);
Ingo Molnar5508a742008-01-30 13:33:56 +0100184 BUG_ON(pte && pte_none(*pte));
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100185 }
186 }
Ingo Molnar5508a742008-01-30 13:33:56 +0100187
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100188 return change_page_attr(virt_to_page(addr), i, prot);
189}
190
191static void flush_kernel_map(void *arg)
192{
193 /*
194 * Flush all to work around Errata in early athlons regarding
195 * large page flushing.
196 */
197 __flush_tlb_all();
198
199 if (boot_cpu_data.x86_model >= 4)
200 wbinvd();
201}
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203void global_flush_tlb(void)
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700204{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 BUG_ON(irqs_disabled());
206
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100207 on_each_cpu(flush_kernel_map, NULL, 1, 1);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700208}
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100209EXPORT_SYMBOL(global_flush_tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211#ifdef CONFIG_DEBUG_PAGEALLOC
212void kernel_map_pages(struct page *page, int numpages, int enable)
213{
214 if (PageHighMem(page))
215 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100216 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700217 debug_check_no_locks_freed(page_address(page),
218 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100219 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800220
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100221 /*
222 * the return value is ignored - the calls cannot fail,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * large pages are disabled at boot time.
224 */
225 change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100226
227 /*
228 * we should perform an IPI and flush all tlbs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * but that can deadlock->flush only current cpu.
230 */
231 __flush_tlb_all();
232}
233#endif