enclave_xstate.c 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * this function is shamelessly stolen from linux-sgx
  3. * https://github.com/intel/linux-sgx/blob/9ddec08fb98c1636ed3b1a77bbc4fa3520344ede/sdk/trts/trts_xsave.cpp
  4. * It has BSD lisence.
  5. */
  6. /*
  7. * Copyright (C) 2011-2019 Intel Corporation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. * * Neither the name of Intel Corporation nor the names of its
  20. * contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. #include <pal_linux.h>
  37. #include <pal_linux_error.h>
  38. #include <pal_internal.h>
  39. #include <pal_debug.h>
  40. #include <pal_error.h>
  41. #include <pal_security.h>
  42. #include <pal_crypto.h>
  43. #include <api.h>
  44. #include <list.h>
  45. #include <stdbool.h>
  46. #include "enclave_pages.h"
  47. int xsave_enabled = 0;
  48. uint64_t xsave_features = 0;
  49. uint32_t xsave_size = 0;
  50. //FXRSTOR only cares about the first 512 bytes, while
  51. //XRSTOR in compacted mode will ignore the first 512 bytes.
  52. const uint32_t xsave_reset_state[XSAVE_RESET_STATE_SIZE/sizeof(uint32_t)]
  53. __attribute__((aligned(PAL_XSTATE_ALIGN))) = {
  54. 0x037F, 0, 0, 0, 0, 0, 0x1F80, 0xFFFF, 0, 0, 0, 0, 0, 0, 0, 0,
  55. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  56. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  57. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  58. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  59. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  60. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  61. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  62. 0, 0, 0, 0x80000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // XCOMP_BV[63] = 1, compaction mode
  63. };
  64. void init_xsave_size(uint64_t xfrm) {
  65. const struct {
  66. uint64_t bits;
  67. uint32_t size;
  68. } xsave_size_table[] = { // Note that the xsave_size should be in ascending order
  69. {SGX_XFRM_LEGACY, 512 + 64}, // 512 for legacy features, 64 for xsave header
  70. {SGX_XFRM_AVX, 512 + 64 + 256}, // 256 for YMM0_H - YMM15_H registers
  71. {SGX_XFRM_MPX, 512 + 64 + 256 + 256}, // 256 for MPX
  72. {SGX_XFRM_AVX512, 512 + 64 + 256 + 256 + 1600}, // 1600 for k0 - k7, ZMM0_H - ZMM15_H, ZMM16 - ZMM31
  73. };
  74. /* fxsave/fxrstore as fallback */
  75. xsave_enabled = 0;
  76. xsave_features = PAL_XFEATURE_MASK_FPSSE;
  77. xsave_size = 512 + 64;
  78. if (!xfrm || (xfrm & SGX_XFRM_RESERVED)) {
  79. SGX_DBG(DBG_M, "xsave is disabled, xfrm 0x%lx\n", xfrm);
  80. return;
  81. }
  82. xsave_enabled = (xfrm == SGX_XFRM_LEGACY) ? 0 : 1;
  83. for (size_t i = 0; i < ARRAY_SIZE(xsave_size_table); i++) {
  84. if ((xfrm & xsave_size_table[i].bits) == xsave_size_table[i].bits) {
  85. xsave_features = xfrm;
  86. xsave_size = xsave_size_table[i].size;
  87. }
  88. }
  89. SGX_DBG(DBG_M, "xsave is enabled with xsave_size: %u\n", xsave_size);
  90. }