[Pal/Linux-SGX] Avoid caching of CPUID leaf 0xB and 0x1F

Caching of cpuid leaf 0xB and 0x1F can cause incorrect enumeration
of CPU topology. This patch fixes the issue.

Co-authored-by: Gary <gordon.king@intel.com>
This commit is contained in:
Vijay Dhanraj
2020-11-06 22:28:32 +01:00
committed by Michał Kowalczyk
co-authored by Gary
parent 94c684512a
commit b07bca1aa5
3 changed files with 24 additions and 3 deletions
+12 -2
View File
@@ -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;
}
+10 -1
View File
@@ -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]);
+2
View File
@@ -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
*