Skip to content

Commit

Permalink
Set max cpuid input value to highest emulated value
Browse files Browse the repository at this point in the history
cpuid(0) and cpuid(0x80000000) should return the highest emulated value
instead of the CPU's actual value. This prevents some existing code to
try higher leaves.

Signed-off-by: Thomas Tendyck <[email protected]>
  • Loading branch information
thomasten committed Nov 10, 2021
1 parent dee3c1c commit f16b7c3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
6 changes: 6 additions & 0 deletions enclave/core/sgx/cpuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ oe_result_t oe_initialize_cpuid(void)
if (!(_cpuid_table[1][OE_CPUID_RCX] & OE_CPUID_AESNI_FEATURE))
oe_abort();

// set max input value for basic and extended
_cpuid_table[oe_get_emulated_cpuid_leaf_index(0)][OE_CPUID_RAX] =
OE_CPUID_MAX_BASIC;
_cpuid_table[oe_get_emulated_cpuid_leaf_index(0x80000000)][OE_CPUID_RAX] =
OE_CPUID_MAX_EXTENDED;

result = OE_OK;

done:
Expand Down
2 changes: 2 additions & 0 deletions include/openenclave/internal/cpuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#define OE_CPUID_OPCODE 0xA20F
#define OE_CPUID_LEAF_COUNT 6 /* 0,1,4,7,0x80000000,0x80000001 */
#define OE_CPUID_MAX_BASIC 7
#define OE_CPUID_MAX_EXTENDED 0x80000001

#define OE_CPUID_RAX 0
#define OE_CPUID_RBX 1
Expand Down
13 changes: 11 additions & 2 deletions tests/VectorException/host/host.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,13 @@ void test_sigill_handling(

for (uint32_t j = 0; j < OE_CPUID_REG_COUNT; j++)
{
if (i == 1 && j == 1)
if (leaf == 0 && j == OE_CPUID_RAX)
{
// The enclave sets this to the highest emulated leaf.
OE_TEST(OE_CPUID_MAX_BASIC == cpuid_table[i][j]);
}
else if (leaf == 1 && j == OE_CPUID_RBX)
{
// Leaf 1. EBX register.
// The highest 8 bits indicates the current executing processor
// id.
// There is no guarantee that the value is the same across
Expand All @@ -135,6 +139,11 @@ void test_sigill_handling(
(cpuid_info[j] & 0x00FFFFFF) ==
(cpuid_table[i][j] & 0x00FFFFFF));
}
else if (leaf == 0x80000000 && j == OE_CPUID_RAX)
{
// The enclave sets this to the highest emulated leaf.
OE_TEST(OE_CPUID_MAX_EXTENDED == cpuid_table[i][j]);
}
else
{
OE_TEST(cpuid_info[j] == cpuid_table[i][j]);
Expand Down

0 comments on commit f16b7c3

Please sign in to comment.