Re: [PATCH 2/4] x86/tdx: Use ReportFatalError to report missing SEPT_VE_DISABLE

Dave Hansen
2022-12-13T23:06:31Z

On 12/9/22 05:25, Kirill A. Shutemov wrote:
> The check for SEPT_VE_DISABLE happens early in the kernel boot where
> earlyprintk is not yet functional. Kernel successfully detect broken
> TD configuration and stops the kernel with panic(), but it cannot
> communicate the reason to the user.

Linux TDX guests require that the SEPT_VE_DISABLE "attribute" be set.
If it is not set, the kernel is theoretically required to handle
exceptions anywhere that kernel memory is accessed, including places
like NMI handlers and in the syscall entry gap.

Rather than even try to handle these exceptions, the kernel refuses to
run if SEPT_VE_DISABLE is unset.

However, the SEPT_VE_DISABLE detection and refusal code happens very
early in boot, even before earlyprintk runs. Calling panic() will
effectively just hang the system.

Instead, call a TDX-specific panic() function. This makes a very simple
TDVMCALL which gets a short error string out to the hypervisor without
any console infrastructure.

--

Is that better?

Also, are you sure we want to do this? Is there any way to do this
inside of panic() itself to get panic() itself to call tdx_panic() and
get a short error message out to the hypervisor?

Getting *all* users of panic this magic ability would be a lot better
than giving it to one call-site of panic().

I'm all for making the panic() path as short and simple as possible, but
it would be nice if this fancy hypercall would get used in more than one
spot.

> Use TDG.VP.VMCALL<ReportFatalError> to report the error. The hypercall
> can encode message up to 64 bytes in eight registers.
>
> Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
> ---
> arch/x86/coco/tdx/tdx.c | 38 +++++++++++++++++++++++++++++++++++++-
> 1 file changed, 37 insertions(+), 1 deletion(-)
>
> diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c
> index cfd4c95b9f04..8ad04d101270 100644
> --- a/arch/x86/coco/tdx/tdx.c
> +++ b/arch/x86/coco/tdx/tdx.c
> @@ -22,6 +22,7 @@
>
> /* TDX hypercall Leaf IDs */
> #define TDVMCALL_MAP_GPA 0x10001
> +#define TDVMCALL_REPORT_FATAL_ERROR 0x10003
>
> /* MMIO direction */
> #define EPT_READ 0
> @@ -140,6 +141,41 @@ int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport)
> }
> EXPORT_SYMBOL_GPL(tdx_mcall_get_report0);
>
> +static void __noreturn tdx_panic(const char *msg)
> +{
> + struct tdx_hypercall_args args = {
> + .r10 = TDX_HYPERCALL_STANDARD,
> + .r11 = TDVMCALL_REPORT_FATAL_ERROR,
> + .r12 = 0, /* Error code: 0 is Panic */
> + };
> + union {
> + /* Define register order according to the GHCI */
> + struct { u64 r14, r15, rbx, rdi, rsi, r8, r9, rdx; };
> +
> + char str[64];
> + } message;
> +
> + /* VMM assumes '\0' in byte 65, if the message took all 64 bytes */
> + strncpy(message.str, msg, 64);
> +
> + args.r8 = message.r8;
> + args.r9 = message.r9;
> + args.r14 = message.r14;
> + args.r15 = message.r15;
> + args.rdi = message.rdi;
> + args.rsi = message.rsi;
> + args.rbx = message.rbx;
> + args.rdx = message.rdx;

I dunno. Is that struct/union better, or would something like this be
more readable:

args.r8 = *(u64 *)&message[48];
args.r9 = *(u64 *)&message[56];

and just hard-code the offsets.

> + /*
> + * Keep calling the hypercall in case VMM did not terminated

terminate^

> + * the TD as it must.
> + */
> + while (1) {
> + __tdx_hypercall(&args, 0);
> + }
> +}
> +
> static void tdx_parse_tdinfo(u64 *cc_mask)
> {
> struct tdx_module_output out;
> @@ -172,7 +208,7 @@ static void tdx_parse_tdinfo(u64 *cc_mask)
> */
> td_attr = out.rdx;
> if (!(td_attr & ATTR_SEPT_VE_DISABLE))
> - panic("TD misconfiguration: SEPT_VE_DISABLE attibute must be set.\n");
> + tdx_panic("TD misconfiguration: SEPT_VE_DISABLE attribute must be set.");
> }

Would it be worth making it more clear when the message is truncated?
Maybe something like:

if (strlen(msg) > 64) {
len = 64
strncpy(&msg[61], "...", 3);
}

I'm sure I have five off-by-one bugs in there, but you get the idea.
Can we stick a "..." at the end of things that get truncated?