From nobody Sat Apr 5 18:05:29 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=1737726414; cv=none; d=zohomail.com; s=zohoarc; b=i2P8dU4xXeqObRJzMby9BPSZdAArvAAxXwc6gysHq0VAH7TZLii4w4wxTwLg09ifI4kkU6QY1uysP0momlG0YhuHxKu4JzRfpO2OUm8hYi05rTCnHKS/VuAyWlA2ZGaOLsi05y64fkEtIbQdgb9QZFcdcceImh43f3enSwzDq6w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1737726414; 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=DzHOZA2IFH3lCS2NGTQEwnr9XAF1drFKt4CaSh/wvak=; b=UkNVJ9EWELBs7wYTu98bhaHILkBKoRHDBO2x2SDiZ1T7ExHPL3jkPq38qNGo9xVbNTDJH4mwkSdZJsOYqpRcr2NVrnd4QeyzQY74n4E9Vg4Kk4HME37vioNTMpQvAk+1yMwoAqDKMJJPL/E1HMfgPqPAx4WznZw+GbntrxF5eBA= 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 1737726414236421.53798027082416; Fri, 24 Jan 2025 05:46:54 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tbJuN-0007qq-CT; Fri, 24 Jan 2025 08:39:39 -0500 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 1tbJtp-0006ru-2u for qemu-devel@nongnu.org; Fri, 24 Jan 2025 08:39:07 -0500 Received: from mgamail.intel.com ([198.175.65.13]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tbJtn-00041c-1E for qemu-devel@nongnu.org; Fri, 24 Jan 2025 08:39:04 -0500 Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by orvoesa105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jan 2025 05:38:48 -0800 Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa003.fm.intel.com with ESMTP; 24 Jan 2025 05:38:44 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1737725943; x=1769261943; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=85XePJaofoFgh2exxL+wI9EtRIBe+GFG6YbreIJ1ZbA=; b=DJJx2ROIYQZceuemaPGKjxCMjy4CGaiCyB/5WpDTWz/TJ2wkwxc6FQdk NKsT86kOqJnCtcuFC+Bb6Kn5WBqlnEA80soR5n1qaD1so9k2Jlzux7zS1 Kn8eVAyGbITPKzqdFTX0OsWjHqHe5i42kaNfmwMIT/Xhx4/xlzFqrM98C X4tvzu+afEFNhlbIfjiX3/39PYP+Es1hwZWbdDcdbY5PeylPAVQO6wQkk Qc9Budvzn17PVWNPOCiVo8C7FQ5eM1p4t9jten8LeZx5u/Z4v9E9IVSza vU/AQtSHwhVcIQGKnFMuszM2xtboEDjLoueUonM6MHuAz8b+RwSquLMhf w==; X-CSE-ConnectionGUID: f/QpukVlTrGfN33GT2d7bA== X-CSE-MsgGUID: dQx8MPTBTeCUxKLS3aBEog== X-IronPort-AV: E=McAfee;i="6700,10204,11325"; a="49246458" X-IronPort-AV: E=Sophos;i="6.13,231,1732608000"; d="scan'208";a="49246458" X-CSE-ConnectionGUID: mMNcARQFQ2m0Qdea8T315A== X-CSE-MsgGUID: CcvpFmjiSHeAN8kNvKqavA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,224,1728975600"; d="scan'208";a="111804358" From: Xiaoyao Li To: Paolo Bonzini , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Igor Mammedov Cc: Zhao Liu , "Michael S. Tsirkin" , Eric Blake , Markus Armbruster , Peter Maydell , Marcelo Tosatti , Huacai Chen , Rick Edgecombe , Francesco Lavra , xiaoyao.li@intel.com, qemu-devel@nongnu.org, kvm@vger.kernel.org Subject: [PATCH v7 28/52] i386/tdx: Wire TDX_REPORT_FATAL_ERROR with GuestPanic facility Date: Fri, 24 Jan 2025 08:20:24 -0500 Message-Id: <20250124132048.3229049-29-xiaoyao.li@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250124132048.3229049-1-xiaoyao.li@intel.com> References: <20250124132048.3229049-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=198.175.65.13; envelope-from=xiaoyao.li@intel.com; helo=mgamail.intel.com X-Spam_score_int: -63 X-Spam_score: -6.4 X-Spam_bar: ------ X-Spam_report: (-6.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.996, 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.998, 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, T_SCC_BODY_TEXT_LINE=-0.01 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: 1737726416254019100 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 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 | 67 +++++++++++++++++++++++++++++++++++++++++++ target/i386/kvm/tdx.c | 24 +++++++++++++++- 3 files changed, 119 insertions(+), 3 deletions(-) diff --git a/qapi/run-state.json b/qapi/run-state.json index ce95cfa46b73..e63611780a2c 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: 9.0) +# # 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.0 +## +{'struct': 'GuestPanicInformationTdx', + 'data': {'error-code': 'uint32', + 'message': 'str', + '*gpa': 'uint64'}} + ## # @MEMORY_FAILURE: # diff --git a/system/runstate.c b/system/runstate.c index 272801d30769..c4244c8915c6 100644 --- a/system/runstate.c +++ b/system/runstate.c @@ -565,6 +565,60 @@ 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_malloc(len); + memcpy(buf, message, len); + } + } + + return buf; +} + void qemu_system_guest_panicked(GuestPanicInformation *info) { qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed"); @@ -606,7 +660,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 f857fddd839b..591de30eedf4 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 @@ -601,10 +602,25 @@ 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[0]; char *message =3D NULL; + uint64_t gpa =3D -1ull; =20 if (error_code & 0xffff) { error_report("TDX: REPORT_FATAL_ERROR: invalid error code: 0x%lx", @@ -621,7 +637,13 @@ int tdx_handle_report_fatal_error(X86CPU *cpu, struct = kvm_run *run) message[TDX_FATAL_MESSAGE_MAX] =3D '\0'; } =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[1]; + } + + tdx_panicked_on_fatal_error(cpu, error_code, message, gpa); + return -1; } =20 --=20 2.34.1