audk/OvmfPkg/Library
Laszlo Ersek 43df61878d OvmfPkg: enable SMM Monarch Election in PiSmmCpuDxeSmm
With "PcdCpuSmmEnableBspElection" set to FALSE, PiSmmCpuDxeSmm always
considers the processor with index 0 to be the SMM Monarch (a.k.a. the SMM
BSP). The SMM Monarch handles the SMI for real, while the other CPUs wait
in their SMM loops.

In a subsequent patch, we want to set "PcdCpuHotPlugSupport" to TRUE. For
that, PiCpuSmmEntry() [UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c] forces
us with an ASSERT() to set "PcdCpuSmmEnableBspElection" to TRUE as well.
To satisfy that expectation, we can simply remove our current
"PcdCpuSmmEnableBspElection|FALSE" setting, and inherit the default TRUE
value from "UefiCpuPkg.dec".

This causes "mSmmMpSyncData->BspIndex" in PiSmmCpuDxeSmm to lose its
static zero value (standing for CPU#0); instead it becomes (-1) in
general, and the SMM Monarch is elected anew on every SMI.

The default SMM Monarch Election is basically a race -- whichever CPU can
flip "mSmmMpSyncData->BspIndex" from (-1) to its own index, becomes king,
for handling that SMI. Refer to SmiRendezvous()
[UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c].

I consider this non-determinism less than ideal on QEMU/KVM; it would be
nice to stick with a "mostly permanent" SMM Monarch even with the Election
enabled. We can do that by implementing the PlatformSmmBspElection() API
in the SmmCpuPlatformHookLibQemu instance:

The IA32 APIC Base MSR can be read on each CPU concurrently, and it will
report the BSP bit as set only on the current Boot Service Processor. QEMU
marks CPU#0 as the BSP, by default.

Elect the current BSP, as reported by QEMU, for the SMM Monarch role.

(Note that the QEMU commit history is not entirely consistent on whether
QEMU/KVM may mark a CPU with nonzero index as the BSP:

- At tag v4.2.0, "target/i386/cpu.c" has a comment saying "We hard-wire
  the BSP to the first CPU". This comment goes back to commit 6cb2996cef5e
  ("x86: Extend validity of bsp_to_cpu", 2010-03-04).

- Compare commit 9cb11fd7539b ("target-i386: clear bsp bit when
  designating bsp", 2015-04-02) though, especially considering KVM.

Either way, this OvmfPkg patch is *not* dependent on CPU index 0; it just
takes the race on every SMI out of the game.)

One benefit of using a "mostly permanent" SMM Monarch / BSP is that we can
continue testing the SMM CPU synchronization by deterministically entering
the firmware on the BSP, vs. on an AP, from Linux guests:

$ time taskset -c 0 efibootmgr
$ time taskset -c 1 efibootmgr

(See
<https://github.com/tianocore/tianocore.github.io/wiki/Testing-SMM-with-QEMU,-KVM-and-libvirt#uefi-variable-access-test>.)

Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Igor Mammedov <imammedo@redhat.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Michael Kinney <michael.d.kinney@intel.com>
Cc: Philippe Mathieu-Daudé <philmd@redhat.com>
Suggested-by: Igor Mammedov <imammedo@redhat.com>
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=1512#c5
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20200226221156.29589-5-lersek@redhat.com>
Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Tested-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
2020-03-04 12:22:07 +00:00
..
AcpiTimerLib OvmfPkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:19 -07:00
BaseMemEncryptSevLib OvmfPkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:19 -07:00
BasePciCapLib OvmfPkg/BasePciCapLib: suppress invalid "nullptr deref" warning 2019-04-18 16:06:04 +02:00
BasePciCapPciSegmentLib OvmfPkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:19 -07:00
DxePciLibI440FxQ35 OvmfPkg/Qemu: Fix various typos 2020-02-10 22:30:07 +00:00
EmuVariableFvbLib OvmfPkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:19 -07:00
LoadLinuxLib OvmfPkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:19 -07:00
LockBoxLib OvmfPkg: strip trailing whitespace 2019-10-04 11:18:32 +01:00
NvVarsFileLib OvmfPkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:19 -07:00
PciHostBridgeLib OvmfPkg/Xen: Fix various typos 2020-02-10 22:30:07 +00:00
PlatformBmPrintScLib OvmfPkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:19 -07:00
PlatformBootManagerLib OvmfPkg/PlatformBootManagerLib: Don't update progress if Pcd is 0 2019-10-16 18:27:37 +02:00
PlatformDebugLibIoPort OvmfPkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:19 -07:00
PlatformFvbLibNull OvmfPkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:19 -07:00
PlatformHasIoMmuLib OvmfPkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:19 -07:00
PlatformSecureLib OvmfPkg: strip trailing whitespace 2019-10-04 11:18:32 +01:00
QemuBootOrderLib OvmfPkg/Qemu: Fix various typos 2020-02-10 22:30:07 +00:00
QemuFwCfgLib OvmfPkg/Qemu: Fix various typos 2020-02-10 22:30:07 +00:00
QemuFwCfgS3Lib OvmfPkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:19 -07:00
ResetSystemLib OvmfPkg: remove EnterS3WithImmediateWake () from ResetSystemLib 2020-01-10 07:00:51 +00:00
SerializeVariablesLib OvmfPkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:19 -07:00
SmbiosVersionLib OvmfPkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:19 -07:00
SmmCpuFeaturesLib OvmfPkg/SEV: don't manage the lifecycle of the SMRAM at the default SMBASE 2020-02-05 12:59:32 +00:00
SmmCpuPlatformHookLibQemu OvmfPkg: enable SMM Monarch Election in PiSmmCpuDxeSmm 2020-03-04 12:22:07 +00:00
Tcg2PhysicalPresenceLibNull OvmfPkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:19 -07:00
Tcg2PhysicalPresenceLibQemu OvmfPkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:19 -07:00
TlsAuthConfigLib OvmfPkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:19 -07:00
UefiPciCapPciIoLib OvmfPkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:19 -07:00
VirtioLib OvmfPkg/Virtio: Fix few typos 2020-02-10 22:30:07 +00:00
VirtioMmioDeviceLib OvmfPkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:19 -07:00
XenConsoleSerialPortLib OvmfPkg/Xen: Fix various typos 2020-02-10 22:30:07 +00:00
XenHypercallLib OvmfPkg/XenHypercallLib: Enable it in PEIM 2019-08-21 18:03:49 +02:00
XenIoMmioLib OvmfPkg: strip trailing whitespace 2019-10-04 11:18:32 +01:00
XenPlatformLib OvmfPkg/XenPlatformLib: Cache result for XenDetected 2019-08-21 18:03:49 +02:00
XenRealTimeClockLib OvmfPkg: Move XenRealTimeClockLib from ArmVirtPkg 2019-08-21 18:03:50 +02:00