From nobody Sat Apr 5 13:51:49 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.com; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1743515682; cv=none; d=zohomail.com; s=zohoarc; b=l1EaucgowrZiyVY0OK+t/uGmGVZl88Au2e7OP5e367Et3Cu1c7rRhAZyOyz9DVD8tr0jkhI1kq9YbVHZ0Wl4vUsjbl+pl1ze3HLuUKvDZ+WCP3RzI4Or63goahuDUIDh+w2eBTFUG2OgoeFmoqx9pmPiTPPgI+Dq0YaJloF5FvY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1743515682; h=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=EwXekQh0D9heKFpbSyUHh7WfwXKPAFkW6ud/cDmjLZg=; b=NBCMQHL5YwGA/A1qnzQbmyyfRGl0x8yNPf5rjz3p/vG2NJSzPI8+n3o8CTq5VEkzruGrQ0lhAHdRpDGQTAakXKeQZQzemJlo0X3wPKniVQeDsvA6lVLJFpLNyqK3Gboh0HEd5MfwWeUogYJ2ZzcAa8eHq+jHDNj0ncXqW767cI8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.com; 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 174351568216160.63124942231252; Tue, 1 Apr 2025 06:54:42 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tzbte-0008W6-GN; Tue, 01 Apr 2025 09:43:19 -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 1tzbta-0008Fr-2P for qemu-devel@nongnu.org; Tue, 01 Apr 2025 09:43:15 -0400 Received: from mgamail.intel.com ([192.198.163.16]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tzbtW-0006XQ-3D for qemu-devel@nongnu.org; Tue, 01 Apr 2025 09:43:13 -0400 Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Apr 2025 06:42:26 -0700 Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa008.fm.intel.com with ESMTP; 01 Apr 2025 06:42:23 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1743514990; x=1775050990; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=BzUEZQXwyQ0SYtOiOVXTeFahkvxKBSDfEg7mXj11lgM=; b=aPoz4/zvZp5o2Jgi2rc+NM/qdOYQ3rNc9U2uyx3iA6nphG454HDCQ56z ziASUTdPMB9QZAv+r/JDqTZCJqFJHNUX1iitSjCLNl/YGTVb3S2tTiZQT NRImAJ5UFwy34qd4S6Rq8PjlS2cBnYb3srST3fjPBQdOMq5kYtgqaNG5Q XE9yObtek5u4lAQZQQ9FLlM+xn2VDlTsl9kBnTJiSlvhX+UuEkd6ayukZ iQ20co32bNzwHD0ZWofG7nOnQIexzNHp2h1SD8ITaPPrrFY92kOMMMBSO 5VmZA2WtnzMDVpJOHvakr8RhRlGkdXxU9hcT9DSdt6f7Ivs3AsT9ngu4C A==; X-CSE-ConnectionGUID: BqY5lwO4RPukf9f9rLQZOg== X-CSE-MsgGUID: bs8ILDwgSSGBjaeTc73x5w== X-IronPort-AV: E=McAfee;i="6700,10204,11391"; a="32433408" X-IronPort-AV: E=Sophos;i="6.14,293,1736841600"; d="scan'208";a="32433408" X-CSE-ConnectionGUID: xy7AvXGgQUqqNmdgcK7fgg== X-CSE-MsgGUID: UercaAKwQe21vEJ61dckXA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.14,293,1736841600"; d="scan'208";a="126640086" From: Xiaoyao Li To: Paolo Bonzini , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Cc: "Michael S. Tsirkin" , Markus Armbruster , Francesco Lavra , Marcelo Tosatti , qemu-devel@nongnu.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Rick Edgecombe , Xiaoyao Li Subject: [PATCH v8 29/55] i386/tdx: Wire TDX_REPORT_FATAL_ERROR with GuestPanic facility Date: Tue, 1 Apr 2025 09:01:39 -0400 Message-Id: <20250401130205.2198253-30-xiaoyao.li@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250401130205.2198253-1-xiaoyao.li@intel.com> References: <20250401130205.2198253-1-xiaoyao.li@intel.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=192.198.163.16; envelope-from=xiaoyao.li@intel.com; helo=mgamail.intel.com X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.997, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.999, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: 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 @intel.com) X-ZM-MESSAGEID: 1743515684551019100 Content-Type: text/plain; charset="utf-8" Integrate TDX's TDX_REPORT_FATAL_ERROR into QEMU GuestPanic facility Originated-from: Isaku Yamahata Signed-off-by: Xiaoyao Li Acked-by: Markus Armbruster --- Changes in v8: - use g_strdup() for copy string; - use the new data ABI of KVM_SYSTEM_EVENT_TDX_FATAL to grab gpa info; Changes in v6: - change error_code of GuestPanicInformationTdx from uint64_t to uint32_t, to only contains the bit 31:0 returned in r12. Changes in v5: - mention additional error information in gpa when it presents; - refine the documentation; (Markus) Changes in v4: - refine the documentation; (Markus) Changes in v3: - Add docmentation of new type and struct; (Daniel) - refine the error message handling; (Daniel) --- qapi/run-state.json | 31 +++++++++++++++++++-- system/runstate.c | 65 +++++++++++++++++++++++++++++++++++++++++++ target/i386/kvm/tdx.c | 24 +++++++++++++++- 3 files changed, 117 insertions(+), 3 deletions(-) diff --git a/qapi/run-state.json b/qapi/run-state.json index ce95cfa46b73..ee11adc50889 100644 --- a/qapi/run-state.json +++ b/qapi/run-state.json @@ -501,10 +501,12 @@ # # @s390: s390 guest panic information type (Since: 2.12) # +# @tdx: tdx guest panic information type (Since: 10.1) +# # Since: 2.9 ## { 'enum': 'GuestPanicInformationType', - 'data': [ 'hyper-v', 's390' ] } + 'data': [ 'hyper-v', 's390', 'tdx' ] } =20 ## # @GuestPanicInformation: @@ -519,7 +521,8 @@ 'base': {'type': 'GuestPanicInformationType'}, 'discriminator': 'type', 'data': {'hyper-v': 'GuestPanicInformationHyperV', - 's390': 'GuestPanicInformationS390'}} + 's390': 'GuestPanicInformationS390', + 'tdx' : 'GuestPanicInformationTdx'}} =20 ## # @GuestPanicInformationHyperV: @@ -598,6 +601,30 @@ 'psw-addr': 'uint64', 'reason': 'S390CrashReason'}} =20 +## +# @GuestPanicInformationTdx: +# +# TDX Guest panic information specific to TDX, as specified in the +# "Guest-Hypervisor Communication Interface (GHCI) Specification", +# section TDG.VP.VMCALL. +# +# @error-code: TD-specific error code +# +# @message: Human-readable error message provided by the guest. Not +# to be trusted. +# +# @gpa: guest-physical address of a page that contains more verbose +# error information, as zero-terminated string. Present when the +# "GPA valid" bit (bit 63) is set in @error-code. +# +# +# Since: 10.1 +## +{'struct': 'GuestPanicInformationTdx', + 'data': {'error-code': 'uint32', + 'message': 'str', + '*gpa': 'uint64'}} + ## # @MEMORY_FAILURE: # diff --git a/system/runstate.c b/system/runstate.c index 272801d30769..e9d5d7505b4a 100644 --- a/system/runstate.c +++ b/system/runstate.c @@ -565,6 +565,58 @@ static void qemu_system_wakeup(void) } } =20 +static char *tdx_parse_panic_message(char *message) +{ + bool printable =3D false; + char *buf =3D NULL; + int len =3D 0, i; + + /* + * Although message is defined as a json string, we shouldn't + * unconditionally treat it as is because the guest generated it and + * it's not necessarily trustable. + */ + if (message) { + /* The caller guarantees the NULL-terminated string. */ + len =3D strlen(message); + + printable =3D len > 0; + for (i =3D 0; i < len; i++) { + if (!(0x20 <=3D message[i] && message[i] <=3D 0x7e)) { + printable =3D false; + break; + } + } + } + + if (len =3D=3D 0) { + buf =3D g_malloc(1); + buf[0] =3D '\0'; + } else { + if (!printable) { + /* 3 =3D length of "%02x " */ + buf =3D g_malloc(len * 3); + for (i =3D 0; i < len; i++) { + if (message[i] =3D=3D '\0') { + break; + } else { + sprintf(buf + 3 * i, "%02x ", message[i]); + } + } + if (i > 0) { + /* replace the last ' '(space) to NULL */ + buf[i * 3 - 1] =3D '\0'; + } else { + buf[0] =3D '\0'; + } + } else { + buf =3D g_strdup(message); + } + } + + return buf; +} + void qemu_system_guest_panicked(GuestPanicInformation *info) { qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed"); @@ -606,7 +658,20 @@ void qemu_system_guest_panicked(GuestPanicInformation = *info) S390CrashReason_str(info->u.s390.reason), info->u.s390.psw_mask, info->u.s390.psw_addr); + } else if (info->type =3D=3D GUEST_PANIC_INFORMATION_TYPE_TDX) { + char *message =3D tdx_parse_panic_message(info->u.tdx.message); + qemu_log_mask(LOG_GUEST_ERROR, + "\nTDX guest reports fatal error." + " error code: 0x%" PRIx32 " error message:\"%s\"= \n", + info->u.tdx.error_code, message); + g_free(message); + if (info->u.tdx.gpa !=3D -1ull) { + qemu_log_mask(LOG_GUEST_ERROR, "Additional error informati= on " + "can be found at gpa page: 0x%" PRIx64 "\n", + info->u.tdx.gpa); + } } + qapi_free_GuestPanicInformation(info); } } diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 74b7e3ac85fe..282cdbd775c5 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -16,6 +16,7 @@ #include "qapi/error.h" #include "qom/object_interfaces.h" #include "crypto/hash.h" +#include "system/runstate.h" #include "system/system.h" #include "exec/ramblock.h" =20 @@ -607,12 +608,27 @@ int tdx_parse_tdvf(void *flash_ptr, int size) return tdvf_parse_metadata(&tdx_guest->tdvf, flash_ptr, size); } =20 +static void tdx_panicked_on_fatal_error(X86CPU *cpu, uint64_t error_code, + char *message, uint64_t gpa) +{ + GuestPanicInformation *panic_info; + + panic_info =3D g_new0(GuestPanicInformation, 1); + panic_info->type =3D GUEST_PANIC_INFORMATION_TYPE_TDX; + panic_info->u.tdx.error_code =3D (uint32_t) error_code; + panic_info->u.tdx.message =3D message; + panic_info->u.tdx.gpa =3D gpa; + + qemu_system_guest_panicked(panic_info); +} + int tdx_handle_report_fatal_error(X86CPU *cpu, struct kvm_run *run) { uint64_t error_code =3D run->system_event.data[R_R12]; uint64_t reg_mask =3D run->system_event.data[R_ECX]; char *message =3D NULL; uint64_t *tmp; + uint64_t gpa =3D -1ull; =20 if (error_code & 0xffff) { error_report("TDX: REPORT_FATAL_ERROR: invalid error code: 0x%lx", @@ -648,7 +664,13 @@ int tdx_handle_report_fatal_error(X86CPU *cpu, struct = kvm_run *run) } #undef COPY_REG =20 - error_report("TD guest reports fatal error. %s", message ? : ""); +#define TDX_REPORT_FATAL_ERROR_GPA_VALID BIT_ULL(63) + if (error_code & TDX_REPORT_FATAL_ERROR_GPA_VALID) { + gpa =3D run->system_event.data[R_R13]; + } + + tdx_panicked_on_fatal_error(cpu, error_code, message, gpa); + return -1; } =20 --=20 2.34.1