test-sgx.c 2.9 KB

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