audk/OvmfPkg/CpuHotplugSmm
Laszlo Ersek cbccf99592 OvmfPkg/CpuHotplugSmm: fix CPU hotplug race just after SMI broadcast
The "virsh setvcpus" (plural) command may hot-plug several VCPUs in quick
succession -- it means a series of "device_add" QEMU monitor commands,
back-to-back.

If a "device_add" occurs *just after* ACPI raises the broadcast SMI, then:

- the CPU_FOREACH() loop in QEMU's ich9_apm_ctrl_changed() cannot make the
  SMI pending for the new CPU -- at that time, the new CPU doesn't even
  exist yet,

- OVMF will find the new CPU however (in the CPU hotplug register block),
  in QemuCpuhpCollectApicIds().

As a result, when the firmware sends an INIT-SIPI-SIPI to the new CPU in
SmbaseRelocate(), expecting it to boot into SMM (due to the pending SMI),
the new CPU instead boots straight into the post-RSM (normal mode) "pen",
skipping its initial SMI handler.

The CPU halts nicely in the pen, but its SMBASE is never relocated, and
the SMRAM message exchange with the BSP falls apart -- the BSP gets stuck
in the following loop:

  //
  // Wait until the hot-added CPU is just about to execute RSM.
  //
  while (Context->AboutToLeaveSmm == 0) {
    CpuPause ();
  }

because the new CPU's initial SMI handler never sets the flag to nonzero.

Fix this by sending a directed SMI to the new CPU just before sending it
the INIT-SIPI-SIPI. The various scenarios are documented in the code --
the cases affected by the patch are documented under point (2).

Note that this is not considered a security patch, as for a malicious
guest OS, the issue is not exploitable -- the symptom is a hang on the
BSP, in the above-noted loop in SmbaseRelocate(). Instead, the patch fixes
behavior for a benign guest OS.

Cc: Ard Biesheuvel <ard.biesheuvel@arm.com>
Cc: Igor Mammedov <imammedo@redhat.com>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Philippe Mathieu-Daudé <philmd@redhat.com>
Fixes: 51a6fb4118
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=2929
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20200826222129.25798-3-lersek@redhat.com>
Reviewed-by: Ard Biesheuvel <ard.biesheuvel@arm.com>
2020-08-27 18:01:00 +00:00
..
ApicId.h OvmfPkg/CpuHotplugSmm: add function for collecting CPUs with events 2020-03-04 12:22:07 +00:00
CpuHotplug.c OvmfPkg/CpuHotplugSmm: fix CPU hotplug race just before SMI broadcast 2020-08-27 18:01:00 +00:00
CpuHotplugSmm.inf OvmfPkg/CpuHotplugSmm: introduce First SMI Handler for hot-added CPUs 2020-03-04 12:22:07 +00:00
FirstSmiHandler.nasm OvmfPkg/CpuHotplugSmm: introduce First SMI Handler for hot-added CPUs 2020-03-04 12:22:07 +00:00
FirstSmiHandlerContext.h OvmfPkg/CpuHotplugSmm: introduce First SMI Handler for hot-added CPUs 2020-03-04 12:22:07 +00:00
PostSmmPen.nasm OvmfPkg/CpuHotplugSmm: introduce Post-SMM Pen for hot-added CPUs 2020-03-04 12:22:07 +00:00
QemuCpuhp.c OvmfPkg/CpuHotplugSmm: add function for collecting CPUs with events 2020-03-04 12:22:07 +00:00
QemuCpuhp.h OvmfPkg/CpuHotplugSmm: add function for collecting CPUs with events 2020-03-04 12:22:07 +00:00
Smbase.c OvmfPkg/CpuHotplugSmm: fix CPU hotplug race just after SMI broadcast 2020-08-27 18:01:00 +00:00
Smbase.h OvmfPkg/CpuHotplugSmm: introduce First SMI Handler for hot-added CPUs 2020-03-04 12:22:07 +00:00