2 * self test for change_page_attr.
4 * Clears the global bit on random pages in the direct mapping, then reverts
5 * and compares page tables forwards and afterwards.
7 #include <linux/bootmem.h>
8 #include <linux/random.h>
9 #include <linux/kernel.h>
10 #include <linux/init.h>
13 #include <asm/cacheflush.h>
14 #include <asm/pgtable.h>
15 #include <asm/kdebug.h>
19 LOWEST_LEVEL = PG_LEVEL_4K,
21 LPS = (1 << PMD_SHIFT),
22 #elif defined(CONFIG_X86_PAE)
23 LPS = (1 << PMD_SHIFT),
31 long lpg, gpg, spg, exec;
32 long min_exec, max_exec;
35 static __init int print_split(struct split_state *s)
37 long i, expected, missed = 0;
41 s->lpg = s->gpg = s->spg = s->exec = 0;
44 for (i = 0; i < max_pfn_mapped; ) {
45 unsigned long addr = (unsigned long)__va(i << PAGE_SHIFT);
49 pte = lookup_address(addr, &level);
53 printk(KERN_INFO "CPA %lx no pte level %d\n",
62 if (level == 2 && sizeof(long) == 8) {
65 } else if (level != LOWEST_LEVEL) {
66 if (!(pte_val(*pte) & _PAGE_PSE)) {
68 "%lx level %d but not PSE %Lx\n",
69 addr, level, (u64)pte_val(*pte));
78 if (!(pte_val(*pte) & _PAGE_NX)) {
80 if (addr < s->min_exec)
82 if (addr > s->max_exec)
87 "CPA mapping 4k %lu large %lu gb %lu x %lu[%lx-%lx] miss %lu\n",
88 s->spg, s->lpg, s->gpg, s->exec,
89 s->min_exec != ~0UL ? s->min_exec : 0, s->max_exec, missed);
91 expected = (s->gpg*GPS + s->lpg*LPS)/PAGE_SIZE + s->spg + missed;
93 printk(KERN_ERR "CPA max_pfn_mapped %lu but expected %lu\n",
94 max_pfn_mapped, expected);
100 static unsigned long __initdata addr[NTEST];
101 static unsigned int __initdata len[NTEST];
103 /* Change the global bit on random pages in the direct mapping */
104 static __init int exercise_pageattr(void)
106 struct split_state sa, sb, sc;
114 printk(KERN_INFO "CPA exercising pageattr\n");
116 bm = vmalloc((max_pfn_mapped + 7) / 8);
118 printk(KERN_ERR "CPA Cannot vmalloc bitmap\n");
121 memset(bm, 0, (max_pfn_mapped + 7) / 8);
123 failed += print_split(&sa);
126 for (i = 0; i < NTEST; i++) {
127 unsigned long pfn = random32() % max_pfn_mapped;
129 addr[i] = (unsigned long)__va(pfn << PAGE_SHIFT);
130 len[i] = random32() % 100;
131 len[i] = min_t(unsigned long, len[i], max_pfn_mapped - pfn - 1);
137 pte0 = pfn_pte(0, __pgprot(0)); /* shut gcc up */
139 for (k = 0; k < len[i]; k++) {
140 pte = lookup_address(addr[i] + k*PAGE_SIZE, &level);
141 if (!pte || pgprot_val(pte_pgprot(*pte)) == 0) {
148 if (pgprot_val(pte_pgprot(*pte)) !=
149 pgprot_val(pte_pgprot(pte0))) {
154 if (test_bit(pfn + k, bm)) {
158 __set_bit(pfn + k, bm);
160 if (!addr[i] || !pte || !k) {
165 err = change_page_attr_clear(addr[i], len[i],
166 __pgprot(_PAGE_GLOBAL));
168 printk(KERN_ERR "CPA %d failed %d\n", i, err);
172 pte = lookup_address(addr[i], &level);
173 if (!pte || pte_global(*pte) || pte_huge(*pte)) {
174 printk(KERN_ERR "CPA %lx: bad pte %Lx\n", addr[i],
175 pte ? (u64)pte_val(*pte) : 0ULL);
178 if (level != LOWEST_LEVEL) {
179 printk(KERN_ERR "CPA %lx: unexpected level %d\n",
188 failed += print_split(&sb);
190 printk(KERN_INFO "CPA reverting everything\n");
191 for (i = 0; i < NTEST; i++) {
194 pte = lookup_address(addr[i], &level);
196 printk(KERN_ERR "CPA lookup of %lx failed\n", addr[i]);
200 err = change_page_attr_set(addr[i], len[i],
201 __pgprot(_PAGE_GLOBAL));
203 printk(KERN_ERR "CPA reverting failed: %d\n", err);
206 pte = lookup_address(addr[i], &level);
207 if (!pte || !pte_global(*pte)) {
208 printk(KERN_ERR "CPA %lx: bad pte after revert %Lx\n",
209 addr[i], pte ? (u64)pte_val(*pte) : 0ULL);
216 failed += print_split(&sc);
219 printk(KERN_ERR "CPA selftests NOT PASSED. Please report.\n");
222 printk(KERN_INFO "CPA selftests PASSED\n");
227 module_init(exercise_pageattr);