x86: Look up the PTEs rather than assuming them
Subsystems (2)
PATCH v2 1/1 x86: Look up the PTEs rather than assuming them
Rather than assuming which PTEs the SMEP test runs on, look them up to ensure they are correct. If this test were to run on a different page table (ie: run in an L2 test) the wrong PTEs would be set. Switch to looking up the PTEs to avoid this from happening.
--- lib/libcflat.h | 1 + lib/x86/vm.c | 19 +++++++++++++++++++ lib/x86/vm.h | 3 +++ x86/access.c | 23 +++++++++++++++-------- x86/cstart64.S | 1 - x86/flat.lds | 1 + 6 files changed, 39 insertions(+), 9 deletions(-)
lib/libcflat.h +1 -0
@@ -35,6 +35,7 @@#define __ALIGN_MASK (x, mask) (((x) + (mask)) & ~(mask))#define __ALIGN (x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1)#define ALIGN (x, a) __ALIGN((x), (a)) +#define ALIGN_DOWN (x, a) __ALIGN((x) - ((a) - 1), (a))#define IS_ALIGNED (x, a) (((x) & ((typeof(x))(a) - 1)) == 0)#define MIN (a, b) ((a) < (b) ? (a) : (b))
lib/x86/vm.c +19 -0
@@ -281,3 +281,22 @@ void force_4k_page(void *addr)if (pte &PT_PAGE_SIZE_MASK )split_large_page (ptep,2 ); } + +/* +* Call the callback, function, on each page from va_start to va_end. +*/ +void walk_pte (void *va_start,void *va_end, +void (*function)(pteval_t *pte,void *va)){ +struct pte_search search; +uintptr_t curr_addr; +u64 page_size; + +for (curr_addr = (uintptr_t )va_start; curr_addr < (uintptr_t )va_end; + curr_addr =ALIGN_DOWN (curr_addr + page_size, page_size)) { + search =find_pte_level (current_page_table (), (void *)curr_addr,1 ); +assert (found_leaf_pte (search)); + page_size =1ul <<PGDIR_BITS (search.level); + +function (search.pte, (void *)curr_addr); + } +}
lib/x86/vm.h +3 -0
@@ -52,4 +52,7 @@ struct vm_vcpu_info { u64 cr0; }; +void walk_pte (void *va_start,void *va_end, +void (*function)(pteval_t *pte,void *va)); +#endif
x86/access.c +15 -8
@@ -201,10 +201,21 @@ static void set_cr0_wp(int wp) } } +static void clear_user_mask (pteval_t *pte,void *va) { + *pte &= ~PT_USER_MASK ; +} + +static void set_user_mask (pteval_t *pte,void *va) { + *pte |=PT_USER_MASK ; + +/* Flush to avoid spurious #PF */ +invlpg (va); +} +static unsigned set_cr4_smep (int smep) { +extern char stext, etext;unsigned long cr4 = shadow_cr4; -extern u64 ptl2[];unsigned r; cr4 &= ~CR4_SMEP_MASK ;
@@ -214,14 +225,10 @@ static unsigned set_cr4_smep(int smep)return 0 ;if (smep) - ptl2[2 ] &= ~PT_USER_MASK ; +walk_pte (&stext, &etext, clear_user_mask); r =write_cr4_checking (cr4); -if (r || !smep) { - ptl2[2 ] |=PT_USER_MASK ; - -/* Flush to avoid spurious #PF */ -invlpg ((void *)(2 <<21 )); - } +if (r || !smep) +walk_pte (&stext, &etext, set_user_mask);if (!r) shadow_cr4 = cr4;return r;
x86/cstart64.S +0 -1
@@ -26,7 +26,6 @@ ring0stacktop: .data .align 4096 -.globl ptl2 ptl2: i = 0 .rept 512 * 4
x86/flat.lds +1 -0
@@ -3,6 +3,7 @@ SECTIONS . = 4M + SIZEOF_HEADERS; stext = .; .text : { *(.init) *(.text) *(.text.*) } + etext = .; . = ALIGN(4K); .data : { *(.data)
Signature · 1 line
2.34.0.rc0.344.g81b53c2807-goog
Sean Christopherson
reviewerQuoted 32 lines · Aaron Lewis
Rather than assuming which PTEs the SMEP test runs on, look them up to ensure they are correct. If this test were to run on a different page table (ie: run in an L2 test) the wrong PTEs would be set. Switch to looking up the PTEs to avoid this from happening.
Signed-off-by Aaron Lewis <[email protected]>--- lib/libcflat.h | 1 + lib/x86/vm.c | 19 +++++++++++++++++++ lib/x86/vm.h | 3 +++ x86/access.c | 23 +++++++++++++++-------- x86/cstart64.S | 1 - x86/flat.lds | 1 + 6 files changed, 39 insertions(+), 9 deletions(-)lib/libcflat.h +1
-0@@ -35,6 +35,7 @@#define __ALIGN_MASK (x, mask) (((x) + (mask)) & ~(mask))#define __ALIGN (x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1)#define ALIGN (x, a) __ALIGN((x), (a)) +#define ALIGN_DOWN (x, a) __ALIGN((x) - ((a) - 1), (a))#define IS_ALIGNED (x, a) (((x) & ((typeof(x))(a) - 1)) == 0)#define MIN (a, b) ((a) < (b) ? (a) : (b))lib/x86/vm.c +0
-0
lib/x86/vm.c void force_4k_page(void *addr) · line 288
Quoted 8 lines
lib/x86/vm.c +5
-0@@ -281,3 +281,22 @@ void force_4k_page(void *addr)if (pte &PT_PAGE_SIZE_MASK )split_large_page (ptep,2 ); } + +/* +* Call the callback, function, on each page from va_start to va_end. +*/ +void walk_pte(void *va_start, void *va_end,
To align with other helpers in vm.c, "void *virt, size_t len" would be more approriate. That would also avoid having to document whether or not va_end is inclusive or exclusive.
lib/x86/vm.c void force_4k_page(void *addr) · line 289
Quoted 1 line
lib/x86/vm.c +1
-0+ void (*function)(pteval_t *pte, void *va)){
Curly brace goes on a separate line for functions.
lib/x86/vm.c void force_4k_page(void *addr) · line 291
Quoted 2 lines
lib/x86/vm.c +2
-0+struct pte_search search; +uintptr_t curr_addr;
Maybe just curr? To match other code and maybe squeeze the for-loop on a single line?
for (curr = virt; curr < end; curr = ALIGN_DOWN(curr + page_size, page_size)) {
}If you do split the loop, it's usually easier to read if all three parts are on separate lines, e.g.
for (curr = virt;
curr < end;
curr = curr = ALIGN_DOWN(curr + page_size, page_size)) {
}lib/x86/vm.c void force_4k_page(void *addr) · line 292
Quoted 1 line
lib/x86/vm.c +1
-0+u64 page_size;
Probably should be size_t.
lib/x86/vm.c void force_4k_page(void *addr) · line 296
Quoted 5 lines
lib/x86/vm.c +3
-0+for (curr_addr = (uintptr_t )va_start; curr_addr < (uintptr_t )va_end; + curr_addr =ALIGN_DOWN (curr_addr + page_size, page_size)) { + search =find_pte_level (current_page_table (), (void *)curr_addr,1 );
Probably worth caching current_page_table(). On x86 with shadow paging, that'll trigger an exit on every iteration to read CR3.
Quoted 11 lines
lib/x86/vm.c +6
-0+assert (found_leaf_pte (search)); + page_size =1ul <<PGDIR_BITS (search.level); + +function (search.pte, (void *)curr_addr); + } +}lib/x86/vm.h +0
-0
lib/x86/vm.h struct vm_vcpu_info { · line 56
Quoted 5 lines
lib/x86/vm.h +2
-0@@ -52,4 +52,7 @@ struct vm_vcpu_info {u64 cr0; }; +void walk_pte (void *va_start,void *va_end, +void (*function)(pteval_t *pte,void *va));
A typedef for the function pointer would be helpful. Maybe pte_callback_t? And pass "struct pte_search" instead of pteval_t so that future users can get at the level?
x86/access.c static void set_cr0_wp(int wp) · line 204
Quoted 10 lines
lib/x86/vm.h +0
-0#endif x86/access.c +1
-0@@ -201,10 +201,21 @@ static void set_cr0_wp(int wp) } } +static void clear_user_mask (pteval_t *pte,void *va) {
Curly brace thing again.
x86/access.c static void set_cr0_wp(int wp) · line 208
Quoted 4 lines
x86/access.c +4
-0+ *pte &= ~PT_USER_MASK ; +} + +static void set_user_mask (pteval_t *pte,void *va) {
And here.
Quoted 14 lines
x86/access.c +7
-1+ *pte |=PT_USER_MASK ; + +/* Flush to avoid spurious #PF */ +invlpg (va); +} +static unsigned set_cr4_smep (int smep) { +extern char stext, etext;unsigned long cr4 = shadow_cr4; -extern u64 ptl2[];unsigned r; cr4 &= ~CR4_SMEP_MASK ;