getcontext.S 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2008 Google, Inc
  3. Contributed by Paul Pluzhnikov <ppluzhnikov@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. /* int _Ux86_64_getcontext (ucontext_t *ucp)
  24. Saves the machine context in UCP necessary for libunwind.
  25. Unlike the libc implementation, we don't save the signal mask
  26. and hence avoid the cost of a system call per unwind.
  27. */
  28. .global _Ux86_64_getcontext
  29. .type _Ux86_64_getcontext, @function
  30. _Ux86_64_getcontext:
  31. .cfi_startproc
  32. /* Callee saved: RBX, RBP, R12-R15 */
  33. movq %r12, UC_MCONTEXT_GREGS_R12(%rdi)
  34. movq %r13, UC_MCONTEXT_GREGS_R13(%rdi)
  35. movq %r14, UC_MCONTEXT_GREGS_R14(%rdi)
  36. movq %r15, UC_MCONTEXT_GREGS_R15(%rdi)
  37. movq %rbp, UC_MCONTEXT_GREGS_RBP(%rdi)
  38. movq %rbx, UC_MCONTEXT_GREGS_RBX(%rdi)
  39. /* Save argument registers (not strictly needed, but setcontext
  40. restores them, so don't restore garbage). */
  41. movq %r8, UC_MCONTEXT_GREGS_R8(%rdi)
  42. movq %r9, UC_MCONTEXT_GREGS_R9(%rdi)
  43. movq %rdi, UC_MCONTEXT_GREGS_RDI(%rdi)
  44. movq %rsi, UC_MCONTEXT_GREGS_RSI(%rdi)
  45. movq %rdx, UC_MCONTEXT_GREGS_RDX(%rdi)
  46. movq %rax, UC_MCONTEXT_GREGS_RAX(%rdi)
  47. movq %rcx, UC_MCONTEXT_GREGS_RCX(%rdi)
  48. #if defined __linux__
  49. /* Save fp state (not needed, except for setcontext not
  50. restoring garbage). */
  51. leaq UC_MCONTEXT_FPREGS_MEM(%rdi),%r8
  52. movq %r8, UC_MCONTEXT_FPREGS_PTR(%rdi)
  53. fnstenv (%r8)
  54. stmxcsr FPREGS_OFFSET_MXCSR(%r8)
  55. #elif defined __FreeBSD__
  56. fxsave UC_MCONTEXT_FPSTATE(%rdi)
  57. movq $UC_MCONTEXT_FPOWNED_FPU,UC_MCONTEXT_OWNEDFP(%rdi)
  58. movq $UC_MCONTEXT_FPFMT_XMM,UC_MCONTEXT_FPFORMAT(%rdi)
  59. /* Save rflags and segment registers, so that sigreturn(2)
  60. does not complain. */
  61. pushfq
  62. .cfi_adjust_cfa_offset 8
  63. popq UC_MCONTEXT_RFLAGS(%rdi)
  64. .cfi_adjust_cfa_offset -8
  65. movl $0, UC_MCONTEXT_FLAGS(%rdi)
  66. movw %cs, UC_MCONTEXT_CS(%rdi)
  67. movw %ss, UC_MCONTEXT_SS(%rdi)
  68. #if 0
  69. /* Setting the flags to 0 above disables restore of segment
  70. registers from the context */
  71. movw %ds, UC_MCONTEXT_DS(%rdi)
  72. movw %es, UC_MCONTEXT_ES(%rdi)
  73. movw %fs, UC_MCONTEXT_FS(%rdi)
  74. movw %gs, UC_MCONTEXT_GS(%rdi)
  75. #endif
  76. movq $UC_MCONTEXT_MC_LEN_VAL, UC_MCONTEXT_MC_LEN(%rdi)
  77. #else
  78. #error Port me
  79. #endif
  80. leaq 8(%rsp), %rax /* exclude this call. */
  81. movq %rax, UC_MCONTEXT_GREGS_RSP(%rdi)
  82. movq 0(%rsp), %rax
  83. movq %rax, UC_MCONTEXT_GREGS_RIP(%rdi)
  84. xorq %rax, %rax
  85. retq
  86. .cfi_endproc
  87. .size _Ux86_64_getcontext, . - _Ux86_64_getcontext
  88. /* int _Ux86_64_getcontext_trace (ucontext_t *ucp)
  89. Saves limited machine context in UCP necessary for libunwind.
  90. Unlike _Ux86_64_getcontext, saves only the parts needed for
  91. fast trace. If fast trace fails, caller will have to get the
  92. full context.
  93. */
  94. .global _Ux86_64_getcontext_trace
  95. .hidden _Ux86_64_getcontext_trace
  96. .type _Ux86_64_getcontext_trace, @function
  97. _Ux86_64_getcontext_trace:
  98. .cfi_startproc
  99. /* Save only RBP, RBX, RSP, RIP - exclude this call. */
  100. movq %rbp, UC_MCONTEXT_GREGS_RBP(%rdi)
  101. movq %rbx, UC_MCONTEXT_GREGS_RBX(%rdi)
  102. leaq 8(%rsp), %rax
  103. movq %rax, UC_MCONTEXT_GREGS_RSP(%rdi)
  104. movq 0(%rsp), %rax
  105. movq %rax, UC_MCONTEXT_GREGS_RIP(%rdi)
  106. xorq %rax, %rax
  107. retq
  108. .cfi_endproc
  109. .size _Ux86_64_getcontext_trace, . - _Ux86_64_getcontext_trace
  110. /* We do not need executable stack. */
  111. .section .note.GNU-stack,"",@progbits