blob: 0dce0e248a42c7b7556b175052b554acc7e12a9b [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,
Andi Kleenfa2d8362008-01-30 13:33:43 +010019#ifdef CONFIG_X86_64
Ingo Molnar851339b2008-01-30 13:33:43 +010020 LOWEST_LEVEL = 4,
21 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 LOWEST_LEVEL = 3,
24 LPS = (1 << PMD_SHIFT),
Andi Kleenfa2d8362008-01-30 13:33:43 +010025#else
Ingo Molnar851339b2008-01-30 13:33:43 +010026 LOWEST_LEVEL = 3, /* lookup_address lies here */
27 LPS = (1 << 22),
Andi Kleenfa2d8362008-01-30 13:33:43 +010028#endif
Ingo Molnar851339b2008-01-30 13:33:43 +010029 GPS = (1<<30)
Andi Kleenfa2d8362008-01-30 13:33:43 +010030};
31
32#ifdef CONFIG_X86_64
Ingo Molnar851339b2008-01-30 13:33:43 +010033# include <asm/proto.h>
34# define max_mapped end_pfn_map
Andi Kleenfa2d8362008-01-30 13:33:43 +010035#else
Ingo Molnar851339b2008-01-30 13:33:43 +010036# define max_mapped max_low_pfn
Andi Kleenfa2d8362008-01-30 13:33:43 +010037#endif
38
39struct split_state {
40 long lpg, gpg, spg, exec;
41 long min_exec, max_exec;
42};
43
44static __init int print_split(struct split_state *s)
45{
Andi Kleenfa2d8362008-01-30 13:33:43 +010046 long i, expected, missed = 0;
Ingo Molnar851339b2008-01-30 13:33:43 +010047 int printed = 0;
Andi Kleenfa2d8362008-01-30 13:33:43 +010048 int err = 0;
49
50 s->lpg = s->gpg = s->spg = s->exec = 0;
51 s->min_exec = ~0UL;
52 s->max_exec = 0;
53 for (i = 0; i < max_mapped; ) {
Ingo Molnar851339b2008-01-30 13:33:43 +010054 unsigned long addr = (unsigned long)__va(i << PAGE_SHIFT);
Andi Kleenfa2d8362008-01-30 13:33:43 +010055 int level;
56 pte_t *pte;
Andi Kleenfa2d8362008-01-30 13:33:43 +010057
Ingo Molnar851339b2008-01-30 13:33:43 +010058 pte = lookup_address(addr, &level);
Andi Kleenfa2d8362008-01-30 13:33:43 +010059 if (!pte) {
60 if (!printed) {
Ingo Molnar851339b2008-01-30 13:33:43 +010061 dump_pagetable(addr);
62 printk(KERN_INFO "CPA %lx no pte level %d\n",
63 addr, level);
Andi Kleenfa2d8362008-01-30 13:33:43 +010064 printed = 1;
65 }
66 missed++;
67 i++;
68 continue;
69 }
70
71 if (level == 2 && sizeof(long) == 8) {
72 s->gpg++;
73 i += GPS/PAGE_SIZE;
74 } else if (level != LOWEST_LEVEL) {
75 if (!(pte_val(*pte) & _PAGE_PSE)) {
Ingo Molnar851339b2008-01-30 13:33:43 +010076 printk(KERN_ERR
77 "%lx level %d but not PSE %Lx\n",
78 addr, level, (u64)pte_val(*pte));
Andi Kleenfa2d8362008-01-30 13:33:43 +010079 err = 1;
80 }
81 s->lpg++;
82 i += LPS/PAGE_SIZE;
83 } else {
84 s->spg++;
85 i++;
86 }
87 if (!(pte_val(*pte) & _PAGE_NX)) {
88 s->exec++;
Ingo Molnar851339b2008-01-30 13:33:43 +010089 if (addr < s->min_exec)
90 s->min_exec = addr;
91 if (addr > s->max_exec)
92 s->max_exec = addr;
Andi Kleenfa2d8362008-01-30 13:33:43 +010093 }
94 }
Ingo Molnar851339b2008-01-30 13:33:43 +010095 printk(KERN_INFO
96 "CPA mapping 4k %lu large %lu gb %lu x %lu[%lx-%lx] miss %lu\n",
Andi Kleenfa2d8362008-01-30 13:33:43 +010097 s->spg, s->lpg, s->gpg, s->exec,
98 s->min_exec != ~0UL ? s->min_exec : 0, s->max_exec, missed);
Ingo Molnar851339b2008-01-30 13:33:43 +010099
Andi Kleenfa2d8362008-01-30 13:33:43 +0100100 expected = (s->gpg*GPS + s->lpg*LPS)/PAGE_SIZE + s->spg + missed;
101 if (expected != i) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100102 printk(KERN_ERR "CPA max_mapped %lu but expected %lu\n",
Andi Kleenfa2d8362008-01-30 13:33:43 +0100103 max_mapped, expected);
104 return 1;
105 }
106 return err;
107}
108
Ingo Molnar851339b2008-01-30 13:33:43 +0100109static unsigned long __initdata addr[NTEST];
110static unsigned int __initdata len[NTEST];
Andi Kleenfa2d8362008-01-30 13:33:43 +0100111
112/* Change the global bit on random pages in the direct mapping */
113static __init int exercise_pageattr(void)
114{
Andi Kleenfa2d8362008-01-30 13:33:43 +0100115 struct split_state sa, sb, sc;
Andi Kleenfa2d8362008-01-30 13:33:43 +0100116 unsigned long *bm;
Ingo Molnar851339b2008-01-30 13:33:43 +0100117 pte_t *pte, pte0;
118 int failed = 0;
119 int level;
120 int i, k;
121 int err;
Andi Kleenfa2d8362008-01-30 13:33:43 +0100122
Ingo Molnar851339b2008-01-30 13:33:43 +0100123 printk(KERN_INFO "CPA exercising pageattr\n");
Andi Kleenfa2d8362008-01-30 13:33:43 +0100124
125 bm = vmalloc((max_mapped + 7) / 8);
126 if (!bm) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100127 printk(KERN_ERR "CPA Cannot vmalloc bitmap\n");
Andi Kleenfa2d8362008-01-30 13:33:43 +0100128 return -ENOMEM;
129 }
130 memset(bm, 0, (max_mapped + 7) / 8);
131
132 failed += print_split(&sa);
133 srandom32(100);
Ingo Molnar851339b2008-01-30 13:33:43 +0100134
Andi Kleenfa2d8362008-01-30 13:33:43 +0100135 for (i = 0; i < NTEST; i++) {
136 unsigned long pfn = random32() % max_mapped;
Ingo Molnar851339b2008-01-30 13:33:43 +0100137
Andi Kleenfa2d8362008-01-30 13:33:43 +0100138 addr[i] = (unsigned long)__va(pfn << PAGE_SHIFT);
139 len[i] = random32() % 100;
140 len[i] = min_t(unsigned long, len[i], max_mapped - pfn - 1);
Ingo Molnar851339b2008-01-30 13:33:43 +0100141
Andi Kleenfa2d8362008-01-30 13:33:43 +0100142 if (len[i] == 0)
143 len[i] = 1;
144
145 pte = NULL;
146 pte0 = pfn_pte(0, __pgprot(0)); /* shut gcc up */
Ingo Molnar851339b2008-01-30 13:33:43 +0100147
Andi Kleenfa2d8362008-01-30 13:33:43 +0100148 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 addr[i] = 0;
152 break;
153 }
Ingo Molnar851339b2008-01-30 13:33:43 +0100154 if (k == 0) {
Andi Kleenfa2d8362008-01-30 13:33:43 +0100155 pte0 = *pte;
Ingo Molnar851339b2008-01-30 13:33:43 +0100156 } else {
157 if (pgprot_val(pte_pgprot(*pte)) !=
Andi Kleenfa2d8362008-01-30 13:33:43 +0100158 pgprot_val(pte_pgprot(pte0))) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100159 len[i] = k;
160 break;
161 }
Andi Kleenfa2d8362008-01-30 13:33:43 +0100162 }
163 if (test_bit(pfn + k, bm)) {
164 len[i] = k;
165 break;
166 }
167 __set_bit(pfn + k, bm);
168 }
169 if (!addr[i] || !pte || !k) {
170 addr[i] = 0;
171 continue;
172 }
173
Andi Kleen6ba9b7d2008-01-30 13:33:52 +0100174 err = change_page_attr_addr(addr[i], len[i],
Andi Kleenfa2d8362008-01-30 13:33:43 +0100175 pte_pgprot(pte_clrhuge(pte_clrglobal(pte0))));
176 if (err < 0) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100177 printk(KERN_ERR "CPA %d failed %d\n", i, err);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100178 failed++;
179 }
180
181 pte = lookup_address(addr[i], &level);
182 if (!pte || pte_global(*pte) || pte_huge(*pte)) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100183 printk(KERN_ERR "CPA %lx: bad pte %Lx\n", addr[i],
Andi Kleenfa2d8362008-01-30 13:33:43 +0100184 pte ? (u64)pte_val(*pte) : 0ULL);
185 failed++;
186 }
187 if (level != LOWEST_LEVEL) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100188 printk(KERN_ERR "CPA %lx: unexpected level %d\n",
189 addr[i], level);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100190 failed++;
191 }
192
193 }
194 vfree(bm);
195 global_flush_tlb();
196
197 failed += print_split(&sb);
198
Ingo Molnar851339b2008-01-30 13:33:43 +0100199 printk(KERN_INFO "CPA reverting everything\n");
Andi Kleenfa2d8362008-01-30 13:33:43 +0100200 for (i = 0; i < NTEST; i++) {
201 if (!addr[i])
202 continue;
203 pte = lookup_address(addr[i], &level);
204 if (!pte) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100205 printk(KERN_ERR "CPA lookup of %lx failed\n", addr[i]);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100206 failed++;
207 continue;
208 }
Andi Kleen6ba9b7d2008-01-30 13:33:52 +0100209 err = change_page_attr_addr(addr[i], len[i],
Andi Kleenfa2d8362008-01-30 13:33:43 +0100210 pte_pgprot(pte_mkglobal(*pte)));
211 if (err < 0) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100212 printk(KERN_ERR "CPA reverting failed: %d\n", err);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100213 failed++;
214 }
215 pte = lookup_address(addr[i], &level);
216 if (!pte || !pte_global(*pte)) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100217 printk(KERN_ERR "CPA %lx: bad pte after revert %Lx\n",
218 addr[i], pte ? (u64)pte_val(*pte) : 0ULL);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100219 failed++;
220 }
221
222 }
223 global_flush_tlb();
224
225 failed += print_split(&sc);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100226
Ingo Molnar55ce29b2008-01-30 13:33:58 +0100227 if (failed) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100228 printk(KERN_ERR "CPA selftests NOT PASSED. Please report.\n");
Ingo Molnar55ce29b2008-01-30 13:33:58 +0100229 WARN_ON(1);
230 } else {
Ingo Molnar851339b2008-01-30 13:33:43 +0100231 printk(KERN_INFO "CPA selftests PASSED\n");
Ingo Molnar55ce29b2008-01-30 13:33:58 +0100232 }
Andi Kleenfa2d8362008-01-30 13:33:43 +0100233
234 return 0;
235}
Andi Kleenfa2d8362008-01-30 13:33:43 +0100236module_init(exercise_pageattr);