[kernel][hypervisor] Ensure singleton mutex is initialised.

Due to fxbug.dev/78920, singleton mutexes are not thread-safe on their
first use. In the case of the hypervisor, this can lead to the system's
EL2 state incorrectly being initialised multiple times, leading to stack
corruption of EL2.

We temporarily work around the issue until the underlying bug is fixed
by ensuring the mutex is initialised during system boot.

Bug: 78920
Bug: 77932
Change-Id: Ie46cde3ea2e06ae594c26be6470b4a6e0f4d6830
Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/544301
Reviewed-by: Abdulla Kamar <abdulla@google.com>
Reviewed-by: Adrian Danis <adanis@google.com>
Commit-Queue: David Greenaway <dgreenaway@google.com>
diff --git a/zircon/kernel/arch/arm64/hypervisor/BUILD.gn b/zircon/kernel/arch/arm64/hypervisor/BUILD.gn
index 024e8f3..515e79d 100644
--- a/zircon/kernel/arch/arm64/hypervisor/BUILD.gn
+++ b/zircon/kernel/arch/arm64/hypervisor/BUILD.gn
@@ -23,6 +23,7 @@
     "//zircon/kernel/dev/psci",
     "//zircon/kernel/dev/timer/arm_generic",
     "//zircon/kernel/hypervisor:headers",
+    "//zircon/kernel/lib/init",
     "//zircon/kernel/lib/ktl",
     "//zircon/kernel/lib/ktrace",
     "//zircon/system/ulib/affine",
diff --git a/zircon/kernel/arch/arm64/hypervisor/el2_cpu_state.cc b/zircon/kernel/arch/arm64/hypervisor/el2_cpu_state.cc
index f8a8e91..e4a35a8 100644
--- a/zircon/kernel/arch/arm64/hypervisor/el2_cpu_state.cc
+++ b/zircon/kernel/arch/arm64/hypervisor/el2_cpu_state.cc
@@ -12,6 +12,7 @@
 #include <kernel/cpu.h>
 #include <kernel/mutex.h>
 #include <ktl/move.h>
+#include <lk/init.h>
 #include <vm/physmap.h>
 #include <vm/pmm.h>
 
@@ -147,3 +148,13 @@
   }
   return ZX_OK;
 }
+
+LK_INIT_HOOK(
+    hypervisor_el2_state,
+    [](unsigned int) {
+      // Work around fxbug.dev/78920 by initialising the Mutex during boot.
+      //
+      // TODO(fxbug.dev/78920): Remove this once singleton mutexes are thread-safe on first use.
+      GuestMutex::Get();
+    },
+    LK_INIT_LEVEL_ARCH)