未验证 提交 1a38ec31 编写于 作者: H haosanzi 提交者: GitHub

rune/libenclave/skeleton: Simplify get_sgx_xfrm_by_cpuid()

Signed-off-by: NShirong Hao <shirong@linux.alibaba.com>
上级 d008d4b1
...@@ -49,44 +49,30 @@ static inline uint64_t get_xcr0() ...@@ -49,44 +49,30 @@ static inline uint64_t get_xcr0()
return xgetbv(0); return xgetbv(0);
} }
static bool try_get_xcr0(uint64_t *value) static uint64_t try_get_xcr0()
{ {
int cpu_info[4] = {0, 0, 0, 0}; int cpu_info[4] = {0, 0, 0, 0};
*value = SGX_XFRM_LEGACY; // Check if xgetbv instruction is supported.
// check if xgetbv instruction is supported
__cpuid(cpu_info, 1); __cpuid(cpu_info, 1);
// ecx[27:26] indicate whether support xsave/xrstor, and whether enable xgetbv, xsetbv // ecx[27:26] indicate whether support xsave/xrstor, and whether enable xgetbv, xsetbv.
if (!(cpu_info[2] & (1<<XSAVE_SHIFT)) || !(cpu_info[2] & (1<<OSXSAVE_SHIFT))) if (!(cpu_info[2] & (1 << OSXSAVE_SHIFT)))
return false; return SGX_XFRM_LEGACY;
*value = get_xcr0();
// check if xsavec is supported // If XSAVE is supported, while XSAVEC is not supported,
// Assume that XSAVEC is always supported if XSAVE is supported // set xfrm to legacy, because skeleton requires XSAVEC feature available on the path of enclave-exit.
cpu_info[0] = cpu_info[1] = cpu_info[2] = cpu_info[3] = 0;
__cpuidex(cpu_info, 0xD, 1); __cpuidex(cpu_info, 0xD, 1);
if (!(cpu_info[0] & (1<<XSAVEC_SHIFT))) if (!(cpu_info[0] & (1 << XSAVEC_SHIFT)))
return false; return SGX_XFRM_LEGACY;
return true; // If x-feature is supported and enabled by OS, we need make sure it is also supported in enclave.
__cpuidex(cpu_info, SGX_LEAF, 1);
return (get_xcr0() & (((uint64_t)cpu_info[3] << 32) | cpu_info[2]));
} }
void get_sgx_xfrm_by_cpuid(uint64_t *xfrm) void get_sgx_xfrm_by_cpuid(uint64_t *xfrm)
{ {
int cpu_info[4] = {0, 0, 0, 0}; *xfrm = try_get_xcr0();
__cpuidex(cpu_info, SGX_LEAF, 1);
if (try_get_xcr0(xfrm) == false) {
// if XSAVE is supported, while XSAVEC is not supported,
// set xfrm to legacy, because XSAVEC cannot be executed within enclave.
*xfrm = SGX_XFRM_LEGACY;
} else {
// If x-feature is supported and enabled by OS, we need make sure it is also supported in enclave.
*xfrm &= (((uint64_t)cpu_info[3] << 32) | cpu_info[2]);
}
} }
bool is_launch_control_supported(void) bool is_launch_control_supported(void)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册