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> |
Ingo Molnar | 8192206 | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 6 | #include <linux/bootmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Thomas Gleixner | 950f9d9 | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 12 | #include <asm/e820.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <asm/processor.h> |
| 14 | #include <asm/tlbflush.h> |
Dave Jones | f8af095 | 2006-01-06 00:12:10 -0800 | [diff] [blame] | 15 | #include <asm/sections.h> |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 16 | #include <asm/uaccess.h> |
| 17 | #include <asm/pgalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Ingo Molnar | 9df8499 | 2008-02-04 16:48:09 +0100 | [diff] [blame^] | 19 | /* |
| 20 | * The current flushing context - we pass it instead of 5 arguments: |
| 21 | */ |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 22 | struct cpa_data { |
| 23 | unsigned long vaddr; |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 24 | pgprot_t mask_set; |
| 25 | pgprot_t mask_clr; |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 26 | int numpages; |
Thomas Gleixner | f4ae5da | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 27 | int flushtlb; |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 28 | }; |
| 29 | |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 30 | enum { |
| 31 | CPA_NO_SPLIT = 0, |
| 32 | CPA_SPLIT, |
| 33 | }; |
| 34 | |
Arjan van de Ven | ed724be | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 35 | static inline int |
| 36 | within(unsigned long addr, unsigned long start, unsigned long end) |
Ingo Molnar | 687c482 | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 37 | { |
Arjan van de Ven | ed724be | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 38 | return addr >= start && addr < end; |
| 39 | } |
| 40 | |
| 41 | /* |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 42 | * Flushing functions |
| 43 | */ |
Thomas Gleixner | cd8ddf1 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 44 | |
Thomas Gleixner | cd8ddf1 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 45 | /** |
| 46 | * clflush_cache_range - flush a cache range with clflush |
| 47 | * @addr: virtual start address |
| 48 | * @size: number of bytes to flush |
| 49 | * |
| 50 | * clflush is an unordered instruction which needs fencing with mfence |
| 51 | * to avoid ordering issues. |
| 52 | */ |
Ingo Molnar | 4c61afc | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 53 | void clflush_cache_range(void *vaddr, unsigned int size) |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 54 | { |
Ingo Molnar | 4c61afc | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 55 | void *vend = vaddr + size - 1; |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 56 | |
Thomas Gleixner | cd8ddf1 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 57 | mb(); |
Ingo Molnar | 4c61afc | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 58 | |
| 59 | for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size) |
| 60 | clflush(vaddr); |
| 61 | /* |
| 62 | * Flush any possible final partial cacheline: |
| 63 | */ |
| 64 | clflush(vend); |
| 65 | |
Thomas Gleixner | cd8ddf1 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 66 | mb(); |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Thomas Gleixner | af1e684 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 69 | static void __cpa_flush_all(void *arg) |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 70 | { |
Andi Kleen | 6bb8383 | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 71 | unsigned long cache = (unsigned long)arg; |
| 72 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 73 | /* |
| 74 | * Flush all to work around Errata in early athlons regarding |
| 75 | * large page flushing. |
| 76 | */ |
| 77 | __flush_tlb_all(); |
| 78 | |
Andi Kleen | 6bb8383 | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 79 | if (cache && boot_cpu_data.x86_model >= 4) |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 80 | wbinvd(); |
| 81 | } |
| 82 | |
Andi Kleen | 6bb8383 | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 83 | static void cpa_flush_all(unsigned long cache) |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 84 | { |
| 85 | BUG_ON(irqs_disabled()); |
| 86 | |
Andi Kleen | 6bb8383 | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 87 | on_each_cpu(__cpa_flush_all, (void *) cache, 1, 1); |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Thomas Gleixner | 57a6a46 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 90 | static void __cpa_flush_range(void *arg) |
| 91 | { |
Thomas Gleixner | 57a6a46 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 92 | /* |
| 93 | * We could optimize that further and do individual per page |
| 94 | * tlb invalidates for a low number of pages. Caveat: we must |
| 95 | * flush the high aliases on 64bit as well. |
| 96 | */ |
| 97 | __flush_tlb_all(); |
Thomas Gleixner | 57a6a46 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Andi Kleen | 6bb8383 | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 100 | static void cpa_flush_range(unsigned long start, int numpages, int cache) |
Thomas Gleixner | 57a6a46 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 101 | { |
Ingo Molnar | 4c61afc | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 102 | unsigned int i, level; |
| 103 | unsigned long addr; |
| 104 | |
Thomas Gleixner | 57a6a46 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 105 | BUG_ON(irqs_disabled()); |
Ingo Molnar | 4c61afc | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 106 | WARN_ON(PAGE_ALIGN(start) != start); |
Thomas Gleixner | 57a6a46 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 107 | |
Thomas Gleixner | 3b233e5 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 108 | on_each_cpu(__cpa_flush_range, NULL, 1, 1); |
Thomas Gleixner | 57a6a46 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 109 | |
Andi Kleen | 6bb8383 | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 110 | if (!cache) |
| 111 | return; |
| 112 | |
Thomas Gleixner | 3b233e5 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 113 | /* |
| 114 | * We only need to flush on one CPU, |
| 115 | * clflush is a MESI-coherent instruction that |
| 116 | * will cause all other CPUs to flush the same |
| 117 | * cachelines: |
| 118 | */ |
Ingo Molnar | 4c61afc | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 119 | for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) { |
| 120 | pte_t *pte = lookup_address(addr, &level); |
| 121 | |
| 122 | /* |
| 123 | * Only flush present addresses: |
| 124 | */ |
Thomas Gleixner | 7bfb72e | 2008-02-04 16:48:08 +0100 | [diff] [blame] | 125 | if (pte && (pte_val(*pte) & _PAGE_PRESENT)) |
Ingo Molnar | 4c61afc | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 126 | clflush_cache_range((void *) addr, PAGE_SIZE); |
| 127 | } |
Thomas Gleixner | 57a6a46 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Arjan van de Ven | cc0f21b | 2008-02-04 16:48:05 +0100 | [diff] [blame] | 130 | #define HIGH_MAP_START __START_KERNEL_map |
| 131 | #define HIGH_MAP_END (__START_KERNEL_map + KERNEL_TEXT_SIZE) |
| 132 | |
| 133 | |
| 134 | /* |
| 135 | * Converts a virtual address to a X86-64 highmap address |
| 136 | */ |
| 137 | static unsigned long virt_to_highmap(void *address) |
| 138 | { |
| 139 | #ifdef CONFIG_X86_64 |
| 140 | return __pa((unsigned long)address) + HIGH_MAP_START - phys_base; |
| 141 | #else |
| 142 | return (unsigned long)address; |
| 143 | #endif |
| 144 | } |
| 145 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 146 | /* |
Arjan van de Ven | ed724be | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 147 | * Certain areas of memory on x86 require very specific protection flags, |
| 148 | * for example the BIOS area or kernel text. Callers don't always get this |
| 149 | * right (again, ioremap() on BIOS memory is not uncommon) so this function |
| 150 | * checks and fixes these known static required protection bits. |
| 151 | */ |
| 152 | static inline pgprot_t static_protections(pgprot_t prot, unsigned long address) |
| 153 | { |
| 154 | pgprot_t forbidden = __pgprot(0); |
| 155 | |
Ingo Molnar | 687c482 | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 156 | /* |
Arjan van de Ven | ed724be | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 157 | * The BIOS area between 640k and 1Mb needs to be executable for |
| 158 | * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support. |
Ingo Molnar | 687c482 | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 159 | */ |
Arjan van de Ven | ed724be | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 160 | if (within(__pa(address), BIOS_BEGIN, BIOS_END)) |
| 161 | pgprot_val(forbidden) |= _PAGE_NX; |
| 162 | |
| 163 | /* |
| 164 | * The kernel text needs to be executable for obvious reasons |
| 165 | * Does not cover __inittext since that is gone later on |
| 166 | */ |
| 167 | if (within(address, (unsigned long)_text, (unsigned long)_etext)) |
| 168 | pgprot_val(forbidden) |= _PAGE_NX; |
Arjan van de Ven | cc0f21b | 2008-02-04 16:48:05 +0100 | [diff] [blame] | 169 | /* |
| 170 | * Do the same for the x86-64 high kernel mapping |
| 171 | */ |
| 172 | if (within(address, virt_to_highmap(_text), virt_to_highmap(_etext))) |
| 173 | pgprot_val(forbidden) |= _PAGE_NX; |
| 174 | |
Arjan van de Ven | ed724be | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 175 | |
| 176 | #ifdef CONFIG_DEBUG_RODATA |
| 177 | /* The .rodata section needs to be read-only */ |
| 178 | if (within(address, (unsigned long)__start_rodata, |
| 179 | (unsigned long)__end_rodata)) |
| 180 | pgprot_val(forbidden) |= _PAGE_RW; |
Arjan van de Ven | cc0f21b | 2008-02-04 16:48:05 +0100 | [diff] [blame] | 181 | /* |
| 182 | * Do the same for the x86-64 high kernel mapping |
| 183 | */ |
| 184 | if (within(address, virt_to_highmap(__start_rodata), |
| 185 | virt_to_highmap(__end_rodata))) |
| 186 | pgprot_val(forbidden) |= _PAGE_RW; |
Arjan van de Ven | ed724be | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 187 | #endif |
| 188 | |
| 189 | prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden)); |
Ingo Molnar | 687c482 | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 190 | |
| 191 | return prot; |
| 192 | } |
| 193 | |
Thomas Gleixner | 9a14aef | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 194 | /* |
| 195 | * Lookup the page table entry for a virtual address. Return a pointer |
| 196 | * to the entry and the level of the mapping. |
| 197 | * |
| 198 | * Note: We return pud and pmd either when the entry is marked large |
| 199 | * or when the present bit is not set. Otherwise we would return a |
| 200 | * pointer to a nonexisting mapping. |
| 201 | */ |
Ingo Molnar | f0646e4 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 202 | pte_t *lookup_address(unsigned long address, int *level) |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 203 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | pgd_t *pgd = pgd_offset_k(address); |
| 205 | pud_t *pud; |
| 206 | pmd_t *pmd; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 207 | |
Thomas Gleixner | 30551bb | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 208 | *level = PG_LEVEL_NONE; |
| 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | if (pgd_none(*pgd)) |
| 211 | return NULL; |
Ingo Molnar | 9df8499 | 2008-02-04 16:48:09 +0100 | [diff] [blame^] | 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | pud = pud_offset(pgd, address); |
| 214 | if (pud_none(*pud)) |
| 215 | return NULL; |
Andi Kleen | c2f71ee | 2008-02-04 16:48:09 +0100 | [diff] [blame] | 216 | |
| 217 | *level = PG_LEVEL_1G; |
| 218 | if (pud_large(*pud) || !pud_present(*pud)) |
| 219 | return (pte_t *)pud; |
| 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | pmd = pmd_offset(pud, address); |
| 222 | if (pmd_none(*pmd)) |
| 223 | return NULL; |
Thomas Gleixner | 30551bb | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 224 | |
| 225 | *level = PG_LEVEL_2M; |
Thomas Gleixner | 9a14aef | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 226 | if (pmd_large(*pmd) || !pmd_present(*pmd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | return (pte_t *)pmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Thomas Gleixner | 30551bb | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 229 | *level = PG_LEVEL_4K; |
Ingo Molnar | 9df8499 | 2008-02-04 16:48:09 +0100 | [diff] [blame^] | 230 | |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 231 | return pte_offset_kernel(pmd, address); |
| 232 | } |
| 233 | |
Ingo Molnar | 9df8499 | 2008-02-04 16:48:09 +0100 | [diff] [blame^] | 234 | /* |
| 235 | * Set the new pmd in all the pgds we know about: |
| 236 | */ |
Ingo Molnar | 9a3dc78 | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 237 | 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] | 238 | { |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 239 | /* change init_mm */ |
| 240 | set_pte_atomic(kpte, pte); |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 241 | #ifdef CONFIG_X86_32 |
Ingo Molnar | e4b71dc | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 242 | if (!SHARED_KERNEL_PMD) { |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 243 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Jeremy Fitzhardinge | e3ed910 | 2008-01-30 13:34:11 +0100 | [diff] [blame] | 245 | list_for_each_entry(page, &pgd_list, lru) { |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 246 | pgd_t *pgd; |
| 247 | pud_t *pud; |
| 248 | pmd_t *pmd; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 249 | |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 250 | pgd = (pgd_t *)page_address(page) + pgd_index(address); |
| 251 | pud = pud_offset(pgd, address); |
| 252 | pmd = pmd_offset(pud, address); |
| 253 | set_pte_atomic((pte_t *)pmd, pte); |
| 254 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 256 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Ingo Molnar | 9df8499 | 2008-02-04 16:48:09 +0100 | [diff] [blame^] | 259 | static int |
| 260 | try_preserve_large_page(pte_t *kpte, unsigned long address, |
| 261 | struct cpa_data *cpa) |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 262 | { |
| 263 | unsigned long nextpage_addr, numpages, pmask, psize, flags; |
| 264 | pte_t new_pte, old_pte, *tmp; |
| 265 | pgprot_t old_prot, new_prot; |
| 266 | int level, res = CPA_SPLIT; |
| 267 | |
Ingo Molnar | 34508f6 | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 268 | /* |
| 269 | * An Athlon 64 X2 showed hard hangs if we tried to preserve |
| 270 | * largepages and changed the PSE entry from RW to RO. |
| 271 | * |
| 272 | * As AMD CPUs have a long series of erratas in this area, |
| 273 | * (and none of the known ones seem to explain this hang), |
| 274 | * disable this code until the hang can be debugged: |
| 275 | */ |
| 276 | if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) |
| 277 | return res; |
| 278 | |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 279 | spin_lock_irqsave(&pgd_lock, flags); |
| 280 | /* |
| 281 | * Check for races, another CPU might have split this page |
| 282 | * up already: |
| 283 | */ |
| 284 | tmp = lookup_address(address, &level); |
| 285 | if (tmp != kpte) |
| 286 | goto out_unlock; |
| 287 | |
| 288 | switch (level) { |
| 289 | case PG_LEVEL_2M: |
Andi Kleen | 31422c5 | 2008-02-04 16:48:08 +0100 | [diff] [blame] | 290 | psize = PMD_PAGE_SIZE; |
| 291 | pmask = PMD_PAGE_MASK; |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 292 | break; |
Andi Kleen | f07333f | 2008-02-04 16:48:09 +0100 | [diff] [blame] | 293 | #ifdef CONFIG_X86_64 |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 294 | case PG_LEVEL_1G: |
Andi Kleen | f07333f | 2008-02-04 16:48:09 +0100 | [diff] [blame] | 295 | psize = PMD_PAGE_SIZE; |
| 296 | pmask = PMD_PAGE_MASK; |
| 297 | break; |
| 298 | #endif |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 299 | default: |
| 300 | res = -EINVAL; |
| 301 | goto out_unlock; |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * Calculate the number of pages, which fit into this large |
| 306 | * page starting at address: |
| 307 | */ |
| 308 | nextpage_addr = (address + psize) & pmask; |
| 309 | numpages = (nextpage_addr - address) >> PAGE_SHIFT; |
| 310 | if (numpages < cpa->numpages) |
| 311 | cpa->numpages = numpages; |
| 312 | |
| 313 | /* |
| 314 | * We are safe now. Check whether the new pgprot is the same: |
| 315 | */ |
| 316 | old_pte = *kpte; |
| 317 | old_prot = new_prot = pte_pgprot(old_pte); |
| 318 | |
| 319 | pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr); |
| 320 | pgprot_val(new_prot) |= pgprot_val(cpa->mask_set); |
| 321 | new_prot = static_protections(new_prot, address); |
| 322 | |
| 323 | /* |
| 324 | * If there are no changes, return. maxpages has been updated |
| 325 | * above: |
| 326 | */ |
| 327 | if (pgprot_val(new_prot) == pgprot_val(old_prot)) { |
| 328 | res = CPA_NO_SPLIT; |
| 329 | goto out_unlock; |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * We need to change the attributes. Check, whether we can |
| 334 | * change the large page in one go. We request a split, when |
| 335 | * the address is not aligned and the number of pages is |
| 336 | * smaller than the number of pages in the large page. Note |
| 337 | * that we limited the number of possible pages already to |
| 338 | * the number of pages in the large page. |
| 339 | */ |
| 340 | if (address == (nextpage_addr - psize) && cpa->numpages == numpages) { |
| 341 | /* |
| 342 | * The address is aligned and the number of pages |
| 343 | * covers the full page. |
| 344 | */ |
| 345 | new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot)); |
| 346 | __set_pmd_pte(kpte, address, new_pte); |
| 347 | cpa->flushtlb = 1; |
| 348 | res = CPA_NO_SPLIT; |
| 349 | } |
| 350 | |
| 351 | out_unlock: |
| 352 | spin_unlock_irqrestore(&pgd_lock, flags); |
Ingo Molnar | 9df8499 | 2008-02-04 16:48:09 +0100 | [diff] [blame^] | 353 | |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 354 | return res; |
| 355 | } |
| 356 | |
Ingo Molnar | 7afe15b | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 357 | static int split_large_page(pte_t *kpte, unsigned long address) |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 358 | { |
Andi Kleen | f07333f | 2008-02-04 16:48:09 +0100 | [diff] [blame] | 359 | unsigned long flags, addr, pfn, pfninc = 1; |
Ingo Molnar | 9df8499 | 2008-02-04 16:48:09 +0100 | [diff] [blame^] | 360 | gfp_t gfp_flags = GFP_KERNEL; |
Ingo Molnar | 86f03989d | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 361 | unsigned int i, level; |
Ingo Molnar | 9df8499 | 2008-02-04 16:48:09 +0100 | [diff] [blame^] | 362 | pte_t *pbase, *tmp; |
| 363 | pgprot_t ref_prot; |
| 364 | struct page *base; |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 365 | |
Ingo Molnar | 12d6f21 | 2008-01-30 13:33:58 +0100 | [diff] [blame] | 366 | #ifdef CONFIG_DEBUG_PAGEALLOC |
Ingo Molnar | 86f03989d | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 367 | gfp_flags = GFP_ATOMIC | __GFP_NOWARN; |
Ingo Molnar | 12d6f21 | 2008-01-30 13:33:58 +0100 | [diff] [blame] | 368 | #endif |
| 369 | base = alloc_pages(gfp_flags, 0); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 370 | if (!base) |
| 371 | return -ENOMEM; |
| 372 | |
Ingo Molnar | 9a3dc78 | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 373 | spin_lock_irqsave(&pgd_lock, flags); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 374 | /* |
| 375 | * Check for races, another CPU might have split this page |
| 376 | * up for us already: |
| 377 | */ |
| 378 | tmp = lookup_address(address, &level); |
Ingo Molnar | 6ce9fc1 | 2008-02-04 16:48:08 +0100 | [diff] [blame] | 379 | if (tmp != kpte) |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 380 | goto out_unlock; |
| 381 | |
| 382 | address = __pa(address); |
Andi Kleen | 31422c5 | 2008-02-04 16:48:08 +0100 | [diff] [blame] | 383 | addr = address & PMD_PAGE_MASK; |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 384 | pbase = (pte_t *)page_address(base); |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 385 | #ifdef CONFIG_X86_32 |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 386 | paravirt_alloc_pt(&init_mm, page_to_pfn(base)); |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 387 | #endif |
Thomas Gleixner | 07cf89c | 2008-02-04 16:48:08 +0100 | [diff] [blame] | 388 | ref_prot = pte_pgprot(pte_clrhuge(*kpte)); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 389 | |
Andi Kleen | f07333f | 2008-02-04 16:48:09 +0100 | [diff] [blame] | 390 | #ifdef CONFIG_X86_64 |
| 391 | if (level == PG_LEVEL_1G) { |
| 392 | pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT; |
| 393 | pgprot_val(ref_prot) |= _PAGE_PSE; |
| 394 | addr &= PUD_PAGE_MASK; |
| 395 | } |
| 396 | #endif |
| 397 | |
Thomas Gleixner | 63c1dcf | 2008-02-04 16:48:05 +0100 | [diff] [blame] | 398 | /* |
| 399 | * Get the target pfn from the original entry: |
| 400 | */ |
| 401 | pfn = pte_pfn(*kpte); |
Andi Kleen | f07333f | 2008-02-04 16:48:09 +0100 | [diff] [blame] | 402 | for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc) |
Thomas Gleixner | 63c1dcf | 2008-02-04 16:48:05 +0100 | [diff] [blame] | 403 | set_pte(&pbase[i], pfn_pte(pfn, ref_prot)); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 404 | |
| 405 | /* |
Thomas Gleixner | 07cf89c | 2008-02-04 16:48:08 +0100 | [diff] [blame] | 406 | * Install the new, split up pagetable. Important details here: |
Huang, Ying | 4c881ca | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 407 | * |
| 408 | * On Intel the NX bit of all levels must be cleared to make a |
| 409 | * page executable. See section 4.13.2 of Intel 64 and IA-32 |
| 410 | * Architectures Software Developer's Manual). |
Thomas Gleixner | 07cf89c | 2008-02-04 16:48:08 +0100 | [diff] [blame] | 411 | * |
| 412 | * Mark the entry present. The current mapping might be |
| 413 | * set to not present, which we preserved above. |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 414 | */ |
Huang, Ying | 4c881ca | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 415 | ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte))); |
Thomas Gleixner | 07cf89c | 2008-02-04 16:48:08 +0100 | [diff] [blame] | 416 | pgprot_val(ref_prot) |= _PAGE_PRESENT; |
Ingo Molnar | 9a3dc78 | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 417 | __set_pmd_pte(kpte, address, mk_pte(base, ref_prot)); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 418 | base = NULL; |
| 419 | |
| 420 | out_unlock: |
Ingo Molnar | 9a3dc78 | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 421 | spin_unlock_irqrestore(&pgd_lock, flags); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 422 | |
| 423 | if (base) |
| 424 | __free_pages(base, 0); |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 429 | static int __change_page_attr(unsigned long address, struct cpa_data *cpa) |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 430 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | struct page *kpte_page; |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 432 | int level, res; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 433 | pte_t *kpte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Ingo Molnar | 97f99fe | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 435 | repeat: |
Ingo Molnar | f0646e4 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 436 | kpte = lookup_address(address, &level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | if (!kpte) |
| 438 | return -EINVAL; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | kpte_page = virt_to_page(kpte); |
Andi Kleen | 65d2f0b | 2007-07-21 17:09:51 +0200 | [diff] [blame] | 441 | BUG_ON(PageLRU(kpte_page)); |
| 442 | BUG_ON(PageCompound(kpte_page)); |
| 443 | |
Thomas Gleixner | 30551bb | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 444 | if (level == PG_LEVEL_4K) { |
Ingo Molnar | 86f03989d | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 445 | pte_t new_pte, old_pte = *kpte; |
Arjan van de Ven | 626c2c9 | 2008-02-04 16:48:05 +0100 | [diff] [blame] | 446 | pgprot_t new_prot = pte_pgprot(old_pte); |
| 447 | |
| 448 | if(!pte_val(old_pte)) { |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 449 | printk(KERN_WARNING "CPA: called for zero pte. " |
| 450 | "vaddr = %lx cpa->vaddr = %lx\n", address, |
| 451 | cpa->vaddr); |
| 452 | WARN_ON(1); |
Arjan van de Ven | 626c2c9 | 2008-02-04 16:48:05 +0100 | [diff] [blame] | 453 | return -EINVAL; |
| 454 | } |
Thomas Gleixner | a72a08a | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 455 | |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 456 | pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr); |
| 457 | pgprot_val(new_prot) |= pgprot_val(cpa->mask_set); |
Ingo Molnar | 86f03989d | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 458 | |
| 459 | new_prot = static_protections(new_prot, address); |
| 460 | |
Arjan van de Ven | 626c2c9 | 2008-02-04 16:48:05 +0100 | [diff] [blame] | 461 | /* |
| 462 | * We need to keep the pfn from the existing PTE, |
| 463 | * after all we're only going to change it's attributes |
| 464 | * not the memory it points to |
| 465 | */ |
| 466 | new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot)); |
Thomas Gleixner | f4ae5da | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 467 | |
| 468 | /* |
| 469 | * Do we really change anything ? |
| 470 | */ |
| 471 | if (pte_val(old_pte) != pte_val(new_pte)) { |
| 472 | set_pte_atomic(kpte, new_pte); |
| 473 | cpa->flushtlb = 1; |
| 474 | } |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 475 | cpa->numpages = 1; |
| 476 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 478 | |
| 479 | /* |
| 480 | * Check, whether we can keep the large page intact |
| 481 | * and just change the pte: |
| 482 | */ |
| 483 | res = try_preserve_large_page(kpte, address, cpa); |
| 484 | if (res < 0) |
| 485 | return res; |
| 486 | |
| 487 | /* |
| 488 | * When the range fits into the existing large page, |
| 489 | * return. cp->numpages and cpa->tlbflush have been updated in |
| 490 | * try_large_page: |
| 491 | */ |
| 492 | if (res == CPA_NO_SPLIT) |
| 493 | return 0; |
| 494 | |
| 495 | /* |
| 496 | * We have to split the large page: |
| 497 | */ |
| 498 | res = split_large_page(kpte, address); |
| 499 | if (res) |
| 500 | return res; |
| 501 | cpa->flushtlb = 1; |
| 502 | goto repeat; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 503 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 505 | /** |
| 506 | * change_page_attr_addr - Change page table attributes in linear mapping |
| 507 | * @address: Virtual address in linear mapping. |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 508 | * @prot: New page table attribute (PAGE_*) |
| 509 | * |
| 510 | * Change page attributes of a page in the direct mapping. This is a variant |
| 511 | * of change_page_attr() that also works on memory holes that do not have |
| 512 | * mem_map entry (pfn_valid() is false). |
| 513 | * |
| 514 | * See change_page_attr() documentation for more details. |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 515 | * |
| 516 | * Modules and drivers should use the set_memory_* APIs instead. |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 517 | */ |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 518 | static int change_page_attr_addr(struct cpa_data *cpa) |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 519 | { |
Thomas Gleixner | 0879750 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 520 | int err; |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 521 | unsigned long address = cpa->vaddr; |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 522 | |
Arjan van de Ven | 488fd99 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 523 | #ifdef CONFIG_X86_64 |
Arjan van de Ven | 626c2c9 | 2008-02-04 16:48:05 +0100 | [diff] [blame] | 524 | unsigned long phys_addr = __pa(address); |
| 525 | |
Arjan van de Ven | 488fd99 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 526 | /* |
Thomas Gleixner | 0879750 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 527 | * If we are inside the high mapped kernel range, then we |
| 528 | * fixup the low mapping first. __va() returns the virtual |
| 529 | * address in the linear mapping: |
Arjan van de Ven | 488fd99 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 530 | */ |
Thomas Gleixner | 0879750 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 531 | if (within(address, HIGH_MAP_START, HIGH_MAP_END)) |
| 532 | address = (unsigned long) __va(phys_addr); |
Arjan van de Ven | 488fd99 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 533 | #endif |
| 534 | |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 535 | err = __change_page_attr(address, cpa); |
Thomas Gleixner | 0879750 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 536 | if (err) |
| 537 | return err; |
| 538 | |
| 539 | #ifdef CONFIG_X86_64 |
| 540 | /* |
| 541 | * If the physical address is inside the kernel map, we need |
| 542 | * to touch the high mapped kernel as well: |
| 543 | */ |
| 544 | if (within(phys_addr, 0, KERNEL_TEXT_SIZE)) { |
| 545 | /* |
| 546 | * Calc the high mapping address. See __phys_addr() |
| 547 | * for the non obvious details. |
Arjan van de Ven | cc0f21b | 2008-02-04 16:48:05 +0100 | [diff] [blame] | 548 | * |
| 549 | * Note that NX and other required permissions are |
| 550 | * checked in static_protections(). |
Thomas Gleixner | 0879750 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 551 | */ |
| 552 | address = phys_addr + HIGH_MAP_START - phys_base; |
Thomas Gleixner | 0879750 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 553 | |
| 554 | /* |
| 555 | * Our high aliases are imprecise, because we check |
| 556 | * everything between 0 and KERNEL_TEXT_SIZE, so do |
| 557 | * not propagate lookup failures back to users: |
| 558 | */ |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 559 | __change_page_attr(address, cpa); |
Thomas Gleixner | 0879750 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 560 | } |
| 561 | #endif |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 562 | return err; |
| 563 | } |
| 564 | |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 565 | static int __change_page_attr_set_clr(struct cpa_data *cpa) |
Thomas Gleixner | ff31452 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 566 | { |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 567 | int ret, numpages = cpa->numpages; |
Thomas Gleixner | ff31452 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 568 | |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 569 | while (numpages) { |
| 570 | /* |
| 571 | * Store the remaining nr of pages for the large page |
| 572 | * preservation check. |
| 573 | */ |
| 574 | cpa->numpages = numpages; |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 575 | ret = change_page_attr_addr(cpa); |
Thomas Gleixner | ff31452 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 576 | if (ret) |
| 577 | return ret; |
Thomas Gleixner | ff31452 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 578 | |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 579 | /* |
| 580 | * Adjust the number of pages with the result of the |
| 581 | * CPA operation. Either a large page has been |
| 582 | * preserved or a single page update happened. |
| 583 | */ |
| 584 | BUG_ON(cpa->numpages > numpages); |
| 585 | numpages -= cpa->numpages; |
| 586 | cpa->vaddr += cpa->numpages * PAGE_SIZE; |
| 587 | } |
Thomas Gleixner | ff31452 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 588 | return 0; |
| 589 | } |
| 590 | |
Andi Kleen | 6bb8383 | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 591 | static inline int cache_attr(pgprot_t attr) |
| 592 | { |
| 593 | return pgprot_val(attr) & |
| 594 | (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD); |
| 595 | } |
| 596 | |
Thomas Gleixner | ff31452 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 597 | static int change_page_attr_set_clr(unsigned long addr, int numpages, |
| 598 | pgprot_t mask_set, pgprot_t mask_clr) |
| 599 | { |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 600 | struct cpa_data cpa; |
Andi Kleen | 6bb8383 | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 601 | int ret, cache; |
Thomas Gleixner | 331e406 | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 602 | |
| 603 | /* |
| 604 | * Check, if we are requested to change a not supported |
| 605 | * feature: |
| 606 | */ |
| 607 | mask_set = canon_pgprot(mask_set); |
| 608 | mask_clr = canon_pgprot(mask_clr); |
| 609 | if (!pgprot_val(mask_set) && !pgprot_val(mask_clr)) |
| 610 | return 0; |
| 611 | |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 612 | cpa.vaddr = addr; |
| 613 | cpa.numpages = numpages; |
| 614 | cpa.mask_set = mask_set; |
| 615 | cpa.mask_clr = mask_clr; |
Thomas Gleixner | f4ae5da | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 616 | cpa.flushtlb = 0; |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 617 | |
| 618 | ret = __change_page_attr_set_clr(&cpa); |
Thomas Gleixner | ff31452 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 619 | |
Thomas Gleixner | 57a6a46 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 620 | /* |
Thomas Gleixner | f4ae5da | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 621 | * Check whether we really changed something: |
| 622 | */ |
| 623 | if (!cpa.flushtlb) |
| 624 | return ret; |
| 625 | |
| 626 | /* |
Andi Kleen | 6bb8383 | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 627 | * No need to flush, when we did not set any of the caching |
| 628 | * attributes: |
| 629 | */ |
| 630 | cache = cache_attr(mask_set); |
| 631 | |
| 632 | /* |
Thomas Gleixner | 57a6a46 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 633 | * On success we use clflush, when the CPU supports it to |
| 634 | * avoid the wbindv. If the CPU does not support it and in the |
Thomas Gleixner | af1e684 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 635 | * error case we fall back to cpa_flush_all (which uses |
Thomas Gleixner | 57a6a46 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 636 | * wbindv): |
| 637 | */ |
| 638 | if (!ret && cpu_has_clflush) |
Andi Kleen | 6bb8383 | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 639 | cpa_flush_range(addr, numpages, cache); |
Thomas Gleixner | 57a6a46 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 640 | else |
Andi Kleen | 6bb8383 | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 641 | cpa_flush_all(cache); |
Thomas Gleixner | ff31452 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 642 | |
| 643 | return ret; |
| 644 | } |
| 645 | |
Thomas Gleixner | 5674454 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 646 | static inline int change_page_attr_set(unsigned long addr, int numpages, |
| 647 | pgprot_t mask) |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 648 | { |
Thomas Gleixner | 5674454 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 649 | return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0)); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 650 | } |
| 651 | |
Thomas Gleixner | 5674454 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 652 | static inline int change_page_attr_clear(unsigned long addr, int numpages, |
| 653 | pgprot_t mask) |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 654 | { |
Huang, Ying | 5827040 | 2008-01-31 22:05:43 +0100 | [diff] [blame] | 655 | return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask); |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 656 | } |
| 657 | |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 658 | int set_memory_uc(unsigned long addr, int numpages) |
| 659 | { |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 660 | return change_page_attr_set(addr, numpages, |
| 661 | __pgprot(_PAGE_PCD | _PAGE_PWT)); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 662 | } |
| 663 | EXPORT_SYMBOL(set_memory_uc); |
| 664 | |
| 665 | int set_memory_wb(unsigned long addr, int numpages) |
| 666 | { |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 667 | return change_page_attr_clear(addr, numpages, |
| 668 | __pgprot(_PAGE_PCD | _PAGE_PWT)); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 669 | } |
| 670 | EXPORT_SYMBOL(set_memory_wb); |
| 671 | |
| 672 | int set_memory_x(unsigned long addr, int numpages) |
| 673 | { |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 674 | return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX)); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 675 | } |
| 676 | EXPORT_SYMBOL(set_memory_x); |
| 677 | |
| 678 | int set_memory_nx(unsigned long addr, int numpages) |
| 679 | { |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 680 | return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX)); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 681 | } |
| 682 | EXPORT_SYMBOL(set_memory_nx); |
| 683 | |
| 684 | int set_memory_ro(unsigned long addr, int numpages) |
| 685 | { |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 686 | return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW)); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 687 | } |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 688 | |
| 689 | int set_memory_rw(unsigned long addr, int numpages) |
| 690 | { |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 691 | return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW)); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 692 | } |
Ingo Molnar | f62d0f0 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 693 | |
| 694 | int set_memory_np(unsigned long addr, int numpages) |
| 695 | { |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 696 | return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT)); |
Ingo Molnar | f62d0f0 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 697 | } |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 698 | |
| 699 | int set_pages_uc(struct page *page, int numpages) |
| 700 | { |
| 701 | unsigned long addr = (unsigned long)page_address(page); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 702 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 703 | return set_memory_uc(addr, numpages); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 704 | } |
| 705 | EXPORT_SYMBOL(set_pages_uc); |
| 706 | |
| 707 | int set_pages_wb(struct page *page, int numpages) |
| 708 | { |
| 709 | unsigned long addr = (unsigned long)page_address(page); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 710 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 711 | return set_memory_wb(addr, numpages); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 712 | } |
| 713 | EXPORT_SYMBOL(set_pages_wb); |
| 714 | |
| 715 | int set_pages_x(struct page *page, int numpages) |
| 716 | { |
| 717 | unsigned long addr = (unsigned long)page_address(page); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 718 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 719 | return set_memory_x(addr, numpages); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 720 | } |
| 721 | EXPORT_SYMBOL(set_pages_x); |
| 722 | |
| 723 | int set_pages_nx(struct page *page, int numpages) |
| 724 | { |
| 725 | unsigned long addr = (unsigned long)page_address(page); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 726 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 727 | return set_memory_nx(addr, numpages); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 728 | } |
| 729 | EXPORT_SYMBOL(set_pages_nx); |
| 730 | |
| 731 | int set_pages_ro(struct page *page, int numpages) |
| 732 | { |
| 733 | unsigned long addr = (unsigned long)page_address(page); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 734 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 735 | return set_memory_ro(addr, numpages); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 736 | } |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 737 | |
| 738 | int set_pages_rw(struct page *page, int numpages) |
| 739 | { |
| 740 | unsigned long addr = (unsigned long)page_address(page); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 741 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 742 | return set_memory_rw(addr, numpages); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 743 | } |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 744 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | #ifdef CONFIG_DEBUG_PAGEALLOC |
Ingo Molnar | f62d0f0 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 746 | |
| 747 | static int __set_pages_p(struct page *page, int numpages) |
| 748 | { |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 749 | struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page), |
| 750 | .numpages = numpages, |
| 751 | .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW), |
| 752 | .mask_clr = __pgprot(0)}; |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 753 | |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 754 | return __change_page_attr_set_clr(&cpa); |
Ingo Molnar | f62d0f0 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | static int __set_pages_np(struct page *page, int numpages) |
| 758 | { |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 759 | struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page), |
| 760 | .numpages = numpages, |
| 761 | .mask_set = __pgprot(0), |
| 762 | .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW)}; |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 763 | |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 764 | return __change_page_attr_set_clr(&cpa); |
Ingo Molnar | f62d0f0 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 765 | } |
| 766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | void kernel_map_pages(struct page *page, int numpages, int enable) |
| 768 | { |
| 769 | if (PageHighMem(page)) |
| 770 | return; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 771 | if (!enable) { |
Ingo Molnar | f9b8404 | 2006-06-27 02:54:49 -0700 | [diff] [blame] | 772 | debug_check_no_locks_freed(page_address(page), |
| 773 | numpages * PAGE_SIZE); |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 774 | } |
Ingo Molnar | de5097c | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 775 | |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 776 | /* |
Ingo Molnar | 12d6f21 | 2008-01-30 13:33:58 +0100 | [diff] [blame] | 777 | * If page allocator is not up yet then do not call c_p_a(): |
| 778 | */ |
| 779 | if (!debug_pagealloc_enabled) |
| 780 | return; |
| 781 | |
| 782 | /* |
Ingo Molnar | e4b71dc | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 783 | * The return value is ignored - the calls cannot fail, |
| 784 | * large pages are disabled at boot time: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | */ |
Ingo Molnar | f62d0f0 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 786 | if (enable) |
| 787 | __set_pages_p(page, numpages); |
| 788 | else |
| 789 | __set_pages_np(page, numpages); |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 790 | |
| 791 | /* |
Ingo Molnar | e4b71dc | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 792 | * We should perform an IPI and flush all tlbs, |
| 793 | * but that can deadlock->flush only current cpu: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | */ |
| 795 | __flush_tlb_all(); |
| 796 | } |
| 797 | #endif |
Arjan van de Ven | d1028a1 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 798 | |
| 799 | /* |
| 800 | * The testcases use internal knowledge of the implementation that shouldn't |
| 801 | * be exposed to the rest of the kernel. Include these directly here. |
| 802 | */ |
| 803 | #ifdef CONFIG_CPA_DEBUG |
| 804 | #include "pageattr-test.c" |
| 805 | #endif |