[kvm-unit-tests PATCH] x86/msr.c generalize to any input msr

yqwfh
2021-08-10T14:32:10Z

If an MSR description is provided as input by the user,
run the test against that MSR. This allows the user to
run tests on custom MSR's.

Otherwise run all default tests.

Signed-off-by: Daniele Ahmed <ahmeddan@amazon.com>
---
x86/msr.c | 48 ++++++++++++++++++++++++++++++++++--------------
1 file changed, 34 insertions(+), 14 deletions(-)

diff --git a/x86/msr.c b/x86/msr.c
index 7a551c4..554014e 100644
--- a/x86/msr.c
+++ b/x86/msr.c
@@ -3,6 +3,7 @@
#include "libcflat.h"
#include "processor.h"
#include "msr.h"
+#include <stdlib.h>

struct msr_info {
int index;
@@ -77,25 +78,44 @@ static void test_rdmsr_fault(struct msr_info *msr)
"Expected #GP on RDSMR(%s), got vector %d", msr->name, vector);
}

+static void test_msr(struct msr_info *msr, bool is_64bit_host)
+{
+ if (is_64bit_host || !msr->is_64bit_only) {
+ test_msr_rw(msr, msr->value);
+
+ /*
+ * The 64-bit only MSRs that take an address always perform
+ * canonical checks on both Intel and AMD.
+ */
+ if (msr->is_64bit_only &&
+ msr->value == addr_64)
+ test_wrmsr_fault(msr, NONCANONICAL);
+ } else {
+ test_wrmsr_fault(msr, msr->value);
+ test_rdmsr_fault(msr);
+ }
+}
+
int main(int ac, char **av)
{
bool is_64bit_host = this_cpu_has(X86_FEATURE_LM);
int i;

- for (i = 0 ; i < ARRAY_SIZE(msr_info); i++) {
- if (is_64bit_host || !msr_info[i].is_64bit_only) {
- test_msr_rw(&msr_info[i], msr_info[i].value);
-
- /*
- * The 64-bit only MSRs that take an address always perform
- * canonical checks on both Intel and AMD.
- */
- if (msr_info[i].is_64bit_only &&
- msr_info[i].value == addr_64)
- test_wrmsr_fault(&msr_info[i], NONCANONICAL);
- } else {
- test_wrmsr_fault(&msr_info[i], msr_info[i].value);
- test_rdmsr_fault(&msr_info[i]);
+ if (ac == 4) {
+ char msr_name[16];
+ int index = strtoul(av[1], NULL, 0x10);
+ snprintf(msr_name, sizeof(msr_name), "MSR:0x%x", index);
+
+ struct msr_info msr = {
+ .index = index,
+ .name = msr_name,
+ .is_64bit_only = !strcmp(av[3], "0"),
+ .value = strtoul(av[2], NULL, 0x10)
+ };
+ test_msr(&msr, is_64bit_host);
+ } else {
+ for (i = 0 ; i < ARRAY_SIZE(msr_info); i++) {
+ test_msr(&msr_info[i], is_64bit_host);
}
}

--
2.20.1