2 * self test for change_page_attr.
4 * Clears the a test pte bit on random pages in the direct mapping,
5 * then reverts and compares page tables forwards and afterwards.
7 #include <linux/bootmem.h>
8 #include <linux/kthread.h>
9 #include <linux/random.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
14 #include <asm/cacheflush.h>
15 #include <asm/pgtable.h>
16 #include <asm/kdebug.h>
19 * Only print the results of the first pass:
21 static __read_mostly
int print
= 1;
26 LPS
= (1 << PMD_SHIFT
),
27 #elif defined(CONFIG_X86_PAE)
28 LPS
= (1 << PMD_SHIFT
),
35 #define PAGE_CPA_TEST __pgprot(_PAGE_CPA_TEST)
37 static int pte_testbit(pte_t pte
)
39 return pte_flags(pte
) & _PAGE_UNUSED1
;
43 long lpg
, gpg
, spg
, exec
;
44 long min_exec
, max_exec
;
47 static int print_split(struct split_state
*s
)
49 long i
, expected
, missed
= 0;
52 s
->lpg
= s
->gpg
= s
->spg
= s
->exec
= 0;
55 for (i
= 0; i
< max_pfn_mapped
; ) {
56 unsigned long addr
= (unsigned long)__va(i
<< PAGE_SHIFT
);
60 pte
= lookup_address(addr
, &level
);
67 if (level
== PG_LEVEL_1G
&& sizeof(long) == 8) {
70 } else if (level
== PG_LEVEL_2M
) {
71 if (!(pte_val(*pte
) & _PAGE_PSE
)) {
73 "%lx level %d but not PSE %Lx\n",
74 addr
, level
, (u64
)pte_val(*pte
));
83 if (!(pte_val(*pte
) & _PAGE_NX
)) {
85 if (addr
< s
->min_exec
)
87 if (addr
> s
->max_exec
)
93 " 4k %lu large %lu gb %lu x %lu[%lx-%lx] miss %lu\n",
94 s
->spg
, s
->lpg
, s
->gpg
, s
->exec
,
95 s
->min_exec
!= ~0UL ? s
->min_exec
: 0,
99 expected
= (s
->gpg
*GPS
+ s
->lpg
*LPS
)/PAGE_SIZE
+ s
->spg
+ missed
;
101 printk(KERN_ERR
"CPA max_pfn_mapped %lu but expected %lu\n",
102 max_pfn_mapped
, expected
);
108 static unsigned long addr
[NTEST
];
109 static unsigned int len
[NTEST
];
111 /* Change the global bit on random pages in the direct mapping */
112 static int pageattr_test(void)
114 struct split_state sa
, sb
, sc
;
121 unsigned long test_addr
;
124 printk(KERN_INFO
"CPA self-test:\n");
126 bm
= vzalloc((max_pfn_mapped
+ 7) / 8);
128 printk(KERN_ERR
"CPA Cannot vmalloc bitmap\n");
132 failed
+= print_split(&sa
);
135 for (i
= 0; i
< NTEST
; i
++) {
136 unsigned long pfn
= random32() % max_pfn_mapped
;
138 addr
[i
] = (unsigned long)__va(pfn
<< PAGE_SHIFT
);
139 len
[i
] = random32() % 100;
140 len
[i
] = min_t(unsigned long, len
[i
], max_pfn_mapped
- pfn
- 1);
146 pte0
= pfn_pte(0, __pgprot(0)); /* shut gcc up */
148 for (k
= 0; k
< len
[i
]; k
++) {
149 pte
= lookup_address(addr
[i
] + k
*PAGE_SIZE
, &level
);
150 if (!pte
|| pgprot_val(pte_pgprot(*pte
)) == 0 ||
151 !(pte_val(*pte
) & _PAGE_PRESENT
)) {
158 if (pgprot_val(pte_pgprot(*pte
)) !=
159 pgprot_val(pte_pgprot(pte0
))) {
164 if (test_bit(pfn
+ k
, bm
)) {
168 __set_bit(pfn
+ k
, bm
);
170 if (!addr
[i
] || !pte
|| !k
) {
176 err
= change_page_attr_set(&test_addr
, len
[i
], PAGE_CPA_TEST
, 0);
178 printk(KERN_ERR
"CPA %d failed %d\n", i
, err
);
182 pte
= lookup_address(addr
[i
], &level
);
183 if (!pte
|| !pte_testbit(*pte
) || pte_huge(*pte
)) {
184 printk(KERN_ERR
"CPA %lx: bad pte %Lx\n", addr
[i
],
185 pte
? (u64
)pte_val(*pte
) : 0ULL);
188 if (level
!= PG_LEVEL_4K
) {
189 printk(KERN_ERR
"CPA %lx: unexpected level %d\n",
197 failed
+= print_split(&sb
);
199 for (i
= 0; i
< NTEST
; i
++) {
202 pte
= lookup_address(addr
[i
], &level
);
204 printk(KERN_ERR
"CPA lookup of %lx failed\n", addr
[i
]);
209 err
= change_page_attr_clear(&test_addr
, len
[i
], PAGE_CPA_TEST
, 0);
211 printk(KERN_ERR
"CPA reverting failed: %d\n", err
);
214 pte
= lookup_address(addr
[i
], &level
);
215 if (!pte
|| pte_testbit(*pte
)) {
216 printk(KERN_ERR
"CPA %lx: bad pte after revert %Lx\n",
217 addr
[i
], pte
? (u64
)pte_val(*pte
) : 0ULL);
223 failed
+= print_split(&sc
);
226 WARN(1, KERN_ERR
"NOT PASSED. Please report.\n");
230 printk(KERN_INFO
"ok.\n");
236 static int do_pageattr_test(void *__unused
)
238 while (!kthread_should_stop()) {
239 schedule_timeout_interruptible(HZ
*30);
240 if (pageattr_test() < 0)
248 static int start_pageattr_test(void)
250 struct task_struct
*p
;
252 p
= kthread_create(do_pageattr_test
, NULL
, "pageattr-test");
261 module_init(start_pageattr_test
);