_setjmp.S 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 xbp and xsp, push them to stack */
  178. movl (SE_WORDSIZE + 8*SE_WORDSIZE)(%esp), %eax
  179. movl (_JB_ESP * SE_WORDSIZE)(%eax), %ebx
  180. movl (_JB_EBP * SE_WORDSIZE)(%eax), %ecx
  181. call get_stack_guard
  182. xorl %eax, %ebx
  183. xorl %eax, %ecx
  184. pushl %ebx
  185. pushl %ecx
  186. /* check restored ebp is on current statck */
  187. call is_valid_sp
  188. cmpl $0, %eax
  189. jz .crash
  190. popl %ecx
  191. /* check restored esp is on current statck */
  192. call is_valid_sp
  193. cmpl $0, %eax
  194. jz .crash
  195. popl %ebx
  196. POPAL
  197. /* restore the registers */
  198. movl SE_WORDSIZE(%esp),%edx
  199. movl (SE_WORDSIZE*2)(%esp),%eax
  200. pushl %eax
  201. movl (_JB_PC * SE_WORDSIZE)(%edx),%ecx
  202. movl (_JB_EBX * SE_WORDSIZE)(%edx),%ebx
  203. pushl (_JB_ESP * SE_WORDSIZE)(%edx)
  204. pushl (_JB_EBP * SE_WORDSIZE)(%edx)
  205. movl (_JB_ESI * SE_WORDSIZE)(%edx),%esi
  206. movl (_JB_EDI * SE_WORDSIZE)(%edx),%edi
  207. call get_stack_guard
  208. xorl %eax, %ecx
  209. xorl %eax, %ebx
  210. movl (0)(%esp), %edx
  211. xorl %eax, %edx
  212. movl %edx, (0)(%esp)
  213. movl (SE_WORDSIZE)(%esp), %edx
  214. xorl %eax, %edx
  215. movl %edx, (SE_WORDSIZE)(%esp)
  216. xorl %eax, %esi
  217. xorl %eax, %edi
  218. popl %ebp
  219. popl %edx
  220. movl %ecx, (0)(%edx)
  221. popl %eax
  222. movl %edx, %esp
  223. #endif
  224. #ifdef LINUX64
  225. PUSHAQ
  226. pushq %rdi
  227. /* check the buf is within the enclave */
  228. movq $SE_WORDSIZE, %rsi
  229. call sgx_is_within_enclave
  230. cmpl $0, %eax
  231. jz .crash
  232. popq %rdi
  233. /* restore xbp and xsp, push them to stack */
  234. movq (_JB_RBP * SE_WORDSIZE)(%rdi),%rcx
  235. movq (_JB_RSP * SE_WORDSIZE)(%rdi),%rdx
  236. call get_stack_guard
  237. xorq %rax, %rcx
  238. xorq %rax, %rdx
  239. pushq %rdx
  240. pushq %rcx
  241. /* check restored rbp is on current statck */
  242. popq %rdi
  243. call is_valid_sp
  244. cmpl $0, %eax
  245. jz .crash
  246. /* check restored rsp is on current statck */
  247. popq %rdi
  248. call is_valid_sp
  249. cmpl $0, %eax
  250. jz .crash
  251. POPAQ
  252. /* restore the registers */
  253. movl %esi,%eax
  254. movq (_JB_RBX * SE_WORDSIZE)(%rdi),%rbx
  255. movq (_JB_RBP * SE_WORDSIZE)(%rdi),%rsi
  256. movq (_JB_R12 * SE_WORDSIZE)(%rdi),%r12
  257. movq (_JB_R13 * SE_WORDSIZE)(%rdi),%r13
  258. movq (_JB_R14 * SE_WORDSIZE)(%rdi),%r14
  259. movq (_JB_R15 * SE_WORDSIZE)(%rdi),%r15
  260. movq (_JB_RSP * SE_WORDSIZE)(%rdi),%rdx
  261. movq (_JB_PC * SE_WORDSIZE)(%rdi),%rcx
  262. pushq %rax
  263. call get_stack_guard
  264. xorq %rax, %rbx
  265. xorq %rax, %rsi
  266. xorq %rax, %r12
  267. xorq %rax, %r13
  268. xorq %rax, %r14
  269. xorq %rax, %r15
  270. xorq %rax, %rdx
  271. xorq %rax, %rcx
  272. popq %rax
  273. movq %rsi, %rbp
  274. movq %rcx, 0(%rdx)
  275. movq %rdx, %rsp
  276. #endif
  277. testl %eax,%eax
  278. jnz 1f
  279. incl %eax
  280. 1: ret
  281. DECLARE_GLOBAL_FUNC set_sgx_tlongjmp_version
  282. lea_pic sgx_tsetjmp_version, %xax
  283. ret
  284. .weak _setjmp
  285. _setjmp=setjmp
  286. .weak _longjmp
  287. _longjmp=longjmp