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>
13 #include <asm/cacheflush.h>
14 #include <asm/pgtable.h>
15 #include <asm/kdebug.h>
18 * Only print the results of the first pass:
20 static __read_mostly
int print
= 1;
25 LPS
= (1 << PMD_SHIFT
),
26 #elif defined(CONFIG_X86_PAE)
27 LPS
= (1 << PMD_SHIFT
),
34 #define PAGE_CPA_TEST __pgprot(_PAGE_CPA_TEST)
36 static int pte_testbit(pte_t pte
)
38 return pte_flags(pte
) & _PAGE_SOFTW1
;
42 long lpg
, gpg
, spg
, exec
;
43 long min_exec
, max_exec
;
46 static int print_split(struct split_state
*s
)
48 long i
, expected
, missed
= 0;
51 s
->lpg
= s
->gpg
= s
->spg
= s
->exec
= 0;
54 for (i
= 0; i
< max_pfn_mapped
; ) {
55 unsigned long addr
= (unsigned long)__va(i
<< PAGE_SHIFT
);
59 pte
= lookup_address(addr
, &level
);
66 if (level
== PG_LEVEL_1G
&& sizeof(long) == 8) {
69 } else if (level
== PG_LEVEL_2M
) {
70 if ((pte_val(*pte
) & _PAGE_PRESENT
) && !(pte_val(*pte
) & _PAGE_PSE
)) {
72 "%lx level %d but not PSE %Lx\n",
73 addr
, level
, (u64
)pte_val(*pte
));
82 if (!(pte_val(*pte
) & _PAGE_NX
)) {
84 if (addr
< s
->min_exec
)
86 if (addr
> s
->max_exec
)
92 " 4k %lu large %lu gb %lu x %lu[%lx-%lx] miss %lu\n",
93 s
->spg
, s
->lpg
, s
->gpg
, s
->exec
,
94 s
->min_exec
!= ~0UL ? s
->min_exec
: 0,
98 expected
= (s
->gpg
*GPS
+ s
->lpg
*LPS
)/PAGE_SIZE
+ s
->spg
+ missed
;
100 printk(KERN_ERR
"CPA max_pfn_mapped %lu but expected %lu\n",
101 max_pfn_mapped
, expected
);
107 static unsigned long addr
[NTEST
];
108 static unsigned int len
[NTEST
];
110 /* Change the global bit on random pages in the direct mapping */
111 static int pageattr_test(void)
113 struct split_state sa
, sb
, sc
;
120 unsigned long test_addr
;
123 printk(KERN_INFO
"CPA self-test:\n");
125 bm
= vzalloc((max_pfn_mapped
+ 7) / 8);
127 printk(KERN_ERR
"CPA Cannot vmalloc bitmap\n");
131 failed
+= print_split(&sa
);
133 for (i
= 0; i
< NTEST
; i
++) {
134 unsigned long pfn
= prandom_u32() % max_pfn_mapped
;
136 addr
[i
] = (unsigned long)__va(pfn
<< PAGE_SHIFT
);
137 len
[i
] = prandom_u32() % 100;
138 len
[i
] = min_t(unsigned long, len
[i
], max_pfn_mapped
- pfn
- 1);
144 pte0
= pfn_pte(0, __pgprot(0)); /* shut gcc up */
146 for (k
= 0; k
< len
[i
]; k
++) {
147 pte
= lookup_address(addr
[i
] + k
*PAGE_SIZE
, &level
);
148 if (!pte
|| pgprot_val(pte_pgprot(*pte
)) == 0 ||
149 !(pte_val(*pte
) & _PAGE_PRESENT
)) {
156 if (pgprot_val(pte_pgprot(*pte
)) !=
157 pgprot_val(pte_pgprot(pte0
))) {
162 if (test_bit(pfn
+ k
, bm
)) {
166 __set_bit(pfn
+ k
, bm
);
168 if (!addr
[i
] || !pte
|| !k
) {
174 err
= change_page_attr_set(&test_addr
, len
[i
], PAGE_CPA_TEST
, 0);
176 printk(KERN_ERR
"CPA %d failed %d\n", i
, err
);
180 pte
= lookup_address(addr
[i
], &level
);
181 if (!pte
|| !pte_testbit(*pte
) || pte_huge(*pte
)) {
182 printk(KERN_ERR
"CPA %lx: bad pte %Lx\n", addr
[i
],
183 pte
? (u64
)pte_val(*pte
) : 0ULL);
186 if (level
!= PG_LEVEL_4K
) {
187 printk(KERN_ERR
"CPA %lx: unexpected level %d\n",
195 failed
+= print_split(&sb
);
197 for (i
= 0; i
< NTEST
; i
++) {
200 pte
= lookup_address(addr
[i
], &level
);
202 printk(KERN_ERR
"CPA lookup of %lx failed\n", addr
[i
]);
207 err
= change_page_attr_clear(&test_addr
, len
[i
], PAGE_CPA_TEST
, 0);
209 printk(KERN_ERR
"CPA reverting failed: %d\n", err
);
212 pte
= lookup_address(addr
[i
], &level
);
213 if (!pte
|| pte_testbit(*pte
)) {
214 printk(KERN_ERR
"CPA %lx: bad pte after revert %Lx\n",
215 addr
[i
], pte
? (u64
)pte_val(*pte
) : 0ULL);
221 failed
+= print_split(&sc
);
224 WARN(1, KERN_ERR
"NOT PASSED. Please report.\n");
228 printk(KERN_INFO
"ok.\n");
234 static int do_pageattr_test(void *__unused
)
236 while (!kthread_should_stop()) {
237 schedule_timeout_interruptible(HZ
*30);
238 if (pageattr_test() < 0)
246 static int start_pageattr_test(void)
248 struct task_struct
*p
;
250 p
= kthread_create(do_pageattr_test
, NULL
, "pageattr-test");
259 module_init(start_pageattr_test
);