Skip to contentSkip to conversation

Add KVM_EXIT_SHUTDOWN metadata for SEV-ES

PATCH Superseded Peter Gonda 2 participants 5 messages Mar 25, 2022 – Mar 30, 2022

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Add KVM_EXIT_SHUTDOWN metadata for SEV-ES

SEV-ES guests can request termination using the GHCB's MSR protocol. See AMD's GHCB spec section '4.1.13 Termination Request'. Currently when a guest does this the userspace VMM sees an KVM_EXIT_UNKNOWN (-EVINAL) return code from KVM_RUN. By adding a KVM_EXIT_SHUTDOWN_ENTRY to kvm_run struct the userspace VMM can clear see the guest has requested a SEV-ES termination including the termination reason code set and reason code.

Cc Paolo Bonzini <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Brijesh Singh <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Marc Orr <[email protected]>
Cc Sean Christopherson <[email protected]>

---

V2
* Add KVM_CAP_EXIT_SHUTDOWN_REASON check for KVM_CHECK_EXTENSION.

Tested by making an SEV-ES guest call sev_es_terminate() with hardcoded reason code set and reason code and then observing the codes from the userspace VMM in the kvm_run.shutdown.data fields.

---
 arch/x86/kvm/svm/sev.c   |  9 +++++++--
 include/uapi/linux/kvm.h | 13 +++++++++++++
 virt/kvm/kvm_main.c      |  1 +
 3 files changed, 21 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/sev.c +7 -2
index 75fa6dd268f0..5f9d37dd3f6f 100644
@@ -2735,8 +2735,13 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
 		pr_info("SEV-ES guest requested termination: %#llx:%#llx\n",
 			reason_set, reason_code);
 
-		ret = -EINVAL;
-		break;
+		vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
+		vcpu->run->shutdown.reason = KVM_SHUTDOWN_SEV_TERM;
+		vcpu->run->shutdown.ndata = 2;
+		vcpu->run->shutdown.data[0] = reason_set;
+		vcpu->run->shutdown.data[1] = reason_code;
+
+		return 0;
 	}
 	default:
 		/* Error, keep GHCB MSR value as-is */
include/uapi/linux/kvm.h +13 -0
index 8616af85dc5d..017c03421c48 100644
@@ -271,6 +271,12 @@ struct kvm_xen_exit {
 #define KVM_EXIT_XEN              34
 #define KVM_EXIT_RISCV_SBI        35
 
+/* For KVM_EXIT_SHUTDOWN */
+/* Standard VM shutdown request. No additional metadata provided. */
+#define KVM_SHUTDOWN_REQ	0
+/* SEV-ES termination request */
+#define KVM_SHUTDOWN_SEV_TERM	1
+
 /* For KVM_EXIT_INTERNAL_ERROR */
 /* Emulate instruction failed. */
 #define KVM_INTERNAL_ERROR_EMULATION	1
@@ -311,6 +317,12 @@ struct kvm_run {
 		struct {
 			__u64 hardware_exit_reason;
 		} hw;
+		/* KVM_EXIT_SHUTDOWN */
+		struct {
+			__u64 reason;
+			__u32 ndata;
+			__u64 data[16];
+		} shutdown;
 		/* KVM_EXIT_FAIL_ENTRY */
 		struct {
 			__u64 hardware_entry_failure_reason;
@@ -1145,6 +1157,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_PMU_CAPABILITY 212
 #define KVM_CAP_DISABLE_QUIRKS2 213
 #define KVM_CAP_VM_TSC_CONTROL 214
+#define KVM_CAP_EXIT_SHUTDOWN_REASON 215
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
virt/kvm/kvm_main.c +1 -0
index 70e05af5ebea..03b6e472f32c 100644
@@ -4299,6 +4299,7 @@ static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
 	case KVM_CAP_CHECK_EXTENSION_VM:
 	case KVM_CAP_ENABLE_CAP_VM:
 	case KVM_CAP_HALT_POLL:
+	case KVM_CAP_EXIT_SHUTDOWN_REASON:
 		return 1;
 #ifdef CONFIG_KVM_MMIO
 	case KVM_CAP_COALESCED_MMIO:
Signature · 1 line
2.35.1.1021.g381101b075-goog
maintainer
#1 lore raw
Quoted 7 lines · Peter Gonda

SEV-ES guests can request termination using the GHCB's MSR protocol. See AMD's GHCB spec section '4.1.13 Termination Request'. Currently when a guest does this the userspace VMM sees an KVM_EXIT_UNKNOWN (-EVINAL) return code from KVM_RUN. By adding a KVM_EXIT_SHUTDOWN_ENTRY to kvm_run struct the userspace VMM can clear see the guest has requested a SEV-ES termination including the termination reason code set and reason code.

Quoted 10 lines
Cc Paolo Bonzini <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Brijesh Singh <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Marc Orr <[email protected]>
Cc Sean Christopherson <[email protected]>

This is missing an update to Documentation/.

Paolo

Quoted 85 lines

---

V2
* Add KVM_CAP_EXIT_SHUTDOWN_REASON check for KVM_CHECK_EXTENSION.

Tested by making an SEV-ES guest call sev_es_terminate() with hardcoded reason code set and reason code and then observing the codes from the userspace VMM in the kvm_run.shutdown.data fields.

---
  arch/x86/kvm/svm/sev.c   |  9 +++++++--
  include/uapi/linux/kvm.h | 13 +++++++++++++
  virt/kvm/kvm_main.c      |  1 +
  3 files changed, 21 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/sev.c +7 -2
index 75fa6dd268f0..5f9d37dd3f6f 100644
@@ -2735,8 +2735,13 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
  		pr_info("SEV-ES guest requested termination: %#llx:%#llx\n",
  			reason_set, reason_code);
 
-		ret = -EINVAL;
-		break;
+		vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
+		vcpu->run->shutdown.reason = KVM_SHUTDOWN_SEV_TERM;
+		vcpu->run->shutdown.ndata = 2;
+		vcpu->run->shutdown.data[0] = reason_set;
+		vcpu->run->shutdown.data[1] = reason_code;
+
+		return 0;
  	}
  	default:
  		/* Error, keep GHCB MSR value as-is */
include/uapi/linux/kvm.h +13 -0
index 8616af85dc5d..017c03421c48 100644
@@ -271,6 +271,12 @@ struct kvm_xen_exit {
  #define KVM_EXIT_XEN              34
  #define KVM_EXIT_RISCV_SBI        35
 
+/* For KVM_EXIT_SHUTDOWN */
+/* Standard VM shutdown request. No additional metadata provided. */
+#define KVM_SHUTDOWN_REQ	0
+/* SEV-ES termination request */
+#define KVM_SHUTDOWN_SEV_TERM	1
+
  /* For KVM_EXIT_INTERNAL_ERROR */
  /* Emulate instruction failed. */
  #define KVM_INTERNAL_ERROR_EMULATION	1
@@ -311,6 +317,12 @@ struct kvm_run {
  		struct {
  			__u64 hardware_exit_reason;
  		} hw;
+		/* KVM_EXIT_SHUTDOWN */
+		struct {
+			__u64 reason;
+			__u32 ndata;
+			__u64 data[16];
+		} shutdown;
  		/* KVM_EXIT_FAIL_ENTRY */
  		struct {
  			__u64 hardware_entry_failure_reason;
@@ -1145,6 +1157,7 @@ struct kvm_ppc_resize_hpt {
  #define KVM_CAP_PMU_CAPABILITY 212
  #define KVM_CAP_DISABLE_QUIRKS2 213
  #define KVM_CAP_VM_TSC_CONTROL 214
+#define KVM_CAP_EXIT_SHUTDOWN_REASON 215
 
  #ifdef KVM_CAP_IRQ_ROUTING
 
virt/kvm/kvm_main.c +1 -0
index 70e05af5ebea..03b6e472f32c 100644
@@ -4299,6 +4299,7 @@ static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
  	case KVM_CAP_CHECK_EXTENSION_VM:
  	case KVM_CAP_ENABLE_CAP_VM:
  	case KVM_CAP_HALT_POLL:
+	case KVM_CAP_EXIT_SHUTDOWN_REASON:
  		return 1;
  #ifdef CONFIG_KVM_MMIO
  	case KVM_CAP_COALESCED_MMIO:
#2 lore raw
Quoted 20 lines · Paolo Bonzini
Quoted 7 lines · Peter Gonda

SEV-ES guests can request termination using the GHCB's MSR protocol. See AMD's GHCB spec section '4.1.13 Termination Request'. Currently when a guest does this the userspace VMM sees an KVM_EXIT_UNKNOWN (-EVINAL) return code from KVM_RUN. By adding a KVM_EXIT_SHUTDOWN_ENTRY to kvm_run struct the userspace VMM can clear see the guest has requested a SEV-ES termination including the termination reason code set and reason code.

Quoted 10 lines
Cc Paolo Bonzini <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Brijesh Singh <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Marc Orr <[email protected]>
Cc Sean Christopherson <[email protected]>
Quoted 2 lines

This is missing an update to Documentation/.

My mistake. I'll send another revision. Is the behavior of KVM_CAP_EXIT_SHUTDOWN_REASON OK? Or should we only return 1 for SEV-ES guests?

maintainer
#3 lore raw
Quoted 21 lines · Peter Gonda
Quoted 20 lines · Paolo Bonzini
Quoted 17 lines · Peter Gonda

SEV-ES guests can request termination using the GHCB's MSR protocol. See AMD's GHCB spec section '4.1.13 Termination Request'. Currently when a guest does this the userspace VMM sees an KVM_EXIT_UNKNOWN (-EVINAL) return code from KVM_RUN. By adding a KVM_EXIT_SHUTDOWN_ENTRY to kvm_run struct the userspace VMM can clear see the guest has requested a SEV-ES termination including the termination reason code set and reason code.

Cc Paolo Bonzini <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Brijesh Singh <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Marc Orr <[email protected]>
Cc Sean Christopherson <[email protected]>
Quoted 6 lines
Quoted 2 lines

This is missing an update to Documentation/.

My mistake. I'll send another revision. Is the behavior of KVM_CAP_EXIT_SHUTDOWN_REASON OK? Or should we only return 1 for SEV-ES guests?

No, you can return 1 unconditionally, but you should also set reason and clear ndata in the other cases that return KVM_EXIT_SHUTDOWN.

Paolo

#4 lore raw
Quoted 26 lines · Paolo Bonzini
Quoted 24 lines · Peter Gonda
Quoted 22 lines · Paolo Bonzini
Quoted 7 lines · Peter Gonda

SEV-ES guests can request termination using the GHCB's MSR protocol. See AMD's GHCB spec section '4.1.13 Termination Request'. Currently when a guest does this the userspace VMM sees an KVM_EXIT_UNKNOWN (-EVINAL) return code from KVM_RUN. By adding a KVM_EXIT_SHUTDOWN_ENTRY to kvm_run struct the userspace VMM can clear see the guest has requested a SEV-ES termination including the termination reason code set and reason code.

Quoted 10 lines
Cc Paolo Bonzini <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Brijesh Singh <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Marc Orr <[email protected]>
Cc Sean Christopherson <[email protected]>

This is missing an update to Documentation/.

Quoted 6 lines
Quoted 3 lines

My mistake. I'll send another revision. Is the behavior of KVM_CAP_EXIT_SHUTDOWN_REASON OK? Or should we only return 1 for SEV-ES guests?

No, you can return 1 unconditionally, but you should also set reason and clear ndata in the other cases that return KVM_EXIT_SHUTDOWN.

Sounds good! Done in V3, thanks