Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2002 Andi Kleen, SuSE Labs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Thanks to Ben LaHaise for precious feedback. |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 4 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/highmem.h> |
| 7 | #include <linux/module.h> |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 8 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/slab.h> |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 10 | #include <linux/mm.h> |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <asm/processor.h> |
| 13 | #include <asm/tlbflush.h> |
Dave Jones | f8af095 | 2006-01-06 00:12:10 -0800 | [diff] [blame] | 14 | #include <asm/sections.h> |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 15 | #include <asm/uaccess.h> |
| 16 | #include <asm/pgalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Ingo Molnar | f0646e4 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 18 | pte_t *lookup_address(unsigned long address, int *level) |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 19 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | pgd_t *pgd = pgd_offset_k(address); |
| 21 | pud_t *pud; |
| 22 | pmd_t *pmd; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | if (pgd_none(*pgd)) |
| 25 | return NULL; |
| 26 | pud = pud_offset(pgd, address); |
| 27 | if (pud_none(*pud)) |
| 28 | return NULL; |
| 29 | pmd = pmd_offset(pud, address); |
| 30 | if (pmd_none(*pmd)) |
| 31 | return NULL; |
Ingo Molnar | f0646e4 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 32 | *level = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | if (pmd_large(*pmd)) |
| 34 | return (pte_t *)pmd; |
Ingo Molnar | f0646e4 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 35 | *level = 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 37 | return pte_offset_kernel(pmd, address); |
| 38 | } |
| 39 | |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 40 | static void set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte) |
| 41 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | unsigned long flags; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 43 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 45 | /* change init_mm */ |
| 46 | set_pte_atomic(kpte, pte); |
Jeremy Fitzhardinge | 5311ab6 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 47 | if (SHARED_KERNEL_PMD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | return; |
| 49 | |
| 50 | spin_lock_irqsave(&pgd_lock, flags); |
| 51 | for (page = pgd_list; page; page = (struct page *)page->index) { |
| 52 | pgd_t *pgd; |
| 53 | pud_t *pud; |
| 54 | pmd_t *pmd; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | pgd = (pgd_t *)page_address(page) + pgd_index(address); |
| 57 | pud = pud_offset(pgd, address); |
| 58 | pmd = pmd_offset(pud, address); |
| 59 | set_pte_atomic((pte_t *)pmd, pte); |
| 60 | } |
| 61 | spin_unlock_irqrestore(&pgd_lock, flags); |
| 62 | } |
| 63 | |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 64 | static int |
| 65 | split_large_page(pte_t *kpte, unsigned long address, pgprot_t ref_prot) |
| 66 | { |
| 67 | int i, level; |
| 68 | unsigned long addr; |
| 69 | pte_t *pbase, *tmp; |
| 70 | struct page *base; |
| 71 | |
| 72 | base = alloc_pages(GFP_KERNEL, 0); |
| 73 | if (!base) |
| 74 | return -ENOMEM; |
| 75 | |
| 76 | down_write(&init_mm.mmap_sem); |
| 77 | /* |
| 78 | * Check for races, another CPU might have split this page |
| 79 | * up for us already: |
| 80 | */ |
| 81 | tmp = lookup_address(address, &level); |
Ingo Molnar | 5508a74 | 2008-01-30 13:33:56 +0100 | [diff] [blame^] | 82 | if (tmp != kpte) { |
| 83 | WARN_ON_ONCE(1); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 84 | goto out_unlock; |
Ingo Molnar | 5508a74 | 2008-01-30 13:33:56 +0100 | [diff] [blame^] | 85 | } |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 86 | |
| 87 | address = __pa(address); |
| 88 | addr = address & LARGE_PAGE_MASK; |
| 89 | pbase = (pte_t *)page_address(base); |
| 90 | paravirt_alloc_pt(&init_mm, page_to_pfn(base)); |
| 91 | |
| 92 | for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE) |
| 93 | set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot)); |
| 94 | |
| 95 | /* |
| 96 | * Install the new, split up pagetable: |
| 97 | */ |
| 98 | set_pmd_pte(kpte, address, mk_pte(base, ref_prot)); |
| 99 | base = NULL; |
| 100 | |
| 101 | out_unlock: |
| 102 | up_write(&init_mm.mmap_sem); |
| 103 | |
| 104 | if (base) |
| 105 | __free_pages(base, 0); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 110 | static int __change_page_attr(struct page *page, pgprot_t prot) |
| 111 | { |
Ingo Molnar | 78c94ab | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 112 | pgprot_t ref_prot = PAGE_KERNEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | struct page *kpte_page; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 114 | unsigned long address; |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 115 | int level, err = 0; |
Ingo Molnar | 78c94ab | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 116 | pgprot_t oldprot; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 117 | pte_t *kpte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
| 119 | BUG_ON(PageHighMem(page)); |
| 120 | address = (unsigned long)page_address(page); |
| 121 | |
Ingo Molnar | 97f99fe | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 122 | repeat: |
Ingo Molnar | f0646e4 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 123 | kpte = lookup_address(address, &level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | if (!kpte) |
| 125 | return -EINVAL; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 126 | |
Ingo Molnar | 78c94ab | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 127 | oldprot = pte_pgprot(*kpte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | kpte_page = virt_to_page(kpte); |
Andi Kleen | 65d2f0b | 2007-07-21 17:09:51 +0200 | [diff] [blame] | 129 | BUG_ON(PageLRU(kpte_page)); |
| 130 | BUG_ON(PageCompound(kpte_page)); |
| 131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | /* |
Ingo Molnar | 78c94ab | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 133 | * Better fail early if someone sets the kernel text to NX. |
| 134 | * Does not cover __inittext |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | */ |
Ingo Molnar | 78c94ab | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 136 | BUG_ON(address >= (unsigned long)&_text && |
| 137 | address < (unsigned long)&_etext && |
| 138 | (pgprot_val(prot) & _PAGE_NX)); |
Andi Kleen | 65d2f0b | 2007-07-21 17:09:51 +0200 | [diff] [blame] | 139 | |
Ingo Molnar | 78c94ab | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 140 | if ((address & LARGE_PAGE_MASK) < (unsigned long)&_etext) |
| 141 | ref_prot = PAGE_KERNEL_EXEC; |
| 142 | |
| 143 | ref_prot = canon_pgprot(ref_prot); |
| 144 | prot = canon_pgprot(prot); |
| 145 | |
| 146 | if (level == 3) { |
| 147 | set_pte_atomic(kpte, mk_pte(page, prot)); |
| 148 | } else { |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 149 | err = split_large_page(kpte, address, ref_prot); |
| 150 | if (!err) |
| 151 | goto repeat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 153 | return err; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 154 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | /* |
| 157 | * Change the page attributes of an page in the linear mapping. |
| 158 | * |
| 159 | * This should be used when a page is mapped with a different caching policy |
| 160 | * than write-back somewhere - some CPUs do not like it when mappings with |
| 161 | * different caching policies exist. This changes the page attributes of the |
| 162 | * in kernel linear mapping too. |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 163 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | * The caller needs to ensure that there are no conflicting mappings elsewhere. |
| 165 | * This function only deals with the kernel linear map. |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 166 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | * Caller must call global_flush_tlb() after this. |
| 168 | */ |
| 169 | int change_page_attr(struct page *page, int numpages, pgprot_t prot) |
| 170 | { |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 171 | int err = 0, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 173 | for (i = 0; i < numpages; i++, page++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | err = __change_page_attr(page, prot); |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 175 | if (err) |
| 176 | break; |
| 177 | } |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | return err; |
| 180 | } |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 181 | EXPORT_SYMBOL(change_page_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Ingo Molnar | 78c94ab | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 183 | int change_page_attr_addr(unsigned long addr, int numpages, pgprot_t prot) |
| 184 | { |
| 185 | int i; |
Ingo Molnar | 5508a74 | 2008-01-30 13:33:56 +0100 | [diff] [blame^] | 186 | unsigned long pfn = (__pa(addr) >> PAGE_SHIFT); |
Ingo Molnar | 78c94ab | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 187 | |
| 188 | for (i = 0; i < numpages; i++) { |
| 189 | if (!pfn_valid(pfn + i)) { |
Ingo Molnar | 5508a74 | 2008-01-30 13:33:56 +0100 | [diff] [blame^] | 190 | WARN_ON_ONCE(1); |
Ingo Molnar | 78c94ab | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 191 | break; |
| 192 | } else { |
| 193 | int level; |
| 194 | pte_t *pte = lookup_address(addr + i*PAGE_SIZE, &level); |
Ingo Molnar | 5508a74 | 2008-01-30 13:33:56 +0100 | [diff] [blame^] | 195 | BUG_ON(pte && pte_none(*pte)); |
Ingo Molnar | 78c94ab | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 196 | } |
| 197 | } |
Ingo Molnar | 5508a74 | 2008-01-30 13:33:56 +0100 | [diff] [blame^] | 198 | |
Ingo Molnar | 78c94ab | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 199 | return change_page_attr(virt_to_page(addr), i, prot); |
| 200 | } |
| 201 | |
| 202 | static void flush_kernel_map(void *arg) |
| 203 | { |
| 204 | /* |
| 205 | * Flush all to work around Errata in early athlons regarding |
| 206 | * large page flushing. |
| 207 | */ |
| 208 | __flush_tlb_all(); |
| 209 | |
| 210 | if (boot_cpu_data.x86_model >= 4) |
| 211 | wbinvd(); |
| 212 | } |
| 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | void global_flush_tlb(void) |
Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 215 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | BUG_ON(irqs_disabled()); |
| 217 | |
Ingo Molnar | 78c94ab | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 218 | on_each_cpu(flush_kernel_map, NULL, 1, 1); |
Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 219 | } |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 220 | EXPORT_SYMBOL(global_flush_tlb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
| 222 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 223 | void kernel_map_pages(struct page *page, int numpages, int enable) |
| 224 | { |
| 225 | if (PageHighMem(page)) |
| 226 | return; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 227 | if (!enable) { |
Ingo Molnar | f9b8404 | 2006-06-27 02:54:49 -0700 | [diff] [blame] | 228 | debug_check_no_locks_freed(page_address(page), |
| 229 | numpages * PAGE_SIZE); |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 230 | } |
Ingo Molnar | de5097c | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 231 | |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 232 | /* |
| 233 | * the return value is ignored - the calls cannot fail, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | * large pages are disabled at boot time. |
| 235 | */ |
| 236 | change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0)); |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 237 | |
| 238 | /* |
| 239 | * we should perform an IPI and flush all tlbs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | * but that can deadlock->flush only current cpu. |
| 241 | */ |
| 242 | __flush_tlb_all(); |
| 243 | } |
| 244 | #endif |