All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] x86/boot: enable NMIs after traps init
@ 2018-10-23 10:59 Sergey Dyasli
  2018-10-23 11:07 ` Andrew Cooper
  0 siblings, 1 reply; 10+ messages in thread
From: Sergey Dyasli @ 2018-10-23 10:59 UTC (permalink / raw)
  To: xen-devel; +Cc: Sergey Dyasli, Wei Liu, Jan Beulich, Andrew Cooper

In certain scenarios, NMIs might be disabled during Xen boot process.
Such situation will cause alternative_instructions() to:

    panic("Timed out waiting for alternatives self-NMI to hit");

This bug was originally seen when using Tboot to boot Xen.

To prevent this from happening, enable NMIs during cpu_init() and
during __start_xen() for BSP.

Signed-off-by: Sergey Dyasli <sergey.dyasli@citrix.com>
---
v2:
- Added enable_nmis() to __start_xen() for BSP
- Added comments as per Andrew's suggestion

CC: Jan Beulich <jbeulich@suse.com>
CC: Andrew Cooper <andrew.cooper3@citrix.com>
CC: Wei Liu <wei.liu2@citrix.com>
---
 xen/arch/x86/cpu/common.c | 3 +++
 xen/arch/x86/setup.c      | 3 +++
 2 files changed, 6 insertions(+)

diff --git a/xen/arch/x86/cpu/common.c b/xen/arch/x86/cpu/common.c
index 057859ab14..2d02335fef 100644
--- a/xen/arch/x86/cpu/common.c
+++ b/xen/arch/x86/cpu/common.c
@@ -819,6 +819,9 @@ void cpu_init(void)
 #define CD(register) asm volatile ( "mov %0,%%db" #register : : "r"(0UL) );
 	CD(0); CD(1); CD(2); CD(3); /* no db4 and db5 */; CD(6); CD(7);
 #undef CD
+
+	/* Enable NMIs in case our loader (e.g. Tboot) has left them disabled */
+	enable_nmis();
 }
 
 void cpu_uninit(unsigned int cpu)
diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c
index 93b79c7c0c..194388ef47 100644
--- a/xen/arch/x86/setup.c
+++ b/xen/arch/x86/setup.c
@@ -708,6 +708,9 @@ void __init noreturn __start_xen(unsigned long mbi_p)
 
     /* Full exception support from here on in. */
 
+    /* Re-enable NMIs in case our loader (e.g. Tboot) has left them disabled. */
+    enable_nmis();
+
     if ( pvh_boot )
     {
         /*
-- 
2.17.1


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH v2] x86/boot: enable NMIs after traps init
  2018-10-23 10:59 [PATCH v2] x86/boot: enable NMIs after traps init Sergey Dyasli
@ 2018-10-23 11:07 ` Andrew Cooper
  2018-10-23 14:01   ` Jason Andryuk
  0 siblings, 1 reply; 10+ messages in thread
From: Andrew Cooper @ 2018-10-23 11:07 UTC (permalink / raw)
  To: Sergey Dyasli, xen-devel; +Cc: Wei Liu, Jan Beulich

On 23/10/18 11:59, Sergey Dyasli wrote:
> In certain scenarios, NMIs might be disabled during Xen boot process.
> Such situation will cause alternative_instructions() to:
>
>     panic("Timed out waiting for alternatives self-NMI to hit");
>
> This bug was originally seen when using Tboot to boot Xen.
>
> To prevent this from happening, enable NMIs during cpu_init() and
> during __start_xen() for BSP.
>
> Signed-off-by: Sergey Dyasli <sergey.dyasli@citrix.com>

Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2] x86/boot: enable NMIs after traps init
  2018-10-23 11:07 ` Andrew Cooper
@ 2018-10-23 14:01   ` Jason Andryuk
  2018-10-23 14:33     ` Andrew Cooper
  0 siblings, 1 reply; 10+ messages in thread
From: Jason Andryuk @ 2018-10-23 14:01 UTC (permalink / raw)
  To: Andrew Cooper
  Cc: sergey.dyasli, Wei Liu, Ross Philipson, xen-devel, Jan Beulich,
	eric chanudet

On Tue, Oct 23, 2018 at 7:15 AM Andrew Cooper <andrew.cooper3@citrix.com> wrote:
>
> On 23/10/18 11:59, Sergey Dyasli wrote:
> > In certain scenarios, NMIs might be disabled during Xen boot process.
> > Such situation will cause alternative_instructions() to:
> >
> >     panic("Timed out waiting for alternatives self-NMI to hit");
> >
> > This bug was originally seen when using Tboot to boot Xen.
> >
> > To prevent this from happening, enable NMIs during cpu_init() and
> > during __start_xen() for BSP.
> >
> > Signed-off-by: Sergey Dyasli <sergey.dyasli@citrix.com>
>
> Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>

FYI, Ross and Eric came up with a tboot patch recently added to OpenXT:
https://github.com/OpenXT/xenclient-oe/blob/master/recipes-openxt/tboot/tboot-1.9.6/0023-tboot-Unmask-NMI-potentially-masked-during-SENTER.patch

Using this Xen patch with the tboot one reverted works too.

Tested-by: Jason Andryuk <jandryuk@gmail.com>

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2] x86/boot: enable NMIs after traps init
  2018-10-23 14:01   ` Jason Andryuk
@ 2018-10-23 14:33     ` Andrew Cooper
  2018-10-23 15:31       ` Jason Andryuk
  2018-10-25 12:23       ` Jan Beulich
  0 siblings, 2 replies; 10+ messages in thread
From: Andrew Cooper @ 2018-10-23 14:33 UTC (permalink / raw)
  To: Jason Andryuk
  Cc: sergey.dyasli, Wei Liu, Ross Philipson, xen-devel, Jan Beulich,
	eric chanudet

On 23/10/18 15:01, Jason Andryuk wrote:
> On Tue, Oct 23, 2018 at 7:15 AM Andrew Cooper <andrew.cooper3@citrix.com> wrote:
>> On 23/10/18 11:59, Sergey Dyasli wrote:
>>> In certain scenarios, NMIs might be disabled during Xen boot process.
>>> Such situation will cause alternative_instructions() to:
>>>
>>>     panic("Timed out waiting for alternatives self-NMI to hit");
>>>
>>> This bug was originally seen when using Tboot to boot Xen.
>>>
>>> To prevent this from happening, enable NMIs during cpu_init() and
>>> during __start_xen() for BSP.
>>>
>>> Signed-off-by: Sergey Dyasli <sergey.dyasli@citrix.com>
>> Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>
> FYI, Ross and Eric came up with a tboot patch recently added to OpenXT:
> https://github.com/OpenXT/xenclient-oe/blob/master/recipes-openxt/tboot/tboot-1.9.6/0023-tboot-Unmask-NMI-potentially-masked-during-SENTER.patch
>
> Using this Xen patch with the tboot one reverted works too.
>
> Tested-by: Jason Andryuk <jandryuk@gmail.com>

:(

Can bugs like this please be reported upstream?  Given the observation
of "Tboot hands off with NMIs disabled", the fix is very easy.

On the subject of having NMIs disabled, it is definitely a more robust
way of handing off between components.  Until Xen has transitioned the
BSP and APs into 64bit mode and fully set the IDT up, NMIs are fatal to
the system.

Furthermore, I've got some plans to suggest we do this on the kexec path
as well.  XenServer has one example where we crash from a GPU related
error, and a slightly delayed NMI arrives, usually when we're in purgatory.

~Andrew

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2] x86/boot: enable NMIs after traps init
  2018-10-23 14:33     ` Andrew Cooper
@ 2018-10-23 15:31       ` Jason Andryuk
  2018-10-23 16:42         ` Ross Philipson
  2018-10-25 12:23       ` Jan Beulich
  1 sibling, 1 reply; 10+ messages in thread
From: Jason Andryuk @ 2018-10-23 15:31 UTC (permalink / raw)
  To: Andrew Cooper
  Cc: sergey.dyasli, Wei Liu, Ross Philipson, xen-devel, Jan Beulich,
	eric chanudet

On Tue, Oct 23, 2018 at 10:46 AM Andrew Cooper
<andrew.cooper3@citrix.com> wrote:
>
> On 23/10/18 15:01, Jason Andryuk wrote:
> > On Tue, Oct 23, 2018 at 7:15 AM Andrew Cooper <andrew.cooper3@citrix.com> wrote:
> >> On 23/10/18 11:59, Sergey Dyasli wrote:
> >>> In certain scenarios, NMIs might be disabled during Xen boot process.
> >>> Such situation will cause alternative_instructions() to:
> >>>
> >>>     panic("Timed out waiting for alternatives self-NMI to hit");
> >>>
> >>> This bug was originally seen when using Tboot to boot Xen.
> >>>
> >>> To prevent this from happening, enable NMIs during cpu_init() and
> >>> during __start_xen() for BSP.
> >>>
> >>> Signed-off-by: Sergey Dyasli <sergey.dyasli@citrix.com>
> >> Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>
> > FYI, Ross and Eric came up with a tboot patch recently added to OpenXT:
> > https://github.com/OpenXT/xenclient-oe/blob/master/recipes-openxt/tboot/tboot-1.9.6/0023-tboot-Unmask-NMI-potentially-masked-during-SENTER.patch
> >
> > Using this Xen patch with the tboot one reverted works too.
> >
> > Tested-by: Jason Andryuk <jandryuk@gmail.com>
>
> :(
>
> Can bugs like this please be reported upstream?  Given the observation
> of "Tboot hands off with NMIs disabled", the fix is very easy.

I'm not opposed to reporting upstream.  In this case, I at least
assumed it was something we did in our EFI & tboot combo.  An Ivy
Bridge legacy boot system with tboot & Xen worked fine.  For me, it
was only a newer Skylake (or Kaby Lake) machine that had issue when
booting our EFI & tboot combo.  So it wasn't clear that tboot always
left NMIs disabled.  Yes, we should have reported something upstream
as a heads up for other tboot/Xen users.

Regards,
Jason

> On the subject of having NMIs disabled, it is definitely a more robust
> way of handing off between components.  Until Xen has transitioned the
> BSP and APs into 64bit mode and fully set the IDT up, NMIs are fatal to
> the system.
>
> Furthermore, I've got some plans to suggest we do this on the kexec path
> as well.  XenServer has one example where we crash from a GPU related
> error, and a slightly delayed NMI arrives, usually when we're in purgatory.
>
> ~Andrew

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2] x86/boot: enable NMIs after traps init
  2018-10-23 15:31       ` Jason Andryuk
@ 2018-10-23 16:42         ` Ross Philipson
  2018-10-23 16:58           ` Andrew Cooper
  0 siblings, 1 reply; 10+ messages in thread
From: Ross Philipson @ 2018-10-23 16:42 UTC (permalink / raw)
  To: Jason Andryuk, Andrew Cooper
  Cc: sergey.dyasli, Wei Liu, eric chanudet, Jan Beulich, xen-devel



On 10/23/2018 11:31 AM, Jason Andryuk wrote:
> On Tue, Oct 23, 2018 at 10:46 AM Andrew Cooper
> <andrew.cooper3@citrix.com> wrote:
>> On 23/10/18 15:01, Jason Andryuk wrote:
>>> On Tue, Oct 23, 2018 at 7:15 AM Andrew Cooper <andrew.cooper3@citrix.com> wrote:
>>>> On 23/10/18 11:59, Sergey Dyasli wrote:
>>>>> In certain scenarios, NMIs might be disabled during Xen boot process.
>>>>> Such situation will cause alternative_instructions() to:
>>>>>
>>>>>     panic("Timed out waiting for alternatives self-NMI to hit");
>>>>>
>>>>> This bug was originally seen when using Tboot to boot Xen.
>>>>>
>>>>> To prevent this from happening, enable NMIs during cpu_init() and
>>>>> during __start_xen() for BSP.
>>>>>
>>>>> Signed-off-by: Sergey Dyasli <sergey.dyasli@citrix.com>
>>>> Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>
>>> FYI, Ross and Eric came up with a tboot patch recently added to OpenXT:
>>> https://github.com/OpenXT/xenclient-oe/blob/master/recipes-openxt/tboot/tboot-1.9.6/0023-tboot-Unmask-NMI-potentially-masked-during-SENTER.patch
>>>
>>> Using this Xen patch with the tboot one reverted works too.
>>>
>>> Tested-by: Jason Andryuk <jandryuk@gmail.com>
>> :(
>>
>> Can bugs like this please be reported upstream?  Given the observation
>> of "Tboot hands off with NMIs disabled", the fix is very easy.
> I'm not opposed to reporting upstream.  In this case, I at least
> assumed it was something we did in our EFI & tboot combo.  An Ivy
> Bridge legacy boot system with tboot & Xen worked fine.  For me, it
> was only a newer Skylake (or Kaby Lake) machine that had issue when
> booting our EFI & tboot combo.  So it wasn't clear that tboot always
> left NMIs disabled.  Yes, we should have reported something upstream
> as a heads up for other tboot/Xen users.

According to the specs, NMIs and SMIs are disabled post launch on the BSP and
after AP wakeup is done. The TBOOT code explicitly re-enables SMIs but currently
not NMIs. Any IRET later on would have re-enabled them so it might explain how
they incidentally get re-enabled in certain configurations.

Personally I think the fix to re-enable them in TBOOT should go upstream.

Thanks
Ross

>
> Regards,
> Jason
>
>> On the subject of having NMIs disabled, it is definitely a more robust
>> way of handing off between components.  Until Xen has transitioned the
>> BSP and APs into 64bit mode and fully set the IDT up, NMIs are fatal to
>> the system.
>>
>> Furthermore, I've got some plans to suggest we do this on the kexec path
>> as well.  XenServer has one example where we crash from a GPU related
>> error, and a slightly delayed NMI arrives, usually when we're in purgatory.
>>
>> ~Andrew


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2] x86/boot: enable NMIs after traps init
  2018-10-23 16:42         ` Ross Philipson
@ 2018-10-23 16:58           ` Andrew Cooper
  2018-10-23 22:01             ` Ross Philipson
  0 siblings, 1 reply; 10+ messages in thread
From: Andrew Cooper @ 2018-10-23 16:58 UTC (permalink / raw)
  To: Ross Philipson, Jason Andryuk
  Cc: sergey.dyasli, Wei Liu, eric chanudet, Jan Beulich, xen-devel

On 23/10/18 17:42, Ross Philipson wrote:
>
> On 10/23/2018 11:31 AM, Jason Andryuk wrote:
>> On Tue, Oct 23, 2018 at 10:46 AM Andrew Cooper
>> <andrew.cooper3@citrix.com> wrote:
>>> On 23/10/18 15:01, Jason Andryuk wrote:
>>>> On Tue, Oct 23, 2018 at 7:15 AM Andrew Cooper <andrew.cooper3@citrix.com> wrote:
>>>>> On 23/10/18 11:59, Sergey Dyasli wrote:
>>>>>> In certain scenarios, NMIs might be disabled during Xen boot process.
>>>>>> Such situation will cause alternative_instructions() to:
>>>>>>
>>>>>>     panic("Timed out waiting for alternatives self-NMI to hit");
>>>>>>
>>>>>> This bug was originally seen when using Tboot to boot Xen.
>>>>>>
>>>>>> To prevent this from happening, enable NMIs during cpu_init() and
>>>>>> during __start_xen() for BSP.
>>>>>>
>>>>>> Signed-off-by: Sergey Dyasli <sergey.dyasli@citrix.com>
>>>>> Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>
>>>> FYI, Ross and Eric came up with a tboot patch recently added to OpenXT:
>>>> https://github.com/OpenXT/xenclient-oe/blob/master/recipes-openxt/tboot/tboot-1.9.6/0023-tboot-Unmask-NMI-potentially-masked-during-SENTER.patch
>>>>
>>>> Using this Xen patch with the tboot one reverted works too.
>>>>
>>>> Tested-by: Jason Andryuk <jandryuk@gmail.com>
>>> :(
>>>
>>> Can bugs like this please be reported upstream?  Given the observation
>>> of "Tboot hands off with NMIs disabled", the fix is very easy.
>> I'm not opposed to reporting upstream.  In this case, I at least
>> assumed it was something we did in our EFI & tboot combo.  An Ivy
>> Bridge legacy boot system with tboot & Xen worked fine.  For me, it
>> was only a newer Skylake (or Kaby Lake) machine that had issue when
>> booting our EFI & tboot combo.  So it wasn't clear that tboot always
>> left NMIs disabled.  Yes, we should have reported something upstream
>> as a heads up for other tboot/Xen users.
> According to the specs, NMIs and SMIs are disabled post launch on the BSP and
> after AP wakeup is done. The TBOOT code explicitly re-enables SMIs but currently
> not NMIs. Any IRET later on would have re-enabled them so it might explain how
> they incidentally get re-enabled in certain configurations.
>
> Personally I think the fix to re-enable them in TBOOT should go upstream.

Which spec?  Can it be changed/improved?

I accept this might be what it currently says, but enabling NMIs before
the OS is capable of handling them is only going to cause sad users on
anything but a completely idle system.

x86 is a gnarley architecture when it comes to this level of detail.  It
is not possible for a kernel to be a mode transition (real => protected,
or into long mode) and handle interrupts safely, because we can't
atomically set the mode and change the layout/position of the IDT.

We can at least avoid taking #MC when it is definitely unsafe to do so,
by not having CR4.MCE set.  An #MC at that point will be terminal to the
system, but you've got hardware problems at that point and all bets were
off anyway.  (Also, for those not keeping score on L1 Terminal Fault and
disabling hyperthreads threads, if any single one of your hyperthreads
has CR4.MCE clear, e.g. because your in the middle of booting it, an #MC
is fatal to the system.)

For NMI's the only way of guaranteeing that one doesn't arrive until the
IDT is set up properly is to leave the NMI shadow active until the OS
explicitly decided that it is safe.

While some might be viewed as a bug in tboot, I view it as a feature
which should be retained.  How late can late launches occur in practice?

~Andrew

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2] x86/boot: enable NMIs after traps init
  2018-10-23 16:58           ` Andrew Cooper
@ 2018-10-23 22:01             ` Ross Philipson
  2018-10-23 23:41               ` Andrew Cooper
  0 siblings, 1 reply; 10+ messages in thread
From: Ross Philipson @ 2018-10-23 22:01 UTC (permalink / raw)
  To: Andrew Cooper, Jason Andryuk
  Cc: sergey.dyasli, Wei Liu, eric chanudet, Jan Beulich, xen-devel



On 10/23/2018 12:58 PM, Andrew Cooper wrote:
> On 23/10/18 17:42, Ross Philipson wrote:
>> On 10/23/2018 11:31 AM, Jason Andryuk wrote:
>>> On Tue, Oct 23, 2018 at 10:46 AM Andrew Cooper
>>> <andrew.cooper3@citrix.com> wrote:
>>>> On 23/10/18 15:01, Jason Andryuk wrote:
>>>>> On Tue, Oct 23, 2018 at 7:15 AM Andrew Cooper <andrew.cooper3@citrix.com> wrote:
>>>>>> On 23/10/18 11:59, Sergey Dyasli wrote:
>>>>>>> In certain scenarios, NMIs might be disabled during Xen boot process.
>>>>>>> Such situation will cause alternative_instructions() to:
>>>>>>>
>>>>>>>     panic("Timed out waiting for alternatives self-NMI to hit");
>>>>>>>
>>>>>>> This bug was originally seen when using Tboot to boot Xen.
>>>>>>>
>>>>>>> To prevent this from happening, enable NMIs during cpu_init() and
>>>>>>> during __start_xen() for BSP.
>>>>>>>
>>>>>>> Signed-off-by: Sergey Dyasli <sergey.dyasli@citrix.com>
>>>>>> Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>
>>>>> FYI, Ross and Eric came up with a tboot patch recently added to OpenXT:
>>>>> https://github.com/OpenXT/xenclient-oe/blob/master/recipes-openxt/tboot/tboot-1.9.6/0023-tboot-Unmask-NMI-potentially-masked-during-SENTER.patch
>>>>>
>>>>> Using this Xen patch with the tboot one reverted works too.
>>>>>
>>>>> Tested-by: Jason Andryuk <jandryuk@gmail.com>
>>>> :(
>>>>
>>>> Can bugs like this please be reported upstream?  Given the observation
>>>> of "Tboot hands off with NMIs disabled", the fix is very easy.
>>> I'm not opposed to reporting upstream.  In this case, I at least
>>> assumed it was something we did in our EFI & tboot combo.  An Ivy
>>> Bridge legacy boot system with tboot & Xen worked fine.  For me, it
>>> was only a newer Skylake (or Kaby Lake) machine that had issue when
>>> booting our EFI & tboot combo.  So it wasn't clear that tboot always
>>> left NMIs disabled.  Yes, we should have reported something upstream
>>> as a heads up for other tboot/Xen users.
>> According to the specs, NMIs and SMIs are disabled post launch on the BSP and
>> after AP wakeup is done. The TBOOT code explicitly re-enables SMIs but currently
>> not NMIs. Any IRET later on would have re-enabled them so it might explain how
>> they incidentally get re-enabled in certain configurations.
>>
>> Personally I think the fix to re-enable them in TBOOT should go upstream.
> Which spec?  Can it be changed/improved?
>
> I accept this might be what it currently says, but enabling NMIs before
> the OS is capable of handling them is only going to cause sad users on
> anything but a completely idle system.

Sorry I should have been clearer. The Intel SDM states that various GETSEC leaf
functions leave the BSP or APs with both SMIs and NMIs disabled. It does not say
anything about where/when they should be re-enabled. It is the TBOOT
implementation that (presumably though it is not documented) decided to
re-enable SMIs and not NMIs.

>
> x86 is a gnarley architecture when it comes to this level of detail.  It
> is not possible for a kernel to be a mode transition (real => protected,
> or into long mode) and handle interrupts safely, because we can't
> atomically set the mode and change the layout/position of the IDT.
>
> We can at least avoid taking #MC when it is definitely unsafe to do so,
> by not having CR4.MCE set.  An #MC at that point will be terminal to the
> system, but you've got hardware problems at that point and all bets were
> off anyway.  (Also, for those not keeping score on L1 Terminal Fault and
> disabling hyperthreads threads, if any single one of your hyperthreads
> has CR4.MCE clear, e.g. because your in the middle of booting it, an #MC
> is fatal to the system.)
>
> For NMI's the only way of guaranteeing that one doesn't arrive until the
> IDT is set up properly is to leave the NMI shadow active until the OS
> explicitly decided that it is safe.
>
> While some might be viewed as a bug in tboot, I view it as a feature

Hmm that is an interesting perspective and I see your point. We will have to
think about this with respect to where we try to re-enable NMIs. On the other
side of this though, how long is it safe to leave NMIs disabled before it causes
a problem on the platform depending on what the NMIs might be doing? I believe
incoming NMIs are being pended in this state - that is clearly stated at least
on AMD when clearing/setting GIF.

> which should be retained.  How late can late launches occur in practice?

Theoretically at any point SMX mode can be entered/left/entered/etc - at least
that is how I understand it on Intel platforms.

>
> ~Andrew


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2] x86/boot: enable NMIs after traps init
  2018-10-23 22:01             ` Ross Philipson
@ 2018-10-23 23:41               ` Andrew Cooper
  0 siblings, 0 replies; 10+ messages in thread
From: Andrew Cooper @ 2018-10-23 23:41 UTC (permalink / raw)
  To: Ross Philipson, Jason Andryuk
  Cc: sergey.dyasli, Wei Liu, eric chanudet, Jan Beulich, xen-devel

On 23/10/2018 23:01, Ross Philipson wrote:
> On 10/23/2018 12:58 PM, Andrew Cooper wrote:
>> On 23/10/18 17:42, Ross Philipson wrote:
>>> On 10/23/2018 11:31 AM, Jason Andryuk wrote:
>>>> On Tue, Oct 23, 2018 at 10:46 AM Andrew Cooper
>>>> <andrew.cooper3@citrix.com> wrote:
>>>>> On 23/10/18 15:01, Jason Andryuk wrote:
>>>>>> On Tue, Oct 23, 2018 at 7:15 AM Andrew Cooper <andrew.cooper3@citrix.com> wrote:
>>>>>>> On 23/10/18 11:59, Sergey Dyasli wrote:
>>>>>>>> In certain scenarios, NMIs might be disabled during Xen boot process.
>>>>>>>> Such situation will cause alternative_instructions() to:
>>>>>>>>
>>>>>>>>     panic("Timed out waiting for alternatives self-NMI to hit");
>>>>>>>>
>>>>>>>> This bug was originally seen when using Tboot to boot Xen.
>>>>>>>>
>>>>>>>> To prevent this from happening, enable NMIs during cpu_init() and
>>>>>>>> during __start_xen() for BSP.
>>>>>>>>
>>>>>>>> Signed-off-by: Sergey Dyasli <sergey.dyasli@citrix.com>
>>>>>>> Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>
>>>>>> FYI, Ross and Eric came up with a tboot patch recently added to OpenXT:
>>>>>> https://github.com/OpenXT/xenclient-oe/blob/master/recipes-openxt/tboot/tboot-1.9.6/0023-tboot-Unmask-NMI-potentially-masked-during-SENTER.patch
>>>>>>
>>>>>> Using this Xen patch with the tboot one reverted works too.
>>>>>>
>>>>>> Tested-by: Jason Andryuk <jandryuk@gmail.com>
>>>>> :(
>>>>>
>>>>> Can bugs like this please be reported upstream?  Given the observation
>>>>> of "Tboot hands off with NMIs disabled", the fix is very easy.
>>>> I'm not opposed to reporting upstream.  In this case, I at least
>>>> assumed it was something we did in our EFI & tboot combo.  An Ivy
>>>> Bridge legacy boot system with tboot & Xen worked fine.  For me, it
>>>> was only a newer Skylake (or Kaby Lake) machine that had issue when
>>>> booting our EFI & tboot combo.  So it wasn't clear that tboot always
>>>> left NMIs disabled.  Yes, we should have reported something upstream
>>>> as a heads up for other tboot/Xen users.
>>> According to the specs, NMIs and SMIs are disabled post launch on the BSP and
>>> after AP wakeup is done. The TBOOT code explicitly re-enables SMIs but currently
>>> not NMIs. Any IRET later on would have re-enabled them so it might explain how
>>> they incidentally get re-enabled in certain configurations.
>>>
>>> Personally I think the fix to re-enable them in TBOOT should go upstream.
>> Which spec?  Can it be changed/improved?
>>
>> I accept this might be what it currently says, but enabling NMIs before
>> the OS is capable of handling them is only going to cause sad users on
>> anything but a completely idle system.
> Sorry I should have been clearer. The Intel SDM states that various GETSEC leaf
> functions leave the BSP or APs with both SMIs and NMIs disabled. It does not say
> anything about where/when they should be re-enabled. It is the TBOOT
> implementation that (presumably though it is not documented) decided to
> re-enable SMIs and not NMIs.

Oh - that's nice.  I wonder what the reason for that behaviour is? ;)

>> x86 is a gnarley architecture when it comes to this level of detail.  It
>> is not possible for a kernel to be a mode transition (real => protected,
>> or into long mode) and handle interrupts safely, because we can't
>> atomically set the mode and change the layout/position of the IDT.
>>
>> We can at least avoid taking #MC when it is definitely unsafe to do so,
>> by not having CR4.MCE set.  An #MC at that point will be terminal to the
>> system, but you've got hardware problems at that point and all bets were
>> off anyway.  (Also, for those not keeping score on L1 Terminal Fault and
>> disabling hyperthreads threads, if any single one of your hyperthreads
>> has CR4.MCE clear, e.g. because your in the middle of booting it, an #MC
>> is fatal to the system.)
>>
>> For NMI's the only way of guaranteeing that one doesn't arrive until the
>> IDT is set up properly is to leave the NMI shadow active until the OS
>> explicitly decided that it is safe.
>>
>> While some might be viewed as a bug in tboot, I view it as a feature
> Hmm that is an interesting perspective and I see your point. We will have to
> think about this with respect to where we try to re-enable NMIs. On the other
> side of this though, how long is it safe to leave NMIs disabled before it causes
> a problem on the platform depending on what the NMIs might be doing? I believe
> incoming NMIs are being pended in this state - that is clearly stated at least
> on AMD when clearing/setting GIF.

NMIs are handled as edge triggered.

As soon as one NMI is accepted, the NMI shadow is asserted until the
following IRET instruction.  (Faults for this IRET drop the shadow
before logically finishing the NMI handler, which is how we can end up
in the corner case of re-entrant NMIs - an issue which I still haven't
fixed in Xen yet.)

One single NMI can be held pending when one is in service, which will be
delivered as soon as the NMI shadow clears.  (It is possible from the
point of view of the NMI handler to observe a livelock on the underlying
code if one NMI gets delivered during the running of the running of the
NMI handler.  It turns our that Skylake hardware can easily spend 200ns
in SMI context on a read from port 0x61, which doesn't combine well with
a retired-instructions perfcounter serving as a watchdog.)

As for "how long is it safe to leave disabled", that still fairly easy
to answer.  What would you miss out on by not handling NMIs immediately?

The kernel itself won't set up sources of NMIs which it isn't capable of
handling, because that would be silly.  Therefore, we don't have IPIs or
perf counters configured with a delivery type of NMI.

Realistically, that means that arriving NMIs during this period are of
the unexpected variety, so most likely a PCI SERR/IOCK or other
interrupts raised by the system.  These really are safe to defer for the
milliseconds it takes to switch mode and get interrupt handling working
properly.

>> which should be retained.  How late can late launches occur in practice?
> Theoretically at any point SMX mode can be entered/left/entered/etc - at least
> that is how I understand it on Intel platforms.

Right, which can be after PCI devices are starting to be used in anger.

~Andrew

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2] x86/boot: enable NMIs after traps init
  2018-10-23 14:33     ` Andrew Cooper
  2018-10-23 15:31       ` Jason Andryuk
@ 2018-10-25 12:23       ` Jan Beulich
  1 sibling, 0 replies; 10+ messages in thread
From: Jan Beulich @ 2018-10-25 12:23 UTC (permalink / raw)
  To: Andrew Cooper
  Cc: Sergey Dyasli, Wei Liu, ross.philipson, Jason Andryuk, xen-devel,
	eric chanudet

>>> On 23.10.18 at 16:33, <andrew.cooper3@citrix.com> wrote:
> On the subject of having NMIs disabled, it is definitely a more robust
> way of handing off between components.  Until Xen has transitioned the
> BSP and APs into 64bit mode and fully set the IDT up, NMIs are fatal to
> the system.

I don't follow the AP part here - as you say elsewhere on this thread,
IPIs with NMI delivery won't be sent to not fully online CPUs, and
platform originated NMIs ought to go to CPU0, i.e. the BSP.

> Furthermore, I've got some plans to suggest we do this on the kexec path
> as well.  XenServer has one example where we crash from a GPU related
> error, and a slightly delayed NMI arrives, usually when we're in purgatory.

Couldn't we go further an send ourselves an NMI on the BSP (with the
IDT minimally set up so it can be delivered) to disable it until we're ready
to really service it?

Jan



_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2018-10-25 12:23 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-23 10:59 [PATCH v2] x86/boot: enable NMIs after traps init Sergey Dyasli
2018-10-23 11:07 ` Andrew Cooper
2018-10-23 14:01   ` Jason Andryuk
2018-10-23 14:33     ` Andrew Cooper
2018-10-23 15:31       ` Jason Andryuk
2018-10-23 16:42         ` Ross Philipson
2018-10-23 16:58           ` Andrew Cooper
2018-10-23 22:01             ` Ross Philipson
2018-10-23 23:41               ` Andrew Cooper
2018-10-25 12:23       ` Jan Beulich

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.