Skip to content

Commit 8b47442

Browse files
committed
KVM: x86: use kvm_complete_insn_gp in emulating RDMSR/WRMSR
Simplify the four functions that handle {kernel,user} {rd,wr}msr, there is still some repetition between the two instances of rdmsr but the whole business of calling kvm_inject_gp and kvm_skip_emulated_instruction can be unified nicely. Because complete_emulated_wrmsr now becomes essentially a call to kvm_complete_insn_gp, remove complete_emulated_msr. Reviewed-by: Tom Lendacky <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 9caec4b commit 8b47442

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

arch/x86/kvm/x86.c

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,27 +1634,20 @@ int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data)
16341634
}
16351635
EXPORT_SYMBOL_GPL(kvm_set_msr);
16361636

1637-
static int complete_emulated_msr(struct kvm_vcpu *vcpu, bool is_read)
1637+
static int complete_emulated_rdmsr(struct kvm_vcpu *vcpu)
16381638
{
1639-
if (vcpu->run->msr.error) {
1640-
kvm_inject_gp(vcpu, 0);
1641-
return 1;
1642-
} else if (is_read) {
1639+
int err = vcpu->run->msr.error;
1640+
if (!err) {
16431641
kvm_rax_write(vcpu, (u32)vcpu->run->msr.data);
16441642
kvm_rdx_write(vcpu, vcpu->run->msr.data >> 32);
16451643
}
16461644

1647-
return kvm_skip_emulated_instruction(vcpu);
1648-
}
1649-
1650-
static int complete_emulated_rdmsr(struct kvm_vcpu *vcpu)
1651-
{
1652-
return complete_emulated_msr(vcpu, true);
1645+
return kvm_complete_insn_gp(vcpu, err);
16531646
}
16541647

16551648
static int complete_emulated_wrmsr(struct kvm_vcpu *vcpu)
16561649
{
1657-
return complete_emulated_msr(vcpu, false);
1650+
return kvm_complete_insn_gp(vcpu, vcpu->run->msr.error);
16581651
}
16591652

16601653
static u64 kvm_msr_reason(int r)
@@ -1717,18 +1710,16 @@ int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu)
17171710
return 0;
17181711
}
17191712

1720-
/* MSR read failed? Inject a #GP */
1721-
if (r) {
1713+
if (!r) {
1714+
trace_kvm_msr_read(ecx, data);
1715+
1716+
kvm_rax_write(vcpu, data & -1u);
1717+
kvm_rdx_write(vcpu, (data >> 32) & -1u);
1718+
} else {
17221719
trace_kvm_msr_read_ex(ecx);
1723-
kvm_inject_gp(vcpu, 0);
1724-
return 1;
17251720
}
17261721

1727-
trace_kvm_msr_read(ecx, data);
1728-
1729-
kvm_rax_write(vcpu, data & -1u);
1730-
kvm_rdx_write(vcpu, (data >> 32) & -1u);
1731-
return kvm_skip_emulated_instruction(vcpu);
1722+
return kvm_complete_insn_gp(vcpu, r);
17321723
}
17331724
EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr);
17341725

@@ -1749,15 +1740,12 @@ int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu)
17491740
if (r < 0)
17501741
return r;
17511742

1752-
/* MSR write failed? Inject a #GP */
1753-
if (r > 0) {
1743+
if (!r)
1744+
trace_kvm_msr_write(ecx, data);
1745+
else
17541746
trace_kvm_msr_write_ex(ecx, data);
1755-
kvm_inject_gp(vcpu, 0);
1756-
return 1;
1757-
}
17581747

1759-
trace_kvm_msr_write(ecx, data);
1760-
return kvm_skip_emulated_instruction(vcpu);
1748+
return kvm_complete_insn_gp(vcpu, r);
17611749
}
17621750
EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
17631751

0 commit comments

Comments
 (0)