Skip to content

Commit eea1cff

Browse files
aprzywaravikivity
authored andcommitted
KVM: x86: fix CR8 handling
The handling of CR8 writes in KVM is currently somewhat cumbersome. This patch makes it look like the other CR register handlers and fixes a possible issue in VMX, where the RIP would be incremented despite an injected #GP. Signed-off-by: Andre Przywara <[email protected]> Signed-off-by: Marcelo Tosatti <[email protected]>
1 parent a63512a commit eea1cff

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason,
661661
int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
662662
int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
663663
int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
664-
void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8);
664+
int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8);
665665
int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val);
666666
int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val);
667667
unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu);

arch/x86/kvm/svm.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2676,16 +2676,17 @@ static int cr0_write_interception(struct vcpu_svm *svm)
26762676
static int cr8_write_interception(struct vcpu_svm *svm)
26772677
{
26782678
struct kvm_run *kvm_run = svm->vcpu.run;
2679+
int r;
26792680

26802681
u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
26812682
/* instruction emulation calls kvm_set_cr8() */
2682-
emulate_instruction(&svm->vcpu, 0, 0, 0);
2683+
r = emulate_instruction(&svm->vcpu, 0, 0, 0);
26832684
if (irqchip_in_kernel(svm->vcpu.kvm)) {
26842685
clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
2685-
return 1;
2686+
return r == EMULATE_DONE;
26862687
}
26872688
if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2688-
return 1;
2689+
return r == EMULATE_DONE;
26892690
kvm_run->exit_reason = KVM_EXIT_SET_TPR;
26902691
return 0;
26912692
}

arch/x86/kvm/vmx.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3185,8 +3185,8 @@ static int handle_cr(struct kvm_vcpu *vcpu)
31853185
case 8: {
31863186
u8 cr8_prev = kvm_get_cr8(vcpu);
31873187
u8 cr8 = kvm_register_read(vcpu, reg);
3188-
kvm_set_cr8(vcpu, cr8);
3189-
skip_emulated_instruction(vcpu);
3188+
err = kvm_set_cr8(vcpu, cr8);
3189+
complete_insn_gp(vcpu, err);
31903190
if (irqchip_in_kernel(vcpu->kvm))
31913191
return 1;
31923192
if (cr8_prev <= cr8)

arch/x86/kvm/x86.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
662662
}
663663
EXPORT_SYMBOL_GPL(kvm_set_cr3);
664664

665-
int __kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
665+
int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
666666
{
667667
if (cr8 & CR8_RESERVED_BITS)
668668
return 1;
@@ -672,12 +672,6 @@ int __kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
672672
vcpu->arch.cr8 = cr8;
673673
return 0;
674674
}
675-
676-
void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
677-
{
678-
if (__kvm_set_cr8(vcpu, cr8))
679-
kvm_inject_gp(vcpu, 0);
680-
}
681675
EXPORT_SYMBOL_GPL(kvm_set_cr8);
682676

683677
unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
@@ -4104,7 +4098,7 @@ static int emulator_set_cr(int cr, unsigned long val, struct kvm_vcpu *vcpu)
41044098
res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
41054099
break;
41064100
case 8:
4107-
res = __kvm_set_cr8(vcpu, val & 0xfUL);
4101+
res = kvm_set_cr8(vcpu, val);
41084102
break;
41094103
default:
41104104
vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
@@ -5381,8 +5375,12 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
53815375
}
53825376

53835377
/* re-sync apic's tpr */
5384-
if (!irqchip_in_kernel(vcpu->kvm))
5385-
kvm_set_cr8(vcpu, kvm_run->cr8);
5378+
if (!irqchip_in_kernel(vcpu->kvm)) {
5379+
if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
5380+
r = -EINVAL;
5381+
goto out;
5382+
}
5383+
}
53865384

53875385
if (vcpu->arch.pio.count || vcpu->mmio_needed) {
53885386
if (vcpu->mmio_needed) {

0 commit comments

Comments
 (0)