]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg: Add library to handle TPL from within nested interrupt handlers
authorMichael Brown <mcb30@ipxe.org>
Fri, 9 Dec 2022 10:20:24 +0000 (10:20 +0000)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Fri, 23 Dec 2022 14:44:48 +0000 (14:44 +0000)
UEFI requires us to support nested interrupts, but provides no way for
an interrupt handler to call RestoreTPL() without implicitly
re-enabling interrupts.  In a virtual machine, it is possible for a
large burst of interrupts to arrive.  We must prevent such a burst
from leading to stack underrun, while continuing to allow nested
interrupts to occur.

This can be achieved by allowing, when provably safe to do so, an
inner interrupt handler to return from the interrupt without restoring
the TPL and with interrupts remaining disabled after IRET, with the
deferred call to RestoreTPL() then being issued from the outer
interrupt handler.  This is necessarily messy and involves direct
manipulation of the interrupt stack frame, and so should not be
implemented as open-coded logic within each interrupt handler.

Add the Nested Interrupt TPL Library (NestedInterruptTplLib) to
provide helper functions that can be used by nested interrupt handlers
in place of RaiseTPL()/RestoreTPL().

Example call tree for a timer interrupt occurring at TPL_APPLICATION
with a nested timer interrupt that makes its own call to RestoreTPL():

  outer TimerInterruptHandler()
    InterruptedTPL == TPL_APPLICATION
    ...
    IsrState->InProgressRestoreTPL = TPL_APPLICATION;
    gBS->RestoreTPL (TPL_APPLICATION);
      EnableInterrupts();
      dispatch a TPL_CALLBACK event
        gEfiCurrentTpl = TPL_CALLBACK;
        nested timer interrupt occurs
        inner TimerInterruptHandler()
          InterruptedTPL == TPL_CALLBACK
          ...
          IsrState->InProgressRestoreTPL = TPL_CALLBACK;
          gBS->RestoreTPL (TPL_CALLBACK);
            EnableInterrupts();
          DisableInterrupts();
          IsrState->InProgressRestoreTPL = TPL_APPLICATION;
          IRET re-enables interrupts
      ... finish dispatching TPL_CALLBACK events ...
      gEfiCurrentTpl = TPL_APPLICATION;
    DisableInterrupts();
    IsrState->InProgressRestoreTPL = 0;
    sees IsrState->DeferredRestoreTPL == FALSE and returns
    IRET re-enables interrupts

Example call tree for a timer interrupt occurring at TPL_APPLICATION
with a nested timer interrupt that defers its call to RestoreTPL() to
the outer instance of the interrupt handler:

  outer TimerInterruptHandler()
    InterruptedTPL == TPL_APPLICATION
    ...
    IsrState->InProgressRestoreTPL = TPL_APPLICATION;
    gBS->RestoreTPL (TPL_APPLICATION);
      EnableInterrupts();
      dispatch a TPL_CALLBACK event
      ... finish dispatching TPL_CALLBACK events ...
      gEfiCurrentTpl = TPL_APPLICATION;
      nested timer interrupt occurs
      inner TimerInterruptHandler()
        InterruptedTPL == TPL_APPLICATION;
        ...
        sees InterruptedTPL == IsrState->InProgressRestoreTPL
        IsrState->DeferredRestoreTPL = TRUE;
        DisableInterruptsOnIret();
        IRET returns without re-enabling interrupts
    DisableInterrupts();
    IsrState->InProgressRestoreTPL = 0;
    sees IsrState->DeferredRestoreTPL == TRUE and loops
    IsrState->InProgressRestoreTPL = TPL_APPLICATION;
    gBS->RestoreTPL (TPL_APPLICATION);  <-- deferred call
      EnableInterrupts();
    DisableInterrupts();
    IsrState->InProgressRestoreTPL = 0;
    sees IsrState->DeferredRestoreTPL == FALSE and returns
    IRET re-enables interrupts

Cc: Paolo Bonzini <pbonzini@redhat.com>
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=4162
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Acked-by: Laszlo Ersek <lersek@redhat.com>
OvmfPkg/Include/Library/NestedInterruptTplLib.h [new file with mode: 0644]
OvmfPkg/Library/NestedInterruptTplLib/Iret.c [new file with mode: 0644]
OvmfPkg/Library/NestedInterruptTplLib/Iret.h [new file with mode: 0644]
OvmfPkg/Library/NestedInterruptTplLib/NestedInterruptTplLib.inf [new file with mode: 0644]
OvmfPkg/Library/NestedInterruptTplLib/Tpl.c [new file with mode: 0644]
OvmfPkg/OvmfPkg.dec

diff --git a/OvmfPkg/Include/Library/NestedInterruptTplLib.h b/OvmfPkg/Include/Library/NestedInterruptTplLib.h
new file mode 100644 (file)
index 0000000..0ead6e4
--- /dev/null
@@ -0,0 +1,87 @@
+/** @file\r
+  Handle raising and lowering TPL from within nested interrupt handlers.\r
+\r
+  Allows interrupt handlers to safely raise and lower the TPL to\r
+  dispatch event notifications, correctly allowing for nested\r
+  interrupts to occur without risking stack exhaustion.\r
+\r
+  Copyright (C) 2022, Fen Systems Ltd.\r
+\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+**/\r
+\r
+#ifndef __NESTED_INTERRUPT_TPL_LIB__\r
+#define __NESTED_INTERRUPT_TPL_LIB__\r
+\r
+#include <Uefi/UefiBaseType.h>\r
+#include <Uefi/UefiSpec.h>\r
+#include <Protocol/DebugSupport.h>\r
+\r
+///\r
+/// State shared between all invocations of a nested interrupt handler.\r
+///\r
+typedef struct {\r
+  ///\r
+  /// Highest TPL that is currently the target of a call to\r
+  /// RestoreTPL() by an instance of this interrupt handler.\r
+  ///\r
+  EFI_TPL    InProgressRestoreTPL;\r
+  ///\r
+  /// Flag used to defer a call to RestoreTPL() from an inner instance\r
+  /// of the interrupt handler to an outer instance of the same\r
+  /// interrupt handler.\r
+  ///\r
+  BOOLEAN    DeferredRestoreTPL;\r
+} NESTED_INTERRUPT_STATE;\r
+\r
+/**\r
+  Raise the task priority level to TPL_HIGH_LEVEL.\r
+\r
+  @param  None.\r
+\r
+  @return The task priority level at which the interrupt occurred.\r
+**/\r
+EFI_TPL\r
+EFIAPI\r
+NestedInterruptRaiseTPL (\r
+  VOID\r
+  );\r
+\r
+/**\r
+  Lower the task priority back to the value at which the interrupt\r
+  occurred.\r
+\r
+  This is unfortunately messy.  UEFI requires us to support nested\r
+  interrupts, but provides no way for an interrupt handler to call\r
+  RestoreTPL() without implicitly re-enabling interrupts.  In a\r
+  virtual machine, it is possible for a large burst of interrupts to\r
+  arrive.  We must prevent such a burst from leading to stack\r
+  exhaustion, while continuing to allow nested interrupts to occur.\r
+\r
+  Since nested interrupts are permitted, an interrupt handler may be\r
+  invoked as an inner interrupt handler while an outer instance of the\r
+  same interrupt handler is still inside its call to RestoreTPL().\r
+\r
+  To avoid stack exhaustion, this call may therefore (when provably\r
+  safe to do so) defer the actual TPL lowering to be performed by an\r
+  outer instance of the same interrupt handler.\r
+\r
+  @param InterruptedTPL        The task priority level at which the interrupt\r
+                               occurred, as previously returned from\r
+                               NestedInterruptRaiseTPL().\r
+\r
+  @param SystemContext         A pointer to the system context when the\r
+                               interrupt occurred.\r
+\r
+  @param IsrState              A pointer to the state shared between all\r
+                               invocations of the nested interrupt handler.\r
+**/\r
+VOID\r
+EFIAPI\r
+NestedInterruptRestoreTPL (\r
+  IN EFI_TPL                     InterruptedTPL,\r
+  IN OUT EFI_SYSTEM_CONTEXT      SystemContext,\r
+  IN OUT NESTED_INTERRUPT_STATE  *IsrState\r
+  );\r
+\r
+#endif // __NESTED_INTERRUPT_TPL_LIB__\r
diff --git a/OvmfPkg/Library/NestedInterruptTplLib/Iret.c b/OvmfPkg/Library/NestedInterruptTplLib/Iret.c
new file mode 100644 (file)
index 0000000..f6b2c51
--- /dev/null
@@ -0,0 +1,62 @@
+/** @file\r
+  Force interrupt handler to return with interrupts still disabled.\r
+\r
+  Copyright (C) 2022, Fen Systems Ltd.\r
+\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+**/\r
+\r
+#include <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
+\r
+#include "Iret.h"\r
+\r
+/**\r
+  Force interrupt handler to return with interrupts still disabled.\r
+\r
+  @param SystemContext         A pointer to the system context when the\r
+                               interrupt occurred.\r
+**/\r
+VOID\r
+DisableInterruptsOnIret (\r
+  IN OUT EFI_SYSTEM_CONTEXT  SystemContext\r
+  )\r
+{\r
+ #if defined (MDE_CPU_X64)\r
+\r
+  IA32_EFLAGS32  Rflags;\r
+\r
+  //\r
+  // Get flags from system context.\r
+  //\r
+  Rflags.UintN = SystemContext.SystemContextX64->Rflags;\r
+  ASSERT (Rflags.Bits.IF);\r
+\r
+  //\r
+  // Clear interrupts-enabled flag.\r
+  //\r
+  Rflags.Bits.IF                         = 0;\r
+  SystemContext.SystemContextX64->Rflags = Rflags.UintN;\r
+\r
+ #elif defined (MDE_CPU_IA32)\r
+\r
+  IA32_EFLAGS32  Eflags;\r
+\r
+  //\r
+  // Get flags from system context.\r
+  //\r
+  Eflags.UintN = SystemContext.SystemContextIa32->Eflags;\r
+  ASSERT (Eflags.Bits.IF);\r
+\r
+  //\r
+  // Clear interrupts-enabled flag.\r
+  //\r
+  Eflags.Bits.IF                          = 0;\r
+  SystemContext.SystemContextIa32->Eflags = Eflags.UintN;\r
+\r
+ #else\r
+\r
+  #error "Unsupported CPU"\r
+\r
+ #endif\r
+}\r
diff --git a/OvmfPkg/Library/NestedInterruptTplLib/Iret.h b/OvmfPkg/Library/NestedInterruptTplLib/Iret.h
new file mode 100644 (file)
index 0000000..278c1e2
--- /dev/null
@@ -0,0 +1,19 @@
+/** @file\r
+  Force interrupt handler to return with interrupts still disabled.\r
+\r
+  Copyright (C) 2022, Fen Systems Ltd.\r
+\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+**/\r
+\r
+#ifndef _IRET_H_\r
+#define _IRET_H_\r
+\r
+#include <Protocol/DebugSupport.h>\r
+\r
+VOID\r
+DisableInterruptsOnIret (\r
+  IN OUT EFI_SYSTEM_CONTEXT  SystemContext\r
+  );\r
+\r
+#endif // _IRET_H_\r
diff --git a/OvmfPkg/Library/NestedInterruptTplLib/NestedInterruptTplLib.inf b/OvmfPkg/Library/NestedInterruptTplLib/NestedInterruptTplLib.inf
new file mode 100644 (file)
index 0000000..5eafb41
--- /dev/null
@@ -0,0 +1,35 @@
+## @file\r
+# Handle raising and lowering TPL from within nested interrupt handlers.\r
+#\r
+# Allows interrupt handlers to safely raise and lower the TPL to\r
+# dispatch event notifications, correctly allowing for nested\r
+# interrupts to occur without risking stack exhaustion.\r
+#\r
+# Copyright (C) 2022, Fen Systems Ltd.\r
+#\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION    = 1.29\r
+  BASE_NAME      = NestedInterruptTplLib\r
+  FILE_GUID      = 8df39823-2f9e-4ef2-b971-243b44c32c67\r
+  MODULE_TYPE    = DXE_DRIVER\r
+  VERSION_STRING = 1.0\r
+  LIBRARY_CLASS  = NestedInterruptTplLib|DXE_DRIVER\r
+\r
+[Sources]\r
+  Tpl.c\r
+  Iret.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  OvmfPkg/OvmfPkg.dec\r
+\r
+[LibraryClasses]\r
+  BaseLib\r
+  DebugLib\r
+  UefiBootServicesTableLib\r
+\r
+[Depex.common.DXE_DRIVER]\r
+  TRUE\r
diff --git a/OvmfPkg/Library/NestedInterruptTplLib/Tpl.c b/OvmfPkg/Library/NestedInterruptTplLib/Tpl.c
new file mode 100644 (file)
index 0000000..e19d988
--- /dev/null
@@ -0,0 +1,216 @@
+/** @file\r
+  Handle raising and lowering TPL from within nested interrupt handlers.\r
+\r
+  Allows interrupt handlers to safely raise and lower the TPL to\r
+  dispatch event notifications, correctly allowing for nested\r
+  interrupts to occur without risking stack exhaustion.\r
+\r
+  Copyright (C) 2022, Fen Systems Ltd.\r
+\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+**/\r
+\r
+#include <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/NestedInterruptTplLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+\r
+#include "Iret.h"\r
+\r
+/**\r
+  Raise the task priority level to TPL_HIGH_LEVEL.\r
+\r
+  @param  None.\r
+\r
+  @return The task priority level at which the interrupt occurred.\r
+**/\r
+EFI_TPL\r
+EFIAPI\r
+NestedInterruptRaiseTPL (\r
+  VOID\r
+  )\r
+{\r
+  EFI_TPL  InterruptedTPL;\r
+\r
+  //\r
+  // Raise TPL and assert that we were called from within an interrupt\r
+  // handler (i.e. with TPL below TPL_HIGH_LEVEL but with interrupts\r
+  // disabled).\r
+  //\r
+  ASSERT (GetInterruptState () == FALSE);\r
+  InterruptedTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
+  ASSERT (InterruptedTPL < TPL_HIGH_LEVEL);\r
+\r
+  return InterruptedTPL;\r
+}\r
+\r
+/**\r
+  Lower the task priority back to the value at which the interrupt\r
+  occurred.\r
+\r
+  This is unfortunately messy.  UEFI requires us to support nested\r
+  interrupts, but provides no way for an interrupt handler to call\r
+  RestoreTPL() without implicitly re-enabling interrupts.  In a\r
+  virtual machine, it is possible for a large burst of interrupts to\r
+  arrive.  We must prevent such a burst from leading to stack\r
+  exhaustion, while continuing to allow nested interrupts to occur.\r
+\r
+  Since nested interrupts are permitted, an interrupt handler may be\r
+  invoked as an inner interrupt handler while an outer instance of the\r
+  same interrupt handler is still inside its call to RestoreTPL().\r
+\r
+  To avoid stack exhaustion, this call may therefore (when provably\r
+  safe to do so) defer the actual TPL lowering to be performed by an\r
+  outer instance of the same interrupt handler.\r
+\r
+  @param InterruptedTPL        The task priority level at which the interrupt\r
+                               occurred, as previously returned from\r
+                               NestedInterruptRaiseTPL().\r
+\r
+  @param SystemContext         A pointer to the system context when the\r
+                               interrupt occurred.\r
+\r
+  @param IsrState              A pointer to the state shared between all\r
+                               invocations of the nested interrupt handler.\r
+**/\r
+VOID\r
+EFIAPI\r
+NestedInterruptRestoreTPL (\r
+  IN EFI_TPL                     InterruptedTPL,\r
+  IN OUT EFI_SYSTEM_CONTEXT      SystemContext,\r
+  IN OUT NESTED_INTERRUPT_STATE  *IsrState\r
+  )\r
+{\r
+  EFI_TPL  SavedInProgressRestoreTPL;\r
+  BOOLEAN  DeferredRestoreTPL;\r
+\r
+  //\r
+  // If the TPL at which this interrupt occurred is equal to that of\r
+  // the in-progress RestoreTPL() for an outer instance of the same\r
+  // interrupt handler, then that outer handler's call to RestoreTPL()\r
+  // must have finished dispatching all event notifications.  This\r
+  // interrupt must therefore have occurred at the point that the\r
+  // outer handler's call to RestoreTPL() had finished and was about\r
+  // to return to the outer handler.\r
+  //\r
+  // If we were to call RestoreTPL() at this point, then we would open\r
+  // up the possibility for unlimited stack consumption in the event\r
+  // of an interrupt storm.  We therefore cannot safely call\r
+  // RestoreTPL() from within this stack frame (i.e. from within this\r
+  // instance of the interrupt handler).\r
+  //\r
+  // Instead, we arrange to return from this interrupt with the TPL\r
+  // still at TPL_HIGH_LEVEL and with interrupts disabled, and to\r
+  // defer our call to RestoreTPL() to the in-progress outer instance\r
+  // of the same interrupt handler.\r
+  //\r
+  if (InterruptedTPL == IsrState->InProgressRestoreTPL) {\r
+    //\r
+    // Trigger outer instance of this interrupt handler to perform the\r
+    // RestoreTPL() call that we cannot issue at this point without\r
+    // risking stack exhaustion.\r
+    //\r
+    ASSERT (IsrState->DeferredRestoreTPL == FALSE);\r
+    IsrState->DeferredRestoreTPL = TRUE;\r
+\r
+    //\r
+    // DEFERRAL INVOCATION POINT\r
+    //\r
+    // Return from this interrupt handler with interrupts still\r
+    // disabled (by clearing the "interrupts-enabled" bit in the CPU\r
+    // flags that will be restored by the IRET or equivalent\r
+    // instruction).\r
+    //\r
+    // This ensures that no further interrupts may occur before\r
+    // control reaches the outer interrupt handler's RestoreTPL() loop\r
+    // at the point marked "DEFERRAL RETURN POINT" (see below).\r
+    //\r
+    DisableInterruptsOnIret (SystemContext);\r
+    return;\r
+  }\r
+\r
+  //\r
+  // If the TPL at which this interrupt occurred is higher than that\r
+  // of the in-progress RestoreTPL() for an outer instance of the same\r
+  // interrupt handler, then that outer handler's call to RestoreTPL()\r
+  // must still be dispatching event notifications.\r
+  //\r
+  // We must therefore call RestoreTPL() at this point to allow more\r
+  // event notifications to be dispatched, since those event\r
+  // notification callback functions may themselves be waiting upon\r
+  // other events.\r
+  //\r
+  // We cannot avoid creating a new stack frame for this call to\r
+  // RestoreTPL(), but the total number of such stack frames is\r
+  // intrinsically limited by the number of distinct TPLs.\r
+  //\r
+  // We may need to issue the call to RestoreTPL() more than once, if\r
+  // an inner instance of the same interrupt handler needs to defer\r
+  // its RestoreTPL() call to be performed from within this stack\r
+  // frame (see above).\r
+  //\r
+  while (TRUE) {\r
+    //\r
+    // Check shared state loop invariants.\r
+    //\r
+    ASSERT (IsrState->InProgressRestoreTPL < InterruptedTPL);\r
+    ASSERT (IsrState->DeferredRestoreTPL == FALSE);\r
+\r
+    //\r
+    // Record the in-progress RestoreTPL() value in the shared state\r
+    // where it will be visible to an inner instance of the same\r
+    // interrupt handler, in case a nested interrupt occurs during our\r
+    // call to RestoreTPL().\r
+    //\r
+    SavedInProgressRestoreTPL      = IsrState->InProgressRestoreTPL;\r
+    IsrState->InProgressRestoreTPL = InterruptedTPL;\r
+\r
+    //\r
+    // Call RestoreTPL() to allow event notifications to be\r
+    // dispatched.  This will implicitly re-enable interrupts.\r
+    //\r
+    gBS->RestoreTPL (InterruptedTPL);\r
+\r
+    //\r
+    // Re-disable interrupts after the call to RestoreTPL() to ensure\r
+    // that we have exclusive access to the shared state.\r
+    //\r
+    DisableInterrupts ();\r
+\r
+    //\r
+    // DEFERRAL RETURN POINT\r
+    //\r
+    // An inner instance of the same interrupt handler may have chosen\r
+    // to defer its RestoreTPL() call to be performed from within this\r
+    // stack frame.  If so, it is guaranteed that no further event\r
+    // notifications or interrupts have been processed between the\r
+    // DEFERRAL INVOCATION POINT (see above) and this DEFERRAL RETURN\r
+    // POINT.\r
+    //\r
+\r
+    //\r
+    // Restore the locally saved in-progress RestoreTPL() value in the\r
+    // shared state, now that our call to RestoreTPL() has returned\r
+    // and is therefore no longer in progress.\r
+    //\r
+    ASSERT (IsrState->InProgressRestoreTPL == InterruptedTPL);\r
+    IsrState->InProgressRestoreTPL = SavedInProgressRestoreTPL;\r
+\r
+    //\r
+    // Check (and clear) the shared state to see if an inner instance\r
+    // of the same interrupt handler deferred its call to\r
+    // RestoreTPL().\r
+    //\r
+    DeferredRestoreTPL           = IsrState->DeferredRestoreTPL;\r
+    IsrState->DeferredRestoreTPL = FALSE;\r
+\r
+    //\r
+    // If no inner interrupt handler deferred its call to\r
+    // RestoreTPL(), then the TPL has been successfully restored and\r
+    // we may return from the interrupt handler.\r
+    //\r
+    if (DeferredRestoreTPL == FALSE) {\r
+      return;\r
+    }\r
+  }\r
+}\r
index a350bb8f84b909f7c93128e804bab98b7737b471..693925a1dc7aa917af1ee8b0ccd174e469dee4ae 100644 (file)
   #\r
   MemEncryptTdxLib|Include/Library/MemEncryptTdxLib.h\r
 \r
+  ##  @libraryclass  Handle TPL changes within nested interrupt handlers\r
+  #\r
+  NestedInterruptTplLib|Include/Library/NestedInterruptTplLib.h\r
+\r
   ##  @libraryclass  Save and restore variables using a file\r
   #\r
   NvVarsFileLib|Include/Library/NvVarsFileLib.h\r