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); |
| 63 | for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE) { |
Zachary Amsden | c9b02a2 | 2005-09-03 15:56:40 -0700 | [diff] [blame] | 64 | set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, |
Dave Jones | f8af095 | 2006-01-06 00:12:10 -0800 | [diff] [blame] | 65 | addr == address ? prot : ref_prot)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
| 67 | return base; |
| 68 | } |
| 69 | |
| 70 | static void flush_kernel_map(void *dummy) |
| 71 | { |
| 72 | /* Could use CLFLUSH here if the CPU supports it (Hammer,P4) */ |
| 73 | if (boot_cpu_data.x86_model >= 4) |
Zachary Amsden | 4bb0d3e | 2005-09-03 15:56:36 -0700 | [diff] [blame] | 74 | wbinvd(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | /* Flush all to work around Errata in early athlons regarding |
| 76 | * large page flushing. |
| 77 | */ |
| 78 | __flush_tlb_all(); |
| 79 | } |
| 80 | |
| 81 | static void set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte) |
| 82 | { |
| 83 | struct page *page; |
| 84 | unsigned long flags; |
| 85 | |
| 86 | set_pte_atomic(kpte, pte); /* change init_mm */ |
| 87 | if (PTRS_PER_PMD > 1) |
| 88 | return; |
| 89 | |
| 90 | spin_lock_irqsave(&pgd_lock, flags); |
| 91 | for (page = pgd_list; page; page = (struct page *)page->index) { |
| 92 | pgd_t *pgd; |
| 93 | pud_t *pud; |
| 94 | pmd_t *pmd; |
| 95 | pgd = (pgd_t *)page_address(page) + pgd_index(address); |
| 96 | pud = pud_offset(pgd, address); |
| 97 | pmd = pmd_offset(pud, address); |
| 98 | set_pte_atomic((pte_t *)pmd, pte); |
| 99 | } |
| 100 | spin_unlock_irqrestore(&pgd_lock, flags); |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * No more special protections in this 2/4MB area - revert to a |
| 105 | * large page again. |
| 106 | */ |
| 107 | static inline void revert_page(struct page *kpte_page, unsigned long address) |
| 108 | { |
Dave Jones | f8af095 | 2006-01-06 00:12:10 -0800 | [diff] [blame] | 109 | pgprot_t ref_prot; |
| 110 | pte_t *linear; |
| 111 | |
| 112 | ref_prot = |
| 113 | ((address & LARGE_PAGE_MASK) < (unsigned long)&_etext) |
| 114 | ? PAGE_KERNEL_LARGE_EXEC : PAGE_KERNEL_LARGE; |
| 115 | |
| 116 | linear = (pte_t *) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | pmd_offset(pud_offset(pgd_offset_k(address), address), address); |
| 118 | set_pmd_pte(linear, address, |
| 119 | pfn_pte((__pa(address) & LARGE_PAGE_MASK) >> PAGE_SHIFT, |
Dave Jones | f8af095 | 2006-01-06 00:12:10 -0800 | [diff] [blame] | 120 | ref_prot)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static int |
| 124 | __change_page_attr(struct page *page, pgprot_t prot) |
| 125 | { |
| 126 | pte_t *kpte; |
| 127 | unsigned long address; |
| 128 | struct page *kpte_page; |
| 129 | |
| 130 | BUG_ON(PageHighMem(page)); |
| 131 | address = (unsigned long)page_address(page); |
| 132 | |
| 133 | kpte = lookup_address(address); |
| 134 | if (!kpte) |
| 135 | return -EINVAL; |
| 136 | kpte_page = virt_to_page(kpte); |
| 137 | if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL)) { |
| 138 | if ((pte_val(*kpte) & _PAGE_PSE) == 0) { |
| 139 | set_pte_atomic(kpte, mk_pte(page, prot)); |
| 140 | } else { |
Dave Jones | f8af095 | 2006-01-06 00:12:10 -0800 | [diff] [blame] | 141 | pgprot_t ref_prot; |
| 142 | struct page *split; |
| 143 | |
| 144 | ref_prot = |
| 145 | ((address & LARGE_PAGE_MASK) < (unsigned long)&_etext) |
| 146 | ? PAGE_KERNEL_EXEC : PAGE_KERNEL; |
| 147 | split = split_large_page(address, prot, ref_prot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | if (!split) |
| 149 | return -ENOMEM; |
Dave Jones | f8af095 | 2006-01-06 00:12:10 -0800 | [diff] [blame] | 150 | set_pmd_pte(kpte,address,mk_pte(split, ref_prot)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | kpte_page = split; |
Nick Piggin | 84d1c05 | 2006-03-22 00:08:31 -0800 | [diff] [blame] | 152 | } |
| 153 | page_private(kpte_page)++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } else if ((pte_val(*kpte) & _PAGE_PSE) == 0) { |
| 155 | set_pte_atomic(kpte, mk_pte(page, PAGE_KERNEL)); |
Nick Piggin | 84d1c05 | 2006-03-22 00:08:31 -0800 | [diff] [blame] | 156 | BUG_ON(page_private(kpte_page) == 0); |
| 157 | page_private(kpte_page)--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } else |
| 159 | BUG(); |
| 160 | |
| 161 | /* |
| 162 | * If the pte was reserved, it means it was created at boot |
| 163 | * time (not via split_large_page) and in turn we must not |
| 164 | * replace it with a largepage. |
| 165 | */ |
| 166 | if (!PageReserved(kpte_page)) { |
Nick Piggin | 84d1c05 | 2006-03-22 00:08:31 -0800 | [diff] [blame] | 167 | if (cpu_has_pse && (page_private(kpte_page) == 0)) { |
| 168 | ClearPagePrivate(kpte_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | list_add(&kpte_page->lru, &df_list); |
| 170 | revert_page(kpte_page, address); |
| 171 | } |
| 172 | } |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static inline void flush_map(void) |
| 177 | { |
| 178 | on_each_cpu(flush_kernel_map, NULL, 1, 1); |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Change the page attributes of an page in the linear mapping. |
| 183 | * |
| 184 | * This should be used when a page is mapped with a different caching policy |
| 185 | * than write-back somewhere - some CPUs do not like it when mappings with |
| 186 | * different caching policies exist. This changes the page attributes of the |
| 187 | * in kernel linear mapping too. |
| 188 | * |
| 189 | * The caller needs to ensure that there are no conflicting mappings elsewhere. |
| 190 | * This function only deals with the kernel linear map. |
| 191 | * |
| 192 | * Caller must call global_flush_tlb() after this. |
| 193 | */ |
| 194 | int change_page_attr(struct page *page, int numpages, pgprot_t prot) |
| 195 | { |
| 196 | int err = 0; |
| 197 | int i; |
| 198 | unsigned long flags; |
| 199 | |
| 200 | spin_lock_irqsave(&cpa_lock, flags); |
| 201 | for (i = 0; i < numpages; i++, page++) { |
| 202 | err = __change_page_attr(page, prot); |
| 203 | if (err) |
| 204 | break; |
| 205 | } |
| 206 | spin_unlock_irqrestore(&cpa_lock, flags); |
| 207 | return err; |
| 208 | } |
| 209 | |
| 210 | void global_flush_tlb(void) |
Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 211 | { |
| 212 | struct list_head l; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | struct page *pg, *next; |
| 214 | |
| 215 | BUG_ON(irqs_disabled()); |
| 216 | |
| 217 | spin_lock_irq(&cpa_lock); |
Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 218 | list_replace_init(&df_list, &l); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | spin_unlock_irq(&cpa_lock); |
| 220 | flush_map(); |
| 221 | list_for_each_entry_safe(pg, next, &l, lru) |
| 222 | __free_page(pg); |
Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 223 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 226 | void kernel_map_pages(struct page *page, int numpages, int enable) |
| 227 | { |
| 228 | if (PageHighMem(page)) |
| 229 | return; |
Ingo Molnar | de5097c | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 230 | if (!enable) |
Ingo Molnar | f9b8404 | 2006-06-27 02:54:49 -0700 | [diff] [blame] | 231 | debug_check_no_locks_freed(page_address(page), |
| 232 | numpages * PAGE_SIZE); |
Ingo Molnar | de5097c | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | /* the return value is ignored - the calls cannot fail, |
| 235 | * large pages are disabled at boot time. |
| 236 | */ |
| 237 | change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0)); |
| 238 | /* we should perform an IPI and flush all tlbs, |
| 239 | * but that can deadlock->flush only current cpu. |
| 240 | */ |
| 241 | __flush_tlb_all(); |
| 242 | } |
| 243 | #endif |
| 244 | |
| 245 | EXPORT_SYMBOL(change_page_attr); |
| 246 | EXPORT_SYMBOL(global_flush_tlb); |