Test that userspace manipulation of the tsc offset (via KVM_{GET,SET}_SYSTEM_COUNTER_STATE) results in the expected guest view of the TSC. Reviewed-by: David Matlack <dmatlack@xxxxxxxxxx> Signed-off-by: Oliver Upton <oupton@xxxxxxxxxx> --- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/system_counter_state_test.c | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index c2e5a7d877b1..28207474c79c 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -76,6 +76,7 @@ TEST_GEN_PROGS_x86_64 += kvm_page_table_test TEST_GEN_PROGS_x86_64 += memslot_modification_stress_test TEST_GEN_PROGS_x86_64 += set_memory_region_test TEST_GEN_PROGS_x86_64 += steal_time +TEST_GEN_PROGS_x86_64 += system_counter_state_test TEST_GEN_PROGS_aarch64 += aarch64/counter_emulation_benchmark TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list diff --git a/tools/testing/selftests/kvm/system_counter_state_test.c b/tools/testing/selftests/kvm/system_counter_state_test.c index f537eb7b928c..9dcbc95bba3f 100644 --- a/tools/testing/selftests/kvm/system_counter_state_test.c +++ b/tools/testing/selftests/kvm/system_counter_state_test.c @@ -138,6 +138,38 @@ static uint64_t host_read_guest_counter(struct system_counter_state_test *test) return r; } +#elif __x86_64__ + +/* no additional information required beyond the counter state. */ +#define system_counter_state_test kvm_system_counter_state + +static struct system_counter_state_test test_cases[] = { + { .tsc_offset = 0 }, + { .tsc_offset = 1000000 }, + { .tsc_offset = -1 }, +}; + +static void pr_test(struct system_counter_state_test *test) +{ + printf("tsc_offset: %lld\n", test->tsc_offset); +} + +static struct kvm_system_counter_state * +get_system_counter_state(struct system_counter_state_test *test) +{ + return test; +} + +static uint64_t guest_read_counter(struct system_counter_state_test *test) +{ + return rdtsc(); +} + +static uint64_t host_read_guest_counter(struct system_counter_state_test *test) +{ + return rdtsc() + test->tsc_offset; +} + #else #error test not implemented for architecture being built! #endif -- 2.32.0.rc1.229.g3e70b5a671-goog