se_string_init.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "stdint.h"
  32. #include "se_cpu_feature.h"
  33. #include "se_cdefs.h"
  34. // add a version to tlibc.
  35. SGX_ACCESS_VERSION(tstdc, 1)
  36. #ifdef _TLIBC_USE_INTEL_FAST_STRING_
  37. extern uint64_t __intel_cpu_feature_indicator;
  38. extern uint64_t __intel_cpu_feature_indicator_x;
  39. extern unsigned int __intel_cpu_indicator;
  40. static int _intel_cpu_indicator_init(uint64_t cpu_feature_bits)
  41. {
  42. // We have the assumption that SSE3 is the lowest feature for enclave loading,
  43. // so failure will be returned if features are all below-SSE3.
  44. __intel_cpu_indicator = CPU_GENERIC;
  45. if ((cpu_feature_bits & CPU_FEATURE_AVX2) &&
  46. (cpu_feature_bits & CPU_FEATURE_FMA) &&
  47. (cpu_feature_bits & CPU_FEATURE_BMI) &&
  48. (cpu_feature_bits & CPU_FEATURE_LZCNT) &&
  49. (cpu_feature_bits & CPU_FEATURE_HLE) &&
  50. (cpu_feature_bits & CPU_FEATURE_RTM)) {
  51. __intel_cpu_indicator = CPU_HSW;
  52. }
  53. else if (cpu_feature_bits & CPU_FEATURE_F16C) {
  54. __intel_cpu_indicator = CPU_IVB;
  55. }
  56. else if (cpu_feature_bits & CPU_FEATURE_AVX) {
  57. __intel_cpu_indicator = CPU_SNB;
  58. }
  59. else if ((cpu_feature_bits & CPU_FEATURE_PCLMULQDQ) &&
  60. (cpu_feature_bits & CPU_FEATURE_AES)) {
  61. __intel_cpu_indicator = CPU_WSM;
  62. }
  63. else if ((cpu_feature_bits & CPU_FEATURE_SSE4_2) &&
  64. (cpu_feature_bits & CPU_FEATURE_POPCNT)) {
  65. __intel_cpu_indicator = CPU_NHM;
  66. }
  67. else if (cpu_feature_bits & CPU_FEATURE_SSE4_1) {
  68. __intel_cpu_indicator = CPU_SNI;
  69. }
  70. else if (cpu_feature_bits & CPU_FEATURE_MOVBE) {
  71. __intel_cpu_indicator = CPU_BNL;
  72. }
  73. else if (cpu_feature_bits & CPU_FEATURE_SSSE3) {
  74. __intel_cpu_indicator = CPU_MNI;
  75. }
  76. else if (cpu_feature_bits & CPU_FEATURE_SSE3) {
  77. __intel_cpu_indicator = CPU_PENTIUM_4_PNI;
  78. }
  79. else if (cpu_feature_bits & CPU_FEATURE_SSE2) {
  80. __intel_cpu_indicator = CPU_BNI;
  81. }
  82. else if (cpu_feature_bits & CPU_FEATURE_SSE) {
  83. __intel_cpu_indicator = CPU_PENTIUM_III_SSE;
  84. }
  85. else {
  86. return -1;
  87. }
  88. return 0;
  89. }
  90. int sgx_init_string_lib(uint64_t cpu_feature_indicator)
  91. {
  92. int genuine_intel = (cpu_feature_indicator & ~(CPU_FEATURE_GENERIC_IA32));
  93. if(genuine_intel) {
  94. __intel_cpu_feature_indicator = __intel_cpu_feature_indicator_x = cpu_feature_indicator;
  95. }
  96. else {
  97. __intel_cpu_feature_indicator = __intel_cpu_feature_indicator_x = CPU_FEATURE_GENERIC_IA32;
  98. }
  99. return _intel_cpu_indicator_init(cpu_feature_indicator);
  100. }
  101. #else
  102. int sgx_init_string_lib(uint64_t cpu_feature_indicator)
  103. {
  104. (void)cpu_feature_indicator;
  105. return 0;
  106. }
  107. #endif