Merge branch 'for-linus' of git://one.firstfloor.org/home/andi/git/linux-2.6
[linux-2.6] / arch / x86_64 / mm / pageattr.c
1 /* 
2  * Copyright 2002 Andi Kleen, SuSE Labs. 
3  * Thanks to Ben LaHaise for precious feedback.
4  */ 
5
6 #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
16 static 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
38 static 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;
47         /*
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
54         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
65 static 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
81 static inline void flush_map(unsigned long address)
82 {       
83         on_each_cpu(flush_kernel_map, (void *)address, 1, 1);
84 }
85
86 static struct page *deferred_pages; /* protected by init_mm.mmap_sem */
87
88 static inline void save_page(struct page *fpage)
89 {
90         fpage->lru.next = (struct list_head *)deferred_pages;
91         deferred_pages = fpage;
92 }
93
94 /* 
95  * No more special protections in this 2/4MB area - revert to a
96  * large page again. 
97  */
98 static 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);
111         large_pte = mk_pte_phys(__pa(address) & LARGE_PAGE_MASK, ref_prot);
112         large_pte = pte_mkhuge(large_pte);
113         set_pte((pte_t *)pmd, large_pte);
114 }      
115
116 static 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;
122         pgprot_t ref_prot2;
123         kpte = lookup_address(address);
124         if (!kpte) return 0;
125         kpte_page = virt_to_page(((unsigned long)kpte) & PAGE_MASK);
126         if (pgprot_val(prot) != pgprot_val(ref_prot)) { 
127                 if (!pte_huge(*kpte)) {
128                         set_pte(kpte, pfn_pte(pfn, prot));
129                 } else {
130                         /*
131                          * split_large_page will take the reference for this
132                          * change_page_attr on the split page.
133                          */
134                         struct page *split;
135                         ref_prot2 = pte_pgprot(pte_clrhuge(*kpte));
136                         split = split_large_page(address, prot, ref_prot2);
137                         if (!split)
138                                 return -ENOMEM;
139                         set_pte(kpte, mk_pte(split, ref_prot2));
140                         kpte_page = split;
141                 }
142                 page_private(kpte_page)++;
143         } else if (!pte_huge(*kpte)) {
144                 set_pte(kpte, pfn_pte(pfn, ref_prot));
145                 BUG_ON(page_private(kpte_page) == 0);
146                 page_private(kpte_page)--;
147         } 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
153         if (page_private(kpte_page) == 0) {
154                 save_page(kpte_page);
155                 revert_page(address, ref_prot);
156         }
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  */
173 int 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;
189                         pgprot_t prot2;
190                         addr2 = __START_KERNEL_map + __pa(address);
191                         /* 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);
195                 } 
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 */
202 int 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
208 void global_flush_tlb(void)
209
210         struct page *dpage;
211
212         down_read(&init_mm.mmap_sem);
213         dpage = xchg(&deferred_pages, NULL);
214         up_read(&init_mm.mmap_sem);
215
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;
220                 ClearPagePrivate(tmp);
221                 __free_page(tmp);
222         } 
223
224
225 EXPORT_SYMBOL(change_page_attr);
226 EXPORT_SYMBOL(global_flush_tlb);