_setjmp.S 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* $NetBSD: _setjmp.S,v 1.9 2014/05/23 02:34:19 uebayasi Exp $ */
  2. /*-
  3. * Copyright (c) 1990 The Regents of the University of California.
  4. * All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * William Jolitz.
  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. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * from: @(#)_setjmp.s 5.1 (Berkeley) 4/23/90
  34. */
  35. .file "_setjmp.S"
  36. #include "linux-regs.h"
  37. #if defined(LIBC_SCCS)
  38. RCSID("$NetBSD: _setjmp.S,v 1.9 2014/05/23 02:34:19 uebayasi Exp $")
  39. #endif
  40. /*
  41. * C library -- setjmp, longjmp
  42. *
  43. * longjmp(a,v)
  44. * will generate a "return(v)" from the last call to
  45. * setjmp(a)
  46. * by restoring registers from the stack.
  47. * The previous signal state is NOT restored.
  48. */
  49. #include "../trts/linux/trts_pic.h"
  50. .text
  51. #ifdef LINUX32
  52. #define _JB_PC 0
  53. #define _JB_EBX 1
  54. #define _JB_ESP 2
  55. #define _JB_EBP 3
  56. #define _JB_ESI 4
  57. #define _JB_EDI 5
  58. #endif
  59. #ifdef LINUX64
  60. #define _JB_RBX 0
  61. #define _JB_RBP 1
  62. #define _JB_R12 2
  63. #define _JB_R13 3
  64. #define _JB_R14 4
  65. #define _JB_R15 5
  66. #define _JB_RSP 6
  67. #define _JB_PC 7
  68. #endif
  69. .macro PUSHAQ
  70. push %rax
  71. push %rbx
  72. push %rcx
  73. push %rdx
  74. push %rsi
  75. push %rdi
  76. push %r8
  77. push %r9
  78. push %r10
  79. push %r11
  80. push %r12
  81. push %r13
  82. push %r14
  83. push %r15
  84. .endm
  85. .macro POPAQ
  86. pop %r15
  87. pop %r14
  88. pop %r13
  89. pop %r12
  90. pop %r11
  91. pop %r10
  92. pop %r9
  93. pop %r8
  94. pop %rdi
  95. pop %rsi
  96. pop %rdx
  97. pop %rcx
  98. pop %rbx
  99. pop %rax
  100. .endm
  101. DECLARE_GLOBAL_FUNC setjmp
  102. #ifdef LINUX32
  103. PUSHAL
  104. /* check the buf is within the enclave */
  105. movl (SE_WORDSIZE + 8*SE_WORDSIZE)(%esp), %eax
  106. pushl $SE_WORDSIZE
  107. pushl %eax
  108. call sgx_is_within_enclave
  109. cmpl $0, %eax
  110. jz .crash
  111. addl $(2*SE_WORDSIZE), %esp
  112. POPAL
  113. /* store the registers */
  114. movl SE_WORDSIZE(%esp),%eax
  115. movl 0(%esp),%edx
  116. movl %edx, (_JB_PC * SE_WORDSIZE)(%eax) /* rta */
  117. movl %ebx, (_JB_EBX * SE_WORDSIZE)(%eax)
  118. movl %esp, (_JB_ESP * SE_WORDSIZE)(%eax)
  119. movl %ebp, (_JB_EBP * SE_WORDSIZE)(%eax)
  120. movl %esi, (_JB_ESI * SE_WORDSIZE)(%eax)
  121. movl %edi, (_JB_EDI * SE_WORDSIZE)(%eax)
  122. movl %eax, %edx
  123. /* use statck_guard as cookie*/
  124. call get_stack_guard
  125. xchg %eax, %edx
  126. xorl %edx, (_JB_PC * SE_WORDSIZE)(%eax)
  127. xorl %edx, (_JB_EBX * SE_WORDSIZE)(%eax)
  128. xorl %edx, (_JB_ESP * SE_WORDSIZE)(%eax)
  129. xorl %edx, (_JB_EBP * SE_WORDSIZE)(%eax)
  130. xorl %edx, (_JB_ESI * SE_WORDSIZE)(%eax)
  131. xorl %edx, (_JB_EDI * SE_WORDSIZE)(%eax)
  132. #endif
  133. #ifdef LINUX64
  134. PUSHAQ
  135. /* check the buf is within the enclave */
  136. movq $SE_WORDSIZE, %rsi
  137. call sgx_is_within_enclave
  138. cmpl $0, %eax
  139. jz .crash
  140. POPAQ
  141. /* store the registers */
  142. movq (%rsp),%r11
  143. movq %rbx, (_JB_RBX * SE_WORDSIZE)(%rdi)
  144. movq %rbp, (_JB_RBP * SE_WORDSIZE)(%rdi)
  145. movq %r12, (_JB_R12 * SE_WORDSIZE)(%rdi)
  146. movq %r13, (_JB_R13 * SE_WORDSIZE)(%rdi)
  147. movq %r14, (_JB_R14 * SE_WORDSIZE)(%rdi)
  148. movq %r15, (_JB_R15 * SE_WORDSIZE)(%rdi)
  149. movq %rsp, (_JB_RSP * SE_WORDSIZE)(%rdi)
  150. movq %r11, (_JB_PC * SE_WORDSIZE)(%rdi)
  151. /* use statck_guard as cookie*/
  152. call get_stack_guard
  153. xorq %rax, (_JB_RBX * SE_WORDSIZE)(%rdi)
  154. xorq %rax, (_JB_RBP * SE_WORDSIZE)(%rdi)
  155. xorq %rax, (_JB_R12 * SE_WORDSIZE)(%rdi)
  156. xorq %rax, (_JB_R13 * SE_WORDSIZE)(%rdi)
  157. xorq %rax, (_JB_R14 * SE_WORDSIZE)(%rdi)
  158. xorq %rax, (_JB_R15 * SE_WORDSIZE)(%rdi)
  159. xorq %rax, (_JB_RSP * SE_WORDSIZE)(%rdi)
  160. xorq %rax, (_JB_PC * SE_WORDSIZE)(%rdi)
  161. #endif
  162. xorl %eax,%eax
  163. ret
  164. .crash:
  165. ud2
  166. DECLARE_GLOBAL_FUNC longjmp
  167. #ifdef LINUX32
  168. PUSHAL
  169. /* check the buf is within the enclave */
  170. movl (SE_WORDSIZE + 8*SE_WORDSIZE)(%esp), %eax
  171. pushl $SE_WORDSIZE
  172. pushl %eax
  173. call sgx_is_within_enclave
  174. cmpl $0, %eax
  175. jz .crash
  176. addl $(2*SE_WORDSIZE), %esp
  177. /* restore xsp */
  178. movl (SE_WORDSIZE + 8*SE_WORDSIZE)(%esp), %eax
  179. movl (_JB_ESP * SE_WORDSIZE)(%eax), %ebx
  180. call get_stack_guard
  181. xorl %eax, %ebx
  182. pushl %ebx
  183. /* check restored esp is on current statck */
  184. call is_valid_sp
  185. cmpl $0, %eax
  186. jz .crash
  187. popl %ebx
  188. POPAL
  189. /* restore the registers */
  190. movl SE_WORDSIZE(%esp),%edx
  191. movl (SE_WORDSIZE*2)(%esp),%eax
  192. pushl %eax
  193. movl (_JB_PC * SE_WORDSIZE)(%edx),%ecx
  194. movl (_JB_EBX * SE_WORDSIZE)(%edx),%ebx
  195. pushl (_JB_ESP * SE_WORDSIZE)(%edx)
  196. pushl (_JB_EBP * SE_WORDSIZE)(%edx)
  197. movl (_JB_ESI * SE_WORDSIZE)(%edx),%esi
  198. movl (_JB_EDI * SE_WORDSIZE)(%edx),%edi
  199. call get_stack_guard
  200. xorl %eax, %ecx
  201. xorl %eax, %ebx
  202. movl (0)(%esp), %edx
  203. xorl %eax, %edx
  204. movl %edx, (0)(%esp)
  205. movl (SE_WORDSIZE)(%esp), %edx
  206. xorl %eax, %edx
  207. movl %edx, (SE_WORDSIZE)(%esp)
  208. xorl %eax, %esi
  209. xorl %eax, %edi
  210. popl %ebp
  211. popl %edx
  212. movl %ecx, (0)(%edx)
  213. popl %eax
  214. movl %edx, %esp
  215. #endif
  216. #ifdef LINUX64
  217. PUSHAQ
  218. pushq %rdi
  219. /* check the buf is within the enclave */
  220. movq $SE_WORDSIZE, %rsi
  221. call sgx_is_within_enclave
  222. cmpl $0, %eax
  223. jz .crash
  224. popq %rdi
  225. /* restore xsp*/
  226. movq (_JB_RSP * SE_WORDSIZE)(%rdi),%rdx
  227. call get_stack_guard
  228. xorq %rax, %rdx
  229. pushq %rdx
  230. /* check restored rsp is on current statck */
  231. popq %rdi
  232. call is_valid_sp
  233. cmpl $0, %eax
  234. jz .crash
  235. POPAQ
  236. /* restore the registers */
  237. movl %esi,%eax
  238. movq (_JB_RBX * SE_WORDSIZE)(%rdi),%rbx
  239. movq (_JB_RBP * SE_WORDSIZE)(%rdi),%rsi
  240. movq (_JB_R12 * SE_WORDSIZE)(%rdi),%r12
  241. movq (_JB_R13 * SE_WORDSIZE)(%rdi),%r13
  242. movq (_JB_R14 * SE_WORDSIZE)(%rdi),%r14
  243. movq (_JB_R15 * SE_WORDSIZE)(%rdi),%r15
  244. movq (_JB_RSP * SE_WORDSIZE)(%rdi),%rdx
  245. movq (_JB_PC * SE_WORDSIZE)(%rdi),%rcx
  246. pushq %rax
  247. call get_stack_guard
  248. xorq %rax, %rbx
  249. xorq %rax, %rsi
  250. xorq %rax, %r12
  251. xorq %rax, %r13
  252. xorq %rax, %r14
  253. xorq %rax, %r15
  254. xorq %rax, %rdx
  255. xorq %rax, %rcx
  256. popq %rax
  257. movq %rsi, %rbp
  258. movq %rcx, 0(%rdx)
  259. movq %rdx, %rsp
  260. #endif
  261. testl %eax,%eax
  262. jnz 1f
  263. incl %eax
  264. 1: ret
  265. DECLARE_GLOBAL_FUNC set_sgx_tlongjmp_version
  266. lea_pic sgx_tsetjmp_version, %xax
  267. ret
  268. .weak _setjmp
  269. _setjmp=setjmp
  270. .weak _longjmp
  271. _longjmp=longjmp