blob: d7a93008cc12ec5598d28fc9cfd9c8e72601b3dc [file] [log] [blame]
Andi Kleenfa2d8362008-01-30 13:33:43 +01001/*
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 Molnar851339b2008-01-30 13:33:43 +01007#include <linux/bootmem.h>
Andi Kleenfa2d8362008-01-30 13:33:43 +01008#include <linux/random.h>
9#include <linux/kernel.h>
10#include <linux/init.h>
Ingo Molnar851339b2008-01-30 13:33:43 +010011#include <linux/mm.h>
12
Andi Kleenfa2d8362008-01-30 13:33:43 +010013#include <asm/cacheflush.h>
14#include <asm/pgtable.h>
15#include <asm/kdebug.h>
16
17enum {
Ingo Molnar851339b2008-01-30 13:33:43 +010018 NTEST = 400,
Thomas Gleixner30551bb2008-01-30 13:34:04 +010019 LOWEST_LEVEL = PG_LEVEL_4K,
Andi Kleenfa2d8362008-01-30 13:33:43 +010020#ifdef CONFIG_X86_64
Ingo Molnar851339b2008-01-30 13:33:43 +010021 LPS = (1 << PMD_SHIFT),
Andi Kleenfa2d8362008-01-30 13:33:43 +010022#elif defined(CONFIG_X86_PAE)
Ingo Molnar851339b2008-01-30 13:33:43 +010023 LPS = (1 << PMD_SHIFT),
Andi Kleenfa2d8362008-01-30 13:33:43 +010024#else
Ingo Molnar851339b2008-01-30 13:33:43 +010025 LPS = (1 << 22),
Andi Kleenfa2d8362008-01-30 13:33:43 +010026#endif
Ingo Molnar851339b2008-01-30 13:33:43 +010027 GPS = (1<<30)
Andi Kleenfa2d8362008-01-30 13:33:43 +010028};
29
30#ifdef CONFIG_X86_64
Ingo Molnar851339b2008-01-30 13:33:43 +010031# include <asm/proto.h>
32# define max_mapped end_pfn_map
Andi Kleenfa2d8362008-01-30 13:33:43 +010033#else
Ingo Molnar851339b2008-01-30 13:33:43 +010034# define max_mapped max_low_pfn
Andi Kleenfa2d8362008-01-30 13:33:43 +010035#endif
36
37struct split_state {
38 long lpg, gpg, spg, exec;
39 long min_exec, max_exec;
40};
41
42static __init int print_split(struct split_state *s)
43{
Andi Kleenfa2d8362008-01-30 13:33:43 +010044 long i, expected, missed = 0;
Ingo Molnar851339b2008-01-30 13:33:43 +010045 int printed = 0;
Andi Kleenfa2d8362008-01-30 13:33:43 +010046 int err = 0;
47
48 s->lpg = s->gpg = s->spg = s->exec = 0;
49 s->min_exec = ~0UL;
50 s->max_exec = 0;
51 for (i = 0; i < max_mapped; ) {
Ingo Molnar851339b2008-01-30 13:33:43 +010052 unsigned long addr = (unsigned long)__va(i << PAGE_SHIFT);
Andi Kleenfa2d8362008-01-30 13:33:43 +010053 int level;
54 pte_t *pte;
Andi Kleenfa2d8362008-01-30 13:33:43 +010055
Ingo Molnar851339b2008-01-30 13:33:43 +010056 pte = lookup_address(addr, &level);
Andi Kleenfa2d8362008-01-30 13:33:43 +010057 if (!pte) {
58 if (!printed) {
Ingo Molnar851339b2008-01-30 13:33:43 +010059 dump_pagetable(addr);
60 printk(KERN_INFO "CPA %lx no pte level %d\n",
61 addr, level);
Andi Kleenfa2d8362008-01-30 13:33:43 +010062 printed = 1;
63 }
64 missed++;
65 i++;
66 continue;
67 }
68
69 if (level == 2 && sizeof(long) == 8) {
70 s->gpg++;
71 i += GPS/PAGE_SIZE;
72 } else if (level != LOWEST_LEVEL) {
73 if (!(pte_val(*pte) & _PAGE_PSE)) {
Ingo Molnar851339b2008-01-30 13:33:43 +010074 printk(KERN_ERR
75 "%lx level %d but not PSE %Lx\n",
76 addr, level, (u64)pte_val(*pte));
Andi Kleenfa2d8362008-01-30 13:33:43 +010077 err = 1;
78 }
79 s->lpg++;
80 i += LPS/PAGE_SIZE;
81 } else {
82 s->spg++;
83 i++;
84 }
85 if (!(pte_val(*pte) & _PAGE_NX)) {
86 s->exec++;
Ingo Molnar851339b2008-01-30 13:33:43 +010087 if (addr < s->min_exec)
88 s->min_exec = addr;
89 if (addr > s->max_exec)
90 s->max_exec = addr;
Andi Kleenfa2d8362008-01-30 13:33:43 +010091 }
92 }
Ingo Molnar851339b2008-01-30 13:33:43 +010093 printk(KERN_INFO
94 "CPA mapping 4k %lu large %lu gb %lu x %lu[%lx-%lx] miss %lu\n",
Andi Kleenfa2d8362008-01-30 13:33:43 +010095 s->spg, s->lpg, s->gpg, s->exec,
96 s->min_exec != ~0UL ? s->min_exec : 0, s->max_exec, missed);
Ingo Molnar851339b2008-01-30 13:33:43 +010097
Andi Kleenfa2d8362008-01-30 13:33:43 +010098 expected = (s->gpg*GPS + s->lpg*LPS)/PAGE_SIZE + s->spg + missed;
99 if (expected != i) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100100 printk(KERN_ERR "CPA max_mapped %lu but expected %lu\n",
Andi Kleenfa2d8362008-01-30 13:33:43 +0100101 max_mapped, expected);
102 return 1;
103 }
104 return err;
105}
106
Ingo Molnar851339b2008-01-30 13:33:43 +0100107static unsigned long __initdata addr[NTEST];
108static unsigned int __initdata len[NTEST];
Andi Kleenfa2d8362008-01-30 13:33:43 +0100109
110/* Change the global bit on random pages in the direct mapping */
111static __init int exercise_pageattr(void)
112{
Andi Kleenfa2d8362008-01-30 13:33:43 +0100113 struct split_state sa, sb, sc;
Andi Kleenfa2d8362008-01-30 13:33:43 +0100114 unsigned long *bm;
Ingo Molnar851339b2008-01-30 13:33:43 +0100115 pte_t *pte, pte0;
116 int failed = 0;
117 int level;
118 int i, k;
119 int err;
Andi Kleenfa2d8362008-01-30 13:33:43 +0100120
Ingo Molnar851339b2008-01-30 13:33:43 +0100121 printk(KERN_INFO "CPA exercising pageattr\n");
Andi Kleenfa2d8362008-01-30 13:33:43 +0100122
123 bm = vmalloc((max_mapped + 7) / 8);
124 if (!bm) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100125 printk(KERN_ERR "CPA Cannot vmalloc bitmap\n");
Andi Kleenfa2d8362008-01-30 13:33:43 +0100126 return -ENOMEM;
127 }
128 memset(bm, 0, (max_mapped + 7) / 8);
129
130 failed += print_split(&sa);
131 srandom32(100);
Ingo Molnar851339b2008-01-30 13:33:43 +0100132
Andi Kleenfa2d8362008-01-30 13:33:43 +0100133 for (i = 0; i < NTEST; i++) {
134 unsigned long pfn = random32() % max_mapped;
Ingo Molnar851339b2008-01-30 13:33:43 +0100135
Andi Kleenfa2d8362008-01-30 13:33:43 +0100136 addr[i] = (unsigned long)__va(pfn << PAGE_SHIFT);
137 len[i] = random32() % 100;
138 len[i] = min_t(unsigned long, len[i], max_mapped - pfn - 1);
Ingo Molnar851339b2008-01-30 13:33:43 +0100139
Andi Kleenfa2d8362008-01-30 13:33:43 +0100140 if (len[i] == 0)
141 len[i] = 1;
142
143 pte = NULL;
144 pte0 = pfn_pte(0, __pgprot(0)); /* shut gcc up */
Ingo Molnar851339b2008-01-30 13:33:43 +0100145
Andi Kleenfa2d8362008-01-30 13:33:43 +0100146 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 addr[i] = 0;
150 break;
151 }
Ingo Molnar851339b2008-01-30 13:33:43 +0100152 if (k == 0) {
Andi Kleenfa2d8362008-01-30 13:33:43 +0100153 pte0 = *pte;
Ingo Molnar851339b2008-01-30 13:33:43 +0100154 } else {
155 if (pgprot_val(pte_pgprot(*pte)) !=
Andi Kleenfa2d8362008-01-30 13:33:43 +0100156 pgprot_val(pte_pgprot(pte0))) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100157 len[i] = k;
158 break;
159 }
Andi Kleenfa2d8362008-01-30 13:33:43 +0100160 }
161 if (test_bit(pfn + k, bm)) {
162 len[i] = k;
163 break;
164 }
165 __set_bit(pfn + k, bm);
166 }
167 if (!addr[i] || !pte || !k) {
168 addr[i] = 0;
169 continue;
170 }
171
Andi Kleen6ba9b7d2008-01-30 13:33:52 +0100172 err = change_page_attr_addr(addr[i], len[i],
Andi Kleenfa2d8362008-01-30 13:33:43 +0100173 pte_pgprot(pte_clrhuge(pte_clrglobal(pte0))));
174 if (err < 0) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100175 printk(KERN_ERR "CPA %d failed %d\n", i, err);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100176 failed++;
177 }
178
179 pte = lookup_address(addr[i], &level);
180 if (!pte || pte_global(*pte) || pte_huge(*pte)) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100181 printk(KERN_ERR "CPA %lx: bad pte %Lx\n", addr[i],
Andi Kleenfa2d8362008-01-30 13:33:43 +0100182 pte ? (u64)pte_val(*pte) : 0ULL);
183 failed++;
184 }
185 if (level != LOWEST_LEVEL) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100186 printk(KERN_ERR "CPA %lx: unexpected level %d\n",
187 addr[i], level);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100188 failed++;
189 }
190
191 }
192 vfree(bm);
193 global_flush_tlb();
194
195 failed += print_split(&sb);
196
Ingo Molnar851339b2008-01-30 13:33:43 +0100197 printk(KERN_INFO "CPA reverting everything\n");
Andi Kleenfa2d8362008-01-30 13:33:43 +0100198 for (i = 0; i < NTEST; i++) {
199 if (!addr[i])
200 continue;
201 pte = lookup_address(addr[i], &level);
202 if (!pte) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100203 printk(KERN_ERR "CPA lookup of %lx failed\n", addr[i]);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100204 failed++;
205 continue;
206 }
Andi Kleen6ba9b7d2008-01-30 13:33:52 +0100207 err = change_page_attr_addr(addr[i], len[i],
Andi Kleenfa2d8362008-01-30 13:33:43 +0100208 pte_pgprot(pte_mkglobal(*pte)));
209 if (err < 0) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100210 printk(KERN_ERR "CPA reverting failed: %d\n", err);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100211 failed++;
212 }
213 pte = lookup_address(addr[i], &level);
214 if (!pte || !pte_global(*pte)) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100215 printk(KERN_ERR "CPA %lx: bad pte after revert %Lx\n",
216 addr[i], pte ? (u64)pte_val(*pte) : 0ULL);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100217 failed++;
218 }
219
220 }
221 global_flush_tlb();
222
223 failed += print_split(&sc);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100224
Ingo Molnar55ce29b2008-01-30 13:33:58 +0100225 if (failed) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100226 printk(KERN_ERR "CPA selftests NOT PASSED. Please report.\n");
Ingo Molnar55ce29b2008-01-30 13:33:58 +0100227 WARN_ON(1);
228 } else {
Ingo Molnar851339b2008-01-30 13:33:43 +0100229 printk(KERN_INFO "CPA selftests PASSED\n");
Ingo Molnar55ce29b2008-01-30 13:33:58 +0100230 }
Andi Kleenfa2d8362008-01-30 13:33:43 +0100231
232 return 0;
233}
Andi Kleenfa2d8362008-01-30 13:33:43 +0100234module_init(exercise_pageattr);