trts_xsave.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (C) 2011-2018 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 "arch.h"
  32. #include "xsave.h"
  33. #include "trts_inst.h"
  34. #include "util.h"
  35. #include "global_data.h"
  36. #include "stdlib.h"
  37. #define SYNTHETIC_STATE_SIZE (512 + 64) // 512 for legacy regs, 64 for xsave header
  38. //FXRSTOR only cares about the first 512 bytes, while
  39. //XRSTOR in compacted mode will ignore the first 512 bytes.
  40. extern "C" SE_DECLSPEC_ALIGN(XSAVE_ALIGN_SIZE) const uint32_t
  41. SYNTHETIC_STATE[SYNTHETIC_STATE_SIZE/sizeof(uint32_t)] __attribute__((section(".niprod"))) = {
  42. 0x037F, 0, 0, 0, 0, 0, 0x1F80, 0xFFFF, 0, 0, 0, 0, 0, 0, 0, 0,
  43. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  44. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  45. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  46. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  47. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  48. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  49. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  50. 0, 0, 0, 0x80000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // XCOMP_BV[63] = 1, compaction mode
  51. };
  52. int g_xsave_enabled __attribute__((section(".nipd"))) = 0; // flag to indicate whether xsave is enabled or not
  53. #ifdef SE_SIM
  54. uint32_t g_xsave_mask_high __attribute__((section(".nipd"))) = 0xFFFFFFFF;
  55. uint32_t g_xsave_mask_low __attribute__((section(".nipd"))) = 0xFFFFFFFF;
  56. #endif
  57. // EENTER will set xcr0 with secs.attr.xfrm,
  58. // So use the xfeature mask from report instead of calling xgetbv
  59. #ifdef __clang__
  60. #define SE_OPTIMIZE_OFF [[clang::optnone]]
  61. #else
  62. #define SE_OPTIMIZE_OFF
  63. #endif
  64. SE_OPTIMIZE_OFF
  65. uint64_t get_xfeature_state()
  66. {
  67. se_static_assert (REPORT_ALIGN_SIZE >= REPORT_DATA_ALIGN_SIZE);
  68. se_static_assert (REPORT_ALIGN_SIZE >= TARGET_INFO_ALIGN_SIZE);
  69. se_static_assert (sizeof(sgx_target_info_t) >= sizeof(sgx_report_t));
  70. se_static_assert (sizeof(sgx_report_t) >= sizeof(sgx_report_data_t));
  71. // target_info and report_data are useless
  72. // we only need to make sure their alignment and within enclave
  73. uint8_t buffer[ROUND_TO(sizeof(sgx_report_t), REPORT_ALIGN_SIZE) + REPORT_ALIGN_SIZE - 1];
  74. for(size_t i=0; i< ROUND_TO(sizeof(sgx_report_t), REPORT_ALIGN_SIZE) + REPORT_ALIGN_SIZE - 1; i++)
  75. {
  76. buffer[i] = 0;
  77. }
  78. sgx_report_t *report = (sgx_report_t *)ROUND_TO((size_t)buffer, REPORT_ALIGN_SIZE);
  79. sgx_target_info_t *target_info = (sgx_target_info_t *)report;
  80. sgx_report_data_t *report_data = (sgx_report_data_t *)report;
  81. do_ereport(target_info, report_data, report);
  82. g_xsave_enabled = (report->body.attributes.xfrm == SGX_XFRM_LEGACY) ? 0 : 1;
  83. uint64_t xfrm = report->body.attributes.xfrm;
  84. #ifdef SE_SIM
  85. g_xsave_mask_high = (uint32_t)(xfrm >> 32);
  86. g_xsave_mask_low = (uint32_t)(xfrm & 0xFFFFFFFF);
  87. #endif
  88. // no secrets in target_info, report_data, and report. no need to clear them before return
  89. // tlibc functions cannot be used before calling init_optimized_libs().
  90. return xfrm;
  91. }