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 6This file is part of libunwind. 7 8Permission is hereby granted, free of charge, to any person obtaining 9a copy of this software and associated documentation files (the 10"Software"), to deal in the Software without restriction, including 11without limitation the rights to use, copy, modify, merge, publish, 12distribute, sublicense, and/or sell copies of the Software, and to 13permit persons to whom the Software is furnished to do so, subject to 14the following conditions: 15 16The above copyright notice and this permission notice shall be 17included in all copies or substantial portions of the Software. 18 19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 23LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ 26 27#include "ucontext_i.h" 28 29/* int _Ux86_64_getcontext (ucontext_t *ucp) 30 31 Saves the machine context in UCP necessary for libunwind. 32 Unlike the libc implementation, we don't save the signal mask 33 and hence avoid the cost of a system call per unwind. 34 35*/ 36 37 .global _Ux86_64_getcontext 38 .type _Ux86_64_getcontext, @function 39_Ux86_64_getcontext: 40 .cfi_startproc 41 42 /* Callee saved: RBX, RBP, R12-R15 */ 43 movq %r12, UC_MCONTEXT_GREGS_R12(%rdi) 44 movq %r13, UC_MCONTEXT_GREGS_R13(%rdi) 45 movq %r14, UC_MCONTEXT_GREGS_R14(%rdi) 46 movq %r15, UC_MCONTEXT_GREGS_R15(%rdi) 47 movq %rbp, UC_MCONTEXT_GREGS_RBP(%rdi) 48 movq %rbx, UC_MCONTEXT_GREGS_RBX(%rdi) 49 50 /* Save argument registers (not strictly needed, but setcontext 51 restores them, so don't restore garbage). */ 52 movq %r8, UC_MCONTEXT_GREGS_R8(%rdi) 53 movq %r9, UC_MCONTEXT_GREGS_R9(%rdi) 54 movq %rdi, UC_MCONTEXT_GREGS_RDI(%rdi) 55 movq %rsi, UC_MCONTEXT_GREGS_RSI(%rdi) 56 movq %rdx, UC_MCONTEXT_GREGS_RDX(%rdi) 57 movq %rax, UC_MCONTEXT_GREGS_RAX(%rdi) 58 movq %rcx, UC_MCONTEXT_GREGS_RCX(%rdi) 59 60#if defined __linux__ 61 /* Save fp state (not needed, except for setcontext not 62 restoring garbage). */ 63 leaq UC_MCONTEXT_FPREGS_MEM(%rdi),%r8 64 movq %r8, UC_MCONTEXT_FPREGS_PTR(%rdi) 65 fnstenv (%r8) 66 stmxcsr FPREGS_OFFSET_MXCSR(%r8) 67#elif defined __FreeBSD__ 68 fxsave UC_MCONTEXT_FPSTATE(%rdi) 69 movq $UC_MCONTEXT_FPOWNED_FPU,UC_MCONTEXT_OWNEDFP(%rdi) 70 movq $UC_MCONTEXT_FPFMT_XMM,UC_MCONTEXT_FPFORMAT(%rdi) 71 /* Save rflags and segment registers, so that sigreturn(2) 72 does not complain. */ 73 pushfq 74 .cfi_adjust_cfa_offset 8 75 popq UC_MCONTEXT_RFLAGS(%rdi) 76 .cfi_adjust_cfa_offset -8 77 movl $0, UC_MCONTEXT_FLAGS(%rdi) 78 movw %cs, UC_MCONTEXT_CS(%rdi) 79 movw %ss, UC_MCONTEXT_SS(%rdi) 80#if 0 81 /* Setting the flags to 0 above disables restore of segment 82 registers from the context */ 83 movw %ds, UC_MCONTEXT_DS(%rdi) 84 movw %es, UC_MCONTEXT_ES(%rdi) 85 movw %fs, UC_MCONTEXT_FS(%rdi) 86 movw %gs, UC_MCONTEXT_GS(%rdi) 87#endif 88 movq $UC_MCONTEXT_MC_LEN_VAL, UC_MCONTEXT_MC_LEN(%rdi) 89#else 90#error Port me 91#endif 92 93 leaq 8(%rsp), %rax /* exclude this call. */ 94 movq %rax, UC_MCONTEXT_GREGS_RSP(%rdi) 95 96 movq 0(%rsp), %rax 97 movq %rax, UC_MCONTEXT_GREGS_RIP(%rdi) 98 99 xorq %rax, %rax 100 retq 101 .cfi_endproc 102 .size _Ux86_64_getcontext, . - _Ux86_64_getcontext 103 104/* int _Ux86_64_getcontext_trace (ucontext_t *ucp) 105 106 Saves limited machine context in UCP necessary for libunwind. 107 Unlike _Ux86_64_getcontext, saves only the parts needed for 108 fast trace. If fast trace fails, caller will have to get the 109 full context. 110*/ 111 112 .global _Ux86_64_getcontext_trace 113 .hidden _Ux86_64_getcontext_trace 114 .type _Ux86_64_getcontext_trace, @function 115_Ux86_64_getcontext_trace: 116 .cfi_startproc 117 118 /* Save only RBP, RBX, RSP, RIP - exclude this call. */ 119 movq %rbp, UC_MCONTEXT_GREGS_RBP(%rdi) 120 movq %rbx, UC_MCONTEXT_GREGS_RBX(%rdi) 121 122 leaq 8(%rsp), %rax 123 movq %rax, UC_MCONTEXT_GREGS_RSP(%rdi) 124 125 movq 0(%rsp), %rax 126 movq %rax, UC_MCONTEXT_GREGS_RIP(%rdi) 127 128 xorq %rax, %rax 129 retq 130 .cfi_endproc 131 .size _Ux86_64_getcontext_trace, . - _Ux86_64_getcontext_trace 132 133 /* We do not need executable stack. */ 134 .section .note.GNU-stack,"",@progbits 135