Skip to content

Commit

Permalink
OS-7006 kvm leaks host %xcr0
Browse files Browse the repository at this point in the history
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Reviewed by: Alex Wilson <alex.wilson@joyent.com>
Reviewed by: John Levon <john.levon@joyent.com>
Approved by: Jerry Jelinek <jerry.jelinek@joyent.com>
  • Loading branch information
rmustacc committed Jun 13, 2018
1 parent 440eb6b commit 6d6a2a6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
16 changes: 16 additions & 0 deletions kvm_x86.c
Expand Up @@ -30,6 +30,7 @@
#include <sys/fp.h>
#include <sys/tss.h>
#include <sys/x86_archext.h>
#include <sys/controlregs.h>

#include <vm/page.h>
#include <vm/hat.h>
Expand Down Expand Up @@ -4534,6 +4535,9 @@ kvm_load_guest_fpu(struct kvm_vcpu *vcpu)

vcpu->guest_fpu_loaded = 1;
hma_fpu_start_guest(vcpu->arch.guest_fpu);
if (vcpu->kvm->arch.need_xcr0) {
set_xcr(XFEATURE_ENABLED_MASK, XFEATURE_LEGACY_FP);
}
KVM_TRACE1(fpu, int, 1);
}

Expand All @@ -4544,6 +4548,10 @@ kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
return;

vcpu->guest_fpu_loaded = 0;
if (vcpu->kvm->arch.need_xcr0) {
set_xcr(XFEATURE_ENABLED_MASK, vcpu->kvm->arch.host_xcr0);
}
KVM_TRACE1(fpu, int, 1);
hma_fpu_stop_guest(vcpu->arch.guest_fpu);
KVM_VCPU_KSTAT_INC(vcpu, kvmvs_fpu_reload);
set_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests);
Expand Down Expand Up @@ -4801,6 +4809,14 @@ kvm_arch_create_vm(void)
/* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);

if ((native_read_cr4() & CR4_OSXSAVE) != 0) {
kvm->arch.need_xcr0 = 1;
kvm->arch.host_xcr0 = get_xcr(XFEATURE_ENABLED_MASK);
} else {
kvm->arch.need_xcr0 = 0;
kvm->arch.host_xcr0 = 0;
}

return (kvm);
}

Expand Down
3 changes: 3 additions & 0 deletions kvm_x86host.h
Expand Up @@ -533,6 +533,9 @@ typedef struct kvm_arch {

struct kvm_xen_hvm_config xen_hvm_config;

uint8_t need_xcr0;
uint64_t host_xcr0;

/* fields used by HYPER-V emulation */
uint64_t hv_guest_os_id;
uint64_t hv_hypercall;
Expand Down

0 comments on commit 6d6a2a6

Please sign in to comment.