]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: SVM: Clean up macros related to architectural MSRPM definitions
authorSean Christopherson <seanjc@google.com>
Tue, 10 Jun 2025 22:57:14 +0000 (15:57 -0700)
committerSean Christopherson <seanjc@google.com>
Fri, 20 Jun 2025 20:07:11 +0000 (13:07 -0700)
Move SVM's MSR Permissions Map macros to svm.h in anticipation of adding
helpers that are available to SVM code, and opportunistically replace a
variety of open-coded literals with (hopefully) informative macros.

Opportunistically open code ARRAY_SIZE(msrpm_ranges) instead of wrapping
it as NUM_MSR_MAPS, which is an ambiguous name even if it were qualified
with "SVM_MSRPM".

Deliberately leave the ranges as open coded literals, as using macros to
define the ranges actually introduces more potential failure points, since
both the definitions and the usage have to be careful to use the correct
index.  The lack of clear intent behind the ranges will be addressed in
future patches.

No functional change intended.

Link: https://lore.kernel.org/r/20250610225737.156318-10-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/svm/svm.c
arch/x86/kvm/svm/svm.h

index 4d90dd3a3b3ae38c9f00f2ff234de4d973fce66a..eb10895e3eb4e4c918ff7d695e5dd11b4ac59194 100644 (file)
@@ -269,22 +269,18 @@ static int tsc_aux_uret_slot __read_mostly = -1;
 
 static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
 
-#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
-#define MSRS_RANGE_SIZE 2048
-#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
-
 u32 svm_msrpm_offset(u32 msr)
 {
        u32 offset;
        int i;
 
-       for (i = 0; i < NUM_MSR_MAPS; i++) {
+       for (i = 0; i < ARRAY_SIZE(msrpm_ranges); i++) {
                if (msr < msrpm_ranges[i] ||
-                   msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
+                   msr >= msrpm_ranges[i] + SVM_MSRS_PER_RANGE)
                        continue;
 
-               offset  = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
-               offset += (i * MSRS_RANGE_SIZE);       /* add range offset */
+               offset  = (msr - msrpm_ranges[i]) / SVM_MSRS_PER_BYTE;
+               offset += (i * SVM_MSRPM_BYTES_PER_RANGE);  /* add range offset */
 
                /* Now we have the u8 offset - but need the u32 offset */
                return offset / 4;
index f1e466a10219b18880da56dedd9c9e1347a0ed74..086a8c8aae86232e8fc5a36ab5ae1b32631d31d1 100644 (file)
@@ -613,11 +613,22 @@ static inline void svm_vmgexit_no_action(struct vcpu_svm *svm, u64 data)
        svm_vmgexit_set_return_code(svm, GHCB_HV_RESP_NO_ACTION, data);
 }
 
-/* svm.c */
+/*
+ * The MSRPM is 8KiB in size, divided into four 2KiB ranges (the fourth range
+ * is reserved).  Each MSR within a range is covered by two bits, one each for
+ * read (bit 0) and write (bit 1), where a bit value of '1' means intercepted.
+ */
+#define SVM_MSRPM_BYTES_PER_RANGE 2048
+#define SVM_BITS_PER_MSR 2
+#define SVM_MSRS_PER_BYTE (BITS_PER_BYTE / SVM_BITS_PER_MSR)
+#define SVM_MSRS_PER_RANGE (SVM_MSRPM_BYTES_PER_RANGE * SVM_MSRS_PER_BYTE)
+static_assert(SVM_MSRS_PER_RANGE == 8192);
+
 #define MSR_INVALID                            0xffffffffU
 
 #define DEBUGCTL_RESERVED_BITS (~DEBUGCTLMSR_LBR)
 
+/* svm.c */
 extern bool dump_invalid_vmcb;
 
 u32 svm_msrpm_offset(u32 msr);