diff options
author | Daniel P. Berrangé <berrange@redhat.com> | 2024-08-02 17:26:00 +0200 |
---|---|---|
committer | Daniel P. Berrangé <berrange@redhat.com> | 2024-08-02 17:26:00 +0200 |
commit | 1c4bd7adcc281af2a2dd40867f64f2ac54a43c7a (patch) | |
tree | cc1cf594f87c38f6407a54d3ba0712840f7c4cd6 /src/basic/confidential-virt.c | |
parent | test: don't use /skipped for subtests (diff) | |
download | systemd-1c4bd7adcc281af2a2dd40867f64f2ac54a43c7a.tar.xz systemd-1c4bd7adcc281af2a2dd40867f64f2ac54a43c7a.zip |
confidential-virt: split caching of CVM detection into separate method
We have different impls of detect_confidential_virtualization per
architecture. The detection is cached in the x86_64 impl, and as we
add support for more targets, we want to use caching for all. It thus
makes sense to split caching out into an architecture independent
method.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
Diffstat (limited to 'src/basic/confidential-virt.c')
-rw-r--r-- | src/basic/confidential-virt.c | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/src/basic/confidential-virt.c b/src/basic/confidential-virt.c index 8a88a3eb83..0e05ecffbf 100644 --- a/src/basic/confidential-virt.c +++ b/src/basic/confidential-virt.c @@ -194,34 +194,37 @@ static bool detect_hypervisor(void) { return is_hv; } -ConfidentialVirtualization detect_confidential_virtualization(void) { - static thread_local ConfidentialVirtualization cached_found = _CONFIDENTIAL_VIRTUALIZATION_INVALID; +static ConfidentialVirtualization detect_confidential_virtualization_impl(void) { char sig[13] = {}; - ConfidentialVirtualization cv = CONFIDENTIAL_VIRTUALIZATION_NONE; - - if (cached_found >= 0) - return cached_found; /* Skip everything on bare metal */ if (detect_hypervisor()) { cpuid_leaf(0, sig, true); if (memcmp(sig, CPUID_SIG_AMD, sizeof(sig)) == 0) - cv = detect_sev(); + return detect_sev(); else if (memcmp(sig, CPUID_SIG_INTEL, sizeof(sig)) == 0) - cv = detect_tdx(); + return detect_tdx(); } - cached_found = cv; - return cv; + return CONFIDENTIAL_VIRTUALIZATION_NONE; } #else /* ! x86_64 */ -ConfidentialVirtualization detect_confidential_virtualization(void) { +static ConfidentialVirtualization detect_confidential_virtualization_impl(void) { log_debug("No confidential virtualization detection on this architecture"); return CONFIDENTIAL_VIRTUALIZATION_NONE; } #endif /* ! x86_64 */ +ConfidentialVirtualization detect_confidential_virtualization(void) { + static thread_local ConfidentialVirtualization cached_found = _CONFIDENTIAL_VIRTUALIZATION_INVALID; + + if (cached_found == _CONFIDENTIAL_VIRTUALIZATION_INVALID) + cached_found = detect_confidential_virtualization_impl(); + + return cached_found; +} + static const char *const confidential_virtualization_table[_CONFIDENTIAL_VIRTUALIZATION_MAX] = { [CONFIDENTIAL_VIRTUALIZATION_NONE] = "none", [CONFIDENTIAL_VIRTUALIZATION_SEV] = "sev", |