test-sgx.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <stdio.h>
  2. static inline void native_cpuid(unsigned int* eax, unsigned int* ebx, unsigned int* ecx,
  3. unsigned int* edx) {
  4. /* ecx is often an input as well as an output. */
  5. asm volatile("cpuid" : "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx) : "0"(*eax), "2"(*ecx));
  6. }
  7. int main(int argc, char** argv) {
  8. /* This programm prints some CPUID information and tests the SGX support of the CPU */
  9. unsigned eax, ebx, ecx, edx;
  10. eax = 1; /* processor info and feature bits */
  11. native_cpuid(&eax, &ebx, &ecx, &edx);
  12. printf("eax: %x ebx: %x ecx: %x edx: %x\n", eax, ebx, ecx, edx);
  13. printf("stepping %d\n", eax & 0xF); // Bit 3-0
  14. printf("model %d\n", (eax >> 4) & 0xF); // Bit 7-4
  15. printf("family %d\n", (eax >> 8) & 0xF); // Bit 11-8
  16. printf("processor type %d\n", (eax >> 12) & 0x3); // Bit 13-12
  17. printf("extended model %d\n", (eax >> 16) & 0xF); // Bit 19-16
  18. printf("extended family %d\n", (eax >> 20) & 0xFF); // Bit 27-20
  19. // if smx set - SGX global enable is supported
  20. printf("smx: %d\n", (ecx >> 6) & 1); // CPUID.1:ECX.[bit6]
  21. /* Extended feature bits (EAX=07H, ECX=0H)*/
  22. printf("\nExtended feature bits (EAX=07H, ECX=0H)\n");
  23. eax = 7;
  24. ecx = 0;
  25. native_cpuid(&eax, &ebx, &ecx, &edx);
  26. printf("eax: %x ebx: %x ecx: %x edx: %x\n", eax, ebx, ecx, edx);
  27. // CPUID.(EAX=07H, ECX=0H):EBX.SGX = 1,
  28. printf("sgx available: %d\n", (ebx >> 2) & 0x1);
  29. /* SGX has to be enabled in MSR.IA32_Feature_Control.SGX_Enable
  30. * check with msr-tools: rdmsr -ax 0x3a
  31. * SGX_Enable is Bit 18
  32. * if SGX_Enable = 0 no leaf information will appear.
  33. * for more information check Intel Docs
  34. * Architectures-software-developer-system-programming-manual - 35.1
  35. * Architectural MSRS
  36. */
  37. /* CPUID Leaf 12H, Sub-Leaf 0 Enumeration of Intel SGX Capabilities
  38. * (EAX=12H,ECX=0)
  39. */
  40. printf("\nCPUID Leaf 12H, Sub-Leaf 0 of Intel SGX Capabilities (EAX=12H,ECX=0)\n");
  41. eax = 0x12;
  42. ecx = 0;
  43. native_cpuid(&eax, &ebx, &ecx, &edx);
  44. printf("eax: %x ebx: %x ecx: %x edx: %x\n", eax, ebx, ecx, edx);
  45. printf("sgx 1 supported: %d\n", eax & 0x1);
  46. printf("sgx 2 supported: %d\n", (eax >> 1) & 0x1);
  47. printf("MaxEnclaveSize_Not64: %x\n", edx & 0xFF);
  48. printf("MaxEnclaveSize_64: %x\n", (edx >> 8) & 0xFF);
  49. /* CPUID Leaf 12H, Sub-Leaf 1 Enumeration of Intel SGX Capabilities
  50. * (EAX=12H,ECX=1) */
  51. printf("\nCPUID Leaf 12H, Sub-Leaf 1 of Intel SGX Capabilities (EAX=12H,ECX=1)\n");
  52. eax = 0x12;
  53. ecx = 1;
  54. native_cpuid(&eax, &ebx, &ecx, &edx);
  55. printf("eax: %x ebx: %x ecx: %x edx: %x\n", eax, ebx, ecx, edx);
  56. int i;
  57. for (i = 2; i < 10; i++) {
  58. /* CPUID Leaf 12H, Sub-Leaf i Enumeration of Intel SGX Capabilities
  59. * (EAX=12H,ECX=i) */
  60. printf("\nCPUID Leaf 12H, Sub-Leaf %d of Intel SGX Capabilities (EAX=12H,ECX=%d)\n", i, i);
  61. eax = 0x12;
  62. ecx = i;
  63. native_cpuid(&eax, &ebx, &ecx, &edx);
  64. printf("eax: %x ebx: %x ecx: %x edx: %x\n", eax, ebx, ecx, edx);
  65. }
  66. return 0;
  67. }