2008-01-30 19:33:43 +07:00
|
|
|
/*
|
|
|
|
* self test for change_page_attr.
|
|
|
|
*
|
|
|
|
* Clears the global bit on random pages in the direct mapping, then reverts
|
|
|
|
* and compares page tables forwards and afterwards.
|
|
|
|
*/
|
2008-01-30 19:33:43 +07:00
|
|
|
#include <linux/bootmem.h>
|
2008-01-30 19:33:43 +07:00
|
|
|
#include <linux/random.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/init.h>
|
2008-01-30 19:33:43 +07:00
|
|
|
#include <linux/mm.h>
|
|
|
|
|
2008-01-30 19:33:43 +07:00
|
|
|
#include <asm/cacheflush.h>
|
|
|
|
#include <asm/pgtable.h>
|
|
|
|
#include <asm/kdebug.h>
|
|
|
|
|
|
|
|
enum {
|
2008-01-30 19:33:43 +07:00
|
|
|
NTEST = 400,
|
2008-01-30 19:33:43 +07:00
|
|
|
#ifdef CONFIG_X86_64
|
2008-01-30 19:33:43 +07:00
|
|
|
LOWEST_LEVEL = 4,
|
|
|
|
LPS = (1 << PMD_SHIFT),
|
2008-01-30 19:33:43 +07:00
|
|
|
#elif defined(CONFIG_X86_PAE)
|
2008-01-30 19:33:43 +07:00
|
|
|
LOWEST_LEVEL = 3,
|
|
|
|
LPS = (1 << PMD_SHIFT),
|
2008-01-30 19:33:43 +07:00
|
|
|
#else
|
2008-01-30 19:33:43 +07:00
|
|
|
LOWEST_LEVEL = 3, /* lookup_address lies here */
|
|
|
|
LPS = (1 << 22),
|
2008-01-30 19:33:43 +07:00
|
|
|
#endif
|
2008-01-30 19:33:43 +07:00
|
|
|
GPS = (1<<30)
|
2008-01-30 19:33:43 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef CONFIG_X86_64
|
2008-01-30 19:33:43 +07:00
|
|
|
# include <asm/proto.h>
|
|
|
|
# define max_mapped end_pfn_map
|
2008-01-30 19:33:43 +07:00
|
|
|
#else
|
2008-01-30 19:33:43 +07:00
|
|
|
# define max_mapped max_low_pfn
|
2008-01-30 19:33:43 +07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
struct split_state {
|
|
|
|
long lpg, gpg, spg, exec;
|
|
|
|
long min_exec, max_exec;
|
|
|
|
};
|
|
|
|
|
|
|
|
static __init int print_split(struct split_state *s)
|
|
|
|
{
|
|
|
|
long i, expected, missed = 0;
|
2008-01-30 19:33:43 +07:00
|
|
|
int printed = 0;
|
2008-01-30 19:33:43 +07:00
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
s->lpg = s->gpg = s->spg = s->exec = 0;
|
|
|
|
s->min_exec = ~0UL;
|
|
|
|
s->max_exec = 0;
|
|
|
|
for (i = 0; i < max_mapped; ) {
|
2008-01-30 19:33:43 +07:00
|
|
|
unsigned long addr = (unsigned long)__va(i << PAGE_SHIFT);
|
2008-01-30 19:33:43 +07:00
|
|
|
int level;
|
|
|
|
pte_t *pte;
|
|
|
|
|
2008-01-30 19:33:43 +07:00
|
|
|
pte = lookup_address(addr, &level);
|
2008-01-30 19:33:43 +07:00
|
|
|
if (!pte) {
|
|
|
|
if (!printed) {
|
2008-01-30 19:33:43 +07:00
|
|
|
dump_pagetable(addr);
|
|
|
|
printk(KERN_INFO "CPA %lx no pte level %d\n",
|
|
|
|
addr, level);
|
2008-01-30 19:33:43 +07:00
|
|
|
printed = 1;
|
|
|
|
}
|
|
|
|
missed++;
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (level == 2 && sizeof(long) == 8) {
|
|
|
|
s->gpg++;
|
|
|
|
i += GPS/PAGE_SIZE;
|
|
|
|
} else if (level != LOWEST_LEVEL) {
|
|
|
|
if (!(pte_val(*pte) & _PAGE_PSE)) {
|
2008-01-30 19:33:43 +07:00
|
|
|
printk(KERN_ERR
|
|
|
|
"%lx level %d but not PSE %Lx\n",
|
|
|
|
addr, level, (u64)pte_val(*pte));
|
2008-01-30 19:33:43 +07:00
|
|
|
err = 1;
|
|
|
|
}
|
|
|
|
s->lpg++;
|
|
|
|
i += LPS/PAGE_SIZE;
|
|
|
|
} else {
|
|
|
|
s->spg++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (!(pte_val(*pte) & _PAGE_NX)) {
|
|
|
|
s->exec++;
|
2008-01-30 19:33:43 +07:00
|
|
|
if (addr < s->min_exec)
|
|
|
|
s->min_exec = addr;
|
|
|
|
if (addr > s->max_exec)
|
|
|
|
s->max_exec = addr;
|
2008-01-30 19:33:43 +07:00
|
|
|
}
|
|
|
|
}
|
2008-01-30 19:33:43 +07:00
|
|
|
printk(KERN_INFO
|
|
|
|
"CPA mapping 4k %lu large %lu gb %lu x %lu[%lx-%lx] miss %lu\n",
|
2008-01-30 19:33:43 +07:00
|
|
|
s->spg, s->lpg, s->gpg, s->exec,
|
|
|
|
s->min_exec != ~0UL ? s->min_exec : 0, s->max_exec, missed);
|
2008-01-30 19:33:43 +07:00
|
|
|
|
2008-01-30 19:33:43 +07:00
|
|
|
expected = (s->gpg*GPS + s->lpg*LPS)/PAGE_SIZE + s->spg + missed;
|
|
|
|
if (expected != i) {
|
2008-01-30 19:33:43 +07:00
|
|
|
printk(KERN_ERR "CPA max_mapped %lu but expected %lu\n",
|
2008-01-30 19:33:43 +07:00
|
|
|
max_mapped, expected);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __init int state_same(struct split_state *a, struct split_state *b)
|
|
|
|
{
|
|
|
|
return a->lpg == b->lpg && a->gpg == b->gpg && a->spg == b->spg &&
|
2008-01-30 19:33:43 +07:00
|
|
|
a->exec == b->exec;
|
2008-01-30 19:33:43 +07:00
|
|
|
}
|
|
|
|
|
2008-01-30 19:33:43 +07:00
|
|
|
static unsigned long __initdata addr[NTEST];
|
|
|
|
static unsigned int __initdata len[NTEST];
|
2008-01-30 19:33:43 +07:00
|
|
|
|
|
|
|
/* Change the global bit on random pages in the direct mapping */
|
|
|
|
static __init int exercise_pageattr(void)
|
|
|
|
{
|
2008-01-30 19:33:43 +07:00
|
|
|
struct split_state sa, sb, sc;
|
|
|
|
unsigned long *bm;
|
2008-01-30 19:33:43 +07:00
|
|
|
pte_t *pte, pte0;
|
2008-01-30 19:33:43 +07:00
|
|
|
int failed = 0;
|
2008-01-30 19:33:43 +07:00
|
|
|
int level;
|
2008-01-30 19:33:43 +07:00
|
|
|
int i, k;
|
2008-01-30 19:33:43 +07:00
|
|
|
int err;
|
|
|
|
|
2008-01-30 19:33:43 +07:00
|
|
|
printk(KERN_INFO "CPA exercising pageattr\n");
|
2008-01-30 19:33:43 +07:00
|
|
|
|
|
|
|
bm = vmalloc((max_mapped + 7) / 8);
|
|
|
|
if (!bm) {
|
2008-01-30 19:33:43 +07:00
|
|
|
printk(KERN_ERR "CPA Cannot vmalloc bitmap\n");
|
2008-01-30 19:33:43 +07:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
memset(bm, 0, (max_mapped + 7) / 8);
|
|
|
|
|
|
|
|
failed += print_split(&sa);
|
|
|
|
srandom32(100);
|
2008-01-30 19:33:43 +07:00
|
|
|
|
2008-01-30 19:33:43 +07:00
|
|
|
for (i = 0; i < NTEST; i++) {
|
|
|
|
unsigned long pfn = random32() % max_mapped;
|
2008-01-30 19:33:43 +07:00
|
|
|
|
2008-01-30 19:33:43 +07:00
|
|
|
addr[i] = (unsigned long)__va(pfn << PAGE_SHIFT);
|
|
|
|
len[i] = random32() % 100;
|
|
|
|
len[i] = min_t(unsigned long, len[i], max_mapped - pfn - 1);
|
2008-01-30 19:33:43 +07:00
|
|
|
|
2008-01-30 19:33:43 +07:00
|
|
|
if (len[i] == 0)
|
|
|
|
len[i] = 1;
|
|
|
|
|
|
|
|
pte = NULL;
|
|
|
|
pte0 = pfn_pte(0, __pgprot(0)); /* shut gcc up */
|
2008-01-30 19:33:43 +07:00
|
|
|
|
2008-01-30 19:33:43 +07:00
|
|
|
for (k = 0; k < len[i]; k++) {
|
|
|
|
pte = lookup_address(addr[i] + k*PAGE_SIZE, &level);
|
|
|
|
if (!pte || pgprot_val(pte_pgprot(*pte)) == 0) {
|
|
|
|
addr[i] = 0;
|
|
|
|
break;
|
|
|
|
}
|
2008-01-30 19:33:43 +07:00
|
|
|
if (k == 0) {
|
2008-01-30 19:33:43 +07:00
|
|
|
pte0 = *pte;
|
2008-01-30 19:33:43 +07:00
|
|
|
} else {
|
|
|
|
if (pgprot_val(pte_pgprot(*pte)) !=
|
2008-01-30 19:33:43 +07:00
|
|
|
pgprot_val(pte_pgprot(pte0))) {
|
2008-01-30 19:33:43 +07:00
|
|
|
len[i] = k;
|
|
|
|
break;
|
|
|
|
}
|
2008-01-30 19:33:43 +07:00
|
|
|
}
|
|
|
|
if (test_bit(pfn + k, bm)) {
|
|
|
|
len[i] = k;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
__set_bit(pfn + k, bm);
|
|
|
|
}
|
|
|
|
if (!addr[i] || !pte || !k) {
|
|
|
|
addr[i] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = change_page_attr(virt_to_page(addr[i]), len[i],
|
|
|
|
pte_pgprot(pte_clrhuge(pte_clrglobal(pte0))));
|
|
|
|
if (err < 0) {
|
2008-01-30 19:33:43 +07:00
|
|
|
printk(KERN_ERR "CPA %d failed %d\n", i, err);
|
2008-01-30 19:33:43 +07:00
|
|
|
failed++;
|
|
|
|
}
|
|
|
|
|
|
|
|
pte = lookup_address(addr[i], &level);
|
|
|
|
if (!pte || pte_global(*pte) || pte_huge(*pte)) {
|
2008-01-30 19:33:43 +07:00
|
|
|
printk(KERN_ERR "CPA %lx: bad pte %Lx\n", addr[i],
|
2008-01-30 19:33:43 +07:00
|
|
|
pte ? (u64)pte_val(*pte) : 0ULL);
|
|
|
|
failed++;
|
|
|
|
}
|
|
|
|
if (level != LOWEST_LEVEL) {
|
2008-01-30 19:33:43 +07:00
|
|
|
printk(KERN_ERR "CPA %lx: unexpected level %d\n",
|
|
|
|
addr[i], level);
|
2008-01-30 19:33:43 +07:00
|
|
|
failed++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
vfree(bm);
|
|
|
|
global_flush_tlb();
|
|
|
|
|
|
|
|
failed += print_split(&sb);
|
|
|
|
|
2008-01-30 19:33:43 +07:00
|
|
|
printk(KERN_INFO "CPA reverting everything\n");
|
2008-01-30 19:33:43 +07:00
|
|
|
for (i = 0; i < NTEST; i++) {
|
|
|
|
if (!addr[i])
|
|
|
|
continue;
|
|
|
|
pte = lookup_address(addr[i], &level);
|
|
|
|
if (!pte) {
|
2008-01-30 19:33:43 +07:00
|
|
|
printk(KERN_ERR "CPA lookup of %lx failed\n", addr[i]);
|
2008-01-30 19:33:43 +07:00
|
|
|
failed++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
err = change_page_attr(virt_to_page(addr[i]), len[i],
|
|
|
|
pte_pgprot(pte_mkglobal(*pte)));
|
|
|
|
if (err < 0) {
|
2008-01-30 19:33:43 +07:00
|
|
|
printk(KERN_ERR "CPA reverting failed: %d\n", err);
|
2008-01-30 19:33:43 +07:00
|
|
|
failed++;
|
|
|
|
}
|
|
|
|
pte = lookup_address(addr[i], &level);
|
|
|
|
if (!pte || !pte_global(*pte)) {
|
2008-01-30 19:33:43 +07:00
|
|
|
printk(KERN_ERR "CPA %lx: bad pte after revert %Lx\n",
|
|
|
|
addr[i], pte ? (u64)pte_val(*pte) : 0ULL);
|
2008-01-30 19:33:43 +07:00
|
|
|
failed++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
global_flush_tlb();
|
|
|
|
|
|
|
|
failed += print_split(&sc);
|
|
|
|
if (!state_same(&sa, &sc))
|
|
|
|
failed++;
|
|
|
|
|
|
|
|
if (failed)
|
2008-01-30 19:33:43 +07:00
|
|
|
printk(KERN_ERR "CPA selftests NOT PASSED. Please report.\n");
|
2008-01-30 19:33:43 +07:00
|
|
|
else
|
2008-01-30 19:33:43 +07:00
|
|
|
printk(KERN_INFO "CPA selftests PASSED\n");
|
2008-01-30 19:33:43 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
module_init(exercise_pageattr);
|