Skip to contentSkip to conversation

x86/boot: enable NMIs after traps init

PATCH In review Sergey Dyasli 5 participants 10 messages Oct 23, 2018 – Oct 25, 2018

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/boot: enable NMIs after traps init

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.

---
v2:
- Added enable_nmis() to __start_xen() for BSP
- Added comments as per Andrew's suggestion

CC Jan Beulich <[email protected]>
CC Andrew Cooper <[email protected]>
CC Wei Liu <[email protected]>
---
 xen/arch/x86/cpu/common.c | 3 +++
 xen/arch/x86/setup.c      | 3 +++
 2 files changed, 6 insertions(+)
xen/arch/x86/cpu/common.c +3 -0
index 057859ab14..2d02335fef 100644
@@ -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)
xen/arch/x86/setup.c +3 -0
index 93b79c7c0c..194388ef47 100644
@@ -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 )
     {
         /*
Signature · 7 lines
2.17.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v2 1/1
#1 lore raw
Quoted 11 lines · Sergey Dyasli

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.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Tested-by
#2 lore raw
Quoted 7 lines · Andrew Cooper
Quoted 5 lines · Sergey Dyasli

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");
Quoted 8 lines
Quoted 6 lines

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.

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.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 12 lines · Jason Andryuk
Quoted 11 lines · Andrew Cooper
Quoted 10 lines · Sergey Dyasli

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.

Quoted 8 lines
Quoted 2 lines
Quoted 1 line

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.

:(

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
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
Quoted 19 lines · Andrew Cooper
Quoted 17 lines · Jason Andryuk
Quoted 6 lines · Andrew Cooper
Quoted 5 lines · Sergey Dyasli

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");
Quoted 7 lines
Quoted 6 lines

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.

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

Quoted 8 lines
Quoted 3 lines

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

:(

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

Quoted 10 lines

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
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
Quoted 27 lines · Jason Andryuk
Quoted 18 lines · Andrew Cooper
Quoted 17 lines · Jason Andryuk
Quoted 6 lines · Andrew Cooper
Quoted 5 lines · Sergey Dyasli

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");
Quoted 7 lines
Quoted 6 lines

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.

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

Quoted 7 lines
Quoted 3 lines

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

:(

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

Quoted 7 lines

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

Quoted 14 lines

Regards,
Jason

Quoted 10 lines

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
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw
Quoted 36 lines · Ross Philipson
Quoted 27 lines · Jason Andryuk
Quoted 18 lines · Andrew Cooper
Quoted 17 lines · Jason Andryuk
Quoted 6 lines · Andrew Cooper
Quoted 5 lines · Sergey Dyasli

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");
Quoted 7 lines
Quoted 6 lines

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.

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

Quoted 7 lines
Quoted 3 lines

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

:(

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

Quoted 7 lines

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.

Quoted 6 lines

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
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw
Quoted 41 lines · Andrew Cooper
Quoted 40 lines · Ross Philipson
Quoted 27 lines · Jason Andryuk
Quoted 18 lines · Andrew Cooper
Quoted 17 lines · Jason Andryuk
Quoted 6 lines · Andrew Cooper
Quoted 5 lines · Sergey Dyasli

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");
Quoted 7 lines
Quoted 6 lines

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.

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

Quoted 7 lines
Quoted 3 lines

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

:(

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

Quoted 7 lines

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.

Quoted 6 lines
Quoted 1 line

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.

Quoted 14 lines

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.)

Quoted 5 lines

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.

Quoted 1 line

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.

Quoted 2 lines

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw
Quoted 45 lines · Ross Philipson
Quoted 44 lines · Andrew Cooper
Quoted 35 lines · Ross Philipson
Quoted 27 lines · Jason Andryuk
Quoted 18 lines · Andrew Cooper
Quoted 17 lines · Jason Andryuk
Quoted 6 lines · Andrew Cooper
Quoted 5 lines · Sergey Dyasli

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");
Quoted 7 lines
Quoted 6 lines

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.

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

Quoted 7 lines
Quoted 3 lines

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

:(

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

Quoted 7 lines

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.

Quoted 6 lines

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?

Quoted 8 lines
Quoted 3 lines

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? ;)

Quoted 17 lines
Quoted 17 lines

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.

Quoted 7 lines
Quoted 1 line

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.

Quoted 3 lines
Quoted 1 line

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
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#9 lore raw
In reply to Andrew Cooper
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 23.10.18 at 16:33, <[email protected]> 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.

Quoted 3 lines

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
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel