init_optimized_lib.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (C) 2011-2017 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. // Requires SSE4.1. Take SSE4.1 as the baseline.
  48. if(!(feature_bit_array & ~(CPU_FEATURE_SSE4_1 - 1)))
  49. {
  50. return -1;
  51. }
  52. // Check for inconsistencies in the CPUID feature mask.
  53. if ( (((feature_bit_array & CPU_FEATURE_SSE) == CPU_FEATURE_SSE) &&((feature_bit_array & (CPU_FEATURE_SSE - 1)) != (CPU_FEATURE_SSE - 1))) ||
  54. (((feature_bit_array & CPU_FEATURE_SSE2) == CPU_FEATURE_SSE2) &&((feature_bit_array & (CPU_FEATURE_SSE2 - 1)) != (CPU_FEATURE_SSE2 - 1))) ||
  55. (((feature_bit_array & CPU_FEATURE_SSE3) == CPU_FEATURE_SSE3) &&((feature_bit_array & (CPU_FEATURE_SSE3 - 1)) != (CPU_FEATURE_SSE3 - 1))) ||
  56. (((feature_bit_array & CPU_FEATURE_SSSE3) == CPU_FEATURE_SSSE3) && ((feature_bit_array & (CPU_FEATURE_SSSE3 - 1)) != (CPU_FEATURE_SSSE3 - 1))) ||
  57. (((feature_bit_array & CPU_FEATURE_SSE4_1) == CPU_FEATURE_SSE4_1) && ((feature_bit_array & (CPU_FEATURE_SSE4_1 - 1)) != (CPU_FEATURE_SSE4_1 - 1))) ||
  58. (((feature_bit_array & CPU_FEATURE_SSE4_2) == CPU_FEATURE_SSE4_2) && ((feature_bit_array & (CPU_FEATURE_SSE4_2 - 1)) != (CPU_FEATURE_SSE4_2 - 1))) )
  59. {
  60. return -1;
  61. }
  62. // Determine whether the OS & ENCLAVE support SAVE/RESTORE of the AVX register set
  63. // IF NOT, clear the advanced feature set bits corresponding to AVX and beyond
  64. if(!XFEATURE_ENABLED_AVX(xfrm))
  65. {
  66. // AVX is disabled by OS, so clear the AVX related feature bits
  67. feature_bit_array &= (~(CPU_FEATURE_AVX | CPU_FEATURE_F16C | CPU_FEATURE_AVX2 |
  68. CPU_FEATURE_FMA | CPU_FEATURE_RTM | CPU_FEATURE_HLE | CPU_FEATURE_BMI |
  69. CPU_FEATURE_PREFETCHW | CPU_FEATURE_RDSEED | CPU_FEATURE_ADCOX));
  70. }
  71. g_cpu_feature_indicator = feature_bit_array;
  72. return 0;
  73. }
  74. extern "C" int init_optimized_libs(const uint64_t feature_bit_array, uint64_t xfrm)
  75. {
  76. if (g_enclave_state != ENCLAVE_INIT_IN_PROGRESS)
  77. {
  78. return -1;
  79. }
  80. // set the global feature indicator
  81. if(set_global_feature_indicator(feature_bit_array, xfrm))
  82. {
  83. return -1;
  84. }
  85. // Init string library with the global feature indicator
  86. if(sgx_init_string_lib(g_cpu_feature_indicator) != 0)
  87. {
  88. return -1;
  89. }
  90. // Init IPP crypto library with the global feature indicator
  91. if(sgx_init_crypto_lib(g_cpu_feature_indicator) != 0)
  92. {
  93. return -1;
  94. }
  95. return 0;
  96. }