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>
---
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)
This commit is contained in:
Xiaoyao Li
2023-01-17 22:35:52 +08:00
parent cf78496f76
commit c19eb0bf28
3 changed files with 110 additions and 3 deletions

View File

@@ -483,10 +483,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' ] }
##
# @GuestPanicInformation:
@@ -501,7 +503,8 @@
'base': {'type': 'GuestPanicInformationType'},
'discriminator': 'type',
'data': {'hyper-v': 'GuestPanicInformationHyperV',
's390': 'GuestPanicInformationS390'}}
's390': 'GuestPanicInformationS390',
'tdx' : 'GuestPanicInformationTdx'}}
##
# @GuestPanicInformationHyperV:
@@ -564,6 +567,30 @@
'psw-addr': 'uint64',
'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: 9.0
##
{'struct': 'GuestPanicInformationTdx',
'data': {'error-code': 'uint64',
'message': 'str',
'*gpa': 'uint64'}}
##
# @MEMORY_FAILURE:
#

View File

@@ -519,6 +519,52 @@ 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 NUL-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 (!printable && len) {
/* 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 NUL */
buf[i * 3 - 1] = '\0';
else
buf[0] = '\0';
return buf;
}
return message;
}
void qemu_system_guest_panicked(GuestPanicInformation *info)
{
qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
@@ -560,7 +606,19 @@ 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 == GUEST_PANIC_INFORMATION_TYPE_TDX) {
qemu_log_mask(LOG_GUEST_ERROR,
" TDX guest reports fatal error:"
" error code: 0x%#" PRIx64 " error message:\"%s\"\n",
info->u.tdx.error_code,
tdx_parse_panic_message(info->u.tdx.message));
if (info->u.tdx.error_code & (1ull << 63)) {
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);
}
}

View File

@@ -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/ramblock.h"
@@ -1092,11 +1093,26 @@ static int tdx_handle_get_quote(X86CPU *cpu, struct kvm_tdx_vmcall *vmcall)
return 0;
}
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 = error_code;
panic_info->u.tdx.message = message;
panic_info->u.tdx.gpa = gpa;
qemu_system_guest_panicked(panic_info);
}
static int tdx_handle_report_fatal_error(X86CPU *cpu,
struct kvm_tdx_vmcall *vmcall)
{
uint64_t error_code = vmcall->in_r12;
char *message = NULL;
uint64_t gpa = -1ull;
if (error_code & 0xffff) {
error_report("TDX: REPORT_FATAL_ERROR: invalid error code: "
@@ -1125,7 +1141,13 @@ static int tdx_handle_report_fatal_error(X86CPU *cpu,
assert((char *)tmp == message + GUEST_PANIC_INFO_TDX_MESSAGE_MAX);
}
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 = vmcall->in_r13;
}
tdx_panicked_on_fatal_error(cpu, error_code, message, gpa);
return -1;
}