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