From nobody Tue Apr 7 01:05:27 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1773752103; cv=none; d=zohomail.com; s=zohoarc; b=buwveLncDzDhhw9vbxDzdziYRzYQn0Sw1U8ptohNrnbu0zTGIbOYTy/mz8wsz4927F1THMylEmWpqVSGxo5nZ91xs4sop/h1tQINOK+PECkyVwVGc584Who3VLsEofPRUWnWLnjcpVZ0F4P+8Ttl/uBLkZY0YUHaqMREOIB95U4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1773752103; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Ku09qmzrODTSmfxYbubtxjPnJjnmpj9GC4sIO5Jhb34=; b=CpdkaLi3GebpDDE99aHtYIMbXkXmbE25HltfcYJ/xhxrl0r439JRKGTAjP5mHtvnfPgG1/MJd2tmxHxAyy4aG830FT8aR+96xrfFocxNIrYdT0672f+mj1qSsotdUHvlsrItMt+D8xrF2FkRcOzDIYnKJIuln70gXPZ9n/gfGWs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1773752103400541.3703796675342; Tue, 17 Mar 2026 05:55:03 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1w2TwV-0008Vk-66; Tue, 17 Mar 2026 08:54:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1w2Sl2-0003Su-7j for qemu-devel@nongnu.org; Tue, 17 Mar 2026 07:38:46 -0400 Received: from mail-wm1-x32c.google.com ([2a00:1450:4864:20::32c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1w2Sky-0007sO-9m for qemu-devel@nongnu.org; Tue, 17 Mar 2026 07:38:43 -0400 Received: by mail-wm1-x32c.google.com with SMTP id 5b1f17b1804b1-48538c5956bso6797525e9.0 for ; Tue, 17 Mar 2026 04:38:39 -0700 (PDT) Received: from DesktopTC.localdomain (host-87-27-45-215.business.telecomitalia.it. [87.27.45.215]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4856eaee04fsm62721365e9.13.2026.03.17.04.38.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Mar 2026 04:38:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773747518; x=1774352318; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Ku09qmzrODTSmfxYbubtxjPnJjnmpj9GC4sIO5Jhb34=; b=DgdnWjrRirWolBl5ZIq5Nl2ecAu72y6PPtMmYqfxWcPzXaCp9V/Uf7i9Sl31GUIxTI 4ZZ2pGFpmP0ksfuhvH74mUwiFNIXiXEprr6VfVRPC4KZDEXm1vwLO7u7kAhwkgfmlrUj IQIR0GLgMp1pIwG20guUHGlMsXwjwcZWbGv7dIK5EHdQS+Uqgbt4UQQBzhdTwG3yCcvu /MlEVgSd0DSik8p5SpUy5mKfgmmGRHmcLs+MWkgvzzk38Jy7jMspfmeM+BVvq/DJkqXc DQMEQyF8qES2OsyhpfQEISxTlO3K4b/fj8MSAMybxGu3Vij/nYWxjNHneyrt/wM3PGyt PrxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773747518; x=1774352318; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Ku09qmzrODTSmfxYbubtxjPnJjnmpj9GC4sIO5Jhb34=; b=mvKSoeJ/IdrjL3Yixp0bYY3+ddTarcRud3VHvMJQCjLRjYJghnEETZbG+ZGEnHglz6 Jz1ji7Zf7Hgr6wzJiMQlGid1GZ+3RCAHUOFI0u+wD/rKxkxW0QtFd3rB3qePr0FMCPcE dd2CftSWkgJDC94y5vylKGScnAlXCcUOdIBlXbD73p3ciY+Rd5/VwROySmgp+6YdgzEg fWdCZioFZ4S8whF6qULqMz1njhzmMqh8Fy+qhOz2TJUBtuzlxVuCm5pei1Sn4Sxh/s4c nrhJZdV7YPnK4m/DV9gnzNcQIzNFDqqexXvR8aBUJ0AIf9/uaC6NJxIxkelAaUf/lwXq vJoQ== X-Gm-Message-State: AOJu0YzW5c1hIgK87az46WJzN3CYLxvSqWWW4X+VasVkVzOGG+PHZDGe LlAWxZjRZz8RFuiVCHwJL6TWf8uBKUbcyM+B26PyIikupgENrJAJyE9TBzlsyutg X-Gm-Gg: ATEYQzwI92N3jqtX1HMeOqxnL1o6BHiikgh60uRgZ1JXfxkegeTwtDLWWjG1F4THQoC yrb8sZLwzTSgfDqqIrKv631koTvzaw7JhzOxiwIKlH04sezX/YJDGSiX4wZkDcT4mHFbajyzSk2 lyDCzOuQsomtcVApYL64P5vXHJeNDItDsq5jX+Keug5zYJaFBsYycPaBoJs3zIVk19NYGe/6XAH eIOu2i3IYfcYy9WIOpxlKAhhrcf2mGrsmN4a3Gi8qMUmMarEwSmAod3T8ZkIJM277jj/4dSbtfZ joZ07HP4/m1DB/LuPHMK41yOIpGrqgCaAAbSaUWfX6lm8+bIVpo7I+V1bpVXyCaD1qS5vcUcyim XzNXNalLDwx36GZXQv+LtE/EEPKRp5Ru0uCO+RR9YcQUkaw7fDSBY0914DUprBZRy4+YdQHgY+9 9XDMz8EBbTFpPV+PZh3Ie5JlGNg8GR9UbQHBe4r0OXeV3rnTE6I8MT52jpSNnHUm8izTtNXSAwa aTyXdf/P6aO7nf7AG7v X-Received: by 2002:a05:600c:c4aa:b0:477:9890:9ab8 with SMTP id 5b1f17b1804b1-4856eaaa656mr52729155e9.3.1773747517861; Tue, 17 Mar 2026 04:38:37 -0700 (PDT) From: Tommaso Califano To: qemu-devel@nongnu.org Cc: kvm@vger.kernel.org, Eduardo Habkost , Markus Armbruster , Zhao Liu , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Marcelo Tosatti , Eric Blake , Oliver Steffen , Stefano Garzarella , Giuseppe Lettieri , Paolo Bonzini , Luigi Leonardi , Richard Henderson , Tommaso Califano Subject: [PATCH 3/5] i386/sev: Implement SEV launch state sequence and query-sev Date: Tue, 17 Mar 2026 12:38:38 +0100 Message-ID: <20260317113840.33017-4-califano.tommaso@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260317113840.33017-1-califano.tommaso@gmail.com> References: <20260317113840.33017-1-califano.tommaso@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::32c; envelope-from=califano.tommaso@gmail.com; helo=mail-wm1-x32c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Tue, 17 Mar 2026 08:54:34 -0400 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1773752104619154100 Content-Type: text/plain; charset="utf-8" With prior patches exposing SEV CPUID and MSR, the guest recognizes SEV as active, but SEV progress states and "query-sev" QMP output remain incorrect, breaking the attestation workflow. For TCG emulation aimed at debugging and testing SEV guests=E2=80=94without= real cryptography needs=E2=80=94SEV_STATE_LAUNCH_START (crypto context initializ= ation) is skipped, proceeding directly to LAUNCH_UPDATE. Here, instead of encrypting firmware and (if kernel-hashes=3Don) kernel, this memory locations are tracked for future Launch Digest (LD) computation, required for the launch measurement in the next phase. These regions are stored in the QEMUIOVector ld_data within SevEmulatedState using sev_emulated_launch_update_data(). For the last state, sev_emulated_launch_finish() handles the transition to RUNNING state for the VM, while preserving the migration blocker. sev_emulated_init() initializes all fields for accurate "query-sev" output alongside state setup. This is preparatory for sev_launch_measurement() implementation. Note: In sev_kvm_type(), there is a condition that forces the legacy VM type for consistency. Normally, this function is never called during a TCG run. However, since sev-emulated derives from sev-guest, it is possible to run it with KVM support. This leads to incomplete emulation (MSR will be inactive, and C-bit management will be missing), although it still functions. In such cases, when the function is invoked and legacy-vm-type=3Doff is set, KVM compatibility checks will inevitably fail. Instead, this allows the VM to boot, issuing a warning about the change. Additionally, qmp_query_sev_capabilities and qmp_query_sev_attestation_report return a new error indicating that these functions are not supported if emulation is active. From this point, the VM follows the correct state transitions (except LAUNCH_SECRET), and the data reported by "query-sev" is consistent. Signed-off-by: Tommaso Califano --- target/i386/sev.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/target/i386/sev.c b/target/i386/sev.c index cdadd83ab5..5904f2c983 100644 --- a/target/i386/sev.c +++ b/target/i386/sev.c @@ -66,6 +66,12 @@ OBJECT_DECLARE_TYPE(SevSnpGuestState, SevCommonStateClas= s, SEV_SNP_GUEST) #define FLAGS_SEGCACHE_TO_VMSA(flags) \ ((((flags) & 0xff00) >> 8) | (((flags) & 0xf00000) >> 12)) =20 +/* SEV-EMULATED default values */ +#define INVALID_FD -1 +#define FAKE_BUILD_ID 40 +#define FAKE_API_MAJOR 1 +#define FAKE_API_MINOR 40 + typedef struct QEMU_PACKED SevHashTableEntry { QemuUUID guid; uint16_t len; @@ -191,6 +197,7 @@ struct SevGuestState { =20 typedef struct SevEmulatedState { SevGuestState parent_obj; + QEMUIOVector ld_data; } SevEmulatedState; =20 struct SevSnpGuestState { @@ -915,6 +922,12 @@ static SevCapability *sev_get_capabilities(Error **err= p) SevCommonState *sev_common; char *sev_device; =20 + if (sev_emulated_enabled()) { + error_setg(errp, "SEV emulation does not support" + "returning capabilities"); + return NULL; + } + if (!kvm_enabled()) { error_setg(errp, "KVM not enabled"); return NULL; @@ -1024,6 +1037,12 @@ static SevAttestationReport *sev_get_attestation_rep= ort(const char *mnonce, return NULL; } =20 + if (sev_emulated_enabled()) { + error_setg(errp, "SEV emulation does not support" + "attestation report"); + return NULL; + } + /* lets decode the mnonce string */ buf =3D g_base64_decode(mnonce, &len); if (!buf) { @@ -1752,6 +1771,21 @@ static int sev_kvm_type(X86ConfidentialGuest *cg) */ kvm_type =3D (sev_guest->policy & SEV_POLICY_ES) ? KVM_X86_SEV_ES_VM : KVM_X86_SEV_VM; + + /* + * If we are in emulated mode, force the legacy VM type as the only + * actively supported option. + */ + if (sev_emulated_enabled()) { + if (kvm_type !=3D KVM_X86_DEFAULT_VM) { + warn_report("Only legacy VM are supported in emulated mode:" + " using KVM_X86_DEFAULT_VM"); + kvm_type =3D KVM_X86_DEFAULT_VM; + } + sev_common->kvm_type =3D kvm_type; + goto out; + } + if (!kvm_is_vm_type_supported(kvm_type)) { if (sev_guest->legacy_vm_type =3D=3D ON_OFF_AUTO_AUTO) { error_report("SEV: host kernel does not support requested %s V= M type, which is required " @@ -2973,6 +3007,10 @@ sev_guest_instance_init(Object *obj) static int sev_emulated_init(ConfidentialGuestSupport *cgs, Error **errp) { SevCommonState *sev_common =3D SEV_COMMON(cgs); + SevGuestState *sev_guest =3D SEV_GUEST(sev_common); + SevEmulatedState *sev_emulated =3D SEV_EMULATED(sev_guest); + + sev_common->state =3D SEV_STATE_UNINIT; =20 /* * The cbitpos value will be placed in bit positions 5:0 of the EBX @@ -2999,15 +3037,53 @@ static int sev_emulated_init(ConfidentialGuestSuppo= rt *cgs, Error **errp) __func__, sev_common->reduced_phys_bits); return -1; } + /* + * The device does not exist so we initialize the values as default. + * We can skip to SEV_STATE_LAUNCH_UPDATE as there is nothing to encry= pt. + * This avoids the launch start call. + */ + sev_set_guest_state(sev_common, SEV_STATE_LAUNCH_UPDATE); + sev_common->sev_fd =3D INVALID_FD; + sev_common->build_id =3D FAKE_BUILD_ID; + sev_common->api_major =3D FAKE_API_MAJOR; + sev_common->api_minor =3D FAKE_API_MINOR; + + /* Initialize the iovec for the measurements blobs */ + qemu_iovec_init(&sev_emulated->ld_data, 3); + + qemu_add_vm_change_state_handler(sev_vm_state_change, sev_common); + cgs->ready =3D true; return 0; } =20 +static int sev_emulated_launch_update_data(SevCommonState *sev_common, + hwaddr gpa, uint8_t *addr, size_t len, Error **errp) +{ + SevEmulatedState *sev_emulated =3D SEV_EMULATED(sev_common); + + if (!addr || !len) { + return 1; + } + qemu_iovec_add(&sev_emulated->ld_data, addr, len); + + return 0; +} + +static void +sev_emulated_launch_finish(SevCommonState *sev_common) +{ + sev_set_guest_state(sev_common, SEV_STATE_RUNNING); +} + static void sev_emulated_class_init(ObjectClass *oc, const void *data) { + SevCommonStateClass *scc =3D SEV_COMMON_CLASS(oc); ConfidentialGuestSupportClass *klass =3D CONFIDENTIAL_GUEST_SUPPORT_CL= ASS(oc); - /* Override the sev-common method that uses kvm */ + /* Override the sev-common methods that use kvm */ klass->kvm_init =3D sev_emulated_init; + scc->launch_update_data =3D sev_emulated_launch_update_data; + scc->launch_finish =3D sev_emulated_launch_finish; } =20 /* guest info specific sev/sev-es */ --=20 2.53.0