enter_enclave.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. /* ------------------------------------------------------------
  32. * Normal function enter/leave wrappers.
  33. * ------------------------------------------------------------
  34. */
  35. #ifndef _ENTER_ENCLAVE_H_
  36. #define _ENTER_ENCLAVE_H_
  37. #include "linux-regs.h"
  38. #include "rts_cmd.h"
  39. .macro lea_symbol symbol, reg
  40. #ifdef __x86_64__
  41. mov \symbol@GOTPCREL(%rip), \reg
  42. #else
  43. lea \symbol, \reg
  44. #endif
  45. .endm
  46. /* macro for enter_enclave
  47. * There is no .cfi_xxx to describe unwind information, because we want c++ exception can't across enclave boundary
  48. */
  49. .macro EENTER_PROLOG
  50. push %xbp
  51. mov %xsp, %xbp
  52. /* save GPRs */
  53. #ifdef __i386__
  54. sub $(4 * SE_WORDSIZE), %xsp /* for xsave, xbx, xdi, xsi */
  55. mov %xbx, -2 * SE_WORDSIZE(%xbp)
  56. mov %xsi, -3 * SE_WORDSIZE(%xbp)
  57. mov %xdi, -4 * SE_WORDSIZE(%xbp)
  58. #else /* __x86_64__ */
  59. sub $(12 * SE_WORDSIZE), %xsp /* for xsave, params, and non-volatile GPRs */
  60. mov %xdi, -10 * SE_WORDSIZE(%xbp)
  61. mov %xsi, -9 * SE_WORDSIZE(%xbp)
  62. mov %rdx, -8 * SE_WORDSIZE(%xbp)
  63. mov %rcx, -7 * SE_WORDSIZE(%xbp)
  64. mov %r8, -6 * SE_WORDSIZE(%xbp)
  65. mov %xbx, -11 * SE_WORDSIZE(%xbp)
  66. mov %r12, -5 * SE_WORDSIZE(%xbp)
  67. mov %r13, -4 * SE_WORDSIZE(%xbp)
  68. mov %r14, -3 * SE_WORDSIZE(%xbp)
  69. mov %r15, -2 * SE_WORDSIZE(%xbp)
  70. #endif
  71. lea_symbol g_xsave_size, %xdi
  72. xor %xax, %xax
  73. movl (%xdi), %eax
  74. sub %xax, %xsp
  75. mov %xax, %xcx /* xsave size */
  76. mov $0x3f, %xax
  77. not %xax
  78. and %xax, %xsp /* xsave requires 64 byte aligned */
  79. mov %xsp, -1 * SE_WORDSIZE(%xbp) /* xsave pointer */
  80. /* shadow space for arguments */
  81. sub $(4 * SE_WORDSIZE), %xsp
  82. /* save extended xfeature registers */
  83. shr $2, %xcx
  84. xor %xax, %xax
  85. mov -1 * SE_WORDSIZE(%xbp), %xdi
  86. cld
  87. rep stos %eax, %es:(%xdi)
  88. mov -1 * SE_WORDSIZE(%xbp), %xdi
  89. mov %xdi, (%xsp)
  90. call save_xregs
  91. .endm
  92. .macro EENTER_EPILOG
  93. /* restore extended xfeature registers */
  94. mov -SE_WORDSIZE*1(%xbp), %xdi
  95. mov %xdi, (%xsp)
  96. call restore_xregs
  97. mov %xsi, %xax
  98. /* restore GPRs */
  99. #ifdef __i386__
  100. mov -SE_WORDSIZE*2(%xbp), %xbx
  101. mov -SE_WORDSIZE*3(%xbp), %xsi
  102. mov -SE_WORDSIZE*4(%xbp), %xdi
  103. #else
  104. mov -SE_WORDSIZE*11(%xbp), %xbx
  105. mov -SE_WORDSIZE*10(%xbp), %xdi
  106. mov -SE_WORDSIZE*9(%xbp), %xsi
  107. mov -SE_WORDSIZE*5(%rbp), %r12
  108. mov -SE_WORDSIZE*4(%rbp), %r13
  109. mov -SE_WORDSIZE*3(%rbp), %r14
  110. mov -SE_WORDSIZE*2(%rbp), %r15
  111. #endif
  112. /* don't need recover rdi, rsi, rdx, rcx */
  113. mov %xbp, %xsp
  114. pop %xbp
  115. ret
  116. .endm
  117. #if defined(__i386__)
  118. #define frame_arg0 2*SE_WORDSIZE(%ebp)
  119. #define frame_arg1 3*SE_WORDSIZE(%ebp)
  120. #define frame_arg2 4*SE_WORDSIZE(%ebp)
  121. #define frame_arg3 5*SE_WORDSIZE(%ebp)
  122. #define frame_arg4 6*SE_WORDSIZE(%ebp)
  123. #elif defined(__x86_64__)
  124. #define frame_arg0 -10*SE_WORDSIZE(%rbp)
  125. #define frame_arg1 -9*SE_WORDSIZE(%rbp)
  126. #define frame_arg2 -8*SE_WORDSIZE(%rbp)
  127. #define frame_arg3 -7*SE_WORDSIZE(%rbp)
  128. #define frame_arg4 -6*SE_WORDSIZE(%rbp)
  129. #else
  130. # error unknown platform
  131. #endif
  132. //refer sgx_error.h
  133. #define SE_ERROR_READ_LOCK_FAIL 0xc0002202
  134. #endif