Skip to contentSkip to conversation

KVM: x86: Handle MXCSR in kvm_arch_vcpu_ioctl_{get,set}_fpu.

PATCH In review Justinien Bouron 2 participants 2 messages Sep 17, 2022 – Sep 20, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: x86: Handle MXCSR in kvm_arch_vcpu_ioctl_{get,set}_fpu.

kvm_arch_vcpu_ioctl_get_fpu does not set the mxcsr in the kvm_fpu struct; conversely kvm_arch_vcpu_ioctl_set_fpu does not set the mxcsr value in the fxregs_state struct of the vcpu.
This leads to the KVM_GET_FPU ioctl returning 0 as the mxcsr value, regardless of the actual value on the vcpu; while KVM_SET_FPU leaves the MXCSR on the vcpu untouched.

Fix kvm_arch_vcpu_ioctl_{get,set}_fpu to properly handle MXCSR.

---
 arch/x86/kvm/x86.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kvm/x86.c +2 -0
index 43a6a7efc6ec..c33a2599a497 100644
@@ -11462,6 +11462,7 @@ int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
 	fpu->last_ip = fxsave->rip;
 	fpu->last_dp = fxsave->rdp;
 	memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space));
+	fpu->mxcsr = fxsave->mxcsr;
 
 	vcpu_put(vcpu);
 	return 0;
@@ -11486,6 +11487,7 @@ int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
 	fxsave->rip = fpu->last_ip;
 	fxsave->rdp = fpu->last_dp;
 	memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space));
+	fxsave->mxcsr = fpu->mxcsr;
 
 	vcpu_put(vcpu);
 	return 0;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 27 lines · Justinien Bouron

kvm_arch_vcpu_ioctl_get_fpu does not set the mxcsr in the kvm_fpu struct; conversely kvm_arch_vcpu_ioctl_set_fpu does not set the mxcsr value in the fxregs_state struct of the vcpu.
This leads to the KVM_GET_FPU ioctl returning 0 as the mxcsr value, regardless of the actual value on the vcpu; while KVM_SET_FPU leaves the MXCSR on the vcpu untouched.

Fix kvm_arch_vcpu_ioctl_{get,set}_fpu to properly handle MXCSR.

---
 arch/x86/kvm/x86.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kvm/x86.c +1 -0
index 43a6a7efc6ec..c33a2599a497 100644
@@ -11462,6 +11462,7 @@ int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
 	fpu->last_ip = fxsave->rip;
 	fpu->last_dp = fxsave->rdp;
 	memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space));
+	fpu->mxcsr = fxsave->mxcsr;
 
 	vcpu_put(vcpu);
 	return 0;

arch/x86/kvm/x86.c int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) · line 11490

Quoted 4 lines
arch/x86/kvm/x86.c +1 -0
@@ -11486,6 +11487,7 @@ int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
 	fxsave->rip = fpu->last_ip;
 	fxsave->rdp = fpu->last_dp;
 	memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space));
+	fxsave->mxcsr = fpu->mxcsr;

The incoming MXCSR needs to be vetted, e.g. see fpu_copy_uabi_to_guest_fpstate().

That said, this code is incredibly ancient and has been obsolete for years. I wonder if we can get away with deprecating KVM_{G,S}ET_FPU on x86 and leaving the broken behavior as-is.