trts_xsave.cpp 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 "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)] = {
  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; // flag to indicate whether xsave is enabled or not
  53. // EENTER will set xcr0 with secs.attr.xfrm,
  54. // So use the xfeature mask from report instead of calling xgetbv
  55. #ifdef __clang__
  56. #define SE_OPTIMIZE_OFF [[clang::optnone]]
  57. #else
  58. #define SE_OPTIMIZE_OFF
  59. #endif
  60. SE_OPTIMIZE_OFF
  61. uint64_t get_xfeature_state()
  62. {
  63. assert (REPORT_ALIGN_SIZE >= REPORT_DATA_ALIGN_SIZE);
  64. assert (REPORT_ALIGN_SIZE >= TARGET_INFO_ALIGN_SIZE);
  65. // target_info and report_data are useless
  66. // we only need to make sure their alignment and within enclave
  67. uint8_t buffer[sizeof(sgx_report_t) + REPORT_ALIGN_SIZE -1];
  68. for(size_t i=0; i< sizeof(sgx_report_t) + REPORT_ALIGN_SIZE -1; i++)
  69. {
  70. buffer[i] = 0;
  71. }
  72. sgx_report_t *report = (sgx_report_t *)ROUND_TO((size_t)buffer, REPORT_ALIGN_SIZE);
  73. sgx_target_info_t *target_info = (sgx_target_info_t *)report;
  74. sgx_report_data_t *report_data = (sgx_report_data_t *)report;
  75. do_ereport(target_info, report_data, report);
  76. g_xsave_enabled = (report->body.attributes.xfrm == SGX_XFRM_LEGACY) ? 0 : 1;
  77. uint64_t xfrm = report->body.attributes.xfrm;
  78. // no secrets in target_info, report_data, and report. no need to clear them before return
  79. // tlibc functions cannot be used before calling init_optimized_libs().
  80. return xfrm;
  81. }