trts_xsave.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "arch.h"
  32. #include "xsave.h"
  33. #include "trts_inst.h"
  34. #include "util.h"
  35. // 'SYNTHETIC_STATE' buffer size is (512 + 64 + 256) bytes
  36. // 512 for fxsave buffer,
  37. // 64 for xsave header,
  38. // 256 for YMM State (16 * 16 bytes of each YMMH-register)
  39. // and the buffer should be 64 byte aligned.
  40. #define SYNTHETIC_STATE_SIZE (512 + 64 + 256)
  41. se_static_assert(SYNTHETIC_STATE_SIZE <= SE_PAGE_SIZE);
  42. static SE_DECLSPEC_ALIGN(4096) const uint16_t
  43. SYNTHETIC_STATE[SYNTHETIC_STATE_SIZE/sizeof(uint16_t)] = {
  44. 0x037F, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1F80, 0, 0xFFFF, 0
  45. };
  46. static int g_xsave_enabled; // flag to indicate whether xsave is enabled or not
  47. // EENTER will set xcr0 with secs.attr.xfrm,
  48. // So use the xfeature mask from report instead of calling xgetbv
  49. uint64_t get_xfeature_state()
  50. {
  51. // target_info and report_data are useless
  52. // we only need to make sure their alignment and within enclave
  53. // so set the pointers to SYNTHETIC_STATE
  54. sgx_target_info_t *target_info = (sgx_target_info_t *)SYNTHETIC_STATE;
  55. sgx_report_data_t *report_data = (sgx_report_data_t *)SYNTHETIC_STATE;
  56. uint8_t buffer[sizeof(sgx_report_t) + REPORT_ALIGN_SIZE -1] = {0};
  57. sgx_report_t *report = (sgx_report_t *)ROUND_TO((size_t)buffer, REPORT_ALIGN_SIZE);
  58. do_ereport(target_info, report_data, report);
  59. g_xsave_enabled = (report->body.attributes.xfrm == SGX_XFRM_LEGACY) ? 0 : 1;
  60. uint64_t xfrm = report->body.attributes.xfrm;
  61. // no secrets in target_info, report_data, and report. no need to clear them before return
  62. // tlibc functions cannot be used before calling init_optimized_libs().
  63. return xfrm;
  64. }
  65. // save_and_clean_xfeature_regs()
  66. // do fwait, fxsave, and then clean the extended feature registers
  67. // Parameters:
  68. // buffer - If the pointer is not NULL, save the CPU state to the memory
  69. // Return Value:
  70. // none
  71. void save_and_clean_xfeature_regs(uint8_t *buffer)
  72. {
  73. do_fwait();
  74. if(buffer != 0)
  75. {
  76. uint8_t *buf = (uint8_t*)ROUND_TO((size_t)buffer, FXSAVE_ALIGN_SIZE);
  77. do_fxsave(buf);
  78. }
  79. if(g_xsave_enabled)
  80. {
  81. do_xrstor(SYNTHETIC_STATE);
  82. }
  83. else
  84. {
  85. do_fxrstor(SYNTHETIC_STATE);
  86. }
  87. }
  88. // restore_xfeature_regs()
  89. // restore the extended feature registers
  90. //
  91. void restore_xfeature_regs(const uint8_t *buffer)
  92. {
  93. if(buffer != 0)
  94. {
  95. uint8_t *buf = (uint8_t*)ROUND_TO((size_t)buffer, FXSAVE_ALIGN_SIZE);
  96. do_fxrstor(buf);
  97. }
  98. }