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