i386/tdx: Wire TDX_REPORT_FATAL_ERROR with GuestPanic facility

Integrate TDX's TDX_REPORT_FATAL_ERROR into QEMU GuestPanic facility

Originated-from: Isaku Yamahata <isaku.yamahata@intel.com>
Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
Acked-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Link: https://lore.kernel.org/r/20250508150002.689633-30-xiaoyao.li@intel.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Xiaoyao Li 2025-05-08 10:59:35 -04:00 committed by Paolo Bonzini
parent 98dbfd6849
commit 6e250463b0
3 changed files with 118 additions and 3 deletions

View file

@ -501,10 +501,12 @@
# #
# @s390: s390 guest panic information type (Since: 2.12) # @s390: s390 guest panic information type (Since: 2.12)
# #
# @tdx: tdx guest panic information type (Since: 10.1)
#
# Since: 2.9 # Since: 2.9
## ##
{ 'enum': 'GuestPanicInformationType', { 'enum': 'GuestPanicInformationType',
'data': [ 'hyper-v', 's390' ] } 'data': [ 'hyper-v', 's390', 'tdx' ] }
## ##
# @GuestPanicInformation: # @GuestPanicInformation:
@ -519,7 +521,8 @@
'base': {'type': 'GuestPanicInformationType'}, 'base': {'type': 'GuestPanicInformationType'},
'discriminator': 'type', 'discriminator': 'type',
'data': {'hyper-v': 'GuestPanicInformationHyperV', 'data': {'hyper-v': 'GuestPanicInformationHyperV',
's390': 'GuestPanicInformationS390'}} 's390': 'GuestPanicInformationS390',
'tdx' : 'GuestPanicInformationTdx'}}
## ##
# @GuestPanicInformationHyperV: # @GuestPanicInformationHyperV:
@ -598,6 +601,30 @@
'psw-addr': 'uint64', 'psw-addr': 'uint64',
'reason': 'S390CrashReason'}} 'reason': 'S390CrashReason'}}
##
# @GuestPanicInformationTdx:
#
# TDX Guest panic information specific to TDX, as specified in the
# "Guest-Hypervisor Communication Interface (GHCI) Specification",
# section TDG.VP.VMCALL<ReportFatalError>.
#
# @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: # @MEMORY_FAILURE:
# #

View file

@ -590,6 +590,58 @@ static void qemu_system_wakeup(void)
} }
} }
static char *tdx_parse_panic_message(char *message)
{
bool printable = false;
char *buf = NULL;
int len = 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 = strlen(message);
printable = len > 0;
for (i = 0; i < len; i++) {
if (!(0x20 <= message[i] && message[i] <= 0x7e)) {
printable = false;
break;
}
}
}
if (len == 0) {
buf = g_malloc(1);
buf[0] = '\0';
} else {
if (!printable) {
/* 3 = length of "%02x " */
buf = g_malloc(len * 3);
for (i = 0; i < len; i++) {
if (message[i] == '\0') {
break;
} else {
sprintf(buf + 3 * i, "%02x ", message[i]);
}
}
if (i > 0) {
/* replace the last ' '(space) to NULL */
buf[i * 3 - 1] = '\0';
} else {
buf[0] = '\0';
}
} else {
buf = g_strdup(message);
}
}
return buf;
}
void qemu_system_guest_panicked(GuestPanicInformation *info) void qemu_system_guest_panicked(GuestPanicInformation *info)
{ {
qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed"); qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
@ -631,7 +683,20 @@ void qemu_system_guest_panicked(GuestPanicInformation *info)
S390CrashReason_str(info->u.s390.reason), S390CrashReason_str(info->u.s390.reason),
info->u.s390.psw_mask, info->u.s390.psw_mask,
info->u.s390.psw_addr); info->u.s390.psw_addr);
} else if (info->type == GUEST_PANIC_INFORMATION_TYPE_TDX) {
char *message = 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 != -1ull) {
qemu_log_mask(LOG_GUEST_ERROR, "Additional error information "
"can be found at gpa page: 0x%" PRIx64 "\n",
info->u.tdx.gpa);
}
} }
qapi_free_GuestPanicInformation(info); qapi_free_GuestPanicInformation(info);
} }
} }

View file

@ -16,6 +16,7 @@
#include "qapi/error.h" #include "qapi/error.h"
#include "qom/object_interfaces.h" #include "qom/object_interfaces.h"
#include "crypto/hash.h" #include "crypto/hash.h"
#include "system/runstate.h"
#include "system/system.h" #include "system/system.h"
#include "system/ramblock.h" #include "system/ramblock.h"
@ -615,18 +616,35 @@ int tdx_parse_tdvf(void *flash_ptr, int size)
return tdvf_parse_metadata(&tdx_guest->tdvf, flash_ptr, size); return tdvf_parse_metadata(&tdx_guest->tdvf, flash_ptr, size);
} }
static void tdx_panicked_on_fatal_error(X86CPU *cpu, uint64_t error_code,
char *message, uint64_t gpa)
{
GuestPanicInformation *panic_info;
panic_info = g_new0(GuestPanicInformation, 1);
panic_info->type = GUEST_PANIC_INFORMATION_TYPE_TDX;
panic_info->u.tdx.error_code = (uint32_t) error_code;
panic_info->u.tdx.message = message;
panic_info->u.tdx.gpa = gpa;
qemu_system_guest_panicked(panic_info);
}
/* /*
* Only 8 registers can contain valid ASCII byte stream to form the fatal * Only 8 registers can contain valid ASCII byte stream to form the fatal
* message, and their sequence is: R14, R15, RBX, RDI, RSI, R8, R9, RDX * message, and their sequence is: R14, R15, RBX, RDI, RSI, R8, R9, RDX
*/ */
#define TDX_FATAL_MESSAGE_MAX 64 #define TDX_FATAL_MESSAGE_MAX 64
#define TDX_REPORT_FATAL_ERROR_GPA_VALID BIT_ULL(63)
int tdx_handle_report_fatal_error(X86CPU *cpu, struct kvm_run *run) int tdx_handle_report_fatal_error(X86CPU *cpu, struct kvm_run *run)
{ {
uint64_t error_code = run->system_event.data[R_R12]; uint64_t error_code = run->system_event.data[R_R12];
uint64_t reg_mask = run->system_event.data[R_ECX]; uint64_t reg_mask = run->system_event.data[R_ECX];
char *message = NULL; char *message = NULL;
uint64_t *tmp; uint64_t *tmp;
uint64_t gpa = -1ull;
if (error_code & 0xffff) { if (error_code & 0xffff) {
error_report("TDX: REPORT_FATAL_ERROR: invalid error code: 0x%lx", error_report("TDX: REPORT_FATAL_ERROR: invalid error code: 0x%lx",
@ -657,7 +675,12 @@ int tdx_handle_report_fatal_error(X86CPU *cpu, struct kvm_run *run)
} }
#undef COPY_REG #undef COPY_REG
error_report("TD guest reports fatal error. %s", message ? : ""); if (error_code & TDX_REPORT_FATAL_ERROR_GPA_VALID) {
gpa = run->system_event.data[R_R13];
}
tdx_panicked_on_fatal_error(cpu, error_code, message, gpa);
return -1; return -1;
} }