linux-regs.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. * This header wraps the register names for x86/x64.
  33. */
  34. #ifndef LINUX_REGS_H__
  35. #define LINUX_REGS_H__
  36. #if defined(__i386) || defined(__i386__)
  37. # define LINUX32 1
  38. # define SE_WORDSIZE 4
  39. /* Generic argument picker for `naked' functions */
  40. # define naked_arg0 4(%esp)
  41. # define naked_arg1 8(%esp)
  42. # define naked_arg2 12(%esp)
  43. # define naked_arg3 16(%esp)
  44. # define xax eax
  45. # define xbx ebx
  46. # define xcx ecx
  47. # define xdx edx
  48. # define xsi esi
  49. # define xdi edi
  50. # define xbp ebp
  51. # define xsp esp
  52. #elif defined(__x86_64) || defined(__x86_64__)
  53. # define LINUX64 1
  54. # define SE_WORDSIZE 8
  55. /* For x86_64, the first six parameters are passed by
  56. * rdi, rsi, rdx, rcx, r8, r9.
  57. */
  58. # define naked_arg0 %rdi
  59. # define naked_arg1 %rsi
  60. # define naked_arg2 %rdx
  61. # define naked_arg3 %rcx
  62. # define xax rax
  63. # define xbx rbx
  64. # define xcx rcx
  65. # define xdx rdx
  66. # define xsi rsi
  67. # define xdi rdi
  68. # define xbp rbp
  69. # define xsp rsp
  70. #else
  71. # error unknown platform!
  72. #endif
  73. /* SE instructions - needs to be sync-up with inst70.h */
  74. #define SE_EREPORT 0
  75. #define SE_EGETKEY 1
  76. #define SE_EENTER 2
  77. #define SE_EEXIT 4
  78. #define SE_EACCEPT 5
  79. #define SE_EMODPE 6
  80. #define SE_ECREATE 0
  81. #define SE_EADD 1
  82. #define SE_EINIT 2
  83. #define SE_EREMOVE 3
  84. /*
  85. * Macros for GNU assembly
  86. */
  87. .macro ENCLU
  88. #ifdef SE_SIM
  89. cmp $SE_EEXIT, %xax
  90. jne 1f
  91. /* if leaf is EEXIT, xbp and xsp need to be passed by xdx and xcx */
  92. mov %xbp, %xdx
  93. mov %xsp, %xcx
  94. 1:
  95. push %xdi
  96. push %xsi
  97. push %xdx
  98. push %xcx
  99. push %xbx
  100. push %xax
  101. # ifdef LINUX64
  102. pop %rdi
  103. pop %rsi
  104. pop %rdx
  105. pop %rcx
  106. pop %r8
  107. pop %r9
  108. # endif
  109. .type _SE3,@function
  110. .protected _SE3
  111. call _SE3
  112. # ifdef LINUX32
  113. add $(SE_WORDSIZE * 6), %esp
  114. # endif
  115. #else /* SE_SIM */
  116. .byte 0x0f, 0x01, 0xd7 /* 0xf3 */
  117. #endif /* !SE_SIM */
  118. .endm
  119. /* declare a function with default visibility */
  120. .macro DECLARE_GLOBAL_FUNC name
  121. .globl \name
  122. .type \name, @function
  123. \name:
  124. .endm
  125. /* declare a function with visibility='hidden' */
  126. .macro DECLARE_LOCAL_FUNC name
  127. .globl \name
  128. .hidden \name
  129. .type \name, @function
  130. \name:
  131. .endm
  132. .macro NAKED_PROLOG
  133. push %xbp
  134. mov %xsp, %xbp
  135. sub $(7 * SE_WORDSIZE), %xsp
  136. .endm
  137. .macro NAKED_EPILOG
  138. mov %xbp, %xsp
  139. pop %xbp
  140. .endm
  141. /* `paramN' (N = 1,2,3,4) should be registers. */
  142. .macro SET_PARAMS param1:req, param2, param3, param4
  143. #if defined(LINUX32)
  144. .ifnb \param4
  145. mov \param4, 3*SE_WORDSIZE(%esp)
  146. .endif
  147. .ifnb \param3
  148. mov \param3, 2*SE_WORDSIZE(%esp)
  149. .endif
  150. .ifnb \param2
  151. mov \param2, 1*SE_WORDSIZE(%esp)
  152. .endif
  153. mov \param1, 0*SE_WORDSIZE(%esp)
  154. #else /* LINUX32 */
  155. .ifnb \param4
  156. .ifnc \param4, %rcx
  157. mov \param4, %rcx
  158. .endif
  159. .endif
  160. .ifnb \param3
  161. .ifnc \param3, %rdx
  162. mov \param3, %rdx
  163. .endif
  164. .endif
  165. .ifnb \param2
  166. .ifnc \param2, %rsi
  167. mov \param2, %rsi
  168. .endif
  169. .endif
  170. .ifnc \param1, %rdi
  171. mov \param1, %rdi
  172. .endif
  173. #endif /* LINUX64 */
  174. .endm
  175. /*******************************************************************/
  176. .macro SE_PROLOG
  177. .cfi_startproc
  178. #ifdef LINUX32
  179. pushl %ebp
  180. movl %esp, %ebp
  181. #endif
  182. push %xbx
  183. push %xcx
  184. push %xdx
  185. #if defined LINUX64
  186. movq %rdi, %rbx
  187. movq %rsi, %rcx
  188. /* rdx remains the same, rdi/rsi is not used by _SE0
  189. */
  190. #elif defined LINUX32
  191. movl 2*SE_WORDSIZE(%ebp), %ebx
  192. movl 3*SE_WORDSIZE(%ebp), %ecx
  193. movl 4*SE_WORDSIZE(%ebp), %edx
  194. #endif
  195. .endm
  196. /*******************************************************************/
  197. .macro SE_EPILOG
  198. pop %xdx
  199. pop %xcx
  200. pop %xbx
  201. #ifdef LINUX32
  202. movl %ebp, %esp
  203. popl %ebp
  204. #endif
  205. ret
  206. .cfi_endproc
  207. .endm
  208. /*******************************************************************/
  209. /* load the address of `symbol' to the register `reg' in PIC way. */
  210. .macro lea_pic symbol, reg
  211. #ifdef LINUX64
  212. lea \symbol(%rip), \reg
  213. #else
  214. /* The real code on x86 would look like this (get `bar' from `foo'):
  215. *
  216. * 00000198 <bar>:
  217. * 198: c3 ret
  218. *
  219. * 00000199 <foo>:
  220. * 199: e8 00 00 00 00 call 19e <foo+0x5>
  221. * 19e: 58 pop %eax
  222. * 19f: 8d 40 fa lea -0x6(%eax),%eax
  223. */
  224. call . + 0x5 /* No label here to avoid interfering w/ calling code */
  225. pop \reg
  226. lea (\symbol - . + 1)(\reg), \reg
  227. #endif
  228. .endm
  229. #endif /* LINUX_REGS_H__ */