From: Sean Christopherson <seanjc@google.com>
To: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Marc Orr <marcorr@google.com>, Ben Gardon <bgardon@google.com>,
Venkatesh Srinivas <venkateshs@chromium.org>
Subject: [PATCH 4/6] KVM: x86: Split out logic to generate "readable" APIC regs mask to helper
Date: Sat, 7 Jan 2023 01:10:23 +0000 [thread overview]
Message-ID: <20230107011025.565472-5-seanjc@google.com> (raw)
In-Reply-To: <20230107011025.565472-1-seanjc@google.com>
Move the generation of the readable APIC regs bitmask to a standalone
helper so that VMX can use the mask for its MSR interception bitmaps.
No functional change intended.
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
arch/x86/kvm/lapic.c | 34 +++++++++++++++++++++-------------
arch/x86/kvm/lapic.h | 2 ++
2 files changed, 23 insertions(+), 13 deletions(-)
diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index c49b13418638..19697fe9b2c7 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -1529,12 +1529,9 @@ static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
#define APIC_REGS_MASK(first, count) \
(APIC_REG_MASK(first) * ((1ull << (count)) - 1))
-static int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
- void *data)
+u64 kvm_lapic_readable_reg_mask(struct kvm_lapic *apic)
{
- unsigned char alignment = offset & 0xf;
- u32 result;
- /* this bitmask has a bit cleared for each reserved register */
+ /* Leave bits '0' for reserved and write-only registers. */
u64 valid_reg_mask =
APIC_REG_MASK(APIC_ID) |
APIC_REG_MASK(APIC_LVR) |
@@ -1560,22 +1557,33 @@ static int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
if (kvm_lapic_lvt_supported(apic, LVT_CMCI))
valid_reg_mask |= APIC_REG_MASK(APIC_LVTCMCI);
- /*
- * ARBPRI, DFR, and ICR2 are not valid in x2APIC mode. WARN if KVM
- * reads ICR in x2APIC mode as it's an 8-byte register in x2APIC and
- * needs to be manually handled by the caller.
- */
+ /* ARBPRI, DFR, and ICR2 are not valid in x2APIC mode. */
if (!apic_x2apic_mode(apic))
valid_reg_mask |= APIC_REG_MASK(APIC_ARBPRI) |
APIC_REG_MASK(APIC_DFR) |
APIC_REG_MASK(APIC_ICR2);
- else
- WARN_ON_ONCE(offset == APIC_ICR);
+
+ return valid_reg_mask;
+}
+EXPORT_SYMBOL_GPL(kvm_lapic_readable_reg_mask);
+
+static int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
+ void *data)
+{
+ unsigned char alignment = offset & 0xf;
+ u32 result;
+
+ /*
+ * WARN if KVM reads ICR in x2APIC mode, as it's an 8-byte register in
+ * x2APIC and needs to be manually handled by the caller.
+ */
+ WARN_ON_ONCE(apic_x2apic_mode(apic) && offset == APIC_ICR);
if (alignment + len > 4)
return 1;
- if (offset > 0x3f0 || !(valid_reg_mask & APIC_REG_MASK(offset)))
+ if (offset > 0x3f0 ||
+ !(kvm_lapic_readable_reg_mask(apic) & APIC_REG_MASK(offset)))
return 1;
result = __apic_read(apic, offset & ~0xf);
diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h
index df316ede7546..0a0ea4b5dd8c 100644
--- a/arch/x86/kvm/lapic.h
+++ b/arch/x86/kvm/lapic.h
@@ -146,6 +146,8 @@ int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
int kvm_lapic_set_pv_eoi(struct kvm_vcpu *vcpu, u64 data, unsigned long len);
void kvm_lapic_exit(void);
+u64 kvm_lapic_readable_reg_mask(struct kvm_lapic *apic);
+
#define VEC_POS(v) ((v) & (32 - 1))
#define REG_POS(v) (((v) >> 5) << 4)
--
2.39.0.314.g84b9a713c41-goog
next prev parent reply other threads:[~2023-01-07 1:11 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-07 1:10 [PATCH 0/6] KVM: x86: x2APIC reserved bits/regs fixes Sean Christopherson
2023-01-07 1:10 ` [PATCH 1/6] KVM: x86: Inject #GP if WRMSR sets reserved bits in APIC Self-IPI Sean Christopherson
2023-01-08 16:40 ` Maxim Levitsky
2023-01-07 1:10 ` [PATCH 2/6] KVM: x86: Inject #GP on x2APIC WRMSR that sets reserved bits 63:32 Sean Christopherson
2023-01-08 16:41 ` Maxim Levitsky
2023-01-07 1:10 ` [PATCH 3/6] KVM: x86: Mark x2APIC DFR reg as non-existent for x2APIC Sean Christopherson
2023-01-08 16:43 ` Maxim Levitsky
2023-01-07 1:10 ` Sean Christopherson [this message]
2023-01-08 17:38 ` [PATCH 4/6] KVM: x86: Split out logic to generate "readable" APIC regs mask to helper Maxim Levitsky
2023-01-07 1:10 ` [PATCH 5/6] KVM: VMX: Always intercept accesses to unsupported "extended" x2APIC regs Sean Christopherson
2023-01-08 18:07 ` Maxim Levitsky
2023-01-09 16:32 ` Sean Christopherson
2023-01-09 17:25 ` Jim Mattson
2023-01-07 1:10 ` [PATCH 6/6] KVM: VMX: Intercept reads to invalid and write-only x2APIC registers Sean Christopherson
2023-01-08 18:09 ` Maxim Levitsky
2023-01-13 18:06 ` [PATCH 0/6] KVM: x86: x2APIC reserved bits/regs fixes Paolo Bonzini
2023-01-13 18:41 ` Sean Christopherson
2023-01-20 0:19 ` Sean Christopherson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230107011025.565472-5-seanjc@google.com \
--to=seanjc@google.com \
--cc=bgardon@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=marcorr@google.com \
--cc=pbonzini@redhat.com \
--cc=venkateshs@chromium.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox