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