| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#if defined(__i386__) || defined(__x86_64__) |
| 4 |
# include <cpuid.h> |
| 5 |
#endif |
| 6 |
|
| 7 |
#include "confidential-virt.h" |
| 8 |
#include "device-path-util.h" |
| 9 |
#include "drivers.h" |
| 10 |
#include "efi-string.h" |
| 11 |
#include "efi-efivars.h" |
| 12 |
#include "proto/device-path.h" |
| 13 |
#include "smbios.h" |
| 14 |
#include "util.h" |
| 15 |
#include "vmm.h" |
| 16 |
|
| 17 |
#define QEMU_KERNEL_LOADER_FS_MEDIA_GUID \ |
| 18 |
{ 0x1428f772, 0xb64a, 0x441e, { 0xb8, 0xc3, 0x9e, 0xbd, 0xd7, 0xf8, 0x93, 0xc7 } } |
| 19 |
|
| 20 |
#define VMM_BOOT_ORDER_GUID \ |
| 21 |
{ 0x668f4529, 0x63d0, 0x4bb5, { 0xb6, 0x5d, 0x6f, 0xbb, 0x9d, 0x36, 0xa4, 0x4a } } |
| 22 |
|
| 23 |
/* detect direct boot */ |
| 24 |
bool is_direct_boot(EFI_HANDLE device) { |
| 25 |
EFI_STATUS err; |
| 26 |
VENDOR_DEVICE_PATH *dp; |
| 27 |
|
| 28 |
err = BS->HandleProtocol(device, MAKE_GUID_PTR(EFI_DEVICE_PATH_PROTOCOL), (void **) &dp); |
| 29 |
if (err != EFI_SUCCESS) |
| 30 |
return false; |
| 31 |
|
| 32 |
/* 'qemu -kernel systemd-bootx64.efi' */ |
| 33 |
if (dp->Header.Type == MEDIA_DEVICE_PATH && |
| 34 |
dp->Header.SubType == MEDIA_VENDOR_DP && |
| 35 |
memcmp(&dp->Guid, MAKE_GUID_PTR(QEMU_KERNEL_LOADER_FS_MEDIA), sizeof(EFI_GUID)) == 0) |
| 36 |
return true; |
| 37 |
|
| 38 |
/* loaded from firmware volume (sd-boot added to ovmf) */ |
| 39 |
if (dp->Header.Type == MEDIA_DEVICE_PATH && |
| 40 |
dp->Header.SubType == MEDIA_PIWG_FW_VOL_DP) |
| 41 |
return true; |
| 42 |
|
| 43 |
return false; |
| 44 |
} |
| 45 |
|
| 46 |
/* |
| 47 |
* Try find ESP when not loaded from ESP |
| 48 |
* |
| 49 |
* Inspect all filesystems known to the firmware, try find the ESP. In case VMMBootOrderNNNN variables are |
| 50 |
* present they are used to inspect the filesystems in the specified order. When nothing was found or the |
| 51 |
* variables are not present the function will do one final search pass over all filesystems. |
| 52 |
* |
| 53 |
* Recent OVMF builds store the qemu boot order (as specified using the bootindex property on the qemu |
| 54 |
* command line) in VMMBootOrderNNNN. The variables contain a device path. |
| 55 |
* |
| 56 |
* Example qemu command line: |
| 57 |
* qemu -virtio-scsi-pci,addr=14.0 -device scsi-cd,scsi-id=4,bootindex=1 |
| 58 |
* |
| 59 |
* Resulting variable: |
| 60 |
* VMMBootOrder0000 = PciRoot(0x0)/Pci(0x14,0x0)/Scsi(0x4,0x0) |
| 61 |
*/ |
| 62 |
EFI_STATUS vmm_open(EFI_HANDLE *ret_vmm_dev, EFI_FILE **ret_vmm_dir) { |
| 63 |
_cleanup_free_ EFI_HANDLE *handles = NULL; |
| 64 |
size_t n_handles; |
| 65 |
EFI_STATUS err, dp_err; |
| 66 |
|
| 67 |
assert(ret_vmm_dev); |
| 68 |
assert(ret_vmm_dir); |
| 69 |
|
| 70 |
/* Make sure all file systems have been initialized. Only do this in VMs as this is slow |
| 71 |
* on some real firmwares. */ |
| 72 |
(void) reconnect_all_drivers(); |
| 73 |
|
| 74 |
/* find all file system handles */ |
| 75 |
err = BS->LocateHandleBuffer( |
| 76 |
ByProtocol, MAKE_GUID_PTR(EFI_SIMPLE_FILE_SYSTEM_PROTOCOL), NULL, &n_handles, &handles); |
| 77 |
if (err != EFI_SUCCESS) |
| 78 |
return err; |
| 79 |
|
| 80 |
for (size_t order = 0;; order++) { |
| 81 |
_cleanup_free_ EFI_DEVICE_PATH *dp = NULL; |
| 82 |
|
| 83 |
_cleanup_free_ char16_t *order_str = xasprintf("VMMBootOrder%04zx", order); |
| 84 |
dp_err = efivar_get_raw(MAKE_GUID_PTR(VMM_BOOT_ORDER), order_str, (void**) &dp, NULL); |
| 85 |
|
| 86 |
for (size_t i = 0; i < n_handles; i++) { |
| 87 |
_cleanup_file_close_ EFI_FILE *root_dir = NULL, *efi_dir = NULL; |
| 88 |
EFI_DEVICE_PATH *fs; |
| 89 |
|
| 90 |
err = BS->HandleProtocol( |
| 91 |
handles[i], MAKE_GUID_PTR(EFI_DEVICE_PATH_PROTOCOL), (void **) &fs); |
| 92 |
if (err != EFI_SUCCESS) |
| 93 |
return err; |
| 94 |
|
| 95 |
/* check against VMMBootOrderNNNN (if set) */ |
| 96 |
if (dp_err == EFI_SUCCESS && !device_path_startswith(fs, dp)) |
| 97 |
continue; |
| 98 |
|
| 99 |
err = open_volume(handles[i], &root_dir); |
| 100 |
if (err != EFI_SUCCESS) |
| 101 |
continue; |
| 102 |
|
| 103 |
/* simple ESP check */ |
| 104 |
err = root_dir->Open(root_dir, &efi_dir, (char16_t*) u"\\EFI", |
| 105 |
EFI_FILE_MODE_READ, |
| 106 |
EFI_FILE_READ_ONLY | EFI_FILE_DIRECTORY); |
| 107 |
if (err != EFI_SUCCESS) |
| 108 |
continue; |
| 109 |
|
| 110 |
*ret_vmm_dev = handles[i]; |
| 111 |
*ret_vmm_dir = TAKE_PTR(root_dir); |
| 112 |
return EFI_SUCCESS; |
| 113 |
} |
| 114 |
|
| 115 |
if (dp_err != EFI_SUCCESS) |
| 116 |
return EFI_NOT_FOUND; |
| 117 |
} |
| 118 |
assert_not_reached(); |
| 119 |
} |
| 120 |
|
| 121 |
static bool cpuid_in_hypervisor(void) { |
| 122 |
#if defined(__i386__) || defined(__x86_64__) |
| 123 |
unsigned eax, ebx, ecx, edx; |
| 124 |
|
| 125 |
/* This is a dumbed down version of src/basic/virt.c's detect_vm() that safely works in the UEFI |
| 126 |
* environment. */ |
| 127 |
|
| 128 |
if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) == 0) |
| 129 |
return false; |
| 130 |
|
| 131 |
if (FLAGS_SET(ecx, 0x80000000U)) |
| 132 |
return true; |
| 133 |
#endif |
| 134 |
|
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
bool in_hypervisor(void) { |
| 139 |
static int cache = -1; |
| 140 |
if (cache >= 0) |
| 141 |
return cache; |
| 142 |
|
| 143 |
cache = cpuid_in_hypervisor() || smbios_in_hypervisor(); |
| 144 |
return cache; |
| 145 |
} |
| 146 |
|
| 147 |
#if defined(__i386__) || defined(__x86_64__) |
| 148 |
static uint32_t cpuid_leaf(uint32_t eax, char ret_sig[static 13], bool swapped) { |
| 149 |
/* zero-init as some queries explicitly require subleaf == 0 */ |
| 150 |
uint32_t sig[3] = {}; |
| 151 |
|
| 152 |
if (swapped) |
| 153 |
__cpuid_count(eax, 0, eax, sig[0], sig[2], sig[1]); |
| 154 |
else |
| 155 |
__cpuid_count(eax, 0, eax, sig[0], sig[1], sig[2]); |
| 156 |
|
| 157 |
memcpy(ret_sig, sig, sizeof(sig)); |
| 158 |
ret_sig[12] = 0; /* \0-terminate the string to make string comparison possible */ |
| 159 |
|
| 160 |
return eax; |
| 161 |
} |
| 162 |
|
| 163 |
static uint64_t msr(uint32_t index) { |
| 164 |
uint64_t val; |
| 165 |
#ifdef __x86_64__ |
| 166 |
uint32_t low, high; |
| 167 |
asm volatile ("rdmsr" : "=a"(low), "=d"(high) : "c"(index) : "memory"); |
| 168 |
val = ((uint64_t)high << 32) | low; |
| 169 |
#else |
| 170 |
asm volatile ("rdmsr" : "=A"(val) : "c"(index) : "memory"); |
| 171 |
#endif |
| 172 |
return val; |
| 173 |
} |
| 174 |
|
| 175 |
static bool detect_hyperv_cvm(uint32_t isoltype) { |
| 176 |
uint32_t eax, ebx, ecx, edx, feat; |
| 177 |
char sig[13] = {}; |
| 178 |
|
| 179 |
feat = cpuid_leaf(CPUID_HYPERV_VENDOR_AND_MAX_FUNCTIONS, sig, false); |
| 180 |
|
| 181 |
if (feat < CPUID_HYPERV_MIN || feat > CPUID_HYPERV_MAX) |
| 182 |
return false; |
| 183 |
|
| 184 |
if (memcmp(sig, CPUID_SIG_HYPERV, sizeof(sig)) != 0) |
| 185 |
return false; |
| 186 |
|
| 187 |
__cpuid(CPUID_HYPERV_FEATURES, eax, ebx, ecx, edx); |
| 188 |
|
| 189 |
if (ebx & CPUID_HYPERV_ISOLATION && !(ebx & CPUID_HYPERV_CPU_MANAGEMENT)) { |
| 190 |
__cpuid(CPUID_HYPERV_ISOLATION_CONFIG, eax, ebx, ecx, edx); |
| 191 |
|
| 192 |
if ((ebx & CPUID_HYPERV_ISOLATION_TYPE_MASK) == isoltype) |
| 193 |
return true; |
| 194 |
} |
| 195 |
|
| 196 |
return false; |
| 197 |
} |
| 198 |
|
| 199 |
static bool detect_sev(void) { |
| 200 |
uint32_t eax, ebx, ecx, edx; |
| 201 |
uint64_t msrval; |
| 202 |
|
| 203 |
__cpuid(CPUID_GET_HIGHEST_FUNCTION, eax, ebx, ecx, edx); |
| 204 |
|
| 205 |
if (eax < CPUID_AMD_GET_ENCRYPTED_MEMORY_CAPABILITIES) |
| 206 |
return false; |
| 207 |
|
| 208 |
__cpuid(CPUID_AMD_GET_ENCRYPTED_MEMORY_CAPABILITIES, eax, ebx, ecx, edx); |
| 209 |
|
| 210 |
/* bit 1 == CPU supports SEV feature |
| 211 |
* |
| 212 |
* Note, Azure blocks this CPUID leaf from its SEV-SNP |
| 213 |
* guests, so we must fallback to trying some HyperV |
| 214 |
* specific CPUID checks. |
| 215 |
*/ |
| 216 |
if (!(eax & EAX_SEV)) |
| 217 |
return detect_hyperv_cvm(CPUID_HYPERV_ISOLATION_TYPE_SNP); |
| 218 |
|
| 219 |
msrval = msr(MSR_AMD64_SEV); |
| 220 |
|
| 221 |
if (msrval & (MSR_SEV_SNP | MSR_SEV_ES | MSR_SEV)) |
| 222 |
return true; |
| 223 |
|
| 224 |
return false; |
| 225 |
} |
| 226 |
|
| 227 |
static bool detect_tdx(void) { |
| 228 |
uint32_t eax, ebx, ecx, edx; |
| 229 |
char sig[13] = {}; |
| 230 |
|
| 231 |
__cpuid(CPUID_GET_HIGHEST_FUNCTION, eax, ebx, ecx, edx); |
| 232 |
|
| 233 |
if (eax < CPUID_INTEL_TDX_ENUMERATION) |
| 234 |
return false; |
| 235 |
|
| 236 |
cpuid_leaf(CPUID_INTEL_TDX_ENUMERATION, sig, true); |
| 237 |
|
| 238 |
if (memcmp(sig, CPUID_SIG_INTEL_TDX, sizeof(sig)) == 0) |
| 239 |
return true; |
| 240 |
|
| 241 |
if (detect_hyperv_cvm(CPUID_HYPERV_ISOLATION_TYPE_TDX)) |
| 242 |
return true; |
| 243 |
|
| 244 |
return false; |
| 245 |
} |
| 246 |
#endif /* ! __i386__ && ! __x86_64__ */ |
| 247 |
|
| 248 |
bool is_confidential_vm(void) { |
| 249 |
#if defined(__i386__) || defined(__x86_64__) |
| 250 |
char sig[13] = {}; |
| 251 |
|
| 252 |
if (!cpuid_in_hypervisor()) |
| 253 |
return false; |
| 254 |
|
| 255 |
cpuid_leaf(0, sig, true); |
| 256 |
|
| 257 |
if (memcmp(sig, CPUID_SIG_AMD, sizeof(sig)) == 0) |
| 258 |
return detect_sev(); |
| 259 |
if (memcmp(sig, CPUID_SIG_INTEL, sizeof(sig)) == 0) |
| 260 |
return detect_tdx(); |
| 261 |
#endif /* ! __i386__ && ! __x86_64__ */ |
| 262 |
|
| 263 |
return false; |
| 264 |
} |