[PATCH RFC v3 40/43] KVM: x86: Add support for applying content modes
From: Ackerley Tng
Date: Fri Mar 13 2026 - 02:30:54 EST
For x86, override the default implementations of content mode functions to
handle reporting of supported content modes, and application of requested
modes based on x86 VM types.
Signed-off-by: Ackerley Tng <ackerleytng@xxxxxxxxxx>
---
arch/x86/kvm/x86.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 69 insertions(+)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 9c29407712580..49c19897a9c30 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -14078,6 +14078,75 @@ void kvm_arch_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end)
kvm_x86_call(gmem_invalidate)(start, end);
}
#endif
+
+u64 kvm_arch_gmem_supported_content_modes(struct kvm *kvm)
+{
+ switch (kvm->arch.vm_type) {
+ case KVM_X86_SW_PROTECTED_VM:
+ return KVM_SET_MEMORY_ATTRIBUTES2_ZERO |
+ KVM_SET_MEMORY_ATTRIBUTES2_PRESERVE;
+ case KVM_X86_SEV_VM:
+ case KVM_X86_SEV_ES_VM:
+ case KVM_X86_SNP_VM:
+ case KVM_X86_TDX_VM:
+ return 0;
+ case KVM_X86_DEFAULT_VM:
+ default:
+ WARN_ONCE(1, "Unexpected conversion request for vm_type.");
+ return 0;
+ }
+}
+
+int kvm_arch_gmem_apply_content_mode_zero(struct kvm *kvm,
+ struct folio *folio)
+{
+ switch (kvm->arch.vm_type) {
+ case KVM_X86_SW_PROTECTED_VM:
+ return kvm_gmem_apply_content_mode_zero(folio);
+ default:
+ WARN_ONCE(1, "Unexpected request to zero for vm_type.");
+ return -EOPNOTSUPP;
+ }
+}
+
+int kvm_arch_gmem_apply_content_mode_preserve(struct kvm *kvm,
+ struct folio *folio)
+{
+ switch (kvm->arch.vm_type) {
+ case KVM_X86_SW_PROTECTED_VM:
+ return 0;
+ default:
+ WARN_ONCE(1, "Unexpected request to preserve for vm_type.");
+ return -EOPNOTSUPP;
+ }
+}
+
+int kvm_arch_gmem_apply_content_mode_unspecified(struct kvm *kvm,
+ struct folio *folio)
+{
+ switch (kvm->arch.vm_type) {
+ case KVM_X86_SW_PROTECTED_VM: {
+ char *addr;
+ int i;
+
+ /*
+ * KVM_X86_SW_PROTECTED_VM is a test vehicle, this
+ * case is just for testing and does not need to be
+ * performant.
+ */
+ for (i = 0; i < folio_nr_pages(folio); ++i) {
+ addr = kmap_local_page(folio_page(folio, i));
+ get_random_bytes(addr, PAGE_SIZE);
+ kunmap_local(addr);
+ }
+
+ return 0;
+ }
+ default:
+ return 0;
+ }
+}
+
#endif
int kvm_spec_ctrl_test_value(u64 value)
--
2.53.0.851.ga537e3e6e9-goog