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