linux-regs.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. /*
  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_ECREATE 0
  79. #define SE_EADD 1
  80. #define SE_EINIT 2
  81. #define SE_EREMOVE 3
  82. /*
  83. * Macros for GNU assembly
  84. */
  85. .macro ENCLU
  86. #ifdef SE_SIM
  87. cmp $SE_EEXIT, %xax
  88. jne 1f
  89. /* if leaf is EEXIT, xbp and xsp need to be passed by xdx and xcx */
  90. mov %xbp, %xdx
  91. mov %xsp, %xcx
  92. 1:
  93. push %xdi
  94. push %xsi
  95. push %xdx
  96. push %xcx
  97. push %xbx
  98. push %xax
  99. # ifdef LINUX64
  100. pop %rdi
  101. pop %rsi
  102. pop %rdx
  103. pop %rcx
  104. pop %r8
  105. pop %r9
  106. # endif
  107. .type _SE3,@function
  108. .protected _SE3
  109. call _SE3
  110. # ifdef LINUX32
  111. add $(SE_WORDSIZE * 6), %esp
  112. # endif
  113. #else /* SE_SIM */
  114. .byte 0x0f, 0x01, 0xd7 /* 0xf3 */
  115. #endif /* !SE_SIM */
  116. .endm
  117. /* declare a function with default visibility */
  118. .macro DECLARE_GLOBAL_FUNC name
  119. .globl \name
  120. .type \name, @function
  121. \name:
  122. .endm
  123. /* declare a function with visibility='hidden' */
  124. .macro DECLARE_LOCAL_FUNC name
  125. .globl \name
  126. .hidden \name
  127. .type \name, @function
  128. \name:
  129. .endm
  130. .macro NAKED_PROLOG
  131. push %xbp
  132. mov %xsp, %xbp
  133. sub $(7 * SE_WORDSIZE), %xsp
  134. .endm
  135. .macro NAKED_EPILOG
  136. mov %xbp, %xsp
  137. pop %xbp
  138. .endm
  139. /* `paramN' (N = 1,2,3,4) should be registers. */
  140. .macro SET_PARAMS param1:req, param2, param3, param4
  141. #if defined(LINUX32)
  142. .ifnb \param4
  143. mov \param4, 3*SE_WORDSIZE(%esp)
  144. .endif
  145. .ifnb \param3
  146. mov \param3, 2*SE_WORDSIZE(%esp)
  147. .endif
  148. .ifnb \param2
  149. mov \param2, 1*SE_WORDSIZE(%esp)
  150. .endif
  151. mov \param1, 0*SE_WORDSIZE(%esp)
  152. #else /* LINUX32 */
  153. .ifnb \param4
  154. .ifnc \param4, %rcx
  155. mov \param4, %rcx
  156. .endif
  157. .endif
  158. .ifnb \param3
  159. .ifnc \param3, %rdx
  160. mov \param3, %rdx
  161. .endif
  162. .endif
  163. .ifnb \param2
  164. .ifnc \param2, %rsi
  165. mov \param2, %rsi
  166. .endif
  167. .endif
  168. .ifnc \param1, %rdi
  169. mov \param1, %rdi
  170. .endif
  171. #endif /* LINUX64 */
  172. .endm
  173. /*******************************************************************/
  174. .macro SE_PROLOG
  175. .cfi_startproc
  176. #ifdef LINUX32
  177. pushl %ebp
  178. movl %esp, %ebp
  179. #endif
  180. push %xbx
  181. push %xcx
  182. push %xdx
  183. #if defined LINUX64
  184. movq %rdi, %rbx
  185. movq %rsi, %rcx
  186. /* rdx remains the same, rdi/rsi is not used by _SE0
  187. */
  188. #elif defined LINUX32
  189. movl 2*SE_WORDSIZE(%ebp), %ebx
  190. movl 3*SE_WORDSIZE(%ebp), %ecx
  191. movl 4*SE_WORDSIZE(%ebp), %edx
  192. #endif
  193. .endm
  194. /*******************************************************************/
  195. .macro SE_EPILOG
  196. pop %xdx
  197. pop %xcx
  198. pop %xbx
  199. #ifdef LINUX32
  200. movl %ebp, %esp
  201. popl %ebp
  202. #endif
  203. ret
  204. .cfi_endproc
  205. .endm
  206. /*******************************************************************/
  207. /* load the address of `symbol' to the register `reg' in PIC way. */
  208. .macro lea_pic symbol, reg
  209. #ifdef LINUX64
  210. lea \symbol(%rip), \reg
  211. #else
  212. /* The real code on x86 would look like this (get `bar' from `foo'):
  213. *
  214. * 00000198 <bar>:
  215. * 198: c3 ret
  216. *
  217. * 00000199 <foo>:
  218. * 199: e8 00 00 00 00 call 19e <foo+0x5>
  219. * 19e: 58 pop %eax
  220. * 19f: 8d 40 fa lea -0x6(%eax),%eax
  221. */
  222. call . + 0x5 /* No label here to avoid interfering w/ calling code */
  223. pop \reg
  224. lea (\symbol - . + 1)(\reg), \reg
  225. #endif
  226. .endm
  227. #endif /* LINUX_REGS_H__ */