x86: Add split/bus lock smoke test
Sean Christopherson
reviewercover x86: Add split/bus lock smoke test
Add a testcase to verify the guest does NOT get hit with an #AC or #DB when generating a split-lock access. Bus Lock Detect, a.k.a. BusLockTrap, enabling on AMD exposed a bug where KVM incorrectly runs the guest with the host's DEBUGCTL MSR.
Sean Christopherson (2):
x86: Include libcflat.h in atomic.h for u64 typedef x86/debug: Add a split-lock #AC / bus-lock #DB testcase
lib/x86/atomic.h | 2 ++ x86/debug.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+)
base-commit: f77fb696cfd0e4a5562cdca189be557946bf522f
Signature · 1 line
2.48.1.601.g30ceb7b040-goog
Sean Christopherson
reviewerPATCH 1/2 x86: Include libcflat.h in atomic.h for u64 typedef
Include libcflat.h in x86's atomic.h to pick up the u64 typedef, which is used to define atomic64_t. The missing include results in build errors if a test includes atomic.h without (or before) libcflat.h.
lib/x86/atomic.h:162:1: error: unknown type name ‘u64’ 162 | u64 atomic64_cmpxchg(atomic64_t *v, u64 old, u64 new);
--- lib/x86/atomic.h | 2 ++ 1 file changed, 2 insertions(+)
lib/x86/atomic.h +2 -0
@@ -3,6 +3,8 @@#include "asm-generic/atomic.h" +#include "libcflat.h" +typedef struct {volatile int counter; }atomic_t ;
Signature · 1 line
2.48.1.601.g30ceb7b040-goog
Sean Christopherson
reviewerPATCH 2/2 x86/debug: Add a split-lock #AC / bus-lock #DB testcase
In reply to Sean ChristophersonAdd a testcase to the debug test to verify that a split-lock in the guest does NOT result in an #AC or #DB (and that the test isn't killed). While KVM may run the guest with split-lock #AC enabled, KVM should never inject an #AC into the guest. And Bus Lock Detect/Trap should flat out never be enabled while KVM is running the guest.
--- x86/debug.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)
x86/debug.c +45 -0
@@ -10,6 +10,7 @@*/ #include <asm/debugreg.h> +#include "atomic.h" #include "libcflat.h" #include "processor.h" #include "desc.h"
@@ -81,6 +82,12 @@ static void handle_ud(struct ex_regs *regs) got_ud =1 ; } +static bool got_ac; +static void handle_ac (struct ex_regs *regs) +{ + got_ac = true; +} +typedef unsigned long (*db_test_fn )(void );typedef void (*db_report_fn )(unsigned long ,const char *);
@@ -409,6 +416,41 @@ static noinline unsigned long singlestep_with_sti_hlt(void)return start_rip; } +static noinline uint64_t bus_lock(uint64_t magic) +{ + uint8_t buffer[128] __attribute__((aligned(64))) = { }; +atomic64_t *val = (atomic64_t *)&buffer[60 ]; + +atomic64_cmpxchg (val,0 , magic); +return READ_ONCE (*(uint64_t *)val); +} + +static void bus_lock_test (void ) +{ +const uint64_t magic =0xdeadbeefdeadbeefull ; +bool bus_lock_db = false; +uint64_t val; + +/* +* Generate a bus lock (via a locked access that splits cache lines) +* in CPL0 and again in CPL3 (Bus Lock Detect only affects CPL3), and +* verify that no #AC or #DB is generated (the relevant features are +* not enabled). +*/ + val =bus_lock (magic); +report (!got_ac && !n && val == magic, +"CPL0 Split Lock #AC = %u (#DB = %u), val = %lx (wanted %lx)" , + got_ac, n, val, magic); + + val =run_in_user ((usermode_func )bus_lock,DB_VECTOR , magic,0 ,0 ,0 , &bus_lock_db); +report (!bus_lock_db && val == magic, +"CPL3 Bus Lock #DB = %u, val = %lx (wanted %lx)" , + bus_lock_db, val, magic); + + n =0 ; + got_ac = false; +} +int main (int ac,char **av) {unsigned long cr4;
@@ -416,6 +458,9 @@ int main(int ac, char **av)handle_exception (DB_VECTOR , handle_db);handle_exception (BP_VECTOR , handle_bp);handle_exception (UD_VECTOR , handle_ud); +handle_exception (AC_VECTOR , handle_ac); + +bus_lock_test ();/* * DR4 is an alias for DR6 (and DR5 aliases DR7) if CR4.DE is NOT set,
Signature · 1 line
2.48.1.601.g30ceb7b040-goog
Quoted 10 lines · Sean Christopherson
Add a testcase to verify the guest does NOT get hit with an #AC or #DB when generating a split-lock access. Bus Lock Detect, a.k.a. BusLockTrap, enabling on AMD exposed a bug where KVM incorrectly runs the guest with the host's DEBUGCTL MSR.
Sean Christopherson (2):
x86: Includelibcflat.hinatomic.hfor u64 typedef x86/debug: Add a split-lock #AC / bus-lock #DB testcase[...]
Applied to kvm-x86 next (and now pulled by Paolo), thanks!
[1/2] x86: Include libcflat.h in atomic.h for u64 typedef
https://github.com/kvm-x86/kvm-unit-tests/commit/36fb9e84a465 [2/2] x86/debug: Add a split-lock #AC / bus-lock #DB testcase https://github.com/kvm-x86/kvm-unit-tests/commit/8d9218bb6b7c