With protected state (like SEV-ES and SEV-SNP), KVM does not have direct access to guest registers. However, we deflect modifications to CR0, CR4 and EFER to the host. We also carry the apic_base register and learn about CR8 directly from a VMCB field. That means these bits of information do exist in the host's KVM data structures. If we ever want to resume consumption of an already initialized VMSA (guest state), we will need to also restore these additional bits of KVM state. Prepare ourselves for such a world by allowing set_sregs to set the relevant fields. This way, it mirrors get_sregs properly that already exposes them to user space. Signed-off-by: Alexander Graf <graf@xxxxxxxxxx> --- arch/x86/kvm/x86.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 7713420abab0..88fa8b657a2f 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11370,7 +11370,8 @@ static int __set_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs, int idx; struct desc_ptr dt; - if (!kvm_is_valid_sregs(vcpu, sregs)) + if (!vcpu->arch.guest_state_protected && + !kvm_is_valid_sregs(vcpu, sregs)) return -EINVAL; apic_base_msr.data = sregs->apic_base; @@ -11378,8 +11379,19 @@ static int __set_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs, if (kvm_set_apic_base(vcpu, &apic_base_msr)) return -EINVAL; - if (vcpu->arch.guest_state_protected) + if (vcpu->arch.guest_state_protected) { + /* + * While most actual guest state is hidden from us, + * CR{0,4,8}, efer and apic_base still hold KVM state + * with protection enabled, so let's allow restoring + */ + kvm_set_cr8(vcpu, sregs->cr8); + kvm_x86_ops.set_efer(vcpu, sregs->efer); + kvm_x86_ops.set_cr0(vcpu, sregs->cr0); + vcpu->arch.cr0 = sregs->cr0; + kvm_x86_ops.set_cr4(vcpu, sregs->cr4); return 0; + } dt.size = sregs->idt.limit; dt.address = sregs->idt.base; -- 2.39.2 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879