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>
20 LPS
= (1 << PMD_SHIFT
),
21 #elif defined(CONFIG_X86_PAE)
22 LPS
= (1 << PMD_SHIFT
),
30 long lpg
, gpg
, spg
, exec
;
31 long min_exec
, max_exec
;
34 static __init
int print_split(struct split_state
*s
)
36 long i
, expected
, missed
= 0;
40 s
->lpg
= s
->gpg
= s
->spg
= s
->exec
= 0;
43 for (i
= 0; i
< max_pfn_mapped
; ) {
44 unsigned long addr
= (unsigned long)__va(i
<< PAGE_SHIFT
);
48 pte
= lookup_address(addr
, &level
);
52 printk(KERN_INFO
"CPA %lx no pte level %d\n",
61 if (level
== PG_LEVEL_1G
&& sizeof(long) == 8) {
64 } else if (level
== PG_LEVEL_2M
) {
65 if (!(pte_val(*pte
) & _PAGE_PSE
)) {
67 "%lx level %d but not PSE %Lx\n",
68 addr
, level
, (u64
)pte_val(*pte
));
77 if (!(pte_val(*pte
) & _PAGE_NX
)) {
79 if (addr
< s
->min_exec
)
81 if (addr
> s
->max_exec
)
86 "CPA mapping 4k %lu large %lu gb %lu x %lu[%lx-%lx] miss %lu\n",
87 s
->spg
, s
->lpg
, s
->gpg
, s
->exec
,
88 s
->min_exec
!= ~0UL ? s
->min_exec
: 0, s
->max_exec
, missed
);
90 expected
= (s
->gpg
*GPS
+ s
->lpg
*LPS
)/PAGE_SIZE
+ s
->spg
+ missed
;
92 printk(KERN_ERR
"CPA max_pfn_mapped %lu but expected %lu\n",
93 max_pfn_mapped
, expected
);
99 static unsigned long __initdata addr
[NTEST
];
100 static unsigned int __initdata len
[NTEST
];
102 /* Change the global bit on random pages in the direct mapping */
103 static __init
int exercise_pageattr(void)
105 struct split_state sa
, sb
, sc
;
113 printk(KERN_INFO
"CPA exercising pageattr\n");
115 bm
= vmalloc((max_pfn_mapped
+ 7) / 8);
117 printk(KERN_ERR
"CPA Cannot vmalloc bitmap\n");
120 memset(bm
, 0, (max_pfn_mapped
+ 7) / 8);
122 failed
+= print_split(&sa
);
125 for (i
= 0; i
< NTEST
; i
++) {
126 unsigned long pfn
= random32() % max_pfn_mapped
;
128 addr
[i
] = (unsigned long)__va(pfn
<< PAGE_SHIFT
);
129 len
[i
] = random32() % 100;
130 len
[i
] = min_t(unsigned long, len
[i
], max_pfn_mapped
- pfn
- 1);
136 pte0
= pfn_pte(0, __pgprot(0)); /* shut gcc up */
138 for (k
= 0; k
< len
[i
]; k
++) {
139 pte
= lookup_address(addr
[i
] + k
*PAGE_SIZE
, &level
);
140 if (!pte
|| pgprot_val(pte_pgprot(*pte
)) == 0) {
147 if (pgprot_val(pte_pgprot(*pte
)) !=
148 pgprot_val(pte_pgprot(pte0
))) {
153 if (test_bit(pfn
+ k
, bm
)) {
157 __set_bit(pfn
+ k
, bm
);
159 if (!addr
[i
] || !pte
|| !k
) {
164 err
= change_page_attr_clear(addr
[i
], len
[i
],
165 __pgprot(_PAGE_GLOBAL
));
167 printk(KERN_ERR
"CPA %d failed %d\n", i
, err
);
171 pte
= lookup_address(addr
[i
], &level
);
172 if (!pte
|| pte_global(*pte
) || pte_huge(*pte
)) {
173 printk(KERN_ERR
"CPA %lx: bad pte %Lx\n", addr
[i
],
174 pte
? (u64
)pte_val(*pte
) : 0ULL);
177 if (level
!= PG_LEVEL_4K
) {
178 printk(KERN_ERR
"CPA %lx: unexpected level %d\n",
186 failed
+= print_split(&sb
);
188 printk(KERN_INFO
"CPA reverting everything\n");
189 for (i
= 0; i
< NTEST
; i
++) {
192 pte
= lookup_address(addr
[i
], &level
);
194 printk(KERN_ERR
"CPA lookup of %lx failed\n", addr
[i
]);
198 err
= change_page_attr_set(addr
[i
], len
[i
],
199 __pgprot(_PAGE_GLOBAL
));
201 printk(KERN_ERR
"CPA reverting failed: %d\n", err
);
204 pte
= lookup_address(addr
[i
], &level
);
205 if (!pte
|| !pte_global(*pte
)) {
206 printk(KERN_ERR
"CPA %lx: bad pte after revert %Lx\n",
207 addr
[i
], pte
? (u64
)pte_val(*pte
) : 0ULL);
213 failed
+= print_split(&sc
);
216 printk(KERN_ERR
"CPA selftests NOT PASSED. Please report.\n");
219 printk(KERN_INFO
"CPA selftests PASSED\n");
224 module_init(exercise_pageattr
);