]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/x86/mm/pageattr.c
x86: cpa: move flush to cpa
[linux-2.6-omap-h63xx.git] / arch / x86 / mm / pageattr.c
1 /*
2  * Copyright 2002 Andi Kleen, SuSE Labs.
3  * Thanks to Ben LaHaise for precious feedback.
4  */
5 #include <linux/highmem.h>
6 #include <linux/bootmem.h>
7 #include <linux/module.h>
8 #include <linux/sched.h>
9 #include <linux/slab.h>
10 #include <linux/mm.h>
11
12 #include <asm/e820.h>
13 #include <asm/processor.h>
14 #include <asm/tlbflush.h>
15 #include <asm/sections.h>
16 #include <asm/uaccess.h>
17 #include <asm/pgalloc.h>
18
19 static inline int
20 within(unsigned long addr, unsigned long start, unsigned long end)
21 {
22         return addr >= start && addr < end;
23 }
24
25 /*
26  * Flushing functions
27  */
28 void clflush_cache_range(void *addr, int size)
29 {
30         int i;
31
32         for (i = 0; i < size; i += boot_cpu_data.x86_clflush_size)
33                 clflush(addr+i);
34 }
35
36 static void flush_kernel_map(void *arg)
37 {
38         /*
39          * Flush all to work around Errata in early athlons regarding
40          * large page flushing.
41          */
42         __flush_tlb_all();
43
44         if (boot_cpu_data.x86_model >= 4)
45                 wbinvd();
46 }
47
48 static void global_flush_tlb(void)
49 {
50         BUG_ON(irqs_disabled());
51
52         on_each_cpu(flush_kernel_map, NULL, 1, 1);
53 }
54
55 /*
56  * Certain areas of memory on x86 require very specific protection flags,
57  * for example the BIOS area or kernel text. Callers don't always get this
58  * right (again, ioremap() on BIOS memory is not uncommon) so this function
59  * checks and fixes these known static required protection bits.
60  */
61 static inline pgprot_t static_protections(pgprot_t prot, unsigned long address)
62 {
63         pgprot_t forbidden = __pgprot(0);
64
65         /*
66          * The BIOS area between 640k and 1Mb needs to be executable for
67          * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
68          */
69         if (within(__pa(address), BIOS_BEGIN, BIOS_END))
70                 pgprot_val(forbidden) |= _PAGE_NX;
71
72         /*
73          * The kernel text needs to be executable for obvious reasons
74          * Does not cover __inittext since that is gone later on
75          */
76         if (within(address, (unsigned long)_text, (unsigned long)_etext))
77                 pgprot_val(forbidden) |= _PAGE_NX;
78
79 #ifdef CONFIG_DEBUG_RODATA
80         /* The .rodata section needs to be read-only */
81         if (within(address, (unsigned long)__start_rodata,
82                                 (unsigned long)__end_rodata))
83                 pgprot_val(forbidden) |= _PAGE_RW;
84 #endif
85
86         prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
87
88         return prot;
89 }
90
91 pte_t *lookup_address(unsigned long address, int *level)
92 {
93         pgd_t *pgd = pgd_offset_k(address);
94         pud_t *pud;
95         pmd_t *pmd;
96
97         *level = PG_LEVEL_NONE;
98
99         if (pgd_none(*pgd))
100                 return NULL;
101         pud = pud_offset(pgd, address);
102         if (pud_none(*pud))
103                 return NULL;
104         pmd = pmd_offset(pud, address);
105         if (pmd_none(*pmd))
106                 return NULL;
107
108         *level = PG_LEVEL_2M;
109         if (pmd_large(*pmd))
110                 return (pte_t *)pmd;
111
112         *level = PG_LEVEL_4K;
113         return pte_offset_kernel(pmd, address);
114 }
115
116 static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
117 {
118         /* change init_mm */
119         set_pte_atomic(kpte, pte);
120 #ifdef CONFIG_X86_32
121         if (!SHARED_KERNEL_PMD) {
122                 struct page *page;
123
124                 for (page = pgd_list; page; page = (struct page *)page->index) {
125                         pgd_t *pgd;
126                         pud_t *pud;
127                         pmd_t *pmd;
128
129                         pgd = (pgd_t *)page_address(page) + pgd_index(address);
130                         pud = pud_offset(pgd, address);
131                         pmd = pmd_offset(pud, address);
132                         set_pte_atomic((pte_t *)pmd, pte);
133                 }
134         }
135 #endif
136 }
137
138 static int split_large_page(pte_t *kpte, unsigned long address)
139 {
140         pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
141         gfp_t gfp_flags = GFP_KERNEL;
142         unsigned long flags;
143         unsigned long addr;
144         pte_t *pbase, *tmp;
145         struct page *base;
146         int i, level;
147
148 #ifdef CONFIG_DEBUG_PAGEALLOC
149         gfp_flags = GFP_ATOMIC;
150 #endif
151         base = alloc_pages(gfp_flags, 0);
152         if (!base)
153                 return -ENOMEM;
154
155         spin_lock_irqsave(&pgd_lock, flags);
156         /*
157          * Check for races, another CPU might have split this page
158          * up for us already:
159          */
160         tmp = lookup_address(address, &level);
161         if (tmp != kpte) {
162                 WARN_ON_ONCE(1);
163                 goto out_unlock;
164         }
165
166         address = __pa(address);
167         addr = address & LARGE_PAGE_MASK;
168         pbase = (pte_t *)page_address(base);
169 #ifdef CONFIG_X86_32
170         paravirt_alloc_pt(&init_mm, page_to_pfn(base));
171 #endif
172
173         for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
174                 set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
175
176         /*
177          * Install the new, split up pagetable. Important detail here:
178          *
179          * On Intel the NX bit of all levels must be cleared to make a
180          * page executable. See section 4.13.2 of Intel 64 and IA-32
181          * Architectures Software Developer's Manual).
182          */
183         ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
184         __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
185         base = NULL;
186
187 out_unlock:
188         spin_unlock_irqrestore(&pgd_lock, flags);
189
190         if (base)
191                 __free_pages(base, 0);
192
193         return 0;
194 }
195
196 static int
197 __change_page_attr(unsigned long address, unsigned long pfn, pgprot_t prot)
198 {
199         struct page *kpte_page;
200         int level, err = 0;
201         pte_t *kpte;
202
203 #ifdef CONFIG_X86_32
204         BUG_ON(pfn > max_low_pfn);
205 #endif
206
207 repeat:
208         kpte = lookup_address(address, &level);
209         if (!kpte)
210                 return -EINVAL;
211
212         kpte_page = virt_to_page(kpte);
213         BUG_ON(PageLRU(kpte_page));
214         BUG_ON(PageCompound(kpte_page));
215
216         prot = static_protections(prot, address);
217
218         if (level == PG_LEVEL_4K) {
219                 set_pte_atomic(kpte, pfn_pte(pfn, canon_pgprot(prot)));
220         } else {
221                 err = split_large_page(kpte, address);
222                 if (!err)
223                         goto repeat;
224         }
225         return err;
226 }
227
228 /**
229  * change_page_attr_addr - Change page table attributes in linear mapping
230  * @address: Virtual address in linear mapping.
231  * @numpages: Number of pages to change
232  * @prot:    New page table attribute (PAGE_*)
233  *
234  * Change page attributes of a page in the direct mapping. This is a variant
235  * of change_page_attr() that also works on memory holes that do not have
236  * mem_map entry (pfn_valid() is false).
237  *
238  * See change_page_attr() documentation for more details.
239  *
240  * Modules and drivers should use the set_memory_* APIs instead.
241  */
242
243 static int change_page_attr_addr(unsigned long address, int numpages,
244                                                                 pgprot_t prot)
245 {
246         int err = 0, kernel_map = 0, i;
247
248 #ifdef CONFIG_X86_64
249         if (address >= __START_KERNEL_map &&
250                         address < __START_KERNEL_map + KERNEL_TEXT_SIZE) {
251
252                 address = (unsigned long)__va(__pa(address));
253                 kernel_map = 1;
254         }
255 #endif
256
257         for (i = 0; i < numpages; i++, address += PAGE_SIZE) {
258                 unsigned long pfn = __pa(address) >> PAGE_SHIFT;
259
260                 if (!kernel_map || pte_present(pfn_pte(0, prot))) {
261                         err = __change_page_attr(address, pfn, prot);
262                         if (err)
263                                 break;
264                 }
265 #ifdef CONFIG_X86_64
266                 /*
267                  * Handle kernel mapping too which aliases part of
268                  * lowmem:
269                  */
270                 if (__pa(address) < KERNEL_TEXT_SIZE) {
271                         unsigned long addr2;
272                         pgprot_t prot2;
273
274                         addr2 = __START_KERNEL_map + __pa(address);
275                         /* Make sure the kernel mappings stay executable */
276                         prot2 = pte_pgprot(pte_mkexec(pfn_pte(0, prot)));
277                         err = __change_page_attr(addr2, pfn, prot2);
278                 }
279 #endif
280         }
281
282         return err;
283 }
284
285 /**
286  * change_page_attr_set - Change page table attributes in the linear mapping.
287  * @addr: Virtual address in linear mapping.
288  * @numpages: Number of pages to change
289  * @prot: Protection/caching type bits to set (PAGE_*)
290  *
291  * Returns 0 on success, otherwise a negated errno.
292  *
293  * This should be used when a page is mapped with a different caching policy
294  * than write-back somewhere - some CPUs do not like it when mappings with
295  * different caching policies exist. This changes the page attributes of the
296  * in kernel linear mapping too.
297  *
298  * The caller needs to ensure that there are no conflicting mappings elsewhere
299  * (e.g. in user space) * This function only deals with the kernel linear map.
300  *
301  * This function is different from change_page_attr() in that only selected bits
302  * are impacted, all other bits remain as is.
303  */
304 static int change_page_attr_set(unsigned long addr, int numpages,
305                                                                 pgprot_t prot)
306 {
307         pgprot_t current_prot;
308         int level;
309         pte_t *pte;
310
311         pte = lookup_address(addr, &level);
312         if (pte)
313                 current_prot = pte_pgprot(*pte);
314         else
315                 pgprot_val(current_prot) = 0;
316
317         pgprot_val(prot) = pgprot_val(current_prot) | pgprot_val(prot);
318
319         return change_page_attr_addr(addr, numpages, prot);
320 }
321
322 /**
323  * change_page_attr_clear - Change page table attributes in the linear mapping.
324  * @addr: Virtual address in linear mapping.
325  * @numpages: Number of pages to change
326  * @prot: Protection/caching type bits to clear (PAGE_*)
327  *
328  * Returns 0 on success, otherwise a negated errno.
329  *
330  * This should be used when a page is mapped with a different caching policy
331  * than write-back somewhere - some CPUs do not like it when mappings with
332  * different caching policies exist. This changes the page attributes of the
333  * in kernel linear mapping too.
334  *
335  * The caller needs to ensure that there are no conflicting mappings elsewhere
336  * (e.g. in user space) * This function only deals with the kernel linear map.
337  *
338  * This function is different from change_page_attr() in that only selected bits
339  * are impacted, all other bits remain as is.
340  */
341 static int change_page_attr_clear(unsigned long addr, int numpages,
342                                                                 pgprot_t prot)
343 {
344         pgprot_t current_prot;
345         int level;
346         pte_t *pte;
347
348         pte = lookup_address(addr, &level);
349         if (pte)
350                 current_prot = pte_pgprot(*pte);
351         else
352                 pgprot_val(current_prot) = 0;
353
354         pgprot_val(prot) = pgprot_val(current_prot) & ~pgprot_val(prot);
355
356         return change_page_attr_addr(addr, numpages, prot);
357 }
358
359 int set_memory_uc(unsigned long addr, int numpages)
360 {
361         int err;
362
363         err = change_page_attr_set(addr, numpages,
364                                 __pgprot(_PAGE_PCD | _PAGE_PWT));
365         global_flush_tlb();
366         return err;
367 }
368 EXPORT_SYMBOL(set_memory_uc);
369
370 int set_memory_wb(unsigned long addr, int numpages)
371 {
372         int err;
373
374         err = change_page_attr_clear(addr, numpages,
375                                 __pgprot(_PAGE_PCD | _PAGE_PWT));
376         global_flush_tlb();
377         return err;
378 }
379 EXPORT_SYMBOL(set_memory_wb);
380
381 int set_memory_x(unsigned long addr, int numpages)
382 {
383         int err;
384
385         err = change_page_attr_clear(addr, numpages,
386                                 __pgprot(_PAGE_NX));
387         global_flush_tlb();
388         return err;
389 }
390 EXPORT_SYMBOL(set_memory_x);
391
392 int set_memory_nx(unsigned long addr, int numpages)
393 {
394         int err;
395
396         err = change_page_attr_set(addr, numpages,
397                                 __pgprot(_PAGE_NX));
398         global_flush_tlb();
399         return err;
400 }
401 EXPORT_SYMBOL(set_memory_nx);
402
403 int set_memory_ro(unsigned long addr, int numpages)
404 {
405         int err;
406
407         err = change_page_attr_clear(addr, numpages,
408                                 __pgprot(_PAGE_RW));
409         global_flush_tlb();
410         return err;
411 }
412
413 int set_memory_rw(unsigned long addr, int numpages)
414 {
415         int err;
416
417         err = change_page_attr_set(addr, numpages,
418                                 __pgprot(_PAGE_RW));
419         global_flush_tlb();
420         return err;
421 }
422
423 int set_memory_np(unsigned long addr, int numpages)
424 {
425         int err;
426
427         err = change_page_attr_clear(addr, numpages,
428                                 __pgprot(_PAGE_PRESENT));
429         global_flush_tlb();
430         return err;
431 }
432
433 int set_pages_uc(struct page *page, int numpages)
434 {
435         unsigned long addr = (unsigned long)page_address(page);
436
437         return set_memory_uc(addr, numpages);
438 }
439 EXPORT_SYMBOL(set_pages_uc);
440
441 int set_pages_wb(struct page *page, int numpages)
442 {
443         unsigned long addr = (unsigned long)page_address(page);
444
445         return set_memory_wb(addr, numpages);
446 }
447 EXPORT_SYMBOL(set_pages_wb);
448
449 int set_pages_x(struct page *page, int numpages)
450 {
451         unsigned long addr = (unsigned long)page_address(page);
452
453         return set_memory_x(addr, numpages);
454 }
455 EXPORT_SYMBOL(set_pages_x);
456
457 int set_pages_nx(struct page *page, int numpages)
458 {
459         unsigned long addr = (unsigned long)page_address(page);
460
461         return set_memory_nx(addr, numpages);
462 }
463 EXPORT_SYMBOL(set_pages_nx);
464
465 int set_pages_ro(struct page *page, int numpages)
466 {
467         unsigned long addr = (unsigned long)page_address(page);
468
469         return set_memory_ro(addr, numpages);
470 }
471
472 int set_pages_rw(struct page *page, int numpages)
473 {
474         unsigned long addr = (unsigned long)page_address(page);
475
476         return set_memory_rw(addr, numpages);
477 }
478
479
480 #ifdef CONFIG_DEBUG_PAGEALLOC
481
482 static int __set_pages_p(struct page *page, int numpages)
483 {
484         unsigned long addr = (unsigned long)page_address(page);
485         return change_page_attr_set(addr, numpages,
486                                 __pgprot(_PAGE_PRESENT | _PAGE_RW));
487 }
488
489 static int __set_pages_np(struct page *page, int numpages)
490 {
491         unsigned long addr = (unsigned long)page_address(page);
492         return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
493 }
494
495 void kernel_map_pages(struct page *page, int numpages, int enable)
496 {
497         if (PageHighMem(page))
498                 return;
499         if (!enable) {
500                 debug_check_no_locks_freed(page_address(page),
501                                            numpages * PAGE_SIZE);
502         }
503
504         /*
505          * If page allocator is not up yet then do not call c_p_a():
506          */
507         if (!debug_pagealloc_enabled)
508                 return;
509
510         /*
511          * The return value is ignored - the calls cannot fail,
512          * large pages are disabled at boot time:
513          */
514         if (enable)
515                 __set_pages_p(page, numpages);
516         else
517                 __set_pages_np(page, numpages);
518
519         /*
520          * We should perform an IPI and flush all tlbs,
521          * but that can deadlock->flush only current cpu:
522          */
523         __flush_tlb_all();
524 }
525 #endif
526
527 /*
528  * The testcases use internal knowledge of the implementation that shouldn't
529  * be exposed to the rest of the kernel. Include these directly here.
530  */
531 #ifdef CONFIG_CPA_DEBUG
532 #include "pageattr-test.c"
533 #endif