Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * self test for change_page_attr. |
| 3 | * |
| 4 | * Clears the global bit on random pages in the direct mapping, then reverts |
| 5 | * and compares page tables forwards and afterwards. |
| 6 | */ |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 7 | #include <linux/bootmem.h> |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 8 | #include <linux/random.h> |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/init.h> |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 11 | #include <linux/mm.h> |
| 12 | |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 13 | #include <asm/cacheflush.h> |
| 14 | #include <asm/pgtable.h> |
| 15 | #include <asm/kdebug.h> |
| 16 | |
| 17 | enum { |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 18 | NTEST = 400, |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 19 | #ifdef CONFIG_X86_64 |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 20 | LOWEST_LEVEL = 4, |
| 21 | LPS = (1 << PMD_SHIFT), |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 22 | #elif defined(CONFIG_X86_PAE) |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 23 | LOWEST_LEVEL = 3, |
| 24 | LPS = (1 << PMD_SHIFT), |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 25 | #else |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 26 | LOWEST_LEVEL = 3, /* lookup_address lies here */ |
| 27 | LPS = (1 << 22), |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 28 | #endif |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 29 | GPS = (1<<30) |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | #ifdef CONFIG_X86_64 |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 33 | # include <asm/proto.h> |
| 34 | # define max_mapped end_pfn_map |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 35 | #else |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 36 | # define max_mapped max_low_pfn |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 37 | #endif |
| 38 | |
| 39 | struct split_state { |
| 40 | long lpg, gpg, spg, exec; |
| 41 | long min_exec, max_exec; |
| 42 | }; |
| 43 | |
| 44 | static __init int print_split(struct split_state *s) |
| 45 | { |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 46 | long i, expected, missed = 0; |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 47 | int printed = 0; |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 48 | int err = 0; |
| 49 | |
| 50 | s->lpg = s->gpg = s->spg = s->exec = 0; |
| 51 | s->min_exec = ~0UL; |
| 52 | s->max_exec = 0; |
| 53 | for (i = 0; i < max_mapped; ) { |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 54 | unsigned long addr = (unsigned long)__va(i << PAGE_SHIFT); |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 55 | int level; |
| 56 | pte_t *pte; |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 57 | |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 58 | pte = lookup_address(addr, &level); |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 59 | if (!pte) { |
| 60 | if (!printed) { |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 61 | dump_pagetable(addr); |
| 62 | printk(KERN_INFO "CPA %lx no pte level %d\n", |
| 63 | addr, level); |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 64 | printed = 1; |
| 65 | } |
| 66 | missed++; |
| 67 | i++; |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | if (level == 2 && sizeof(long) == 8) { |
| 72 | s->gpg++; |
| 73 | i += GPS/PAGE_SIZE; |
| 74 | } else if (level != LOWEST_LEVEL) { |
| 75 | if (!(pte_val(*pte) & _PAGE_PSE)) { |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 76 | printk(KERN_ERR |
| 77 | "%lx level %d but not PSE %Lx\n", |
| 78 | addr, level, (u64)pte_val(*pte)); |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 79 | err = 1; |
| 80 | } |
| 81 | s->lpg++; |
| 82 | i += LPS/PAGE_SIZE; |
| 83 | } else { |
| 84 | s->spg++; |
| 85 | i++; |
| 86 | } |
| 87 | if (!(pte_val(*pte) & _PAGE_NX)) { |
| 88 | s->exec++; |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 89 | if (addr < s->min_exec) |
| 90 | s->min_exec = addr; |
| 91 | if (addr > s->max_exec) |
| 92 | s->max_exec = addr; |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 93 | } |
| 94 | } |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 95 | printk(KERN_INFO |
| 96 | "CPA mapping 4k %lu large %lu gb %lu x %lu[%lx-%lx] miss %lu\n", |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 97 | s->spg, s->lpg, s->gpg, s->exec, |
| 98 | s->min_exec != ~0UL ? s->min_exec : 0, s->max_exec, missed); |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 99 | |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 100 | expected = (s->gpg*GPS + s->lpg*LPS)/PAGE_SIZE + s->spg + missed; |
| 101 | if (expected != i) { |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 102 | printk(KERN_ERR "CPA max_mapped %lu but expected %lu\n", |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 103 | max_mapped, expected); |
| 104 | return 1; |
| 105 | } |
| 106 | return err; |
| 107 | } |
| 108 | |
| 109 | static __init int state_same(struct split_state *a, struct split_state *b) |
| 110 | { |
| 111 | return a->lpg == b->lpg && a->gpg == b->gpg && a->spg == b->spg && |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 112 | a->exec == b->exec; |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 115 | static unsigned long __initdata addr[NTEST]; |
| 116 | static unsigned int __initdata len[NTEST]; |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 117 | |
| 118 | /* Change the global bit on random pages in the direct mapping */ |
| 119 | static __init int exercise_pageattr(void) |
| 120 | { |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 121 | struct split_state sa, sb, sc; |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 122 | unsigned long *bm; |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 123 | pte_t *pte, pte0; |
| 124 | int failed = 0; |
| 125 | int level; |
| 126 | int i, k; |
| 127 | int err; |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 128 | |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 129 | printk(KERN_INFO "CPA exercising pageattr\n"); |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 130 | |
| 131 | bm = vmalloc((max_mapped + 7) / 8); |
| 132 | if (!bm) { |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 133 | printk(KERN_ERR "CPA Cannot vmalloc bitmap\n"); |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 134 | return -ENOMEM; |
| 135 | } |
| 136 | memset(bm, 0, (max_mapped + 7) / 8); |
| 137 | |
| 138 | failed += print_split(&sa); |
| 139 | srandom32(100); |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 140 | |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 141 | for (i = 0; i < NTEST; i++) { |
| 142 | unsigned long pfn = random32() % max_mapped; |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 143 | |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 144 | addr[i] = (unsigned long)__va(pfn << PAGE_SHIFT); |
| 145 | len[i] = random32() % 100; |
| 146 | len[i] = min_t(unsigned long, len[i], max_mapped - pfn - 1); |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 147 | |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 148 | if (len[i] == 0) |
| 149 | len[i] = 1; |
| 150 | |
| 151 | pte = NULL; |
| 152 | pte0 = pfn_pte(0, __pgprot(0)); /* shut gcc up */ |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 153 | |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 154 | for (k = 0; k < len[i]; k++) { |
| 155 | pte = lookup_address(addr[i] + k*PAGE_SIZE, &level); |
| 156 | if (!pte || pgprot_val(pte_pgprot(*pte)) == 0) { |
| 157 | addr[i] = 0; |
| 158 | break; |
| 159 | } |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 160 | if (k == 0) { |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 161 | pte0 = *pte; |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 162 | } else { |
| 163 | if (pgprot_val(pte_pgprot(*pte)) != |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 164 | pgprot_val(pte_pgprot(pte0))) { |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 165 | len[i] = k; |
| 166 | break; |
| 167 | } |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 168 | } |
| 169 | if (test_bit(pfn + k, bm)) { |
| 170 | len[i] = k; |
| 171 | break; |
| 172 | } |
| 173 | __set_bit(pfn + k, bm); |
| 174 | } |
| 175 | if (!addr[i] || !pte || !k) { |
| 176 | addr[i] = 0; |
| 177 | continue; |
| 178 | } |
| 179 | |
Andi Kleen | 6ba9b7d | 2008-01-30 13:33:52 +0100 | [diff] [blame^] | 180 | err = change_page_attr_addr(addr[i], len[i], |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 181 | pte_pgprot(pte_clrhuge(pte_clrglobal(pte0)))); |
| 182 | if (err < 0) { |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 183 | printk(KERN_ERR "CPA %d failed %d\n", i, err); |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 184 | failed++; |
| 185 | } |
| 186 | |
| 187 | pte = lookup_address(addr[i], &level); |
| 188 | if (!pte || pte_global(*pte) || pte_huge(*pte)) { |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 189 | printk(KERN_ERR "CPA %lx: bad pte %Lx\n", addr[i], |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 190 | pte ? (u64)pte_val(*pte) : 0ULL); |
| 191 | failed++; |
| 192 | } |
| 193 | if (level != LOWEST_LEVEL) { |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 194 | printk(KERN_ERR "CPA %lx: unexpected level %d\n", |
| 195 | addr[i], level); |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 196 | failed++; |
| 197 | } |
| 198 | |
| 199 | } |
| 200 | vfree(bm); |
| 201 | global_flush_tlb(); |
| 202 | |
| 203 | failed += print_split(&sb); |
| 204 | |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 205 | printk(KERN_INFO "CPA reverting everything\n"); |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 206 | for (i = 0; i < NTEST; i++) { |
| 207 | if (!addr[i]) |
| 208 | continue; |
| 209 | pte = lookup_address(addr[i], &level); |
| 210 | if (!pte) { |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 211 | printk(KERN_ERR "CPA lookup of %lx failed\n", addr[i]); |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 212 | failed++; |
| 213 | continue; |
| 214 | } |
Andi Kleen | 6ba9b7d | 2008-01-30 13:33:52 +0100 | [diff] [blame^] | 215 | err = change_page_attr_addr(addr[i], len[i], |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 216 | pte_pgprot(pte_mkglobal(*pte))); |
| 217 | if (err < 0) { |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 218 | printk(KERN_ERR "CPA reverting failed: %d\n", err); |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 219 | failed++; |
| 220 | } |
| 221 | pte = lookup_address(addr[i], &level); |
| 222 | if (!pte || !pte_global(*pte)) { |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 223 | printk(KERN_ERR "CPA %lx: bad pte after revert %Lx\n", |
| 224 | addr[i], pte ? (u64)pte_val(*pte) : 0ULL); |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 225 | failed++; |
| 226 | } |
| 227 | |
| 228 | } |
| 229 | global_flush_tlb(); |
| 230 | |
| 231 | failed += print_split(&sc); |
| 232 | if (!state_same(&sa, &sc)) |
| 233 | failed++; |
| 234 | |
| 235 | if (failed) |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 236 | printk(KERN_ERR "CPA selftests NOT PASSED. Please report.\n"); |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 237 | else |
Ingo Molnar | 851339b | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 238 | printk(KERN_INFO "CPA selftests PASSED\n"); |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 239 | |
| 240 | return 0; |
| 241 | } |
Andi Kleen | fa2d836 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 242 | module_init(exercise_pageattr); |