Re: [PATCHv2.1 05/29] x86/tdx: Add HLT support for TDX guests

From: Kirill A. Shutemov
Date: Wed Feb 02 2022 - 07:48:25 EST


On Tue, Feb 01, 2022 at 10:21:58PM +0100, Thomas Gleixner wrote:
> On Sun, Jan 30 2022 at 01:30, Kirill A. Shutemov wrote:
> > +/*
> > + * Used in __tdx_hypercall() to determine whether to enable interrupts
> > + * before issuing TDCALL for the EXIT_REASON_HLT case.
> > + */
> > +#define ENABLE_IRQS_BEFORE_HLT 0x01
> > +
> > /*
> > * __tdx_module_call() - Used by TDX guests to request services from
> > * the TDX module (does not include VMM services).
> > @@ -230,6 +237,30 @@ SYM_FUNC_START(__tdx_hypercall)
> >
> > movl $TDVMCALL_EXPOSE_REGS_MASK, %ecx
> >
> > + /*
> > + * For the idle loop STI needs to be called directly before
> > + * the TDCALL that enters idle (EXIT_REASON_HLT case). STI
> > + * instruction enables interrupts only one instruction later.
> > + * If there is a window between STI and the instruction that
> > + * emulates the HALT state, there is a chance for interrupts to
> > + * happen in this window, which can delay the HLT operation
> > + * indefinitely. Since this is the not the desired result,
> > + * conditionally call STI before TDCALL.
> > + *
> > + * Since STI instruction is only required for the idle case
> > + * (a special case of EXIT_REASON_HLT), use the r15 register
> > + * value to identify it. Since the R15 register is not used
> > + * by the VMM as per EXIT_REASON_HLT ABI, re-use it in
> > + * software to identify the STI case.
> > + */
> > + cmpl $EXIT_REASON_HLT, %r11d
> > + jne .Lskip_sti
> > + cmpl $ENABLE_IRQS_BEFORE_HLT, %r15d
> > + jne .Lskip_sti
> > + /* Set R15 register to 0, it is unused in EXIT_REASON_HLT case */
> > + xor %r15, %r15
> > + sti
> > +.Lskip_sti:
> > tdcall
>
> This really can be simplified:
>
> cmpl $EXIT_REASON_SAFE_HLT, %r11d
> jne .Lnohalt
> movl $EXIT_REASON_HLT, %r11d
> sti
> .Lnohalt:
> tdcall
>
> and the below becomes:
>
> static bool tdx_halt(void)
> {
> return !!__tdx_hypercall(EXIT_REASON_HLT, !!irqs_disabled(), 0, 0, 0, NULL);
> }
>
> void __cpuidle tdx_safe_halt(void)
> {
> if (__tdx_hypercall(EXIT_REASON_SAFE_HLT, 0, 0, 0, 0, NULL)
> WARN_ONCE(1, "HLT instruction emulation failed\n");
> }
>
> Hmm?

EXIT_REASON_* are architectural, see SDM vol 3D, appendix C. There's no
EXIT_REASON_SAFE_HLT.

Do you want to define a synthetic one? Like

#define EXIT_REASON_SAFE_HLT 0x10000

?

Looks dubious to me, I donno. I worry about possible conflicts with the
spec in the future.

--
Kirill A. Shutemov