diff --git a/Pal/src/host/Linux-SGX/db_misc.c b/Pal/src/host/Linux-SGX/db_misc.c index 55217be2..c86aa1d5 100644 --- a/Pal/src/host/Linux-SGX/db_misc.c +++ b/Pal/src/host/Linux-SGX/db_misc.c @@ -307,7 +307,15 @@ static void sanity_check_cpuid(uint32_t leaf, uint32_t subleaf, uint32_t values[ } int _DkCpuIdRetrieve(unsigned int leaf, unsigned int subleaf, unsigned int values[4]) { - if (!get_cpuid_from_cache(leaf, subleaf, values)) + bool skip_cache = false; + + /* the cpu core info cannot be cached due to its data varying depending on the calling thread */ + if (leaf == CPUID_EXT_TOPOLOGY_ENUMERATION_LEAF || + leaf == CPUID_V2EXT_TOPOLOGY_ENUMERATION_LEAF) { + skip_cache = true; + } + + if (!skip_cache && !get_cpuid_from_cache(leaf, subleaf, values)) return 0; if (IS_ERR(ocall_cpuid(leaf, subleaf, values))) @@ -315,7 +323,9 @@ int _DkCpuIdRetrieve(unsigned int leaf, unsigned int subleaf, unsigned int value sanity_check_cpuid(leaf, subleaf, values); - add_cpuid_to_cache(leaf, subleaf, values); + if (!skip_cache) + add_cpuid_to_cache(leaf, subleaf, values); + return 0; } diff --git a/Pal/src/host/Linux-SGX/enclave_ocalls.c b/Pal/src/host/Linux-SGX/enclave_ocalls.c index 4960c93e..d77cfabf 100644 --- a/Pal/src/host/Linux-SGX/enclave_ocalls.c +++ b/Pal/src/host/Linux-SGX/enclave_ocalls.c @@ -262,6 +262,15 @@ static void ocall_munmap_untrusted_cache(void* mem, size_t size, bool need_munma int ocall_cpuid(unsigned int leaf, unsigned int subleaf, unsigned int values[4]) { int retval = 0; ms_ocall_cpuid_t* ms; + bool bypass_exitless = false; + + /* the cpu topology info must be retrieved in the context of current thread + * rather than rpc thread in case exitless feature is enabled. + */ + if (leaf == CPUID_EXT_TOPOLOGY_ENUMERATION_LEAF || + leaf == CPUID_V2EXT_TOPOLOGY_ENUMERATION_LEAF) { + bypass_exitless = true; + } void* old_ustack = sgx_prepare_ustack(); ms = sgx_alloc_on_ustack_aligned(sizeof(*ms), alignof(*ms)); @@ -273,7 +282,7 @@ int ocall_cpuid(unsigned int leaf, unsigned int subleaf, unsigned int values[4]) WRITE_ONCE(ms->ms_leaf, leaf); WRITE_ONCE(ms->ms_subleaf, subleaf); - retval = sgx_exitless_ocall(OCALL_CPUID, ms); + retval = bypass_exitless ? sgx_ocall(OCALL_CPUID, ms) : sgx_exitless_ocall(OCALL_CPUID, ms); if (!retval) { values[0] = READ_ONCE(ms->ms_values[0]); diff --git a/Pal/src/pal_internal.h b/Pal/src/pal_internal.h index 569a4d07..fe5a026b 100644 --- a/Pal/src/pal_internal.h +++ b/Pal/src/pal_internal.h @@ -174,6 +174,8 @@ extern PAL_CONTROL g_pal_control; #define ALLOC_ALIGN_DOWN(addr) ALIGN_DOWN_POW2(addr, g_pal_state.alloc_align) #define ALLOC_ALIGN_DOWN_PTR(addr) ALIGN_DOWN_PTR_POW2(addr, g_pal_state.alloc_align) +#define CPUID_EXT_TOPOLOGY_ENUMERATION_LEAF 0x0b +#define CPUID_V2EXT_TOPOLOGY_ENUMERATION_LEAF 0x1f /*! * \brief Main initialization function *