trts_xsave.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. // '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];
  57. for(size_t i=0; i< sizeof(sgx_report_t) + REPORT_ALIGN_SIZE -1; i++)
  58. {
  59. buffer[i] = 0;
  60. }
  61. sgx_report_t *report = (sgx_report_t *)ROUND_TO((size_t)buffer, REPORT_ALIGN_SIZE);
  62. do_ereport(target_info, report_data, report);
  63. g_xsave_enabled = (report->body.attributes.xfrm == SGX_XFRM_LEGACY) ? 0 : 1;
  64. uint64_t xfrm = report->body.attributes.xfrm;
  65. // no secrets in target_info, report_data, and report. no need to clear them before return
  66. // tlibc functions cannot be used before calling init_optimized_libs().
  67. return xfrm;
  68. }
  69. // save_and_clean_xfeature_regs()
  70. // do fwait, fxsave, and then clean the extended feature registers
  71. // Parameters:
  72. // buffer - If the pointer is not NULL, save the CPU state to the memory
  73. // Return Value:
  74. // none
  75. void save_and_clean_xfeature_regs(uint8_t *buffer)
  76. {
  77. do_fwait();
  78. if(buffer != 0)
  79. {
  80. uint8_t *buf = (uint8_t*)ROUND_TO((size_t)buffer, FXSAVE_ALIGN_SIZE);
  81. do_fxsave(buf);
  82. }
  83. if(g_xsave_enabled)
  84. {
  85. do_xrstor(SYNTHETIC_STATE);
  86. }
  87. else
  88. {
  89. do_fxrstor(SYNTHETIC_STATE);
  90. }
  91. }
  92. // restore_xfeature_regs()
  93. // restore the extended feature registers
  94. //
  95. void restore_xfeature_regs(const uint8_t *buffer)
  96. {
  97. if(buffer != 0)
  98. {
  99. uint8_t *buf = (uint8_t*)ROUND_TO((size_t)buffer, FXSAVE_ALIGN_SIZE);
  100. do_fxrstor(buf);
  101. }
  102. }