blob: 3e231d762aaa8527d112687c711747f98f4bac83 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2002 Andi Kleen, SuSE Labs.
3 * Thanks to Ben LaHaise for precious feedback.
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/mm.h>
7#include <linux/sched.h>
8#include <linux/highmem.h>
9#include <linux/module.h>
10#include <linux/slab.h>
11#include <asm/uaccess.h>
12#include <asm/processor.h>
13#include <asm/tlbflush.h>
14#include <asm/io.h>
15
16static inline pte_t *lookup_address(unsigned long address)
17{
18 pgd_t *pgd = pgd_offset_k(address);
19 pud_t *pud;
20 pmd_t *pmd;
21 pte_t *pte;
22 if (pgd_none(*pgd))
23 return NULL;
24 pud = pud_offset(pgd, address);
25 if (!pud_present(*pud))
26 return NULL;
27 pmd = pmd_offset(pud, address);
28 if (!pmd_present(*pmd))
29 return NULL;
30 if (pmd_large(*pmd))
31 return (pte_t *)pmd;
32 pte = pte_offset_kernel(pmd, address);
33 if (pte && !pte_present(*pte))
34 pte = NULL;
35 return pte;
36}
37
38static struct page *split_large_page(unsigned long address, pgprot_t prot,
39 pgprot_t ref_prot)
40{
41 int i;
42 unsigned long addr;
43 struct page *base = alloc_pages(GFP_KERNEL, 0);
44 pte_t *pbase;
45 if (!base)
46 return NULL;
Nick Piggin4fa4f532006-03-22 00:08:33 -080047 /*
48 * page_private is used to track the number of entries in
49 * the page table page have non standard attributes.
50 */
51 SetPagePrivate(base);
52 page_private(base) = 0;
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 address = __pa(address);
55 addr = address & LARGE_PAGE_MASK;
56 pbase = (pte_t *)page_address(base);
57 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE) {
58 pbase[i] = pfn_pte(addr >> PAGE_SHIFT,
59 addr == address ? prot : ref_prot);
60 }
61 return base;
62}
63
64
65static void flush_kernel_map(void *address)
66{
67 if (0 && address && cpu_has_clflush) {
68 /* is this worth it? */
69 int i;
70 for (i = 0; i < PAGE_SIZE; i += boot_cpu_data.x86_clflush_size)
71 asm volatile("clflush (%0)" :: "r" (address + i));
72 } else
73 asm volatile("wbinvd":::"memory");
74 if (address)
75 __flush_tlb_one(address);
76 else
77 __flush_tlb_all();
78}
79
80
81static inline void flush_map(unsigned long address)
82{
83 on_each_cpu(flush_kernel_map, (void *)address, 1, 1);
84}
85
Nick Piggin20aaffd2006-03-22 00:08:32 -080086static struct page *deferred_pages; /* protected by init_mm.mmap_sem */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Nick Piggin20aaffd2006-03-22 00:08:32 -080088static inline void save_page(struct page *fpage)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Nick Piggin20aaffd2006-03-22 00:08:32 -080090 fpage->lru.next = (struct list_head *)deferred_pages;
91 deferred_pages = fpage;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
94/*
95 * No more special protections in this 2/4MB area - revert to a
96 * large page again.
97 */
98static void revert_page(unsigned long address, pgprot_t ref_prot)
99{
100 pgd_t *pgd;
101 pud_t *pud;
102 pmd_t *pmd;
103 pte_t large_pte;
104
105 pgd = pgd_offset_k(address);
106 BUG_ON(pgd_none(*pgd));
107 pud = pud_offset(pgd,address);
108 BUG_ON(pud_none(*pud));
109 pmd = pmd_offset(pud, address);
110 BUG_ON(pmd_val(*pmd) & _PAGE_PSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 large_pte = mk_pte_phys(__pa(address) & LARGE_PAGE_MASK, ref_prot);
Andi Kleen5e6b0bf2006-09-26 10:52:37 +0200112 large_pte = pte_mkhuge(large_pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 set_pte((pte_t *)pmd, large_pte);
114}
115
116static int
117__change_page_attr(unsigned long address, unsigned long pfn, pgprot_t prot,
118 pgprot_t ref_prot)
119{
120 pte_t *kpte;
121 struct page *kpte_page;
Arjan van de Venc7282522006-01-06 00:12:03 -0800122 pgprot_t ref_prot2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 kpte = lookup_address(address);
124 if (!kpte) return 0;
125 kpte_page = virt_to_page(((unsigned long)kpte) & PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (pgprot_val(prot) != pgprot_val(ref_prot)) {
Andi Kleen5e6b0bf2006-09-26 10:52:37 +0200127 if (!pte_huge(*kpte)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 set_pte(kpte, pfn_pte(pfn, prot));
129 } else {
130 /*
Nick Piggin4fa4f532006-03-22 00:08:33 -0800131 * split_large_page will take the reference for this
132 * change_page_attr on the split page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
Arjan van de Venc7282522006-01-06 00:12:03 -0800134 struct page *split;
Andi Kleen5e6b0bf2006-09-26 10:52:37 +0200135 ref_prot2 = pte_pgprot(pte_clrhuge(*kpte));
Arjan van de Venc7282522006-01-06 00:12:03 -0800136 split = split_large_page(address, prot, ref_prot2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (!split)
138 return -ENOMEM;
Andi Kleen5e6b0bf2006-09-26 10:52:37 +0200139 set_pte(kpte, mk_pte(split, ref_prot2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 kpte_page = split;
Andi Kleen5e6b0bf2006-09-26 10:52:37 +0200141 }
Nick Piggin4fa4f532006-03-22 00:08:33 -0800142 page_private(kpte_page)++;
Andi Kleen5e6b0bf2006-09-26 10:52:37 +0200143 } else if (!pte_huge(*kpte)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 set_pte(kpte, pfn_pte(pfn, ref_prot));
Nick Piggin4fa4f532006-03-22 00:08:33 -0800145 BUG_ON(page_private(kpte_page) == 0);
146 page_private(kpte_page)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 } else
148 BUG();
149
150 /* on x86-64 the direct mapping set at boot is not using 4k pages */
151 BUG_ON(PageReserved(kpte_page));
152
Nick Piggin4fa4f532006-03-22 00:08:33 -0800153 if (page_private(kpte_page) == 0) {
Nick Piggin20aaffd2006-03-22 00:08:32 -0800154 save_page(kpte_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 revert_page(address, ref_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157 return 0;
158}
159
160/*
161 * Change the page attributes of an page in the linear mapping.
162 *
163 * This should be used when a page is mapped with a different caching policy
164 * than write-back somewhere - some CPUs do not like it when mappings with
165 * different caching policies exist. This changes the page attributes of the
166 * in kernel linear mapping too.
167 *
168 * The caller needs to ensure that there are no conflicting mappings elsewhere.
169 * This function only deals with the kernel linear map.
170 *
171 * Caller must call global_flush_tlb() after this.
172 */
173int change_page_attr_addr(unsigned long address, int numpages, pgprot_t prot)
174{
175 int err = 0;
176 int i;
177
178 down_write(&init_mm.mmap_sem);
179 for (i = 0; i < numpages; i++, address += PAGE_SIZE) {
180 unsigned long pfn = __pa(address) >> PAGE_SHIFT;
181
182 err = __change_page_attr(address, pfn, prot, PAGE_KERNEL);
183 if (err)
184 break;
185 /* Handle kernel mapping too which aliases part of the
186 * lowmem */
187 if (__pa(address) < KERNEL_TEXT_SIZE) {
188 unsigned long addr2;
Andi Kleendf992842006-09-26 10:52:37 +0200189 pgprot_t prot2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 addr2 = __START_KERNEL_map + __pa(address);
Andi Kleendf992842006-09-26 10:52:37 +0200191 /* Make sure the kernel mappings stay executable */
192 prot2 = pte_pgprot(pte_mkexec(pfn_pte(0, prot)));
193 err = __change_page_attr(addr2, pfn, prot2,
194 PAGE_KERNEL_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196 }
197 up_write(&init_mm.mmap_sem);
198 return err;
199}
200
201/* Don't call this for MMIO areas that may not have a mem_map entry */
202int change_page_attr(struct page *page, int numpages, pgprot_t prot)
203{
204 unsigned long addr = (unsigned long)page_address(page);
205 return change_page_attr_addr(addr, numpages, prot);
206}
207
208void global_flush_tlb(void)
209{
Nick Piggin20aaffd2006-03-22 00:08:32 -0800210 struct page *dpage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 down_read(&init_mm.mmap_sem);
Nick Piggin20aaffd2006-03-22 00:08:32 -0800213 dpage = xchg(&deferred_pages, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 up_read(&init_mm.mmap_sem);
Nick Piggin20aaffd2006-03-22 00:08:32 -0800215
216 flush_map((dpage && !dpage->lru.next) ? (unsigned long)page_address(dpage) : 0);
217 while (dpage) {
218 struct page *tmp = dpage;
219 dpage = (struct page *)dpage->lru.next;
Nick Piggin4fa4f532006-03-22 00:08:33 -0800220 ClearPagePrivate(tmp);
Nick Piggin20aaffd2006-03-22 00:08:32 -0800221 __free_page(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223}
224
225EXPORT_SYMBOL(change_page_attr);
226EXPORT_SYMBOL(global_flush_tlb);