init_optimized_lib.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 2011-2016 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "init_optimized_lib.h"
  32. #include <stdint.h>
  33. #include "se_cpu_feature.h"
  34. #include "sgx_trts.h"
  35. #include "sgx_attributes.h"
  36. #include "global_data.h"
  37. extern "C" int sgx_init_string_lib(uint64_t cpu_feature_indicator);
  38. extern "C" sgx_status_t sgx_init_crypto_lib(uint64_t cpu_feature_indicator);
  39. static int set_global_feature_indicator(uint64_t feature_bit_array, uint64_t xfrm)
  40. {
  41. // Confirm the reserved bits and the unset bits by uRTS must be 0.
  42. if(feature_bit_array & (RESERVED_CPU_FEATURE_BIT))
  43. {
  44. // clear the reserved bits
  45. feature_bit_array = feature_bit_array & (~(RESERVED_CPU_FEATURE_BIT));
  46. }
  47. ;
  48. #ifdef SE_SIM
  49. // Simulation mode - requires SSE for x86 and SSE2 for x64.
  50. #ifdef SE_32
  51. if(!(feature_bit_array & ~(CPU_FEATURE_SSE - 1)))
  52. {
  53. return -1;
  54. }
  55. #else
  56. if(!(feature_bit_array & ~(CPU_FEATURE_SSE2 - 1)))
  57. {
  58. return -1;
  59. }
  60. #endif
  61. #else
  62. // HW mode - requires SSE4.2. Take SSE4.2 as the baseline.
  63. if(!(feature_bit_array & ~(CPU_FEATURE_SSE4_2 - 1)))
  64. {
  65. return -1;
  66. }
  67. #endif
  68. // Check for inconsistencies in the CPUID feature mask.
  69. if ( (((feature_bit_array & CPU_FEATURE_SSE) == CPU_FEATURE_SSE) &&((feature_bit_array & (CPU_FEATURE_SSE - 1)) != (CPU_FEATURE_SSE - 1))) ||
  70. (((feature_bit_array & CPU_FEATURE_SSE2) == CPU_FEATURE_SSE2) &&((feature_bit_array & (CPU_FEATURE_SSE2 - 1)) != (CPU_FEATURE_SSE2 - 1))) ||
  71. (((feature_bit_array & CPU_FEATURE_SSE3) == CPU_FEATURE_SSE3) &&((feature_bit_array & (CPU_FEATURE_SSE3 - 1)) != (CPU_FEATURE_SSE3 - 1))) ||
  72. (((feature_bit_array & CPU_FEATURE_SSSE3) == CPU_FEATURE_SSSE3) && ((feature_bit_array & (CPU_FEATURE_SSSE3 - 1)) != (CPU_FEATURE_SSSE3 - 1))) ||
  73. (((feature_bit_array & CPU_FEATURE_SSE4_1) == CPU_FEATURE_SSE4_1) && ((feature_bit_array & (CPU_FEATURE_SSE4_1 - 1)) != (CPU_FEATURE_SSE4_1 - 1))) ||
  74. (((feature_bit_array & CPU_FEATURE_SSE4_2) == CPU_FEATURE_SSE4_2) && ((feature_bit_array & (CPU_FEATURE_SSE4_2 - 1)) != (CPU_FEATURE_SSE4_2 - 1))) )
  75. {
  76. return -1;
  77. }
  78. // Determine whether the OS & ENCLAVE support SAVE/RESTORE of the AVX register set
  79. // IF NOT, clear the advanced feature set bits corresponding to AVX and beyond
  80. if(!XFEATURE_ENABLED_AVX(xfrm))
  81. {
  82. // AVX is disabled by OS, so clear the AVX related feature bits
  83. feature_bit_array &= (~(CPU_FEATURE_AVX | CPU_FEATURE_F16C | CPU_FEATURE_AVX2 |
  84. CPU_FEATURE_FMA | CPU_FEATURE_RTM | CPU_FEATURE_HLE | CPU_FEATURE_BMI |
  85. CPU_FEATURE_PREFETCHW | CPU_FEATURE_RDSEED | CPU_FEATURE_ADCOX));
  86. }
  87. g_cpu_feature_indicator = feature_bit_array;
  88. return 0;
  89. }
  90. extern "C" int init_optimized_libs(const uint64_t feature_bit_array, uint64_t xfrm)
  91. {
  92. if (g_enclave_state != ENCLAVE_INIT_IN_PROGRESS)
  93. {
  94. return -1;
  95. }
  96. // set the global feature indicator
  97. if(set_global_feature_indicator(feature_bit_array, xfrm))
  98. {
  99. return -1;
  100. }
  101. // Init string library with the global feature indicator
  102. if(sgx_init_string_lib(g_cpu_feature_indicator) != 0)
  103. {
  104. return -1;
  105. }
  106. // Init IPP crypto library with the global feature indicator
  107. if(sgx_init_crypto_lib(g_cpu_feature_indicator) != 0)
  108. {
  109. return -1;
  110. }
  111. return 0;
  112. }