From: Andrew Cooper Date: Fri, 8 Jan 2016 17:44:13 +0000 (+0000) Subject: Unmap the page at 0 to catch errors with NULL pointers X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=f473e9222f6c6adeae9fb3316599e4dfc91beb4f;p=people%2Froyger%2Fxen-test-framework.git Unmap the page at 0 to catch errors with NULL pointers Signed-off-by: Andrew Cooper --- diff --git a/arch/x86/hvm_pagetables.S b/arch/x86/hvm_pagetables.S index b929e24..0e078e0 100644 --- a/arch/x86/hvm_pagetables.S +++ b/arch/x86/hvm_pagetables.S @@ -5,9 +5,20 @@ .data .p2align PAGE_SHIFT -/* Mapping of first 4G of memory in 2M superpages. Uses 4x 4k pages. */ +/* Mapping of first 2M of memory in 4k pages. Uses 1x 4k page. */ +GLOBAL(l1_identmap) + .long 0, 0 /* Unmap page at 0 to catch errors with NULL pointers. */ + .rept L1_PT_ENTRIES - 1 + .long (((. - l1_identmap) / 8) << (PAGE_ORDER_4K + PAGE_SHIFT)) + \ + _PAGE_USER + _PAGE_RW + _PAGE_PRESENT + .long 0 + .endr + +/* Mappings up to 4G in 2M superpages. Uses 4x 4k pages. */ GLOBAL(l2_identmap) - .rept (4 * L2_PT_ENTRIES) + .long l1_identmap + _PAGE_USER + _PAGE_RW + _PAGE_PRESENT + .long 0 + .rept (4 * L2_PT_ENTRIES) - 1 .long (((. - l2_identmap) / 8) << (PAGE_ORDER_2M + PAGE_SHIFT)) + \ _PAGE_PSE + _PAGE_USER + _PAGE_RW + _PAGE_PRESENT .long 0 diff --git a/arch/x86/pv/traps.c b/arch/x86/pv/traps.c index 51c2ec9..7f9a190 100644 --- a/arch/x86/pv/traps.c +++ b/arch/x86/pv/traps.c @@ -73,6 +73,11 @@ void arch_init_traps(void) write_fs(__USER_DS); write_gs(__USER_DS); + /* Unmap page at 0 to catch errors with NULL pointers. */ + rc = hypercall_update_va_mapping(NULL, 0, 2); + if ( rc ) + panic("Failed to unmap page at NULL: %d\n", rc); + #ifdef __x86_64__ /* * Set the user pagetables (only applicable to 64bit PV). diff --git a/tests/selftest/main.c b/tests/selftest/main.c index 45eb3bc..adae1de 100644 --- a/tests/selftest/main.c +++ b/tests/selftest/main.c @@ -169,6 +169,27 @@ static void test_exec_user(void) } } +static void test_NULL_unmapped(void) +{ + extern unsigned long label_test_NULL_unmapped[]; + unsigned long tmp; + + printk("Test: NULL unmapped\n"); + + xtf_exlog_start(); + + asm volatile ("label_test_NULL_unmapped: mov 0, %0; 2:" + _ASM_EXTABLE(label_test_NULL_unmapped, 2b) + : "=q" (tmp) :: "memory"); + + if ( check_nr_entries(1) ) + check_exlog_entry(0, __KERN_CS, + (unsigned long)&label_test_NULL_unmapped, + X86_EXC_PF, 0); + + xtf_exlog_stop(); +} + void test_main(void) { printk("XTF Selftests\n"); @@ -177,6 +198,7 @@ void test_main(void) test_extable(); test_exlog(); test_exec_user(); + test_NULL_unmapped(); xtf_success(); }