setcontext.S 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2007 Google, Inc
  3. Contributed by Arun Sharma <arun.sharma@google.com>
  4. Copyright (C) 2010 Konstantin Belousov <kib@freebsd.org>
  5. This file is part of libunwind.
  6. Permission is hereby granted, free of charge, to any person obtaining
  7. a copy of this software and associated documentation files (the
  8. "Software"), to deal in the Software without restriction, including
  9. without limitation the rights to use, copy, modify, merge, publish,
  10. distribute, sublicense, and/or sell copies of the Software, and to
  11. permit persons to whom the Software is furnished to do so, subject to
  12. the following conditions:
  13. The above copyright notice and this permission notice shall be
  14. included in all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  22. #include "ucontext_i.h"
  23. #if (!ISE)
  24. #if defined __linux__
  25. #include <asm/unistd.h>
  26. #define SIG_SETMASK 2
  27. #define SIGSET_BYTE_SIZE (64/8)
  28. #elif defined __FreeBSD__
  29. #include <sys/syscall.h>
  30. #endif
  31. #endif
  32. /* int _Ux86_64_setcontext (const ucontext_t *ucp)
  33. Restores the machine context provided.
  34. Unlike the libc implementation, doesn't clobber %rax
  35. */
  36. .global _Ux86_64_setcontext
  37. .type _Ux86_64_setcontext, @function
  38. _Ux86_64_setcontext:
  39. #if defined __linux__
  40. #if (!ISE)
  41. /* restore signal mask
  42. sigprocmask(SIG_SETMASK, ucp->uc_sigmask, NULL, sizeof(sigset_t)) */
  43. push %rdi
  44. mov $__NR_rt_sigprocmask, %rax
  45. lea UC_SIGMASK(%rdi), %rsi
  46. mov $SIG_SETMASK, %rdi
  47. xor %rdx, %rdx
  48. mov $SIGSET_BYTE_SIZE, %r10
  49. syscall
  50. pop %rdi
  51. #endif
  52. /* restore fp state */
  53. mov UC_MCONTEXT_FPREGS_PTR(%rdi),%r8
  54. fldenv (%r8)
  55. ldmxcsr FPREGS_OFFSET_MXCSR(%r8)
  56. #elif defined __FreeBSD__
  57. #if (!ISE)
  58. /* restore signal mask */
  59. pushq %rdi
  60. xorl %edx,%edx
  61. leaq UC_SIGMASK(%rdi),%rsi
  62. movl $3,%edi/* SIG_SETMASK */
  63. movl $SYS_sigprocmask,%eax
  64. movq %rcx,%r10
  65. syscall
  66. popq %rdi
  67. #endif
  68. /* restore fp state */
  69. cmpq $UC_MCONTEXT_FPOWNED_FPU,UC_MCONTEXT_OWNEDFP(%rdi)
  70. jne 1f
  71. cmpq $UC_MCONTEXT_FPFMT_XMM,UC_MCONTEXT_FPFORMAT(%rdi)
  72. jne 1f
  73. fxrstor UC_MCONTEXT_FPSTATE(%rdi)
  74. 1:
  75. #else
  76. #error Port me
  77. #endif
  78. /* restore the rest of the state */
  79. mov UC_MCONTEXT_GREGS_R8(%rdi),%r8
  80. mov UC_MCONTEXT_GREGS_R9(%rdi),%r9
  81. mov UC_MCONTEXT_GREGS_RBX(%rdi),%rbx
  82. mov UC_MCONTEXT_GREGS_RBP(%rdi),%rbp
  83. mov UC_MCONTEXT_GREGS_R12(%rdi),%r12
  84. mov UC_MCONTEXT_GREGS_R13(%rdi),%r13
  85. mov UC_MCONTEXT_GREGS_R14(%rdi),%r14
  86. mov UC_MCONTEXT_GREGS_R15(%rdi),%r15
  87. mov UC_MCONTEXT_GREGS_RSI(%rdi),%rsi
  88. mov UC_MCONTEXT_GREGS_RDX(%rdi),%rdx
  89. mov UC_MCONTEXT_GREGS_RAX(%rdi),%rax
  90. mov UC_MCONTEXT_GREGS_RCX(%rdi),%rcx
  91. mov UC_MCONTEXT_GREGS_RSP(%rdi),%rsp
  92. /* push the return address on the stack */
  93. mov UC_MCONTEXT_GREGS_RIP(%rdi),%rcx
  94. push %rcx
  95. mov UC_MCONTEXT_GREGS_RCX(%rdi),%rcx
  96. mov UC_MCONTEXT_GREGS_RDI(%rdi),%rdi
  97. retq
  98. .size _Ux86_64_setcontext, . - _Ux86_64_setcontext
  99. /* We do not need executable stack. */
  100. .section .note.GNU-stack,"",@progbits