Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / arch / i386 / 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 <linux/kernel.h>
12 #include <asm/sections.h>
13 #include <asm/uaccess.h>
14 #include <asm/processor.h>
15 #include <asm/tlbflush.h>
16 #include <asm/pgalloc.h>
17 #include <asm/sections.h>
18
19 static DEFINE_SPINLOCK(cpa_lock);
20 static struct list_head df_list = LIST_HEAD_INIT(df_list);
21
22
23 pte_t *lookup_address(unsigned long address) 
24
25         pgd_t *pgd = pgd_offset_k(address);
26         pud_t *pud;
27         pmd_t *pmd;
28         if (pgd_none(*pgd))
29                 return NULL;
30         pud = pud_offset(pgd, address);
31         if (pud_none(*pud))
32                 return NULL;
33         pmd = pmd_offset(pud, address);
34         if (pmd_none(*pmd))
35                 return NULL;
36         if (pmd_large(*pmd))
37                 return (pte_t *)pmd;
38         return pte_offset_kernel(pmd, address);
39
40
41 static struct page *split_large_page(unsigned long address, pgprot_t prot,
42                                         pgprot_t ref_prot)
43
44         int i; 
45         unsigned long addr;
46         struct page *base;
47         pte_t *pbase;
48
49         spin_unlock_irq(&cpa_lock);
50         base = alloc_pages(GFP_KERNEL, 0);
51         spin_lock_irq(&cpa_lock);
52         if (!base) 
53                 return NULL;
54
55         /*
56          * page_private is used to track the number of entries in
57          * the page table page that have non standard attributes.
58          */
59         SetPagePrivate(base);
60         page_private(base) = 0;
61
62         address = __pa(address);
63         addr = address & LARGE_PAGE_MASK; 
64         pbase = (pte_t *)page_address(base);
65         for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE) {
66                set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT,
67                                           addr == address ? prot : ref_prot));
68         }
69         return base;
70
71
72 static void flush_kernel_map(void *dummy) 
73
74         /* Could use CLFLUSH here if the CPU supports it (Hammer,P4) */
75         if (boot_cpu_data.x86_model >= 4) 
76                 wbinvd();
77         /* Flush all to work around Errata in early athlons regarding 
78          * large page flushing. 
79          */
80         __flush_tlb_all();      
81 }
82
83 static void set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte) 
84
85         struct page *page;
86         unsigned long flags;
87
88         set_pte_atomic(kpte, pte);      /* change init_mm */
89         if (HAVE_SHARED_KERNEL_PMD)
90                 return;
91
92         spin_lock_irqsave(&pgd_lock, flags);
93         for (page = pgd_list; page; page = (struct page *)page->index) {
94                 pgd_t *pgd;
95                 pud_t *pud;
96                 pmd_t *pmd;
97                 pgd = (pgd_t *)page_address(page) + pgd_index(address);
98                 pud = pud_offset(pgd, address);
99                 pmd = pmd_offset(pud, address);
100                 set_pte_atomic((pte_t *)pmd, pte);
101         }
102         spin_unlock_irqrestore(&pgd_lock, flags);
103 }
104
105 /* 
106  * No more special protections in this 2/4MB area - revert to a
107  * large page again. 
108  */
109 static inline void revert_page(struct page *kpte_page, unsigned long address)
110 {
111         pgprot_t ref_prot;
112         pte_t *linear;
113
114         ref_prot =
115         ((address & LARGE_PAGE_MASK) < (unsigned long)&_etext)
116                 ? PAGE_KERNEL_LARGE_EXEC : PAGE_KERNEL_LARGE;
117
118         linear = (pte_t *)
119                 pmd_offset(pud_offset(pgd_offset_k(address), address), address);
120         set_pmd_pte(linear,  address,
121                     pfn_pte((__pa(address) & LARGE_PAGE_MASK) >> PAGE_SHIFT,
122                             ref_prot));
123 }
124
125 static int
126 __change_page_attr(struct page *page, pgprot_t prot)
127
128         pte_t *kpte; 
129         unsigned long address;
130         struct page *kpte_page;
131
132         BUG_ON(PageHighMem(page));
133         address = (unsigned long)page_address(page);
134
135         if (address >= (unsigned long)__start_rodata && address <= (unsigned long)__end_rodata &&
136                 (pgprot_val(prot) & _PAGE_RW)) {
137                 pgprot_val(prot) &= ~(_PAGE_RW);
138                 add_taint(TAINT_MACHINE_CHECK);
139         }
140
141         kpte = lookup_address(address);
142         if (!kpte)
143                 return -EINVAL;
144         kpte_page = virt_to_page(kpte);
145         if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL)) { 
146                 if ((pte_val(*kpte) & _PAGE_PSE) == 0) { 
147                         set_pte_atomic(kpte, mk_pte(page, prot)); 
148                 } else {
149                         pgprot_t ref_prot;
150                         struct page *split;
151
152                         ref_prot =
153                         ((address & LARGE_PAGE_MASK) < (unsigned long)&_etext)
154                                 ? PAGE_KERNEL_EXEC : PAGE_KERNEL;
155                         split = split_large_page(address, prot, ref_prot);
156                         if (!split)
157                                 return -ENOMEM;
158                         set_pmd_pte(kpte,address,mk_pte(split, ref_prot));
159                         kpte_page = split;
160                 }
161                 page_private(kpte_page)++;
162         } else if ((pte_val(*kpte) & _PAGE_PSE) == 0) { 
163                 set_pte_atomic(kpte, mk_pte(page, PAGE_KERNEL));
164                 BUG_ON(page_private(kpte_page) == 0);
165                 page_private(kpte_page)--;
166         } else
167                 BUG();
168
169         /*
170          * If the pte was reserved, it means it was created at boot
171          * time (not via split_large_page) and in turn we must not
172          * replace it with a largepage.
173          */
174         if (!PageReserved(kpte_page)) {
175                 if (cpu_has_pse && (page_private(kpte_page) == 0)) {
176                         ClearPagePrivate(kpte_page);
177                         list_add(&kpte_page->lru, &df_list);
178                         revert_page(kpte_page, address);
179                 }
180         }
181         return 0;
182
183
184 static inline void flush_map(void)
185 {
186         on_each_cpu(flush_kernel_map, NULL, 1, 1);
187 }
188
189 /*
190  * Change the page attributes of an page in the linear mapping.
191  *
192  * This should be used when a page is mapped with a different caching policy
193  * than write-back somewhere - some CPUs do not like it when mappings with
194  * different caching policies exist. This changes the page attributes of the
195  * in kernel linear mapping too.
196  * 
197  * The caller needs to ensure that there are no conflicting mappings elsewhere.
198  * This function only deals with the kernel linear map.
199  * 
200  * Caller must call global_flush_tlb() after this.
201  */
202 int change_page_attr(struct page *page, int numpages, pgprot_t prot)
203 {
204         int err = 0; 
205         int i; 
206         unsigned long flags;
207
208         spin_lock_irqsave(&cpa_lock, flags);
209         for (i = 0; i < numpages; i++, page++) { 
210                 err = __change_page_attr(page, prot);
211                 if (err) 
212                         break; 
213         }       
214         spin_unlock_irqrestore(&cpa_lock, flags);
215         return err;
216 }
217
218 void global_flush_tlb(void)
219 {
220         struct list_head l;
221         struct page *pg, *next;
222
223         BUG_ON(irqs_disabled());
224
225         spin_lock_irq(&cpa_lock);
226         list_replace_init(&df_list, &l);
227         spin_unlock_irq(&cpa_lock);
228         flush_map();
229         list_for_each_entry_safe(pg, next, &l, lru)
230                 __free_page(pg);
231 }
232
233 #ifdef CONFIG_DEBUG_PAGEALLOC
234 void kernel_map_pages(struct page *page, int numpages, int enable)
235 {
236         if (PageHighMem(page))
237                 return;
238         if (!enable)
239                 debug_check_no_locks_freed(page_address(page),
240                                            numpages * PAGE_SIZE);
241
242         /* the return value is ignored - the calls cannot fail,
243          * large pages are disabled at boot time.
244          */
245         change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
246         /* we should perform an IPI and flush all tlbs,
247          * but that can deadlock->flush only current cpu.
248          */
249         __flush_tlb_all();
250 }
251 #endif
252
253 EXPORT_SYMBOL(change_page_attr);
254 EXPORT_SYMBOL(global_flush_tlb);