Skip to content

`-machine microvm -cpu host` crashes when guest attempts to check CPUID SGX bits

Running a microvm QEMU with KVM and -cpu host causes an abort right away:

/usr/src/debug/qemu/qemu-8.2.0/include/hw/i386/pc.h:144:PC_MACHINE: Object 0x5555571b0290 is not an instance of type generic-pc-machine

This is because sgx_epc_get_section assumes a PC platform is in use:

bool sgx_epc_get_section(int section_nr, uint64_t *addr, uint64_t *size)
{
    PCMachineState *pcms = PC_MACHINE(qdev_get_machine());

This makes sense, since the SGX EPC is a feature of the PC platform.
However, sgx_epc_get_section is called by CPUID regardless of whether SGX state has been initialized or which platform is in use:

void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
                   uint32_t *eax, uint32_t *ebx,
                   uint32_t *ecx, uint32_t *edx)
{
    // ...
    switch(index) {
    // ...
    case 0x12:
#ifndef CONFIG_USER_ONLY
        if (!kvm_enabled() ||
            !(env->features[FEAT_7_0_EBX] & CPUID_7_0_EBX_SGX)) {
            *eax = *ebx = *ecx = *edx = 0;
            break;
        }
        // ...
        if (count > 1) {
            uint64_t epc_addr, epc_size;
            if (sgx_epc_get_section(count - 2, &epc_addr, &epc_size)) {

My understanding is that the PC_MACHINE() cast above should be converted to a dynamic cast.

This appears to have been the case since 1dec2e1f, not sure why I didn't hit this bug earlier.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information