Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2002 Andi Kleen, SuSE Labs. |
| 3 | * Thanks to Ben LaHaise for precious feedback. |
| 4 | */ |
| 5 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Zachary Amsden | c9b02a2 | 2005-09-03 15:56:40 -0700 | [diff] [blame] | 14 | #include <asm/pgalloc.h> |
Dave Jones | f8af095 | 2006-01-06 00:12:10 -0800 | [diff] [blame] | 15 | #include <asm/sections.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | static DEFINE_SPINLOCK(cpa_lock); |
| 18 | static struct list_head df_list = LIST_HEAD_INIT(df_list); |
| 19 | |
| 20 | |
| 21 | pte_t *lookup_address(unsigned long address) |
| 22 | { |
| 23 | pgd_t *pgd = pgd_offset_k(address); |
| 24 | pud_t *pud; |
| 25 | pmd_t *pmd; |
| 26 | if (pgd_none(*pgd)) |
| 27 | return NULL; |
| 28 | pud = pud_offset(pgd, address); |
| 29 | if (pud_none(*pud)) |
| 30 | return NULL; |
| 31 | pmd = pmd_offset(pud, address); |
| 32 | if (pmd_none(*pmd)) |
| 33 | return NULL; |
| 34 | if (pmd_large(*pmd)) |
| 35 | return (pte_t *)pmd; |
| 36 | return pte_offset_kernel(pmd, address); |
| 37 | } |
| 38 | |
Dave Jones | f8af095 | 2006-01-06 00:12:10 -0800 | [diff] [blame] | 39 | static struct page *split_large_page(unsigned long address, pgprot_t prot, |
| 40 | pgprot_t ref_prot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
| 42 | int i; |
| 43 | unsigned long addr; |
| 44 | struct page *base; |
| 45 | pte_t *pbase; |
| 46 | |
| 47 | spin_unlock_irq(&cpa_lock); |
| 48 | base = alloc_pages(GFP_KERNEL, 0); |
| 49 | spin_lock_irq(&cpa_lock); |
| 50 | if (!base) |
| 51 | return NULL; |
| 52 | |
Nick Piggin | 84d1c05 | 2006-03-22 00:08:31 -0800 | [diff] [blame] | 53 | /* |
| 54 | * page_private is used to track the number of entries in |
| 55 | * the page table page that have non standard attributes. |
| 56 | */ |
| 57 | SetPagePrivate(base); |
| 58 | page_private(base) = 0; |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | address = __pa(address); |
| 61 | addr = address & LARGE_PAGE_MASK; |
| 62 | pbase = (pte_t *)page_address(base); |
Jeremy Fitzhardinge | fdb4c33 | 2007-07-17 18:37:03 -0700 | [diff] [blame] | 63 | paravirt_alloc_pt(&init_mm, page_to_pfn(base)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE) { |
Zachary Amsden | c9b02a2 | 2005-09-03 15:56:40 -0700 | [diff] [blame] | 65 | set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, |
Dave Jones | f8af095 | 2006-01-06 00:12:10 -0800 | [diff] [blame] | 66 | addr == address ? prot : ref_prot)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | return base; |
| 69 | } |
| 70 | |
Andi Kleen | 018d2ad | 2007-06-20 12:23:36 +0200 | [diff] [blame] | 71 | static void cache_flush_page(struct page *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
Andi Kleen | 018d2ad | 2007-06-20 12:23:36 +0200 | [diff] [blame] | 73 | unsigned long adr = (unsigned long)page_address(p); |
| 74 | int i; |
| 75 | for (i = 0; i < PAGE_SIZE; i += boot_cpu_data.x86_clflush_size) |
| 76 | asm volatile("clflush (%0)" :: "r" (adr + i)); |
| 77 | } |
Andi Kleen | 3760dd6 | 2006-12-07 02:14:05 +0100 | [diff] [blame] | 78 | |
Andi Kleen | 018d2ad | 2007-06-20 12:23:36 +0200 | [diff] [blame] | 79 | static void flush_kernel_map(void *arg) |
| 80 | { |
| 81 | struct list_head *lh = (struct list_head *)arg; |
| 82 | struct page *p; |
| 83 | |
| 84 | /* High level code is not ready for clflush yet */ |
Andi Kleen | d3f3c93 | 2007-08-10 22:31:02 +0200 | [diff] [blame] | 85 | if (0 && cpu_has_clflush) { |
Andi Kleen | 018d2ad | 2007-06-20 12:23:36 +0200 | [diff] [blame] | 86 | list_for_each_entry (p, lh, lru) |
| 87 | cache_flush_page(p); |
Andi Kleen | 3760dd6 | 2006-12-07 02:14:05 +0100 | [diff] [blame] | 88 | } else if (boot_cpu_data.x86_model >= 4) |
Zachary Amsden | 4bb0d3e | 2005-09-03 15:56:36 -0700 | [diff] [blame] | 89 | wbinvd(); |
Andi Kleen | 3760dd6 | 2006-12-07 02:14:05 +0100 | [diff] [blame] | 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | /* Flush all to work around Errata in early athlons regarding |
| 92 | * large page flushing. |
| 93 | */ |
| 94 | __flush_tlb_all(); |
| 95 | } |
| 96 | |
| 97 | static void set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte) |
| 98 | { |
| 99 | struct page *page; |
| 100 | unsigned long flags; |
| 101 | |
| 102 | set_pte_atomic(kpte, pte); /* change init_mm */ |
Jeremy Fitzhardinge | 5311ab6 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 103 | if (SHARED_KERNEL_PMD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | return; |
| 105 | |
| 106 | spin_lock_irqsave(&pgd_lock, flags); |
| 107 | for (page = pgd_list; page; page = (struct page *)page->index) { |
| 108 | pgd_t *pgd; |
| 109 | pud_t *pud; |
| 110 | pmd_t *pmd; |
| 111 | pgd = (pgd_t *)page_address(page) + pgd_index(address); |
| 112 | pud = pud_offset(pgd, address); |
| 113 | pmd = pmd_offset(pud, address); |
| 114 | set_pte_atomic((pte_t *)pmd, pte); |
| 115 | } |
| 116 | spin_unlock_irqrestore(&pgd_lock, flags); |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * No more special protections in this 2/4MB area - revert to a |
| 121 | * large page again. |
| 122 | */ |
| 123 | static inline void revert_page(struct page *kpte_page, unsigned long address) |
| 124 | { |
Dave Jones | f8af095 | 2006-01-06 00:12:10 -0800 | [diff] [blame] | 125 | pgprot_t ref_prot; |
| 126 | pte_t *linear; |
| 127 | |
| 128 | ref_prot = |
| 129 | ((address & LARGE_PAGE_MASK) < (unsigned long)&_etext) |
| 130 | ? PAGE_KERNEL_LARGE_EXEC : PAGE_KERNEL_LARGE; |
| 131 | |
| 132 | linear = (pte_t *) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | pmd_offset(pud_offset(pgd_offset_k(address), address), address); |
| 134 | set_pmd_pte(linear, address, |
| 135 | pfn_pte((__pa(address) & LARGE_PAGE_MASK) >> PAGE_SHIFT, |
Dave Jones | f8af095 | 2006-01-06 00:12:10 -0800 | [diff] [blame] | 136 | ref_prot)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Andi Kleen | 65d2f0b | 2007-07-21 17:09:51 +0200 | [diff] [blame] | 139 | static inline void save_page(struct page *kpte_page) |
| 140 | { |
| 141 | if (!test_and_set_bit(PG_arch_1, &kpte_page->flags)) |
| 142 | list_add(&kpte_page->lru, &df_list); |
| 143 | } |
| 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | static int |
| 146 | __change_page_attr(struct page *page, pgprot_t prot) |
| 147 | { |
| 148 | pte_t *kpte; |
| 149 | unsigned long address; |
| 150 | struct page *kpte_page; |
| 151 | |
| 152 | BUG_ON(PageHighMem(page)); |
| 153 | address = (unsigned long)page_address(page); |
| 154 | |
| 155 | kpte = lookup_address(address); |
| 156 | if (!kpte) |
| 157 | return -EINVAL; |
| 158 | kpte_page = virt_to_page(kpte); |
Andi Kleen | 65d2f0b | 2007-07-21 17:09:51 +0200 | [diff] [blame] | 159 | BUG_ON(PageLRU(kpte_page)); |
| 160 | BUG_ON(PageCompound(kpte_page)); |
| 161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL)) { |
Jan Beulich | d01ad8d | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 163 | if (!pte_huge(*kpte)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | set_pte_atomic(kpte, mk_pte(page, prot)); |
| 165 | } else { |
Dave Jones | f8af095 | 2006-01-06 00:12:10 -0800 | [diff] [blame] | 166 | pgprot_t ref_prot; |
| 167 | struct page *split; |
| 168 | |
| 169 | ref_prot = |
| 170 | ((address & LARGE_PAGE_MASK) < (unsigned long)&_etext) |
| 171 | ? PAGE_KERNEL_EXEC : PAGE_KERNEL; |
| 172 | split = split_large_page(address, prot, ref_prot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | if (!split) |
| 174 | return -ENOMEM; |
Dave Jones | f8af095 | 2006-01-06 00:12:10 -0800 | [diff] [blame] | 175 | set_pmd_pte(kpte,address,mk_pte(split, ref_prot)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | kpte_page = split; |
Nick Piggin | 84d1c05 | 2006-03-22 00:08:31 -0800 | [diff] [blame] | 177 | } |
| 178 | page_private(kpte_page)++; |
Jan Beulich | d01ad8d | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 179 | } else if (!pte_huge(*kpte)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | set_pte_atomic(kpte, mk_pte(page, PAGE_KERNEL)); |
Nick Piggin | 84d1c05 | 2006-03-22 00:08:31 -0800 | [diff] [blame] | 181 | BUG_ON(page_private(kpte_page) == 0); |
| 182 | page_private(kpte_page)--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } else |
| 184 | BUG(); |
| 185 | |
| 186 | /* |
| 187 | * If the pte was reserved, it means it was created at boot |
| 188 | * time (not via split_large_page) and in turn we must not |
| 189 | * replace it with a largepage. |
| 190 | */ |
Andi Kleen | 65d2f0b | 2007-07-21 17:09:51 +0200 | [diff] [blame] | 191 | |
| 192 | save_page(kpte_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | if (!PageReserved(kpte_page)) { |
Nick Piggin | 84d1c05 | 2006-03-22 00:08:31 -0800 | [diff] [blame] | 194 | if (cpu_has_pse && (page_private(kpte_page) == 0)) { |
Zachary Amsden | c119ecc | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 195 | paravirt_release_pt(page_to_pfn(kpte_page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | revert_page(kpte_page, address); |
| 197 | } |
| 198 | } |
| 199 | return 0; |
| 200 | } |
| 201 | |
Andi Kleen | 018d2ad | 2007-06-20 12:23:36 +0200 | [diff] [blame] | 202 | static inline void flush_map(struct list_head *l) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
Andi Kleen | 018d2ad | 2007-06-20 12:23:36 +0200 | [diff] [blame] | 204 | on_each_cpu(flush_kernel_map, l, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Change the page attributes of an page in the linear mapping. |
| 209 | * |
| 210 | * This should be used when a page is mapped with a different caching policy |
| 211 | * than write-back somewhere - some CPUs do not like it when mappings with |
| 212 | * different caching policies exist. This changes the page attributes of the |
| 213 | * in kernel linear mapping too. |
| 214 | * |
| 215 | * The caller needs to ensure that there are no conflicting mappings elsewhere. |
| 216 | * This function only deals with the kernel linear map. |
| 217 | * |
| 218 | * Caller must call global_flush_tlb() after this. |
| 219 | */ |
| 220 | int change_page_attr(struct page *page, int numpages, pgprot_t prot) |
| 221 | { |
| 222 | int err = 0; |
| 223 | int i; |
| 224 | unsigned long flags; |
| 225 | |
| 226 | spin_lock_irqsave(&cpa_lock, flags); |
| 227 | for (i = 0; i < numpages; i++, page++) { |
| 228 | err = __change_page_attr(page, prot); |
| 229 | if (err) |
| 230 | break; |
| 231 | } |
| 232 | spin_unlock_irqrestore(&cpa_lock, flags); |
| 233 | return err; |
| 234 | } |
| 235 | |
| 236 | void global_flush_tlb(void) |
Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 237 | { |
| 238 | struct list_head l; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | struct page *pg, *next; |
| 240 | |
| 241 | BUG_ON(irqs_disabled()); |
| 242 | |
| 243 | spin_lock_irq(&cpa_lock); |
Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 244 | list_replace_init(&df_list, &l); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | spin_unlock_irq(&cpa_lock); |
Andi Kleen | 018d2ad | 2007-06-20 12:23:36 +0200 | [diff] [blame] | 246 | flush_map(&l); |
Andi Kleen | 3760dd6 | 2006-12-07 02:14:05 +0100 | [diff] [blame] | 247 | list_for_each_entry_safe(pg, next, &l, lru) { |
Andi Kleen | 65d2f0b | 2007-07-21 17:09:51 +0200 | [diff] [blame] | 248 | list_del(&pg->lru); |
| 249 | clear_bit(PG_arch_1, &pg->flags); |
| 250 | if (PageReserved(pg) || !cpu_has_pse || page_private(pg) != 0) |
| 251 | continue; |
| 252 | ClearPagePrivate(pg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | __free_page(pg); |
Andi Kleen | 3760dd6 | 2006-12-07 02:14:05 +0100 | [diff] [blame] | 254 | } |
Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 255 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 258 | void kernel_map_pages(struct page *page, int numpages, int enable) |
| 259 | { |
| 260 | if (PageHighMem(page)) |
| 261 | return; |
Ingo Molnar | de5097c | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 262 | if (!enable) |
Ingo Molnar | f9b8404 | 2006-06-27 02:54:49 -0700 | [diff] [blame] | 263 | debug_check_no_locks_freed(page_address(page), |
| 264 | numpages * PAGE_SIZE); |
Ingo Molnar | de5097c | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | /* the return value is ignored - the calls cannot fail, |
| 267 | * large pages are disabled at boot time. |
| 268 | */ |
| 269 | change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0)); |
| 270 | /* we should perform an IPI and flush all tlbs, |
| 271 | * but that can deadlock->flush only current cpu. |
| 272 | */ |
| 273 | __flush_tlb_all(); |
| 274 | } |
| 275 | #endif |
| 276 | |
| 277 | EXPORT_SYMBOL(change_page_attr); |
| 278 | EXPORT_SYMBOL(global_flush_tlb); |