From nobody Tue Nov 26 18:21:29 2024 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=1706153684; cv=none; d=zohomail.com; s=zohoarc; b=VXof+8SmP79lFNPPt3dtGdrrdiTFLgwgNAiC6s7CMFV4a+tgrbztB/p0uE3pFGY+Kao3wrf6C7TBSZmXDX56H3AsqvA+lOVRTxFWhgKYIAY/nuSBKfjRHSRV+RgGvBU6B4oV/0UGVSt7dT/xrpmiPfXBosy0Oq7kGiGeMscG9W4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1706153684; 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=6T2Zed2bGgJUoRYft6p2ZPZsShJpugYqbiPBA2QLrrM=; b=TeGu8LYlLJtT4StQ2ECpuC+gWFfE+DsfLwrsqtyjcDjfsR77+99Cuhg7ay2slE1dmzMVryzkTviKCiMoCbDNlS4jvlvkSYb9SLRr/0V/S68Oyq1WQVdQ8gIqnKOpUblkfzWZIOT8pDC78XV3lpMxAHthQLoIcSzbVaFCiWtAT6k= 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 170615368452070.57633592964748; Wed, 24 Jan 2024 19:34:44 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rSqT2-0006Rb-CU; Wed, 24 Jan 2024 22:31:52 -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 1rSqSm-0005aD-6D for qemu-devel@nongnu.org; Wed, 24 Jan 2024 22:31:38 -0500 Received: from mgamail.intel.com ([192.198.163.10]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rSqSj-0001Dy-BI for qemu-devel@nongnu.org; Wed, 24 Jan 2024 22:31:35 -0500 Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jan 2024 19:28:28 -0800 Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by orviesa005.jf.intel.com with ESMTP; 24 Jan 2024 19:28:23 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706153493; x=1737689493; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=XbBZ8FghQKjl9boDgrpDUaHhve+aNsi3xaBiWaJYrm0=; b=j0fMXpp71xvaZMp7QeMffpaObY5s/EeA3sQs7UBFKgg4af4O0QeCvSDo MJ32Ia/qFE5I/DEQKg2R9vQT7P7PctD+Kr5iI6fJYCcStDmkqQSCS4fRh ZrzAUvjP2BHUXjoZAfRusJRUau1MBE3lxAbYJzURQCsm+qMmDO1tnj7v1 RXUWizqIoystScMrYtpwxxQ8NxprPwfYZKR54lLO4vh0pS0HXcBbVukDx 6otJaPbFwTxidU8ItQ2pre8AyqUOuPGJP5TOUwJZRKeHXm690SbBXS7/j YzDCN7PmfxxGe1dDHyBLDHXM/IoiNluhRiLtXIM+HzBWZZqGid+e67Tf6 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="9430148" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="9430148" X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="2086209" From: Xiaoyao Li To: Paolo Bonzini , David Hildenbrand , Igor Mammedov , "Michael S . Tsirkin" , Marcel Apfelbaum , Richard Henderson , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Cornelia Huck , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Eric Blake , Markus Armbruster , Marcelo Tosatti Cc: qemu-devel@nongnu.org, kvm@vger.kernel.org, xiaoyao.li@intel.com, Michael Roth , Sean Christopherson , Claudio Fontana , Gerd Hoffmann , Isaku Yamahata , Chenyi Qiang Subject: [PATCH v4 53/66] i386/tdx: Wire TDX_REPORT_FATAL_ERROR with GuestPanic facility Date: Wed, 24 Jan 2024 22:23:15 -0500 Message-Id: <20240125032328.2522472-54-xiaoyao.li@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240125032328.2522472-1-xiaoyao.li@intel.com> References: <20240125032328.2522472-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.10; envelope-from=xiaoyao.li@intel.com; helo=mgamail.intel.com X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.5, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HK_RANDOM_ENVFROM=0.999, HK_RANDOM_FROM=1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no 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: 1706153685048100016 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 --- 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 | 28 ++++++++++++++++++++-- system/runstate.c | 54 +++++++++++++++++++++++++++++++++++++++++++ target/i386/kvm/tdx.c | 24 ++++++++++++++++++- 3 files changed, 103 insertions(+), 3 deletions(-) diff --git a/qapi/run-state.json b/qapi/run-state.json index 08bc99cb8561..5429116679e3 100644 --- a/qapi/run-state.json +++ b/qapi/run-state.json @@ -485,10 +485,12 @@ # # @s390: s390 guest panic information type (Since: 2.12) # +# @tdx: tdx guest panic information type (Since: 8.2) +# # Since: 2.9 ## { 'enum': 'GuestPanicInformationType', - 'data': [ 'hyper-v', 's390' ] } + 'data': [ 'hyper-v', 's390', 'tdx' ] } =20 ## # @GuestPanicInformation: @@ -503,7 +505,8 @@ 'base': {'type': 'GuestPanicInformationType'}, 'discriminator': 'type', 'data': {'hyper-v': 'GuestPanicInformationHyperV', - 's390': 'GuestPanicInformationS390'}} + 's390': 'GuestPanicInformationS390', + 'tdx' : 'GuestPanicInformationTdx'}} =20 ## # @GuestPanicInformationHyperV: @@ -566,6 +569,27 @@ 'psw-addr': 'uint64', 'reason': 'S390CrashReason'}} =20 +## +# @GuestPanicInformationTdx: +# +# TDX Guest panic information specific to TDX GCHI +# TDG.VP.VMCALL. +# +# @error-code: TD-specific error code +# +# @gpa: guest-physical address of a page that contains additional +# error data, in forms of zero-terminated string. +# +# @message: Human-readable error message provided by the guest. Not +# to be trusted. +# +# Since: 9.0 +## +{'struct': 'GuestPanicInformationTdx', + 'data': {'error-code': 'uint64', + 'gpa': 'uint64', + 'message': 'str'}} + ## # @MEMORY_FAILURE: # diff --git a/system/runstate.c b/system/runstate.c index d6ab860ecaa7..1ae85ea2c345 100644 --- a/system/runstate.c +++ b/system/runstate.c @@ -519,6 +519,52 @@ 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 NUL-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 (!printable && len) { + /* 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 NUL */ + buf[i * 3 - 1] =3D '\0'; + else + buf[0] =3D '\0'; + + return buf; + } + + return message; +} + void qemu_system_guest_panicked(GuestPanicInformation *info) { qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed"); @@ -560,7 +606,15 @@ 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) { + qemu_log_mask(LOG_GUEST_ERROR, + " TDX guest reports fatal error:\"%s\"" + " error code: 0x%016" PRIx64 " gpa page: 0x%016"= PRIx64 "\n", + tdx_parse_panic_message(info->u.tdx.message), + info->u.tdx.error_code, + info->u.tdx.gpa); } + qapi_free_GuestPanicInformation(info); } } diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 1c79032ca262..4fbb18135951 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -20,6 +20,7 @@ #include "qom/object_interfaces.h" #include "standard-headers/asm-x86/kvm_para.h" #include "sysemu/kvm.h" +#include "sysemu/runstate.h" #include "sysemu/sysemu.h" #include "exec/address-spaces.h" #include "exec/ramblock.h" @@ -1078,11 +1079,26 @@ static int tdx_handle_get_quote(X86CPU *cpu, struct= kvm_tdx_vmcall *vmcall) return 0; } =20 +static void tdx_panicked_on_fatal_error(X86CPU *cpu, uint64_t error_code, + uint64_t gpa, char *message) +{ + 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 error_code; + panic_info->u.tdx.gpa =3D gpa; + panic_info->u.tdx.message =3D message; + + qemu_system_guest_panicked(panic_info); +} + static int tdx_handle_report_fatal_error(X86CPU *cpu, struct kvm_tdx_vmcall *vmcall) { uint64_t error_code =3D vmcall->in_r12; char *message =3D NULL; + uint64_t gpa =3D -1ull; =20 if (error_code & 0xffff) { error_report("TDX: REPORT_FATAL_ERROR: invalid error code: " @@ -1111,7 +1127,13 @@ static int tdx_handle_report_fatal_error(X86CPU *cpu, assert((char *)tmp =3D=3D message + GUEST_PANIC_INFO_TDX_MESSAGE_M= AX); } =20 - error_report("TD guest reports fatal error. %s\n", message ? : ""); +#define TDX_REPORT_FATAL_ERROR_GPA_VALID BIT_ULL(63) + if (error_code & TDX_REPORT_FATAL_ERROR_GPA_VALID) { + gpa =3D vmcall->in_r13; + } + + tdx_panicked_on_fatal_error(cpu, error_code, gpa, message); + return -1; } =20 --=20 2.34.1