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 | |
Arjan van de Ven | ed724be | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 19 | static inline int |
| 20 | within(unsigned long addr, unsigned long start, unsigned long end) |
Ingo Molnar | 687c482 | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 21 | { |
Arjan van de Ven | ed724be | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 22 | return addr >= start && addr < end; |
| 23 | } |
| 24 | |
| 25 | /* |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 26 | * Flushing functions |
| 27 | */ |
| 28 | void clflush_cache_range(void *addr, int size) |
| 29 | { |
| 30 | int i; |
| 31 | |
| 32 | for (i = 0; i < size; i += boot_cpu_data.x86_clflush_size) |
| 33 | clflush(addr+i); |
| 34 | } |
| 35 | |
| 36 | static void flush_kernel_map(void *arg) |
| 37 | { |
| 38 | /* |
| 39 | * Flush all to work around Errata in early athlons regarding |
| 40 | * large page flushing. |
| 41 | */ |
| 42 | __flush_tlb_all(); |
| 43 | |
| 44 | if (boot_cpu_data.x86_model >= 4) |
| 45 | wbinvd(); |
| 46 | } |
| 47 | |
| 48 | static void global_flush_tlb(void) |
| 49 | { |
| 50 | BUG_ON(irqs_disabled()); |
| 51 | |
| 52 | on_each_cpu(flush_kernel_map, NULL, 1, 1); |
| 53 | } |
| 54 | |
Thomas Gleixner | 57a6a46 | 2008-01-30 13:34:08 +0100 | [diff] [blame^] | 55 | struct clflush_data { |
| 56 | unsigned long addr; |
| 57 | int numpages; |
| 58 | }; |
| 59 | |
| 60 | static void __cpa_flush_range(void *arg) |
| 61 | { |
| 62 | struct clflush_data *cld = arg; |
| 63 | |
| 64 | /* |
| 65 | * We could optimize that further and do individual per page |
| 66 | * tlb invalidates for a low number of pages. Caveat: we must |
| 67 | * flush the high aliases on 64bit as well. |
| 68 | */ |
| 69 | __flush_tlb_all(); |
| 70 | |
| 71 | clflush_cache_range((void *) cld->addr, cld->numpages * PAGE_SIZE); |
| 72 | } |
| 73 | |
| 74 | static void cpa_flush_range(unsigned long addr, int numpages) |
| 75 | { |
| 76 | struct clflush_data cld; |
| 77 | |
| 78 | BUG_ON(irqs_disabled()); |
| 79 | |
| 80 | cld.addr = addr; |
| 81 | cld.numpages = numpages; |
| 82 | |
| 83 | on_each_cpu(__cpa_flush_range, &cld, 1, 1); |
| 84 | } |
| 85 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 86 | /* |
Arjan van de Ven | ed724be | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 87 | * Certain areas of memory on x86 require very specific protection flags, |
| 88 | * for example the BIOS area or kernel text. Callers don't always get this |
| 89 | * right (again, ioremap() on BIOS memory is not uncommon) so this function |
| 90 | * checks and fixes these known static required protection bits. |
| 91 | */ |
| 92 | static inline pgprot_t static_protections(pgprot_t prot, unsigned long address) |
| 93 | { |
| 94 | pgprot_t forbidden = __pgprot(0); |
| 95 | |
Ingo Molnar | 687c482 | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 96 | /* |
Arjan van de Ven | ed724be | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 97 | * The BIOS area between 640k and 1Mb needs to be executable for |
| 98 | * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support. |
Ingo Molnar | 687c482 | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 99 | */ |
Arjan van de Ven | ed724be | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 100 | if (within(__pa(address), BIOS_BEGIN, BIOS_END)) |
| 101 | pgprot_val(forbidden) |= _PAGE_NX; |
| 102 | |
| 103 | /* |
| 104 | * The kernel text needs to be executable for obvious reasons |
| 105 | * Does not cover __inittext since that is gone later on |
| 106 | */ |
| 107 | if (within(address, (unsigned long)_text, (unsigned long)_etext)) |
| 108 | pgprot_val(forbidden) |= _PAGE_NX; |
| 109 | |
| 110 | #ifdef CONFIG_DEBUG_RODATA |
| 111 | /* The .rodata section needs to be read-only */ |
| 112 | if (within(address, (unsigned long)__start_rodata, |
| 113 | (unsigned long)__end_rodata)) |
| 114 | pgprot_val(forbidden) |= _PAGE_RW; |
| 115 | #endif |
| 116 | |
| 117 | prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden)); |
Ingo Molnar | 687c482 | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 118 | |
| 119 | return prot; |
| 120 | } |
| 121 | |
Ingo Molnar | f0646e4 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 122 | pte_t *lookup_address(unsigned long address, int *level) |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 123 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | pgd_t *pgd = pgd_offset_k(address); |
| 125 | pud_t *pud; |
| 126 | pmd_t *pmd; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 127 | |
Thomas Gleixner | 30551bb | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 128 | *level = PG_LEVEL_NONE; |
| 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | if (pgd_none(*pgd)) |
| 131 | return NULL; |
| 132 | pud = pud_offset(pgd, address); |
| 133 | if (pud_none(*pud)) |
| 134 | return NULL; |
| 135 | pmd = pmd_offset(pud, address); |
| 136 | if (pmd_none(*pmd)) |
| 137 | return NULL; |
Thomas Gleixner | 30551bb | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 138 | |
| 139 | *level = PG_LEVEL_2M; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | if (pmd_large(*pmd)) |
| 141 | return (pte_t *)pmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Thomas Gleixner | 30551bb | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 143 | *level = PG_LEVEL_4K; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 144 | return pte_offset_kernel(pmd, address); |
| 145 | } |
| 146 | |
Ingo Molnar | 9a3dc78 | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 147 | 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] | 148 | { |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 149 | /* change init_mm */ |
| 150 | set_pte_atomic(kpte, pte); |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 151 | #ifdef CONFIG_X86_32 |
Ingo Molnar | e4b71dc | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 152 | if (!SHARED_KERNEL_PMD) { |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 153 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 155 | for (page = pgd_list; page; page = (struct page *)page->index) { |
| 156 | pgd_t *pgd; |
| 157 | pud_t *pud; |
| 158 | pmd_t *pmd; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 159 | |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 160 | pgd = (pgd_t *)page_address(page) + pgd_index(address); |
| 161 | pud = pud_offset(pgd, address); |
| 162 | pmd = pmd_offset(pud, address); |
| 163 | set_pte_atomic((pte_t *)pmd, pte); |
| 164 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 166 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Ingo Molnar | 7afe15b | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 169 | static int split_large_page(pte_t *kpte, unsigned long address) |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 170 | { |
Ingo Molnar | 7afe15b | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 171 | pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte)); |
Ingo Molnar | 12d6f21 | 2008-01-30 13:33:58 +0100 | [diff] [blame] | 172 | gfp_t gfp_flags = GFP_KERNEL; |
Ingo Molnar | 9a3dc78 | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 173 | unsigned long flags; |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 174 | unsigned long addr; |
| 175 | pte_t *pbase, *tmp; |
| 176 | struct page *base; |
Ingo Molnar | 7afe15b | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 177 | int i, level; |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 178 | |
Ingo Molnar | 12d6f21 | 2008-01-30 13:33:58 +0100 | [diff] [blame] | 179 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 180 | gfp_flags = GFP_ATOMIC; |
| 181 | #endif |
| 182 | base = alloc_pages(gfp_flags, 0); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 183 | if (!base) |
| 184 | return -ENOMEM; |
| 185 | |
Ingo Molnar | 9a3dc78 | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 186 | spin_lock_irqsave(&pgd_lock, flags); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 187 | /* |
| 188 | * Check for races, another CPU might have split this page |
| 189 | * up for us already: |
| 190 | */ |
| 191 | tmp = lookup_address(address, &level); |
Ingo Molnar | 5508a748 | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 192 | if (tmp != kpte) { |
| 193 | WARN_ON_ONCE(1); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 194 | goto out_unlock; |
Ingo Molnar | 5508a748 | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 195 | } |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 196 | |
| 197 | address = __pa(address); |
| 198 | addr = address & LARGE_PAGE_MASK; |
| 199 | pbase = (pte_t *)page_address(base); |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 200 | #ifdef CONFIG_X86_32 |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 201 | paravirt_alloc_pt(&init_mm, page_to_pfn(base)); |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 202 | #endif |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 203 | |
| 204 | for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE) |
| 205 | set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot)); |
| 206 | |
| 207 | /* |
Huang, Ying | 4c881ca | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 208 | * Install the new, split up pagetable. Important detail here: |
| 209 | * |
| 210 | * On Intel the NX bit of all levels must be cleared to make a |
| 211 | * page executable. See section 4.13.2 of Intel 64 and IA-32 |
| 212 | * Architectures Software Developer's Manual). |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 213 | */ |
Huang, Ying | 4c881ca | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 214 | ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte))); |
Ingo Molnar | 9a3dc78 | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 215 | __set_pmd_pte(kpte, address, mk_pte(base, ref_prot)); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 216 | base = NULL; |
| 217 | |
| 218 | out_unlock: |
Ingo Molnar | 9a3dc78 | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 219 | spin_unlock_irqrestore(&pgd_lock, flags); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 220 | |
| 221 | if (base) |
| 222 | __free_pages(base, 0); |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 227 | static int |
Ingo Molnar | 8192206 | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 228 | __change_page_attr(unsigned long address, unsigned long pfn, pgprot_t prot) |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 229 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | struct page *kpte_page; |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 231 | int level, err = 0; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 232 | pte_t *kpte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
Ingo Molnar | 8192206 | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 234 | #ifdef CONFIG_X86_32 |
| 235 | BUG_ON(pfn > max_low_pfn); |
| 236 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Ingo Molnar | 97f99fe | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 238 | repeat: |
Ingo Molnar | f0646e4 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 239 | kpte = lookup_address(address, &level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | if (!kpte) |
| 241 | return -EINVAL; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | kpte_page = virt_to_page(kpte); |
Andi Kleen | 65d2f0b | 2007-07-21 17:09:51 +0200 | [diff] [blame] | 244 | BUG_ON(PageLRU(kpte_page)); |
| 245 | BUG_ON(PageCompound(kpte_page)); |
| 246 | |
Arjan van de Ven | ed724be | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 247 | prot = static_protections(prot, address); |
Andi Kleen | 65d2f0b | 2007-07-21 17:09:51 +0200 | [diff] [blame] | 248 | |
Thomas Gleixner | 30551bb | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 249 | if (level == PG_LEVEL_4K) { |
Thomas Gleixner | a72a08a | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 250 | WARN_ON_ONCE(pgprot_val(prot) & _PAGE_PSE); |
Ingo Molnar | 8192206 | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 251 | set_pte_atomic(kpte, pfn_pte(pfn, canon_pgprot(prot))); |
Ingo Molnar | 78c94ab | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 252 | } else { |
Thomas Gleixner | a72a08a | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 253 | /* Clear the PSE bit for the 4k level pages ! */ |
| 254 | pgprot_val(prot) = pgprot_val(prot) & ~_PAGE_PSE; |
| 255 | |
Ingo Molnar | 7afe15b | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 256 | err = split_large_page(kpte, address); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 257 | if (!err) |
| 258 | goto repeat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | } |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 260 | return err; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 261 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 263 | /** |
| 264 | * change_page_attr_addr - Change page table attributes in linear mapping |
| 265 | * @address: Virtual address in linear mapping. |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 266 | * @prot: New page table attribute (PAGE_*) |
| 267 | * |
| 268 | * Change page attributes of a page in the direct mapping. This is a variant |
| 269 | * of change_page_attr() that also works on memory holes that do not have |
| 270 | * mem_map entry (pfn_valid() is false). |
| 271 | * |
| 272 | * See change_page_attr() documentation for more details. |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 273 | * |
| 274 | * Modules and drivers should use the set_memory_* APIs instead. |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 275 | */ |
| 276 | |
Arjan van de Ven | 488fd99 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 277 | static int change_page_attr_addr(unsigned long address, pgprot_t prot) |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 278 | { |
Arjan van de Ven | 488fd99 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 279 | int err = 0, kernel_map = 0; |
| 280 | unsigned long pfn = __pa(address) >> PAGE_SHIFT; |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 281 | |
| 282 | #ifdef CONFIG_X86_64 |
| 283 | if (address >= __START_KERNEL_map && |
| 284 | address < __START_KERNEL_map + KERNEL_TEXT_SIZE) { |
| 285 | |
| 286 | address = (unsigned long)__va(__pa(address)); |
| 287 | kernel_map = 1; |
| 288 | } |
| 289 | #endif |
| 290 | |
Arjan van de Ven | 488fd99 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 291 | if (!kernel_map || pte_present(pfn_pte(0, prot))) { |
| 292 | err = __change_page_attr(address, pfn, prot); |
| 293 | if (err) |
| 294 | return err; |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 295 | } |
| 296 | |
Arjan van de Ven | 488fd99 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 297 | #ifdef CONFIG_X86_64 |
| 298 | /* |
| 299 | * Handle kernel mapping too which aliases part of |
| 300 | * lowmem: |
| 301 | */ |
| 302 | if (__pa(address) < KERNEL_TEXT_SIZE) { |
| 303 | unsigned long addr2; |
| 304 | pgprot_t prot2; |
| 305 | |
| 306 | addr2 = __START_KERNEL_map + __pa(address); |
| 307 | /* Make sure the kernel mappings stay executable */ |
| 308 | prot2 = pte_pgprot(pte_mkexec(pfn_pte(0, prot))); |
| 309 | err = __change_page_attr(addr2, pfn, prot2); |
| 310 | } |
| 311 | #endif |
| 312 | |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 313 | return err; |
| 314 | } |
| 315 | |
Thomas Gleixner | ff31452 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 316 | static int __change_page_attr_set_clr(unsigned long addr, int numpages, |
| 317 | pgprot_t mask_set, pgprot_t mask_clr) |
| 318 | { |
| 319 | pgprot_t new_prot; |
| 320 | int level; |
| 321 | pte_t *pte; |
| 322 | int i, ret; |
| 323 | |
| 324 | for (i = 0; i < numpages ; i++) { |
| 325 | |
| 326 | pte = lookup_address(addr, &level); |
| 327 | if (!pte) |
| 328 | return -EINVAL; |
| 329 | |
| 330 | new_prot = pte_pgprot(*pte); |
| 331 | |
| 332 | pgprot_val(new_prot) &= ~pgprot_val(mask_clr); |
| 333 | pgprot_val(new_prot) |= pgprot_val(mask_set); |
| 334 | |
| 335 | ret = change_page_attr_addr(addr, new_prot); |
| 336 | if (ret) |
| 337 | return ret; |
| 338 | addr += PAGE_SIZE; |
| 339 | } |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static int change_page_attr_set_clr(unsigned long addr, int numpages, |
| 345 | pgprot_t mask_set, pgprot_t mask_clr) |
| 346 | { |
| 347 | int ret = __change_page_attr_set_clr(addr, numpages, mask_set, |
| 348 | mask_clr); |
| 349 | |
Thomas Gleixner | 57a6a46 | 2008-01-30 13:34:08 +0100 | [diff] [blame^] | 350 | /* |
| 351 | * On success we use clflush, when the CPU supports it to |
| 352 | * avoid the wbindv. If the CPU does not support it and in the |
| 353 | * error case we fall back to global_flush_tlb (which uses |
| 354 | * wbindv): |
| 355 | */ |
| 356 | if (!ret && cpu_has_clflush) |
| 357 | cpa_flush_range(addr, numpages); |
| 358 | else |
| 359 | global_flush_tlb(); |
Thomas Gleixner | ff31452 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 360 | |
| 361 | return ret; |
| 362 | } |
| 363 | |
Thomas Gleixner | 5674454 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 364 | static inline int change_page_attr_set(unsigned long addr, int numpages, |
| 365 | pgprot_t mask) |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 366 | { |
Thomas Gleixner | 5674454 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 367 | 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] | 368 | } |
| 369 | |
Thomas Gleixner | 5674454 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 370 | static inline int change_page_attr_clear(unsigned long addr, int numpages, |
| 371 | pgprot_t mask) |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 372 | { |
Thomas Gleixner | 5674454 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 373 | return __change_page_attr_set_clr(addr, numpages, __pgprot(0), mask); |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 374 | |
| 375 | } |
| 376 | |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 377 | int set_memory_uc(unsigned long addr, int numpages) |
| 378 | { |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 379 | return change_page_attr_set(addr, numpages, |
| 380 | __pgprot(_PAGE_PCD | _PAGE_PWT)); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 381 | } |
| 382 | EXPORT_SYMBOL(set_memory_uc); |
| 383 | |
| 384 | int set_memory_wb(unsigned long addr, int numpages) |
| 385 | { |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 386 | return change_page_attr_clear(addr, numpages, |
| 387 | __pgprot(_PAGE_PCD | _PAGE_PWT)); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 388 | } |
| 389 | EXPORT_SYMBOL(set_memory_wb); |
| 390 | |
| 391 | int set_memory_x(unsigned long addr, int numpages) |
| 392 | { |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 393 | return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX)); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 394 | } |
| 395 | EXPORT_SYMBOL(set_memory_x); |
| 396 | |
| 397 | int set_memory_nx(unsigned long addr, int numpages) |
| 398 | { |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 399 | return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX)); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 400 | } |
| 401 | EXPORT_SYMBOL(set_memory_nx); |
| 402 | |
| 403 | int set_memory_ro(unsigned long addr, int numpages) |
| 404 | { |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 405 | return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW)); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 406 | } |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 407 | |
| 408 | int set_memory_rw(unsigned long addr, int numpages) |
| 409 | { |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 410 | return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW)); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 411 | } |
Ingo Molnar | f62d0f0 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 412 | |
| 413 | int set_memory_np(unsigned long addr, int numpages) |
| 414 | { |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 415 | return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT)); |
Ingo Molnar | f62d0f0 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 416 | } |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 417 | |
| 418 | int set_pages_uc(struct page *page, int numpages) |
| 419 | { |
| 420 | unsigned long addr = (unsigned long)page_address(page); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 421 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 422 | return set_memory_uc(addr, numpages); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 423 | } |
| 424 | EXPORT_SYMBOL(set_pages_uc); |
| 425 | |
| 426 | int set_pages_wb(struct page *page, int numpages) |
| 427 | { |
| 428 | unsigned long addr = (unsigned long)page_address(page); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 429 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 430 | return set_memory_wb(addr, numpages); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 431 | } |
| 432 | EXPORT_SYMBOL(set_pages_wb); |
| 433 | |
| 434 | int set_pages_x(struct page *page, int numpages) |
| 435 | { |
| 436 | unsigned long addr = (unsigned long)page_address(page); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 437 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 438 | return set_memory_x(addr, numpages); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 439 | } |
| 440 | EXPORT_SYMBOL(set_pages_x); |
| 441 | |
| 442 | int set_pages_nx(struct page *page, int numpages) |
| 443 | { |
| 444 | unsigned long addr = (unsigned long)page_address(page); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 445 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 446 | return set_memory_nx(addr, numpages); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 447 | } |
| 448 | EXPORT_SYMBOL(set_pages_nx); |
| 449 | |
| 450 | int set_pages_ro(struct page *page, int numpages) |
| 451 | { |
| 452 | unsigned long addr = (unsigned long)page_address(page); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 453 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 454 | return set_memory_ro(addr, numpages); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 455 | } |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 456 | |
| 457 | int set_pages_rw(struct page *page, int numpages) |
| 458 | { |
| 459 | unsigned long addr = (unsigned long)page_address(page); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 460 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 461 | return set_memory_rw(addr, numpages); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 462 | } |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
Thomas Gleixner | 5674454 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 465 | #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_CPA_DEBUG) |
| 466 | static inline int __change_page_attr_set(unsigned long addr, int numpages, |
| 467 | pgprot_t mask) |
| 468 | { |
| 469 | return __change_page_attr_set_clr(addr, numpages, mask, __pgprot(0)); |
| 470 | } |
| 471 | |
| 472 | static inline int __change_page_attr_clear(unsigned long addr, int numpages, |
| 473 | pgprot_t mask) |
| 474 | { |
| 475 | return __change_page_attr_set_clr(addr, numpages, __pgprot(0), mask); |
| 476 | } |
| 477 | #endif |
| 478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | #ifdef CONFIG_DEBUG_PAGEALLOC |
Ingo Molnar | f62d0f0 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 480 | |
| 481 | static int __set_pages_p(struct page *page, int numpages) |
| 482 | { |
| 483 | unsigned long addr = (unsigned long)page_address(page); |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 484 | |
| 485 | return __change_page_attr_set(addr, numpages, |
| 486 | __pgprot(_PAGE_PRESENT | _PAGE_RW)); |
Ingo Molnar | f62d0f0 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static int __set_pages_np(struct page *page, int numpages) |
| 490 | { |
| 491 | unsigned long addr = (unsigned long)page_address(page); |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 492 | |
| 493 | return __change_page_attr_clear(addr, numpages, |
| 494 | __pgprot(_PAGE_PRESENT)); |
Ingo Molnar | f62d0f0 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 495 | } |
| 496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | void kernel_map_pages(struct page *page, int numpages, int enable) |
| 498 | { |
| 499 | if (PageHighMem(page)) |
| 500 | return; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 501 | if (!enable) { |
Ingo Molnar | f9b8404 | 2006-06-27 02:54:49 -0700 | [diff] [blame] | 502 | debug_check_no_locks_freed(page_address(page), |
| 503 | numpages * PAGE_SIZE); |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 504 | } |
Ingo Molnar | de5097c | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 505 | |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 506 | /* |
Ingo Molnar | 12d6f21 | 2008-01-30 13:33:58 +0100 | [diff] [blame] | 507 | * If page allocator is not up yet then do not call c_p_a(): |
| 508 | */ |
| 509 | if (!debug_pagealloc_enabled) |
| 510 | return; |
| 511 | |
| 512 | /* |
Ingo Molnar | e4b71dc | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 513 | * The return value is ignored - the calls cannot fail, |
| 514 | * large pages are disabled at boot time: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | */ |
Ingo Molnar | f62d0f0 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 516 | if (enable) |
| 517 | __set_pages_p(page, numpages); |
| 518 | else |
| 519 | __set_pages_np(page, numpages); |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 520 | |
| 521 | /* |
Ingo Molnar | e4b71dc | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 522 | * We should perform an IPI and flush all tlbs, |
| 523 | * but that can deadlock->flush only current cpu: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | */ |
| 525 | __flush_tlb_all(); |
| 526 | } |
| 527 | #endif |
Arjan van de Ven | d1028a1 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 528 | |
| 529 | /* |
| 530 | * The testcases use internal knowledge of the implementation that shouldn't |
| 531 | * be exposed to the rest of the kernel. Include these directly here. |
| 532 | */ |
| 533 | #ifdef CONFIG_CPA_DEBUG |
| 534 | #include "pageattr-test.c" |
| 535 | #endif |