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