From nobody Wed Dec 17 12:25:21 2025 Received: from mail-io1-f74.google.com (mail-io1-f74.google.com [209.85.166.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 21CAE34C991 for ; Tue, 28 Oct 2025 21:20:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761686458; cv=none; b=ZVQKhFNkkM0dQAvImOOJJJNPYXHxvY5heJO6mCSdUwFYGm2YDFD/iMZI2mk38UpF+NghwN0lXduJC+yjsccW9LiMtim9rVGh8AT2lakg95hNohLWv8VsOEEmSvZMbvOQifPfqqGxtP37L3UM422mV0n5p884jG6YZ12vIW0xgqg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761686458; c=relaxed/simple; bh=1taRtnVW6cATeCX53WJ7F7rAY20El5Aqn3oYb2ry0KY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aM+oQACYeqj42xQBE6dMTKIv/KM9JBlo7EyMq13ZheWIvxO2sG2oLT6ZhDJ6zDfN7ypSGf0oQmMI+PTrR688OpkqtAHoCTtBlJ5fOyX8XC2eurHQE7060sVd91j/UFJqf2BGWrnYy5fkwtOEr/Hqi7p965yQIcIizClSSPQEDNo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--sagis.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=JyrXlD0E; arc=none smtp.client-ip=209.85.166.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sagis.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="JyrXlD0E" Received: by mail-io1-f74.google.com with SMTP id ca18e2360f4ac-9228ed70eb7so1949015439f.2 for ; Tue, 28 Oct 2025 14:20:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1761686456; x=1762291256; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=p7v7/UUME0K6OufONPcieqq83sRrWQqrOjB3BM2ME/o=; b=JyrXlD0EnMHCqZzQYMYn/CeY6Tb403Y0w4GpVveB6lY/mYK7mAqgiB0+0U+esIvggF GNkLi97zdJGU11xWmWo9OZZz+WBKHHVQf32HBgD4P2wWcw3IVT8vZd4yhf177F7vMtqG BBo+7J6D4yq4228Fq4cq3O7UVul2Q1ZkhrICjQnUGxtd2oIRU5BHpCPGhOqr3WK0KWJ1 ANgzyunn13ghI3CLanfrbsFQ3fDS8826nuw0QRZko6epnxydzimVfbD0e0xIb9PsgPDL wzfFS0Nub5KqQHy/7XGm9UrIgJicHTN31lxvWck8DE2s/rXWvhRlgw71J77vzvMoMyGm olGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761686456; x=1762291256; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=p7v7/UUME0K6OufONPcieqq83sRrWQqrOjB3BM2ME/o=; b=qnDT757t67zMk1JSF0p92krW6Xr2sIOPp8/9lgzIV8KlNQ1Dk7EIwPt57vg4JypQfN NjgTags8seMeTmv9tctj2Ua62cB2rA4IbFqSmVA1qpx8bq6E0gxT1FXf2m6cEnjn9Akn hBDh5MZFdYC102cJpwrGk+46bFK91TqjHMHdHc7qaxeL/ouzxRbW+1RcAcFTNnnwKhDG CJAiCJq3zTg23makWrj0UBuC+z7NOps3TvMIsSC7QuQ6YH72n1aRlaRjMBkQjfPEmFfx 543xAnivv07x1kLGaHpvglv0EPF74jXjpxkTacgdwSSsQYnOPYZNQFY0dyP7eY5B5vek ULjg== X-Gm-Message-State: AOJu0Yyz0EpCALCVlQauKZkA86SlL5vLZHUz3r524mB2msWbkMw1Nw4l gXDjX5yXVfW0WHfPTqPp8bNkt19S/2aqvUwE/rfrgJTDUUmPSO2HuThnDgftU/Krkrhz2ok+I6k SyA== X-Google-Smtp-Source: AGHT+IHNXgJLtfugfZ8phIcSQWQdiDgA9oVsbmqb4/VEjcfKWvug5tIMa6jcolDnkLWp3M99cFU8XavGwQ== X-Received: from iobeh1.prod.google.com ([2002:a05:6602:4a01:b0:943:9a0c:d618]) (user=sagis job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6602:2ccb:b0:943:edb3:ce65 with SMTP id ca18e2360f4ac-945c97f2fddmr159522739f.15.1761686456312; Tue, 28 Oct 2025 14:20:56 -0700 (PDT) Date: Tue, 28 Oct 2025 21:20:27 +0000 In-Reply-To: <20251028212052.200523-1-sagis@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251028212052.200523-1-sagis@google.com> X-Mailer: git-send-email 2.51.1.851.g4ebd6896fd-goog Message-ID: <20251028212052.200523-2-sagis@google.com> Subject: [PATCH v12 01/23] KVM: selftests: Add macros so simplify creating VM shapes for non-default types From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Paolo Bonzini , Shuah Khan , Sean Christopherson , Ackerley Tng , Ryan Afranji , Andrew Jones , Isaku Yamahata , Erdem Aktas , Rick Edgecombe , Sagi Shahar , Roger Wang , Binbin Wu , Oliver Upton , "Pratik R. Sampat" , Reinette Chatre , Ira Weiny , Chao Gao , Chenyi Qiang Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Sean Christopherson Add VM_TYPE() and __VM_TYPE() macros to create a vm_shape structure given a type (and mode), and use the macros to define VM_SHAPE_{SEV,SEV_ES,SNP} shapes for x86's SEV family of VM shapes. Providing common infrastructure will avoid having to copy+paste vm_sev_create_with_one_vcpu() for TDX. Use the new SEV+ shapes and drop vm_sev_create_with_one_vcpu(). No functional change intended. Signed-off-by: Sean Christopherson Signed-off-by: Sagi Shahar Reviewed-by: Binbin Wu Reviewed-by: Ira Weiny --- .../testing/selftests/kvm/include/kvm_util.h | 14 +++++++ .../selftests/kvm/include/x86/processor.h | 4 ++ tools/testing/selftests/kvm/include/x86/sev.h | 2 - tools/testing/selftests/kvm/lib/x86/sev.c | 16 -------- .../selftests/kvm/x86/sev_smoke_test.c | 40 +++++++++---------- 5 files changed, 38 insertions(+), 38 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing= /selftests/kvm/include/kvm_util.h index d3f3e455c031..310ec2b8afb7 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -209,6 +209,20 @@ kvm_static_assert(sizeof(struct vm_shape) =3D=3D sizeo= f(uint64_t)); shape; \ }) =20 +#define __VM_TYPE(__mode, __type) \ +({ \ + struct vm_shape shape =3D { \ + .mode =3D (__mode), \ + .type =3D (__type) \ + }; \ + \ + shape; \ +}) + +#define VM_TYPE(__type) \ + __VM_TYPE(VM_MODE_DEFAULT, __type) + + #if defined(__aarch64__) =20 extern enum vm_guest_mode vm_mode_default; diff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/te= sting/selftests/kvm/include/x86/processor.h index 51cd84b9ca66..dd21e11e1908 100644 --- a/tools/testing/selftests/kvm/include/x86/processor.h +++ b/tools/testing/selftests/kvm/include/x86/processor.h @@ -362,6 +362,10 @@ static inline unsigned int x86_model(unsigned int eax) return ((eax >> 12) & 0xf0) | ((eax >> 4) & 0x0f); } =20 +#define VM_SHAPE_SEV VM_TYPE(KVM_X86_SEV_VM) +#define VM_SHAPE_SEV_ES VM_TYPE(KVM_X86_SEV_ES_VM) +#define VM_SHAPE_SNP VM_TYPE(KVM_X86_SNP_VM) + /* Page table bitfield declarations */ #define PTE_PRESENT_MASK BIT_ULL(0) #define PTE_WRITABLE_MASK BIT_ULL(1) diff --git a/tools/testing/selftests/kvm/include/x86/sev.h b/tools/testing/= selftests/kvm/include/x86/sev.h index 008b4169f5e2..3c3294599ba6 100644 --- a/tools/testing/selftests/kvm/include/x86/sev.h +++ b/tools/testing/selftests/kvm/include/x86/sev.h @@ -53,8 +53,6 @@ void snp_vm_launch_start(struct kvm_vm *vm, uint64_t poli= cy); void snp_vm_launch_update(struct kvm_vm *vm); void snp_vm_launch_finish(struct kvm_vm *vm); =20 -struct kvm_vm *vm_sev_create_with_one_vcpu(uint32_t type, void *guest_code, - struct kvm_vcpu **cpu); void vm_sev_launch(struct kvm_vm *vm, uint64_t policy, uint8_t *measuremen= t); =20 kvm_static_assert(SEV_RET_SUCCESS =3D=3D 0); diff --git a/tools/testing/selftests/kvm/lib/x86/sev.c b/tools/testing/self= tests/kvm/lib/x86/sev.c index c3a9838f4806..1e3f6514c28d 100644 --- a/tools/testing/selftests/kvm/lib/x86/sev.c +++ b/tools/testing/selftests/kvm/lib/x86/sev.c @@ -158,22 +158,6 @@ void snp_vm_launch_finish(struct kvm_vm *vm) vm_sev_ioctl(vm, KVM_SEV_SNP_LAUNCH_FINISH, &launch_finish); } =20 -struct kvm_vm *vm_sev_create_with_one_vcpu(uint32_t type, void *guest_code, - struct kvm_vcpu **cpu) -{ - struct vm_shape shape =3D { - .mode =3D VM_MODE_DEFAULT, - .type =3D type, - }; - struct kvm_vm *vm; - struct kvm_vcpu *cpus[1]; - - vm =3D __vm_create_with_vcpus(shape, 1, 0, guest_code, cpus); - *cpu =3D cpus[0]; - - return vm; -} - void vm_sev_launch(struct kvm_vm *vm, uint64_t policy, uint8_t *measuremen= t) { if (is_sev_snp_vm(vm)) { diff --git a/tools/testing/selftests/kvm/x86/sev_smoke_test.c b/tools/testi= ng/selftests/kvm/x86/sev_smoke_test.c index 77256c89bb8d..3903793c6750 100644 --- a/tools/testing/selftests/kvm/x86/sev_smoke_test.c +++ b/tools/testing/selftests/kvm/x86/sev_smoke_test.c @@ -74,7 +74,7 @@ static void compare_xsave(u8 *from_host, u8 *from_guest) abort(); } =20 -static void test_sync_vmsa(uint32_t type, uint64_t policy) +static void test_sync_vmsa(struct vm_shape shape, uint64_t policy) { struct kvm_vcpu *vcpu; struct kvm_vm *vm; @@ -84,7 +84,7 @@ static void test_sync_vmsa(uint32_t type, uint64_t policy) double x87val =3D M_PI; struct kvm_xsave __attribute__((aligned(64))) xsave =3D { 0 }; =20 - vm =3D vm_sev_create_with_one_vcpu(type, guest_code_xsave, &vcpu); + vm =3D vm_create_shape_with_one_vcpu(shape, &vcpu, guest_code_xsave); gva =3D vm_vaddr_alloc_shared(vm, PAGE_SIZE, KVM_UTIL_MIN_VADDR, MEM_REGION_TEST_DATA); hva =3D addr_gva2hva(vm, gva); @@ -120,13 +120,13 @@ static void test_sync_vmsa(uint32_t type, uint64_t po= licy) kvm_vm_free(vm); } =20 -static void test_sev(void *guest_code, uint32_t type, uint64_t policy) +static void test_sev(void *guest_code, struct vm_shape shape, uint64_t pol= icy) { struct kvm_vcpu *vcpu; struct kvm_vm *vm; struct ucall uc; =20 - vm =3D vm_sev_create_with_one_vcpu(type, guest_code, &vcpu); + vm =3D vm_create_shape_with_one_vcpu(shape, &vcpu, guest_code); =20 /* TODO: Validate the measurement is as expected. */ vm_sev_launch(vm, policy, NULL); @@ -171,12 +171,12 @@ static void guest_shutdown_code(void) __asm__ __volatile__("ud2"); } =20 -static void test_sev_shutdown(uint32_t type, uint64_t policy) +static void test_sev_shutdown(struct vm_shape shape, uint64_t policy) { struct kvm_vcpu *vcpu; struct kvm_vm *vm; =20 - vm =3D vm_sev_create_with_one_vcpu(type, guest_shutdown_code, &vcpu); + vm =3D vm_create_shape_with_one_vcpu(shape, &vcpu, guest_shutdown_code); =20 vm_sev_launch(vm, policy, NULL); =20 @@ -188,28 +188,28 @@ static void test_sev_shutdown(uint32_t type, uint64_t= policy) kvm_vm_free(vm); } =20 -static void test_sev_smoke(void *guest, uint32_t type, uint64_t policy) +static void test_sev_smoke(void *guest, struct vm_shape shape, uint64_t po= licy) { const u64 xf_mask =3D XFEATURE_MASK_X87_AVX; =20 - if (type =3D=3D KVM_X86_SNP_VM) - test_sev(guest, type, policy | SNP_POLICY_DBG); + if (shape.type =3D=3D KVM_X86_SNP_VM) + test_sev(guest, shape, policy | SNP_POLICY_DBG); else - test_sev(guest, type, policy | SEV_POLICY_NO_DBG); - test_sev(guest, type, policy); + test_sev(guest, shape, policy | SEV_POLICY_NO_DBG); + test_sev(guest, shape, policy); =20 - if (type =3D=3D KVM_X86_SEV_VM) + if (shape.type =3D=3D KVM_X86_SEV_VM) return; =20 - test_sev_shutdown(type, policy); + test_sev_shutdown(shape, policy); =20 if (kvm_has_cap(KVM_CAP_XCRS) && (xgetbv(0) & kvm_cpu_supported_xcr0() & xf_mask) =3D=3D xf_mask) { - test_sync_vmsa(type, policy); - if (type =3D=3D KVM_X86_SNP_VM) - test_sync_vmsa(type, policy | SNP_POLICY_DBG); + test_sync_vmsa(shape, policy); + if (shape.type =3D=3D KVM_X86_SNP_VM) + test_sync_vmsa(shape, policy | SNP_POLICY_DBG); else - test_sync_vmsa(type, policy | SEV_POLICY_NO_DBG); + test_sync_vmsa(shape, policy | SEV_POLICY_NO_DBG); } } =20 @@ -217,13 +217,13 @@ int main(int argc, char *argv[]) { TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SEV)); =20 - test_sev_smoke(guest_sev_code, KVM_X86_SEV_VM, 0); + test_sev_smoke(guest_sev_code, VM_SHAPE_SEV, 0); =20 if (kvm_cpu_has(X86_FEATURE_SEV_ES)) - test_sev_smoke(guest_sev_es_code, KVM_X86_SEV_ES_VM, SEV_POLICY_ES); + test_sev_smoke(guest_sev_es_code, VM_SHAPE_SEV_ES, SEV_POLICY_ES); =20 if (kvm_cpu_has(X86_FEATURE_SEV_SNP)) - test_sev_smoke(guest_snp_code, KVM_X86_SNP_VM, snp_default_policy()); + test_sev_smoke(guest_snp_code, VM_SHAPE_SNP, snp_default_policy()); =20 return 0; } --=20 2.51.1.851.g4ebd6896fd-goog