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