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 | #include <linux/highmem.h> |
| 6 | #include <linux/module.h> |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 7 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/slab.h> |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 9 | #include <linux/mm.h> |
| 10 | |
Ingo Molnar | 4554ab9 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 11 | void clflush_cache_range(void *addr, int size) |
| 12 | { |
| 13 | int i; |
| 14 | |
| 15 | for (i = 0; i < size; i += boot_cpu_data.x86_clflush_size) |
| 16 | clflush(addr+i); |
| 17 | } |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/processor.h> |
| 20 | #include <asm/tlbflush.h> |
Dave Jones | f8af095 | 2006-01-06 00:12:10 -0800 | [diff] [blame] | 21 | #include <asm/sections.h> |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 22 | #include <asm/uaccess.h> |
| 23 | #include <asm/pgalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Ingo Molnar | f0646e4 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 25 | pte_t *lookup_address(unsigned long address, int *level) |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 26 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | pgd_t *pgd = pgd_offset_k(address); |
| 28 | pud_t *pud; |
| 29 | pmd_t *pmd; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 30 | |
Thomas Gleixner | 30551bb | 2008-01-30 13:34:04 +0100 | [diff] [blame^] | 31 | *level = PG_LEVEL_NONE; |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | if (pgd_none(*pgd)) |
| 34 | return NULL; |
| 35 | pud = pud_offset(pgd, address); |
| 36 | if (pud_none(*pud)) |
| 37 | return NULL; |
| 38 | pmd = pmd_offset(pud, address); |
| 39 | if (pmd_none(*pmd)) |
| 40 | return NULL; |
Thomas Gleixner | 30551bb | 2008-01-30 13:34:04 +0100 | [diff] [blame^] | 41 | |
| 42 | *level = PG_LEVEL_2M; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | if (pmd_large(*pmd)) |
| 44 | return (pte_t *)pmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Thomas Gleixner | 30551bb | 2008-01-30 13:34:04 +0100 | [diff] [blame^] | 46 | *level = PG_LEVEL_4K; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 47 | return pte_offset_kernel(pmd, address); |
| 48 | } |
| 49 | |
Ingo Molnar | 9a3dc78 | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 50 | static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte) |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 51 | { |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 52 | /* change init_mm */ |
| 53 | set_pte_atomic(kpte, pte); |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 54 | #ifdef CONFIG_X86_32 |
Ingo Molnar | e4b71dc | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 55 | if (!SHARED_KERNEL_PMD) { |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 56 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 58 | for (page = pgd_list; page; page = (struct page *)page->index) { |
| 59 | pgd_t *pgd; |
| 60 | pud_t *pud; |
| 61 | pmd_t *pmd; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 62 | |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 63 | pgd = (pgd_t *)page_address(page) + pgd_index(address); |
| 64 | pud = pud_offset(pgd, address); |
| 65 | pmd = pmd_offset(pud, address); |
| 66 | set_pte_atomic((pte_t *)pmd, pte); |
| 67 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 69 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Ingo Molnar | 7afe15b | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 72 | static int split_large_page(pte_t *kpte, unsigned long address) |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 73 | { |
Ingo Molnar | 7afe15b | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 74 | pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte)); |
Ingo Molnar | 12d6f21 | 2008-01-30 13:33:58 +0100 | [diff] [blame] | 75 | gfp_t gfp_flags = GFP_KERNEL; |
Ingo Molnar | 9a3dc78 | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 76 | unsigned long flags; |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 77 | unsigned long addr; |
| 78 | pte_t *pbase, *tmp; |
| 79 | struct page *base; |
Ingo Molnar | 7afe15b | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 80 | int i, level; |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 81 | |
Ingo Molnar | 12d6f21 | 2008-01-30 13:33:58 +0100 | [diff] [blame] | 82 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 83 | gfp_flags = GFP_ATOMIC; |
| 84 | #endif |
| 85 | base = alloc_pages(gfp_flags, 0); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 86 | if (!base) |
| 87 | return -ENOMEM; |
| 88 | |
Ingo Molnar | 9a3dc78 | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 89 | spin_lock_irqsave(&pgd_lock, flags); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 90 | /* |
| 91 | * Check for races, another CPU might have split this page |
| 92 | * up for us already: |
| 93 | */ |
| 94 | tmp = lookup_address(address, &level); |
Ingo Molnar | 5508a74 | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 95 | if (tmp != kpte) { |
| 96 | WARN_ON_ONCE(1); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 97 | goto out_unlock; |
Ingo Molnar | 5508a74 | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 98 | } |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 99 | |
| 100 | address = __pa(address); |
| 101 | addr = address & LARGE_PAGE_MASK; |
| 102 | pbase = (pte_t *)page_address(base); |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 103 | #ifdef CONFIG_X86_32 |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 104 | paravirt_alloc_pt(&init_mm, page_to_pfn(base)); |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 105 | #endif |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 106 | |
| 107 | for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE) |
| 108 | set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot)); |
| 109 | |
| 110 | /* |
| 111 | * Install the new, split up pagetable: |
| 112 | */ |
Ingo Molnar | 9a3dc78 | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 113 | __set_pmd_pte(kpte, address, mk_pte(base, ref_prot)); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 114 | base = NULL; |
| 115 | |
| 116 | out_unlock: |
Ingo Molnar | 9a3dc78 | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 117 | spin_unlock_irqrestore(&pgd_lock, flags); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 118 | |
| 119 | if (base) |
| 120 | __free_pages(base, 0); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 125 | static int |
| 126 | __change_page_attr(unsigned long address, struct page *page, pgprot_t prot) |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 127 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | struct page *kpte_page; |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 129 | int level, err = 0; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 130 | pte_t *kpte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
| 132 | BUG_ON(PageHighMem(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Ingo Molnar | 97f99fe | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 134 | repeat: |
Ingo Molnar | f0646e4 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 135 | kpte = lookup_address(address, &level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | if (!kpte) |
| 137 | return -EINVAL; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | kpte_page = virt_to_page(kpte); |
Andi Kleen | 65d2f0b | 2007-07-21 17:09:51 +0200 | [diff] [blame] | 140 | BUG_ON(PageLRU(kpte_page)); |
| 141 | BUG_ON(PageCompound(kpte_page)); |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | /* |
Ingo Molnar | 78c94ab | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 144 | * Better fail early if someone sets the kernel text to NX. |
| 145 | * Does not cover __inittext |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | */ |
Ingo Molnar | 78c94ab | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 147 | BUG_ON(address >= (unsigned long)&_text && |
| 148 | address < (unsigned long)&_etext && |
| 149 | (pgprot_val(prot) & _PAGE_NX)); |
Andi Kleen | 65d2f0b | 2007-07-21 17:09:51 +0200 | [diff] [blame] | 150 | |
Thomas Gleixner | 30551bb | 2008-01-30 13:34:04 +0100 | [diff] [blame^] | 151 | if (level == PG_LEVEL_4K) { |
Ingo Molnar | 7afe15b | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 152 | set_pte_atomic(kpte, mk_pte(page, canon_pgprot(prot))); |
Ingo Molnar | 78c94ab | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 153 | } else { |
Ingo Molnar | 7afe15b | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 154 | err = split_large_page(kpte, address); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 155 | if (!err) |
| 156 | goto repeat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 158 | return err; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 159 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 161 | /** |
| 162 | * change_page_attr_addr - Change page table attributes in linear mapping |
| 163 | * @address: Virtual address in linear mapping. |
| 164 | * @numpages: Number of pages to change |
| 165 | * @prot: New page table attribute (PAGE_*) |
| 166 | * |
| 167 | * Change page attributes of a page in the direct mapping. This is a variant |
| 168 | * of change_page_attr() that also works on memory holes that do not have |
| 169 | * mem_map entry (pfn_valid() is false). |
| 170 | * |
| 171 | * See change_page_attr() documentation for more details. |
| 172 | */ |
| 173 | |
| 174 | int change_page_attr_addr(unsigned long address, int numpages, pgprot_t prot) |
| 175 | { |
| 176 | int err = 0, kernel_map = 0, i; |
| 177 | |
| 178 | #ifdef CONFIG_X86_64 |
| 179 | if (address >= __START_KERNEL_map && |
| 180 | address < __START_KERNEL_map + KERNEL_TEXT_SIZE) { |
| 181 | |
| 182 | address = (unsigned long)__va(__pa(address)); |
| 183 | kernel_map = 1; |
| 184 | } |
| 185 | #endif |
| 186 | |
| 187 | for (i = 0; i < numpages; i++, address += PAGE_SIZE) { |
| 188 | unsigned long pfn = __pa(address) >> PAGE_SHIFT; |
| 189 | |
| 190 | if (!kernel_map || pte_present(pfn_pte(0, prot))) { |
| 191 | err = __change_page_attr(address, pfn_to_page(pfn), prot); |
| 192 | if (err) |
| 193 | break; |
| 194 | } |
| 195 | #ifdef CONFIG_X86_64 |
| 196 | /* |
| 197 | * Handle kernel mapping too which aliases part of |
| 198 | * lowmem: |
| 199 | */ |
| 200 | if (__pa(address) < KERNEL_TEXT_SIZE) { |
| 201 | unsigned long addr2; |
| 202 | pgprot_t prot2; |
| 203 | |
| 204 | addr2 = __START_KERNEL_map + __pa(address); |
| 205 | /* Make sure the kernel mappings stay executable */ |
| 206 | prot2 = pte_pgprot(pte_mkexec(pfn_pte(0, prot))); |
| 207 | err = __change_page_attr(addr2, pfn_to_page(pfn), prot2); |
| 208 | } |
| 209 | #endif |
| 210 | } |
| 211 | |
| 212 | return err; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * change_page_attr - Change page table attributes in the linear mapping. |
| 217 | * @page: First page to change |
| 218 | * @numpages: Number of pages to change |
| 219 | * @prot: New protection/caching type (PAGE_*) |
| 220 | * |
| 221 | * Returns 0 on success, otherwise a negated errno. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | * |
| 223 | * This should be used when a page is mapped with a different caching policy |
| 224 | * than write-back somewhere - some CPUs do not like it when mappings with |
| 225 | * different caching policies exist. This changes the page attributes of the |
| 226 | * in kernel linear mapping too. |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 227 | * |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 228 | * Caller must call global_flush_tlb() later to make the changes active. |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 229 | * |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 230 | * The caller needs to ensure that there are no conflicting mappings elsewhere |
| 231 | * (e.g. in user space) * This function only deals with the kernel linear map. |
| 232 | * |
| 233 | * For MMIO areas without mem_map use change_page_attr_addr() instead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | */ |
| 235 | int change_page_attr(struct page *page, int numpages, pgprot_t prot) |
| 236 | { |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 237 | unsigned long addr = (unsigned long)page_address(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 239 | return change_page_attr_addr(addr, numpages, prot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 241 | EXPORT_SYMBOL(change_page_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Ingo Molnar | 78c94ab | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 243 | static void flush_kernel_map(void *arg) |
| 244 | { |
| 245 | /* |
| 246 | * Flush all to work around Errata in early athlons regarding |
| 247 | * large page flushing. |
| 248 | */ |
| 249 | __flush_tlb_all(); |
| 250 | |
| 251 | if (boot_cpu_data.x86_model >= 4) |
| 252 | wbinvd(); |
| 253 | } |
| 254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | void global_flush_tlb(void) |
Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 256 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | BUG_ON(irqs_disabled()); |
| 258 | |
Ingo Molnar | 78c94ab | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 259 | on_each_cpu(flush_kernel_map, NULL, 1, 1); |
Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 260 | } |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 261 | EXPORT_SYMBOL(global_flush_tlb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
| 263 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 264 | void kernel_map_pages(struct page *page, int numpages, int enable) |
| 265 | { |
| 266 | if (PageHighMem(page)) |
| 267 | return; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 268 | if (!enable) { |
Ingo Molnar | f9b8404 | 2006-06-27 02:54:49 -0700 | [diff] [blame] | 269 | debug_check_no_locks_freed(page_address(page), |
| 270 | numpages * PAGE_SIZE); |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 271 | } |
Ingo Molnar | de5097c | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 272 | |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 273 | /* |
Ingo Molnar | 12d6f21 | 2008-01-30 13:33:58 +0100 | [diff] [blame] | 274 | * If page allocator is not up yet then do not call c_p_a(): |
| 275 | */ |
| 276 | if (!debug_pagealloc_enabled) |
| 277 | return; |
| 278 | |
| 279 | /* |
Ingo Molnar | e4b71dc | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 280 | * The return value is ignored - the calls cannot fail, |
| 281 | * large pages are disabled at boot time: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | */ |
| 283 | change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0)); |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 284 | |
| 285 | /* |
Ingo Molnar | e4b71dc | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 286 | * We should perform an IPI and flush all tlbs, |
| 287 | * but that can deadlock->flush only current cpu: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | */ |
| 289 | __flush_tlb_all(); |
| 290 | } |
| 291 | #endif |