Skip to content

Commit c2aa58b

Browse files
yang-weijiangsean-jc
authored andcommitted
KVM: x86: Add kvm_msr_{read,write}() helpers
Wrap __kvm_{get,set}_msr() into two new helpers for KVM usage and use the helpers to replace existing usage of the raw functions. kvm_msr_{read,write}() are KVM-internal helpers, i.e. used when KVM needs to get/set a MSR value for emulating CPU behavior, i.e., host_initiated == %true in the helpers. Suggested-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Yang Weijiang <weijiang.yang@intel.com> Reviewed-by: Maxim Levitsky <mlevitsk@redhat.com> Tested-by: Mathias Krause <minipli@grsecurity.net> Tested-by: John Allen <john.allen@amd.com> Signed-off-by: Chao Gao <chao.gao@intel.com> Tested-by: Rick Edgecombe <rick.p.edgecombe@intel.com> Link: https://lore.kernel.org/r/20250812025606.74625-4-chao.gao@intel.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent db07f3d commit c2aa58b

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2156,9 +2156,10 @@ void kvm_enable_efer_bits(u64);
21562156
bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer);
21572157
int kvm_emulate_msr_read(struct kvm_vcpu *vcpu, u32 index, u64 *data);
21582158
int kvm_emulate_msr_write(struct kvm_vcpu *vcpu, u32 index, u64 data);
2159-
int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data, bool host_initiated);
21602159
int __kvm_emulate_msr_read(struct kvm_vcpu *vcpu, u32 index, u64 *data);
21612160
int __kvm_emulate_msr_write(struct kvm_vcpu *vcpu, u32 index, u64 data);
2161+
int kvm_msr_read(struct kvm_vcpu *vcpu, u32 index, u64 *data);
2162+
int kvm_msr_write(struct kvm_vcpu *vcpu, u32 index, u64 data);
21622163
int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu);
21632164
int kvm_emulate_rdmsr_imm(struct kvm_vcpu *vcpu, u32 msr, int reg);
21642165
int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu);

arch/x86/kvm/cpuid.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2006,7 +2006,7 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
20062006
if (function == 7 && index == 0) {
20072007
u64 data;
20082008
if ((*ebx & (feature_bit(RTM) | feature_bit(HLE))) &&
2009-
!__kvm_get_msr(vcpu, MSR_IA32_TSX_CTRL, &data, true) &&
2009+
!kvm_msr_read(vcpu, MSR_IA32_TSX_CTRL, &data) &&
20102010
(data & TSX_CTRL_CPUID_CLEAR))
20112011
*ebx &= ~(feature_bit(RTM) | feature_bit(HLE));
20122012
} else if (function == 0x80000007) {

arch/x86/kvm/x86.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,8 +1899,8 @@ static int kvm_set_msr_ignored_check(struct kvm_vcpu *vcpu,
18991899
* Returns 0 on success, non-0 otherwise.
19001900
* Assumes vcpu_load() was already called.
19011901
*/
1902-
int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,
1903-
bool host_initiated)
1902+
static int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,
1903+
bool host_initiated)
19041904
{
19051905
struct msr_data msr;
19061906
int ret;
@@ -1926,6 +1926,16 @@ int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,
19261926
return ret;
19271927
}
19281928

1929+
int kvm_msr_write(struct kvm_vcpu *vcpu, u32 index, u64 data)
1930+
{
1931+
return __kvm_set_msr(vcpu, index, data, true);
1932+
}
1933+
1934+
int kvm_msr_read(struct kvm_vcpu *vcpu, u32 index, u64 *data)
1935+
{
1936+
return __kvm_get_msr(vcpu, index, data, true);
1937+
}
1938+
19291939
static int kvm_get_msr_ignored_check(struct kvm_vcpu *vcpu,
19301940
u32 index, u64 *data, bool host_initiated)
19311941
{
@@ -12472,7 +12482,7 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
1247212482
MSR_IA32_MISC_ENABLE_BTS_UNAVAIL;
1247312483

1247412484
__kvm_set_xcr(vcpu, 0, XFEATURE_MASK_FP);
12475-
__kvm_set_msr(vcpu, MSR_IA32_XSS, 0, true);
12485+
kvm_msr_write(vcpu, MSR_IA32_XSS, 0);
1247612486
}
1247712487

1247812488
/* All GPRs except RDX (handled below) are zeroed on RESET/INIT. */

0 commit comments

Comments
 (0)