Created
August 14, 2015 13:39
-
-
Save envy/2a9437564f05cf17e3cd to your computer and use it in GitHub Desktop.
Check SGX availability
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| /* | |
| * This program checks SGX availability by checking CPUID | |
| * Relevant spec sections: 1.7 (and 1.7.1 and 1.7.2) | |
| * 2015-08-14 Nico Weichbrodt | |
| */ | |
| /* | |
| * Helper function for CPUID querying | |
| */ | |
| void cpuid(int eax, int ecx, int *eax_out, int *ebx_out, int *ecx_out, int *edx_out) | |
| { | |
| asm("pushq %%rbx\n\t" | |
| "cpuid\n\t" | |
| "movl %%ebx,%1\n\t" | |
| "popq %%rbx\n\t" | |
| : "=a"(*eax_out), "=r"(*ebx_out), "=c"(*ecx_out), "=d"(*edx_out) | |
| : "a"(eax), "c"(ecx)); | |
| printf("EAX: %x\nEBX: %x\nECX: %x\nEDX: %x\n", *eax_out, *ebx_out, *ecx_out, *edx_out); | |
| } | |
| typedef struct | |
| { | |
| int fsgsbase:1; | |
| int reserved1:1; | |
| int sgx:1; | |
| int bmi1:1; | |
| int hle:1; | |
| int avx2:1; | |
| int reserved6:1; | |
| int smep:1; | |
| int bmi2:1; | |
| int erms:1; | |
| int invpcid:1; | |
| int rtm:1; | |
| int reserved12:1; | |
| int reserved13:1; | |
| int mpx:1; | |
| int reserved15:1; | |
| int avx512f:1; | |
| int avx512dq:1; | |
| int rdseed:1; | |
| int adx:1; | |
| int smap:1; | |
| int avx512ifma:1; | |
| int pcommit:1; | |
| int clflushopt:1; | |
| int clwb:1; | |
| int intel_processor_trace:1; | |
| int avx512pf:1; | |
| int avx512er:1; | |
| int avx512cd:1; | |
| int sha:1; | |
| int avx512bw:1; | |
| int avx512vl:1; | |
| } sgx_cap_7_0_ebx_t; | |
| typedef struct | |
| { | |
| int sgxv1:1; | |
| int sgxv2:1; | |
| int reserved:30; | |
| } sgx_cap_12_0__eax_t; | |
| int main() | |
| { | |
| int eax_out, ebx_out, ecx_out, edx_out; | |
| // Check general SGX availability | |
| cpuid(0x07, 0x00, &eax_out, &ebx_out, &ecx_out, &edx_out); | |
| // Bit 2 of EBX informs of SGX availability. | |
| // The spec does not say if they count from 0 or 1, but looking at the tables 1-4, 1-5 and 1-6 | |
| // I assume they start counting at 0. | |
| sgx_cap_7_0_ebx_t *sgx_avail = (sgx_cap_7_0_ebx_t *) &ebx_out; | |
| printf("SGX availability: %d\n", sgx_avail->sgx); | |
| if (!sgx_avail->sgx) | |
| { | |
| return 0; | |
| } | |
| // Check which SGX version is supported | |
| cpuid(0x12, 0x00, &eax_out, &ebx_out, &ecx_out, &edx_out); | |
| sgx_cap_12_0__eax_t *sgx_version = (sgx_cap_12_0__eax_t *) &eax_out; | |
| printf("SGX v1 support: %d\nSGX v2 support: %d\n", sgx_version->sgxv1, sgx_version->sgxv2); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment